PluginProbe
YayMail – WooCommerce Email Customizer / 3.4
YayMail – WooCommerce Email Customizer v3.4
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 3.4.2 3.4.3 All 55 releases
yaymail / includes / License / LicenseHandler.php

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

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