· 8 years ago · May 26, 2018, 03:38 AM
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 */
11class WPSEO_Database_Proxy {
12
13 /** @var string */
14 protected $table_name;
15
16 /** @var bool */
17 protected $suppress_errors = true;
18
19 /** @var bool */
20 protected $is_multisite_table = false;
21
22 /** @var bool */
23 protected $last_suppressed_state;
24
25 /** @var wpdb */
26 protected $database;
27
28 /**
29 * Sets the class attributes and registers the table.
30 *
31 * @param wpdb $database The database object.
32 * @param string $table_name The table name that is represented.
33 * @param bool $suppress_errors Should the errors be suppressed.
34 * @param bool $is_multisite_table Should the table be global in multisite.
35 */
36 public function __construct( $database, $table_name, $suppress_errors = true, $is_multisite_table = false ) {
37 $this->table_name = $table_name;
38 $this->suppress_errors = (bool) $suppress_errors;
39 $this->is_multisite_table = (bool) $is_multisite_table;
40 $this->database = $database;
41
42 // If the table prefix was provided, strip it as it's handled automatically.
43 $table_prefix = $this->get_table_prefix();
44
45 if ($table_prefix!='' && strpos( $this->table_name, $table_prefix ) === 0 ) {
46 $this->table_prefix = substr( $this->table_name, strlen( $table_prefix ) );
47 }
48
49 if ( ! $this->is_table_registered() ) {
50 $this->register_table();
51 }
52 }
53
54 /**
55 * Inserts data into the database.
56 *
57 * @param array $data Data to insert.
58 * @param null $format Formats for the data.
59 *
60 * @return false|int Total amount of inserted rows or false on error.
61 */
62 public function insert( array $data, $format = null ) {
63 $this->pre_execution();
64
65 $result = $this->database->insert( $this->get_table_name(), $data, $format );
66
67 $this->post_execution();
68
69 return $result;
70 }
71
72 /**
73 * Updates data in the database.
74 *
75 * @param array $data Data to update on the table.
76 * @param array $where Where condition as key => value array.
77 * @param null $format Optional. data prepare format.
78 * @param null $where_format Optional. Where prepare format.
79 *
80 * @return false|int False when the update request is invalid, int on number of rows changed.
81 */
82 public function update( array $data, array $where, $format = null, $where_format = null ) {
83 $this->pre_execution();
84
85 $result = $this->database->update( $this->get_table_name(), $data, $where, $format, $where_format );
86
87 $this->post_execution();
88
89 return $result;
90 }
91
92 /**
93 * Upserts data in the database.
94 *
95 * Tries to insert the data first, if this fails an update is attempted.
96 *
97 * @param array $data Data to update on the table.
98 * @param array $where Where condition as key => value array.
99 * @param null $format Optional. data prepare format.
100 * @param null $where_format Optional. Where prepare format.
101 *
102 * @return false|int False when the upsert request is invalid, int on number of rows changed.
103 */
104 public function upsert( array $data, array $where, $format = null, $where_format = null ) {
105 $result = $this->insert( $data, $format );
106
107 if ( false === $result ) {
108 $result = $this->update( $data, $where, $format, $where_format );
109 }
110
111 return $result;
112 }
113
114 /**
115 * Deletes a record from the database.
116 *
117 * @param array $where Where clauses for the query.
118 * @param null|array $format Formats for the data.
119 *
120 * @return false|int
121 */
122 public function delete( array $where, $format = null ) {
123 $this->pre_execution();
124
125 $result = $this->database->delete( $this->get_table_name(), $where, $format );
126
127 $this->post_execution();
128
129 return $result;
130 }
131
132 /**
133 * Executes the given query and returns the results.
134 *
135 * @param string $query The query to execute.
136 *
137 * @return array|null|object The resultset
138 */
139 public function get_results( $query ) {
140 $this->pre_execution();
141
142 $results = $this->database->get_results( $query );
143
144 $this->post_execution();
145
146 return $results;
147 }
148
149 /**
150 * Creates a table to the database.
151 *
152 * @param array $columns The columns to create.
153 * @param array $indexes The indexes to use.
154 *
155 * @return bool True when creation is successful.
156 */
157 public function create_table( array $columns, array $indexes = array() ) {
158 $create_table = sprintf( '
159 CREATE TABLE IF NOT EXISTS %1$s ( %2$s ) %3$s',
160 $this->get_table_name(),
161 implode( ',', array_merge( $columns, $indexes ) ),
162 $this->database->get_charset_collate()
163 );
164
165 $this->pre_execution();
166
167 $is_created = (bool) $this->database->query( $create_table );
168
169 $this->post_execution();
170
171 return $is_created;
172 }
173
174 /**
175 * Checks if there is an error.
176 *
177 * @return bool Returns true when there is an error.
178 */
179 public function has_error() {
180 return ( $this->database->last_error !== '' );
181 }
182
183 /**
184 * Executed before a query will be ran.
185 */
186 protected function pre_execution() {
187 if ( $this->suppress_errors ) {
188 $this->last_suppressed_state = $this->database->suppress_errors();
189 }
190 }
191
192 /**
193 * Executed after a query has been ran.
194 */
195 protected function post_execution() {
196 if ( $this->suppress_errors ) {
197 $this->database->suppress_errors( $this->last_suppressed_state );
198 }
199 }
200
201 /**
202 * Returns the full table name.
203 *
204 * @return string Full table name including prefix.
205 */
206 public function get_table_name() {
207 return $this->get_table_prefix() . $this->table_name;
208 }
209
210 /**
211 * Returns the prefix to use for the table.
212 *
213 * @return string The table prefix depending on the database context.
214 */
215 protected function get_table_prefix() {
216 if ( $this->is_multisite_table ) {
217 return $this->database->base_prefix;
218 }
219
220 return $this->database->get_blog_prefix();
221 }
222
223 /**
224 * Registers the table with WordPress.
225 *
226 * @return void
227 */
228 protected function register_table() {
229 $table_name = $this->table_name;
230 $full_table_name = $this->get_table_name();
231
232 $this->database->$table_name = $full_table_name;
233
234 if ( $this->is_multisite_table ) {
235 $this->database->ms_global_tables[] = $table_name;
236 return;
237 }
238
239 $this->database->tables[] = $table_name;
240 }
241
242 /**
243 * Checks if the table has been registered with WordPress.
244 *
245 * @return bool True if the table is registered, false otherwise.
246 */
247 protected function is_table_registered() {
248 if ( $this->is_multisite_table ) {
249 return in_array( $this->table_name, $this->database->ms_global_tables, true );
250 }
251
252 return in_array( $this->table_name, $this->database->tables, true );
253 }
254}