class-database-proxy.php 7.2 KB

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