| 1 |
<?php |
| 2 |
/** |
| 3 |
* License: Notification subscriber |
| 4 |
* |
| 5 |
* @package SimplePay |
| 6 |
* @subpackage Core |
| 7 |
* @copyright Copyright (c) 2022, Sandhills Development, LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 4.4.5 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace SimplePay\Core\License; |
| 13 |
|
| 14 |
use SimplePay\Core\EventManagement\SubscriberInterface; |
| 15 |
use SimplePay\Core\NotificationInbox\NotificationAwareInterface; |
| 16 |
use SimplePay\Core\NotificationInbox\NotificationAwareTrait; |
| 17 |
use SimplePay\Core\NotificationInbox\NotificationRepository; |
| 18 |
use SimplePay\Core\Settings; |
| 19 |
|
| 20 |
/** |
| 21 |
* LicenseNotificationSubscriber class. |
| 22 |
* |
| 23 |
* @since 4.4.5 |
| 24 |
*/ |
| 25 |
class LicenseNotificationSubscriber implements SubscriberInterface, LicenseAwareInterface, NotificationAwareInterface { |
| 26 |
|
| 27 |
use LicenseAwareTrait; |
| 28 |
use NotificationAwareTrait; |
| 29 |
|
| 30 |
/** |
| 31 |
* {@inheritdoc} |
| 32 |
*/ |
| 33 |
public function get_subscribed_events() { |
| 34 |
if ( true === $this->license->is_lite() ) { |
| 35 |
return array(); |
| 36 |
} |
| 37 |
|
| 38 |
// Alert via Notification Inbox if availble. Global admin notice is shown as well. |
| 39 |
if ( ! $this->notifications instanceof NotificationRepository ) { |
| 40 |
return array(); |
| 41 |
} |
| 42 |
|
| 43 |
return array( |
| 44 |
'init' => |
| 45 |
array( |
| 46 |
array( 'add_missing_license_notification', 10 ), |
| 47 |
array( 'add_expired_license_notification', 10 ), |
| 48 |
), |
| 49 |
'pre_update_option_simpay_license_data' => |
| 50 |
'dismiss_license_notifications', |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Adds a notification to the inbox if a license is missing. |
| 56 |
* |
| 57 |
* @since 4.4.5 |
| 58 |
* |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
public function add_missing_license_notification() { |
| 62 |
// License key is not empty. |
| 63 |
if ( ! empty( $this->license->get_key() ) ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
$this->notifications->restore( |
| 68 |
array( |
| 69 |
'type' => 'error', |
| 70 |
'source' => 'internal', |
| 71 |
'title' => __( |
| 72 |
'WP Simple Pay Pro is Not Fully Activated!', |
| 73 |
'stripe' |
| 74 |
), |
| 75 |
'slug' => 'missing-license', |
| 76 |
'content' => esc_html__( |
| 77 |
'Add your WP Simple Pay Pro license key to start creating payment forms, enable automatic updates, and complete activation.', |
| 78 |
'stripe' |
| 79 |
), |
| 80 |
'actions' => array( |
| 81 |
array( |
| 82 |
'type' => 'primary', |
| 83 |
'text' => __( 'Complete Activation', 'stripe' ), |
| 84 |
'url' => Settings\get_url( |
| 85 |
array( |
| 86 |
'section' => 'general', |
| 87 |
'subsection' => 'license', |
| 88 |
) |
| 89 |
), |
| 90 |
), |
| 91 |
array( |
| 92 |
'type' => 'secondary', |
| 93 |
'text' => __( 'Learn More', 'stripe' ), |
| 94 |
'url' => 'https://wpsimplepay.com/doc/activate-wp-simple-pay-pro-license/', |
| 95 |
), |
| 96 |
), |
| 97 |
'conditions' => array(), |
| 98 |
'start' => gmdate( 'Y-m-d H:i:s', time() ), |
| 99 |
'end' => gmdate( 'Y-m-d H:i:s', time() + YEAR_IN_SECONDS ), |
| 100 |
'is_dismissible' => false, |
| 101 |
), |
| 102 |
function() { |
| 103 |
$this->notifications->dismiss( 'expired-license' ); |
| 104 |
$this->notifications->dismiss( 'expiring-license' ); |
| 105 |
} |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Adds a notification to the inbox if a license is expired. |
| 111 |
* |
| 112 |
* @since 4.4.6 |
| 113 |
* |
| 114 |
* @return void |
| 115 |
*/ |
| 116 |
public function add_expired_license_notification() { |
| 117 |
// License key is empty, another notification is already showing. |
| 118 |
if ( empty( $this->license->get_key() ) ) { |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
// License key is not expired, another notification is already showing. |
| 123 |
if ( 'expired' !== $this->license->get_status() ) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
/** @var string $expiration */ |
| 128 |
$expiration = $this->license->get_expiration(); |
| 129 |
|
| 130 |
if ( $this->license->is_in_grace_period() ) { |
| 131 |
/** @var string $date_format */ |
| 132 |
$date_format = get_option( 'date_format', 'Y-m-d' ); |
| 133 |
|
| 134 |
$content = sprintf( |
| 135 |
/* translators: License extension date. */ |
| 136 |
__( |
| 137 |
'We have extended WP Simple Pay Pro functionality until %s, at which point functionality will become limited. Renew your license to continue receiving automatic updates, technical support, and access to WP Simple Pay Pro features and functionality.', |
| 138 |
'stripe' |
| 139 |
), |
| 140 |
gmdate( |
| 141 |
$date_format, |
| 142 |
strtotime( $expiration ) + ( DAY_IN_SECONDS * 14 ) |
| 143 |
) |
| 144 |
); |
| 145 |
} else { |
| 146 |
$content = __( |
| 147 |
'Renew your license to continue receiving automatic updates, technical support, and access to WP Simple Pay Pro features and functionality.', |
| 148 |
'stripe' |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
$renew_url = add_query_arg( |
| 153 |
array( |
| 154 |
'edd_license_key' => $this->license->get_key(), |
| 155 |
'discount' => 'SAVE50', |
| 156 |
), |
| 157 |
sprintf( '%s/checkout', untrailingslashit( SIMPLE_PAY_STORE_URL ) ) |
| 158 |
); |
| 159 |
|
| 160 |
$renew_url = simpay_ga_url( $renew_url, 'notification-inbox' ); |
| 161 |
|
| 162 |
$this->notifications->restore( |
| 163 |
array( |
| 164 |
'type' => 'error', |
| 165 |
'source' => 'internal', |
| 166 |
'title' => __( |
| 167 |
'[IMPORTANT] Your WP Simple Pay Pro License Has Expired!', |
| 168 |
'stripe' |
| 169 |
), |
| 170 |
'slug' => 'expired-license', |
| 171 |
'content' => $content, |
| 172 |
'actions' => array( |
| 173 |
array( |
| 174 |
'type' => 'primary', |
| 175 |
'text' => __( 'Renew License for 50% Off!', 'stripe' ), |
| 176 |
'url' => $renew_url, |
| 177 |
), |
| 178 |
array( |
| 179 |
'type' => 'secondary', |
| 180 |
'text' => __( 'Learn More', 'stripe' ), |
| 181 |
'url' => simpay_docs_link( |
| 182 |
'', |
| 183 |
'what-happens-if-my-license-expires', |
| 184 |
'admin-notice-expired-license', |
| 185 |
true |
| 186 |
), |
| 187 |
), |
| 188 |
), |
| 189 |
'conditions' => array(), |
| 190 |
'start' => gmdate( 'Y-m-d H:i:s', time() ), |
| 191 |
'end' => gmdate( 'Y-m-d H:i:s', time() + YEAR_IN_SECONDS ), |
| 192 |
'is_dismissible' => false, |
| 193 |
), |
| 194 |
function() { |
| 195 |
$this->notifications->dismiss( 'missing-license' ); |
| 196 |
$this->notifications->dismiss( 'expiring-license' ); |
| 197 |
} |
| 198 |
); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Dismisses all license notifications when the license data is updated. |
| 203 |
* Allows individual notifications to restore themselves on the next page load. |
| 204 |
* |
| 205 |
* @since 4.4.6 |
| 206 |
* |
| 207 |
* @param \stdClass $value New license data. |
| 208 |
* @return \stdClass License data. |
| 209 |
*/ |
| 210 |
public function dismiss_license_notifications( $value ) { |
| 211 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 212 |
return $value; |
| 213 |
} |
| 214 |
|
| 215 |
$this->notifications->dismiss( 'missing-license' ); |
| 216 |
$this->notifications->dismiss( 'expiring-license' ); |
| 217 |
$this->notifications->dismiss( 'expired-license' ); |
| 218 |
|
| 219 |
return $value; |
| 220 |
} |
| 221 |
|
| 222 |
} |
| 223 |
|