| 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 |
| 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 |
| 51 |
$res = $wpdb->get_results( $sql_check_table ); |
| 52 |
|
| 53 |
return count( $res ); |
| 54 |
} |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
// FixIn: 10.0.0.1. |
| 60 |
/** |
| 61 |
* Check if we are in playground.wordpress.net , where used 'sqlite' DB |
| 62 |
* |
| 63 |
* @return bool |
| 64 |
*/ |
| 65 |
function wpbc_is_this_wp_playground_db(){ |
| 66 |
|
| 67 |
return false; |
| 68 |
|
| 69 |
if ( |
| 70 |
( ( function_exists( 'sqlite_open' ) ) || ( class_exists( 'SQLite3' ) ) ) |
| 71 |
&& ( ! class_exists( 'wpdev_bk_personal' ) ) |
| 72 |
){ |
| 73 |
return true; |
| 74 |
} else { |
| 75 |
return false; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
/** |
| 81 |
* Check if table exist |
| 82 |
* |
| 83 |
* @global $wpdb |
| 84 |
* @param string $tablename |
| 85 |
* @param $fieldname |
| 86 |
* @return 0|1 |
| 87 |
*/ |
| 88 |
function wpbc_is_field_in_table_exists( $tablename, $fieldname ) { |
| 89 |
|
| 90 |
if ( wpbc_is_this_wp_playground_db() ) { |
| 91 |
return 1; // Probably we are in playground.wordpress.net --> So then all such fields already was created // FixIn: 10.0.0.1. |
| 92 |
} |
| 93 |
|
| 94 |
global $wpdb; |
| 95 |
|
| 96 |
if ( |
| 97 |
( ( ! empty( $wpdb->prefix ) ) && ( strpos( $tablename, $wpdb->prefix ) === false ) ) |
| 98 |
|| ( '_' == $wpdb->prefix ) // FixIn: 8.7.3.16. |
| 99 |
) { |
| 100 |
$tablename = $wpdb->prefix . $tablename; |
| 101 |
} |
| 102 |
|
| 103 |
if ( 0 ) { |
| 104 |
|
| 105 |
$sql_check_table = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='{$tablename}' AND TABLE_SCHEMA='{$wpdb->dbname}' "; |
| 106 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared |
| 107 |
$res = $wpdb->get_results( $sql_check_table ); |
| 108 |
|
| 109 |
foreach ( $res as $fld ) { |
| 110 |
if ( $fieldname === $fld->COLUMN_NAME ) { |
| 111 |
return 1; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
} else { |
| 116 |
|
| 117 |
$sql_check_table = "SHOW COLUMNS FROM {$tablename}"; |
| 118 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared |
| 119 |
$res = $wpdb->get_results( $sql_check_table ); |
| 120 |
|
| 121 |
foreach ( $res as $fld ) { |
| 122 |
if ( $fld->Field == $fieldname ) { |
| 123 |
return 1; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
/** |
| 133 |
* Check if index exist |
| 134 |
* |
| 135 |
* @param string $tablename |
| 136 |
* @param $fieldindex |
| 137 |
* |
| 138 |
* @return 0|1 |
| 139 |
* @global $wpdb |
| 140 |
*/ |
| 141 |
function wpbc_is_index_in_table_exists( $tablename, $fieldindex ) { |
| 142 |
global $wpdb; |
| 143 |
if ( ( ! empty( $wpdb->prefix ) ) && ( strpos( $tablename, $wpdb->prefix ) === false ) ) { |
| 144 |
$tablename = $wpdb->prefix . $tablename; |
| 145 |
} |
| 146 |
/* phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQL.InterpolatedNotPrepared */ |
| 147 |
$sql_check_table = $wpdb->prepare( "SHOW INDEX FROM {$tablename} WHERE Key_name = %s", $fieldindex ); |
| 148 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared |
| 149 |
$res = $wpdb->get_results( $sql_check_table ); |
| 150 |
if ( count( $res ) > 0 ) { |
| 151 |
return 1; |
| 152 |
} else { |
| 153 |
return 0; |
| 154 |
} |
| 155 |
} |
| 156 |
|