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