PluginProbe
Polylang / 2.3.6
Polylang v2.3.6
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.3.6, at settings/settings-licenses.php

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