| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base class for creating custom settings views. |
| 4 |
* |
| 5 |
* @package Members |
| 6 |
* @subpackage Admin |
| 7 |
* @author The MemberPress Team |
| 8 |
* @copyright Copyright (c) 2009 - 2018, The MemberPress Team |
| 9 |
* @link https://members-plugin.com/ |
| 10 |
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 11 |
*/ |
| 12 |
namespace Members\Admin; |
| 13 |
|
| 14 |
defined('ABSPATH') || exit; |
| 15 |
/** |
| 16 |
* Settings view base class. |
| 17 |
* |
| 18 |
* @since 2.0.0 |
| 19 |
* @access public |
| 20 |
*/ |
| 21 |
abstract class View { |
| 22 |
|
| 23 |
/** |
| 24 |
* Name/ID for the group. |
| 25 |
* |
| 26 |
* @since 2.0.0 |
| 27 |
* @access protected |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
public $name = ''; |
| 31 |
|
| 32 |
/** |
| 33 |
* Internationalized text label for the group. |
| 34 |
* |
| 35 |
* @since 2.0.0 |
| 36 |
* @access protected |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
public $label = ''; |
| 40 |
|
| 41 |
/** |
| 42 |
* Priority (order) the control should be output. |
| 43 |
* |
| 44 |
* @since 2.0.0 |
| 45 |
* @access public |
| 46 |
* @var int |
| 47 |
*/ |
| 48 |
public $priority = 10; |
| 49 |
|
| 50 |
/** |
| 51 |
* A user role capability required to show the control. |
| 52 |
* |
| 53 |
* @since 2.0.0 |
| 54 |
* @access public |
| 55 |
* @var string|array |
| 56 |
*/ |
| 57 |
public $capability = 'manage_options'; |
| 58 |
|
| 59 |
/** |
| 60 |
* Magic method to use in case someone tries to output the object as a string. |
| 61 |
* We'll just return the name. |
| 62 |
* |
| 63 |
* @since 2.0.0 |
| 64 |
* @access public |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
public function __toString() { |
| 68 |
return $this->name; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Register a new object. |
| 73 |
* |
| 74 |
* @since 2.0.0 |
| 75 |
* @access public |
| 76 |
* @param string $name |
| 77 |
* @param array $args { |
| 78 |
* @type string $label Internationalized text label. |
| 79 |
* @type string $icon Dashicon icon in the form of `dashicons-icon-name`. |
| 80 |
* @type string $callback Callback function for outputting the content for the view. |
| 81 |
* } |
| 82 |
* @return void |
| 83 |
*/ |
| 84 |
public function __construct( $name, $args = array() ) { |
| 85 |
|
| 86 |
foreach ( array_keys( get_object_vars( $this ) ) as $key ) { |
| 87 |
|
| 88 |
if ( isset( $args[ $key ] ) ) |
| 89 |
$this->$key = $args[ $key ]; |
| 90 |
} |
| 91 |
|
| 92 |
$this->name = sanitize_key( $name ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Runs on the `load-{$page}` hook |
| 97 |
* |
| 98 |
* @since 2.0.0 |
| 99 |
* @access public |
| 100 |
* @return void |
| 101 |
*/ |
| 102 |
public function load() {} |
| 103 |
|
| 104 |
/** |
| 105 |
* Enqueue scripts/styles for the control. |
| 106 |
* |
| 107 |
* @since 2.0.0 |
| 108 |
* @access public |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
public function enqueue() {} |
| 112 |
|
| 113 |
/** |
| 114 |
* Register settings for the view. |
| 115 |
* |
| 116 |
* @since 2.0.0 |
| 117 |
* @access public |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
public function register_settings() {} |
| 121 |
|
| 122 |
/** |
| 123 |
* Add help tabs for the view. |
| 124 |
* |
| 125 |
* @since 2.0.0 |
| 126 |
* @access public |
| 127 |
* @return void |
| 128 |
*/ |
| 129 |
public function add_help_tabs() {} |
| 130 |
|
| 131 |
/** |
| 132 |
* Output the content for the view. |
| 133 |
* |
| 134 |
* @since 2.0.0 |
| 135 |
* @access public |
| 136 |
* @return void |
| 137 |
*/ |
| 138 |
public function template() {} |
| 139 |
|
| 140 |
/** |
| 141 |
* Checks if the control should be allowed at all. |
| 142 |
* |
| 143 |
* @since 2.0.0 |
| 144 |
* @access public |
| 145 |
* @return bool |
| 146 |
*/ |
| 147 |
public function check_capabilities() { |
| 148 |
|
| 149 |
if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) |
| 150 |
return false; |
| 151 |
|
| 152 |
return true; |
| 153 |
} |
| 154 |
} |
| 155 |
|