| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\License; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
if ( ! defined( 'YAYCOMMERCE_SELLER_SITE_URL' ) ) { |
| 8 |
define( 'YAYCOMMERCE_SELLER_SITE_URL', 'https://yaycommerce.com/' ); |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* LicenseHandler |
| 13 |
* |
| 14 |
* @package LicenseHandler |
| 15 |
*/ |
| 16 |
class LicenseHandler { |
| 17 |
protected static $instance = null; |
| 18 |
|
| 19 |
public static function get_instance() { |
| 20 |
if ( null === self::$instance ) { |
| 21 |
self::$instance = new self(); |
| 22 |
} |
| 23 |
return self::$instance; |
| 24 |
} |
| 25 |
|
| 26 |
protected function __construct() { |
| 27 |
new RestAPI(); |
| 28 |
if ( is_admin() ) { |
| 29 |
$this->do_hooks(); |
| 30 |
$this->do_cron_job(); |
| 31 |
$this->do_post_requests(); |
| 32 |
$this->show_plugin_page_notification(); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
public function do_hooks() { |
| 37 |
add_filter( 'plugins_list', [ $this, 'support_auto_update' ], 100 ); |
| 38 |
|
| 39 |
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_license_scripts' ] ); |
| 40 |
add_action( 'yaycommerce_licenses_page', [ $this, 'yaycommerce_licenses_settings' ], 100 ); |
| 41 |
add_filter( 'yaycommerce_licensing_plugins', [ $this, 'register_licensing_plugins' ], 100 ); |
| 42 |
|
| 43 |
/** Expired license admin notice */ |
| 44 |
add_action( 'admin_notices', [ $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'], [ $this, 'edit_action_links' ] ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/** Auto update */ |
| 56 |
add_action( 'admin_init', [ $this, 'auto_update' ] ); |
| 57 |
add_filter( 'auto_update_plugin', [ $this, 'add_disabled_auto_update_text' ], 100, 2 ); |
| 58 |
} |
| 59 |
|
| 60 |
public function support_auto_update( $plugins ) { |
| 61 |
foreach ( $plugins['all'] as $ind => $active_plugin ) { |
| 62 |
if ( 'YayCommerce' === $active_plugin['Author'] ) { |
| 63 |
$plugins['all'][ $ind ]['update-supported'] = true; |
| 64 |
} |
| 65 |
} |
| 66 |
return $plugins; |
| 67 |
} |
| 68 |
|
| 69 |
public function do_cron_job() { |
| 70 |
add_filter( 'cron_schedules', [ $this, 'custom_schedules' ] ); |
| 71 |
add_action( 'check_license_cron', [ $this, 'check_license_cron_run' ] ); |
| 72 |
if ( ! wp_next_scheduled( 'check_license_cron' ) ) { |
| 73 |
wp_schedule_event( time(), 'daily', 'check_license_cron' ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
public function custom_schedules( $schedules ) { |
| 78 |
$schedules['3hours'] = [ |
| 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 enqueue_license_scripts() { |
| 94 |
if ( ! isset( $_GET['page'] ) || ! 'yaycommerce-licenses' === $_GET['page'] ) { |
| 95 |
return; |
| 96 |
} |
| 97 |
wp_enqueue_script( CorePlugin::get( 'slug' ) . '-license-script', plugin_dir_url( __FILE__ ) . 'assets/js/license.js', [], CorePlugin::get( 'version' ), true ); |
| 98 |
wp_localize_script( |
| 99 |
CorePlugin::get( 'slug' ) . '-license-script', |
| 100 |
CorePlugin::get( 'slug' ) . 'LicenseData', |
| 101 |
[ |
| 102 |
'apiSettings' => [ |
| 103 |
'restNonce' => wp_create_nonce( 'wp_rest' ), |
| 104 |
'restUrl' => esc_url_raw( rest_url( CorePlugin::get( 'slug' ) . '-license/v1' ) ), |
| 105 |
'adminUrl' => admin_url(), |
| 106 |
], |
| 107 |
] |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
public function yaycommerce_licenses_settings() { |
| 112 |
$licensing_plugins = $this->get_licensing_plugins(); |
| 113 |
foreach ( $licensing_plugins as $_plugin ) { |
| 114 |
$licensing_plugin = new LicensingPlugin( $_plugin['slug'] ); |
| 115 |
$license = $licensing_plugin->get_license(); |
| 116 |
if ( $license->is_active() ) { |
| 117 |
require plugin_dir_path( __FILE__ ) . 'views/information-card.php'; |
| 118 |
} else { |
| 119 |
require plugin_dir_path( __FILE__ ) . 'views/activate-card.php'; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
public static function get_plugin_data( $plugin_info = [] ) { |
| 125 |
$plugin_data = [ |
| 126 |
'Name' => $plugin_info['name'] ?? 'YayMail - WooCommerce Email Customizer', |
| 127 |
'Version' => $plugin_info['version'] ?? YAYMAIL_VERSION, |
| 128 |
'AuthorName' => 'YayCommerce', |
| 129 |
]; |
| 130 |
return $plugin_data; |
| 131 |
} |
| 132 |
|
| 133 |
public function register_licensing_plugins( $plugins = [] ) { |
| 134 |
return array_merge( $plugins, self::get_licensing_plugins() ); |
| 135 |
} |
| 136 |
|
| 137 |
public static function get_licensing_plugins() { |
| 138 |
$plugins = []; |
| 139 |
return apply_filters( CorePlugin::get( 'slug' ) . '_available_licensing_plugins', $plugins ); |
| 140 |
} |
| 141 |
|
| 142 |
public function do_remove_license_request() { |
| 143 |
if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( $_POST['_wpnonce'] ), 'woocommerce-settings' ) ) { |
| 144 |
$licensing_plugin_slug = isset( $_POST[ CorePlugin::get( 'slug' ) . '_licensing_plugin' ] ) ? sanitize_text_field( $_POST[ CorePlugin::get( 'slug' ) . '_licensing_plugin' ] ) : ''; |
| 145 |
$license = new License( $licensing_plugin_slug ); |
| 146 |
$license->remove_license_key(); |
| 147 |
$license->remove_license_info(); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
public function do_post_requests() { |
| 152 |
if ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) { |
| 153 |
if ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( sanitize_text_field( $_POST['_wpnonce'] ), 'woocommerce-settings' ) ) { |
| 154 |
if ( isset( $_POST[ CorePlugin::get( 'slug' ) . '_remove_license' ] ) ) { |
| 155 |
$this->do_remove_license_request(); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
/** NOTIFICATION */ |
| 162 |
public function license_expired_admin_notice() { |
| 163 |
require plugin_dir_path( __FILE__ ) . 'views/expired-admin-notice.php'; |
| 164 |
} |
| 165 |
|
| 166 |
public function show_plugin_page_notification() { |
| 167 |
$licensing_plugins = $this->get_licensing_plugins(); |
| 168 |
foreach ( $licensing_plugins as $plugin ) { |
| 169 |
add_action( 'after_plugin_row_' . $plugin['basename'], [ $this, 'plugin_notifications' ], 10, 2 ); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
public function plugin_notifications( $file ) { |
| 174 |
$licensing_plugins = $this->get_licensing_plugins(); |
| 175 |
$match_position = array_search( $file, array_column( $licensing_plugins, 'basename' ) ); |
| 176 |
if ( false === $match_position ) { |
| 177 |
return; |
| 178 |
} |
| 179 |
$plugin = $licensing_plugins[ $match_position ]; |
| 180 |
$licensing_plugin = new LicensingPlugin( $plugin['slug'] ); |
| 181 |
$license = $licensing_plugin->get_license(); |
| 182 |
if ( ! $license->is_active() || $license->is_expired() ) { |
| 183 |
?> |
| 184 |
<tr class="plugin-update-tr active"><td colspan="4" class="plugin-update colspanchange" style="box-shadow:none;"> |
| 185 |
<script> |
| 186 |
var plugin_row_elements = document.querySelectorAll('tr.plugin-update-tr[data-plugin="<?php echo esc_js( plugin_basename( $file ) ); ?>"] .plugin-update, tr.active[data-plugin="<?php echo esc_js( plugin_basename( $file ) ); ?>"] > th, tr.active[data-plugin="<?php echo esc_js( plugin_basename( $file ) ); ?>"] > td'); |
| 187 |
[].forEach.call(plugin_row_elements, function(element) { |
| 188 |
element.style.boxShadow = "none"; |
| 189 |
}); |
| 190 |
</script> |
| 191 |
<?php |
| 192 |
if ( ! $license->is_active() ) { |
| 193 |
require plugin_dir_path( __FILE__ ) . 'views/not-activate-license-notification.php'; |
| 194 |
} |
| 195 |
if ( $license->is_expired() ) { |
| 196 |
require plugin_dir_path( __FILE__ ) . 'views/expired-license-notification.php'; |
| 197 |
} |
| 198 |
?> |
| 199 |
</td></tr> |
| 200 |
<?php |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
/** Plugin action link */ |
| 205 |
public function edit_action_links( $action_links ) { |
| 206 |
$new_action_links = [ |
| 207 |
'settings' => '<a href="' . admin_url( 'admin.php?page=yaycommerce-licenses' ) . '" aria-label="' . esc_attr( 'View YayCommerce license settings' ) . '">' . esc_html( 'Enter license key' ) . '</a>', |
| 208 |
]; |
| 209 |
return array_merge( $new_action_links, $action_links ); |
| 210 |
} |
| 211 |
|
| 212 |
/** Auto update */ |
| 213 |
public function auto_update() { |
| 214 |
$doing_cron = defined( 'DOING_CRON' ) && DOING_CRON; |
| 215 |
if ( ! current_user_can( 'manage_options' ) && ! $doing_cron ) { |
| 216 |
return; |
| 217 |
} |
| 218 |
$licensing_plugins = $this->get_licensing_plugins(); |
| 219 |
foreach ( $licensing_plugins as $plugin ) { |
| 220 |
$license = new License( $plugin['slug'] ); |
| 221 |
$license_key = ''; |
| 222 |
$_file = $plugin['dir_path'] . basename( $plugin['basename'] ); |
| 223 |
$plugin_data = self::get_plugin_data( $plugin ); |
| 224 |
if ( $license->is_active() && ! $license->is_expired() ) { |
| 225 |
$license_key = $license->get_license_key(); |
| 226 |
} |
| 227 |
$args = [ |
| 228 |
'version' => $plugin_data['Version'], |
| 229 |
'license' => $license_key, |
| 230 |
'author' => $plugin_data['AuthorName'], |
| 231 |
'item_id' => $plugin['item_id'], |
| 232 |
]; |
| 233 |
new EDD_SL_Plugin_Updater( |
| 234 |
YAYCOMMERCE_SELLER_SITE_URL, |
| 235 |
$_file, |
| 236 |
$args |
| 237 |
); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
public static function remove_site_plugin_check() { |
| 242 |
global $pagenow; |
| 243 |
if ( 'plugin-install.php' === $pagenow ) { |
| 244 |
return; |
| 245 |
} |
| 246 |
if ( ! function_exists( 'get_plugin_data' ) ) { |
| 247 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 248 |
} |
| 249 |
$licensing_plugins = self::get_licensing_plugins(); |
| 250 |
$site_transient_update_plugins = get_site_transient( 'update_plugins' ); |
| 251 |
$check = false; |
| 252 |
foreach ( $licensing_plugins as $plugin ) { |
| 253 |
if ( isset( $site_transient_update_plugins->checked[ $plugin['basename'] ] ) ) { |
| 254 |
unset( $site_transient_update_plugins->checked[ $plugin['basename'] ] ); |
| 255 |
$check = true; |
| 256 |
} |
| 257 |
} |
| 258 |
if ( false !== $site_transient_update_plugins && $check ) { |
| 259 |
set_site_transient( 'update_plugins', $site_transient_update_plugins ); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
public function add_disabled_auto_update_text( $value, $plugin_info ) { |
| 264 |
if ( ! isset( $plugin_info->plugin ) ) { |
| 265 |
return $value; |
| 266 |
} |
| 267 |
$licensing_plugins = $this->get_licensing_plugins(); |
| 268 |
foreach ( $licensing_plugins as $plugin ) { |
| 269 |
if ( $plugin['basename'] === $plugin_info->plugin ) { |
| 270 |
$license = new License( $plugin['slug'] ); |
| 271 |
if ( ! $license->is_active() || $license->is_expired() ) { |
| 272 |
return false; |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
return $value; |
| 277 |
} |
| 278 |
|
| 279 |
public static function is_any_core_license_inactive() { |
| 280 |
$licensing_plugins = self::get_licensing_plugins(); |
| 281 |
$result = false; |
| 282 |
foreach ( $licensing_plugins as $plugin_info ) { |
| 283 |
if ( strpos( strtolower( $plugin_info['name'] ), 'addon' ) !== false ) { |
| 284 |
continue; |
| 285 |
} |
| 286 |
$license = new License( $plugin_info['slug'] ); |
| 287 |
if ( ! $license->is_active() ) { |
| 288 |
$result = true; |
| 289 |
break; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
return $result; |
| 294 |
} |
| 295 |
} |
| 296 |
|