Lesson 3 – The MainWP Development Add-on – Admin
Admin
class-mainwp-development-admin.php
What’s this Class for?
This class is responsible for initializing “localization”, enqueueing all js & css & checking if the add-on needs to be updated. This class takes care of firing off the initial add-on “install” script as well as firing off the add-ons instances so that they are available for use.
Namespaces
Each PHP file with a MainWP add-on will need a unique namespace added to the top of each file just under the opening.
<?php
namespace MainWP\Extensions\Development;Class Declaration
Just like before you will want to rename your class declaration by replacing the word “Development” with your add-ons name – everything else may stay the same here:
<?php
class MainWP_Development_Admin {
/**
* Static variable to hold the single instance of the class.
*
* @static
*
* @var mixed Default null
*/
static $instance = null;
public static $instance = null;
/**
* Get Instance
*
* Creates a public static instance of this class file.
*
* @static
*
* @return MainWP_Development_Admin
*/
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
}Class Methods
__construct()
PHP allows developers to declare constructor methods for classes. Classes that have a constructor method call this method on each newly created object, so it is suitable for any initialization that the object may need before it is used.
This is where we will load all of our actions, and filters & Initiate the rest of our add-ons class files.
<?php
public function __construct() {
add_action( 'init', array( &$this, 'init' ) );
add_action( 'init', array( &$this, 'localization' ) );
}
?>init()
<?php
public function init() {
add_filter( 'plugin_row_meta', array( &$this, 'plugin_row_meta' ), 10, 2 );
add_action( 'admin_init', array( &$this, 'admin_init' ) );
MainWP_Development_DB::get_instance()->init();
?>localization()
<?php
public function localization() {
load_plugin_textdomain( 'mainwp-development-extension', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
?>plugin_row_meta()
Displays the meta in the plugin row on the WP > Plugins > Installed Plugins page. You will need to replace the words “development” in this file.
<?php
public function plugin_row_meta( $plugin_meta, $plugin_file ) {
if ( 'mainwp-development-extension/mainwp-development-extension.php' != $plugin_file ) {
return $plugin_meta;
}
$slug = basename( $plugin_file, '.php' );
$plugin_meta[] = '<a href="?do=checkUpgrade" title="Check for updates.">Check for Update</a>';
return $plugin_meta;
}
?>admin_enqueue_scripts()
This method is responsible for loading all JS & CSS for the add-on.
You will want to rename the words “Development”, “development” & “DEVELOPMENT” within this method.
<?php
public function admin_enqueue_scripts() {
wp_enqueue_style( 'mainwp-development-extension', MAINWP_DEVELOPMENT_PLUGIN_URL . 'css/mainwp-development-extension.css', array(), $this->version );
wp_enqueue_script( 'mainwp-development-extension', MAINWP_DEVELOPMENT_PLUGIN_URL . 'js/mainwp-development-extension.js', array(), $this->version, true );
}
?>widget_screen_options()
This method adds the hide checkbox option on the screen options page that is responsible for hiding the Overview & or Sites Overview Widget from view.
<?php
public function widgets_screen_options( $input ) {
$input['advanced-development-widget'] = __( 'Development Widget', 'mainwp-development-extension' );
return $input;
}
?>