PluginProbe
YayMail – WooCommerce Email Customizer / 3.2.6
YayMail – WooCommerce Email Customizer v3.2.6
4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 3.4.1 All 57 releases
yaymail / includes / License / LicenseHandler.php

LicenseHandler.php in YayMail – WooCommerce Email Customizer 3.2.6, at includes/License/LicenseHandler.php

379 lines 13.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace YayMail\License;
4
5 use YayMail\License\EDD_SL_Plugin_Updater;
6 use YayMail\License\RestAPI;
7 use YayMail\License\CorePlugin;
8
9 defined( 'ABSPATH' ) || exit;
10
11 if ( ! defined( 'YAYCOMMERCE_SELLER_SITE_URL' ) ) {
12 define( 'YAYCOMMERCE_SELLER_SITE_URL', 'https://yaycommerce.com/' );
13 }
14
15 class LicenseHandler {
16 protected static $instance = null;
17
18 public static function get_instance() {
19 if ( null === self::$instance ) {
20 self::$instance = new self();
21 }
22 return self::$instance;
23 }
24
25 protected function __construct() {
26 new RestAPI();
27 if ( is_admin() ) {
28 $this->do_hooks();
29 $this->do_cron_job();
30 $this->do_post_requests();
31 $this->show_plugin_page_notification();
32 $this->license_set_site_transient();
33 }
34 }
35
36 public function do_hooks() {
37 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_license_scripts' ) );
38 add_filter( 'woocommerce_settings_tabs_array', array( $this, 'yaycommerce_licenses_setting_tab' ), 100, 1 );
39 add_action( 'woocommerce_settings_yaycommerce_licenses', array( $this, 'yaycommerce_licenses_settings' ), 100 );
40
41 /** Expired license admin notice */
42 add_action( 'admin_notices', array( $this, 'license_expired_admin_notice' ) );
43
44 $licensing_plugins = $this->get_licensing_plugins();
45 foreach ( $licensing_plugins as $plugin ) {
46 $license = new License( $plugin['slug'] );
47 if ( ! $license->is_active() || $license->is_expired() ) {
48 /** Add plugin action when expired license */
49 add_filter( 'plugin_action_links_' . $plugin['basename'], array( $this, 'edit_action_links' ) );
50 }
51 }
52
53 /** Filter for view changelog */
54 add_filter( 'plugins_api', array( $this, 'change_plugin_data' ), 21, 3 );
55
56 /** Auto update */
57 add_action( 'admin_init', array( $this, 'auto_update' ) );
58 add_filter( 'auto_update_plugin', array( $this, 'add_disabled_auto_update_text' ), 100, 2 );
59 }
60
61 public function do_cron_job() {
62 add_filter( 'cron_schedules', array( $this, 'custom_schedules' ) );
63 add_action( 'check_license_cron', array( $this, 'check_license_cron_run' ) );
64 if ( ! wp_next_scheduled( 'check_license_cron' ) ) {
65 wp_schedule_event( time(), 'daily', 'check_license_cron' );
66 };
67
68 add_action( 'get_version_cron', array( $this, 'get_version_cron_run' ) );
69 if ( ! wp_next_scheduled( 'get_version_cron' ) ) {
70 $schedules = \wp_get_schedules();
71 if ( isset( $schedules['3hours'] ) ) {
72 wp_schedule_event( time(), '3hours', 'get_version_cron' );
73 }
74 };
75 }
76
77 public function custom_schedules( $schedules ) {
78 $schedules['3hours'] = array(
79 'interval' => 60 * 60 * 3,
80 'display' => 'Three Hours',
81 );
82 return $schedules;
83 }
84
85 public function check_license_cron_run() {
86 $licensing_plugins = $this->get_licensing_plugins();
87 foreach ( $licensing_plugins as $plugin ) {
88 $license = new License( $plugin['slug'] );
89 $license->update();
90 }
91 }
92
93 public function get_version_cron_run() {
94 $licensing_plugins = $this->get_licensing_plugins();
95 foreach ( $licensing_plugins as $plugin ) {
96 $licensing_plugin = new LicensingPlugin( $plugin['slug'] );
97 $licensing_plugin->update_version_info();
98 }
99 }
100
101 public function enqueue_license_scripts() {
102 if ( ! isset( $_GET['page'] ) || ! 'wc-settings' === $_GET['page'] || ! isset( $_GET['tab'] ) || ! 'yaycommerce_licenses' === $_GET['tab'] ) {
103 return;
104 }
105 wp_enqueue_style( CorePlugin::get( 'slug' ) . '-license-style', CorePlugin::get( 'url' ) . 'assets/admin/css/license.css', array(), CorePlugin::get( 'version' ) );
106 wp_enqueue_script( CorePlugin::get( 'slug' ) . '-license-script', CorePlugin::get( 'url' ) . 'assets/admin/js/license.js', array(), CorePlugin::get( 'version' ), true );
107 wp_localize_script(
108 CorePlugin::get( 'slug' ) . '-license-script',
109 CorePlugin::get( 'slug' ) . 'LicenseData',
110 array(
111 'apiSettings' => array(
112 'restNonce' => wp_create_nonce( 'wp_rest' ),
113 'restUrl' => esc_url_raw( rest_url( CorePlugin::get( 'slug' ) . '-license/v1' ) ),
114 'adminUrl' => admin_url(),
115 ),
116 )
117 );
118 }
119
120 public function yaycommerce_licenses_setting_tab( $settings_array ) {
121 $licensing_plugins = $this->get_licensing_plugins();
122 if ( count( $licensing_plugins ) > 0 ) {
123 $settings_array = array_merge( $settings_array, array( 'yaycommerce_licenses' => 'YayCommerce Licenses' ) );
124 }
125 return $settings_array;
126 }
127
128 public function yaycommerce_licenses_settings() {
129 $GLOBALS['hide_save_button'] = true;
130 add_action(
131 'woocommerce_after_settings_yaycommerce_licenses',
132 function() {
133 $GLOBALS['hide_save_button'] = '';
134 }
135 );
136 require CorePlugin::get( 'path' ) . 'views/license/setting-page.php';
137 }
138
139 public static function get_plugin_data( $plugin_info ) {
140 if ( ! function_exists( 'get_plugin_data' ) ) {
141 require_once ABSPATH . 'wp-admin/includes/plugin.php';
142 }
143 $plugin_data = get_plugin_data( $plugin_info['dir_path'] . basename( $plugin_info['basename'] ) );
144 return $plugin_data;
145 }
146
147 public static function get_licensing_plugins() {
148 $plugins = array();
149 return apply_filters( CorePlugin::get( 'slug' ) . '_available_licensing_plugins', $plugins );
150 }
151
152 public function do_remove_license_request() {
153 if ( isset( $_POST['nonce'] ) ) {
154 wp_verify_nonce( $_POST['nonce'] );
155 }
156 $licensing_plugin_slug = isset( $_POST[ CorePlugin::get( 'slug' ) . '_licensing_plugin' ] ) ? sanitize_text_field( $_POST[ CorePlugin::get( 'slug' ) . '_licensing_plugin' ] ) : '';
157 $license = new License( $licensing_plugin_slug );
158 $license->remove_license_key();
159 $license->remove_license_info();
160 }
161
162 public function do_post_requests() {
163 if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) {
164 if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( $_POST['_wpnonce'] ), 'woocommerce-settings' ) ) {
165 if ( isset( $_POST[ CorePlugin::get( 'slug' ) . '_remove_license' ] ) ) {
166 $this->do_remove_license_request();
167 }
168 }
169 }
170 }
171
172 /** NOTIFICATION */
173 public function license_expired_admin_notice() {
174 require CorePlugin::get( 'path' ) . 'views/license/expired-admin-notice.php';
175 }
176
177 public function show_plugin_page_notification() {
178 $licensing_plugins = $this->get_licensing_plugins();
179 foreach ( $licensing_plugins as $plugin ) {
180 add_action( 'after_plugin_row_' . $plugin['basename'], array( $this, 'plugin_notifications' ), 10, 2 );
181 }
182 }
183
184 public function plugin_notifications( $file, $plugin_info ) {
185 $wp_list_table = _get_list_table( 'WP_MS_Themes_List_Table' );
186 $licensing_plugins = $this->get_licensing_plugins();
187 $match_position = array_search( $file, array_column( $licensing_plugins, 'basename' ) );
188 if ( false === $match_position ) {
189 return;
190 }
191 $plugin = $licensing_plugins[ $match_position ];
192 $licensing_plugin = new LicensingPlugin( $plugin['slug'] );
193 $license = $licensing_plugin->get_license();
194 $plugin_version_info = $licensing_plugin->get_version_info();
195 if ( ! $license->is_active() || $license->is_expired() ) {
196 $new_version = isset( $plugin_version_info['new_version'] ) ? $plugin_version_info['new_version'] : '0';
197 ?>
198 <script>
199 var plugin_row_element = document.querySelector('tr[data-plugin="<?php echo esc_js( plugin_basename( $file ) ); ?>"]');
200 plugin_row_element.classList.add('update');
201 </script>
202 <tr class="plugin-update-tr <?php echo esc_attr( is_plugin_active( $file ) ? 'active' : '' ); ?>">
203 <td colspan="<?php echo esc_attr( $wp_list_table->get_column_count() ); ?>" class="plugin-update colspanchange">
204 <?php
205 if ( $plugin_info['Version'] < $new_version ) {
206 require CorePlugin::get( 'path' ) . 'views/license/update-plugin-notification.php';
207 }
208 if ( ! $license->is_active() ) {
209 require CorePlugin::get( 'path' ) . 'views/license/not-activate-license-notification.php';
210 }
211 if ( $license->is_expired() ) {
212 require CorePlugin::get( 'path' ) . 'views/license/expired-license-notification.php';
213 }
214 }
215 }
216
217 /** Plugin action link */
218 public function edit_action_links( $action_links ) {
219 $new_action_links = array(
220 'settings' => '<a href="' . admin_url( 'admin.php?page=wc-settings&tab=yaycommerce_licenses' ) . '" aria-label="' . esc_attr( 'View YayCommerce license settings' ) . '">' . esc_html( 'Enter license key' ) . '</a>',
221 );
222 return array_merge( $new_action_links, $action_links );
223 }
224
225 public function change_plugin_data( $_data, $_action = '', $_args = null ) {
226 if ( ! isset( $_args->slug ) ) {
227 return $_data;
228 }
229 $licensing_plugins = $this->get_licensing_plugins();
230 $match_position = array_search( $_args->slug, array_column( $licensing_plugins, 'basename' ) );
231 if ( false === $match_position ) {
232 return $_data;
233 }
234
235 $plugin = $licensing_plugins[ $match_position ];
236 $licensing_plugin = new LicensingPlugin( $plugin['slug'] );
237 $license = $licensing_plugin->get_license();
238
239 if ( $license->is_active() && ! $license->is_expired() ) {
240 return $_data;
241 }
242
243 $plugin_data = self::get_plugin_data( $plugin );
244
245 $plugin_version_info = $licensing_plugin->get_version_info();
246
247 $_data = (object) $plugin_version_info;
248 $sites = get_site_transient( 'update_plugins' );
249 if ( ! isset( $sites->response[ $plugin['basename'] ] ) ) {
250 $sites->response[ $plugin['basename'] ] = $_data;
251 set_site_transient( 'update_plugins', $sites );
252 }
253
254 $_data->version = $_data->new_version;
255 $_data->author = $plugin_data['AuthorName'];
256 $_data->author_profile = $plugin_data['AuthorURI'];
257 $_data->download_link = '#';
258
259 add_action(
260 'admin_head',
261 function() use ( $plugin ) {
262 ?>
263 <script>
264 jQuery(document).ready(function(){
265 var update_a_tag = document.querySelector('a[data-plugin="<?php echo esc_js( $plugin['basename'] ); ?>"]#plugin_update_from_iframe');
266 update_a_tag.innerHTML = '<?php echo esc_html( 'Active your license to update' ); ?>';
267 update_a_tag.href = '<?php echo esc_url( admin_url() ) . 'admin.php?page=wc-settings&tab=yaycommerce_licenses'; ?>';
268 update_a_tag.id = '<?php echo esc_attr( CorePlugin::get( 'slug' ) ); ?>-activate-license';
269 update_a_tag.target = '_blank';
270 })
271 </script>
272 <?php
273 },
274 100
275 );
276
277 if ( isset( $_data->sections ) && ! is_array( $_data->sections ) ) {
278 $_data->sections = maybe_unserialize( $_data->sections );
279 }
280
281 if ( isset( $_data->banners ) && ! is_array( $_data->banners ) ) {
282 $_data->banners = maybe_unserialize( $_data->banners );
283 }
284
285 if ( isset( $_data->icons ) && ! is_array( $_data->icons ) ) {
286 $_data->icons = maybe_unserialize( $_data->icons );
287 }
288
289 if ( isset( $_data->contributors ) && ! is_array( $_data->contributors ) ) {
290 $_data->contributors = $this->convert_object_to_array( $_data->contributors );
291 }
292
293 if ( ! isset( $_data->plugin ) ) {
294 $_data->plugin = $plugin['basename'];
295 }
296
297 return $_data;
298 }
299
300 public function convert_object_to_array( $data ) {
301 $new_data = array();
302 foreach ( $data as $key => $value ) {
303 $new_data[ $key ] = is_object( $value ) ? $this->convert_object_to_array( $value ) : $value;
304 }
305 return $new_data;
306 }
307
308 /** Auto update */
309 public function auto_update() {
310 $doing_cron = defined( 'DOING_CRON' ) && DOING_CRON;
311 if ( ! current_user_can( 'manage_options' ) && ! $doing_cron ) {
312 return;
313 }
314 $licensing_plugins = $this->get_licensing_plugins();
315 foreach ( $licensing_plugins as $plugin ) {
316 $license = new License( $plugin['slug'] );
317 if ( $license->is_active() && ! $license->is_expired() ) {
318 $_file = $plugin['dir_path'] . basename( $plugin['basename'] );
319
320 $plugin_data = self::get_plugin_data( $plugin );
321 $license_key = $license->get_license_key();
322 $args = array(
323 'version' => $plugin_data['Version'],
324 'license' => $license_key,
325 'author' => $plugin_data['AuthorName'],
326 'item_id' => $plugin['item_id'],
327 );
328 $edd_updater = new EDD_SL_Plugin_Updater(
329 YAYCOMMERCE_SELLER_SITE_URL,
330 $_file,
331 $args
332 );
333 }
334 }
335 }
336
337 public function license_set_site_transient() {
338 global $pagenow;
339 if ( 'plugin-install.php' === $pagenow ) {
340 return;
341 }
342 if ( ! function_exists( 'get_plugin_data' ) ) {
343 require_once ABSPATH . 'wp-admin/includes/plugin.php';
344 }
345 $licensing_plugins = $this->get_licensing_plugins();
346 $site_transient_update_plugins = get_site_transient( 'update_plugins' );
347 $check = false;
348
349 foreach ( $licensing_plugins as $plugin ) {
350 $license = new License( $plugin['slug'] );
351 if ( ! $license->is_active() || $license->is_expired() ) {
352 if ( isset( $site_transient_update_plugins->response[ $plugin['basename'] ] )
353 || isset( $site_transient_update_plugins->no_update[ $plugin['basename'] ] )
354 ) {
355 $check = true;
356 unset( $site_transient_update_plugins->response[ $plugin['basename'] ] );
357 unset( $site_transient_update_plugins->no_update[ $plugin['basename'] ] );
358 }
359 }
360 }
361 if ( false !== $site_transient_update_plugins && $check ) {
362 set_site_transient( 'update_plugins', $site_transient_update_plugins );
363 }
364 }
365
366 public function add_disabled_auto_update_text( $value, $plugin_info ) {
367 $licensing_plugins = $this->get_licensing_plugins();
368 foreach ( $licensing_plugins as $plugin ) {
369 if ( $plugin['basename'] === $plugin_info->plugin ) {
370 $license = new License( $plugin['slug'] );
371 if ( ! $license->is_active() || $license->is_expired() ) {
372 return false;
373 }
374 }
375 }
376 return $value;
377 }
378 }
379