Plugin.php
346 lines
| 1 | <?php |
| 2 | namespace EmbedPress\Plugins; |
| 3 | |
| 4 | use PublishPress\EDD_License\Core\Container as EDDContainer; |
| 5 | use PublishPress\EDD_License\Core\ServicesConfig as EDDServicesConfig; |
| 6 | use PublishPress\EDD_License\Core\Services as EDDServices; |
| 7 | |
| 8 | (defined('ABSPATH') && defined('EMBEDPRESS_IS_LOADED')) or die("No direct script access allowed."); |
| 9 | |
| 10 | /** |
| 11 | * Entity that represents a model to EmbedPress plugins. |
| 12 | * |
| 13 | * @package EmbedPress |
| 14 | * @author EmbedPress <help@embedpress.com> |
| 15 | * @copyright Copyright (C) 2018 EmbedPress. All rights reserved. |
| 16 | * @license GPLv2 or later |
| 17 | * @since 1.4.0 |
| 18 | * @abstract |
| 19 | */ |
| 20 | abstract class Plugin |
| 21 | { |
| 22 | const VERSION = '0.0.0'; |
| 23 | |
| 24 | protected static $eddContainer; |
| 25 | |
| 26 | protected static function getEddContainer() |
| 27 | { |
| 28 | if (empty(static::$eddContainer)) { |
| 29 | $options = static::getOptions(); |
| 30 | |
| 31 | $licenseKey = isset($options['license']['key']) ? (string)$options['license']['key'] : ""; |
| 32 | $licenseStatus = isset($options['license']['status']) ? (string)$options['license']['status'] : "missed"; |
| 33 | |
| 34 | $config = new EDDServicesConfig(); |
| 35 | $config->setApiUrl(EMBEDPRESS_LICENSES_API_URL); |
| 36 | $config->setLicenseKey($licenseKey); |
| 37 | $config->setLicenseStatus($licenseStatus); |
| 38 | $config->setPluginVersion(static::VERSION); |
| 39 | $config->setEddItemId(static::EDD_ID); |
| 40 | $config->setPluginAuthor('EmbedPress'); |
| 41 | $config->setPluginFile(EMBEDPRESS_PLG_NAME .'/'. EMBEDPRESS_PLG_NAME .'.php'); |
| 42 | |
| 43 | $services = new EDDServices($config); |
| 44 | |
| 45 | $eddContainer = new EDDContainer(); |
| 46 | $eddContainer->register($services); |
| 47 | |
| 48 | static::$eddContainer = $eddContainer; |
| 49 | } |
| 50 | |
| 51 | return static::$eddContainer; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Method that register all EmbedPress events. |
| 56 | * |
| 57 | * @since 1.4.0 |
| 58 | * @static |
| 59 | * |
| 60 | * @return void |
| 61 | */ |
| 62 | public static function registerEvents() |
| 63 | { |
| 64 | // do nothing |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Method that checks if EmbedPress is active or not. |
| 69 | * |
| 70 | * @since 1.4.0 |
| 71 | * @access protected |
| 72 | * @static |
| 73 | * |
| 74 | * @return boolean |
| 75 | */ |
| 76 | protected static function isEmbedPressActive() |
| 77 | { |
| 78 | $isEmbedPressActive = is_plugin_active(EMBEDPRESS_PLG_NAME .'/'. EMBEDPRESS_PLG_NAME .'.php'); |
| 79 | |
| 80 | return $isEmbedPressActive; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Retrieve an error message based on its code. |
| 85 | * |
| 86 | * @since 1.4.0 |
| 87 | * @access protected |
| 88 | * @static |
| 89 | * |
| 90 | * @param string $err The error code. |
| 91 | * @return string |
| 92 | */ |
| 93 | protected static function getErrorMessage($err = '') |
| 94 | { |
| 95 | if ($err === 'ERR_MISSING_DEPENDENCY') { |
| 96 | return __('Please, <strong>install</strong> and <strong>activate <a href="https://wordpress.org/plugins/'. EMBEDPRESS_PLG_NAME .'" target="_blank" rel="noopener noreferrer">'. EMBEDPRESS .'</a></strong> plugin in order to make <em>'. EMBEDPRESS .' - '. static::NAME .'</em> to work.'); |
| 97 | } |
| 98 | |
| 99 | return $err; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Callback triggered by WordPress' 'admin_init' default action. |
| 104 | * |
| 105 | * @since 1.4.0 |
| 106 | * @static |
| 107 | * |
| 108 | * @return void |
| 109 | */ |
| 110 | public static function onLoadAdminCallback() |
| 111 | { |
| 112 | $pluginSignature = EMBEDPRESS_PLG_NAME .'-'. static::SLUG .'/'. EMBEDPRESS_PLG_NAME .'-'. static::SLUG .'.php'; |
| 113 | if (is_admin() && !self::isEmbedPressActive() && is_plugin_active($pluginSignature)) { |
| 114 | deactivate_plugins($pluginSignature); |
| 115 | } else { |
| 116 | static::registerSettings(); |
| 117 | |
| 118 | $eddContainer = static::getEddContainer(); |
| 119 | /* |
| 120 | * Instantiate the update manager. The variable is not used by purpose, only |
| 121 | * to instantiate the manager. |
| 122 | */ |
| 123 | $update = $eddContainer['update_manager']; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Callback triggered by WordPress' 'register_activation_hook' function. |
| 129 | * |
| 130 | * @since 1.4.0 |
| 131 | * @static |
| 132 | * |
| 133 | * @return void |
| 134 | */ |
| 135 | public static function onActivationCallback() |
| 136 | { |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Callback triggered by WordPress' 'register_deactivation_hook' function. |
| 142 | * |
| 143 | * @since 1.4.0 |
| 144 | * @static |
| 145 | * |
| 146 | * @return void |
| 147 | */ |
| 148 | public static function onDeactivationCallback() |
| 149 | { |
| 150 | delete_option(EMBEDPRESS_PLG_NAME .':'. static::SLUG); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Method that validates the EmbedPress plugin's settings form. |
| 155 | * |
| 156 | * @since 1.4.0 |
| 157 | * @static |
| 158 | * |
| 159 | * @param array $postData The data coming from the form via POST. |
| 160 | * @return array |
| 161 | */ |
| 162 | public static function validateForm($postData) |
| 163 | { |
| 164 | $pluginSlugNonce = EMBEDPRESS_PLG_NAME .':'. static::SLUG .':nonce'; |
| 165 | if (!check_admin_referer($pluginSlugNonce, $pluginSlugNonce)) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | $data = array(); |
| 170 | |
| 171 | $schema = static::getOptionsSchema(); |
| 172 | foreach ($schema as $fieldSlug => $field) { |
| 173 | if (isset($postData[$fieldSlug])) { |
| 174 | $value = $postData[$fieldSlug]; |
| 175 | } else { |
| 176 | $value = isset($field['default']) ? $field['default'] : null; |
| 177 | } |
| 178 | |
| 179 | settype($value, isset($field['type']) && in_array(strtolower($field['type']), array('bool', 'boolean', 'int', 'integer', 'float', 'string')) ? $field['type'] : 'string'); |
| 180 | |
| 181 | $data[$fieldSlug] = $value; |
| 182 | } |
| 183 | |
| 184 | static::onAfterFormValidation($data); |
| 185 | |
| 186 | if (isset($data['license_key'])) { |
| 187 | unset($data['license_key']); |
| 188 | } |
| 189 | |
| 190 | return $data; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Method called right after the settings form being validated but before saving the data into DB. |
| 195 | * |
| 196 | * @since 1.4.0 |
| 197 | * @static |
| 198 | * |
| 199 | * @param array Data after validation. |
| 200 | * @return void |
| 201 | */ |
| 202 | public static function onAfterFormValidation(&$data) |
| 203 | { |
| 204 | // do nothing |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Method that appends a tab in EmbedPress' Settings page to the plugin. |
| 209 | * |
| 210 | * @since 1.4.0 |
| 211 | * @static |
| 212 | * |
| 213 | * @return void |
| 214 | */ |
| 215 | public static function renderTab($activeTab) |
| 216 | { |
| 217 | ?> |
| 218 | |
| 219 | <a href="?page=<?php echo EMBEDPRESS_PLG_NAME; ?>&tab=<?php echo static::SLUG; ?>" class="nav-tab<?php echo $activeTab === static::SLUG ? ' nav-tab-active' : ''; ?> "><?php echo static::NAME; ?></a> |
| 220 | |
| 221 | <?php |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Method that return the absolute path to the plugin. |
| 226 | * |
| 227 | * @since 1.4.0 |
| 228 | * @static |
| 229 | * |
| 230 | * @return void |
| 231 | */ |
| 232 | public static function registerSettings() |
| 233 | { |
| 234 | $identifier = EMBEDPRESS_PLG_NAME .':'. static::SLUG; |
| 235 | |
| 236 | register_setting($identifier, $identifier, array(static::NAMESPACE_STRING, 'validateForm')); |
| 237 | add_settings_section($identifier, EMBEDPRESS .' > '. static::NAME .' Settings', array(static::NAMESPACE_STRING, 'onAfterRegisterSettings'), $identifier); |
| 238 | |
| 239 | self::registerSettingsFields(); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Register all plugin fields to the settings page. |
| 244 | * |
| 245 | * @since 1.4.0 |
| 246 | * @static |
| 247 | * |
| 248 | * @return void |
| 249 | */ |
| 250 | public static function registerSettingsFields() |
| 251 | { |
| 252 | $identifier = EMBEDPRESS_PLG_NAME .':'. static::SLUG; |
| 253 | |
| 254 | $schema = static::getOptionsSchema(); |
| 255 | foreach ($schema as $fieldSlug => $field) { |
| 256 | $field['slug'] = $fieldSlug; |
| 257 | |
| 258 | add_settings_field($fieldSlug, $field['label'], array(__NAMESPACE__ .'\Html\Field', 'render'), $identifier, $identifier, array( |
| 259 | 'pluginSlug' => static::SLUG, |
| 260 | 'field' => $field |
| 261 | )); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Callback called after the plugin's settings page has been registered and rendered. |
| 267 | * |
| 268 | * @since 1.4.0 |
| 269 | * @static |
| 270 | * |
| 271 | * @return void |
| 272 | */ |
| 273 | public static function onAfterRegisterSettings() |
| 274 | { |
| 275 | // do nothing |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Retrieve user defined options. |
| 280 | * |
| 281 | * @since 1.4.0 |
| 282 | * @static |
| 283 | * |
| 284 | * @return array |
| 285 | */ |
| 286 | public static function getOptions() |
| 287 | { |
| 288 | $options = (array)get_option(EMBEDPRESS_PLG_NAME .':'. static::SLUG); |
| 289 | if (empty($options) || (count($options) === 1 && empty($options[0]))) { |
| 290 | $options = array(); |
| 291 | $schema = static::getOptionsSchema(); |
| 292 | foreach ($schema as $fieldSlug => $field) { |
| 293 | $value = isset($field['default']) ? $field['default'] : ""; |
| 294 | |
| 295 | settype($value, isset($field['type']) && in_array(strtolower($field['type']), array('bool', 'boolean', 'int', 'integer', 'float', 'string')) ? $field['type'] : 'string'); |
| 296 | |
| 297 | if ($fieldSlug === "license_key") { |
| 298 | $options['license'] = array( |
| 299 | 'key' => $value, |
| 300 | 'status' => "missing" |
| 301 | ); |
| 302 | } else { |
| 303 | $options[$fieldSlug] = $value; |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | return $options; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Handle links displayed below the plugin name in the WordPress Installed Plugins page. |
| 313 | * |
| 314 | * @since 1.4.0 |
| 315 | * @static |
| 316 | * |
| 317 | * @return array |
| 318 | */ |
| 319 | public static function handleActionLinks($links, $file) |
| 320 | { |
| 321 | $settingsLink = '<a href="'. admin_url('admin.php?page='. EMBEDPRESS_PLG_NAME .'&tab='. static::SLUG) .'" aria-label="'. __('Open settings page', 'embedpress-'. static::SLUG) .'">'. __('Settings', 'embedpress-'. static::SLUG) .'</a>'; |
| 322 | |
| 323 | array_unshift($links, $settingsLink); |
| 324 | |
| 325 | return $links; |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Method that validates a license key. |
| 330 | * |
| 331 | * @since 1.4.0 |
| 332 | * @access protected |
| 333 | * @static |
| 334 | * |
| 335 | * @return mixed |
| 336 | */ |
| 337 | protected static function validateLicenseKey($licenseKey) |
| 338 | { |
| 339 | $licenseManager = static::$eddContainer['license_manager']; |
| 340 | |
| 341 | $licenseNewStatus = $licenseManager->validate_license_key($licenseKey, static::EDD_ID); |
| 342 | |
| 343 | return $licenseNewStatus; |
| 344 | } |
| 345 | } |
| 346 |