← All changes
|
includes/admin/settings/class-ph-settings-licenses.php
+200
-47
1.4.5
→
2.4.0
View file →
| @@ -1,5 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | +// phpcs:set WordPress.Security.ValidatedSanitizedInput customSanitizingFunctions[] ph_clean | |
| 3 | +// ph_clean() recursively sanitizes text; presence, shape and unslashing checks remain separate. | |
| 4 | + | |
| 2 | 5 | /** |
| 3 | 6 | * PropertyHive License Settings |
| 4 | 7 | * |
| 5 | 8 | * @author PropertyHive |
| @@ -16,8 +19,9 @@ | ||
| 16 | 19 | |
| 17 | 20 | /** |
| 18 | 21 | * PH_Settings_Licenses. |
| 19 | 22 | */ |
| 23 | +// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- Legacy public global class PH_Settings_Licenses; preserving the existing PH_* class name is required for plugin and extension compatibility. | |
| 20 | 24 | class PH_Settings_Licenses extends PH_Settings_Page { |
| 21 | 25 | |
| 22 | 26 | /** |
| 23 | 27 | * Constructor. |
| @@ -23,9 +27,9 @@ | ||
| 23 | 27 | * Constructor. |
| 24 | 28 | */ |
| 25 | 29 | public function __construct() { |
| 26 | 30 | $this->id = 'licensekey'; |
| 27 | - $this->label = __( 'License Key', 'propertyhive' ); | |
| 31 | + $this->label = __( 'License', 'propertyhive' ); | |
| 28 | 32 | |
| 29 | 33 | add_filter( 'propertyhive_settings_tabs_array', array( $this, 'add_settings_page' ), 20 ); |
| 30 | 34 | add_action( 'propertyhive_settings_' . $this->id, array( $this, 'output' ) ); |
| 31 | 35 | add_action( 'propertyhive_settings_save_' . $this->id, array( $this, 'save' ) ); |
| @@ -31,20 +35,8 @@ | ||
| 31 | 35 | add_action( 'propertyhive_settings_save_' . $this->id, array( $this, 'save' ) ); |
| 32 | 36 | } |
| 33 | 37 | |
| 34 | 38 | /** |
| 35 | - * Get sections. | |
| 36 | - * | |
| 37 | - * @return array | |
| 38 | - */ | |
| 39 | - public function get_sections() { | |
| 40 | - $sections = array( | |
| 41 | - '' => __( 'License Key', 'propertyhive' ), | |
| 42 | - ); | |
| 43 | - return apply_filters( 'propertyhive_get_sections_' . $this->id, $sections ); | |
| 44 | - } | |
| 45 | - | |
| 46 | - /** | |
| 47 | 39 | * Get settings array. |
| 48 | 40 | * |
| 49 | 41 | * @return array |
| 50 | 42 | */ |
| @@ -49,17 +41,34 @@ | ||
| 49 | 41 | * @return array |
| 50 | 42 | */ |
| 51 | 43 | public function get_settings() { |
| 52 | 44 | |
| 45 | + $license_type = PH()->license->get_license_type(); | |
| 46 | + if ( $license_type == '' ) | |
| 47 | + { | |
| 48 | + $license_type = 'pro'; | |
| 49 | + } | |
| 50 | + | |
| 51 | + // get old license information | |
| 53 | 52 | $license = PH()->license->get_current_license(); |
| 54 | 53 | |
| 55 | 54 | $output = ''; |
| 56 | 55 | $input_border_color = ''; |
| 57 | - $renew_link = '<a href=https://wp-property-hive.com/product/12-month-license-key/"" target="_blank">' . __( 'Renew License', 'propertyhive' ) . '</a>'; | |
| 56 | + $renew_link = '<a href="https://wp-property-hive.com/license-key/" target="_blank">' . __( 'Renew License', 'propertyhive' ) . '</a>'; | |
| 58 | 57 | $valid_license = false; |
| 59 | 58 | |
| 60 | - if ( isset($license['active']) && $license['active'] != '1' ) | |
| 59 | + if ( is_array($license) && empty($license) && get_option( 'propertyhive_license_key', '' ) != '' ) | |
| 61 | 60 | { |
| 61 | + $output = '<span style="color:#900">' . __( 'Invalid license key entered. If you\'re seeing this message and the license key is correct please contact Property Hive support.', 'propertyhive' ) . '</span>'; | |
| 62 | + $input_border_color = '#900'; | |
| 63 | + | |
| 64 | + if ( get_option( 'propertyhive_license_key_error', '' ) != '' ) | |
| 65 | + { | |
| 66 | + $output .= ' <span style="color:#900">' . esc_html(get_option( 'propertyhive_license_key_error', '' )) . '</span>'; | |
| 67 | + } | |
| 68 | + } | |
| 69 | + elseif ( isset($license['active']) && $license['active'] != '1' ) | |
| 70 | + { | |
| 62 | 71 | $output = '<span style="color:#900">' . __( 'License inactive.', 'propertyhive' ) . '</span>'; |
| 63 | 72 | $input_border_color = '#900'; |
| 64 | 73 | } |
| 65 | 74 | else |
| @@ -65,27 +74,18 @@ | ||
| 65 | 74 | else |
| 66 | 75 | { |
| 67 | 76 | if ( isset($license['expires_at']) && $license['expires_at'] != '' ) |
| 68 | 77 | { |
| 69 | - if ( strtotime($license['expires_at']) <= time() ) | |
| 78 | + if ( (strtotime($license['expires_at']) + 86400) <= time() ) | |
| 70 | 79 | { |
| 71 | - // Expired | |
| 72 | - $output = '<span style="color:#900">' . __( 'License expired on ' . date("jS F Y", strtotime($license['expires_at'])), 'propertyhive' ) . '. ' . $renew_link . '</span>'; | |
| 80 | + /* translators: %s: License expiry date. */ | |
| 81 | + $output = '<span style="color:#900">' . sprintf( __( 'License expired on %s', 'propertyhive' ), gmdate( 'jS F Y', strtotime( $license['expires_at'] ) ) ) . '. ' . $renew_link . '</span>'; | |
| 73 | 82 | $input_border_color = '#900'; |
| 74 | 83 | } |
| 75 | - elseif ( | |
| 76 | - strtotime($license['expires_at']) > time() && | |
| 77 | - strtotime($license['expires_at']) < (time() + 30 * 24 * 60 * 60) | |
| 78 | - ) | |
| 84 | + else | |
| 79 | 85 | { |
| 80 | - // Expires in less than 30 days | |
| 81 | - $output = '<span style="color:#F90">' . __( 'License expires on ' . date("jS F Y", strtotime($license['expires_at'])), 'propertyhive' ) . '. ' . $renew_link . '</span>'; | |
| 82 | - $input_border_color = '#F90'; | |
| 83 | - } | |
| 84 | - elseif (strtotime($license['expires_at']) > time()) | |
| 85 | - { | |
| 86 | 86 | // Valid |
| 87 | - $output = '<span style="color:#090">' . __( 'License valid. Expires on ' . date("jS F Y", strtotime($license['expires_at'])), 'propertyhive' ) . '.</span>'; | |
| 87 | + $output = '<span style="color:#090">' . __( 'License valid', 'propertyhive') . '.</span>'; | |
| 88 | 88 | $input_border_color = '#090'; |
| 89 | 89 | $valid_license = true; |
| 90 | 90 | } |
| 91 | 91 | } |
| @@ -90,32 +90,141 @@ | ||
| 90 | 90 | } |
| 91 | 91 | } |
| 92 | 92 | } |
| 93 | 93 | |
| 94 | - $settings = apply_filters( 'propertyhive_licenses_settings', array( | |
| 94 | + $legacy_error = get_option( 'propertyhive_license_key_error', '' ); | |
| 95 | + if ( 'old' === $license_type && '' !== $legacy_error ) { | |
| 96 | + $output = '<span style="color:#900">' . esc_html( $legacy_error ) . '</span>'; | |
| 97 | + $input_border_color = '#900'; | |
| 98 | + $valid_license = false; | |
| 99 | + } | |
| 95 | 100 | |
| 101 | + // get new license information | |
| 102 | + $valid_pro_license = false; | |
| 103 | + $pro_input_border_color = ''; | |
| 104 | + $pro_output = ''; | |
| 105 | + $license_key_to_display = ''; | |
| 106 | + if (get_option('propertyhive_pro_license_key', '') != '') | |
| 107 | + { | |
| 108 | + $length = strlen(get_option('propertyhive_pro_license_key', '')); | |
| 109 | + $license_key_to_display = get_option('propertyhive_pro_license_key', ''); | |
| 110 | + if ( $length >= 6 ) | |
| 111 | + { | |
| 112 | + $license_key_to_display = get_option('propertyhive_pro_license_key', '')[0] . | |
| 113 | + get_option('propertyhive_pro_license_key', '')[1] . | |
| 114 | + get_option('propertyhive_pro_license_key', '')[2] . | |
| 115 | + str_repeat('*', $length - 4) . | |
| 116 | + get_option('propertyhive_pro_license_key', '')[$length-3] . | |
| 117 | + get_option('propertyhive_pro_license_key', '')[$length-2] . | |
| 118 | + get_option('propertyhive_pro_license_key', '')[$length-1]; | |
| 119 | + } | |
| 120 | + | |
| 121 | + if ( PH()->license->is_valid_pro_license_key(true) ) | |
| 122 | + { | |
| 123 | + $valid_pro_license = true; | |
| 124 | + // to be used for displaying subscription level in future | |
| 125 | + //$product_id_and_package = PH()->license->get_pro_license_product_id_and_package(); | |
| 126 | + $pro_input_border_color = '#090'; | |
| 127 | + } | |
| 128 | + else | |
| 129 | + { | |
| 130 | + $pro_license = PH()->license->get_current_pro_license(); | |
| 131 | + $pro_input_border_color = '#900'; | |
| 132 | + $pro_output = ( isset($pro_license['error']) ? $pro_license['error'] : '' ); | |
| 133 | + } | |
| 134 | + } | |
| 135 | + | |
| 136 | + | |
| 137 | + $settings = array( | |
| 138 | + | |
| 96 | 139 | array( 'title' => __( 'License Key', 'propertyhive' ), 'type' => 'title', 'id' => 'license_options' ), |
| 97 | 140 | |
| 98 | - array( | |
| 99 | - 'type' => 'html', | |
| 100 | - 'html' => __( '<p>By having a license key you will benefit from priority one-to-one email support, setup assistance, troubleshooting, and updates to purchased add ons containing new functionality and fixes.</p> | |
| 141 | + ); | |
| 101 | 142 | |
| 102 | - <p>Licenses are valid for 12 months from the date of purchase and exist on a per-site-basis. Should a license key not exist, your website will still function as it does now. However support will be limited and potentially slower than usual and you won’t receive updates to any purchased add ons.</p> | |
| 143 | + $settings[] = array( | |
| 144 | + 'title' => __( 'License Type', 'propertyhive' ), | |
| 145 | + 'id' => 'propertyhive_license_type', | |
| 146 | + 'type' => 'radio', | |
| 147 | + 'default' => $license_type, | |
| 148 | + 'options' => array( | |
| 149 | + 'pro' => 'Pro', | |
| 150 | + 'old' => 'Old-Style (license keys purchased before October 2023)' | |
| 151 | + ) | |
| 152 | + ); | |
| 103 | 153 | |
| 104 | - ' . ( (!$valid_license) ? '<br><p><a href="https://wp-property-hive.com/product/12-month-license-key/" class="button button-primary" target="_blank">Purchase License Key</a></p>' : '' ), 'propertyhive' ), | |
| 105 | - ), | |
| 154 | + $settings[] = array( | |
| 155 | + 'type' => 'html', | |
| 156 | + 'id' => 'pro_license_key_info', | |
| 157 | + 'html' => '<p>' . wp_kses_post( __( 'With a Pro license subscription you unlock a wide array of Property Hive functionality. We offer multiple packages to suit your needs. Your Pro subscription details and license key can be found in the <a href="https://wp-property-hive.com/my-account/" target="_blank">My Account</a> section of our website.', 'propertyhive' ) ) . '</p><br><p>' . | |
| 158 | + ( ! $valid_pro_license ? | |
| 159 | + '<a href="https://wp-property-hive.com/pricing/?src=plugin-license-settings" class="button button-primary" target="_blank">' . esc_html__( 'Get PRO', 'propertyhive' ) . '</a>' : | |
| 160 | + '<a href="' . esc_url( admin_url( 'admin.php?page=ph-settings&tab=features' ) ) . '" class="button button-primary">' . esc_html__( 'Activate Features', 'propertyhive' ) . '</a> <a href="https://wp-property-hive.com/my-account/subscriptions/?src=wordpress-license-tab" class="button button" target="_blank">' . esc_html__( 'Manage Subscription', 'propertyhive' ) . '</a>' | |
| 161 | + ) . '</p><input type="hidden" name="pro_license_key_action" value="' . ( $valid_pro_license ? 'deactivate' : 'activate' ) . '">', | |
| 162 | + ); | |
| 106 | 163 | |
| 107 | - array( | |
| 164 | + if ( $valid_pro_license ) | |
| 165 | + { | |
| 166 | + $settings[] = array( | |
| 167 | + 'type' => 'html', | |
| 168 | + 'id' => 'pro_license_key_display', | |
| 169 | + 'title' => __( 'License Key', 'propertyhive' ), | |
| 170 | + 'html' => '<input type="text" disabled="disabled" value="' . esc_attr( $license_key_to_display ) . '" style="min-width:350px; border:1px solid ' . esc_attr( $pro_input_border_color ) . '"> ' . esc_html( $pro_output ) | |
| 171 | + ); | |
| 172 | + | |
| 173 | + $settings[] = array( | |
| 108 | 174 | 'title' => __( 'License Key', 'propertyhive' ), |
| 109 | - 'id' => 'propertyhive_license_key', | |
| 175 | + 'id' => 'propertyhive_pro_license_key', | |
| 176 | + 'type' => 'hidden' | |
| 177 | + ); | |
| 178 | + } | |
| 179 | + else | |
| 180 | + { | |
| 181 | + $settings[] = array( | |
| 182 | + 'title' => __( 'License Key', 'propertyhive' ), | |
| 183 | + 'id' => 'propertyhive_pro_license_key', | |
| 110 | 184 | 'type' => 'text', |
| 111 | - 'css' => 'min-width:300px; border:1px solid ' . $input_border_color, | |
| 112 | - 'desc' => $output, | |
| 185 | + 'css' => 'min-width:350px; border:1px solid ' . $pro_input_border_color, | |
| 186 | + 'desc' => $pro_output | |
| 187 | + ); | |
| 188 | + } | |
| 189 | + | |
| 190 | + $legacy_license_message = ''; | |
| 191 | + | |
| 192 | + if ( 'old' === $license_type ) | |
| 193 | + { | |
| 194 | + $legacy_license_message = '<p>' . sprintf( | |
| 195 | + /* translators: %s: URL to the support page */ | |
| 196 | + __( 'To switch your existing license key over to the new pricing model, please <a href="%s">get in touch</a>.', 'propertyhive' ), | |
| 197 | + esc_url( 'https://wp-property-hive.com/support' ) | |
| 198 | + ) . '</p>'; | |
| 199 | + } | |
| 200 | + | |
| 201 | + $settings[] = array( | |
| 202 | + 'type' => 'html', | |
| 203 | + 'id' => 'license_key_info', | |
| 204 | + 'html' => sprintf( | |
| 205 | + /* translators: 1: URL to the pricing page, 2: optional message shown to legacy license holders */ | |
| 206 | + __( | |
| 207 | + '<p>If you purchased a license key prior to 16th October 2023, you can enter it here to benefit from updates to any existing add-ons you\'ve purchased.</p> | |
| 208 | + <p>We\'ve since moved to a new and improved <a href="%1$s" target="_blank">pro pricing model</a> that is more cost effective for you and gives access to more features.</p> | |
| 209 | + %2$s | |
| 210 | + <br><p><a href="%1$s" class="button button-primary" target="_blank">Get PRO</a></p>', | |
| 211 | + 'propertyhive' | |
| 212 | + ), | |
| 213 | + esc_url( 'https://wp-property-hive.com/pricing/?src=plugin-license-settings' ), | |
| 214 | + $legacy_license_message | |
| 113 | 215 | ), |
| 216 | + ); | |
| 114 | 217 | |
| 115 | - array( 'type' => 'sectionend', 'id' => 'license_options' ), | |
| 218 | + $settings[] = array( | |
| 219 | + 'title' => __( 'License Key', 'propertyhive' ), | |
| 220 | + 'id' => 'propertyhive_license_key', | |
| 221 | + 'type' => 'text', | |
| 222 | + 'css' => 'min-width:350px; border:1px solid ' . $input_border_color, | |
| 223 | + 'desc' => $output, | |
| 224 | + ); | |
| 116 | 225 | |
| 117 | - ) ); | |
| 226 | + $settings[] = array( 'type' => 'sectionend', 'id' => 'license_options' ); | |
| 118 | 227 | |
| 119 | 228 | return apply_filters( 'propertyhive_get_settings_' . $this->id, $settings ); |
| 120 | 229 | } |
| 121 | 230 | |
| @@ -122,11 +231,12 @@ | ||
| 122 | 231 | /** |
| 123 | 232 | * Output the settings. |
| 124 | 233 | */ |
| 125 | 234 | public function output() { |
| 126 | - global $current_section; | |
| 235 | + global $current_section, $hide_save_button; | |
| 127 | 236 | |
| 128 | - $settings = $this->get_settings(); | |
| 237 | + $settings = $this->get_settings(); | |
| 238 | + | |
| 129 | 239 | PH_Admin_Settings::output_fields( $settings ); |
| 130 | 240 | } |
| 131 | 241 | |
| 132 | 242 | /** |
| @@ -132,13 +242,56 @@ | ||
| 132 | 242 | /** |
| 133 | 243 | * Save settings. |
| 134 | 244 | */ |
| 135 | 245 | public function save() { |
| 136 | - PH_Admin_Settings::save_fields( $this->get_settings() ); | |
| 246 | + if ( ! current_user_can( 'manage_options' ) || ! isset( $_REQUEST['_wpnonce'] ) || ! is_string( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'propertyhive-settings' ) ) { | |
| 247 | + return; | |
| 248 | + } | |
| 137 | 249 | |
| 138 | - PH()->license->ph_check_licenses(true); | |
| 250 | + | |
| 251 | + $license_type = isset( $_POST['propertyhive_license_type'] ) && is_string( $_POST['propertyhive_license_type'] ) ? sanitize_text_field( wp_unslash( $_POST['propertyhive_license_type'] ) ) : ''; | |
| 252 | + $license_key = isset( $_POST['propertyhive_pro_license_key'] ) && is_string( $_POST['propertyhive_pro_license_key'] ) ? ph_clean( wp_unslash( $_POST['propertyhive_pro_license_key'] ) ) : ''; | |
| 253 | + $license_action = isset( $_POST['pro_license_key_action'] ) && is_string( $_POST['pro_license_key_action'] ) ? sanitize_text_field( wp_unslash( $_POST['pro_license_key_action'] ) ) : ''; | |
| 254 | + | |
| 255 | + | |
| 256 | + $settings = $this->get_settings(); | |
| 257 | + | |
| 258 | + PH_Admin_Settings::save_fields( $settings ); | |
| 259 | + | |
| 260 | + update_option( 'missing_invalid_expired_license_key_notice_dismissed', '' ); | |
| 261 | + | |
| 262 | + if ( 'pro' === $license_type && ! empty( $license_key ) && 'activate' === $license_action ) | |
| 263 | + { | |
| 264 | + $return = PH()->license->activate_pro_license_key(); | |
| 265 | + if ( $return['success'] === false ) | |
| 266 | + { | |
| 267 | + PH_Admin_Settings::add_error($return['error']); | |
| 268 | + } | |
| 269 | + else | |
| 270 | + { | |
| 271 | + PH_Admin_Settings::add_message('License key activated successfully'); | |
| 272 | + } | |
| 273 | + } | |
| 274 | + | |
| 275 | + if ( 'pro' === $license_type && ! empty( $license_key ) && 'deactivate' === $license_action ) | |
| 276 | + { | |
| 277 | + $return = PH()->license->deactivate_pro_license_key(); | |
| 278 | + if ( $return['success'] === false ) | |
| 279 | + { | |
| 280 | + PH_Admin_Settings::add_error($return['error']); | |
| 281 | + } | |
| 282 | + else | |
| 283 | + { | |
| 284 | + PH_Admin_Settings::add_message('License key deactivated successfully'); | |
| 285 | + } | |
| 286 | + } | |
| 287 | + | |
| 288 | + if ( 'old' === $license_type ) | |
| 289 | + { | |
| 290 | + PH()->license->ph_check_licenses(true); | |
| 291 | + } | |
| 139 | 292 | } |
| 140 | 293 | } |
| 141 | 294 | |
| 142 | 295 | endif; |
| 143 | 296 | |
| 144 | -return new PH_Settings_Licenses(); | |
| 297 | +return new PH_Settings_Licenses(); | |