| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Settings class for licenses |
| 5 |
* |
| 6 |
* @since 1.9 |
| 7 |
*/ |
| 8 |
class PLL_Settings_Licenses extends PLL_Settings_Module { |
| 9 |
protected $items; |
| 10 |
|
| 11 |
/** |
| 12 |
* Constructor |
| 13 |
* |
| 14 |
* @since 1.9 |
| 15 |
* |
| 16 |
* @param object $polylang polylang object |
| 17 |
*/ |
| 18 |
public function __construct( &$polylang ) { |
| 19 |
parent::__construct( $polylang, array( |
| 20 |
'module' => 'licenses', |
| 21 |
'title' => __( 'License keys', 'polylang' ), |
| 22 |
'description' => __( 'Manage licenses for Polylang Pro or addons.', 'polylang' ), |
| 23 |
) ); |
| 24 |
|
| 25 |
$this->buttons['cancel'] = sprintf( '<button type="button" class="button button-secondary cancel">%s</button>', __( 'Close' ) ); |
| 26 |
|
| 27 |
$this->items = apply_filters( 'pll_settings_licenses', array() ); |
| 28 |
|
| 29 |
add_action( 'wp_ajax_pll_deactivate_license', array( $this, 'deactivate_license' ) ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Tells if the module is active |
| 34 |
* |
| 35 |
* @since 1.9 |
| 36 |
* |
| 37 |
* @return bool |
| 38 |
*/ |
| 39 |
public function is_active() { |
| 40 |
return ! empty( $this->items ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Displays the settings form |
| 45 |
* |
| 46 |
* @since 1.9 |
| 47 |
*/ |
| 48 |
protected function form() { |
| 49 |
if ( ! empty( $this->items ) ) { ?> |
| 50 |
<table id="pll-licenses-table" class="form-table"><?php |
| 51 |
foreach ( $this->items as $item ) { |
| 52 |
echo $this->get_row( $item ); |
| 53 |
} ?> |
| 54 |
</table><?php |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Get the html for a row (one per license key) for display |
| 60 |
* |
| 61 |
* @since 1.9 |
| 62 |
* |
| 63 |
* @param array $item licence id, name and key |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
protected function get_row( $item ) { |
| 67 |
if ( ! empty( $item->license_data ) ) { |
| 68 |
$license = $item->license_data; |
| 69 |
} |
| 70 |
|
| 71 |
$class = 'license-null'; |
| 72 |
|
| 73 |
$out = sprintf( |
| 74 |
'<td><label for="pll-licenses[%1$s]">%2$s</label></td>' . |
| 75 |
'<td><input name="licenses[%1$s]" id="pll-licenses[%1$s]" type="text" value="%3$s" class="regular-text code" />', |
| 76 |
esc_attr( $item->id ), esc_attr( $item->name ), esc_html( $item->license_key ) |
| 77 |
); |
| 78 |
|
| 79 |
if ( ! empty( $license ) && is_object( $license ) ) { |
| 80 |
$now = current_time( 'timestamp' ); |
| 81 |
$expiration = strtotime( $license->expires, $now ); |
| 82 |
|
| 83 |
// Special case: the license expired after the last check |
| 84 |
if ( $license->success && $expiration < $now ) { |
| 85 |
$license->success = false; |
| 86 |
$license->error = 'expired'; |
| 87 |
} |
| 88 |
|
| 89 |
if ( false === $license->success ) { |
| 90 |
$class = 'notice-error notice-alt'; |
| 91 |
|
| 92 |
switch ( $license->error ) { |
| 93 |
case 'expired' : |
| 94 |
$message = sprintf( |
| 95 |
/* translators: %1$s is a date, %2$s and %3$s are html tags */ |
| 96 |
__( 'Your license key expired on %1$s. Please %2$srenew your license key%3$s.', 'polylang' ), |
| 97 |
date_i18n( get_option( 'date_format' ), strtotime( $license->expires, current_time( 'timestamp' ) ) ), |
| 98 |
sprintf( '<a href="%s" target="_blank">', 'https://polylang.pro/checkout/?edd_license_key=' . $item->license_key ), |
| 99 |
'</a>' |
| 100 |
); |
| 101 |
break; |
| 102 |
|
| 103 |
case 'missing' : |
| 104 |
$message = sprintf( |
| 105 |
/* translators: %s are html tags */ |
| 106 |
__( 'Invalid license. Please %svisit your account page%s and verify it.', 'polylang' ), |
| 107 |
sprintf( '<a href="%s" target="_blank">', 'https://polylang.pro/account' ), |
| 108 |
'</a>' |
| 109 |
); |
| 110 |
break; |
| 111 |
|
| 112 |
case 'invalid' : |
| 113 |
case 'site_inactive' : |
| 114 |
$message = sprintf( |
| 115 |
/* translators: %1$s is a product name, %2$s and %3$s are html tags */ |
| 116 |
__( 'Your %1$s license key is not active for this URL. Please %2$svisit your account page%3$s to manage your license key URLs.', 'polylang' ), |
| 117 |
$item->name, |
| 118 |
sprintf( '<a href="%s" target="_blank">', 'https://polylang.pro/account' ), |
| 119 |
'</a>' |
| 120 |
); |
| 121 |
break; |
| 122 |
|
| 123 |
case 'item_name_mismatch' : |
| 124 |
/* translators: %s is a product name */ |
| 125 |
$message = sprintf( __( 'This is not a %s license key.', 'polylang' ), $item->name ); |
| 126 |
break; |
| 127 |
|
| 128 |
case 'no_activations_left': |
| 129 |
/* translators: %s are html tags */ |
| 130 |
$message = sprintf( |
| 131 |
__( 'Your license key has reached its activation limit. %sView possible upgrades%s now.', 'polylang' ), |
| 132 |
sprintf( '<a href="%s" target="_blank">', 'https://polylang.pro/account' ), |
| 133 |
'</a>' |
| 134 |
); |
| 135 |
break; |
| 136 |
} |
| 137 |
} else { |
| 138 |
$class = 'license-valid'; |
| 139 |
|
| 140 |
$out .= sprintf( '<button id="deactivate_%s" type="button" class="button button-secondary pll-deactivate-license">%s</button>', $item->id, __( 'Deactivate', 'polylang' ) ); |
| 141 |
|
| 142 |
if ( 'lifetime' === $license->expires ) { |
| 143 |
$message = __( 'The license key never expires.', 'polylang' ); |
| 144 |
} elseif ( $expiration > $now && $expiration - $now < ( DAY_IN_SECONDS * 30 ) ) { |
| 145 |
$class = 'notice-warning notice-alt'; |
| 146 |
$message = sprintf( |
| 147 |
/* translators: %1$s is a date, %2$s and %3$s are html tags */ |
| 148 |
__( 'Your license key expires soon! It expires on %1$s. %2$sRenew your license key%3$s.', 'polylang' ), |
| 149 |
date_i18n( get_option( 'date_format' ), strtotime( $license->expires, $now ) ), |
| 150 |
sprintf( '<a href="%s" target="_blank">', 'https://polylang.pro/checkout/?edd_license_key=' . $item->license_key ), |
| 151 |
'</a>' |
| 152 |
); |
| 153 |
} else { |
| 154 |
$message = sprintf( |
| 155 |
/* translators: %s is a date */ |
| 156 |
__( 'Your license key expires on %s.', 'polylang' ), |
| 157 |
date_i18n( get_option( 'date_format' ), strtotime( $license->expires, $now ) ) |
| 158 |
); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
if ( ! empty( $message ) ) { |
| 164 |
$out .= '<p>' . $message . '</p>'; |
| 165 |
} |
| 166 |
|
| 167 |
return sprintf( '<tr id="pll-license-%s" class="%s">%s</tr>', $item->id, $class, $out ); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Ajax method to save the license keys and activate the licenses at the same time |
| 172 |
* Overrides parent's method |
| 173 |
* |
| 174 |
* @since 1.9 |
| 175 |
*/ |
| 176 |
public function save_options() { |
| 177 |
check_ajax_referer( 'pll_options', '_pll_nonce' ); |
| 178 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 179 |
wp_die( -1 ); |
| 180 |
} |
| 181 |
|
| 182 |
if ( $this->module === $_POST['module'] && ! empty( $_POST['licenses'] ) ) { |
| 183 |
$x = new WP_Ajax_Response(); |
| 184 |
foreach ( $this->items as $item ) { |
| 185 |
$updated_item = $item->activate_license( sanitize_text_field( $_POST['licenses'][ $item->id ] ) ); |
| 186 |
$x->Add( array( 'what' => 'license-update', 'data' => $item->id, 'supplemental' => array( 'html' => $this->get_row( $updated_item ) ) ) ); |
| 187 |
} |
| 188 |
|
| 189 |
// Updated message |
| 190 |
add_settings_error( 'general', 'settings_updated', __( 'Settings saved.' ), 'updated' ); |
| 191 |
ob_start(); |
| 192 |
settings_errors(); |
| 193 |
$x->Add( array( 'what' => 'success', 'data' => ob_get_clean() ) ); |
| 194 |
$x->send(); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Ajax method to deactivate a license |
| 200 |
* |
| 201 |
* @since 1.9 |
| 202 |
*/ |
| 203 |
public function deactivate_license() { |
| 204 |
check_ajax_referer( 'pll_options', '_pll_nonce' ); |
| 205 |
|
| 206 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 207 |
wp_die( -1 ); |
| 208 |
} |
| 209 |
|
| 210 |
$id = sanitize_text_field( substr( $_POST['id'], 11 ) ); |
| 211 |
wp_send_json( array( |
| 212 |
'id' => $id, |
| 213 |
'html' => $this->get_row( $this->items[ $id ]->deactivate_license() ), |
| 214 |
) ); |
| 215 |
} |
| 216 |
} |
| 217 |
|