PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more / 1.4.2
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more v1.4.2
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 9 years ago Plugin.php 9 years ago index.html 9 years ago
Plugin.php
349 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 PressShack <help@pressshack.com>
13 * @copyright Copyright (C) 2016 Open Source Training, LLC. 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' => "PressShack"
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 if (is_admin() && !self::isEmbedPressActive()) {
108 echo '<p><a href="'. admin_url('plugins.php') .'">'. __('Go back') .'</a></p>';
109
110 wp_die(self::getErrorMessage('ERR_MISSING_DEPENDENCY'));
111 }
112 }
113
114 /**
115 * Callback triggered by WordPress' 'register_deactivation_hook' function.
116 *
117 * @since 1.4.0
118 * @static
119 *
120 * @return void
121 */
122 public static function onDeactivationCallback()
123 {
124 delete_option(EMBEDPRESS_PLG_NAME .':'. static::SLUG);
125 }
126
127 /**
128 * Method that validates the EmbedPress plugin's settings form.
129 *
130 * @since 1.4.0
131 * @static
132 *
133 * @param array $postData The data coming from the form via POST.
134 * @return array
135 */
136 public static function validateForm($postData)
137 {
138 $pluginSlugNonce = EMBEDPRESS_PLG_NAME .':'. static::SLUG .':nonce';
139 if (!check_admin_referer($pluginSlugNonce, $pluginSlugNonce)) {
140 return;
141 }
142
143 $data = array();
144
145 $schema = static::getOptionsSchema();
146 foreach ($schema as $fieldSlug => $field) {
147 if (isset($postData[$fieldSlug])) {
148 $value = $postData[$fieldSlug];
149 } else {
150 $value = isset($field['default']) ? $field['default'] : null;
151 }
152
153 settype($value, isset($field['type']) && in_array(strtolower($field['type']), array('bool', 'boolean', 'int', 'integer', 'float', 'string')) ? $field['type'] : 'string');
154
155 $data[$fieldSlug] = $value;
156 }
157
158 static::onAfterFormValidation($data);
159
160 if (isset($data['license_key'])) {
161 unset($data['license_key']);
162 }
163
164 return $data;
165 }
166
167 /**
168 * Method called right after the settings form being validated but before saving the data into DB.
169 *
170 * @since 1.4.0
171 * @static
172 *
173 * @param array Data after validation.
174 * @return void
175 */
176 public static function onAfterFormValidation(&$data)
177 {
178 // do nothing
179 }
180
181 /**
182 * Method that appends a tab in EmbedPress' Settings page to the plugin.
183 *
184 * @since 1.4.0
185 * @static
186 *
187 * @return void
188 */
189 public static function renderTab($activeTab)
190 {
191 ?>
192
193 <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>
194
195 <?php
196 }
197
198 /**
199 * Method that return the absolute path to the plugin.
200 *
201 * @since 1.4.0
202 * @static
203 *
204 * @return void
205 */
206 public static function registerSettings()
207 {
208 $identifier = EMBEDPRESS_PLG_NAME .':'. static::SLUG;
209
210 register_setting($identifier, $identifier, array(static::NAMESPACE_STRING, 'validateForm'));
211 add_settings_section($identifier, EMBEDPRESS .' > '. static::NAME .' Settings', array(static::NAMESPACE_STRING, 'onAfterRegisterSettings'), $identifier);
212
213 self::registerSettingsFields();
214 }
215
216 /**
217 * Register all plugin fields to the settings page.
218 *
219 * @since 1.4.0
220 * @static
221 *
222 * @return void
223 */
224 public static function registerSettingsFields()
225 {
226 $identifier = EMBEDPRESS_PLG_NAME .':'. static::SLUG;
227
228 $schema = static::getOptionsSchema();
229 foreach ($schema as $fieldSlug => $field) {
230 $field['slug'] = $fieldSlug;
231
232 add_settings_field($fieldSlug, $field['label'], array(__NAMESPACE__ .'\Html\Field', 'render'), $identifier, $identifier, array(
233 'pluginSlug' => static::SLUG,
234 'field' => $field
235 ));
236 }
237 }
238
239 /**
240 * Callback called after the plugin's settings page has been registered and rendered.
241 *
242 * @since 1.4.0
243 * @static
244 *
245 * @return void
246 */
247 public static function onAfterRegisterSettings()
248 {
249 // do nothing
250 }
251
252 /**
253 * Retrieve user defined options.
254 *
255 * @since 1.4.0
256 * @static
257 *
258 * @return array
259 */
260 public static function getOptions()
261 {
262 $options = (array)get_option(EMBEDPRESS_PLG_NAME .':'. static::SLUG);
263 if (empty($options) || (count($options) === 1 && empty($options[0]))) {
264 $options = array();
265 $schema = static::getOptionsSchema();
266 foreach ($schema as $fieldSlug => $field) {
267 $value = isset($field['default']) ? $field['default'] : "";
268
269 settype($value, isset($field['type']) && in_array(strtolower($field['type']), array('bool', 'boolean', 'int', 'integer', 'float', 'string')) ? $field['type'] : 'string');
270
271 if ($fieldSlug === "license_key") {
272 $options['license'] = array(
273 'key' => $value,
274 'status' => "missing"
275 );
276 } else {
277 $options[$fieldSlug] = $value;
278 }
279 }
280 }
281
282 return $options;
283 }
284
285 /**
286 * Handle links displayed below the plugin name in the WordPress Installed Plugins page.
287 *
288 * @since 1.4.0
289 * @static
290 *
291 * @return array
292 */
293 public static function handleActionLinks($links, $file)
294 {
295 $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>';
296
297 array_unshift($links, $settingsLink);
298
299 return $links;
300 }
301
302 /**
303 * Method that validates a license key.
304 *
305 * @since 1.4.0
306 * @access protected
307 * @static
308 *
309 * @return mixed
310 */
311 protected static function validateLicenseKey($licenseKey)
312 {
313 $pluginSlug = EMBEDPRESS_PLG_NAME .':'. static::SLUG;
314
315 $response = wp_remote_post(EMBEDPRESS_LICENSES_API_URL, array(
316 'timeout' => 30,
317 'sslverify' => false,
318 'body' => array(
319 'edd_action' => "activate_license",
320 'license' => $licenseKey,
321 'item_name' => "EmbedPress - ". static::NAME,
322 'url' => home_url()
323 )
324 ));
325
326 if (is_wp_error($response) || 200 !== wp_remote_retrieve_response_code($response)) {
327 $errMessage = $response->get_error_message();
328 if (is_wp_error($response) && !empty($errMessage)) {
329 return $errMessage;
330 } else {
331 return __('An error occurred. Please, try again.');
332 }
333 } else {
334 $licenseData = json_decode(wp_remote_retrieve_body($response));
335 if (empty($licenseData) || !is_object($licenseData)) {
336 $licenseNewStatus = "invalid";
337 } else {
338 if (isset($licenseData->success) && $licenseData->success === true) {
339 $licenseNewStatus = "valid";
340 } else {
341 $licenseNewStatus = isset($licenseData->error) && !empty($licenseData->error) ? $licenseData->error : "invalid";
342 }
343 }
344
345 return $licenseNewStatus;
346 }
347 }
348 }
349