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