PluginProbe
Polylang / 2.6.2
Polylang v2.6.2
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / settings / settings-licenses.php

settings-licenses.php in Polylang 2.6.2, at settings/settings-licenses.php

233 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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(
20 $polylang,
21 array(
22 'module' => 'licenses',
23 'title' => __( 'License keys', 'polylang' ),
24 'description' => __( 'Manage licenses for Polylang Pro and add-ons.', 'polylang' ),
25 )
26 );
27
28 $this->buttons['cancel'] = sprintf( '<button type="button" class="button button-secondary cancel">%s</button>', __( 'Close', 'polylang' ) );
29
30 $this->items = apply_filters( 'pll_settings_licenses', array() );
31
32 add_action( 'wp_ajax_pll_deactivate_license', array( $this, 'deactivate_license' ) );
33 }
34
35 /**
36 * Tells if the module is active
37 *
38 * @since 1.9
39 *
40 * @return bool
41 */
42 public function is_active() {
43 return ! empty( $this->items );
44 }
45
46 /**
47 * Displays the settings form
48 *
49 * @since 1.9
50 */
51 protected function form() {
52 if ( ! empty( $this->items ) ) { ?>
53 <table id="pll-licenses-table" class="form-table">
54 <?php
55 foreach ( $this->items as $item ) {
56 echo $this->get_row( $item ); // phpcs:ignore WordPress.Security.EscapeOutput
57 }
58 ?>
59 </table>
60 <?php
61 }
62 }
63
64 /**
65 * Get the html for a row (one per license key) for display
66 *
67 * @since 1.9
68 *
69 * @param array $item licence id, name and key
70 * @return string
71 */
72 protected function get_row( $item ) {
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 will expire soon! Precisely, it will expire on %1$s. %2$sRenew your license key today!%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 );
176 }
177
178 /**
179 * Ajax method to save the license keys and activate the licenses at the same time
180 * Overrides parent's method
181 *
182 * @since 1.9
183 */
184 public function save_options() {
185 check_ajax_referer( 'pll_options', '_pll_nonce' );
186 if ( ! current_user_can( 'manage_options' ) ) {
187 wp_die( -1 );
188 }
189
190 if ( isset( $_POST['module'] ) && $this->module === $_POST['module'] && ! empty( $_POST['licenses'] ) ) {
191 $x = new WP_Ajax_Response();
192 foreach ( $this->items as $item ) {
193 if ( ! empty( $_POST['licenses'][ $item->id ] ) ) {
194 $updated_item = $item->activate_license( sanitize_key( $_POST['licenses'][ $item->id ] ) );
195 $x->Add( array( 'what' => 'license-update', 'data' => $item->id, 'supplemental' => array( 'html' => $this->get_row( $updated_item ) ) ) );
196 }
197 }
198
199 // Updated message
200 add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'polylang' ), 'updated' );
201 ob_start();
202 settings_errors();
203 $x->Add( array( 'what' => 'success', 'data' => ob_get_clean() ) );
204 $x->send();
205 }
206 }
207
208 /**
209 * Ajax method to deactivate a license
210 *
211 * @since 1.9
212 */
213 public function deactivate_license() {
214 check_ajax_referer( 'pll_options', '_pll_nonce' );
215
216 if ( ! current_user_can( 'manage_options' ) ) {
217 wp_die( -1 );
218 }
219
220 if ( ! isset( $_POST['id'] ) ) {
221 wp_die( 0 );
222 }
223
224 $id = substr( sanitize_text_field( wp_unslash( $_POST['id'] ) ), 11 );
225 wp_send_json(
226 array(
227 'id' => $id,
228 'html' => $this->get_row( $this->items[ $id ]->deactivate_license() ),
229 )
230 );
231 }
232 }
233