/*
Plugin Name: MTM WordPress Deployment Updates
Description: Runs update routines allowing code based database updates.
Author: Metal Toad Media
Version: 1.0
*/
add_action( 'admin_init', 'deployment_admin_init' );
/**
* Attached to init. Runs any necessary update routines
*/
function deployment_admin_init() {
// What is the current version of this plugin?
$deployment_version = 3;
// What is the current version in the db
$db_version = get_option( 'deployment_version', 0 );
// Is the db out of date?
if ( $db_version < $deployment_version ) {
// If so, loop over all subsequent version numbers and attempt to run corresponding deployment_update_N functions
for ( $version = $db_version + 1; $version <= $deployment_version; $version ++ ) {
if ( function_exists( 'deployment_update_' . $version ) ) {
$success = call_user_func( 'deployment_update_' . $version );
// If the function returns a boolean false, log an error and bail out. Subsequent updates may rely on this update
// so we shouldn't proceed any further.
if ( $success === FALSE ) {
// @TODO: log error here
break;
}
}
// If we've reached this far without error, update the db version
update_option( 'deployment_version', $version );
}
// @TODO: output update summary on success
}
}
/**
* Update functions.
*/
/**
* Disable the wordpress-meta-description plugin.
* Enable and configure the add-meta-tags plugin
*/
function deployment_update_1() {
// Include the plugin.php file so you have access to the activate_plugin() function
require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
deactivate_plugins( WP_PLUGIN_DIR . '/wordpress-meta-description/wp-meta-description.php' );
activate_plugins( WP_PLUGIN_DIR . '/add-meta-tags/add-meta-tags.php' );
update_option( 'add_meta_tags_opts', array(
'settings_version' => 2,
'site_description' => 'Metal Toad Media',
'site_keywords' => 'Phylum: Chordata, Kingdom: Animalia, Class: Amphibia',
'global_keywords' => 'boreal, arroyo, amargosa',
'site_wide_meta' => '',
'auto_description' => '1',
'auto_keywords' => '1',
'auto_opengraph' => '0',
'auto_dublincore' => '0',
'noodp_description' => '0',
'noindex_search_results' => '1',
'noindex_date_archives' => '0',
'noindex_category_archives' => '0',
'noindex_tag_archives' => '0',
'noindex_author_archives' => '0',
'copyright_url' => '',
'default_image_url' => '',
'i_have_donated' => '0',
) );
return TRUE;
}
// Add another user
function deployment_update_2() {
wp_create_user( 'trevor', 'thefr0g', 'trevor@nospam.dev' );
return TRUE;
}
// Add table to the database
// https://codex.wordpress.org/Creating_Tables_with_Plugins
function deployment_update_3() {
global $mtm_db_version;
$mtm_db_version = '1.0';
global $wpdb;
global $mtm_db_version;
$table_name = $wpdb->prefix . 'mtm_toads';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
name tinytext NOT NULL,
text text NOT NULL,
url varchar(55) DEFAULT '' NOT NULL,
UNIQUE KEY id (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
add_option( 'mtm_db_version', $mtm_db_version );
}