Commit d9ee124d authored by John James Jacoby's avatar John James Jacoby
Browse files

Bump to 2.0.0:

* Extend WP_DB_Table class
* Docs updates
parent 06534466
......@@ -3,7 +3,7 @@ Contributors: johnjamesjacoby, stuttter
Tags: users, signups, admin, multisite
Requires at least: 4.7
Tested up to: 4.8
Stable tag: 1.2.0
Stable tag: 2.0.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=9Q4F4EL5YJ62J
......@@ -82,6 +82,9 @@ http://github.com/stuttter/wp-user-signups
== Changelog ==
= [2.0.0] - 2017-03-15 =
* Extend WP_DB_Table class
= [1.2.0] - 2017-03-03 =
* Fix bug with site column output
* Single-site implementation improvements
......
......@@ -8,7 +8,7 @@
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Description: User signup management for WordPress
* Version: 1.2.0
* Version: 2.0.0
* Text Domain: wp-user-signups
* Domain Path: /wp-user-signups/assets/languages/
*/
......@@ -21,15 +21,6 @@ if ( class_exists( 'WP_User_Signup' ) ) {
return;
}
// Define the table variables
if ( empty( $GLOBALS['wpdb']->signups ) ) {
$GLOBALS['wpdb']->signups = $GLOBALS['wpdb']->base_prefix . 'signups';
$GLOBALS['wpdb']->ms_global_tables[] = 'signups';
}
// Ensure cache is shared
wp_cache_add_global_groups( array( 'user_signups' ) );
// Execute immediately
_wp_user_signups();
......@@ -44,9 +35,10 @@ function _wp_user_signups() {
$plugin_path = wp_user_signups_get_plugin_path();
// Classes
require_once $plugin_path . 'includes/classes/class-wp-db-table.php';
require_once $plugin_path . 'includes/classes/class-wp-db-table-user-signups.php';
require_once $plugin_path . 'includes/classes/class-wp-user-signup.php';
require_once $plugin_path . 'includes/classes/class-wp-user-signup-query.php';
require_once $plugin_path . 'includes/classes/class-wp-user-signups-db-table.php';
// Required Files
require_once $plugin_path . 'includes/functions/admin.php';
......@@ -55,10 +47,16 @@ function _wp_user_signups() {
require_once $plugin_path . 'includes/functions/common.php';
require_once $plugin_path . 'includes/functions/capabilities.php';
require_once $plugin_path . 'includes/functions/hooks.php';
// Tables
new WP_DB_Table_User_Signups();
// Ensure cache is shared
wp_cache_add_global_groups( array( 'user_signups' ) );
}
/**
* Return the plugin's root file
* Return the plugin root file
*
* @since 1.0.0
*
......@@ -69,7 +67,7 @@ function wp_user_signups_get_plugin_file() {
}
/**
* Return the plugin's path
* Return the plugin path
*
* @since 1.0.0
*
......@@ -80,7 +78,7 @@ function wp_user_signups_get_plugin_path() {
}
/**
* Return the plugin's URL
* Return the plugin URL
*
* @since 1.0.0
*
......@@ -98,5 +96,5 @@ function wp_user_signups_get_plugin_url() {
* @return int
*/
function wp_user_signups_get_asset_version() {
return 201611180001;
return 201703150001;
}
<?php
/**
* User Signups Database: WP_DB_Table_User_Signups class
*
* @package Plugins/Users/Signups/Database/Object
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
/**
* Setup the global "blog_aliases" database table
*
* @since 2.0.0
*/
final class WP_DB_Table_User_Signups extends WP_DB_Table {
/**
* @var string Table name
*/
protected $name = 'signups';
/**
* @var string Database version
*/
protected $version = 201703150001;
/**
* @var boolean This is a global table
*/
protected $global = true;
/**
* Setup the database schema
*
* @since 2.0.0
*/
protected function set_schema() {
$this->schema = "signup_id bigint(20) NOT NULL auto_increment,
domain varchar(200) NOT NULL default '',
path varchar(100) NOT NULL default '',
title longtext NOT NULL,
user_login varchar(60) NOT NULL default '',
user_email varchar(100) NOT NULL default '',
registered datetime NOT NULL default '0000-00-00 00:00:00',
activated datetime NOT NULL default '0000-00-00 00:00:00',
active tinyint(1) NOT NULL default '0',
activation_key varchar(50) NOT NULL default '',
meta longtext,
PRIMARY KEY (signup_id),
KEY activation_key (activation_key),
KEY user_email (user_email),
KEY user_login_email (user_login,user_email),
KEY domain_path (domain(140),path(51))";
}
/**
* Handle schema changes
*
* @since 2.0.0
*/
protected function upgrade() {
}
}
<?php
/**
* A Base WordPress Database Table class
*
* @author JJJ
* @link https://jjj.blog
* @version 1.0.0
* @license https://www.gnu.org/licenses/gpl-2.0.html
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'WP_DB_Table' ) ) :
/**
* The base WordPress database table class, which facilitates the creation of
* and schema changes to individual database tables.
*
* This class is intended to be extended for each unique database table,
* including global multisite tables and users tables.
*
* It exists to make managing database tables in WordPress as easy as possible.
*
* Extending this class comes with several automatic benefits:
* - Activation hook makes it great for plugins
* - Tables store their versions in the database independently
* - Tables upgrade via independent upgrade abstract methods
* - Multisite friendly - site tables switch on "switch_blog" action
*
* @since 1.0.0
*/
abstract class WP_DB_Table {
/**
* @var string Table name, without the global table prefix
*/
protected $name = '';
/**
* @var int Database version
*/
protected $version = 0;
/**
* @var boolean Is this table for a site, or global
*/
protected $global = false;
/**
* @var string Database version key (saved in _options or _sitemeta)
*/
protected $db_version_key = '';
/**
* @var string Current database version
*/
protected $db_version = 0;
/**
* @var string Table name
*/
protected $table_name = '';
/**
* @var string Table schema
*/
protected $schema = '';
/**
* @var string Database character-set & collation for table
*/
protected $charset_collation = '';
/**
* @var WPDB Database object (usually $GLOBALS['wpdb'])
*/
protected $db = false;
/** Methods ***************************************************************/
/**
* Hook into queries, admin screens, and more!
*
* @since 1.0.0
*/
public function __construct() {
// Bail if no database object or table name
if ( empty( $GLOBALS['wpdb'] ) || empty( $this->name ) ) {
return;
}
// Setup the database
$this->set_db();
// Get the version of he table currently in the database
$this->get_db_version();
// Add the table to the object
$this->set_wpdb_tables();
// Setup the database schema
$this->set_schema();
// Add hooks to WordPress actions
$this->add_hooks();
}
/** Abstract **************************************************************/
/**
* Setup this database table
*
* @since 1.0.0
*/
protected abstract function set_schema();
/**
* Upgrade this database table
*
* @since 1.0.0
*/
protected abstract function upgrade();
/** Public ****************************************************************/
/**
* Update table version & references.
*
* Hooked to the "switch_blog" action.
*
* @since 1.0.0
*
* @param int $site_id
*/
public function switch_blog( $site_id = 0 ) {
// Update DB version based on the current site
if ( false === $this->global ) {
$this->db_version = get_blog_option( $site_id, $this->db_version_key, false );
}
// Update table references based on th current site
$this->set_wpdb_tables();
}
/**
* Maybe upgrade the database table. Handles creation & schema changes.
*
* Hooked to the "admin_init" action.
*
* @since 1.0.0
*/
public function maybe_upgrade() {
// Bail if no upgrade needed
if ( version_compare( (int) $this->db_version, (int) $this->version, '>=' ) ) {
return;
}
// Include file with dbDelta() for create/upgrade usages
if ( ! function_exists( 'dbDelta' ) ) {
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
}
// Bail if global and upgrading global tables is not allowed
if ( ( true === $this->global ) && ! wp_should_upgrade_global_tables() ) {
return;
}
// Create or upgrade?
$this->exists()
? $this->upgrade()
: $this->create();
// Set the database version
if ( $this->exists() ) {
$this->set_db_version();
}
}
/** Private ***************************************************************/
/**
* Setup the necessary WPDB variables
*
* @since 1.0.0
*/
private function set_db() {
// Setup database
$this->db = $GLOBALS['wpdb'];
$this->name = sanitize_key( $this->name );
// Maybe create database key
if ( empty( $this->db_version_key ) ) {
$this->db_version_key = "wpdb_{$this->name}_version";
}
}
/**
* Modify the database object and add the table to it
*
* This is necessary to do directly because WordPress does have a mechanism
* for manipulating them safely. It's pretty fragile, but oh well.
*
* @since 1.0.0
*/
private function set_wpdb_tables() {
// Global
if ( true === $this->global ) {
$prefix = $this->db->get_blog_prefix( 0 );
$this->db->{$this->name} = "{$prefix}{$this->name}";
$this->db->ms_global_tables[] = $this->name;
// Site
} else {
$prefix = $this->db->get_blog_prefix( null );
$this->db->{$this->name} = "{$prefix}{$this->name}";
$this->db->tables[] = $this->name;
}
// Set the table name locally
$this->table_name = $this->db->{$this->name};
// Charset
if ( ! empty( $this->db->charset ) ) {
$this->charset_collation = "DEFAULT CHARACTER SET {$this->db->charset}";
}
// Collation
if ( ! empty( $this->db->collate ) ) {
$this->charset_collation .= " COLLATE {$this->db->collate}";
}
}
/**
* Set the database version to the table version.
*
* Saves global table version to "wp_sitemeta" with a site ID of -1
*
* @since 1.0.0
*/
private function set_db_version() {
// Set the class version
$this->db_version = $this->version;
// Update the DB version
( true === $this->global )
? update_network_option( -1, $this->db_version_key, $this->version )
: update_option( $this->db_version_key, $this->version );
}
/**
* Get the table version from the database.
*
* Gets global table version from "wp_sitemeta" with a site ID of -1
*
* @since 1.0.0
*/
private function get_db_version() {
$this->db_version = ( true === $this->global )
? get_network_option( -1, $this->db_version_key, false )
: get_option( $this->db_version_key, false );
}
/**
* Add class hooks to WordPress actions
*
* @since 1.0.0
*/
private function add_hooks() {
// Activation hook
register_activation_hook( __FILE__, array( $this, 'maybe_upgrade' ) );
// Add table to the global database object
add_action( 'switch_blog', array( $this, 'switch_blog' ) );
add_action( 'admin_init', array( $this, 'maybe_upgrade' ) );
}
/**
* Create the table
*
* @since 1.0.0
*/
private function create() {
// Run CREATE TABLE query
$created = dbDelta( array( "CREATE TABLE {$this->table_name} ( {$this->schema} ) {$this->charset_collation};" ) );
// Was anything created?
return ! empty( $created );
}
/**
* Check if table already exists
*
* @since 1.0.0
*
* @return bool
*/
private function exists() {
$query = "SHOW TABLES LIKE %s";
$like = $this->db->esc_like( $this->table_name );
$prepared = $this->db->prepare( $query, $like );
$table_exist = $this->db->get_var( $prepared );
return ! empty( $table_exist );
}
}
endif;
<?php
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
/**
* Main WP User Sign-ups class
*
* This class facilitates the following functionality:
*
* - Creates & maintains the `wp_signups` table
* - Adds `wp_signups` to the main database object when appropriate
*
* @since 1.0.0
*/
final class WP_User_Signup_DB {
/**
* @var string Plugin version
*/
public $version = '1.0.0';
/**
* @var string Database version
*/
public $db_version = 201603100001;
/**
* @var string Database version key
*/
public $db_version_key = 'wpdb_user_signups_version';
/**
* @var object Database object (usually $GLOBALS['wpdb'])
*/
private $db = false;
/** Methods ***************************************************************/
/**
* Hook into queries, admin screens, and more!
*
* @since 1.0.0
*/
public function __construct() {
// Activation hook
register_activation_hook( __FILE__, array( $this, 'activate' ) );
// Setup plugin
$this->db = $GLOBALS['wpdb'];
// Force table on to the global database object
add_action( 'init', array( $this, 'add_table_to_db_object' ) );
add_action( 'switch_blog', array( $this, 'add_table_to_db_object' ) );
// Check if DB needs upgrading
if ( is_admin() ) {
add_action( 'admin_init', array( $this, 'admin_init' ) );
}
}
/**
* Administration area hooks
*
* @since 1.0.0
*/
public function admin_init() {
$this->maybe_upgrade_database();
}
/**
* Modify the database object and add the table to it
*
* This is necessary to do directly because WordPress does have a mechanism
* for manipulating them safely. It's pretty fragile, but oh well.
*
* @since 1.0.0
*/
public function add_table_to_db_object() {
$this->db->signups = "{$this->db->base_prefix}signups";
$this->db->ms_global_tables[] = "signups";
}
/**
* Install this plugin on a specific site
*
* @since 1.0.0
*/
public function install() {
$this->upgrade_database();
}
/**
* Activation hook
*
* Handles both single & multi site installations
*
* @since 1.0.0
*
* @param bool $network_wide
*/
public function activate() {
$this->install();
}
/**
* Should a database update occur
*
* Runs on `admin_init`
*
* @since 1.0.0
*/
private function maybe_upgrade_database() {
// Check DB for version
$db_version = get_network_option( -1, $this->db_version_key );
// Needs
if ( (int) $db_version < $this->db_version ) {
$this->upgrade_database( $db_version );
}
}
/**
* Create the database table
*
* @since 1.0.0
*
* @param int $old_version
*/
private function upgrade_database( $old_version = 0 ) {
// The main column alter
if ( version_compare( (int) $old_version, $this->db_version, '>=' ) ) {
return;
}