PluginProbe ʕ •ᴥ•ʔ
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.0
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 5 years ago capabilities 4 years ago endpoints 5 years ago exceptions 7 years ago filters 4 years ago formatter 4 years ago google_search_console 5 years ago import 4 years ago listeners 8 years ago menu 4 years ago metabox 4 years ago notifiers 4 years ago pages 4 years ago roles 5 years ago ryte 5 years ago services 5 years ago statistics 5 years ago taxonomy 4 years ago tracking 4 years ago views 4 years ago watchers 5 years ago admin-settings-changed-listener.php 5 years ago ajax.php 4 years ago class-admin-asset-analysis-worker-location.php 5 years ago class-admin-asset-dev-server-location.php 5 years ago class-admin-asset-location.php 8 years ago class-admin-asset-manager.php 4 years ago class-admin-asset-seo-location.php 4 years ago class-admin-asset-yoast-components-l10n.php 5 years ago class-admin-editor-specific-replace-vars.php 5 years ago class-admin-gutenberg-compatibility-notification.php 5 years ago class-admin-help-panel.php 5 years ago class-admin-init.php 4 years ago class-admin-recommended-replace-vars.php 6 years ago class-admin-user-profile.php 6 years ago class-admin-utils.php 5 years ago class-admin.php 4 years ago class-asset.php 5 years ago class-bulk-description-editor-list-table.php 5 years ago class-bulk-editor-list-table.php 4 years ago class-bulk-title-editor-list-table.php 6 years ago class-collector.php 6 years ago class-config.php 4 years ago class-customizer.php 5 years ago class-database-proxy.php 5 years ago class-export.php 5 years ago class-expose-shortlinks.php 4 years ago class-gutenberg-compatibility.php 4 years ago class-helpscout.php 5 years ago class-meta-columns.php 5 years ago class-my-yoast-proxy.php 5 years ago class-option-tab.php 4 years ago class-option-tabs-formatter.php 5 years ago class-option-tabs.php 5 years ago class-paper-presenter.php 5 years ago class-plugin-availability.php 5 years ago class-plugin-conflict.php 4 years ago class-premium-popup.php 5 years ago class-premium-upsell-admin-block.php 4 years ago class-primary-term-admin.php 5 years ago class-product-upsell-notice.php 5 years ago class-remote-request.php 5 years ago class-schema-person-upgrade-notification.php 4 years ago class-suggested-plugins.php 4 years ago class-yoast-columns.php 5 years ago class-yoast-dashboard-widget.php 4 years ago class-yoast-form.php 4 years ago class-yoast-input-validation.php 5 years ago class-yoast-network-admin.php 5 years ago class-yoast-network-settings-api.php 4 years ago class-yoast-notification-center.php 4 years ago class-yoast-notification.php 5 years ago class-yoast-notifications.php 5 years ago class-yoast-plugin-conflict.php 4 years ago index.php 10 years ago interface-collection.php 7 years ago interface-installable.php 8 years ago
class-database-proxy.php
299 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 * Sets the class attributes and registers the table.
50 *
51 * @param wpdb $database The database object.
52 * @param string $table_name The table name that is represented.
53 * @param bool $suppress_errors Should the errors be suppressed.
54 * @param bool $is_multisite_table Should the table be global in multisite.
55 */
56 public function __construct( $database, $table_name, $suppress_errors = true, $is_multisite_table = false ) {
57 $this->table_name = $table_name;
58 $this->suppress_errors = (bool) $suppress_errors;
59 $this->is_multisite_table = (bool) $is_multisite_table;
60 $this->database = $database;
61
62 // If the table prefix was provided, strip it as it's handled automatically.
63 $table_prefix = $this->get_table_prefix();
64 if ( ! empty( $table_prefix ) && strpos( $this->table_name, $table_prefix ) === 0 ) {
65 $this->table_prefix = substr( $this->table_name, strlen( $table_prefix ) );
66 }
67
68 if ( ! $this->is_table_registered() ) {
69 $this->register_table();
70 }
71 }
72
73 /**
74 * Inserts data into the database.
75 *
76 * @param array $data Data to insert.
77 * @param null $format Formats for the data.
78 *
79 * @return false|int Total amount of inserted rows or false on error.
80 */
81 public function insert( array $data, $format = null ) {
82 $this->pre_execution();
83
84 $result = $this->database->insert( $this->get_table_name(), $data, $format );
85
86 $this->post_execution();
87
88 return $result;
89 }
90
91 /**
92 * Updates data in the database.
93 *
94 * @param array $data Data to update on the table.
95 * @param array $where Where condition as key => value array.
96 * @param null $format Optional. Data prepare format.
97 * @param null $where_format Optional. Where prepare format.
98 *
99 * @return false|int False when the update request is invalid, int on number of rows changed.
100 */
101 public function update( array $data, array $where, $format = null, $where_format = null ) {
102 $this->pre_execution();
103
104 $result = $this->database->update( $this->get_table_name(), $data, $where, $format, $where_format );
105
106 $this->post_execution();
107
108 return $result;
109 }
110
111 /**
112 * Upserts data in the database.
113 *
114 * Performs an insert into and if key is duplicate it will update the existing record.
115 *
116 * @param array $data Data to update on the table.
117 * @param array|null $where Unused. Where condition as key => value array.
118 * @param null $format Optional. Data prepare format.
119 * @param null $where_format Deprecated. Where prepare format.
120 *
121 * @return false|int False when the upsert request is invalid, int on number of rows changed.
122 */
123 public function upsert( array $data, array $where = null, $format = null, $where_format = null ) {
124 if ( $where_format !== null ) {
125 _deprecated_argument( __METHOD__, '7.7.0', 'The where_format argument is deprecated' );
126 }
127
128 $this->pre_execution();
129
130 $update = [];
131 $keys = [];
132 $columns = array_keys( $data );
133 foreach ( $columns as $column ) {
134 $keys[] = '`' . $column . '`';
135 $update[] = sprintf( '`%1$s` = VALUES(`%1$s`)', $column );
136 }
137
138 $query = sprintf(
139 'INSERT INTO `%1$s` (%2$s) VALUES ( %3$s ) ON DUPLICATE KEY UPDATE %4$s',
140 $this->get_table_name(),
141 implode( ', ', $keys ),
142 implode( ', ', array_fill( 0, count( $data ), '%s' ) ),
143 implode( ', ', $update )
144 );
145
146 $result = $this->database->query(
147 $this->database->prepare(
148 $query,
149 array_values( $data )
150 )
151 );
152
153 $this->post_execution();
154
155 return $result;
156 }
157
158 /**
159 * Deletes a record from the database.
160 *
161 * @param array $where Where clauses for the query.
162 * @param array|null $format Formats for the data.
163 *
164 * @return false|int
165 */
166 public function delete( array $where, $format = null ) {
167 $this->pre_execution();
168
169 $result = $this->database->delete( $this->get_table_name(), $where, $format );
170
171 $this->post_execution();
172
173 return $result;
174 }
175
176 /**
177 * Executes the given query and returns the results.
178 *
179 * @param string $query The query to execute.
180 *
181 * @return array|object|null The resultset
182 */
183 public function get_results( $query ) {
184 $this->pre_execution();
185
186 $results = $this->database->get_results( $query );
187
188 $this->post_execution();
189
190 return $results;
191 }
192
193 /**
194 * Creates a table to the database.
195 *
196 * @param array $columns The columns to create.
197 * @param array $indexes The indexes to use.
198 *
199 * @return bool True when creation is successful.
200 */
201 public function create_table( array $columns, array $indexes = [] ) {
202 $create_table = sprintf(
203 'CREATE TABLE IF NOT EXISTS %1$s ( %2$s ) %3$s',
204 $this->get_table_name(),
205 implode( ',', array_merge( $columns, $indexes ) ),
206 $this->database->get_charset_collate()
207 );
208
209 $this->pre_execution();
210
211 $is_created = (bool) $this->database->query( $create_table );
212
213 $this->post_execution();
214
215 return $is_created;
216 }
217
218 /**
219 * Checks if there is an error.
220 *
221 * @return bool Returns true when there is an error.
222 */
223 public function has_error() {
224 return ( $this->database->last_error !== '' );
225 }
226
227 /**
228 * Executed before a query will be ran.
229 */
230 protected function pre_execution() {
231 if ( $this->suppress_errors ) {
232 $this->last_suppressed_state = $this->database->suppress_errors();
233 }
234 }
235
236 /**
237 * Executed after a query has been ran.
238 */
239 protected function post_execution() {
240 if ( $this->suppress_errors ) {
241 $this->database->suppress_errors( $this->last_suppressed_state );
242 }
243 }
244
245 /**
246 * Returns the full table name.
247 *
248 * @return string Full table name including prefix.
249 */
250 public function get_table_name() {
251 return $this->get_table_prefix() . $this->table_name;
252 }
253
254 /**
255 * Returns the prefix to use for the table.
256 *
257 * @return string The table prefix depending on the database context.
258 */
259 protected function get_table_prefix() {
260 if ( $this->is_multisite_table ) {
261 return $this->database->base_prefix;
262 }
263
264 return $this->database->get_blog_prefix();
265 }
266
267 /**
268 * Registers the table with WordPress.
269 *
270 * @return void
271 */
272 protected function register_table() {
273 $table_name = $this->table_name;
274 $full_table_name = $this->get_table_name();
275
276 $this->database->$table_name = $full_table_name;
277
278 if ( $this->is_multisite_table ) {
279 $this->database->ms_global_tables[] = $table_name;
280 return;
281 }
282
283 $this->database->tables[] = $table_name;
284 }
285
286 /**
287 * Checks if the table has been registered with WordPress.
288 *
289 * @return bool True if the table is registered, false otherwise.
290 */
291 protected function is_table_registered() {
292 if ( $this->is_multisite_table ) {
293 return in_array( $this->table_name, $this->database->ms_global_tables, true );
294 }
295
296 return in_array( $this->table_name, $this->database->tables, true );
297 }
298 }
299