Edit File by line
/home/barbar84/public_h.../wp-admin
File: install-helper.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Plugins may load this file to gain access to special helper functions for
[2] Fix | Delete
* plugin installation. This file is not included by WordPress and it is
[3] Fix | Delete
* recommended, to prevent fatal errors, that this file is included using
[4] Fix | Delete
* require_once.
[5] Fix | Delete
*
[6] Fix | Delete
* These functions are not optimized for speed, but they should only be used
[7] Fix | Delete
* once in a while, so speed shouldn't be a concern. If it is and you are
[8] Fix | Delete
* needing to use these functions a lot, you might experience time outs. If you
[9] Fix | Delete
* do, then it is advised to just write the SQL code yourself.
[10] Fix | Delete
*
[11] Fix | Delete
* check_column( 'wp_links', 'link_description', 'mediumtext' );
[12] Fix | Delete
* if ( check_column( $wpdb->comments, 'comment_author', 'tinytext' ) ) {
[13] Fix | Delete
* echo "ok\n";
[14] Fix | Delete
* }
[15] Fix | Delete
*
[16] Fix | Delete
* $error_count = 0;
[17] Fix | Delete
* $tablename = $wpdb->links;
[18] Fix | Delete
* // Check the column.
[19] Fix | Delete
* if ( ! check_column( $wpdb->links, 'link_description', 'varchar( 255 )' ) ) {
[20] Fix | Delete
* $ddl = "ALTER TABLE $wpdb->links MODIFY COLUMN link_description varchar(255) NOT NULL DEFAULT '' ";
[21] Fix | Delete
* $q = $wpdb->query( $ddl );
[22] Fix | Delete
* }
[23] Fix | Delete
*
[24] Fix | Delete
* if ( check_column( $wpdb->links, 'link_description', 'varchar( 255 )' ) ) {
[25] Fix | Delete
* $res .= $tablename . ' - ok <br />';
[26] Fix | Delete
* } else {
[27] Fix | Delete
* $res .= 'There was a problem with ' . $tablename . '<br />';
[28] Fix | Delete
* ++$error_count;
[29] Fix | Delete
* }
[30] Fix | Delete
*
[31] Fix | Delete
* @package WordPress
[32] Fix | Delete
* @subpackage Plugin
[33] Fix | Delete
*/
[34] Fix | Delete
[35] Fix | Delete
/** Load WordPress Bootstrap */
[36] Fix | Delete
require_once dirname( __DIR__ ) . '/wp-load.php';
[37] Fix | Delete
[38] Fix | Delete
if ( ! function_exists( 'maybe_create_table' ) ) :
[39] Fix | Delete
/**
[40] Fix | Delete
* Creates a table in the database if it doesn't already exist.
[41] Fix | Delete
*
[42] Fix | Delete
* @since 1.0.0
[43] Fix | Delete
*
[44] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[45] Fix | Delete
*
[46] Fix | Delete
* @param string $table_name Database table name.
[47] Fix | Delete
* @param string $create_ddl SQL statement to create table.
[48] Fix | Delete
* @return bool True on success or if the table already exists. False on failure.
[49] Fix | Delete
*/
[50] Fix | Delete
function maybe_create_table( $table_name, $create_ddl ) {
[51] Fix | Delete
global $wpdb;
[52] Fix | Delete
[53] Fix | Delete
foreach ( $wpdb->get_col( 'SHOW TABLES', 0 ) as $table ) {
[54] Fix | Delete
if ( $table === $table_name ) {
[55] Fix | Delete
return true;
[56] Fix | Delete
}
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
// Didn't find it, so try to create it.
[60] Fix | Delete
$wpdb->query( $create_ddl );
[61] Fix | Delete
[62] Fix | Delete
// We cannot directly tell that whether this succeeded!
[63] Fix | Delete
foreach ( $wpdb->get_col( 'SHOW TABLES', 0 ) as $table ) {
[64] Fix | Delete
if ( $table === $table_name ) {
[65] Fix | Delete
return true;
[66] Fix | Delete
}
[67] Fix | Delete
}
[68] Fix | Delete
[69] Fix | Delete
return false;
[70] Fix | Delete
}
[71] Fix | Delete
endif;
[72] Fix | Delete
[73] Fix | Delete
if ( ! function_exists( 'maybe_add_column' ) ) :
[74] Fix | Delete
/**
[75] Fix | Delete
* Adds column to database table, if it doesn't already exist.
[76] Fix | Delete
*
[77] Fix | Delete
* @since 1.0.0
[78] Fix | Delete
*
[79] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[80] Fix | Delete
*
[81] Fix | Delete
* @param string $table_name Database table name.
[82] Fix | Delete
* @param string $column_name Table column name.
[83] Fix | Delete
* @param string $create_ddl SQL statement to add column.
[84] Fix | Delete
* @return bool True on success or if the column already exists. False on failure.
[85] Fix | Delete
*/
[86] Fix | Delete
function maybe_add_column( $table_name, $column_name, $create_ddl ) {
[87] Fix | Delete
global $wpdb;
[88] Fix | Delete
[89] Fix | Delete
foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
[90] Fix | Delete
if ( $column === $column_name ) {
[91] Fix | Delete
return true;
[92] Fix | Delete
}
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
// Didn't find it, so try to create it.
[96] Fix | Delete
$wpdb->query( $create_ddl );
[97] Fix | Delete
[98] Fix | Delete
// We cannot directly tell that whether this succeeded!
[99] Fix | Delete
foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
[100] Fix | Delete
if ( $column === $column_name ) {
[101] Fix | Delete
return true;
[102] Fix | Delete
}
[103] Fix | Delete
}
[104] Fix | Delete
[105] Fix | Delete
return false;
[106] Fix | Delete
}
[107] Fix | Delete
endif;
[108] Fix | Delete
[109] Fix | Delete
/**
[110] Fix | Delete
* Drops column from database table, if it exists.
[111] Fix | Delete
*
[112] Fix | Delete
* @since 1.0.0
[113] Fix | Delete
*
[114] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[115] Fix | Delete
*
[116] Fix | Delete
* @param string $table_name Database table name.
[117] Fix | Delete
* @param string $column_name Table column name.
[118] Fix | Delete
* @param string $drop_ddl SQL statement to drop column.
[119] Fix | Delete
* @return bool True on success or if the column doesn't exist. False on failure.
[120] Fix | Delete
*/
[121] Fix | Delete
function maybe_drop_column( $table_name, $column_name, $drop_ddl ) {
[122] Fix | Delete
global $wpdb;
[123] Fix | Delete
[124] Fix | Delete
foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
[125] Fix | Delete
if ( $column === $column_name ) {
[126] Fix | Delete
[127] Fix | Delete
// Found it, so try to drop it.
[128] Fix | Delete
$wpdb->query( $drop_ddl );
[129] Fix | Delete
[130] Fix | Delete
// We cannot directly tell that whether this succeeded!
[131] Fix | Delete
foreach ( $wpdb->get_col( "DESC $table_name", 0 ) as $column ) {
[132] Fix | Delete
if ( $column === $column_name ) {
[133] Fix | Delete
return false;
[134] Fix | Delete
}
[135] Fix | Delete
}
[136] Fix | Delete
}
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
// Else didn't find it.
[140] Fix | Delete
return true;
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
/**
[144] Fix | Delete
* Checks that database table column matches the criteria.
[145] Fix | Delete
*
[146] Fix | Delete
* Uses the SQL DESC for retrieving the table info for the column. It will help
[147] Fix | Delete
* understand the parameters, if you do more research on what column information
[148] Fix | Delete
* is returned by the SQL statement. Pass in null to skip checking that
[149] Fix | Delete
* criteria.
[150] Fix | Delete
*
[151] Fix | Delete
* Column names returned from DESC table are case sensitive and are listed:
[152] Fix | Delete
* Field
[153] Fix | Delete
* Type
[154] Fix | Delete
* Null
[155] Fix | Delete
* Key
[156] Fix | Delete
* Default
[157] Fix | Delete
* Extra
[158] Fix | Delete
*
[159] Fix | Delete
* @since 1.0.0
[160] Fix | Delete
*
[161] Fix | Delete
* @global wpdb $wpdb WordPress database abstraction object.
[162] Fix | Delete
*
[163] Fix | Delete
* @param string $table_name Database table name.
[164] Fix | Delete
* @param string $col_name Table column name.
[165] Fix | Delete
* @param string $col_type Table column type.
[166] Fix | Delete
* @param bool $is_null Optional. Check is null.
[167] Fix | Delete
* @param mixed $key Optional. Key info.
[168] Fix | Delete
* @param mixed $default Optional. Default value.
[169] Fix | Delete
* @param mixed $extra Optional. Extra value.
[170] Fix | Delete
* @return bool True, if matches. False, if not matching.
[171] Fix | Delete
*/
[172] Fix | Delete
function check_column( $table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null ) {
[173] Fix | Delete
global $wpdb;
[174] Fix | Delete
[175] Fix | Delete
$diffs = 0;
[176] Fix | Delete
$results = $wpdb->get_results( "DESC $table_name" );
[177] Fix | Delete
[178] Fix | Delete
foreach ( $results as $row ) {
[179] Fix | Delete
[180] Fix | Delete
if ( $row->Field === $col_name ) {
[181] Fix | Delete
[182] Fix | Delete
// Got our column, check the params.
[183] Fix | Delete
if ( ( null !== $col_type ) && ( $row->Type !== $col_type ) ) {
[184] Fix | Delete
++$diffs;
[185] Fix | Delete
}
[186] Fix | Delete
if ( ( null !== $is_null ) && ( $row->Null !== $is_null ) ) {
[187] Fix | Delete
++$diffs;
[188] Fix | Delete
}
[189] Fix | Delete
if ( ( null !== $key ) && ( $row->Key !== $key ) ) {
[190] Fix | Delete
++$diffs;
[191] Fix | Delete
}
[192] Fix | Delete
if ( ( null !== $default ) && ( $row->Default !== $default ) ) {
[193] Fix | Delete
++$diffs;
[194] Fix | Delete
}
[195] Fix | Delete
if ( ( null !== $extra ) && ( $row->Extra !== $extra ) ) {
[196] Fix | Delete
++$diffs;
[197] Fix | Delete
}
[198] Fix | Delete
[199] Fix | Delete
if ( $diffs > 0 ) {
[200] Fix | Delete
return false;
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
return true;
[204] Fix | Delete
} // End if found our column.
[205] Fix | Delete
}
[206] Fix | Delete
[207] Fix | Delete
return false;
[208] Fix | Delete
}
[209] Fix | Delete
[210] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function