11<?php
2- /**
3- * A base WordPress database table class with query builder
4- *
5- * @author Varun Sridharan <varunsridharan23@gmail.com>
6- * @version 1.0
7- * @since 1.0
8- * @link
9- * @copyright 2018 Varun Sridharan
10- * @license GPLV3 Or Greater (https://www.gnu.org/licenses/gpl-3.0.txt)
11- */
2+
123
134namespace Varunsridharan \WordPress ;
145
156if ( ! defined ( 'ABSPATH ' ) ) {
167 die;
178}
189
10+ use TheLeague \Database \Query_Builder ;
11+
1912if ( ! class_exists ( '\Varunsridharan\WordPress\DB_Table ' ) ) {
2013 /**
2114 * Class DB_Table
2215 *
2316 * @package Varunsridharan\WordPress
2417 * @author Varun Sridharan <varunsridharan23@gmail.com>
25- * @since 1.0
2618 *
2719 * A base WordPress database table class, which facilitates the creation of
2820 * and schema changes to individual database tables.
3830 * - Tables upgrade via independent upgrade abstract methods
3931 * - Multisite friendly - site tables switch on "switch_blog" action
4032 */
41- abstract class DB_Table extends \ TheLeague \ Database \ Query_Builder {
33+ abstract class DB_Table extends Query_Builder {
4234
4335 /**
4436 * Table name, without the global table prefix
@@ -141,7 +133,7 @@ public function __construct() {
141133 /**
142134 * Returns Current Instance / create a new instance
143135 *
144- * @return self
136+ * @return self|static
145137 */
146138 public static function instance () {
147139 if ( ! isset ( self ::$ _instances [ static ::class ] ) ) {
@@ -152,15 +144,11 @@ public static function instance() {
152144
153145 /**
154146 * Setup this database table
155- *
156- * @since 1.1.0
157147 */
158148 protected abstract function set_schema ();
159149
160150 /**
161151 * Upgrade this database table
162- *
163- * @since 1.1.0
164152 */
165153 protected abstract function upgrade ();
166154
@@ -183,8 +171,6 @@ protected abstract function table_version();
183171 *
184172 * Hooked to the "switch_blog" action.
185173 *
186- * @since 1.1.0
187- *
188174 * @param int $site_id The site being switched to
189175 */
190176 public function switch_blog ( $ site_id = 0 ) {
@@ -198,35 +184,31 @@ public function switch_blog( $site_id = 0 ) {
198184 * Maybe upgrade the database table. Handles creation & schema changes.
199185 *
200186 * Hooked to the "admin_init" action.
201- *
202- * @since 1.1.0
203187 */
204188 public function maybe_upgrade () {
205189 if ( ! $ this ->exists () ) {
206190 $ this ->create ();
207- }
191+ } else {
192+ $ needs_upgrade = version_compare ( $ this ->version , $ this ->db_version , '>= ' );
208193
209- $ needs_upgrade = version_compare ( (int ) $ this ->db_version , (int ) $ this ->version , '>= ' );
194+ if ( true === $ needs_upgrade ) {
195+ return ;
196+ }
210197
211- if ( true === $ needs_upgrade ) {
212- return ;
213- }
198+ if ( $ this -> is_global () && ! \wp_should_upgrade_global_tables () ) {
199+ return ;
200+ }
214201
215- if ( $ this ->is_global () && ! \wp_should_upgrade_global_tables () ) {
216- return ;
202+ $ this ->exists () ? $ this ->upgrade () : $ this ->create ();
217203 }
218204
219- $ this ->exists () ? $ this ->upgrade () : $ this ->create ();
220-
221205 if ( $ this ->exists () ) {
222206 $ this ->set_db_version ();
223207 }
224208 }
225209
226210 /**
227211 * Setup the necessary table variables
228- *
229- * @since 1.1.0
230212 */
231213 private function setup () {
232214 $ this ->db = isset ( $ GLOBALS ['wpdb ' ] ) ? $ GLOBALS ['wpdb ' ] : false ;
@@ -252,8 +234,6 @@ private function setup() {
252234 *
253235 * This must be done directly because WordPress does not have a mechanism
254236 * for manipulating them safely
255- *
256- * @since 1.1.0
257237 */
258238 private function set_wpdb_tables () {
259239 if ( $ this ->is_global () ) {
@@ -281,38 +261,30 @@ private function set_wpdb_tables() {
281261 * Set the database version for the table
282262 *
283263 * Global table version in "_sitemeta" on the main network
284- *
285- * @since 1.1.0
286264 */
287265 private function set_db_version () {
288- $ this ->db_version = $ this ->version ;
266+ $ this ->version = $ this ->db_version ;
289267 $ this ->is_global () ? \update_network_option ( null , $ this ->db_version_key , $ this ->version ) : \update_option ( $ this ->db_version_key , $ this ->version );
290268 }
291269
292270 /**
293271 * Get the table version from the database
294272 *
295273 * Global table version from "_sitemeta" on the main network
296- *
297- * @since 1.1.0
298274 */
299275 private function get_db_version () {
300- $ this ->db_version = $ this ->is_global () ? \get_network_option ( null , $ this ->db_version_key , false ) : \get_option ( $ this ->db_version_key , false );
276+ $ this ->version = $ this ->is_global () ? \get_network_option ( null , $ this ->db_version_key , false ) : \get_option ( $ this ->db_version_key , false );
301277 }
302278
303279 /**
304280 * Add class hooks to WordPress actions
305- *
306- * @since 1.1.0
307281 */
308282 private function add_hooks () {
309283 \add_action ( 'switch_blog ' , array ( $ this , 'switch_blog ' ) );
310284 }
311285
312286 /**
313287 * Create the table
314- *
315- * @since 1.1.0
316288 */
317289 private function create () {
318290 if ( ! function_exists ( 'dbDelta ' ) ) {
@@ -340,8 +312,6 @@ protected function after_table_created() {
340312 /**
341313 * Check if table already exists
342314 *
343- * @since 1.1.0
344- *
345315 * @return bool
346316 */
347317 private function exists () {
@@ -355,8 +325,6 @@ private function exists() {
355325 /**
356326 * Check if table is global
357327 *
358- * @since 1.2.0
359- *
360328 * @return bool
361329 */
362330 private function is_global () {
@@ -373,8 +341,6 @@ private function is_global() {
373341 * - No double underscores
374342 * - No trailing underscores
375343 *
376- * @since 1.3.0
377- *
378344 * @param string $name The name of the database table
379345 *
380346 * @return string Sanitized database table name
0 commit comments