Skip to content

Instantly share code, notes, and snippets.

@robertholf
Created September 10, 2016 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertholf/e868509028e85de5a014678e26dfe111 to your computer and use it in GitHub Desktop.
Save robertholf/e868509028e85de5a014678e26dfe111 to your computer and use it in GitHub Desktop.
WordPress Plugin
<?php
/*
* RBAgency_Init Class
*
* These are core functions needed to enable a WordPress plugin shell
* and handle common plugin functions like activation & uninstall, etc.
*/
class RBAgency_Init {
// *************************************************************************************************** //
/*
*
*/
public static function init(){
/*
* Internationalization
*/
// Identify Folder for PO files
load_plugin_textdomain( RBAGENCY_TEXTDOMAIN, false, basename( dirname( __FILE__ ) ) . '/assets/translation/' );
// Register Settings
add_action('admin_init', array('RBAgency_Init', 'do_register_settings'));
}
// *************************************************************************************************** //
/*
* Plugin Activation
* Run when the plugin is installed.
*/
public static function activation(){
// Required for all WordPress database manipulations
global $wpdb;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
/*
* Check Permissions
*/
// Does the user have permission to activate the plugin
if ( !current_user_can('activate_plugins') )
return;
// Check Admin Referer
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "activate-plugin_{$plugin}" );
/*
* Setup Required Directories
*/
// Create Upload Directory
if (!is_dir(RBAGENCY_UPLOADPATH)) {
@mkdir(RBAGENCY_UPLOADPATH, 0755);
@chmod(RBAGENCY_UPLOADPATH, 0777);
}
/*
* Initialize Options
*/
// Update the options in the database
if(!get_option("rb_agency_options")) {
// Set Default Options
$rb_agency_options_arr = array(
/*... TRUNCATED FOR SECUIRTY ... */
);
// Add Options
//add_option("rb_agency_options",$rb_agency_options_arr);
update_option("rb_agency_options",$rb_agency_options_arr);
}
/*
* License Key
*/
// Set license key via the RB_AGENCY_LICENSE constant or the $rb_agency_LICENSE variable
global $rb_agency_LICENSE;
// Set Constant
$license_key = defined("RB_AGENCY_LICENSE") && empty($rb_agency_LICENSE) ? RB_AGENCY_LICENSE : $rb_agency_LICENSE;
if(!empty($license_key)) {
// Store Key
update_option("rb_agency_license", md5($license_key));
}
/*
* Install Schema
*/
// Profile
$sql = "CREATE TABLE IF NOT EXISTS " . table_agency_profile
/*... TRUNCATED FOR SECUIRTY ... */
/*
* Flush rewrite rules
*/
// Flush rewrite rules
self::flush_rules();
}
/*
* Plugin Deactivation
* Cleanup when complete
*/
public static function deactivation(){
/*
// Does user have correct permissions?
if ( ! current_user_can( 'activate_plugins' ) )
return;
// Is it coming from the right referer?
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "deactivate-plugin_{$plugin}" );
*/
// TODO: Enhance
}
/*
* Plugin Uninstall
* Cleanup when complete
*/
public static function uninstall(){
// Permission Granted... Remove
global $wpdb; // Required for all WordPress database manipulations
// Drop the tables
$wpdb->query("DROP TABLE " . table_agency_xxx);
/*... TRUNCATED FOR SECUIRTY ... */
// Delete Saved Settings
delete_option('rb_agency_options');
/*
// Deactivate Plugin
$thepluginfile = "rb-agency/rb-agency.php";
$current = get_settings('active_plugins');
array_splice($current, array_search( $thepluginfile, $current), 1 );
update_option('active_plugins', $current);
do_action('deactivate_' . $thepluginfile );
*/
// Redirect back to Plugins
echo "<div style=\"padding:50px;font-weight:bold;\"><p>". __("Almost done...", RBAGENCY_TEXTDOMAIN) ."</p><h1>". __("please uninstall on plugins page.", RBAGENCY_TEXTDOMAIN) ."</h1><a href=\"plugins.php?deactivate=true\">". __("Please click here to complete the uninstallation process", RBAGENCY_TEXTDOMAIN) ."</a></h1></div>";
die;
}
/*
* Register Settings
* Register Settings group
*/
public static function do_register_settings() {
register_setting('rb-agency-settings-group', 'rb_agency_options'); //, 'rb_agency_options_validate'
register_setting('rb-agency-settings-layout-group', 'rb_agency_layout_options');
register_setting('rb-agency-dummy-settings-group', 'rb_agency_dummy_options'); //, setup dummy profile options
}
/*
* License
* Updates and License related
*/
// Get License Key
public static function get_key(){
return get_option("rb_agency_license");
}
// *************************************************************************************************** //
/*
* Flush Rewrite Rules
* Remember to flush_rules() when adding rules
*/
public static function flush_rules(){
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
// *************************************************************************************************** //
/*
* Update Needed
* Is this an updated version of the software and needs database upgrade?
*/
public static function update_check(){
// Hold the version in a seprate option
if(!get_option("rb_agency_version")) {
update_option("rb_agency_version", RBAGENCY_VERSION);
} else {
// Version Exists, but is it out of date?
if(get_option("rb_agency_version") <> RBAGENCY_VERSION){
include_once(RBAGENCY_PLUGIN_DIR .'/upgrade.php');
} else {
// Namaste, version number is correct
}
}
}
/*
* Upgrade Check
* Is there a newer version of the software available to upgrade to?
*/
public static function upgrade_check(){
// TODO:
//if(!class_exists("RBAgency_Update"))
//require_once("update.php");
//return RBAgency_Update::check_version($update_plugins_option, true);
}
// *************************************************************************************************** //
/*
* Diagnostics
*/
// Check Setup
public static function setup_check(){
// Get Options
$options = get_option('arez_options'); // TODO
// Check if missing permalinks
if ( isset($options['authorized']) && ! $options['authorized'] ) {
// Hide if on Settings Page
if ( (isset($_GET["page"]) && $_GET["page"] == 'arez') || (isset($_GET["page"]) && $_GET["page"] == 'arez-settings') ) {
} else {
echo '<div class="updated"><p>ActivityRez Plugin ready for setup. <a href="'. admin_url("admin.php?page=arez") .'">Click here to get started</a>.</p></div>';
}
}
}
// Check Permalinks
public static function permalinks_check(){
// Check if missing permalinks
if ( ! get_option('permalink_structure') ) {
// Check if we are already on settings page
if ( get_option( 'rbagency_permalinkignore' ) !== true ) {
// Hide if on Settings Page
if (isset($_GET["page"]) && $_GET["page"] == 'arez-settings') {
} else {
echo '<div class="error"><p>WARNING: Your permalinks are not set. <a href="'. admin_url("admin.php?page=arez-settings") .'">Click here to resolve</a>.</p><span class="dismiss"><a href="'. admin_url("admin.php?page=arez-settings&action=permalink-dismiss") .'">Dismiss</a></div>';
}
}
}
}
// Check Permalinks
public static function permalinks_change(){
global $wp_rewrite;
$wp_rewrite->set_permalink_structure('/%category%/%postname%/');
$wp_rewrite->flush_rules();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment