| 1 |
<?php |
| 2 |
/** |
| 3 |
* License: Setting |
| 4 |
* |
| 5 |
* Handles the UI for activating and deactivating a license key and the side effects of that. |
| 6 |
* |
| 7 |
* @package SimplePay |
| 8 |
* @subpackage Core |
| 9 |
* @copyright Copyright (c) 2022, Sandhills Development, LLC |
| 10 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 11 |
* @since 4.4.5 |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace SimplePay\Core\License; |
| 15 |
|
| 16 |
use SimplePay\Core\EventManagement\SubscriberInterface; |
| 17 |
use SimplePay\Core\Settings; |
| 18 |
|
| 19 |
/** |
| 20 |
* LicenseSettingSubscriber class. |
| 21 |
* |
| 22 |
* @since 4.4.5 |
| 23 |
*/ |
| 24 |
class LicenseSettingSubscriber implements SubscriberInterface, LicenseAwareInterface { |
| 25 |
|
| 26 |
use LicenseAwareTrait; |
| 27 |
|
| 28 |
/** |
| 29 |
* {@inheritdoc} |
| 30 |
*/ |
| 31 |
public function get_subscribed_events() { |
| 32 |
return array( |
| 33 |
'simpay_register_settings_subsections' => 'register_settings_subsection', |
| 34 |
'simpay_register_settings' => 'register_setting', |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Registers settings subsections. |
| 40 |
* |
| 41 |
* @since 4.4.5 |
| 42 |
* |
| 43 |
* @param \SimplePay\Core\Settings\Subsection_Collection $subsections Subsections collection. |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
function register_settings_subsection( $subsections ) { |
| 47 |
// License/License. |
| 48 |
$subsections->add( |
| 49 |
new Settings\Subsection( |
| 50 |
array( |
| 51 |
'id' => 'license', |
| 52 |
'section' => 'general', |
| 53 |
'label' => esc_html_x( |
| 54 |
'License', |
| 55 |
'settings subsection label', |
| 56 |
'stripe' |
| 57 |
), |
| 58 |
'priority' => 1, |
| 59 |
) |
| 60 |
) |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Registers the settings. |
| 66 |
* |
| 67 |
* @since 4.4.5 |
| 68 |
* |
| 69 |
* @param \SimplePay\Core\Settings\Setting_Collection $settings Settings collection. |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
function register_setting( $settings ) { |
| 73 |
$settings->add( |
| 74 |
new Settings\Setting( |
| 75 |
array( |
| 76 |
'id' => 'license', |
| 77 |
'section' => 'general', |
| 78 |
'subsection' => 'license', |
| 79 |
'label' => esc_html__( 'License Key', 'stripe' ), |
| 80 |
'output' => function() { |
| 81 |
// Disable submit button. |
| 82 |
add_filter( |
| 83 |
'simpay_admin_page_settings_general_submit', |
| 84 |
'__return_false' |
| 85 |
); |
| 86 |
|
| 87 |
ob_start(); |
| 88 |
|
| 89 |
if ( true === $this->license->is_lite() ) { |
| 90 |
$this->get_lite_setting_ui(); |
| 91 |
} else { |
| 92 |
$this->get_pro_setting_ui(); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Allows additional output after the license field. |
| 97 |
* |
| 98 |
* @since 4.4.0 |
| 99 |
*/ |
| 100 |
do_action( '__unstable_simpay_license_field' ); |
| 101 |
|
| 102 |
return ob_get_clean(); |
| 103 |
}, |
| 104 |
) |
| 105 |
) |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Returns the UI for the Pro license setting. |
| 111 |
* |
| 112 |
* @since 4.5.2 |
| 113 |
* |
| 114 |
* @return string|bool |
| 115 |
*/ |
| 116 |
private function get_pro_setting_ui() { |
| 117 |
// Disable submit button. |
| 118 |
add_filter( 'simpay_admin_page_settings_general_submit', '__return_false' ); |
| 119 |
|
| 120 |
$settings_url = Settings\get_url( |
| 121 |
array( |
| 122 |
'section' => 'general', |
| 123 |
'subsection' => 'license', |
| 124 |
) |
| 125 |
); |
| 126 |
|
| 127 |
$has_config = defined( 'SIMPLE_PAY_LICENSE_KEY' ); |
| 128 |
$license = $this->license; |
| 129 |
$feedback = $this->get_license_feedback(); |
| 130 |
$nonce = wp_create_nonce( 'simpay-manage-license' ); |
| 131 |
$refresh_url = add_query_arg( |
| 132 |
array( |
| 133 |
'simpay-action' => 'simpay-activate-license', |
| 134 |
'simpay-license-nonce' => $nonce, |
| 135 |
'simpay-license-key' => $license->get_key(), |
| 136 |
'simpay-license-refresh' => true, |
| 137 |
) |
| 138 |
); |
| 139 |
|
| 140 |
$is_upgraded = isset( $_GET['is_upgraded'] ); |
| 141 |
|
| 142 |
// @todo use a ViewLoader |
| 143 |
include_once SIMPLE_PAY_DIR . '/views/admin-setting-license.php'; // @phpstan-ignore-line |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Returns the UI for the Lite license setting. |
| 148 |
* |
| 149 |
* @since 4.5.2 |
| 150 |
* |
| 151 |
* @return string|bool |
| 152 |
*/ |
| 153 |
private function get_lite_setting_ui() { |
| 154 |
$nonce = wp_create_nonce( 'simpay-connect-url' ); |
| 155 |
|
| 156 |
// @todo use a ViewLoader |
| 157 |
include_once SIMPLE_PAY_DIR . '/views/admin-setting-lite-license.php'; // @phpstan-ignore-line |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Get license feedback, based on a specific status. |
| 162 |
* |
| 163 |
* @since 4.4.5 |
| 164 |
* |
| 165 |
* @return string |
| 166 |
*/ |
| 167 |
private function get_license_feedback() { |
| 168 |
if ( empty( $this->license->get_key() ) ) { |
| 169 |
return ''; |
| 170 |
} |
| 171 |
|
| 172 |
switch ( $this->license->get_status() ) { |
| 173 |
case 'valid': |
| 174 |
return wp_kses( |
| 175 |
sprintf( |
| 176 |
/* translators: %1$s License level. */ |
| 177 |
__( 'Your license level is %1$s.', 'stripe' ), |
| 178 |
'<strong>' . ucfirst( $this->license->get_level() ) . '</strong>' |
| 179 |
), |
| 180 |
array( |
| 181 |
'strong' => array(), |
| 182 |
) |
| 183 |
); |
| 184 |
case 'expired': |
| 185 |
/** @var string $date_format */ |
| 186 |
$date_format = get_option( 'date_format' ); |
| 187 |
|
| 188 |
/** @var string $expiration */ |
| 189 |
$expiration = $this->license->get_expiration(); |
| 190 |
/** @var int $expiration */ |
| 191 |
$expiration = strtotime( $expiration ); |
| 192 |
|
| 193 |
return sprintf( |
| 194 |
/* translators: License expiration date. */ |
| 195 |
__( 'Your license key expired on %1$s', 'stripe' ), |
| 196 |
date( $date_format, $expiration ) |
| 197 |
); |
| 198 |
case 'inactive': |
| 199 |
case 'deactivated': |
| 200 |
case 'site_inactive': |
| 201 |
case 'empty': |
| 202 |
case 'disabled': |
| 203 |
case 'revoked': |
| 204 |
case 'invalid': |
| 205 |
default: |
| 206 |
return esc_html__( 'Please enter a valid license key.', 'stripe' ); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
} |
| 211 |
|