myCRED_Module

Description

The myCRED_Module is an abstract class, responsible for constructing and supporting each myCRED Module via the Module API. It contains a set of commonly used functions to help you built modules with ease.

As an abstract class, your module must extend this class which will allow you to use any of the provided functions or replace them with your own.

Usage

class My_Custom_Module extends myCRED_Module {
	function __construct( $point_type ) {
		parent::__construct( $module_id, $args, $point_type );
	}
}

Properties

  • $module_id (string)
    The unique module id. Important! Modules will fail and die if no module id is given!
  • $core (object)
    myCRED Settings.
  • $module_name (string)
    Required identifier for settings native to this module.
  • $option_id (string)
    Optional option ID this module uses.
  • $labels (array)
    Menu and Page Title labels. Only used if the module generates a sub page under the “myCRED” menu.
  • $register (boolean)
    Option to register module settings (if used). Defaults to false.
  • $screen_id (string)
    Screen ID for the module page (if used).
  • $menu_pos (int)
    Menu position.

Methods

  1. myCRED_Module( $module_id, $args )Constructs a module. If no module id is set this method will fail and die!
    • $module_id (string) required
      Constructs a module. If no module id is set this method will fail and die!.
    • $args (array)
      Module arguments.
  2. set_settings( $defaults )Populates the modules settings applying defaults if not set.
    • $defaults (array)
      Default settings.
  3. load()Hooks into myCRED. A module can overwrite this with it’s own load() method.
  4. module_pre_init()Runs before ‘init’.
  5. module_init()Runs at ‘init’.
  6. module_admin_init()Runs at ‘admin_init’.
  7. module_widgets_init()Runs at ‘widgets_init’.
  8. call( $call, $callback, $return )Either runs a given function or a class method. If $return is set, the value passed though it is passed on to the function/class method.
    • $call (string)
      Class method name to call. Ignored if function is called.
    • $callback (string)
      Function or Class to call.
    • $return (any)
      Optional value to pass on and return.
  9. add_menu()Adds a new sub menu item to the “myCRED” Admin Menu. Requires $labels and $screen_id to be set!
  10. register_settings()Registers a modules custom settings.
  11. settings_header()Outputs the “click to open” and “click to close” text to the accordion. Only used if the accordion is requested.
  12. admin_page()Outputs the admin page content. This method must be replaced by the custom module or an empty page is returned.
  13. sanitize_settings( $post )Used if the module has custom settings. This method must be replaced when used by the module and proper sanitation of the values must be made!
  14. after_general_settings()Option to insert custom content after the myCRED Core settings.
  15. sanitize_extra_settings( $new_data, $data, $core )If after_general_settings() is used, this method must be replaced by the custom module and sanitize the settings before they are saved into the DB. If not set, no settings are saved!
    • $new_data (array)
      The sanitized and correct data to save in the database.
    • $data (array)
      The initial posted data.
    • $core (object)
      myCRED Settings.

Module Arguments

  • // default
    $args = array(
    	'module_name' => '',
    	'option_id'   => '',
    	'defaults'    => array(),
    	'labels'      => array(
    		'menu'        => '',
    		'page_title'  => ''
    	),
    	'register'    => true,
    	'screen_id'   => '',
    	'add_to_core' => false,
    	'accordion'   => false,
    	'menu_pos'    => 10
    );
  • module_name (string)
    Module name used when modules add settings that can be accessed though myCRED_Settings.
  • option_id (string)
    Optional option id used by this module.
  • defaults (array)
    Default module settings.
  • labels (array)
    Array of labels when the module has it’s own admin page.
  • register (boolean)
    Option to register a modules settings though register_setting(). Should be set to true of option_id is set.
  • screen_id (string)
    A screen id if the module generates a custom admin page.
  • add_to_core (boolean)
    Option to add module settings to the myCRED Settings page. Defaults to false.
  • accordion (boolean)
    Option to include the Accordion script on the module page. Defaults to false.
  • menu_pos (int)
    Menu position.Default positions: Log (10), Hooks (20), Add-ons (30), Settings (99).

Examples

Example 1: A module which generates a new menu item between Log and Hooks called “Custom”.

class My_Custom_Module extends myCRED_Module {
	function __construct() {
		parent::__construct( 'My_Custom_Module', array(
			'module_name' => 'custom',
			'labels'      => array(
				'menu'        => __( 'Custom' ),      // Menu Label
				'page_title'  => __( 'Custom Page' )  // Page Title
			),
			'screen_id'   => 'myCRED_page_custom',
			'menu_pos'    => 15
		) );
	}

	function admin_page() {
		echo 'This is my custom page!';
	}
}

Example 2: A module which does not add a new page but instead adds new settings under myCred Settings.

class My_Custom_Module extends myCRED_Module {
	function __construct() {
		parent::__construct( 'My_Custom_Module', array(
			'module_name' => 'custom',
			'defaults'    => array(
				'foo'   => 1,
				'bar'   => ''
			),
			'register'    => false,
			'add_to_core' => true
		) );
	}

	// Output our settings or content
	function after_general_settings() {
		// We can access our custom settings under $this->$module_name
		$custom_settings = $this->custom;
		echo 'Custom settings output.';
	}

	// Sanitize our settings
	function sanitize_extra_settings( $new_data, $data, $core ) {
		// Our custom settings are found under $data[$module_name]
		$new_data['custom']['foo'] = sanitize_text_field( $data['custom']['foo'] );
		$new_data['custom']['bar'] = sanitize_text_field( $data['custom']['bar'] );
	}
}