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