| 1 |
<?php |
| 2 |
/** |
| 3 |
* Form builder: License check |
| 4 |
* |
| 5 |
* Ensures a license key has been entered, and is valid, before allowing payment |
| 6 |
* forms to be created or edited. If the license key is expired there is a two week |
| 7 |
* grace period before preventing edits. |
| 8 |
* |
| 9 |
* @package SimplePay |
| 10 |
* @subpackage Core |
| 11 |
* @copyright Copyright (c) 2022, Sandhills Development, LLC |
| 12 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 13 |
* @since 4.4.6 |
| 14 |
*/ |
| 15 |
|
| 16 |
namespace SimplePay\Core\Admin\FormBuilder; |
| 17 |
|
| 18 |
use SimplePay\Core\EventManagement\SubscriberInterface; |
| 19 |
use SimplePay\Core\License\LicenseAwareInterface; |
| 20 |
use SimplePay\Core\License\LicenseAwareTrait; |
| 21 |
use SimplePay\Core\Settings; |
| 22 |
|
| 23 |
/** |
| 24 |
* LicenseCheck class. |
| 25 |
* |
| 26 |
* @since 4.4.6 |
| 27 |
*/ |
| 28 |
class LicenseCheck implements SubscriberInterface, LicenseAwareInterface { |
| 29 |
|
| 30 |
use LicenseAwareTrait; |
| 31 |
|
| 32 |
/** |
| 33 |
* {@inheritdoc} |
| 34 |
*/ |
| 35 |
public function get_subscribed_events() { |
| 36 |
if ( |
| 37 |
true === $this->license->is_lite() || |
| 38 |
true === $this->license->is_valid() |
| 39 |
) { |
| 40 |
return array(); |
| 41 |
} |
| 42 |
|
| 43 |
return array( |
| 44 |
'add_meta_boxes' => 'maybe_remove_meta_boxes', |
| 45 |
'edit_form_top' => 'maybe_show_license_notice', |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Shows a license notice if the license is missing, invalid, or expired. |
| 51 |
* |
| 52 |
* @since 4.4.6 |
| 53 |
* |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public function maybe_show_license_notice() { |
| 57 |
$adding = ( |
| 58 |
isset( $_GET['post_type'] ) && |
| 59 |
'simple-pay' === sanitize_text_field( $_GET['post_type'] ) |
| 60 |
); |
| 61 |
|
| 62 |
$post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0; |
| 63 |
$editing = 'simple-pay' === get_post_type( $post_id ); |
| 64 |
|
| 65 |
// Not adding or editing. |
| 66 |
if ( ! ( $adding || $editing ) ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
$is_missing = empty( $this->license->get_key() ); |
| 71 |
$installed = get_option( 'simpay_installed', time() ); |
| 72 |
|
| 73 |
// If the license key has not been entered, give a 24 hour grace period. |
| 74 |
if ( $is_missing && ( time() - $installed < ( HOUR_IN_SECONDS * 24 ) ) ) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
$license_url = Settings\get_url( |
| 79 |
array( |
| 80 |
'section' => 'general', |
| 81 |
'subsection' => 'license', |
| 82 |
) |
| 83 |
); |
| 84 |
|
| 85 |
$renew_url = add_query_arg( |
| 86 |
array( |
| 87 |
'edd_license_key' => $this->license->get_key(), |
| 88 |
'discount' => 'SAVE50', |
| 89 |
), |
| 90 |
sprintf( '%s/checkout', untrailingslashit( SIMPLE_PAY_STORE_URL ) ) |
| 91 |
); |
| 92 |
|
| 93 |
$renew_url = simpay_ga_url( $renew_url, 'payment-form-license-renewal' ); |
| 94 |
|
| 95 |
$learn_more_url = simpay_ga_url( |
| 96 |
'https://wpsimplepay.com/lite-vs-pro/', |
| 97 |
'payment-form-license-renewal' |
| 98 |
); |
| 99 |
|
| 100 |
// License is missing. |
| 101 |
if ( $is_missing ) { |
| 102 |
include_once SIMPLE_PAY_DIR . '/views/admin-payment-forms-license-missing.php'; |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
switch ( $this->license->get_status() ) { |
| 107 |
// License is expired... |
| 108 |
case 'expired': |
| 109 |
// ...and not in the grace period. |
| 110 |
if ( ! $this->license->is_in_grace_period() ) { |
| 111 |
$action = isset( $_GET['action'] ) && 'edit' === $_GET['action'] |
| 112 |
? _x( 'Editing', 'payment form action', 'stripe' ) |
| 113 |
: _x( 'Creation', 'payment form action', 'stripe' ); |
| 114 |
|
| 115 |
include_once SIMPLE_PAY_DIR . '/views/admin-payment-forms-license-expired.php'; |
| 116 |
return; |
| 117 |
} |
| 118 |
break; |
| 119 |
|
| 120 |
// License is invalid for some other reason, treat it as missing. |
| 121 |
case 'invalid': |
| 122 |
include_once SIMPLE_PAY_DIR . '/views/admin-payment-forms-license-missing.php'; |
| 123 |
return; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Removes all metaboxes from the page if a license is missing or expired. |
| 129 |
* |
| 130 |
* @since 4.4.6 |
| 131 |
* |
| 132 |
* @return void |
| 133 |
*/ |
| 134 |
public function maybe_remove_meta_boxes() { |
| 135 |
$remove = false; |
| 136 |
|
| 137 |
switch ( $this->license->get_status() ) { |
| 138 |
case 'expired': |
| 139 |
if ( ! $this->license->is_in_grace_period() ) { |
| 140 |
$remove = true; |
| 141 |
} |
| 142 |
|
| 143 |
break; |
| 144 |
case 'invalid': |
| 145 |
$remove = true; |
| 146 |
break; |
| 147 |
} |
| 148 |
|
| 149 |
$is_missing = empty( $this->license->get_key() ); |
| 150 |
$installed = get_option( 'simpay_installed', time() ); |
| 151 |
|
| 152 |
// If the license key has not been entered, give a 24 hour grace period. |
| 153 |
if ( $is_missing && ! ( time() - $installed < ( HOUR_IN_SECONDS * 24 ) ) ) { |
| 154 |
$remove = true; |
| 155 |
} |
| 156 |
|
| 157 |
if ( ! $remove ) { |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
global $wp_meta_boxes; |
| 162 |
|
| 163 |
foreach ( $wp_meta_boxes['simple-pay'] as $context_id => $contexts ) { |
| 164 |
foreach ( $contexts as $priorities ) { |
| 165 |
foreach ( $priorities as $metabox ) { |
| 166 |
if ( ! $metabox ) { |
| 167 |
continue; |
| 168 |
} |
| 169 |
|
| 170 |
remove_meta_box( $metabox['id'], 'simple-pay', $context_id ); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
} |
| 177 |
|