Lesson 3 – The MainWP Development Add-on – Utility

Utility

class-mainwp-development-utility.php

What’s this Class for?

This class is where all the reusale code will be stored.

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_Utility {
  /**
  * 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_Utility
  */
  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() {
  if ( null === $this->option ) {
    $this->option = get_option( $this->option_handle );
  }
}
?>

MainWP Developer Add-on framework comes with some useable utility functions. Ones that are not needed can be removed. 

  • get_setting() – Retrieves an option value based on an option name.
  • update_setting() – Updates an option value based on an option name.
  • get_timestamp() – Retrieves the timestamp
  • format_timestamp() – Formats the timestamp 
  • format_datestamp() – Formats the datestamp
  • ctype_digit() – Checks if all of the characters in the provided string, text, are numerical.
  • map_fields() – Maps site data.
  • esc_content() – Escapes HTML content while certain tags are allowed. 
  • get_websites() – Retrieves sites data.
  • get_nice_url() – Returns site URL without https:// or http:// part.
  • starts_with() – Returns the start of a string.
  • ends_with() – Returns the end of a string.
  • verify_action_nonce() – Nonce verification.