|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Statify: Statify_Schema class |
| 4 | + * |
| 5 | + * This file contains class for the plugin's DB scheme handling. |
| 6 | + * |
| 7 | + * @package Statify |
| 8 | + * @since 2.0.0 |
| 9 | + */ |
| 10 | + |
| 11 | +// Quit if accessed outside WP context. |
| 12 | +defined( 'ABSPATH' ) || exit; |
| 13 | + |
| 14 | +/** |
| 15 | + * Statify DB Schema |
| 16 | + * |
| 17 | + * @since 2.0.0 |
| 18 | + */ |
| 19 | +class Statify_Schema { |
| 20 | + /** |
| 21 | + * Database tables |
| 22 | + * |
| 23 | + * @var string[] |
| 24 | + */ |
| 25 | + public static $tables = array( |
| 26 | + 'statify', |
| 27 | + 'statifymeta', |
| 28 | + ); |
| 29 | + |
| 30 | + /** |
| 31 | + * Needed statify db version for current plugin version |
| 32 | + * |
| 33 | + * @var string |
| 34 | + */ |
| 35 | + public static $db_version = '2.0.0'; |
| 36 | + |
| 37 | + /** |
| 38 | + * Definition of the custom tables. |
| 39 | + * |
| 40 | + * @since 2.0.0 |
| 41 | + * @version 2.0.0 |
| 42 | + */ |
| 43 | + public static function init() { |
| 44 | + // Global. |
| 45 | + global $wpdb; |
| 46 | + |
| 47 | + foreach ( static::$tables as $table ) { |
| 48 | + $wpdb->tables[] = $table; |
| 49 | + $wpdb->$table = $wpdb->get_blog_prefix() . $table; |
| 50 | + } |
| 51 | + |
| 52 | + self::maybe_create_tables(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Create the tables. |
| 57 | + * |
| 58 | + * @since 2.0.0 |
| 59 | + * @version 2.0.0 |
| 60 | + */ |
| 61 | + public static function maybe_create_tables() { |
| 62 | + $current_db_version = get_option( 'statify_db_version', '1.8.4' ); |
| 63 | + if ( $current_db_version === self::$db_version ) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + // Global. |
| 68 | + global $wpdb, $charset_collate; |
| 69 | + |
| 70 | + /** |
| 71 | + * Use same index length like the WordPress core |
| 72 | + * |
| 73 | + * @see wp_get_db_schema() |
| 74 | + */ |
| 75 | + $max_index_length = 191; |
| 76 | + |
| 77 | + // Include. |
| 78 | + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 79 | + |
| 80 | + // Create statify table. |
| 81 | + dbDelta( |
| 82 | + "CREATE TABLE {$wpdb->statify} ( |
| 83 | + id bigint(20) unsigned NOT NULL auto_increment, |
| 84 | + created date NOT NULL default '0000-00-00', |
| 85 | + referrer varchar(255) NOT NULL default '', |
| 86 | + target varchar(255) NOT NULL default '', |
| 87 | + hits integer NOT NULL default 1, |
| 88 | + PRIMARY KEY (id), |
| 89 | + KEY referrer (referrer), |
| 90 | + KEY target (target), |
| 91 | + KEY created (created) |
| 92 | + ) {$charset_collate};" |
| 93 | + ); |
| 94 | + |
| 95 | + // Create statifymeta table. |
| 96 | + dbDelta( |
| 97 | + "CREATE TABLE {$wpdb->statifymeta} ( |
| 98 | + meta_id bigint(20) unsigned NOT NULL auto_increment, |
| 99 | + statify_id bigint(20) unsigned NOT NULL default 0, |
| 100 | + meta_key varchar(255) default NULL, |
| 101 | + meta_value longtext, |
| 102 | + PRIMARY KEY (meta_id), |
| 103 | + KEY statify_id (statify_id), |
| 104 | + KEY meta_key (meta_key({$max_index_length})) |
| 105 | + ) {$charset_collate};" |
| 106 | + ); |
| 107 | + |
| 108 | + update_option( 'statify_db_version', self::$db_version ); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Remove the custom tables. |
| 113 | + * |
| 114 | + * @since 2.0.0 |
| 115 | + * @version 2.0.0 |
| 116 | + */ |
| 117 | + public static function drop_tables() { |
| 118 | + global $wpdb; |
| 119 | + |
| 120 | + // Remove. |
| 121 | + foreach ( static::$tables as $table ) { |
| 122 | + $wpdb->query( "DROP TABLE IF EXISTS `{$wpdb->$table}`" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 123 | + } |
| 124 | + |
| 125 | + delete_option( 'statify_db_version' ); |
| 126 | + } |
| 127 | +} |
0 commit comments