Skip to content

Commit

Permalink
add new statifymeta table
Browse files Browse the repository at this point in the history
  • Loading branch information
jujoko7CF authored and stklcode committed Oct 18, 2023
1 parent e789310 commit e3d991a
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 100 deletions.
5 changes: 2 additions & 3 deletions inc/class-statify-install.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ private static function _apply() {
);
}

// Create the actual tables.
Statify_Table::init();
Statify_Table::create();
// Initialize the database schema.
Statify_Schema::init();
}
}
127 changes: 127 additions & 0 deletions inc/class-statify-schema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/**
* Statify: Statify_Schema class
*
* This file contains class for the plugin's DB scheme handling.
*
* @package Statify
* @since 2.0.0
*/

// Quit if accessed outside WP context.
defined( 'ABSPATH' ) || exit;

/**
* Statify DB Schema
*
* @since 2.0.0
*/
class Statify_Schema {
/**
* Database tables
*
* @var string[]
*/
public static $tables = array(
'statify',
'statifymeta',
);

/**
* Needed statify db version for current plugin version
*
* @var string
*/
public static $db_version = '2.0.0';

/**
* Definition of the custom tables.
*
* @since 2.0.0
* @version 2.0.0
*/
public static function init() {
// Global.
global $wpdb;

foreach ( static::$tables as $table ) {
$wpdb->tables[] = $table;
$wpdb->$table = $wpdb->get_blog_prefix() . $table;
}

self::maybe_create_tables();
}

/**
* Create the tables.
*
* @since 2.0.0
* @version 2.0.0
*/
public static function maybe_create_tables() {
$current_db_version = get_option( 'statify_db_version', '1.8.4' );
if ( $current_db_version === self::$db_version ) {
return;
}

// Global.
global $wpdb, $charset_collate;

/**
* Use same index length like the WordPress core
*
* @see wp_get_db_schema()
*/
$max_index_length = 191;

// Include.
require_once ABSPATH . 'wp-admin/includes/upgrade.php';

// Create statify table.
dbDelta(
"CREATE TABLE {$wpdb->statify} (
id bigint(20) unsigned NOT NULL auto_increment,
created date NOT NULL default '0000-00-00',
referrer varchar(255) NOT NULL default '',
target varchar(255) NOT NULL default '',
hits integer NOT NULL default 1,
PRIMARY KEY (id),
KEY referrer (referrer),
KEY target (target),
KEY created (created)
) {$charset_collate};"
);

// Create statifymeta table.
dbDelta(
"CREATE TABLE {$wpdb->statifymeta} (
meta_id bigint(20) unsigned NOT NULL auto_increment,
statify_id bigint(20) unsigned NOT NULL default 0,
meta_key varchar(255) default NULL,
meta_value longtext,
PRIMARY KEY (meta_id),
KEY statify_id (statify_id),
KEY meta_key (meta_key({$max_index_length}))
) {$charset_collate};"
);

update_option( 'statify_db_version', self::$db_version );
}

/**
* Remove the custom tables.
*
* @since 2.0.0
* @version 2.0.0
*/
public static function drop_tables() {
global $wpdb;

// Remove.
foreach ( static::$tables as $table ) {
$wpdb->query( "DROP TABLE IF EXISTS `{$wpdb->$table}`" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}

delete_option( 'statify_db_version' );
}
}
90 changes: 0 additions & 90 deletions inc/class-statify-table.php

This file was deleted.

8 changes: 4 additions & 4 deletions inc/class-statify-uninstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ private static function _apply() {
// Delete options.
delete_option( 'statify' );

// Init table.
Statify_Table::init();
// Initialize the database schema.
Statify_Schema::init();

// Delete table.
Statify_Table::drop();
// Delete tables.
Statify_Schema::drop_tables();
}
}
4 changes: 2 additions & 2 deletions inc/class-statify.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public static function init() {
return;
}

// Table init.
Statify_Table::init();
// Initialize the database schema.
Statify_Schema::init();

// Plugin options.
self::$_options = wp_parse_args(
Expand Down
2 changes: 1 addition & 1 deletion statify.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function statify_autoload( $class ) {
'Statify_Uninstall',
'Statify_Deactivate',
'Statify_Settings',
'Statify_Table',
'Statify_Schema',
'Statify_XMLRPC',
'Statify_Cron',
);
Expand Down

0 comments on commit e3d991a

Please sign in to comment.