llms-abstract-admin-tool.php 3.51 KB
Newer Older
cyrille's avatar
cyrille committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
<?php
/**
 * Base for tools listed on the LifterLMS -> Status -> Tools & Utilities screen
 *
 * @package LifterLMS/Abstracts/Classes
 *
 * @since 3.37.19
 * @version 5.0.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * LLMS_Abstract_Admin_Tool
 *
 * @since 3.37.19
 */
abstract class LLMS_Abstract_Admin_Tool {

	/**
	 * Tool ID
	 *
	 * @var string
	 */
	protected $id = '';

	/**
	 * Tool Priority
	 *
	 * Passed to the `llms_status_tools` filter when registering the tool.
	 *
	 * @var integer
	 */
	protected $priority = 10;

	/**
	 * Process the tool.
	 *
	 * This method should do whatever the tool actually does.
	 *
	 * By the time this tool is called a nonce and the user's capabilities have already been checked.
	 *
	 * @since 3.37.19
	 *
	 * @return mixed
	 */
	abstract protected function handle();

	/**
	 * Retrieve a description of the tool
	 *
	 * This is displayed on the right side of the tool's list before the button.
	 *
	 * @since 3.37.19
	 *
	 * @return string
	 */
	abstract protected function get_description();

	/**
	 * Retrieve the tool's label
	 *
	 * The label is the tool's title. It's displayed in the left column on the tool's list.
	 *
	 * @since 3.37.19
	 *
	 * @return string
	 */
	abstract protected function get_label();

	/**
	 * Retrieve the tool's button text
	 *
	 * @since 3.37.19
	 *
	 * @return string
	 */
	abstract protected function get_text();

	/**
	 * Static constructor.
	 *
	 * @since 3.37.19
	 *
	 * @return void
	 */
	public function __construct() {

		add_action( 'llms_status_tool', array( $this, 'maybe_handle' ) );
		add_filter( 'llms_status_tools', array( $this, 'register' ), $this->priority );

	}

	/**
	 * Processes the tool if the submitted tool matches the tool's ID.
	 *
	 * @since 3.37.19
	 * @since 5.0.0 Add before and after action hooks.
	 *
	 * @param string tool_id ID of the submitted tool.
	 * @return mixed|false
	 */
	public function maybe_handle( $tool_id ) {

		if ( $this->should_load() && $this->id === $tool_id ) {

			/**
			 * Action run prior to running an admin tool's main `handle()` method.
			 *
			 * The dynamic portion of this hook `${tool_id}` refers to the unique ID
			 * of the admin tool.
			 *
			 * @since 5.0.0
			 *
			 * @param object $tool_class Instance of the extending tool class.
			 */
			do_action( "llms_before_handle_tool_{$tool_id}", $this );

			$handled = $this->handle();

			/**
			 * Action run prior to running an admin tool's main `handle()` method.
			 *
			 * The dynamic portion of this hook `${tool_id}` refers to the unique ID
			 * of the admin tool.
			 *
			 * @since 5.0.0
			 *
			 * @param object $tool_class Instance of the extending tool class.
			 */
			do_action( "llms_after_handle_tool_{$tool_id}", $this );

			return $handled;

		}

		return false;

	}

	/**
	 * Register the tool.
	 *
	 * @since 3.37.19
	 *
	 * @see llms_status_tools (filter)
	 *
	 * @param array[] $tools Array of tool definitions.
	 * @return array[]
	 */
	public function register( $tools ) {

		if ( ! $this->should_load() ) {
			return $tools;
		}

		$tools[ $this->id ] = array(
			'description' => $this->get_description(),
			'label'       => $this->get_label(),
			'text'        => $this->get_text(),
		);

		return $tools;

	}

	/**
	 * Conditionally load the tool
	 *
	 * This stub can be overridden by the tool to provide custom logic to determine
	 * whether or not the tool should be loaded and registered.
	 *
	 * @since 3.37.19
	 *
	 * @return boolean Return `true` to load the tool and `false` to not load it.
	 */
	protected function should_load() {
		return true;
	}

}