class-module.php
246 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The Module |
| 4 | * |
| 5 | * @since 1.0.32 |
| 6 | * @package RankMath |
| 7 | * @subpackage RankMath\Module |
| 8 | * @author Rank Math <support@rankmath.com> |
| 9 | */ |
| 10 | |
| 11 | namespace RankMath\Module; |
| 12 | |
| 13 | use RankMath\Helper; |
| 14 | |
| 15 | defined( 'ABSPATH' ) || exit; |
| 16 | |
| 17 | /** |
| 18 | * Module class. |
| 19 | */ |
| 20 | class Module { |
| 21 | |
| 22 | /** |
| 23 | * Module id. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | private $id = ''; |
| 28 | |
| 29 | /** |
| 30 | * Module arguments. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | private $args = ''; |
| 35 | |
| 36 | /** |
| 37 | * The Constructor. |
| 38 | * |
| 39 | * @param string $id Module unique id. |
| 40 | * @param array $args Module configuration. |
| 41 | */ |
| 42 | public function __construct( $id, $args ) { |
| 43 | $this->id = $id; |
| 44 | $this->args = $args; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Getter. |
| 49 | * |
| 50 | * @param string $key Key to get data for. |
| 51 | * @param mixed $default_value Defaul value if not found. |
| 52 | * |
| 53 | * @return mixed |
| 54 | */ |
| 55 | public function get( $key, $default_value = '' ) { |
| 56 | return isset( $this->args[ $key ] ) ? $this->args[ $key ] : $default_value; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Has. |
| 61 | * |
| 62 | * @param string $key Key to check data. |
| 63 | * |
| 64 | * @return bool |
| 65 | */ |
| 66 | public function has( $key ) { |
| 67 | return isset( $this->args[ $key ] ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get module id. |
| 72 | * |
| 73 | * @return string |
| 74 | */ |
| 75 | public function get_id() { |
| 76 | return $this->id; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get module icon. |
| 81 | * |
| 82 | * @return string |
| 83 | */ |
| 84 | public function get_icon() { |
| 85 | return isset( $this->args['icon'] ) ? $this->args['icon'] : 'dashicons-category'; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Echo the setting link. |
| 90 | */ |
| 91 | public function the_link() { |
| 92 | if ( empty( $this->args['settings'] ) ) { |
| 93 | return; |
| 94 | } |
| 95 | ?> |
| 96 | <a href="<?php echo esc_url( $this->args['settings'] ); ?>" class="module-settings button button-secondary"><?php esc_html_e( 'Settings', 'rank-math' ); ?></a> |
| 97 | <?php |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Is module disabled. |
| 102 | * |
| 103 | * @return bool |
| 104 | */ |
| 105 | public function is_disabled() { |
| 106 | return isset( $this->args['disabled'] ) && $this->args['disabled']; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Does module has BETA version? |
| 111 | * |
| 112 | * @return bool |
| 113 | */ |
| 114 | public function is_betabadge() { |
| 115 | return isset( $this->args['betabadge'] ) && $this->args['betabadge']; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Does module has PRO version? |
| 120 | * |
| 121 | * @return bool |
| 122 | */ |
| 123 | public function is_probadge() { |
| 124 | return isset( $this->args['probadge'] ) && $this->args['probadge']; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Is module upgradeable? |
| 129 | * |
| 130 | * @return bool |
| 131 | */ |
| 132 | public function is_upgradeable() { |
| 133 | return isset( $this->args['upgradeable'] ) && $this->args['upgradeable']; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Is PRO module. |
| 138 | * |
| 139 | * @return bool |
| 140 | */ |
| 141 | public function is_pro_module() { |
| 142 | return isset( $this->args['probadge'] ) && $this->args['probadge'] && ! defined( 'RANK_MATH_PRO_FILE' ); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Get the module dependencies. |
| 147 | * |
| 148 | * @return array |
| 149 | */ |
| 150 | public function get_dependencies() { |
| 151 | return isset( $this->args['dep_modules'] ) ? $this->args['dep_modules'] : []; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Is module disabled. |
| 156 | * |
| 157 | * @return bool |
| 158 | */ |
| 159 | public function is_hidden() { |
| 160 | if ( Helper::is_advanced_mode() ) { |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | return in_array( $this->get_id(), [ '404-monitor', 'acf', 'bbpress', 'buddypress', 'redirections', 'role-manager', 'image-seo' ], true ); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Is module admin only. |
| 169 | * |
| 170 | * @return bool |
| 171 | */ |
| 172 | public function is_admin() { |
| 173 | return $this->only( 'admin' ); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Is module internal. |
| 178 | * |
| 179 | * @return bool |
| 180 | */ |
| 181 | public function is_internal() { |
| 182 | return $this->only( 'internal' ); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Is module skip. |
| 187 | * |
| 188 | * @return bool |
| 189 | */ |
| 190 | public function is_skip() { |
| 191 | return $this->only( 'skip' ); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Is module active. |
| 196 | * |
| 197 | * @return bool |
| 198 | */ |
| 199 | public function is_active() { |
| 200 | if ( $this->is_disabled() ) { |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | $active_modules = get_option( 'rank_math_modules', [] ); |
| 205 | return is_array( $active_modules ) && in_array( $this->get_id(), $active_modules, true ); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Can display module on form. |
| 210 | * |
| 211 | * @return bool |
| 212 | */ |
| 213 | public function can_display() { |
| 214 | return ! $this->is_internal(); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Check if we can load the module. |
| 219 | * |
| 220 | * @return bool |
| 221 | */ |
| 222 | public function can_load_module() { |
| 223 | // If its an internal module should be loaded all the time. |
| 224 | if ( $this->is_internal() ) { |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | if ( ! $this->is_active() || $this->is_skip() ) { |
| 229 | return false; |
| 230 | } |
| 231 | |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Check module only for. |
| 237 | * |
| 238 | * @param string $check Check against. |
| 239 | * |
| 240 | * @return bool |
| 241 | */ |
| 242 | private function only( $check ) { |
| 243 | return isset( $this->args['only'] ) && $check === $this->args['only']; |
| 244 | } |
| 245 | } |
| 246 |