Lesson 3 – The MainWP Development Add-on – Main File

The Main Extension File

mainwp-development-extension.php

What’s this Class for?

The bootstrap file is where the important system variables are set needed for the add-on to display and function correctly. It’s responsible for making sure that the MainWP Dashboard plugin is activated & all of the class files within the /class folder are being loaded so that it may be seen within the WordPress Plugins UI as well as within the MainWP UI.

It contains all of the extension’s descriptional details that WordPress needs to display on the WordPress Plugins Page along with the MainWP Hooks that display the menu items for the add-on’s Main Page / Settings Page as well as each Child Site subpage.

File Structure

For each new MainWP add-on you will need to set up the WordPress plugin header & also do a search and replace & replace of all instances of the following words; keep in mind these words ARE CASE SENSITIVE: “development”, “Development”, “DEVELOPMENT” within this file in order for WordPress & MainWP to recognize your new add-on. We will go over what each method does below so you may get things up and running more quickly.

This information is also included within the comments of each file in this documentation.

The Header

This is the information header – it’s required in order for WordPress to properly display the plugin and for WordPress to recognize it. This is the bare minimum information needed to get your extension working. You may replace this information with anything you wish.

<?php
/**
 * Plugin Name: MainWP Development Extension
 * Plugin URI: https://mainwp.com
 * Description: MainWP Development Extension is used for development.
 * Version: 4.0
 * Author: MainWP
 * Author URI: https://mainwp.com
 */

Once the add-on is installed & activated this information will be displayed within the WordPress > Plugin list.

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;

System Variables

Each of these variables are needed in order for the MainWP Add-on to function as intended; You will want to rename these as needed replacing the word “DEVELOPMENT” with your add-ons name.

<?php
if ( ! defined( 'MAINWP_DEVELOPMENT_PLUGIN_FILE' ) ) {
  define( 'MAINWP_DEVELOPMENT_PLUGIN_FILE', __FILE__ );
}

if ( ! defined( 'MAINWP_DEVELOPMENT_PLUGIN_DIR' ) ) {
  define( 'MAINWP_DEVELOPMENT_PLUGIN_DIR', dirname( __FILE__ ) );
}

if ( ! defined( 'MAINWP_DEVELOPMENT_PLUGIN_URL' ) ) {
  define( 'MAINWP_DEVELOPMENT_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}

if ( ! defined( 'MAINWP_DEVELOPMENT_PLUGIN_SLUG' ) ) {
  define( 'MAINWP_DEVELOPMENT_PLUGIN_SLUG', plugin_basename( __FILE__ ) );
}

Class Declaration

The opening of each class file will look similar to this;

Just like above you will want to rename your class declaration by replacing the words “Development” & “development” with your add-on name – everything else may stay the same here:

<?php
class MainWP_Development_Extension_Activator {
  protected $mainwpMainActivated = false;
  protected $childEnabled = false;
  protected $childKey = false;
  protected $childFile;
  protected $plugin_handle = 'mainwp-development-extension';
  protected $product_id = 'MainWP Development Extension';
  protected $software_version = '1.0';
}

Class Methods

__construct()

PHP allows developers to declare constructor methods for classes. Classes which 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 method should be left untouched. It simply tells WordPress to run our “autoload” method as well as check to see if MainWP Dashboard Plugin has been installed and activated – then it adds the necessary WP Action Hooks to link up with the `activate` & `deactivate` methods for the add-on to load correctly. We have commented on each line to explain what they do.

<?php
public function __construct() {

  $this->childFile = __FILE__;

  // Register given function as __autoload() implementation
  spl_autoload_register( array( $this, 'autoload' ) );

  // Register activation and deactivation hooks
  register_activation_hook( __FILE__, array( $this, 'activate' ) );
  register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );

  /**
   * This is a filter similar to adding the management page in WordPress.
   * It calls the function get_this_extension, which adds the extension to the
   * $extensions array. This array is a list of all of the extensions MainWP
   * uses, and the functions that it has to call to show settings for them.
   * In this case, the function is settings.
   */
  add_filter( 'mainwp_getextensions', array( &$this, 'get_this_extension' ) );

  /**
   * This variable checks to see if MainWP is activated. By default it will
   * return false & return admin notices to the user that MainWP Dashboard needs
   * to be activated. If MainWP is activated, then call the function
   * activate_this_plugin.
   */
  $this->mainwpMainActivated = apply_filters( 'mainwp_activated_check', false );

  if ( $this->mainwpMainActivated !== false ) {
    $this->activate_this_plugin();
   } else {
    add_action( 'mainwp_activated', array( &$this, 'activate_this_plugin' ) );
  }
  add_action( 'admin_notices', array( &$this, 'admin_notices' ) );
}

autoload()

This function will go through the /class folder and require all of the files.

The class name is passed in by spl_autoload_register located in the __construct() Method

It is the name of the class that is being instantiated. For example, if you create a new class named MainWP_Development_Api, then $class_name will be MainWP_Development_Api.

The class name is also used to determine the file name. For example, MainWP_Development_Api is in the file name class/class-mainwp-development-api.php.

You will want to rename “Development” to your add-on name.

<?php
  public function autoload( $class_name ) {
    $class_name = str_replace( '_', '-', strtolower( $class_name ) );

  if ( 0 !== strpos( $class_name, 'MainWP\Extensions\Development' ) ) {

    }

  // trim the namespace prefix: MainWP\Extensions\Development\.
  $class_name = substr( $class_name, 30 );
  $class_file = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . str_replace( basename( __FILE__ ), ”, plugin_basename( __FILE__ ) ) . 'class' . DIRECTORY_SEPARATOR . 'class-' . $class_name . '.php';
  if ( file_exists( $class_file ) ) {
    require_once $class_file;
  }
}

get_this_extension()

This method can be left alone; as it is needed to tell MainWP where your add-on is. It calls the method get_this_extension, which adds the add-on to the $extensions array. This array is a list of all of the add-ons MainWP uses. The callback argument points to the method that shows the main page of the add-on. In this case, the method is settings.

<?php
public function get_this_extension( $params ) {
  $params[] = array(
    'plugin'     => __FILE__,
    'api'        => $this->plugin_handle,
    'mainwp'     => true,
    'callback'   => array( &$this, 'settings' ),
    'apiManager' => true,
  );
  return $params;
}

settings()

Displays the add-on page with an adequate header and footer.

You will want to rename “Development” to your add-ons name so that the add-ons pages “tabs” will render.

<?php
public function settings() {
  do_action( 'mainwp_pageheader_extensions', __FILE__ );
  MainWP_Development_Overview::get_instance()->render_tabs();
  do_action( 'mainwp_pagefooter_extensions', __FILE__ );
}

activate_this_plugin()

This method is called when the add-on is activated. It checks to see if MainWP is activated. If it is, then it calls the methods hook_managesites_subpage, hook_get_metaboxes, widgets_screen_options & initiates the main Admin Class that controls the rest of the add-on’s behavior.

You will want to make sure you replace the word “Development” within this method so that everything is pointing to the correct location.

<?php
function activate_this_plugin() {
  $this->mainwpMainActivated = apply_filters( 'mainwp_activated_check', $this->mainwpMainActivated );
  $this->childEnabled = apply_filters( 'mainwp_extension_enabled_check', __FILE__ );
  $this->childKey = $this->childEnabled['key'];

  if ( function_exists( 'mainwp_current_user_can' ) && ! mainwp_current_user_can( 'extension', 'mainwp-development-extension' ) ) {
    return;
  }

  add_filter( 'mainwp_getsubpages_sites', array( &$this, 'hook_managesites_subpage' ), 10, 1 );
  add_filter( 'mainwp_getmetaboxes', array( &$this, 'hook_get_metaboxes' ) );
  add_filter( 'mainwp_widgets_screen_options', array( MainWP_Development_Admin::get_instance(), 'widgets_screen_options' ), 10, 1 );

  MainWP_Development_Admin::get_instance();
}

get_child_key() & get_child_file()

These may be left alone as they are needed for MainWP to function as intended.

<?php
public function get_child_key() {
  return $this->childKey;
}

public function get_child_file() {
  return $this->childFile;
}

admin_notices()

This function displays an admin notice if MainWP is not activated.

You will want to edit the warning message to reflect your add-ons name.

<?php
function admin_notices() {
  global $current_screen;
  if ( $current_screen->parent_base == 'plugins' && $this->mainwpMainActivated == false ) {
  echo '<div class="error"><p>MainWP Development Extension ' . __( 'requires <a href="http://mainwp.com/" target="_blank">MainWP Dashboard Plugin</a> to be activated in order to work. Please install and activate <a href="http://mainwp.com/" target="_blank">MainWP Dashboard Plugin</a> first.', 'mainwp-development-extension' ) . '</p></div>';
  }
}

activate() & deactivate()

These methods are responsible for activating and deactivating the add-on in WordPress. There is no need to alter these for them to function.

<?php
public function activate() {
  $options = array(
    'product_id' => $this->product_id,
    'software_version' => $this->software_version,
  );

  do_action( 'mainwp_activate_extention', $this->plugin_handle, $options );

}

public function deactivate() {
  do_action( 'mainwp_deactivate_extention', $this->plugin_handle );
}

get_metaboxes()

This method adds the metabox (widget) on the MainWP Dashboard Overview page & the Individual Sites Overview page via the ‘mainwp_getmetaboxes’ filter. You will want to rename the word “Development” anywhere within this method so that your Widget displays the correct name of your add-on.

This method has a callback that runs the method MainWP_Development_Widget::render_metabox();

You will want to make sure you replace the word “Development” & “development” within this method so that everything is pointing to the correct location.

<?php
public function hook_get_metaboxes( $metaboxes ) {
  if ( ! $this->childEnabled ) {
    return $metaboxes;
  }
  if ( ! is_array( $metaboxes ) ) {
    $metaboxes = array();
  }
  $metaboxes[] = array(
    'id'            => 'development-widget',
    'plugin'        => $this->childFile,
    'key'           => $this->childKey,
    'metabox_title' => __( 'Development', 'mainwp-development-extension' ),
    'callback'      => array( MainWP_Development_Widget::get_instance(), 'render_metabox' ),
  );

  return $metaboxes;
}