| 1 |
<?php |
| 2 |
/** |
| 3 |
* Charitable Licenses Settings UI. |
| 4 |
* |
| 5 |
* @package Charitable/Classes/Charitable_Licenses_Settings |
| 6 |
* @version 1.0.0 |
| 7 |
* @author David Bisset |
| 8 |
* @copyright Copyright (c) 2022, WP Charitable LLC |
| 9 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Licenses_Settings' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Licenses_Settings |
| 21 |
* |
| 22 |
* @final |
| 23 |
* @since 1.0.0 |
| 24 |
*/ |
| 25 |
final class Charitable_Licenses_Settings { |
| 26 |
|
| 27 |
/** |
| 28 |
* The single instance of this class. |
| 29 |
* |
| 30 |
* @var Charitable_Licenses_Settings|null |
| 31 |
*/ |
| 32 |
private static $instance = null; |
| 33 |
|
| 34 |
/** |
| 35 |
* Create object instance. |
| 36 |
* |
| 37 |
* @since 1.0.0 |
| 38 |
*/ |
| 39 |
private function __construct() { |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns and/or create the single instance of this class. |
| 44 |
* |
| 45 |
* @since 1.2.0 |
| 46 |
* |
| 47 |
* @return Charitable_Licenses_Settings |
| 48 |
*/ |
| 49 |
public static function get_instance() { |
| 50 |
if ( is_null( self::$instance ) ) { |
| 51 |
self::$instance = new self(); |
| 52 |
} |
| 53 |
|
| 54 |
return self::$instance; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Optionally add the licenses tab. |
| 59 |
* |
| 60 |
* @since 1.4.7 |
| 61 |
* |
| 62 |
* @param string[] $tabs Settings tabs. |
| 63 |
* @return string[] |
| 64 |
*/ |
| 65 |
public function maybe_add_licenses_tab( $tabs ) { |
| 66 |
|
| 67 |
$products = charitable_get_helper( 'licenses' )->get_products(); |
| 68 |
|
| 69 |
if ( empty( $products ) ) { |
| 70 |
return $tabs; |
| 71 |
} |
| 72 |
|
| 73 |
$show_licenses_tab = apply_filters( 'charitable_show_old_license_tab', false ); |
| 74 |
|
| 75 |
if ( $show_licenses_tab ) : |
| 76 |
|
| 77 |
$tabs = charitable_add_settings_tab( |
| 78 |
$tabs, |
| 79 |
'licenses', |
| 80 |
__( 'Licenses', 'charitable' ), |
| 81 |
array( |
| 82 |
'index' => 4, |
| 83 |
) |
| 84 |
); |
| 85 |
|
| 86 |
endif; |
| 87 |
|
| 88 |
return $tabs; |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Add the licenses tab settings fields. |
| 94 |
* |
| 95 |
* @since 1.0.0 |
| 96 |
* |
| 97 |
* @return array |
| 98 |
*/ |
| 99 |
public function add_licenses_fields() { |
| 100 |
if ( ! charitable_is_settings_view( 'licenses' ) ) { |
| 101 |
return array(); |
| 102 |
} |
| 103 |
|
| 104 |
$fields = array( |
| 105 |
'section' => array( |
| 106 |
'title' => '', |
| 107 |
'type' => 'hidden', |
| 108 |
'priority' => 10000, |
| 109 |
'value' => 'licenses', |
| 110 |
'save' => false, |
| 111 |
), |
| 112 |
'licenses' => array( |
| 113 |
'title' => false, |
| 114 |
'callback' => array( $this, 'render_licenses_table' ), |
| 115 |
'priority' => 4, |
| 116 |
), |
| 117 |
); |
| 118 |
|
| 119 |
foreach ( charitable_get_helper( 'licenses' )->get_products() as $key => $product ) { |
| 120 |
$fields[ $key ] = array( |
| 121 |
'type' => 'text', |
| 122 |
'render' => false, |
| 123 |
'priority' => 6, |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
return $fields; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Add the licenses group. |
| 132 |
* |
| 133 |
* @since 1.0.0 |
| 134 |
* |
| 135 |
* @param string[] $groups Settings groups. |
| 136 |
* @return string[] |
| 137 |
*/ |
| 138 |
public function add_licenses_group( $groups ) { |
| 139 |
$groups['licenses'] = array(); |
| 140 |
return $groups; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Render the licenses table. |
| 145 |
* |
| 146 |
* @since 1.0.0 |
| 147 |
* |
| 148 |
* @return void |
| 149 |
*/ |
| 150 |
public function render_licenses_table() { |
| 151 |
charitable_admin_view( 'settings/licenses' ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Add an extra button to the Licenses tab to re-check licenses. |
| 156 |
* |
| 157 |
* @since 1.6.0 |
| 158 |
* |
| 159 |
* @param string $button The button HTML. |
| 160 |
* @return string |
| 161 |
*/ |
| 162 |
public function add_license_recheck_button( $button ) { |
| 163 |
$licenses = array_filter( charitable_get_helper( 'licenses' )->get_licenses(), 'is_array' ); |
| 164 |
|
| 165 |
if ( empty( $licenses ) ) { |
| 166 |
return $button; |
| 167 |
} |
| 168 |
|
| 169 |
$slug = Charitable_Addons_Directory::get_current_plan_slug(); |
| 170 |
|
| 171 |
if ( $slug === false || strtolower( $slug ) === 'lite' ) { |
| 172 |
// there is no valid updated license so allow this button to output. |
| 173 |
|
| 174 |
$html = '<input style="margin-left:8px;height:29px;" type="submit" class="button button-secondary" name="recheck" value="' . esc_attr__( 'Save & Re-check All Licenses', 'charitable' ) . '" /></p>'; |
| 175 |
|
| 176 |
return str_replace( |
| 177 |
'</p>', |
| 178 |
$html, |
| 179 |
$button |
| 180 |
); |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
return $button; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Checks for updated license and invalidates status field if not set. |
| 189 |
* |
| 190 |
* @since 1.0.0 |
| 191 |
* Updated 1.7.0.4 |
| 192 |
* |
| 193 |
* @param mixed[] $values The parsed values combining old values & new values. |
| 194 |
* @param mixed[] $new_values The newly submitted values. |
| 195 |
* @return mixed[] |
| 196 |
*/ |
| 197 |
public function save_license( $values, $new_values ) { |
| 198 |
|
| 199 |
/* If we didn't just submit licenses, stop here. */ |
| 200 |
if ( ! isset( $new_values['licenses_legacy'] ) ) { |
| 201 |
return $values; |
| 202 |
} |
| 203 |
|
| 204 |
$re_check = array_key_exists( 'recheck', $_POST ); |
| 205 |
$licenses = $new_values['licenses_legacy']; |
| 206 |
|
| 207 |
// Remember that legacy licenses are passed into values differently in this hook. $values[licenses_legacy] |
| 208 |
foreach ( $licenses as $product_key => $license ) { |
| 209 |
$license = trim( $license ); |
| 210 |
|
| 211 |
if ( empty( $license ) ) { |
| 212 |
$values['licenses_legacy'][ $product_key ] = ''; |
| 213 |
continue; |
| 214 |
} |
| 215 |
|
| 216 |
$license_data = charitable_get_helper( 'licenses' )->verify_license( $product_key, $license, $re_check, true ); // the true added to make sure we let the server know we are attempting to vertify a legacy license. |
| 217 |
|
| 218 |
// updated info should look like: Array ( [license] => 123123123 [expiration_date] => 2023-08-10 23:59:59 [plan_id] => 1 [valid] => 1 [license_limit] => 1 [comm_success] => 1 ) |
| 219 |
|
| 220 |
if ( empty( $license_data ) ) { |
| 221 |
continue; |
| 222 |
} |
| 223 |
|
| 224 |
$values['licenses'][ $product_key ] = $license_data; // this is ok because this follows previous versions of where the licenses would be going. |
| 225 |
} |
| 226 |
|
| 227 |
return $values; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Outputs the "new" license HTML for the general settings tab. |
| 232 |
* |
| 233 |
* @since 1.7.0.4 |
| 234 |
* |
| 235 |
* @param string Allows us to control HTML based on valid license already in the system. |
| 236 |
* @return string |
| 237 |
*/ |
| 238 |
public function generate_license_check_html( $has_valid_license = 'false' ) { |
| 239 |
|
| 240 |
$slug = Charitable_Addons_Directory::get_current_plan_slug(); |
| 241 |
$is_legacy = Charitable_Addons_Directory::is_current_plan_legacy(); |
| 242 |
$readonly = false; |
| 243 |
$show_license_form = true; |
| 244 |
|
| 245 |
if ( $slug === false || strtolower( $slug ) === 'lite' ) { |
| 246 |
$has_valid_license = false; |
| 247 |
} |
| 248 |
|
| 249 |
$output = '<div id="charitable-license-message" class="license-message license-valid-' . $has_valid_license . '">'; |
| 250 |
if ( $has_valid_license && ! $is_legacy ) { |
| 251 |
$output .= $this->get_licensed_message(); |
| 252 |
$readonly = 'readonly value="xxxxxxxxxxxxxxxxxxxx"'; |
| 253 |
} else { |
| 254 |
// Getting the current plan slug might be only applicate for newer licenses, so we need to account for "legacy" licenses to see if the install is licensed or not. |
| 255 |
if ( charitable_is_pro() && $is_legacy ) { |
| 256 |
$output .= $this->get_legacy_licensed_message(); |
| 257 |
$show_license_form = false; |
| 258 |
} else { |
| 259 |
$output .= $this->get_unlicensed_message(); |
| 260 |
} |
| 261 |
} |
| 262 |
if ( $show_license_form ) : |
| 263 |
$output .= '<p>'; |
| 264 |
$output .= '<input type="password" autocomplete="off" name="license-key" id="charitable-settings-upgrade-license-key" ' . $readonly . ' placeholder="' . esc_attr__( 'Paste license key here', 'charitable' ) . '" value="" />'; |
| 265 |
if ( ! $has_valid_license ) { |
| 266 |
$output .= '<button data-action="verify" type="button" class="charitable-btn charitable-btn-md charitable-btn-orange charitable-btn-activate" id="charitable-settings-connect-btn">' . esc_html__( 'Verify Key', 'charitable' ) . '</button>'; |
| 267 |
} |
| 268 |
if ( $has_valid_license ) { |
| 269 |
$output .= '<button data-action="deactivate" type="button" class="charitable-btn charitable-btn-md charitable-btn-orange charitable-btn-deactivate" id="charitable-settings-connect-btn">' . esc_html__( 'Deactivate Key', 'charitable' ) . '</button>'; |
| 270 |
} |
| 271 |
$output .= '</p>'; |
| 272 |
endif; |
| 273 |
$output . '</div>'; |
| 274 |
|
| 275 |
return $output; |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Outputs a message for unlicnensed users for the general settings tab. |
| 281 |
* |
| 282 |
* @since 1.7.0.4 |
| 283 |
* |
| 284 |
* @param boolean $valid Valid license. |
| 285 |
* @param array $license_data Available license information. |
| 286 |
* @return string |
| 287 |
*/ |
| 288 |
public function get_unlicensed_message( $valid = false, $license_data = false ) { |
| 289 |
|
| 290 |
$output = '<p>' . esc_html__( 'You\'re using ', 'charitable' ); |
| 291 |
$output .= '<strong>Charitable Lite</strong>'; |
| 292 |
$output .= esc_html__( ' - no license needed. Enjoy!', 'charitable' ) . ' 🙂</p>'; |
| 293 |
$output .= |
| 294 |
'<p>' . |
| 295 |
sprintf( |
| 296 |
wp_kses( |
| 297 |
/* translators: %s - charitable.com upgrade URL. */ |
| 298 |
__( 'To unlock more features consider <strong><a href="%s" target="_blank" rel="noopener noreferrer" class="charitable-upgrade-modal">upgrading to PRO</a></strong>.', 'charitable' ), |
| 299 |
[ |
| 300 |
'a' => [ |
| 301 |
'href' => [], |
| 302 |
'class' => [], |
| 303 |
'target' => [], |
| 304 |
'rel' => [], |
| 305 |
], |
| 306 |
'strong' => [], |
| 307 |
] |
| 308 |
), |
| 309 |
esc_url( charitable_pro_upgrade_url( 'settings-upgrade' ) ) |
| 310 |
) . |
| 311 |
'</p>'; |
| 312 |
$output .= |
| 313 |
'<p class="discount-note">' . |
| 314 |
wp_kses( |
| 315 |
__( 'As a valued charitable Lite user, you receive up to <strong>$200 off</strong>, automatically applied at checkout!', 'charitable' ), |
| 316 |
[ |
| 317 |
'strong' => [], |
| 318 |
'br' => [], |
| 319 |
] |
| 320 |
) . |
| 321 |
'</p>'; |
| 322 |
|
| 323 |
if ( $valid && false === $error ) { |
| 324 |
$output .= '<p>' . esc_html__( 'Already registered? You might have an expired or invalid license. Reach out to us for support.', 'charitable' ) . '</p>'; |
| 325 |
} else if ( ! $valid && isset( $license_data['license_limit'] ) && false !== $license_data['license_limit'] ) { |
| 326 |
$output .= '<p>' . esc_html__( 'There was an error attempting to validate your license key. Check and see if you have exceeded your license activations.', 'charitable' ) . '</p>'; |
| 327 |
} else if ( ! $valid && isset( $license_data['comm_success'] ) && false !== $license_data['comm_success'] ) { |
| 328 |
$output .= '<p>' . esc_html__( 'There was an error attempting to contact the license server. Please try again later.', 'charitable' ) . '</p>'; |
| 329 |
} else if ( isset( $_GET['valid'] ) && 'invalid' === esc_html( $_GET['valid'] ) && isset( $_GET['comm_success'] ) && 0 === intval( $_GET['comm_success'] ) ) { |
| 330 |
$output .= '<p style="color:red;">' . esc_html__( 'There was an error attempting to validate your license key. Please try again later.', 'charitable' ) . '</p>'; |
| 331 |
} else if ( isset( $_GET['valid'] ) && 'invalid' === esc_html( $_GET['valid'] ) && isset( $_GET['license_limit'] ) && false !== $_GET['license_limit'] ) { |
| 332 |
$output .= '<p style="color:red;">' . esc_html__( 'There was an error attempting to validate your license key. Check and see if you have exceeded your license activations.', 'charitable' ) . '</p>'; |
| 333 |
} else if ( isset( $_GET['valid'] ) && 'invalid' === esc_html( $_GET['valid'] ) ) { |
| 334 |
$output .= '<p style="color:red;" data-invalid="Unknown">' . esc_html__( 'There was a problem attempting to validate your license key. Please try again later.', 'charitable' ) . '</p>'; |
| 335 |
} else { |
| 336 |
$output .= '<hr><p>' . esc_html__( 'Already purchased? Simply enter your license key below to enable Charitable PRO!', 'charitable' ) . '</p>'; |
| 337 |
} |
| 338 |
|
| 339 |
return $output; |
| 340 |
|
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Outputs a message for licnensed users for the general settings tab. |
| 345 |
* |
| 346 |
* @since 1.7.0.4 |
| 347 |
* |
| 348 |
* @param boolean $force_valid ???? |
| 349 |
* @return string |
| 350 |
*/ |
| 351 |
public function get_licensed_message( $force_valid = true ) { |
| 352 |
|
| 353 |
$settings = get_option( 'charitable_settings' ); |
| 354 |
$price_id = intval( $settings['licenses']['charitable-v2'][ 'plan_id' ] ); |
| 355 |
$license_expires = $settings['licenses']['charitable-v2'][ 'expiration_date' ]; |
| 356 |
$valid = $settings['licenses']['charitable-v2'][ 'valid' ]; |
| 357 |
$output = ''; |
| 358 |
|
| 359 |
if ( $valid ) { |
| 360 |
switch ( $price_id ) { |
| 361 |
case 1: |
| 362 |
$plan_name = 'Basic'; |
| 363 |
break; |
| 364 |
case 2: |
| 365 |
$plan_name = 'Plus'; |
| 366 |
break; |
| 367 |
case 3: |
| 368 |
$plan_name = 'Pro'; |
| 369 |
break; |
| 370 |
case 4: |
| 371 |
$plan_name = 'Agency'; |
| 372 |
break; |
| 373 |
default: |
| 374 |
$plan_name = 'Lite'; |
| 375 |
break; |
| 376 |
} |
| 377 |
$output = '<p>' . esc_html__( 'You\'re using ', 'charitable' ); |
| 378 |
$output .= '<strong>Charitable ' . $plan_name . '</strong>'; |
| 379 |
$output .= esc_html__( '. Your license expires on ', 'charitable' ); |
| 380 |
$output .= date( 'M d, Y', strtotime( $license_expires ) ); |
| 381 |
$output .= esc_html__( '. Enjoy!', 'charitable' ) . ' 🙂</p>'; |
| 382 |
|
| 383 |
if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) { |
| 384 |
$output .= print_r( $settings['licenses'], true ); |
| 385 |
error_log( 'get_licensed_message' ); |
| 386 |
error_log( print_r( $settings, true ) ); |
| 387 |
error_log( print_r( $plan_name, true ) ); |
| 388 |
} |
| 389 |
} else { |
| 390 |
$plan_name = 'Lite'; |
| 391 |
if ( intval( $price_id ) > 0 ) { |
| 392 |
$valid = esc_html__( 'Your license is not valid or has expired.', 'charitable' ); |
| 393 |
} else { |
| 394 |
$valid = false; |
| 395 |
} |
| 396 |
$output .= $this->get_unlicensed_message( $valid ); |
| 397 |
if ( defined( 'CHARITABLE_DEBUG' ) && CHARITABLE_DEBUG ) { |
| 398 |
error_log( 'get_licensed_message - not valid' ); |
| 399 |
error_log( print_r( $valid, true ) ); |
| 400 |
} |
| 401 |
|
| 402 |
} |
| 403 |
|
| 404 |
return $output; |
| 405 |
|
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
* Outputs a message for unlicnensed users for the general settings tab. |
| 410 |
* |
| 411 |
* @since 1.7.0.4 |
| 412 |
* |
| 413 |
* @param boolean $valid Valid license. |
| 414 |
* @param array $license_data Available license information. |
| 415 |
* @return string |
| 416 |
*/ |
| 417 |
public function get_legacy_licensed_message() { |
| 418 |
|
| 419 |
$output = |
| 420 |
'<p>' . |
| 421 |
sprintf( |
| 422 |
wp_kses( |
| 423 |
/* translators: %s - charitable.com upgrade URL. */ |
| 424 |
__( 'You\'re using <strong>Charitable</strong> with one or more <a href="%s">activated legacy licenses</a>. Enjoy! 🙂', 'charitable' ), |
| 425 |
[ |
| 426 |
'a' => [ |
| 427 |
'href' => [], |
| 428 |
'class' => [], |
| 429 |
'target' => [], |
| 430 |
'rel' => [], |
| 431 |
], |
| 432 |
'br' => [], |
| 433 |
'strong' => [], |
| 434 |
] |
| 435 |
), |
| 436 |
esc_url( admin_url( 'admin.php?page=charitable-settings&tab=advanced' ) ) |
| 437 |
) . |
| 438 |
'</p>'; |
| 439 |
|
| 440 |
// display a potential upsell |
| 441 |
$licenses = array_filter( charitable_get_helper( 'licenses' )->get_licenses(), 'is_array' ); |
| 442 |
// remove the two charitable 'keys' so we can see if anything licensed is left (say for example someone only has recurring donations licensed/activated ) |
| 443 |
if ( isset( $licenses['charitable'] ) ) { |
| 444 |
unset( $licenses['charitable'] ); |
| 445 |
} |
| 446 |
if ( isset( $licenses['charitable-v2'] ) ) { |
| 447 |
unset( $licenses['charitable-v2'] ); |
| 448 |
} |
| 449 |
// todo: perhaps a better/more effective check for this. |
| 450 |
if ( count( $licenses ) > 0 ) { |
| 451 |
$output .= |
| 452 |
'<p>' . |
| 453 |
sprintf( |
| 454 |
wp_kses( |
| 455 |
/* translators: %s - charitable.com upgrade URL. */ |
| 456 |
__( 'To unlock more features consider <strong><a href="%s" target="_blank" rel="noopener noreferrer" class="charitable-upgrade-modal">upgrading to PRO</a></strong>.', 'charitable' ), |
| 457 |
[ |
| 458 |
'a' => [ |
| 459 |
'href' => [], |
| 460 |
'class' => [], |
| 461 |
'target' => [], |
| 462 |
'rel' => [], |
| 463 |
], |
| 464 |
'strong' => [], |
| 465 |
] |
| 466 |
), |
| 467 |
esc_url( charitable_pro_upgrade_url( 'settings-upgrade' ) ) |
| 468 |
) . |
| 469 |
'</p>'; |
| 470 |
$output .= |
| 471 |
'<p class="discount-note">' . |
| 472 |
wp_kses( |
| 473 |
__( 'As a valued Charitable user, you receive up to <strong>$200 off</strong>, automatically applied at checkout!', 'charitable' ), |
| 474 |
[ |
| 475 |
'strong' => [], |
| 476 |
'br' => [], |
| 477 |
] |
| 478 |
) . |
| 479 |
'</p>'; |
| 480 |
|
| 481 |
} |
| 482 |
|
| 483 |
return $output; |
| 484 |
|
| 485 |
} |
| 486 |
|
| 487 |
} |
| 488 |
|
| 489 |
endif; |
| 490 |
|