PluginProbe
Booking Calendar / 11.8.1
Booking Calendar v11.8.1
11.8.2 11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 10.11 10.11.2 10.11.3 All 202 releases
booking / includes / _functions / is_table_exist.php

is_table_exist.php in Booking Calendar 11.8.1, at includes/_functions/is_table_exist.php

150 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 whether a field exists in a database table.
63 *
64 * Missing tables return zero without issuing a column query. This keeps
65 * activation schema checks quiet while an owning edition is not installed yet.
66 *
67 * @param string $tablename Table name or Booking Calendar table suffix.
68 * @param string $fieldname Field name.
69 *
70 * @return int One when the field exists; otherwise zero.
71 * @global wpdb $wpdb WordPress database abstraction object.
72 */
73 function wpbc_is_field_in_table_exists( $tablename, $fieldname ) {
74
75 global $wpdb;
76
77 // Schema probes against a missing table emit a visible WordPress database error.
78 // Activation callers treat a missing table and a missing field identically, so
79 // fail closed before issuing SHOW COLUMNS.
80 if ( 0 === wpbc_is_table_exists( $tablename ) ) {
81 return 0;
82 }
83
84 if (
85 ( ( ! empty( $wpdb->prefix ) ) && ( strpos( $tablename, $wpdb->prefix ) === false ) )
86 || ( '_' == $wpdb->prefix ) // FixIn: 8.7.3.16.
87 ) {
88 $tablename = $wpdb->prefix . $tablename;
89 }
90
91 if ( 0 ) {
92
93 $sql_check_table = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='{$tablename}' AND TABLE_SCHEMA='{$wpdb->dbname}' ";
94 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter
95 $res = $wpdb->get_results( $sql_check_table );
96
97 foreach ( $res as $fld ) {
98 if ( $fieldname === $fld->COLUMN_NAME ) {
99 return 1;
100 }
101 }
102
103 } else {
104
105 $sql_check_table = "SHOW COLUMNS FROM {$tablename}";
106 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter
107 $res = $wpdb->get_results( $sql_check_table );
108
109 foreach ( $res as $fld ) {
110 if ( $fld->Field == $fieldname ) {
111 return 1;
112 }
113 }
114 }
115
116 return 0;
117 }
118
119
120 /**
121 * Check whether an index exists in a database table.
122 *
123 * Missing tables return zero without issuing an index query.
124 *
125 * @param string $tablename Table name or Booking Calendar table suffix.
126 * @param string $fieldindex Index name.
127 *
128 * @return int One when the index exists; otherwise zero.
129 * @global wpdb $wpdb WordPress database abstraction object.
130 */
131 function wpbc_is_index_in_table_exists( $tablename, $fieldindex ) {
132 global $wpdb;
133
134 if ( 0 === wpbc_is_table_exists( $tablename ) ) {
135 return 0;
136 }
137 if ( ( ! empty( $wpdb->prefix ) ) && ( strpos( $tablename, $wpdb->prefix ) === false ) ) {
138 $tablename = $wpdb->prefix . $tablename;
139 }
140 /* phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQL.InterpolatedNotPrepared */
141 $sql_check_table = $wpdb->prepare( "SHOW INDEX FROM {$tablename} WHERE Key_name = %s", $fieldindex );
142 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter
143 $res = $wpdb->get_results( $sql_check_table );
144 if ( count( $res ) > 0 ) {
145 return 1;
146 } else {
147 return 0;
148 }
149 }
150