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