@@ -6,117 +6,117 @@ |
||
| 6 | 6 | |
| 7 | 7 | class LicenseManager |
| 8 | 8 | { |
| 9 | - private LicenseAPI $license_api; |
|
| 10 | - |
|
| 11 | - private LicenseKeyData $license_key_data; |
|
| 12 | - |
|
| 13 | - private PluginLicenseCollection $plugin_license_collection; |
|
| 14 | - |
|
| 15 | - |
|
| 16 | - public function __construct( |
|
| 17 | - LicenseAPI $license_api, |
|
| 18 | - LicenseKeyData $license_key_data, |
|
| 19 | - PluginLicenseCollection $plugin_license_collection |
|
| 20 | - ) { |
|
| 21 | - $this->license_api = $license_api; |
|
| 22 | - $this->license_key_data = $license_key_data; |
|
| 23 | - $this->plugin_license_collection = $plugin_license_collection; |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - |
|
| 27 | - public function activateLicense( |
|
| 28 | - string $license_key, |
|
| 29 | - string $item_id, |
|
| 30 | - string $item_name, |
|
| 31 | - string $plugin_slug, |
|
| 32 | - string $plugin_version, |
|
| 33 | - string $min_core_version = '' |
|
| 34 | - ): stdCLass { |
|
| 35 | - $license_data = $this->license_api->postRequest( |
|
| 36 | - LicenseAPI::ACTION_ACTIVATE, |
|
| 37 | - $license_key, |
|
| 38 | - $item_id, |
|
| 39 | - $item_name, |
|
| 40 | - $plugin_version, |
|
| 41 | - $min_core_version |
|
| 42 | - ); |
|
| 43 | - $license_data->license_key = $license_data->license === 'valid' ? $license_key : ''; |
|
| 44 | - $this->license_key_data->updateLicense($license_data, $plugin_slug); |
|
| 45 | - return $license_data; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - |
|
| 49 | - public function deactivateLicense( |
|
| 50 | - string $license_key, |
|
| 51 | - string $item_id, |
|
| 52 | - string $item_name, |
|
| 53 | - string $plugin_slug, |
|
| 54 | - string $plugin_version, |
|
| 55 | - string $min_core_version = '' |
|
| 56 | - ): stdCLass { |
|
| 57 | - $license_data = $this->license_api->postRequest( |
|
| 58 | - LicenseAPI::ACTION_DEACTIVATE, |
|
| 59 | - $license_key, |
|
| 60 | - $item_id, |
|
| 61 | - $item_name, |
|
| 62 | - $plugin_version, |
|
| 63 | - $min_core_version |
|
| 64 | - ); |
|
| 65 | - $this->license_key_data->removeLicense($plugin_slug); |
|
| 66 | - return $license_data; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - |
|
| 70 | - public function resetLicenseKey(string $plugin_slug): stdCLass |
|
| 71 | - { |
|
| 72 | - $license_data = $this->getLicenseData($plugin_slug); |
|
| 73 | - $license_data->license_key = ''; |
|
| 74 | - $this->license_key_data->updateLicense($license_data, $plugin_slug, true); |
|
| 75 | - return $license_data; |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - |
|
| 79 | - public function checkLicense( |
|
| 80 | - string $license_key, |
|
| 81 | - string $item_id, |
|
| 82 | - string $item_name, |
|
| 83 | - string $plugin_slug, |
|
| 84 | - string $plugin_version, |
|
| 85 | - string $min_core_version = '' |
|
| 86 | - ): stdCLass { |
|
| 87 | - $existing_license_data = $this->getLicenseData($plugin_slug); |
|
| 88 | - $license_data = $this->license_api->postRequest( |
|
| 89 | - LicenseAPI::ACTION_CHECK, |
|
| 90 | - $license_key, |
|
| 91 | - $item_id, |
|
| 92 | - $item_name, |
|
| 93 | - $plugin_version, |
|
| 94 | - $min_core_version |
|
| 95 | - ); |
|
| 96 | - $license_data = (array) $license_data + (array) $existing_license_data; |
|
| 97 | - return (object) $license_data; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - |
|
| 101 | - public function getLicenseData(string $plugin_slug): stdClass |
|
| 102 | - { |
|
| 103 | - return $this->license_key_data->getLicense($plugin_slug); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - |
|
| 107 | - public function getVersionInfo(): stdClass |
|
| 108 | - { |
|
| 109 | - $products = []; |
|
| 110 | - foreach ($this->plugin_license_collection as $plugin_license) { |
|
| 111 | - if (! $plugin_license instanceof PluginLicense) { |
|
| 112 | - continue; |
|
| 113 | - } |
|
| 114 | - $products[ $plugin_license->pluginSlug() ] = [ |
|
| 115 | - 'item_id' => $plugin_license->itemId(), |
|
| 116 | - 'license' => $plugin_license->licenseKey(), |
|
| 117 | - 'url' => LicenseAPI::url(), |
|
| 118 | - ]; |
|
| 119 | - } |
|
| 120 | - return $this->license_api->getProductVersions($products); |
|
| 121 | - } |
|
| 9 | + private LicenseAPI $license_api; |
|
| 10 | + |
|
| 11 | + private LicenseKeyData $license_key_data; |
|
| 12 | + |
|
| 13 | + private PluginLicenseCollection $plugin_license_collection; |
|
| 14 | + |
|
| 15 | + |
|
| 16 | + public function __construct( |
|
| 17 | + LicenseAPI $license_api, |
|
| 18 | + LicenseKeyData $license_key_data, |
|
| 19 | + PluginLicenseCollection $plugin_license_collection |
|
| 20 | + ) { |
|
| 21 | + $this->license_api = $license_api; |
|
| 22 | + $this->license_key_data = $license_key_data; |
|
| 23 | + $this->plugin_license_collection = $plugin_license_collection; |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + |
|
| 27 | + public function activateLicense( |
|
| 28 | + string $license_key, |
|
| 29 | + string $item_id, |
|
| 30 | + string $item_name, |
|
| 31 | + string $plugin_slug, |
|
| 32 | + string $plugin_version, |
|
| 33 | + string $min_core_version = '' |
|
| 34 | + ): stdCLass { |
|
| 35 | + $license_data = $this->license_api->postRequest( |
|
| 36 | + LicenseAPI::ACTION_ACTIVATE, |
|
| 37 | + $license_key, |
|
| 38 | + $item_id, |
|
| 39 | + $item_name, |
|
| 40 | + $plugin_version, |
|
| 41 | + $min_core_version |
|
| 42 | + ); |
|
| 43 | + $license_data->license_key = $license_data->license === 'valid' ? $license_key : ''; |
|
| 44 | + $this->license_key_data->updateLicense($license_data, $plugin_slug); |
|
| 45 | + return $license_data; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + |
|
| 49 | + public function deactivateLicense( |
|
| 50 | + string $license_key, |
|
| 51 | + string $item_id, |
|
| 52 | + string $item_name, |
|
| 53 | + string $plugin_slug, |
|
| 54 | + string $plugin_version, |
|
| 55 | + string $min_core_version = '' |
|
| 56 | + ): stdCLass { |
|
| 57 | + $license_data = $this->license_api->postRequest( |
|
| 58 | + LicenseAPI::ACTION_DEACTIVATE, |
|
| 59 | + $license_key, |
|
| 60 | + $item_id, |
|
| 61 | + $item_name, |
|
| 62 | + $plugin_version, |
|
| 63 | + $min_core_version |
|
| 64 | + ); |
|
| 65 | + $this->license_key_data->removeLicense($plugin_slug); |
|
| 66 | + return $license_data; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + |
|
| 70 | + public function resetLicenseKey(string $plugin_slug): stdCLass |
|
| 71 | + { |
|
| 72 | + $license_data = $this->getLicenseData($plugin_slug); |
|
| 73 | + $license_data->license_key = ''; |
|
| 74 | + $this->license_key_data->updateLicense($license_data, $plugin_slug, true); |
|
| 75 | + return $license_data; |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + |
|
| 79 | + public function checkLicense( |
|
| 80 | + string $license_key, |
|
| 81 | + string $item_id, |
|
| 82 | + string $item_name, |
|
| 83 | + string $plugin_slug, |
|
| 84 | + string $plugin_version, |
|
| 85 | + string $min_core_version = '' |
|
| 86 | + ): stdCLass { |
|
| 87 | + $existing_license_data = $this->getLicenseData($plugin_slug); |
|
| 88 | + $license_data = $this->license_api->postRequest( |
|
| 89 | + LicenseAPI::ACTION_CHECK, |
|
| 90 | + $license_key, |
|
| 91 | + $item_id, |
|
| 92 | + $item_name, |
|
| 93 | + $plugin_version, |
|
| 94 | + $min_core_version |
|
| 95 | + ); |
|
| 96 | + $license_data = (array) $license_data + (array) $existing_license_data; |
|
| 97 | + return (object) $license_data; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + |
|
| 101 | + public function getLicenseData(string $plugin_slug): stdClass |
|
| 102 | + { |
|
| 103 | + return $this->license_key_data->getLicense($plugin_slug); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + |
|
| 107 | + public function getVersionInfo(): stdClass |
|
| 108 | + { |
|
| 109 | + $products = []; |
|
| 110 | + foreach ($this->plugin_license_collection as $plugin_license) { |
|
| 111 | + if (! $plugin_license instanceof PluginLicense) { |
|
| 112 | + continue; |
|
| 113 | + } |
|
| 114 | + $products[ $plugin_license->pluginSlug() ] = [ |
|
| 115 | + 'item_id' => $plugin_license->itemId(), |
|
| 116 | + 'license' => $plugin_license->licenseKey(), |
|
| 117 | + 'url' => LicenseAPI::url(), |
|
| 118 | + ]; |
|
| 119 | + } |
|
| 120 | + return $this->license_api->getProductVersions($products); |
|
| 121 | + } |
|
| 122 | 122 | } |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | string $plugin_version, |
| 33 | 33 | string $min_core_version = '' |
| 34 | 34 | ): stdCLass { |
| 35 | - $license_data = $this->license_api->postRequest( |
|
| 35 | + $license_data = $this->license_api->postRequest( |
|
| 36 | 36 | LicenseAPI::ACTION_ACTIVATE, |
| 37 | 37 | $license_key, |
| 38 | 38 | $item_id, |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | $plugin_version, |
| 94 | 94 | $min_core_version |
| 95 | 95 | ); |
| 96 | - $license_data = (array) $license_data + (array) $existing_license_data; |
|
| 96 | + $license_data = (array) $license_data + (array) $existing_license_data; |
|
| 97 | 97 | return (object) $license_data; |
| 98 | 98 | } |
| 99 | 99 | |
@@ -108,10 +108,10 @@ discard block |
||
| 108 | 108 | { |
| 109 | 109 | $products = []; |
| 110 | 110 | foreach ($this->plugin_license_collection as $plugin_license) { |
| 111 | - if (! $plugin_license instanceof PluginLicense) { |
|
| 111 | + if ( ! $plugin_license instanceof PluginLicense) { |
|
| 112 | 112 | continue; |
| 113 | 113 | } |
| 114 | - $products[ $plugin_license->pluginSlug() ] = [ |
|
| 114 | + $products[$plugin_license->pluginSlug()] = [ |
|
| 115 | 115 | 'item_id' => $plugin_license->itemId(), |
| 116 | 116 | 'license' => $plugin_license->licenseKey(), |
| 117 | 117 | 'url' => LicenseAPI::url(), |
@@ -16,20 +16,20 @@ discard block |
||
| 16 | 16 | */ |
| 17 | 17 | |
| 18 | 18 | // this is the URL our updater / license checker pings. This should be the URL of the site with EDD installed |
| 19 | -define( 'EDD_SAMPLE_STORE_URL', 'http://easydigitaldownloads.com' ); // you should use your own CONSTANT name, and be sure to replace it throughout this file |
|
| 19 | +define('EDD_SAMPLE_STORE_URL', 'http://easydigitaldownloads.com'); // you should use your own CONSTANT name, and be sure to replace it throughout this file |
|
| 20 | 20 | |
| 21 | 21 | // the download ID for the product in Easy Digital Downloads |
| 22 | -define( 'EDD_SAMPLE_ITEM_ID', 123 ); // you should use your own CONSTANT name, and be sure to replace it throughout this file |
|
| 22 | +define('EDD_SAMPLE_ITEM_ID', 123); // you should use your own CONSTANT name, and be sure to replace it throughout this file |
|
| 23 | 23 | |
| 24 | 24 | // the name of the product in Easy Digital Downloads |
| 25 | -define( 'EDD_SAMPLE_ITEM_NAME', 'Sample Plugin' ); // you should use your own CONSTANT name, and be sure to replace it throughout this file |
|
| 25 | +define('EDD_SAMPLE_ITEM_NAME', 'Sample Plugin'); // you should use your own CONSTANT name, and be sure to replace it throughout this file |
|
| 26 | 26 | |
| 27 | 27 | // the name of the settings page for the license input to be displayed |
| 28 | -define( 'EDD_SAMPLE_PLUGIN_LICENSE_PAGE', 'pluginname-license' ); |
|
| 28 | +define('EDD_SAMPLE_PLUGIN_LICENSE_PAGE', 'pluginname-license'); |
|
| 29 | 29 | |
| 30 | -if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) { |
|
| 30 | +if ( ! class_exists('EDD_SL_Plugin_Updater')) { |
|
| 31 | 31 | // load our custom updater |
| 32 | - include dirname( __FILE__ ) . '/EDD_SL_Plugin_Updater.php'; |
|
| 32 | + include dirname(__FILE__).'/EDD_SL_Plugin_Updater.php'; |
|
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | /** |
@@ -39,29 +39,29 @@ discard block |
||
| 39 | 39 | function edd_sl_sample_plugin_updater() { |
| 40 | 40 | |
| 41 | 41 | // To support auto-updates, this needs to run during the wp_version_check cron job for privileged users. |
| 42 | - $doing_cron = defined( 'DOING_CRON' ) && DOING_CRON; |
|
| 43 | - if ( ! current_user_can( 'manage_options' ) && ! $doing_cron ) { |
|
| 42 | + $doing_cron = defined('DOING_CRON') && DOING_CRON; |
|
| 43 | + if ( ! current_user_can('manage_options') && ! $doing_cron) { |
|
| 44 | 44 | return; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | // retrieve our license key from the DB |
| 48 | - $license_key = trim( get_option( 'edd_sample_license_key' ) ); |
|
| 48 | + $license_key = trim(get_option('edd_sample_license_key')); |
|
| 49 | 49 | |
| 50 | 50 | // setup the updater |
| 51 | 51 | $edd_updater = new EDD_SL_Plugin_Updater( |
| 52 | 52 | EDD_SAMPLE_STORE_URL, |
| 53 | 53 | __FILE__, |
| 54 | 54 | array( |
| 55 | - 'version' => '1.0', // current version number |
|
| 56 | - 'license' => $license_key, // license key (used get_option above to retrieve from DB) |
|
| 57 | - 'item_id' => EDD_SAMPLE_ITEM_ID, // ID of the product |
|
| 55 | + 'version' => '1.0', // current version number |
|
| 56 | + 'license' => $license_key, // license key (used get_option above to retrieve from DB) |
|
| 57 | + 'item_id' => EDD_SAMPLE_ITEM_ID, // ID of the product |
|
| 58 | 58 | 'author' => 'Easy Digital Downloads', // author of this plugin |
| 59 | 59 | 'beta' => false, |
| 60 | 60 | ) |
| 61 | 61 | ); |
| 62 | 62 | |
| 63 | 63 | } |
| 64 | -add_action( 'init', 'edd_sl_sample_plugin_updater' ); |
|
| 64 | +add_action('init', 'edd_sl_sample_plugin_updater'); |
|
| 65 | 65 | |
| 66 | 66 | |
| 67 | 67 | /************************************ |
@@ -77,37 +77,37 @@ discard block |
||
| 77 | 77 | */ |
| 78 | 78 | function edd_sample_license_menu() { |
| 79 | 79 | add_plugins_page( |
| 80 | - __( 'Plugin License' ), |
|
| 81 | - __( 'Plugin License' ), |
|
| 80 | + __('Plugin License'), |
|
| 81 | + __('Plugin License'), |
|
| 82 | 82 | 'manage_options', |
| 83 | 83 | EDD_SAMPLE_PLUGIN_LICENSE_PAGE, |
| 84 | 84 | 'edd_sample_license_page' |
| 85 | 85 | ); |
| 86 | 86 | } |
| 87 | -add_action( 'admin_menu', 'edd_sample_license_menu' ); |
|
| 87 | +add_action('admin_menu', 'edd_sample_license_menu'); |
|
| 88 | 88 | |
| 89 | 89 | function edd_sample_license_page() { |
| 90 | 90 | add_settings_section( |
| 91 | 91 | 'edd_sample_license', |
| 92 | - __( 'Plugin License' ), |
|
| 92 | + __('Plugin License'), |
|
| 93 | 93 | 'edd_sample_license_key_settings_section', |
| 94 | 94 | EDD_SAMPLE_PLUGIN_LICENSE_PAGE |
| 95 | 95 | ); |
| 96 | 96 | add_settings_field( |
| 97 | 97 | 'edd_sample_license_key', |
| 98 | - '<label for="edd_sample_license_key">' . __( 'License Key' ) . '</label>', |
|
| 98 | + '<label for="edd_sample_license_key">'.__('License Key').'</label>', |
|
| 99 | 99 | 'edd_sample_license_key_settings_field', |
| 100 | 100 | EDD_SAMPLE_PLUGIN_LICENSE_PAGE, |
| 101 | 101 | 'edd_sample_license', |
| 102 | 102 | ); |
| 103 | 103 | ?> |
| 104 | 104 | <div class="wrap"> |
| 105 | - <h2><?php esc_html_e( 'Plugin License Options' ); ?></h2> |
|
| 105 | + <h2><?php esc_html_e('Plugin License Options'); ?></h2> |
|
| 106 | 106 | <form method="post" action="options.php"> |
| 107 | 107 | |
| 108 | 108 | <?php |
| 109 | - do_settings_sections( EDD_SAMPLE_PLUGIN_LICENSE_PAGE ); |
|
| 110 | - settings_fields( 'edd_sample_license' ); |
|
| 109 | + do_settings_sections(EDD_SAMPLE_PLUGIN_LICENSE_PAGE); |
|
| 110 | + settings_fields('edd_sample_license'); |
|
| 111 | 111 | submit_button(); |
| 112 | 112 | ?> |
| 113 | 113 | |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | * @return void |
| 122 | 122 | */ |
| 123 | 123 | function edd_sample_license_key_settings_section() { |
| 124 | - esc_html_e( 'This is where you enter your license key.' ); |
|
| 124 | + esc_html_e('This is where you enter your license key.'); |
|
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | /** |
@@ -130,29 +130,29 @@ discard block |
||
| 130 | 130 | * @return void |
| 131 | 131 | */ |
| 132 | 132 | function edd_sample_license_key_settings_field() { |
| 133 | - $license = get_option( 'edd_sample_license_key' ); |
|
| 134 | - $status = get_option( 'edd_sample_license_status' ); |
|
| 133 | + $license = get_option('edd_sample_license_key'); |
|
| 134 | + $status = get_option('edd_sample_license_status'); |
|
| 135 | 135 | |
| 136 | 136 | ?> |
| 137 | - <p class="description"><?php esc_html_e( 'Enter your license key.' ); ?></p> |
|
| 137 | + <p class="description"><?php esc_html_e('Enter your license key.'); ?></p> |
|
| 138 | 138 | <?php |
| 139 | 139 | printf( |
| 140 | 140 | '<input type="text" class="regular-text" id="edd_sample_license_key" name="edd_sample_license_key" value="%s" />', |
| 141 | - esc_attr( $license ) |
|
| 141 | + esc_attr($license) |
|
| 142 | 142 | ); |
| 143 | 143 | $button = array( |
| 144 | 144 | 'name' => 'edd_license_deactivate', |
| 145 | - 'label' => __( 'Deactivate License' ), |
|
| 145 | + 'label' => __('Deactivate License'), |
|
| 146 | 146 | ); |
| 147 | - if ( 'valid' !== $status ) { |
|
| 147 | + if ('valid' !== $status) { |
|
| 148 | 148 | $button = array( |
| 149 | 149 | 'name' => 'edd_license_activate', |
| 150 | - 'label' => __( 'Activate License' ), |
|
| 150 | + 'label' => __('Activate License'), |
|
| 151 | 151 | ); |
| 152 | 152 | } |
| 153 | - wp_nonce_field( 'edd_sample_nonce', 'edd_sample_nonce' ); |
|
| 153 | + wp_nonce_field('edd_sample_nonce', 'edd_sample_nonce'); |
|
| 154 | 154 | ?> |
| 155 | - <input type="submit" class="button-secondary" name="<?php echo esc_attr( $button['name'] ); ?>" value="<?php echo esc_attr( $button['label'] ); ?>"/> |
|
| 155 | + <input type="submit" class="button-secondary" name="<?php echo esc_attr($button['name']); ?>" value="<?php echo esc_attr($button['label']); ?>"/> |
|
| 156 | 156 | <?php |
| 157 | 157 | } |
| 158 | 158 | |
@@ -162,9 +162,9 @@ discard block |
||
| 162 | 162 | * @return void |
| 163 | 163 | */ |
| 164 | 164 | function edd_sample_register_option() { |
| 165 | - register_setting( 'edd_sample_license', 'edd_sample_license_key', 'edd_sanitize_license' ); |
|
| 165 | + register_setting('edd_sample_license', 'edd_sample_license_key', 'edd_sanitize_license'); |
|
| 166 | 166 | } |
| 167 | -add_action( 'admin_init', 'edd_sample_register_option' ); |
|
| 167 | +add_action('admin_init', 'edd_sample_register_option'); |
|
| 168 | 168 | |
| 169 | 169 | /** |
| 170 | 170 | * Sanitizes the license key. |
@@ -172,13 +172,13 @@ discard block |
||
| 172 | 172 | * @param string $new The license key. |
| 173 | 173 | * @return string |
| 174 | 174 | */ |
| 175 | -function edd_sanitize_license( $new ) { |
|
| 176 | - $old = get_option( 'edd_sample_license_key' ); |
|
| 177 | - if ( $old && $old !== $new ) { |
|
| 178 | - delete_option( 'edd_sample_license_status' ); // new license has been entered, so must reactivate |
|
| 175 | +function edd_sanitize_license($new) { |
|
| 176 | + $old = get_option('edd_sample_license_key'); |
|
| 177 | + if ($old && $old !== $new) { |
|
| 178 | + delete_option('edd_sample_license_status'); // new license has been entered, so must reactivate |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | - return sanitize_text_field( $new ); |
|
| 181 | + return sanitize_text_field($new); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | /** |
@@ -189,21 +189,21 @@ discard block |
||
| 189 | 189 | function edd_sample_activate_license() { |
| 190 | 190 | |
| 191 | 191 | // listen for our activate button to be clicked |
| 192 | - if ( ! isset( $_POST['edd_license_activate'] ) ) { |
|
| 192 | + if ( ! isset($_POST['edd_license_activate'])) { |
|
| 193 | 193 | return; |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | // run a quick security check |
| 197 | - if ( ! check_admin_referer( 'edd_sample_nonce', 'edd_sample_nonce' ) ) { |
|
| 197 | + if ( ! check_admin_referer('edd_sample_nonce', 'edd_sample_nonce')) { |
|
| 198 | 198 | return; // get out if we didn't click the Activate button |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | // retrieve the license from the database |
| 202 | - $license = trim( get_option( 'edd_sample_license_key' ) ); |
|
| 203 | - if ( ! $license ) { |
|
| 204 | - $license = ! empty( $_POST['edd_sample_license_key'] ) ? sanitize_text_field( $_POST['edd_sample_license_key'] ) : ''; |
|
| 202 | + $license = trim(get_option('edd_sample_license_key')); |
|
| 203 | + if ( ! $license) { |
|
| 204 | + $license = ! empty($_POST['edd_sample_license_key']) ? sanitize_text_field($_POST['edd_sample_license_key']) : ''; |
|
| 205 | 205 | } |
| 206 | - if ( ! $license ) { |
|
| 206 | + if ( ! $license) { |
|
| 207 | 207 | return; |
| 208 | 208 | } |
| 209 | 209 | |
@@ -212,9 +212,9 @@ discard block |
||
| 212 | 212 | 'edd_action' => 'activate_license', |
| 213 | 213 | 'license' => $license, |
| 214 | 214 | 'item_id' => EDD_SAMPLE_ITEM_ID, |
| 215 | - 'item_name' => rawurlencode( EDD_SAMPLE_ITEM_NAME ), // the name of our product in EDD |
|
| 215 | + 'item_name' => rawurlencode(EDD_SAMPLE_ITEM_NAME), // the name of our product in EDD |
|
| 216 | 216 | 'url' => home_url(), |
| 217 | - 'environment' => function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production', |
|
| 217 | + 'environment' => function_exists('wp_get_environment_type') ? wp_get_environment_type() : 'production', |
|
| 218 | 218 | ); |
| 219 | 219 | |
| 220 | 220 | // Call the custom API. |
@@ -228,83 +228,83 @@ discard block |
||
| 228 | 228 | ); |
| 229 | 229 | |
| 230 | 230 | // make sure the response came back okay |
| 231 | - if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
|
| 231 | + if (is_wp_error($response) || 200 !== wp_remote_retrieve_response_code($response)) { |
|
| 232 | 232 | |
| 233 | - if ( is_wp_error( $response ) ) { |
|
| 233 | + if (is_wp_error($response)) { |
|
| 234 | 234 | $message = $response->get_error_message(); |
| 235 | 235 | } else { |
| 236 | - $message = __( 'An error occurred, please try again.' ); |
|
| 236 | + $message = __('An error occurred, please try again.'); |
|
| 237 | 237 | } |
| 238 | 238 | } else { |
| 239 | 239 | |
| 240 | - $license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
|
| 240 | + $license_data = json_decode(wp_remote_retrieve_body($response)); |
|
| 241 | 241 | |
| 242 | - if ( false === $license_data->success ) { |
|
| 242 | + if (false === $license_data->success) { |
|
| 243 | 243 | |
| 244 | - switch ( $license_data->error ) { |
|
| 244 | + switch ($license_data->error) { |
|
| 245 | 245 | |
| 246 | 246 | case 'expired': |
| 247 | 247 | $message = sprintf( |
| 248 | 248 | /* translators: the license key expiration date */ |
| 249 | - __( 'Your license key expired on %s.', 'edd-sample-plugin' ), |
|
| 250 | - date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires, current_time( 'timestamp' ) ) ) |
|
| 249 | + __('Your license key expired on %s.', 'edd-sample-plugin'), |
|
| 250 | + date_i18n(get_option('date_format'), strtotime($license_data->expires, current_time('timestamp'))) |
|
| 251 | 251 | ); |
| 252 | 252 | break; |
| 253 | 253 | |
| 254 | 254 | case 'disabled': |
| 255 | 255 | case 'revoked': |
| 256 | - $message = __( 'Your license key has been disabled.', 'edd-sample-plugin' ); |
|
| 256 | + $message = __('Your license key has been disabled.', 'edd-sample-plugin'); |
|
| 257 | 257 | break; |
| 258 | 258 | |
| 259 | 259 | case 'missing': |
| 260 | - $message = __( 'Invalid license.', 'edd-sample-plugin' ); |
|
| 260 | + $message = __('Invalid license.', 'edd-sample-plugin'); |
|
| 261 | 261 | break; |
| 262 | 262 | |
| 263 | 263 | case 'invalid': |
| 264 | 264 | case 'site_inactive': |
| 265 | - $message = __( 'Your license is not active for this URL.', 'edd-sample-plugin' ); |
|
| 265 | + $message = __('Your license is not active for this URL.', 'edd-sample-plugin'); |
|
| 266 | 266 | break; |
| 267 | 267 | |
| 268 | 268 | case 'item_name_mismatch': |
| 269 | 269 | /* translators: the plugin name */ |
| 270 | - $message = sprintf( __( 'This appears to be an invalid license key for %s.', 'edd-sample-plugin' ), EDD_SAMPLE_ITEM_NAME ); |
|
| 270 | + $message = sprintf(__('This appears to be an invalid license key for %s.', 'edd-sample-plugin'), EDD_SAMPLE_ITEM_NAME); |
|
| 271 | 271 | break; |
| 272 | 272 | |
| 273 | 273 | case 'no_activations_left': |
| 274 | - $message = __( 'Your license key has reached its activation limit.', 'edd-sample-plugin' ); |
|
| 274 | + $message = __('Your license key has reached its activation limit.', 'edd-sample-plugin'); |
|
| 275 | 275 | break; |
| 276 | 276 | |
| 277 | 277 | default: |
| 278 | - $message = __( 'An error occurred, please try again.', 'edd-sample-plugin' ); |
|
| 278 | + $message = __('An error occurred, please try again.', 'edd-sample-plugin'); |
|
| 279 | 279 | break; |
| 280 | 280 | } |
| 281 | 281 | } |
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | // Check if anything passed on a message constituting a failure |
| 285 | - if ( ! empty( $message ) ) { |
|
| 285 | + if ( ! empty($message)) { |
|
| 286 | 286 | $redirect = add_query_arg( |
| 287 | 287 | array( |
| 288 | 288 | 'page' => EDD_SAMPLE_PLUGIN_LICENSE_PAGE, |
| 289 | 289 | 'sl_activation' => 'false', |
| 290 | - 'message' => rawurlencode( $message ), |
|
| 290 | + 'message' => rawurlencode($message), |
|
| 291 | 291 | ), |
| 292 | - admin_url( 'plugins.php' ) |
|
| 292 | + admin_url('plugins.php') |
|
| 293 | 293 | ); |
| 294 | 294 | |
| 295 | - wp_safe_redirect( $redirect ); |
|
| 295 | + wp_safe_redirect($redirect); |
|
| 296 | 296 | exit(); |
| 297 | 297 | } |
| 298 | 298 | |
| 299 | 299 | // $license_data->license will be either "valid" or "invalid" |
| 300 | - if ( 'valid' === $license_data->license ) { |
|
| 301 | - update_option( 'edd_sample_license_key', $license ); |
|
| 300 | + if ('valid' === $license_data->license) { |
|
| 301 | + update_option('edd_sample_license_key', $license); |
|
| 302 | 302 | } |
| 303 | - update_option( 'edd_sample_license_status', $license_data->license ); |
|
| 304 | - wp_safe_redirect( admin_url( 'plugins.php?page=' . EDD_SAMPLE_PLUGIN_LICENSE_PAGE ) ); |
|
| 303 | + update_option('edd_sample_license_status', $license_data->license); |
|
| 304 | + wp_safe_redirect(admin_url('plugins.php?page='.EDD_SAMPLE_PLUGIN_LICENSE_PAGE)); |
|
| 305 | 305 | exit(); |
| 306 | 306 | } |
| 307 | -add_action( 'admin_init', 'edd_sample_activate_license' ); |
|
| 307 | +add_action('admin_init', 'edd_sample_activate_license'); |
|
| 308 | 308 | |
| 309 | 309 | /** |
| 310 | 310 | * Deactivates the license key. |
@@ -315,24 +315,24 @@ discard block |
||
| 315 | 315 | function edd_sample_deactivate_license() { |
| 316 | 316 | |
| 317 | 317 | // listen for our activate button to be clicked |
| 318 | - if ( isset( $_POST['edd_license_deactivate'] ) ) { |
|
| 318 | + if (isset($_POST['edd_license_deactivate'])) { |
|
| 319 | 319 | |
| 320 | 320 | // run a quick security check |
| 321 | - if ( ! check_admin_referer( 'edd_sample_nonce', 'edd_sample_nonce' ) ) { |
|
| 321 | + if ( ! check_admin_referer('edd_sample_nonce', 'edd_sample_nonce')) { |
|
| 322 | 322 | return; // get out if we didn't click the Activate button |
| 323 | 323 | } |
| 324 | 324 | |
| 325 | 325 | // retrieve the license from the database |
| 326 | - $license = trim( get_option( 'edd_sample_license_key' ) ); |
|
| 326 | + $license = trim(get_option('edd_sample_license_key')); |
|
| 327 | 327 | |
| 328 | 328 | // data to send in our API request |
| 329 | 329 | $api_params = array( |
| 330 | 330 | 'edd_action' => 'deactivate_license', |
| 331 | 331 | 'license' => $license, |
| 332 | 332 | 'item_id' => EDD_SAMPLE_ITEM_ID, |
| 333 | - 'item_name' => rawurlencode( EDD_SAMPLE_ITEM_NAME ), // the name of our product in EDD |
|
| 333 | + 'item_name' => rawurlencode(EDD_SAMPLE_ITEM_NAME), // the name of our product in EDD |
|
| 334 | 334 | 'url' => home_url(), |
| 335 | - 'environment' => function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production', |
|
| 335 | + 'environment' => function_exists('wp_get_environment_type') ? wp_get_environment_type() : 'production', |
|
| 336 | 336 | ); |
| 337 | 337 | |
| 338 | 338 | // Call the custom API. |
@@ -346,41 +346,41 @@ discard block |
||
| 346 | 346 | ); |
| 347 | 347 | |
| 348 | 348 | // make sure the response came back okay |
| 349 | - if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) { |
|
| 349 | + if (is_wp_error($response) || 200 !== wp_remote_retrieve_response_code($response)) { |
|
| 350 | 350 | |
| 351 | - if ( is_wp_error( $response ) ) { |
|
| 351 | + if (is_wp_error($response)) { |
|
| 352 | 352 | $message = $response->get_error_message(); |
| 353 | 353 | } else { |
| 354 | - $message = __( 'An error occurred, please try again.' ); |
|
| 354 | + $message = __('An error occurred, please try again.'); |
|
| 355 | 355 | } |
| 356 | 356 | |
| 357 | 357 | $redirect = add_query_arg( |
| 358 | 358 | array( |
| 359 | 359 | 'page' => EDD_SAMPLE_PLUGIN_LICENSE_PAGE, |
| 360 | 360 | 'sl_activation' => 'false', |
| 361 | - 'message' => rawurlencode( $message ), |
|
| 361 | + 'message' => rawurlencode($message), |
|
| 362 | 362 | ), |
| 363 | - admin_url( 'plugins.php' ) |
|
| 363 | + admin_url('plugins.php') |
|
| 364 | 364 | ); |
| 365 | 365 | |
| 366 | - wp_safe_redirect( $redirect ); |
|
| 366 | + wp_safe_redirect($redirect); |
|
| 367 | 367 | exit(); |
| 368 | 368 | } |
| 369 | 369 | |
| 370 | 370 | // decode the license data |
| 371 | - $license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
|
| 371 | + $license_data = json_decode(wp_remote_retrieve_body($response)); |
|
| 372 | 372 | |
| 373 | 373 | // $license_data->license will be either "deactivated" or "failed" |
| 374 | - if ( 'deactivated' === $license_data->license ) { |
|
| 375 | - delete_option( 'edd_sample_license_status' ); |
|
| 374 | + if ('deactivated' === $license_data->license) { |
|
| 375 | + delete_option('edd_sample_license_status'); |
|
| 376 | 376 | } |
| 377 | 377 | |
| 378 | - wp_safe_redirect( admin_url( 'plugins.php?page=' . EDD_SAMPLE_PLUGIN_LICENSE_PAGE ) ); |
|
| 378 | + wp_safe_redirect(admin_url('plugins.php?page='.EDD_SAMPLE_PLUGIN_LICENSE_PAGE)); |
|
| 379 | 379 | exit(); |
| 380 | 380 | |
| 381 | 381 | } |
| 382 | 382 | } |
| 383 | -add_action( 'admin_init', 'edd_sample_deactivate_license' ); |
|
| 383 | +add_action('admin_init', 'edd_sample_deactivate_license'); |
|
| 384 | 384 | |
| 385 | 385 | /** |
| 386 | 386 | * Checks if a license key is still valid. |
@@ -391,15 +391,15 @@ discard block |
||
| 391 | 391 | */ |
| 392 | 392 | function edd_sample_check_license() { |
| 393 | 393 | |
| 394 | - $license = trim( get_option( 'edd_sample_license_key' ) ); |
|
| 394 | + $license = trim(get_option('edd_sample_license_key')); |
|
| 395 | 395 | |
| 396 | 396 | $api_params = array( |
| 397 | 397 | 'edd_action' => 'check_license', |
| 398 | 398 | 'license' => $license, |
| 399 | 399 | 'item_id' => EDD_SAMPLE_ITEM_ID, |
| 400 | - 'item_name' => rawurlencode( EDD_SAMPLE_ITEM_NAME ), |
|
| 400 | + 'item_name' => rawurlencode(EDD_SAMPLE_ITEM_NAME), |
|
| 401 | 401 | 'url' => home_url(), |
| 402 | - 'environment' => function_exists( 'wp_get_environment_type' ) ? wp_get_environment_type() : 'production', |
|
| 402 | + 'environment' => function_exists('wp_get_environment_type') ? wp_get_environment_type() : 'production', |
|
| 403 | 403 | ); |
| 404 | 404 | |
| 405 | 405 | // Call the custom API. |
@@ -412,13 +412,13 @@ discard block |
||
| 412 | 412 | ) |
| 413 | 413 | ); |
| 414 | 414 | |
| 415 | - if ( is_wp_error( $response ) ) { |
|
| 415 | + if (is_wp_error($response)) { |
|
| 416 | 416 | return false; |
| 417 | 417 | } |
| 418 | 418 | |
| 419 | - $license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
|
| 419 | + $license_data = json_decode(wp_remote_retrieve_body($response)); |
|
| 420 | 420 | |
| 421 | - if ( 'valid' === $license_data->license ) { |
|
| 421 | + if ('valid' === $license_data->license) { |
|
| 422 | 422 | echo 'valid'; |
| 423 | 423 | exit; |
| 424 | 424 | // this license is still valid |
@@ -433,15 +433,15 @@ discard block |
||
| 433 | 433 | * This is a means of catching errors from the activation method above and displaying it to the customer |
| 434 | 434 | */ |
| 435 | 435 | function edd_sample_admin_notices() { |
| 436 | - if ( isset( $_GET['sl_activation'] ) && ! empty( $_GET['message'] ) ) { |
|
| 436 | + if (isset($_GET['sl_activation']) && ! empty($_GET['message'])) { |
|
| 437 | 437 | |
| 438 | - switch ( $_GET['sl_activation'] ) { |
|
| 438 | + switch ($_GET['sl_activation']) { |
|
| 439 | 439 | |
| 440 | 440 | case 'false': |
| 441 | - $message = urldecode( $_GET['message'] ); |
|
| 441 | + $message = urldecode($_GET['message']); |
|
| 442 | 442 | ?> |
| 443 | 443 | <div class="error"> |
| 444 | - <p><?php echo wp_kses_post( $message ); ?></p> |
|
| 444 | + <p><?php echo wp_kses_post($message); ?></p> |
|
| 445 | 445 | </div> |
| 446 | 446 | <?php |
| 447 | 447 | break; |
@@ -454,4 +454,4 @@ discard block |
||
| 454 | 454 | } |
| 455 | 455 | } |
| 456 | 456 | } |
| 457 | -add_action( 'admin_notices', 'edd_sample_admin_notices' ); |
|
| 457 | +add_action('admin_notices', 'edd_sample_admin_notices'); |
|
@@ -8,158 +8,158 @@ |
||
| 8 | 8 | |
| 9 | 9 | class LicenseAPI |
| 10 | 10 | { |
| 11 | - public const ACTION_ACTIVATE = 'activate_license'; |
|
| 12 | - |
|
| 13 | - public const ACTION_DEACTIVATE = 'deactivate_license'; |
|
| 14 | - |
|
| 15 | - public const ACTION_CHECK = 'check_license'; |
|
| 16 | - |
|
| 17 | - public const ACTION_GET_VERSION = 'get_version'; |
|
| 18 | - |
|
| 19 | - public const ACTION_RESET = 'reset_license'; |
|
| 20 | - |
|
| 21 | - public const API_USES_ITEM_ID_OR_NAME = 'item_name'; |
|
| 22 | - |
|
| 23 | - public const AUTHOR = 'Event Espresso'; |
|
| 24 | - |
|
| 25 | - public const REQUEST_PARAM_ACTION = 'license_action'; |
|
| 26 | - |
|
| 27 | - public const REQUEST_PARAM_ITEM_ID = 'item_id'; |
|
| 28 | - |
|
| 29 | - public const REQUEST_PARAM_ITEM_NAME = 'item_name'; |
|
| 30 | - |
|
| 31 | - public const REQUEST_PARAM_LICENSE_KEY = 'license_key'; |
|
| 32 | - |
|
| 33 | - public const REQUEST_PARAM_MIN_CORE_VER = 'min_core_ver'; |
|
| 34 | - |
|
| 35 | - public const REQUEST_PARAM_PLUGIN_SLUG = 'plugin_slug'; |
|
| 36 | - |
|
| 37 | - public const REQUEST_PARAM_PLUGIN_VER = 'plugin_ver'; |
|
| 38 | - |
|
| 39 | - public const URL = 'https://my.eventespresso.com/'; |
|
| 40 | - |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * successfull license key API response example: |
|
| 44 | - * { |
|
| 45 | - * "success": true, |
|
| 46 | - * "license": "valid", |
|
| 47 | - * "item_id": false (or Item ID if passed) |
|
| 48 | - * "item_name": "EDD Product Name", |
|
| 49 | - * "license_limit": 0, |
|
| 50 | - * "site_count": 2, |
|
| 51 | - * "expires": "2020-06-30 23:59:59", |
|
| 52 | - * "activations_left": "unlimited", |
|
| 53 | - * "checksum": "<md5 checksum>", |
|
| 54 | - * "payment_id": 12345, |
|
| 55 | - * "customer_name": "John Doe", |
|
| 56 | - * "customer_email": "[email protected]", |
|
| 57 | - * "price_id": "2" |
|
| 58 | - * } |
|
| 59 | - * failed license key API response example: |
|
| 60 | - * { |
|
| 61 | - * "success": false, |
|
| 62 | - * "license": "invalid", |
|
| 63 | - * "item_id": false (or Item ID if passed) |
|
| 64 | - * "item_name": "EDD Product Name", |
|
| 65 | - * "error": "expired", |
|
| 66 | - * "expires": "2020-04-28 23:59:59", |
|
| 67 | - * "license_limit": 0, |
|
| 68 | - * "site_count": 1, |
|
| 69 | - * "activations_left": "unlimited", |
|
| 70 | - * "checksum": "<md5 checksum>", |
|
| 71 | - * "payment_id": 12345, |
|
| 72 | - * "customer_name": "John Doe", |
|
| 73 | - * "customer_email": "[email protected]", |
|
| 74 | - * "price_id": "2" |
|
| 75 | - * } |
|
| 76 | - * |
|
| 77 | - * @param string $action |
|
| 78 | - * @param string $license_key |
|
| 79 | - * @param string $item_id |
|
| 80 | - * @param string $item_name |
|
| 81 | - * @param string $plugin_version |
|
| 82 | - * @param string $min_core_version |
|
| 83 | - * @return stdClass |
|
| 84 | - */ |
|
| 85 | - public function postRequest( |
|
| 86 | - string $action, |
|
| 87 | - string $license_key, |
|
| 88 | - string $item_id, |
|
| 89 | - string $item_name, |
|
| 90 | - string $plugin_version, |
|
| 91 | - string $min_core_version = '' |
|
| 92 | - ): stdClass { |
|
| 93 | - $body = [ |
|
| 94 | - 'edd_action' => $action, |
|
| 95 | - 'event_espresso_core_version' => EVENT_ESPRESSO_VERSION, |
|
| 96 | - 'min_core_version' => $min_core_version, |
|
| 97 | - 'license' => $license_key, |
|
| 98 | - 'version' => $plugin_version, |
|
| 99 | - 'php_version' => phpversion(), |
|
| 100 | - 'url' => home_url(), |
|
| 101 | - 'wp_version' => get_bloginfo('version'), |
|
| 102 | - 'environment' => function_exists('wp_get_environment_type') |
|
| 103 | - ? wp_get_environment_type() |
|
| 104 | - : 'production', |
|
| 105 | - ]; |
|
| 106 | - // change the item ID or item name based on the API_USES_ITEM_ID_OR_NAME constant |
|
| 107 | - if (LicenseAPI::API_USES_ITEM_ID_OR_NAME === 'item_name') { |
|
| 108 | - $body['item_name'] = $item_name; |
|
| 109 | - } else { |
|
| 110 | - $body['item_id'] = $item_id; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $response = $this->handleApiErrors( |
|
| 114 | - wp_remote_post( |
|
| 115 | - LicenseAPI::url(), |
|
| 116 | - [ |
|
| 117 | - 'timeout' => 15, |
|
| 118 | - 'body' => $body, |
|
| 119 | - ] |
|
| 120 | - ) |
|
| 121 | - ); |
|
| 122 | - // decode the license data |
|
| 123 | - return (object) json_decode(wp_remote_retrieve_body($response)); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * @param array|WP_Error $response |
|
| 129 | - * @return array |
|
| 130 | - */ |
|
| 131 | - private function handleApiErrors($response): array |
|
| 132 | - { |
|
| 133 | - // make sure the response came back okay |
|
| 134 | - if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) { |
|
| 135 | - EE_Error::add_error($response->get_error_message(), __FILE__, __FUNCTION__, __LINE__); |
|
| 136 | - return ['success' => false, 'error' => true]; |
|
| 137 | - } |
|
| 138 | - return $response; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - |
|
| 142 | - public function getProductVersions(array $products): stdClass |
|
| 143 | - { |
|
| 144 | - $response = $this->handleApiErrors( |
|
| 145 | - wp_remote_post( |
|
| 146 | - LicenseAPI::url(), |
|
| 147 | - [ |
|
| 148 | - 'timeout' => 15, |
|
| 149 | - 'body' => [ |
|
| 150 | - 'edd_action' => LicenseAPI::ACTION_GET_VERSION, |
|
| 151 | - 'products' => $products, |
|
| 152 | - ], |
|
| 153 | - ] |
|
| 154 | - ) |
|
| 155 | - ); |
|
| 156 | - // decode the license data |
|
| 157 | - return (object) json_decode(wp_remote_retrieve_body($response)); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - |
|
| 161 | - public static function url(): string |
|
| 162 | - { |
|
| 163 | - return defined('EDD_LICENSE_API_URL') ? EDD_LICENSE_API_URL : LicenseAPI::URL; |
|
| 164 | - } |
|
| 11 | + public const ACTION_ACTIVATE = 'activate_license'; |
|
| 12 | + |
|
| 13 | + public const ACTION_DEACTIVATE = 'deactivate_license'; |
|
| 14 | + |
|
| 15 | + public const ACTION_CHECK = 'check_license'; |
|
| 16 | + |
|
| 17 | + public const ACTION_GET_VERSION = 'get_version'; |
|
| 18 | + |
|
| 19 | + public const ACTION_RESET = 'reset_license'; |
|
| 20 | + |
|
| 21 | + public const API_USES_ITEM_ID_OR_NAME = 'item_name'; |
|
| 22 | + |
|
| 23 | + public const AUTHOR = 'Event Espresso'; |
|
| 24 | + |
|
| 25 | + public const REQUEST_PARAM_ACTION = 'license_action'; |
|
| 26 | + |
|
| 27 | + public const REQUEST_PARAM_ITEM_ID = 'item_id'; |
|
| 28 | + |
|
| 29 | + public const REQUEST_PARAM_ITEM_NAME = 'item_name'; |
|
| 30 | + |
|
| 31 | + public const REQUEST_PARAM_LICENSE_KEY = 'license_key'; |
|
| 32 | + |
|
| 33 | + public const REQUEST_PARAM_MIN_CORE_VER = 'min_core_ver'; |
|
| 34 | + |
|
| 35 | + public const REQUEST_PARAM_PLUGIN_SLUG = 'plugin_slug'; |
|
| 36 | + |
|
| 37 | + public const REQUEST_PARAM_PLUGIN_VER = 'plugin_ver'; |
|
| 38 | + |
|
| 39 | + public const URL = 'https://my.eventespresso.com/'; |
|
| 40 | + |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * successfull license key API response example: |
|
| 44 | + * { |
|
| 45 | + * "success": true, |
|
| 46 | + * "license": "valid", |
|
| 47 | + * "item_id": false (or Item ID if passed) |
|
| 48 | + * "item_name": "EDD Product Name", |
|
| 49 | + * "license_limit": 0, |
|
| 50 | + * "site_count": 2, |
|
| 51 | + * "expires": "2020-06-30 23:59:59", |
|
| 52 | + * "activations_left": "unlimited", |
|
| 53 | + * "checksum": "<md5 checksum>", |
|
| 54 | + * "payment_id": 12345, |
|
| 55 | + * "customer_name": "John Doe", |
|
| 56 | + * "customer_email": "[email protected]", |
|
| 57 | + * "price_id": "2" |
|
| 58 | + * } |
|
| 59 | + * failed license key API response example: |
|
| 60 | + * { |
|
| 61 | + * "success": false, |
|
| 62 | + * "license": "invalid", |
|
| 63 | + * "item_id": false (or Item ID if passed) |
|
| 64 | + * "item_name": "EDD Product Name", |
|
| 65 | + * "error": "expired", |
|
| 66 | + * "expires": "2020-04-28 23:59:59", |
|
| 67 | + * "license_limit": 0, |
|
| 68 | + * "site_count": 1, |
|
| 69 | + * "activations_left": "unlimited", |
|
| 70 | + * "checksum": "<md5 checksum>", |
|
| 71 | + * "payment_id": 12345, |
|
| 72 | + * "customer_name": "John Doe", |
|
| 73 | + * "customer_email": "[email protected]", |
|
| 74 | + * "price_id": "2" |
|
| 75 | + * } |
|
| 76 | + * |
|
| 77 | + * @param string $action |
|
| 78 | + * @param string $license_key |
|
| 79 | + * @param string $item_id |
|
| 80 | + * @param string $item_name |
|
| 81 | + * @param string $plugin_version |
|
| 82 | + * @param string $min_core_version |
|
| 83 | + * @return stdClass |
|
| 84 | + */ |
|
| 85 | + public function postRequest( |
|
| 86 | + string $action, |
|
| 87 | + string $license_key, |
|
| 88 | + string $item_id, |
|
| 89 | + string $item_name, |
|
| 90 | + string $plugin_version, |
|
| 91 | + string $min_core_version = '' |
|
| 92 | + ): stdClass { |
|
| 93 | + $body = [ |
|
| 94 | + 'edd_action' => $action, |
|
| 95 | + 'event_espresso_core_version' => EVENT_ESPRESSO_VERSION, |
|
| 96 | + 'min_core_version' => $min_core_version, |
|
| 97 | + 'license' => $license_key, |
|
| 98 | + 'version' => $plugin_version, |
|
| 99 | + 'php_version' => phpversion(), |
|
| 100 | + 'url' => home_url(), |
|
| 101 | + 'wp_version' => get_bloginfo('version'), |
|
| 102 | + 'environment' => function_exists('wp_get_environment_type') |
|
| 103 | + ? wp_get_environment_type() |
|
| 104 | + : 'production', |
|
| 105 | + ]; |
|
| 106 | + // change the item ID or item name based on the API_USES_ITEM_ID_OR_NAME constant |
|
| 107 | + if (LicenseAPI::API_USES_ITEM_ID_OR_NAME === 'item_name') { |
|
| 108 | + $body['item_name'] = $item_name; |
|
| 109 | + } else { |
|
| 110 | + $body['item_id'] = $item_id; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $response = $this->handleApiErrors( |
|
| 114 | + wp_remote_post( |
|
| 115 | + LicenseAPI::url(), |
|
| 116 | + [ |
|
| 117 | + 'timeout' => 15, |
|
| 118 | + 'body' => $body, |
|
| 119 | + ] |
|
| 120 | + ) |
|
| 121 | + ); |
|
| 122 | + // decode the license data |
|
| 123 | + return (object) json_decode(wp_remote_retrieve_body($response)); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * @param array|WP_Error $response |
|
| 129 | + * @return array |
|
| 130 | + */ |
|
| 131 | + private function handleApiErrors($response): array |
|
| 132 | + { |
|
| 133 | + // make sure the response came back okay |
|
| 134 | + if (is_wp_error($response) || wp_remote_retrieve_response_code($response) !== 200) { |
|
| 135 | + EE_Error::add_error($response->get_error_message(), __FILE__, __FUNCTION__, __LINE__); |
|
| 136 | + return ['success' => false, 'error' => true]; |
|
| 137 | + } |
|
| 138 | + return $response; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + |
|
| 142 | + public function getProductVersions(array $products): stdClass |
|
| 143 | + { |
|
| 144 | + $response = $this->handleApiErrors( |
|
| 145 | + wp_remote_post( |
|
| 146 | + LicenseAPI::url(), |
|
| 147 | + [ |
|
| 148 | + 'timeout' => 15, |
|
| 149 | + 'body' => [ |
|
| 150 | + 'edd_action' => LicenseAPI::ACTION_GET_VERSION, |
|
| 151 | + 'products' => $products, |
|
| 152 | + ], |
|
| 153 | + ] |
|
| 154 | + ) |
|
| 155 | + ); |
|
| 156 | + // decode the license data |
|
| 157 | + return (object) json_decode(wp_remote_retrieve_body($response)); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + |
|
| 161 | + public static function url(): string |
|
| 162 | + { |
|
| 163 | + return defined('EDD_LICENSE_API_URL') ? EDD_LICENSE_API_URL : LicenseAPI::URL; |
|
| 164 | + } |
|
| 165 | 165 | } |
@@ -14,246 +14,246 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class PluginLicense |
| 16 | 16 | { |
| 17 | - private ?LicenseKeyData $license_key_data = null; |
|
| 18 | - |
|
| 19 | - private ?PluginUpdater $updater = null; |
|
| 20 | - |
|
| 21 | - private string $license_key = ''; |
|
| 22 | - |
|
| 23 | - private string $mainfile; |
|
| 24 | - |
|
| 25 | - private string $min_core_version; |
|
| 26 | - |
|
| 27 | - private string $plugin_id; |
|
| 28 | - |
|
| 29 | - private string $plugin_name; |
|
| 30 | - |
|
| 31 | - private string $plugin_slug; |
|
| 32 | - |
|
| 33 | - private string $version; |
|
| 34 | - |
|
| 35 | - private bool $beta; |
|
| 36 | - |
|
| 37 | - private bool $wp_override; |
|
| 38 | - |
|
| 39 | - private static array $slug_prefixes = ['ee-', 'eea-', 'eep-', 'espresso-', 'ee_', 'eea_', 'eep_', 'espresso_']; |
|
| 40 | - |
|
| 41 | - |
|
| 42 | - public function __construct( |
|
| 43 | - string $mainfile, |
|
| 44 | - string $plugin_id, |
|
| 45 | - string $plugin_name, |
|
| 46 | - string $plugin_slug, |
|
| 47 | - string $version, |
|
| 48 | - bool $beta = false, |
|
| 49 | - bool $wp_override = false, |
|
| 50 | - string $min_core_version = '' |
|
| 51 | - ) { |
|
| 52 | - $this->mainfile = $mainfile; |
|
| 53 | - $this->plugin_id = $plugin_id; |
|
| 54 | - $this->plugin_name = $plugin_name; |
|
| 55 | - $this->version = $version; |
|
| 56 | - $this->beta = $beta; |
|
| 57 | - $this->wp_override = $wp_override; |
|
| 58 | - $this->plugin_slug = $this->removePluginSlugPrefixes($plugin_slug); |
|
| 59 | - $this->min_core_version = $min_core_version; |
|
| 60 | - |
|
| 61 | - add_action('init', [$this, 'loadPluginUpdater']); |
|
| 62 | - add_action( |
|
| 63 | - 'AHEE__EventEspresso_core_services_licensing_PluginLicenseCollection__loadPluginLicenses', |
|
| 64 | - [$this, 'loadPluginLicense'] |
|
| 65 | - ); |
|
| 66 | - add_filter( |
|
| 67 | - 'FHEE__LicenseKeysAdminForm__generate__form_subsections', |
|
| 68 | - [$this, 'addLicenseKeyFormSection'] |
|
| 69 | - ); |
|
| 70 | - add_filter( |
|
| 71 | - 'edd_sl_plugin_updater_api_params', |
|
| 72 | - [$this, 'addCustomApiParams'], |
|
| 73 | - 10, |
|
| 74 | - 2 |
|
| 75 | - ); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * removes unnecessary prefixes from plugin slugs |
|
| 81 | - * |
|
| 82 | - * @param string $plugin_slug |
|
| 83 | - * @return string |
|
| 84 | - */ |
|
| 85 | - private function removePluginSlugPrefixes(string $plugin_slug): string |
|
| 86 | - { |
|
| 87 | - foreach (self::$slug_prefixes as $slug_prefix) { |
|
| 88 | - if (strpos($plugin_slug, $slug_prefix) === 0) { |
|
| 89 | - $plugin_slug = str_replace($slug_prefix, '', $plugin_slug); |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - return $plugin_slug; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @return PluginUpdater|null |
|
| 98 | - */ |
|
| 99 | - public function updater(): ?PluginUpdater |
|
| 100 | - { |
|
| 101 | - return $this->updater; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * @return string |
|
| 107 | - */ |
|
| 108 | - public function itemID(): string |
|
| 109 | - { |
|
| 110 | - return $this->plugin_id; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @return string |
|
| 116 | - */ |
|
| 117 | - public function itemName(): string |
|
| 118 | - { |
|
| 119 | - return $this->plugin_name; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - |
|
| 123 | - private function getLicenseKeyData(): stdClass |
|
| 124 | - { |
|
| 125 | - if (! $this->license_key_data instanceof LicenseKeyData) { |
|
| 126 | - $this->license_key_data = LoaderFactory::getShared(LicenseKeyData::class); |
|
| 127 | - } |
|
| 128 | - return $this->license_key_data->getLicense($this->pluginSlug()); |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * @return string |
|
| 134 | - */ |
|
| 135 | - public function licenseKey(): string |
|
| 136 | - { |
|
| 137 | - return $this->license_key; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @param string $license_key |
|
| 143 | - */ |
|
| 144 | - public function updateLicenseKey(string $license_key): void |
|
| 145 | - { |
|
| 146 | - $this->license_key = sanitize_text_field($license_key); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * @return string |
|
| 152 | - */ |
|
| 153 | - public function mainfile(): string |
|
| 154 | - { |
|
| 155 | - return $this->mainfile; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @return string |
|
| 161 | - * @since 5.0.20.p |
|
| 162 | - */ |
|
| 163 | - public function minCoreVersion(): string |
|
| 164 | - { |
|
| 165 | - return $this->min_core_version; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * @return string |
|
| 171 | - */ |
|
| 172 | - public function pluginSlug(): string |
|
| 173 | - { |
|
| 174 | - return $this->plugin_slug; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * @return string |
|
| 180 | - */ |
|
| 181 | - public function version(): string |
|
| 182 | - { |
|
| 183 | - return $this->version; |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * @return bool |
|
| 189 | - */ |
|
| 190 | - public function isBeta(): bool |
|
| 191 | - { |
|
| 192 | - return $this->beta; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * @return bool |
|
| 198 | - */ |
|
| 199 | - public function isWpOverride(): bool |
|
| 200 | - { |
|
| 201 | - return $this->wp_override; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - |
|
| 205 | - public function loadPluginUpdater() |
|
| 206 | - { |
|
| 207 | - // To support auto-updates, this needs to run during the wp_version_check cron job for privileged users. |
|
| 208 | - if (! current_user_can('manage_options') && ! (defined('DOING_CRON') && DOING_CRON)) { |
|
| 209 | - return; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - $license_key_data = $this->getLicenseKeyData(); |
|
| 213 | - $license_key = $license_key_data->license_key ?? ''; |
|
| 214 | - if ($license_key) { |
|
| 215 | - $this->updateLicenseKey($license_key); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - $this->updater = new PluginUpdater( |
|
| 219 | - LicenseAPI::url(), |
|
| 220 | - $this->mainfile(), |
|
| 221 | - [ |
|
| 222 | - 'author' => LicenseAPI::AUTHOR, |
|
| 223 | - 'beta' => $this->isBeta(), |
|
| 224 | - 'item_id' => $this->itemID(), |
|
| 225 | - 'item_name' => $this->itemName(), |
|
| 226 | - 'license' => $this->licenseKey(), |
|
| 227 | - 'version' => $this->version(), |
|
| 228 | - 'wp_override' => $this->isWpOverride(), |
|
| 229 | - ] |
|
| 230 | - ); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - |
|
| 234 | - public function loadPluginLicense(PluginLicenseCollection $plugin_license_collection) |
|
| 235 | - { |
|
| 236 | - $plugin_license_collection->add($this, $this->pluginSlug()); |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - |
|
| 240 | - public function addLicenseKeyFormSection(array $license_keys_form_subsections): array |
|
| 241 | - { |
|
| 242 | - if (is_main_site()) { |
|
| 243 | - $license_keys_form_subsections[ $this->pluginSlug() ] = LoaderFactory::getNew( |
|
| 244 | - LicenseKeyFormInput::class, |
|
| 245 | - [$this] |
|
| 246 | - ); |
|
| 247 | - } |
|
| 248 | - return $license_keys_form_subsections; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - |
|
| 252 | - public function addCustomApiParams($api_params, $api_data) |
|
| 253 | - { |
|
| 254 | - if ($api_data['item_name'] === $this->itemName()) { |
|
| 255 | - $api_params['event_espresso_core_version'] = EVENT_ESPRESSO_VERSION; |
|
| 256 | - } |
|
| 257 | - return $api_params; |
|
| 258 | - } |
|
| 17 | + private ?LicenseKeyData $license_key_data = null; |
|
| 18 | + |
|
| 19 | + private ?PluginUpdater $updater = null; |
|
| 20 | + |
|
| 21 | + private string $license_key = ''; |
|
| 22 | + |
|
| 23 | + private string $mainfile; |
|
| 24 | + |
|
| 25 | + private string $min_core_version; |
|
| 26 | + |
|
| 27 | + private string $plugin_id; |
|
| 28 | + |
|
| 29 | + private string $plugin_name; |
|
| 30 | + |
|
| 31 | + private string $plugin_slug; |
|
| 32 | + |
|
| 33 | + private string $version; |
|
| 34 | + |
|
| 35 | + private bool $beta; |
|
| 36 | + |
|
| 37 | + private bool $wp_override; |
|
| 38 | + |
|
| 39 | + private static array $slug_prefixes = ['ee-', 'eea-', 'eep-', 'espresso-', 'ee_', 'eea_', 'eep_', 'espresso_']; |
|
| 40 | + |
|
| 41 | + |
|
| 42 | + public function __construct( |
|
| 43 | + string $mainfile, |
|
| 44 | + string $plugin_id, |
|
| 45 | + string $plugin_name, |
|
| 46 | + string $plugin_slug, |
|
| 47 | + string $version, |
|
| 48 | + bool $beta = false, |
|
| 49 | + bool $wp_override = false, |
|
| 50 | + string $min_core_version = '' |
|
| 51 | + ) { |
|
| 52 | + $this->mainfile = $mainfile; |
|
| 53 | + $this->plugin_id = $plugin_id; |
|
| 54 | + $this->plugin_name = $plugin_name; |
|
| 55 | + $this->version = $version; |
|
| 56 | + $this->beta = $beta; |
|
| 57 | + $this->wp_override = $wp_override; |
|
| 58 | + $this->plugin_slug = $this->removePluginSlugPrefixes($plugin_slug); |
|
| 59 | + $this->min_core_version = $min_core_version; |
|
| 60 | + |
|
| 61 | + add_action('init', [$this, 'loadPluginUpdater']); |
|
| 62 | + add_action( |
|
| 63 | + 'AHEE__EventEspresso_core_services_licensing_PluginLicenseCollection__loadPluginLicenses', |
|
| 64 | + [$this, 'loadPluginLicense'] |
|
| 65 | + ); |
|
| 66 | + add_filter( |
|
| 67 | + 'FHEE__LicenseKeysAdminForm__generate__form_subsections', |
|
| 68 | + [$this, 'addLicenseKeyFormSection'] |
|
| 69 | + ); |
|
| 70 | + add_filter( |
|
| 71 | + 'edd_sl_plugin_updater_api_params', |
|
| 72 | + [$this, 'addCustomApiParams'], |
|
| 73 | + 10, |
|
| 74 | + 2 |
|
| 75 | + ); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * removes unnecessary prefixes from plugin slugs |
|
| 81 | + * |
|
| 82 | + * @param string $plugin_slug |
|
| 83 | + * @return string |
|
| 84 | + */ |
|
| 85 | + private function removePluginSlugPrefixes(string $plugin_slug): string |
|
| 86 | + { |
|
| 87 | + foreach (self::$slug_prefixes as $slug_prefix) { |
|
| 88 | + if (strpos($plugin_slug, $slug_prefix) === 0) { |
|
| 89 | + $plugin_slug = str_replace($slug_prefix, '', $plugin_slug); |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + return $plugin_slug; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @return PluginUpdater|null |
|
| 98 | + */ |
|
| 99 | + public function updater(): ?PluginUpdater |
|
| 100 | + { |
|
| 101 | + return $this->updater; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * @return string |
|
| 107 | + */ |
|
| 108 | + public function itemID(): string |
|
| 109 | + { |
|
| 110 | + return $this->plugin_id; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @return string |
|
| 116 | + */ |
|
| 117 | + public function itemName(): string |
|
| 118 | + { |
|
| 119 | + return $this->plugin_name; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + |
|
| 123 | + private function getLicenseKeyData(): stdClass |
|
| 124 | + { |
|
| 125 | + if (! $this->license_key_data instanceof LicenseKeyData) { |
|
| 126 | + $this->license_key_data = LoaderFactory::getShared(LicenseKeyData::class); |
|
| 127 | + } |
|
| 128 | + return $this->license_key_data->getLicense($this->pluginSlug()); |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * @return string |
|
| 134 | + */ |
|
| 135 | + public function licenseKey(): string |
|
| 136 | + { |
|
| 137 | + return $this->license_key; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @param string $license_key |
|
| 143 | + */ |
|
| 144 | + public function updateLicenseKey(string $license_key): void |
|
| 145 | + { |
|
| 146 | + $this->license_key = sanitize_text_field($license_key); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * @return string |
|
| 152 | + */ |
|
| 153 | + public function mainfile(): string |
|
| 154 | + { |
|
| 155 | + return $this->mainfile; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @return string |
|
| 161 | + * @since 5.0.20.p |
|
| 162 | + */ |
|
| 163 | + public function minCoreVersion(): string |
|
| 164 | + { |
|
| 165 | + return $this->min_core_version; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * @return string |
|
| 171 | + */ |
|
| 172 | + public function pluginSlug(): string |
|
| 173 | + { |
|
| 174 | + return $this->plugin_slug; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * @return string |
|
| 180 | + */ |
|
| 181 | + public function version(): string |
|
| 182 | + { |
|
| 183 | + return $this->version; |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * @return bool |
|
| 189 | + */ |
|
| 190 | + public function isBeta(): bool |
|
| 191 | + { |
|
| 192 | + return $this->beta; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * @return bool |
|
| 198 | + */ |
|
| 199 | + public function isWpOverride(): bool |
|
| 200 | + { |
|
| 201 | + return $this->wp_override; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + |
|
| 205 | + public function loadPluginUpdater() |
|
| 206 | + { |
|
| 207 | + // To support auto-updates, this needs to run during the wp_version_check cron job for privileged users. |
|
| 208 | + if (! current_user_can('manage_options') && ! (defined('DOING_CRON') && DOING_CRON)) { |
|
| 209 | + return; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + $license_key_data = $this->getLicenseKeyData(); |
|
| 213 | + $license_key = $license_key_data->license_key ?? ''; |
|
| 214 | + if ($license_key) { |
|
| 215 | + $this->updateLicenseKey($license_key); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + $this->updater = new PluginUpdater( |
|
| 219 | + LicenseAPI::url(), |
|
| 220 | + $this->mainfile(), |
|
| 221 | + [ |
|
| 222 | + 'author' => LicenseAPI::AUTHOR, |
|
| 223 | + 'beta' => $this->isBeta(), |
|
| 224 | + 'item_id' => $this->itemID(), |
|
| 225 | + 'item_name' => $this->itemName(), |
|
| 226 | + 'license' => $this->licenseKey(), |
|
| 227 | + 'version' => $this->version(), |
|
| 228 | + 'wp_override' => $this->isWpOverride(), |
|
| 229 | + ] |
|
| 230 | + ); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + |
|
| 234 | + public function loadPluginLicense(PluginLicenseCollection $plugin_license_collection) |
|
| 235 | + { |
|
| 236 | + $plugin_license_collection->add($this, $this->pluginSlug()); |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + |
|
| 240 | + public function addLicenseKeyFormSection(array $license_keys_form_subsections): array |
|
| 241 | + { |
|
| 242 | + if (is_main_site()) { |
|
| 243 | + $license_keys_form_subsections[ $this->pluginSlug() ] = LoaderFactory::getNew( |
|
| 244 | + LicenseKeyFormInput::class, |
|
| 245 | + [$this] |
|
| 246 | + ); |
|
| 247 | + } |
|
| 248 | + return $license_keys_form_subsections; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + |
|
| 252 | + public function addCustomApiParams($api_params, $api_data) |
|
| 253 | + { |
|
| 254 | + if ($api_data['item_name'] === $this->itemName()) { |
|
| 255 | + $api_params['event_espresso_core_version'] = EVENT_ESPRESSO_VERSION; |
|
| 256 | + } |
|
| 257 | + return $api_params; |
|
| 258 | + } |
|
| 259 | 259 | } |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | |
| 123 | 123 | private function getLicenseKeyData(): stdClass |
| 124 | 124 | { |
| 125 | - if (! $this->license_key_data instanceof LicenseKeyData) { |
|
| 125 | + if ( ! $this->license_key_data instanceof LicenseKeyData) { |
|
| 126 | 126 | $this->license_key_data = LoaderFactory::getShared(LicenseKeyData::class); |
| 127 | 127 | } |
| 128 | 128 | return $this->license_key_data->getLicense($this->pluginSlug()); |
@@ -205,7 +205,7 @@ discard block |
||
| 205 | 205 | public function loadPluginUpdater() |
| 206 | 206 | { |
| 207 | 207 | // To support auto-updates, this needs to run during the wp_version_check cron job for privileged users. |
| 208 | - if (! current_user_can('manage_options') && ! (defined('DOING_CRON') && DOING_CRON)) { |
|
| 208 | + if ( ! current_user_can('manage_options') && ! (defined('DOING_CRON') && DOING_CRON)) { |
|
| 209 | 209 | return; |
| 210 | 210 | } |
| 211 | 211 | |
@@ -240,7 +240,7 @@ discard block |
||
| 240 | 240 | public function addLicenseKeyFormSection(array $license_keys_form_subsections): array |
| 241 | 241 | { |
| 242 | 242 | if (is_main_site()) { |
| 243 | - $license_keys_form_subsections[ $this->pluginSlug() ] = LoaderFactory::getNew( |
|
| 243 | + $license_keys_form_subsections[$this->pluginSlug()] = LoaderFactory::getNew( |
|
| 244 | 244 | LicenseKeyFormInput::class, |
| 245 | 245 | [$this] |
| 246 | 246 | ); |
@@ -7,49 +7,49 @@ |
||
| 7 | 7 | |
| 8 | 8 | class LicenseKeyData extends WordPressOption |
| 9 | 9 | { |
| 10 | - const OPTION_NAME = 'event-espresso-license-keys'; |
|
| 10 | + const OPTION_NAME = 'event-espresso-license-keys'; |
|
| 11 | 11 | |
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * SessionLifespanOption constructor. |
|
| 15 | - */ |
|
| 16 | - public function __construct() |
|
| 17 | - { |
|
| 18 | - parent::__construct(LicenseKeyData::OPTION_NAME, [], true); |
|
| 19 | - } |
|
| 13 | + /** |
|
| 14 | + * SessionLifespanOption constructor. |
|
| 15 | + */ |
|
| 16 | + public function __construct() |
|
| 17 | + { |
|
| 18 | + parent::__construct(LicenseKeyData::OPTION_NAME, [], true); |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | 21 | |
| 22 | - public function getLicense(string $plugin): stdCLass |
|
| 23 | - { |
|
| 24 | - $licenses = $this->loadOption(); |
|
| 25 | - $license_data = $licenses[ $plugin ] ?? ['success' => false, 'error' => true]; |
|
| 26 | - return (object) $license_data; |
|
| 27 | - } |
|
| 22 | + public function getLicense(string $plugin): stdCLass |
|
| 23 | + { |
|
| 24 | + $licenses = $this->loadOption(); |
|
| 25 | + $license_data = $licenses[ $plugin ] ?? ['success' => false, 'error' => true]; |
|
| 26 | + return (object) $license_data; |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | 29 | |
| 30 | 30 | |
| 31 | - public function getAllLicenses(): array |
|
| 32 | - { |
|
| 33 | - return $this->loadOption(); |
|
| 34 | - } |
|
| 31 | + public function getAllLicenses(): array |
|
| 32 | + { |
|
| 33 | + return $this->loadOption(); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | 36 | |
| 37 | - public function updateLicense(stdCLass $license_data, string $plugin, bool $force_update = false): int |
|
| 38 | - { |
|
| 39 | - $licenses = $this->loadOption(); |
|
| 40 | - // convert objects to array and merge new data with old |
|
| 41 | - $licenses[ $plugin ] = (array) $license_data; |
|
| 42 | - // then sort by key and convert back to stdCLass |
|
| 43 | - ksort($licenses[ $plugin ]); |
|
| 44 | - $licenses[ $plugin ] = (object) $licenses[ $plugin ]; |
|
| 45 | - return $this->updateOption($licenses, $force_update); |
|
| 46 | - } |
|
| 37 | + public function updateLicense(stdCLass $license_data, string $plugin, bool $force_update = false): int |
|
| 38 | + { |
|
| 39 | + $licenses = $this->loadOption(); |
|
| 40 | + // convert objects to array and merge new data with old |
|
| 41 | + $licenses[ $plugin ] = (array) $license_data; |
|
| 42 | + // then sort by key and convert back to stdCLass |
|
| 43 | + ksort($licenses[ $plugin ]); |
|
| 44 | + $licenses[ $plugin ] = (object) $licenses[ $plugin ]; |
|
| 45 | + return $this->updateOption($licenses, $force_update); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | 48 | |
| 49 | - public function removeLicense(string $plugin): int |
|
| 50 | - { |
|
| 51 | - $licenses = $this->loadOption(); |
|
| 52 | - unset($licenses[ $plugin ]); |
|
| 53 | - return $this->updateOption($licenses); |
|
| 54 | - } |
|
| 49 | + public function removeLicense(string $plugin): int |
|
| 50 | + { |
|
| 51 | + $licenses = $this->loadOption(); |
|
| 52 | + unset($licenses[ $plugin ]); |
|
| 53 | + return $this->updateOption($licenses); |
|
| 54 | + } |
|
| 55 | 55 | } |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | public function getLicense(string $plugin): stdCLass |
| 23 | 23 | { |
| 24 | 24 | $licenses = $this->loadOption(); |
| 25 | - $license_data = $licenses[ $plugin ] ?? ['success' => false, 'error' => true]; |
|
| 25 | + $license_data = $licenses[$plugin] ?? ['success' => false, 'error' => true]; |
|
| 26 | 26 | return (object) $license_data; |
| 27 | 27 | } |
| 28 | 28 | |
@@ -38,10 +38,10 @@ discard block |
||
| 38 | 38 | { |
| 39 | 39 | $licenses = $this->loadOption(); |
| 40 | 40 | // convert objects to array and merge new data with old |
| 41 | - $licenses[ $plugin ] = (array) $license_data; |
|
| 41 | + $licenses[$plugin] = (array) $license_data; |
|
| 42 | 42 | // then sort by key and convert back to stdCLass |
| 43 | - ksort($licenses[ $plugin ]); |
|
| 44 | - $licenses[ $plugin ] = (object) $licenses[ $plugin ]; |
|
| 43 | + ksort($licenses[$plugin]); |
|
| 44 | + $licenses[$plugin] = (object) $licenses[$plugin]; |
|
| 45 | 45 | return $this->updateOption($licenses, $force_update); |
| 46 | 46 | } |
| 47 | 47 | |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | public function removeLicense(string $plugin): int |
| 50 | 50 | { |
| 51 | 51 | $licenses = $this->loadOption(); |
| 52 | - unset($licenses[ $plugin ]); |
|
| 52 | + unset($licenses[$plugin]); |
|
| 53 | 53 | return $this->updateOption($licenses); |
| 54 | 54 | } |
| 55 | 55 | } |
@@ -6,21 +6,21 @@ |
||
| 6 | 6 | |
| 7 | 7 | class PluginLicenseCollection extends Collection |
| 8 | 8 | { |
| 9 | - const COLLECTION_NAME = 'plugin licenses'; |
|
| 9 | + const COLLECTION_NAME = 'plugin licenses'; |
|
| 10 | 10 | |
| 11 | 11 | |
| 12 | - public function __construct() |
|
| 13 | - { |
|
| 14 | - parent::__construct(PluginLicense::class, PluginLicenseCollection::COLLECTION_NAME); |
|
| 15 | - add_action('init', [$this, 'loadPluginLicenses'], 99); |
|
| 16 | - } |
|
| 12 | + public function __construct() |
|
| 13 | + { |
|
| 14 | + parent::__construct(PluginLicense::class, PluginLicenseCollection::COLLECTION_NAME); |
|
| 15 | + add_action('init', [$this, 'loadPluginLicenses'], 99); |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | 18 | |
| 19 | - public function loadPluginLicenses() |
|
| 20 | - { |
|
| 21 | - do_action( |
|
| 22 | - 'AHEE__EventEspresso_core_services_licensing_PluginLicenseCollection__loadPluginLicenses', |
|
| 23 | - $this |
|
| 24 | - ); |
|
| 25 | - } |
|
| 19 | + public function loadPluginLicenses() |
|
| 20 | + { |
|
| 21 | + do_action( |
|
| 22 | + 'AHEE__EventEspresso_core_services_licensing_PluginLicenseCollection__loadPluginLicenses', |
|
| 23 | + $this |
|
| 24 | + ); |
|
| 25 | + } |
|
| 26 | 26 | } |
| 27 | 27 | \ No newline at end of file |
@@ -15,94 +15,94 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | abstract class JsonDataWordpressOption extends WordPressOption |
| 17 | 17 | { |
| 18 | - private JsonDataHandler $json_data_handler; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * @var array|mixed|stdClass |
|
| 22 | - */ |
|
| 23 | - private $options = []; |
|
| 24 | - |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * JsonDataWordpressOption constructor. |
|
| 28 | - * |
|
| 29 | - * @param JsonDataHandler $json_data_handler |
|
| 30 | - * @param string $option_name |
|
| 31 | - * @param $default_value |
|
| 32 | - * @param bool $autoload |
|
| 33 | - */ |
|
| 34 | - public function __construct( |
|
| 35 | - JsonDataHandler $json_data_handler, |
|
| 36 | - string $option_name, |
|
| 37 | - $default_value, |
|
| 38 | - bool $autoload = false |
|
| 39 | - ) { |
|
| 40 | - $this->json_data_handler = $json_data_handler; |
|
| 41 | - if (! $this->json_data_handler->dataType()) { |
|
| 42 | - $this->json_data_handler->configure(JsonDataHandler::DATA_TYPE_OBJECT); |
|
| 43 | - } |
|
| 44 | - parent::__construct($option_name, $default_value, $autoload); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @param $value |
|
| 50 | - * @return int |
|
| 51 | - */ |
|
| 52 | - public function updateOption($value, bool $force_update = false): int |
|
| 53 | - { |
|
| 54 | - $update = parent::updateOption($this->json_data_handler->encodeData($value), $force_update); |
|
| 55 | - if ($update === WordPressOption::UPDATE_SUCCESS) { |
|
| 56 | - $this->options = $value; |
|
| 57 | - } |
|
| 58 | - return $update; |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @param string $property |
|
| 64 | - * @param mixed $value |
|
| 65 | - * @return void |
|
| 66 | - */ |
|
| 67 | - public function addProperty(string $property, $value) |
|
| 68 | - { |
|
| 69 | - $options = $this->getAll(); |
|
| 70 | - $options->{$property} = $value; |
|
| 71 | - $this->updateOption($options); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @param string $property |
|
| 77 | - * @return mixed |
|
| 78 | - */ |
|
| 79 | - public function getProperty(string $property) |
|
| 80 | - { |
|
| 81 | - $options = $this->getAll(); |
|
| 82 | - return property_exists($options, $property) ? $options->{$property} : null; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @return array|mixed|stdClass |
|
| 88 | - */ |
|
| 89 | - public function getAll() |
|
| 90 | - { |
|
| 91 | - if (empty($this->options)) { |
|
| 92 | - $this->options = $this->json_data_handler->decodeJson($this->loadOption()); |
|
| 93 | - } |
|
| 94 | - return $this->options; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @param string $property |
|
| 100 | - * @return void |
|
| 101 | - */ |
|
| 102 | - public function removeProperty(string $property) |
|
| 103 | - { |
|
| 104 | - $options = $this->getAll(); |
|
| 105 | - unset($options->{$property}); |
|
| 106 | - $this->updateOption($options); |
|
| 107 | - } |
|
| 18 | + private JsonDataHandler $json_data_handler; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * @var array|mixed|stdClass |
|
| 22 | + */ |
|
| 23 | + private $options = []; |
|
| 24 | + |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * JsonDataWordpressOption constructor. |
|
| 28 | + * |
|
| 29 | + * @param JsonDataHandler $json_data_handler |
|
| 30 | + * @param string $option_name |
|
| 31 | + * @param $default_value |
|
| 32 | + * @param bool $autoload |
|
| 33 | + */ |
|
| 34 | + public function __construct( |
|
| 35 | + JsonDataHandler $json_data_handler, |
|
| 36 | + string $option_name, |
|
| 37 | + $default_value, |
|
| 38 | + bool $autoload = false |
|
| 39 | + ) { |
|
| 40 | + $this->json_data_handler = $json_data_handler; |
|
| 41 | + if (! $this->json_data_handler->dataType()) { |
|
| 42 | + $this->json_data_handler->configure(JsonDataHandler::DATA_TYPE_OBJECT); |
|
| 43 | + } |
|
| 44 | + parent::__construct($option_name, $default_value, $autoload); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @param $value |
|
| 50 | + * @return int |
|
| 51 | + */ |
|
| 52 | + public function updateOption($value, bool $force_update = false): int |
|
| 53 | + { |
|
| 54 | + $update = parent::updateOption($this->json_data_handler->encodeData($value), $force_update); |
|
| 55 | + if ($update === WordPressOption::UPDATE_SUCCESS) { |
|
| 56 | + $this->options = $value; |
|
| 57 | + } |
|
| 58 | + return $update; |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @param string $property |
|
| 64 | + * @param mixed $value |
|
| 65 | + * @return void |
|
| 66 | + */ |
|
| 67 | + public function addProperty(string $property, $value) |
|
| 68 | + { |
|
| 69 | + $options = $this->getAll(); |
|
| 70 | + $options->{$property} = $value; |
|
| 71 | + $this->updateOption($options); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @param string $property |
|
| 77 | + * @return mixed |
|
| 78 | + */ |
|
| 79 | + public function getProperty(string $property) |
|
| 80 | + { |
|
| 81 | + $options = $this->getAll(); |
|
| 82 | + return property_exists($options, $property) ? $options->{$property} : null; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @return array|mixed|stdClass |
|
| 88 | + */ |
|
| 89 | + public function getAll() |
|
| 90 | + { |
|
| 91 | + if (empty($this->options)) { |
|
| 92 | + $this->options = $this->json_data_handler->decodeJson($this->loadOption()); |
|
| 93 | + } |
|
| 94 | + return $this->options; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @param string $property |
|
| 100 | + * @return void |
|
| 101 | + */ |
|
| 102 | + public function removeProperty(string $property) |
|
| 103 | + { |
|
| 104 | + $options = $this->getAll(); |
|
| 105 | + unset($options->{$property}); |
|
| 106 | + $this->updateOption($options); |
|
| 107 | + } |
|
| 108 | 108 | } |
@@ -30,219 +30,219 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | class ShortcodesManager |
| 32 | 32 | { |
| 33 | - /** |
|
| 34 | - * @type CurrentPage |
|
| 35 | - */ |
|
| 36 | - protected $current_page; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @var LegacyShortcodesManager $legacy_shortcodes_manager |
|
| 40 | - */ |
|
| 41 | - private $legacy_shortcodes_manager; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var CollectionInterface|ShortcodeInterface[] $shortcodes |
|
| 45 | - */ |
|
| 46 | - private $shortcodes; |
|
| 47 | - |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * ShortcodesManager constructor |
|
| 51 | - * |
|
| 52 | - * @param LegacyShortcodesManager $legacy_shortcodes_manager |
|
| 53 | - * @param CurrentPage $current_page |
|
| 54 | - */ |
|
| 55 | - public function __construct(LegacyShortcodesManager $legacy_shortcodes_manager, CurrentPage $current_page) |
|
| 56 | - { |
|
| 57 | - $this->legacy_shortcodes_manager = $legacy_shortcodes_manager; |
|
| 58 | - $this->current_page = $current_page; |
|
| 59 | - // assemble a list of installed and active shortcodes |
|
| 60 | - add_action( |
|
| 61 | - 'AHEE__EE_System__register_shortcodes_modules_and_widgets', |
|
| 62 | - [$this, 'registerShortcodes'], |
|
| 63 | - 999 |
|
| 64 | - ); |
|
| 65 | - // call add_shortcode() for all installed shortcodes |
|
| 66 | - add_action('AHEE__EE_System__core_loaded_and_ready', [$this, 'addShortcodes']); |
|
| 67 | - // check content for shortcodes the old way |
|
| 68 | - add_action('parse_query', [$this->legacy_shortcodes_manager, 'initializeShortcodes'], 5); |
|
| 69 | - // check content for shortcodes the NEW more efficient way |
|
| 70 | - add_action('template_redirect', [$this, 'templateRedirect'], 999); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @return CollectionInterface|ShortcodeInterface[] |
|
| 76 | - * @throws InvalidIdentifierException |
|
| 77 | - * @throws InvalidInterfaceException |
|
| 78 | - * @throws InvalidFilePathException |
|
| 79 | - * @throws InvalidEntityException |
|
| 80 | - * @throws InvalidDataTypeException |
|
| 81 | - * @throws InvalidClassException |
|
| 82 | - */ |
|
| 83 | - public function getShortcodes() |
|
| 84 | - { |
|
| 85 | - if (! $this->shortcodes instanceof CollectionInterface) { |
|
| 86 | - $this->shortcodes = $this->loadShortcodesCollection(); |
|
| 87 | - } |
|
| 88 | - return $this->shortcodes; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @return CollectionInterface|ShortcodeInterface[] |
|
| 94 | - * @throws InvalidIdentifierException |
|
| 95 | - * @throws InvalidInterfaceException |
|
| 96 | - * @throws InvalidFilePathException |
|
| 97 | - * @throws InvalidEntityException |
|
| 98 | - * @throws InvalidDataTypeException |
|
| 99 | - * @throws InvalidClassException |
|
| 100 | - */ |
|
| 101 | - protected function loadShortcodesCollection() |
|
| 102 | - { |
|
| 103 | - $loader = new CollectionLoader( |
|
| 104 | - new CollectionDetails( |
|
| 105 | - // collection name |
|
| 106 | - 'shortcodes', |
|
| 107 | - // collection interface |
|
| 108 | - 'EventEspresso\core\services\shortcodes\ShortcodeInterface', |
|
| 109 | - // FQCNs for classes to add (all classes within that namespace will be loaded) |
|
| 110 | - ['EventEspresso\core\domain\entities\shortcodes'], |
|
| 111 | - // filepaths to classes to add |
|
| 112 | - [], |
|
| 113 | - // file mask to use if parsing folder for files to add |
|
| 114 | - '', |
|
| 115 | - // what to use as identifier for collection entities |
|
| 116 | - // using CLASS NAME prevents duplicates (works like a singleton) |
|
| 117 | - CollectionDetails::ID_CLASS_NAME |
|
| 118 | - ) |
|
| 119 | - ); |
|
| 120 | - return $loader->getCollection(); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * @return void |
|
| 126 | - * @throws DomainException |
|
| 127 | - * @throws InvalidInterfaceException |
|
| 128 | - * @throws InvalidIdentifierException |
|
| 129 | - * @throws InvalidFilePathException |
|
| 130 | - * @throws InvalidEntityException |
|
| 131 | - * @throws InvalidDataTypeException |
|
| 132 | - * @throws InvalidClassException |
|
| 133 | - * @throws Exception |
|
| 134 | - */ |
|
| 135 | - public function registerShortcodes() |
|
| 136 | - { |
|
| 137 | - try { |
|
| 138 | - $this->shortcodes = apply_filters( |
|
| 139 | - 'FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection', |
|
| 140 | - $this->getShortcodes() |
|
| 141 | - ); |
|
| 142 | - if (! $this->shortcodes instanceof CollectionInterface) { |
|
| 143 | - throw new InvalidEntityException( |
|
| 144 | - $this->shortcodes, |
|
| 145 | - 'CollectionInterface', |
|
| 146 | - sprintf( |
|
| 147 | - esc_html__( |
|
| 148 | - 'The "FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection" filter must return a Collection of EspressoShortcode objects. "%1$s" was received instead.', |
|
| 149 | - 'event_espresso' |
|
| 150 | - ), |
|
| 151 | - is_object($this->shortcodes) ? get_class($this->shortcodes) : gettype($this->shortcodes) |
|
| 152 | - ) |
|
| 153 | - ); |
|
| 154 | - } |
|
| 155 | - $this->legacy_shortcodes_manager->registerShortcodes(); |
|
| 156 | - } catch (Exception $exception) { |
|
| 157 | - new ExceptionStackTraceDisplay($exception); |
|
| 158 | - } |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * @return void |
|
| 164 | - * @throws Exception |
|
| 165 | - */ |
|
| 166 | - public function addShortcodes() |
|
| 167 | - { |
|
| 168 | - try { |
|
| 169 | - // cycle thru shortcode folders |
|
| 170 | - foreach ($this->shortcodes as $shortcode) { |
|
| 171 | - if ($shortcode instanceof EnqueueAssetsInterface) { |
|
| 172 | - add_action('wp_enqueue_scripts', [$shortcode, 'registerScriptsAndStylesheets'], 10); |
|
| 173 | - add_action('wp_enqueue_scripts', [$shortcode, 'enqueueStylesheets'], 11); |
|
| 174 | - } |
|
| 175 | - // add_shortcode() if it has not already been added |
|
| 176 | - if (! shortcode_exists($shortcode->getTag())) { |
|
| 177 | - add_shortcode($shortcode->getTag(), [$shortcode, 'processShortcodeCallback']); |
|
| 178 | - } |
|
| 179 | - } |
|
| 180 | - $this->legacy_shortcodes_manager->addShortcodes(); |
|
| 181 | - } catch (Exception $exception) { |
|
| 182 | - new ExceptionStackTraceDisplay($exception); |
|
| 183 | - } |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * callback for the "template_redirect" hook point |
|
| 189 | - * checks posts for EE shortcodes, and initializes them, |
|
| 190 | - * then toggles filter switch that loads core default assets |
|
| 191 | - * |
|
| 192 | - * @return void |
|
| 193 | - */ |
|
| 194 | - public function templateRedirect() |
|
| 195 | - { |
|
| 196 | - global $wp_query; |
|
| 197 | - if (empty($wp_query->posts)) { |
|
| 198 | - return; |
|
| 199 | - } |
|
| 200 | - $load_assets = false; |
|
| 201 | - // array of posts displayed in current request |
|
| 202 | - $posts = is_array($wp_query->posts) ? $wp_query->posts : [$wp_query->posts]; |
|
| 203 | - foreach ($posts as $post) { |
|
| 204 | - // now check post content and excerpt for EE shortcodes |
|
| 205 | - $load_assets = $this->parseContentForShortcodes((string) $post->post_content, $post) |
|
| 206 | - ? true |
|
| 207 | - : $load_assets; |
|
| 208 | - } |
|
| 209 | - if ($load_assets) { |
|
| 210 | - $this->current_page->setEspressoPage(true); |
|
| 211 | - add_filter('FHEE_load_css', '__return_true'); |
|
| 212 | - add_filter('FHEE_load_js', '__return_true'); |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * checks supplied content against list of shortcodes, |
|
| 219 | - * then initializes any found shortcodes, and returns true. |
|
| 220 | - * returns false if no shortcodes found. |
|
| 221 | - * |
|
| 222 | - * @param string $content |
|
| 223 | - * @param WP_Post $post |
|
| 224 | - * @return bool |
|
| 225 | - */ |
|
| 226 | - public function parseContentForShortcodes(string $content, WP_Post $post): bool |
|
| 227 | - { |
|
| 228 | - if (empty($this->shortcodes)) { |
|
| 229 | - return false; |
|
| 230 | - } |
|
| 231 | - $has_shortcode = false; |
|
| 232 | - foreach ($this->shortcodes as $shortcode) { |
|
| 233 | - if ( |
|
| 234 | - apply_filters( |
|
| 235 | - 'FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__parseContentForShortcodes__has_shortcode', |
|
| 236 | - has_shortcode($content, $shortcode->getTag()), |
|
| 237 | - $content, |
|
| 238 | - $post, |
|
| 239 | - $shortcode |
|
| 240 | - ) |
|
| 241 | - ) { |
|
| 242 | - $shortcode->initializeShortcode(); |
|
| 243 | - $has_shortcode = true; |
|
| 244 | - } |
|
| 245 | - } |
|
| 246 | - return $has_shortcode; |
|
| 247 | - } |
|
| 33 | + /** |
|
| 34 | + * @type CurrentPage |
|
| 35 | + */ |
|
| 36 | + protected $current_page; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @var LegacyShortcodesManager $legacy_shortcodes_manager |
|
| 40 | + */ |
|
| 41 | + private $legacy_shortcodes_manager; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var CollectionInterface|ShortcodeInterface[] $shortcodes |
|
| 45 | + */ |
|
| 46 | + private $shortcodes; |
|
| 47 | + |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * ShortcodesManager constructor |
|
| 51 | + * |
|
| 52 | + * @param LegacyShortcodesManager $legacy_shortcodes_manager |
|
| 53 | + * @param CurrentPage $current_page |
|
| 54 | + */ |
|
| 55 | + public function __construct(LegacyShortcodesManager $legacy_shortcodes_manager, CurrentPage $current_page) |
|
| 56 | + { |
|
| 57 | + $this->legacy_shortcodes_manager = $legacy_shortcodes_manager; |
|
| 58 | + $this->current_page = $current_page; |
|
| 59 | + // assemble a list of installed and active shortcodes |
|
| 60 | + add_action( |
|
| 61 | + 'AHEE__EE_System__register_shortcodes_modules_and_widgets', |
|
| 62 | + [$this, 'registerShortcodes'], |
|
| 63 | + 999 |
|
| 64 | + ); |
|
| 65 | + // call add_shortcode() for all installed shortcodes |
|
| 66 | + add_action('AHEE__EE_System__core_loaded_and_ready', [$this, 'addShortcodes']); |
|
| 67 | + // check content for shortcodes the old way |
|
| 68 | + add_action('parse_query', [$this->legacy_shortcodes_manager, 'initializeShortcodes'], 5); |
|
| 69 | + // check content for shortcodes the NEW more efficient way |
|
| 70 | + add_action('template_redirect', [$this, 'templateRedirect'], 999); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @return CollectionInterface|ShortcodeInterface[] |
|
| 76 | + * @throws InvalidIdentifierException |
|
| 77 | + * @throws InvalidInterfaceException |
|
| 78 | + * @throws InvalidFilePathException |
|
| 79 | + * @throws InvalidEntityException |
|
| 80 | + * @throws InvalidDataTypeException |
|
| 81 | + * @throws InvalidClassException |
|
| 82 | + */ |
|
| 83 | + public function getShortcodes() |
|
| 84 | + { |
|
| 85 | + if (! $this->shortcodes instanceof CollectionInterface) { |
|
| 86 | + $this->shortcodes = $this->loadShortcodesCollection(); |
|
| 87 | + } |
|
| 88 | + return $this->shortcodes; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @return CollectionInterface|ShortcodeInterface[] |
|
| 94 | + * @throws InvalidIdentifierException |
|
| 95 | + * @throws InvalidInterfaceException |
|
| 96 | + * @throws InvalidFilePathException |
|
| 97 | + * @throws InvalidEntityException |
|
| 98 | + * @throws InvalidDataTypeException |
|
| 99 | + * @throws InvalidClassException |
|
| 100 | + */ |
|
| 101 | + protected function loadShortcodesCollection() |
|
| 102 | + { |
|
| 103 | + $loader = new CollectionLoader( |
|
| 104 | + new CollectionDetails( |
|
| 105 | + // collection name |
|
| 106 | + 'shortcodes', |
|
| 107 | + // collection interface |
|
| 108 | + 'EventEspresso\core\services\shortcodes\ShortcodeInterface', |
|
| 109 | + // FQCNs for classes to add (all classes within that namespace will be loaded) |
|
| 110 | + ['EventEspresso\core\domain\entities\shortcodes'], |
|
| 111 | + // filepaths to classes to add |
|
| 112 | + [], |
|
| 113 | + // file mask to use if parsing folder for files to add |
|
| 114 | + '', |
|
| 115 | + // what to use as identifier for collection entities |
|
| 116 | + // using CLASS NAME prevents duplicates (works like a singleton) |
|
| 117 | + CollectionDetails::ID_CLASS_NAME |
|
| 118 | + ) |
|
| 119 | + ); |
|
| 120 | + return $loader->getCollection(); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * @return void |
|
| 126 | + * @throws DomainException |
|
| 127 | + * @throws InvalidInterfaceException |
|
| 128 | + * @throws InvalidIdentifierException |
|
| 129 | + * @throws InvalidFilePathException |
|
| 130 | + * @throws InvalidEntityException |
|
| 131 | + * @throws InvalidDataTypeException |
|
| 132 | + * @throws InvalidClassException |
|
| 133 | + * @throws Exception |
|
| 134 | + */ |
|
| 135 | + public function registerShortcodes() |
|
| 136 | + { |
|
| 137 | + try { |
|
| 138 | + $this->shortcodes = apply_filters( |
|
| 139 | + 'FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection', |
|
| 140 | + $this->getShortcodes() |
|
| 141 | + ); |
|
| 142 | + if (! $this->shortcodes instanceof CollectionInterface) { |
|
| 143 | + throw new InvalidEntityException( |
|
| 144 | + $this->shortcodes, |
|
| 145 | + 'CollectionInterface', |
|
| 146 | + sprintf( |
|
| 147 | + esc_html__( |
|
| 148 | + 'The "FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__registerShortcodes__shortcode_collection" filter must return a Collection of EspressoShortcode objects. "%1$s" was received instead.', |
|
| 149 | + 'event_espresso' |
|
| 150 | + ), |
|
| 151 | + is_object($this->shortcodes) ? get_class($this->shortcodes) : gettype($this->shortcodes) |
|
| 152 | + ) |
|
| 153 | + ); |
|
| 154 | + } |
|
| 155 | + $this->legacy_shortcodes_manager->registerShortcodes(); |
|
| 156 | + } catch (Exception $exception) { |
|
| 157 | + new ExceptionStackTraceDisplay($exception); |
|
| 158 | + } |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * @return void |
|
| 164 | + * @throws Exception |
|
| 165 | + */ |
|
| 166 | + public function addShortcodes() |
|
| 167 | + { |
|
| 168 | + try { |
|
| 169 | + // cycle thru shortcode folders |
|
| 170 | + foreach ($this->shortcodes as $shortcode) { |
|
| 171 | + if ($shortcode instanceof EnqueueAssetsInterface) { |
|
| 172 | + add_action('wp_enqueue_scripts', [$shortcode, 'registerScriptsAndStylesheets'], 10); |
|
| 173 | + add_action('wp_enqueue_scripts', [$shortcode, 'enqueueStylesheets'], 11); |
|
| 174 | + } |
|
| 175 | + // add_shortcode() if it has not already been added |
|
| 176 | + if (! shortcode_exists($shortcode->getTag())) { |
|
| 177 | + add_shortcode($shortcode->getTag(), [$shortcode, 'processShortcodeCallback']); |
|
| 178 | + } |
|
| 179 | + } |
|
| 180 | + $this->legacy_shortcodes_manager->addShortcodes(); |
|
| 181 | + } catch (Exception $exception) { |
|
| 182 | + new ExceptionStackTraceDisplay($exception); |
|
| 183 | + } |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * callback for the "template_redirect" hook point |
|
| 189 | + * checks posts for EE shortcodes, and initializes them, |
|
| 190 | + * then toggles filter switch that loads core default assets |
|
| 191 | + * |
|
| 192 | + * @return void |
|
| 193 | + */ |
|
| 194 | + public function templateRedirect() |
|
| 195 | + { |
|
| 196 | + global $wp_query; |
|
| 197 | + if (empty($wp_query->posts)) { |
|
| 198 | + return; |
|
| 199 | + } |
|
| 200 | + $load_assets = false; |
|
| 201 | + // array of posts displayed in current request |
|
| 202 | + $posts = is_array($wp_query->posts) ? $wp_query->posts : [$wp_query->posts]; |
|
| 203 | + foreach ($posts as $post) { |
|
| 204 | + // now check post content and excerpt for EE shortcodes |
|
| 205 | + $load_assets = $this->parseContentForShortcodes((string) $post->post_content, $post) |
|
| 206 | + ? true |
|
| 207 | + : $load_assets; |
|
| 208 | + } |
|
| 209 | + if ($load_assets) { |
|
| 210 | + $this->current_page->setEspressoPage(true); |
|
| 211 | + add_filter('FHEE_load_css', '__return_true'); |
|
| 212 | + add_filter('FHEE_load_js', '__return_true'); |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * checks supplied content against list of shortcodes, |
|
| 219 | + * then initializes any found shortcodes, and returns true. |
|
| 220 | + * returns false if no shortcodes found. |
|
| 221 | + * |
|
| 222 | + * @param string $content |
|
| 223 | + * @param WP_Post $post |
|
| 224 | + * @return bool |
|
| 225 | + */ |
|
| 226 | + public function parseContentForShortcodes(string $content, WP_Post $post): bool |
|
| 227 | + { |
|
| 228 | + if (empty($this->shortcodes)) { |
|
| 229 | + return false; |
|
| 230 | + } |
|
| 231 | + $has_shortcode = false; |
|
| 232 | + foreach ($this->shortcodes as $shortcode) { |
|
| 233 | + if ( |
|
| 234 | + apply_filters( |
|
| 235 | + 'FHEE__EventEspresso_core_services_shortcodes_ShortcodesManager__parseContentForShortcodes__has_shortcode', |
|
| 236 | + has_shortcode($content, $shortcode->getTag()), |
|
| 237 | + $content, |
|
| 238 | + $post, |
|
| 239 | + $shortcode |
|
| 240 | + ) |
|
| 241 | + ) { |
|
| 242 | + $shortcode->initializeShortcode(); |
|
| 243 | + $has_shortcode = true; |
|
| 244 | + } |
|
| 245 | + } |
|
| 246 | + return $has_shortcode; |
|
| 247 | + } |
|
| 248 | 248 | } |
@@ -17,173 +17,173 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | abstract class WordPressOption |
| 19 | 19 | { |
| 20 | - public const NOT_SET_YET = 'wordpress-option-value-not-yet-set'; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * WordPress makes it difficult to determine if an option successfully saved or not, |
|
| 24 | - * which is sometimes really important to know, especially if the information you are saving is critical. |
|
| 25 | - * The following options allow us to have a better chance of knowing when an update actually failed |
|
| 26 | - * or when everything is OK but it just didn't update because the value hasn't changed. |
|
| 27 | - */ |
|
| 28 | - public const UPDATE_SUCCESS = 1; |
|
| 29 | - |
|
| 30 | - public const UPDATE_NONE = 0; |
|
| 31 | - |
|
| 32 | - public const UPDATE_ERROR = -1; |
|
| 33 | - |
|
| 34 | - private bool $autoload = false; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var mixed |
|
| 38 | - */ |
|
| 39 | - private $default_value = null; |
|
| 40 | - |
|
| 41 | - private string $option_name = ''; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @var mixed |
|
| 45 | - */ |
|
| 46 | - private $value = WordPressOption::NOT_SET_YET; |
|
| 47 | - |
|
| 48 | - private OptionEngine $option_engine; |
|
| 49 | - |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * WordPressOption constructor. |
|
| 53 | - * |
|
| 54 | - * @param string $option_name |
|
| 55 | - * @param mixed $default_value |
|
| 56 | - * @param bool $autoload if true, will load the option on EVERY request |
|
| 57 | - * @param bool $is_network_option if true, will save the option to the network as opposed to the current blog |
|
| 58 | - */ |
|
| 59 | - public function __construct( |
|
| 60 | - string $option_name, |
|
| 61 | - $default_value, |
|
| 62 | - bool $autoload = false, |
|
| 63 | - bool $is_network_option = false |
|
| 64 | - ) { |
|
| 65 | - $this->setAutoload($autoload); |
|
| 66 | - $this->setDefaultValue($default_value); |
|
| 67 | - $this->setOptionName($option_name); |
|
| 68 | - $this->option_engine = new OptionEngine($is_network_option); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @param bool|string $autoload |
|
| 74 | - */ |
|
| 75 | - public function setAutoload($autoload): void |
|
| 76 | - { |
|
| 77 | - $this->autoload = filter_var($autoload, FILTER_VALIDATE_BOOLEAN); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @param mixed $default_value |
|
| 83 | - */ |
|
| 84 | - public function setDefaultValue($default_value): void |
|
| 85 | - { |
|
| 86 | - $this->default_value = $default_value; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @param string $option_name |
|
| 92 | - */ |
|
| 93 | - public function setOptionName(string $option_name): void |
|
| 94 | - { |
|
| 95 | - $this->option_name = sanitize_key($option_name); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * @return string |
|
| 101 | - */ |
|
| 102 | - public function optionExists(): string |
|
| 103 | - { |
|
| 104 | - return $this->option_engine->getOption( |
|
| 105 | - $this->getOptionName(), |
|
| 106 | - WordPressOption::NOT_SET_YET |
|
| 107 | - ) !== WordPressOption::NOT_SET_YET; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @return string |
|
| 113 | - */ |
|
| 114 | - public function getOptionName(): string |
|
| 115 | - { |
|
| 116 | - return $this->option_name; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @return false|mixed|void |
|
| 122 | - */ |
|
| 123 | - public function loadOption() |
|
| 124 | - { |
|
| 125 | - if ($this->value === WordPressOption::NOT_SET_YET) { |
|
| 126 | - $this->value = $this->option_engine->getOption($this->getOptionName(), $this->default_value); |
|
| 127 | - } |
|
| 128 | - return $this->value; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * @param $value |
|
| 134 | - * @param bool $force_update |
|
| 135 | - * @return int |
|
| 136 | - */ |
|
| 137 | - public function updateOption($value, bool $force_update = false): int |
|
| 138 | - { |
|
| 139 | - // don't update if value has not changed since last update |
|
| 140 | - if (! $force_update && $this->valueIsUnchanged($value)) { |
|
| 141 | - return WordPressOption::UPDATE_NONE; |
|
| 142 | - } |
|
| 143 | - $this->value = $value; |
|
| 144 | - // because the options for updating differ when adding an option for the first time |
|
| 145 | - // we use the WordPressOption::NOT_SET_YET to determine if things already exist in the db |
|
| 146 | - $updated = $this->optionExists() |
|
| 147 | - ? $this->option_engine->updateOption($this->getOptionName(), $this->value) |
|
| 148 | - : $this->option_engine->addOption($this->getOptionName(), $this->value, $this->autoload()); |
|
| 149 | - |
|
| 150 | - if ($updated) { |
|
| 151 | - return WordPressOption::UPDATE_SUCCESS; |
|
| 152 | - } |
|
| 153 | - return WordPressOption::UPDATE_ERROR; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - |
|
| 157 | - private function valueIsUnchanged($value): bool |
|
| 158 | - { |
|
| 159 | - if (is_array($value) && is_array($this->value)) { |
|
| 160 | - $diff = EEH_Array::array_diff_recursive($value, $this->value); |
|
| 161 | - // $diff = array_diff($value, $this->value); |
|
| 162 | - return empty($diff); |
|
| 163 | - } |
|
| 164 | - // emulate WP's method for checking equality |
|
| 165 | - return $value === $this->value && maybe_serialize($value) === maybe_serialize($this->value); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - |
|
| 169 | - /** |
|
| 170 | - * @return string |
|
| 171 | - */ |
|
| 172 | - private function autoload(): string |
|
| 173 | - { |
|
| 174 | - return $this->autoload ? 'yes' : 'no'; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Deletes the option from the database |
|
| 180 | - * for the rest of the request |
|
| 181 | - * |
|
| 182 | - * @return bool |
|
| 183 | - * @since 5.0.0.p |
|
| 184 | - */ |
|
| 185 | - public function deleteOption(): bool |
|
| 186 | - { |
|
| 187 | - return $this->option_engine->deleteOption($this->getOptionName()); |
|
| 188 | - } |
|
| 20 | + public const NOT_SET_YET = 'wordpress-option-value-not-yet-set'; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * WordPress makes it difficult to determine if an option successfully saved or not, |
|
| 24 | + * which is sometimes really important to know, especially if the information you are saving is critical. |
|
| 25 | + * The following options allow us to have a better chance of knowing when an update actually failed |
|
| 26 | + * or when everything is OK but it just didn't update because the value hasn't changed. |
|
| 27 | + */ |
|
| 28 | + public const UPDATE_SUCCESS = 1; |
|
| 29 | + |
|
| 30 | + public const UPDATE_NONE = 0; |
|
| 31 | + |
|
| 32 | + public const UPDATE_ERROR = -1; |
|
| 33 | + |
|
| 34 | + private bool $autoload = false; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var mixed |
|
| 38 | + */ |
|
| 39 | + private $default_value = null; |
|
| 40 | + |
|
| 41 | + private string $option_name = ''; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @var mixed |
|
| 45 | + */ |
|
| 46 | + private $value = WordPressOption::NOT_SET_YET; |
|
| 47 | + |
|
| 48 | + private OptionEngine $option_engine; |
|
| 49 | + |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * WordPressOption constructor. |
|
| 53 | + * |
|
| 54 | + * @param string $option_name |
|
| 55 | + * @param mixed $default_value |
|
| 56 | + * @param bool $autoload if true, will load the option on EVERY request |
|
| 57 | + * @param bool $is_network_option if true, will save the option to the network as opposed to the current blog |
|
| 58 | + */ |
|
| 59 | + public function __construct( |
|
| 60 | + string $option_name, |
|
| 61 | + $default_value, |
|
| 62 | + bool $autoload = false, |
|
| 63 | + bool $is_network_option = false |
|
| 64 | + ) { |
|
| 65 | + $this->setAutoload($autoload); |
|
| 66 | + $this->setDefaultValue($default_value); |
|
| 67 | + $this->setOptionName($option_name); |
|
| 68 | + $this->option_engine = new OptionEngine($is_network_option); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @param bool|string $autoload |
|
| 74 | + */ |
|
| 75 | + public function setAutoload($autoload): void |
|
| 76 | + { |
|
| 77 | + $this->autoload = filter_var($autoload, FILTER_VALIDATE_BOOLEAN); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @param mixed $default_value |
|
| 83 | + */ |
|
| 84 | + public function setDefaultValue($default_value): void |
|
| 85 | + { |
|
| 86 | + $this->default_value = $default_value; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @param string $option_name |
|
| 92 | + */ |
|
| 93 | + public function setOptionName(string $option_name): void |
|
| 94 | + { |
|
| 95 | + $this->option_name = sanitize_key($option_name); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * @return string |
|
| 101 | + */ |
|
| 102 | + public function optionExists(): string |
|
| 103 | + { |
|
| 104 | + return $this->option_engine->getOption( |
|
| 105 | + $this->getOptionName(), |
|
| 106 | + WordPressOption::NOT_SET_YET |
|
| 107 | + ) !== WordPressOption::NOT_SET_YET; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @return string |
|
| 113 | + */ |
|
| 114 | + public function getOptionName(): string |
|
| 115 | + { |
|
| 116 | + return $this->option_name; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @return false|mixed|void |
|
| 122 | + */ |
|
| 123 | + public function loadOption() |
|
| 124 | + { |
|
| 125 | + if ($this->value === WordPressOption::NOT_SET_YET) { |
|
| 126 | + $this->value = $this->option_engine->getOption($this->getOptionName(), $this->default_value); |
|
| 127 | + } |
|
| 128 | + return $this->value; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * @param $value |
|
| 134 | + * @param bool $force_update |
|
| 135 | + * @return int |
|
| 136 | + */ |
|
| 137 | + public function updateOption($value, bool $force_update = false): int |
|
| 138 | + { |
|
| 139 | + // don't update if value has not changed since last update |
|
| 140 | + if (! $force_update && $this->valueIsUnchanged($value)) { |
|
| 141 | + return WordPressOption::UPDATE_NONE; |
|
| 142 | + } |
|
| 143 | + $this->value = $value; |
|
| 144 | + // because the options for updating differ when adding an option for the first time |
|
| 145 | + // we use the WordPressOption::NOT_SET_YET to determine if things already exist in the db |
|
| 146 | + $updated = $this->optionExists() |
|
| 147 | + ? $this->option_engine->updateOption($this->getOptionName(), $this->value) |
|
| 148 | + : $this->option_engine->addOption($this->getOptionName(), $this->value, $this->autoload()); |
|
| 149 | + |
|
| 150 | + if ($updated) { |
|
| 151 | + return WordPressOption::UPDATE_SUCCESS; |
|
| 152 | + } |
|
| 153 | + return WordPressOption::UPDATE_ERROR; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + |
|
| 157 | + private function valueIsUnchanged($value): bool |
|
| 158 | + { |
|
| 159 | + if (is_array($value) && is_array($this->value)) { |
|
| 160 | + $diff = EEH_Array::array_diff_recursive($value, $this->value); |
|
| 161 | + // $diff = array_diff($value, $this->value); |
|
| 162 | + return empty($diff); |
|
| 163 | + } |
|
| 164 | + // emulate WP's method for checking equality |
|
| 165 | + return $value === $this->value && maybe_serialize($value) === maybe_serialize($this->value); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + |
|
| 169 | + /** |
|
| 170 | + * @return string |
|
| 171 | + */ |
|
| 172 | + private function autoload(): string |
|
| 173 | + { |
|
| 174 | + return $this->autoload ? 'yes' : 'no'; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Deletes the option from the database |
|
| 180 | + * for the rest of the request |
|
| 181 | + * |
|
| 182 | + * @return bool |
|
| 183 | + * @since 5.0.0.p |
|
| 184 | + */ |
|
| 185 | + public function deleteOption(): bool |
|
| 186 | + { |
|
| 187 | + return $this->option_engine->deleteOption($this->getOptionName()); |
|
| 188 | + } |
|
| 189 | 189 | } |
@@ -137,7 +137,7 @@ |
||
| 137 | 137 | public function updateOption($value, bool $force_update = false): int |
| 138 | 138 | { |
| 139 | 139 | // don't update if value has not changed since last update |
| 140 | - if (! $force_update && $this->valueIsUnchanged($value)) { |
|
| 140 | + if ( ! $force_update && $this->valueIsUnchanged($value)) { |
|
| 141 | 141 | return WordPressOption::UPDATE_NONE; |
| 142 | 142 | } |
| 143 | 143 | $this->value = $value; |