PluginProbe ʕ •ᴥ•ʔ
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.5
27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 19.1 19.10 19.11 19.12 19.13 19.14 19.2 19.3 19.4 19.5 19.5.1 19.6 19.6.1 19.7 19.7.1 19.7.2 19.8 19.9 20.0 20.1 20.10 20.11 20.12 20.13 20.2 20.2.1 20.3 20.4 20.5 20.6 20.7 20.8 20.9 21.0 21.1 21.2 21.3 21.4 21.5 21.6 21.7 21.8 21.8.1 21.9 21.9.1 22.0 22.1 22.2 22.3 22.4 22.5 22.6 22.7 22.8 22.9 23.0 23.1 23.2 23.3 23.4 23.5 23.6 23.7 23.8 23.9 24.0 24.1 24.2 24.3 24.4 24.5 24.6 24.7 24.8 24.8.1 24.9 25.0 25.1 25.2 25.3 25.3.1 25.4 25.5 25.6 25.7 25.8 25.9 26.0 26.1 26.1.1 26.2 26.3 26.4 26.5 26.6 26.7 26.8 26.9 27.0 27.1 27.1.1 27.2 27.3 27.4
wordpress-seo / admin / class-database-proxy.php
wordpress-seo / admin Last commit date
ajax 2 years ago capabilities 1 year ago endpoints 2 years ago exceptions 3 months ago filters 3 months ago formatter 1 year ago google_search_console 3 months ago import 3 months ago listeners 8 years ago menu 3 months ago metabox 3 months ago notifiers 3 months ago pages 3 months ago roles 3 months ago services 3 months ago statistics 3 months ago taxonomy 3 months ago tracking 3 months ago views 3 months ago watchers 3 months ago admin-settings-changed-listener.php 2 years ago ajax.php 3 months ago class-admin-asset-analysis-worker-location.php 3 months ago class-admin-asset-dev-server-location.php 3 months ago class-admin-asset-location.php 8 years ago class-admin-asset-manager.php 3 months ago class-admin-asset-seo-location.php 4 years ago class-admin-editor-specific-replace-vars.php 3 months ago class-admin-gutenberg-compatibility-notification.php 3 months ago class-admin-help-panel.php 3 months ago class-admin-init.php 3 months ago class-admin-recommended-replace-vars.php 2 years ago class-admin-user-profile.php 7 months ago class-admin-utils.php 3 months ago class-admin.php 3 months ago class-asset.php 1 year ago class-bulk-description-editor-list-table.php 3 months ago class-bulk-editor-list-table.php 3 months ago class-bulk-title-editor-list-table.php 3 months ago class-collector.php 1 year ago class-config.php 3 months ago class-database-proxy.php 3 months ago class-export.php 3 months ago class-expose-shortlinks.php 7 months ago class-gutenberg-compatibility.php 1 month ago class-meta-columns.php 3 months ago class-my-yoast-proxy.php 3 months ago class-option-tab.php 4 years ago class-option-tabs-formatter.php 3 months ago class-option-tabs.php 2 years ago class-paper-presenter.php 5 years ago class-plugin-availability.php 3 months ago class-plugin-conflict.php 2 years ago class-premium-popup.php 1 year ago class-premium-upsell-admin-block.php 3 months ago class-primary-term-admin.php 3 months ago class-product-upsell-notice.php 3 months ago class-remote-request.php 2 years ago class-schema-person-upgrade-notification.php 3 months ago class-suggested-plugins.php 3 months ago class-wincher-dashboard-widget.php 3 months ago class-yoast-columns.php 3 months ago class-yoast-dashboard-widget.php 3 months ago class-yoast-form.php 3 months ago class-yoast-input-validation.php 3 months ago class-yoast-network-admin.php 3 months ago class-yoast-network-settings-api.php 3 months ago class-yoast-notification-center.php 3 months ago class-yoast-notification.php 3 months ago class-yoast-notifications.php 3 months ago class-yoast-plugin-conflict.php 3 months ago index.php 10 years ago interface-collection.php 7 years ago interface-installable.php 8 years ago
class-database-proxy.php
310 lines
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\Admin
6 */
7
8 /**
9 * Represents the proxy for communicating with the database.
10 */
11 class WPSEO_Database_Proxy {
12
13 /**
14 * Holds the table name.
15 *
16 * @var string
17 */
18 protected $table_name;
19
20 /**
21 * Determines whether to suppress errors or not.
22 *
23 * @var bool
24 */
25 protected $suppress_errors = true;
26
27 /**
28 * Determines if this table is multisite.
29 *
30 * @var bool
31 */
32 protected $is_multisite_table = false;
33
34 /**
35 * Holds the last suppressed state.
36 *
37 * @var bool
38 */
39 protected $last_suppressed_state;
40
41 /**
42 * Holds the WordPress database object.
43 *
44 * @var wpdb
45 */
46 protected $database;
47
48 /**
49 * Holds the table prefix.
50 *
51 * @var string
52 */
53 protected $table_prefix;
54
55 /**
56 * Sets the class attributes and registers the table.
57 *
58 * @param wpdb $database The database object.
59 * @param string $table_name The table name that is represented.
60 * @param bool $suppress_errors Should the errors be suppressed.
61 * @param bool $is_multisite_table Should the table be global in multisite.
62 */
63 public function __construct( $database, $table_name, $suppress_errors = true, $is_multisite_table = false ) {
64 $this->table_name = $table_name;
65 $this->suppress_errors = (bool) $suppress_errors;
66 $this->is_multisite_table = (bool) $is_multisite_table;
67 $this->database = $database;
68
69 // If the table prefix was provided, strip it as it's handled automatically.
70 $table_prefix = $this->get_table_prefix();
71 if ( ! empty( $table_prefix ) && strpos( $this->table_name, $table_prefix ) === 0 ) {
72 $this->table_prefix = substr( $this->table_name, strlen( $table_prefix ) );
73 }
74
75 if ( ! $this->is_table_registered() ) {
76 $this->register_table();
77 }
78 }
79
80 /**
81 * Inserts data into the database.
82 *
83 * @param array $data Data to insert.
84 * @param array|string|null $format Formats for the data.
85 *
86 * @return int|false Total amount of inserted rows or false on error.
87 */
88 public function insert( array $data, $format = null ) {
89 $this->pre_execution();
90
91 $result = $this->database->insert( $this->get_table_name(), $data, $format );
92
93 $this->post_execution();
94
95 return $result;
96 }
97
98 /**
99 * Updates data in the database.
100 *
101 * @param array $data Data to update on the table.
102 * @param array $where Where condition as key => value array.
103 * @param array|string|null $format Optional. Data prepare format.
104 * @param array|string|null $where_format Optional. Where prepare format.
105 *
106 * @return int|false False when the update request is invalid, int on number of rows changed.
107 */
108 public function update( array $data, array $where, $format = null, $where_format = null ) {
109 $this->pre_execution();
110
111 $result = $this->database->update( $this->get_table_name(), $data, $where, $format, $where_format );
112
113 $this->post_execution();
114
115 return $result;
116 }
117
118 /**
119 * Upserts data in the database.
120 *
121 * Performs an insert into and if key is duplicate it will update the existing record.
122 *
123 * @param array $data Data to update on the table.
124 * @param array|null $where Unused. Where condition as key => value array.
125 * @param array|string|null $format Optional. Data prepare format.
126 * @param array|string|null $where_format Optional. Where prepare format.
127 *
128 * @return int|false False when the upsert request is invalid, int on number of rows changed.
129 */
130 public function upsert( array $data, ?array $where = null, $format = null, $where_format = null ) {
131 if ( $where_format !== null ) {
132 _deprecated_argument( __METHOD__, '7.7.0', 'The where_format argument is deprecated' );
133 }
134
135 $this->pre_execution();
136
137 $update = [];
138 $keys = [];
139 $columns = array_keys( $data );
140 foreach ( $columns as $column ) {
141 $keys[] = '`' . $column . '`';
142 $update[] = sprintf( '`%1$s` = VALUES(`%1$s`)', $column );
143 }
144
145 $query = sprintf(
146 'INSERT INTO `%1$s` (%2$s) VALUES ( %3$s ) ON DUPLICATE KEY UPDATE %4$s',
147 $this->get_table_name(),
148 implode( ', ', $keys ),
149 implode( ', ', array_fill( 0, count( $data ), '%s' ) ),
150 implode( ', ', $update ),
151 );
152
153 $result = $this->database->query(
154 $this->database->prepare(
155 $query,
156 array_values( $data ),
157 ),
158 );
159
160 $this->post_execution();
161
162 return $result;
163 }
164
165 /**
166 * Deletes a record from the database.
167 *
168 * @param array $where Where clauses for the query.
169 * @param array|string|null $format Formats for the data.
170 *
171 * @return int|false
172 */
173 public function delete( array $where, $format = null ) {
174 $this->pre_execution();
175
176 $result = $this->database->delete( $this->get_table_name(), $where, $format );
177
178 $this->post_execution();
179
180 return $result;
181 }
182
183 /**
184 * Executes the given query and returns the results.
185 *
186 * @param string $query The query to execute.
187 *
188 * @return array|object|null The resultset
189 */
190 public function get_results( $query ) {
191 $this->pre_execution();
192
193 $results = $this->database->get_results( $query );
194
195 $this->post_execution();
196
197 return $results;
198 }
199
200 /**
201 * Creates a table to the database.
202 *
203 * @param array $columns The columns to create.
204 * @param array $indexes The indexes to use.
205 *
206 * @return bool True when creation is successful.
207 */
208 public function create_table( array $columns, array $indexes = [] ) {
209 $create_table = sprintf(
210 'CREATE TABLE IF NOT EXISTS %1$s ( %2$s ) %3$s',
211 $this->get_table_name(),
212 implode( ',', array_merge( $columns, $indexes ) ),
213 $this->database->get_charset_collate(),
214 );
215
216 $this->pre_execution();
217
218 $is_created = (bool) $this->database->query( $create_table );
219
220 $this->post_execution();
221
222 return $is_created;
223 }
224
225 /**
226 * Checks if there is an error.
227 *
228 * @return bool Returns true when there is an error.
229 */
230 public function has_error() {
231 return ( $this->database->last_error !== '' );
232 }
233
234 /**
235 * Executed before a query will be ran.
236 *
237 * @return void
238 */
239 protected function pre_execution() {
240 if ( $this->suppress_errors ) {
241 $this->last_suppressed_state = $this->database->suppress_errors();
242 }
243 }
244
245 /**
246 * Executed after a query has been ran.
247 *
248 * @return void
249 */
250 protected function post_execution() {
251 if ( $this->suppress_errors ) {
252 $this->database->suppress_errors( $this->last_suppressed_state );
253 }
254 }
255
256 /**
257 * Returns the full table name.
258 *
259 * @return string Full table name including prefix.
260 */
261 public function get_table_name() {
262 return $this->get_table_prefix() . $this->table_name;
263 }
264
265 /**
266 * Returns the prefix to use for the table.
267 *
268 * @return string The table prefix depending on the database context.
269 */
270 protected function get_table_prefix() {
271 if ( $this->is_multisite_table ) {
272 return $this->database->base_prefix;
273 }
274
275 return $this->database->get_blog_prefix();
276 }
277
278 /**
279 * Registers the table with WordPress.
280 *
281 * @return void
282 */
283 protected function register_table() {
284 $table_name = $this->table_name;
285 $full_table_name = $this->get_table_name();
286
287 $this->database->$table_name = $full_table_name;
288
289 if ( $this->is_multisite_table ) {
290 $this->database->ms_global_tables[] = $table_name;
291 return;
292 }
293
294 $this->database->tables[] = $table_name;
295 }
296
297 /**
298 * Checks if the table has been registered with WordPress.
299 *
300 * @return bool True if the table is registered, false otherwise.
301 */
302 protected function is_table_registered() {
303 if ( $this->is_multisite_table ) {
304 return in_array( $this->table_name, $this->database->ms_global_tables, true );
305 }
306
307 return in_array( $this->table_name, $this->database->tables, true );
308 }
309 }
310