| 1 |
<?php |
| 2 |
/** |
| 3 |
* @version 1.0 |
| 4 |
* @package Booking Calendar |
| 5 |
* @subpackage DB - checking if table, field or index exists |
| 6 |
* @category Functions |
| 7 |
* |
| 8 |
* @author wpdevelop |
| 9 |
* @link https://wpbookingcalendar.com/ |
| 10 |
* @email info@wpbookingcalendar.com |
| 11 |
* |
| 12 |
* @modified 2024-09-03 |
| 13 |
*/ |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 16 |
|
| 17 |
// ===================================================================================================================== |
| 18 |
// == DB - checking if table, field or index exists == |
| 19 |
// ===================================================================================================================== |
| 20 |
|
| 21 |
/** |
| 22 |
* Check if table exist |
| 23 |
* |
| 24 |
* @param string $tablename |
| 25 |
* |
| 26 |
* @return 0|1 |
| 27 |
* @global $wpdb |
| 28 |
*/ |
| 29 |
function wpbc_is_table_exists( $tablename ) { |
| 30 |
|
| 31 |
global $wpdb; |
| 32 |
|
| 33 |
if ( |
| 34 |
( ( ! empty( $wpdb->prefix ) ) && ( strpos( $tablename, $wpdb->prefix ) === false ) ) |
| 35 |
|| ( '_' == $wpdb->prefix ) // FixIn: 8.7.3.16. |
| 36 |
) { |
| 37 |
$tablename = $wpdb->prefix . $tablename; |
| 38 |
} |
| 39 |
|
| 40 |
if ( 0 ) { |
| 41 |
$sql_check_table = $wpdb->prepare( "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE (TABLE_SCHEMA = '{$wpdb->dbname}') AND (TABLE_NAME = %s);", $tablename ); |
| 42 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 43 |
$res = $wpdb->get_results( $sql_check_table ); |
| 44 |
|
| 45 |
return count( $res ); |
| 46 |
|
| 47 |
} else { |
| 48 |
|
| 49 |
$sql_check_table = $wpdb->prepare( "SHOW TABLES LIKE %s", $tablename ); //FixIn: 5.4.3 |
| 50 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 51 |
$res = $wpdb->get_results( $sql_check_table ); |
| 52 |
|
| 53 |
return count( $res ); |
| 54 |
} |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
/** |
| 62 |
* Check if table exist |
| 63 |
* |
| 64 |
* @global $wpdb |
| 65 |
* @param string $tablename |
| 66 |
* @param $fieldname |
| 67 |
* @return 0|1 |
| 68 |
*/ |
| 69 |
function wpbc_is_field_in_table_exists( $tablename, $fieldname ) { |
| 70 |
|
| 71 |
|
| 72 |
global $wpdb; |
| 73 |
|
| 74 |
if ( |
| 75 |
( ( ! empty( $wpdb->prefix ) ) && ( strpos( $tablename, $wpdb->prefix ) === false ) ) |
| 76 |
|| ( '_' == $wpdb->prefix ) // FixIn: 8.7.3.16. |
| 77 |
) { |
| 78 |
$tablename = $wpdb->prefix . $tablename; |
| 79 |
} |
| 80 |
|
| 81 |
if ( 0 ) { |
| 82 |
|
| 83 |
$sql_check_table = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='{$tablename}' AND TABLE_SCHEMA='{$wpdb->dbname}' "; |
| 84 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 85 |
$res = $wpdb->get_results( $sql_check_table ); |
| 86 |
|
| 87 |
foreach ( $res as $fld ) { |
| 88 |
if ( $fieldname === $fld->COLUMN_NAME ) { |
| 89 |
return 1; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
} else { |
| 94 |
|
| 95 |
$sql_check_table = "SHOW COLUMNS FROM {$tablename}"; |
| 96 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 97 |
$res = $wpdb->get_results( $sql_check_table ); |
| 98 |
|
| 99 |
foreach ( $res as $fld ) { |
| 100 |
if ( $fld->Field == $fieldname ) { |
| 101 |
return 1; |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
return 0; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
/** |
| 111 |
* Check if index exist |
| 112 |
* |
| 113 |
* @param string $tablename |
| 114 |
* @param $fieldindex |
| 115 |
* |
| 116 |
* @return 0|1 |
| 117 |
* @global $wpdb |
| 118 |
*/ |
| 119 |
function wpbc_is_index_in_table_exists( $tablename, $fieldindex ) { |
| 120 |
global $wpdb; |
| 121 |
if ( ( ! empty( $wpdb->prefix ) ) && ( strpos( $tablename, $wpdb->prefix ) === false ) ) { |
| 122 |
$tablename = $wpdb->prefix . $tablename; |
| 123 |
} |
| 124 |
/* phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQL.InterpolatedNotPrepared */ |
| 125 |
$sql_check_table = $wpdb->prepare( "SHOW INDEX FROM {$tablename} WHERE Key_name = %s", $fieldindex ); |
| 126 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter |
| 127 |
$res = $wpdb->get_results( $sql_check_table ); |
| 128 |
if ( count( $res ) > 0 ) { |
| 129 |
return 1; |
| 130 |
} else { |
| 131 |
return 0; |
| 132 |
} |
| 133 |
} |
| 134 |
|