PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more / 2.0.3
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more v2.0.3
4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.14 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.10 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.4.0 4.4.1 4.4.10 4.4.11 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5.0 4.5.1
embedpress / EmbedPress / Plugins / Plugin.php
embedpress / EmbedPress / Plugins Last commit date
Html 8 years ago Plugin.php 8 years ago index.html 9 years ago
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