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