PluginProbe
Sharing Image / 3.0
Sharing Image v3.0
3.10 trunk 2.0 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.16 2.0.17 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0 3.1 3.2 3.3 All 29 releases
sharing-image / classes / class-premium.php

class-premium.php in Sharing Image 3.0, at classes/class-premium.php

290 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 * Handle premium tab on settings page and provide static methods.
4 *
5 * @package sharing-image
6 * @author Anton Lukin
7 */
8
9 namespace Sharing_Image;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 die;
13 }
14
15 /**
16 * Premium class.
17 *
18 * @class Premium
19 */
20 class Premium {
21 /**
22 * Sharing Image license options name.
23 *
24 * @var string
25 */
26 const OPTION_LICENSE = 'sharing_image_license';
27
28 /**
29 * Remote licenses API url.
30 *
31 * @var string
32 */
33 const REMOTE_LICENSES = 'https://wpset.org/sharing-image/verify/';
34
35 /**
36 * Premium verification event name.
37 *
38 * @var string
39 */
40 const EVENT_PREMIUM = 'sharing_image_event_premium';
41
42 /**
43 * Init class actions and filters.
44 */
45 public static function init() {
46 add_action( 'admin_init', array( __CLASS__, 'handle_requests' ) );
47 add_action( self::EVENT_PREMIUM, array( __CLASS__, 'launch_verification_event' ), 10, 1 );
48 add_action( 'load-settings_page_' . Settings::SETTINGS_SLUG, array( __CLASS__, 'check_verification_event' ) );
49 }
50
51 /**
52 * Handle premium requests from admin-side.
53 */
54 public static function handle_requests() {
55 add_action( 'wp_ajax_sharing_image_verify_premium', array( __CLASS__, 'verify_premium_key' ) );
56 add_action( 'wp_ajax_sharing_image_revoke_premium', array( __CLASS__, 'revoke_premium_access' ) );
57 }
58
59 /**
60 * Verify Premium key from AJAX request.
61 */
62 public static function verify_premium_key() {
63 $check = check_ajax_referer( Settings::SETTINGS_NONCE, 'sharing_image_nonce', false );
64
65 if ( false === $check ) {
66 wp_send_json_error( __( 'Invalid security token. Please reload the page and try again.', 'sharing-image' ), 403 );
67 }
68
69 if ( empty( $_POST['sharing_image_key'] ) ) {
70 wp_send_json_error( __( 'Premium key is undefined.', 'sharing-image' ), 400 );
71 }
72
73 $key = sanitize_text_field( wp_unslash( $_POST['sharing_image_key'] ) );
74
75 $args = array(
76 'body' => array(
77 'key' => $key,
78 'domain' => wp_parse_url( site_url(), PHP_URL_HOST ),
79 ),
80 );
81
82 $response = wp_remote_post( self::REMOTE_LICENSES, $args );
83
84 if ( is_wp_error( $response ) ) {
85 wp_send_json_error( __( 'Remote request error: ', 'sharing-image' ) . $response->get_error_message(), 400 );
86 }
87
88 $answer = json_decode( $response['body'], true );
89
90 if ( ! isset( $answer['success'] ) ) {
91 wp_send_json_error( __( 'Invalid response received from the verification server.', 'sharing-image' ), 400 );
92 }
93
94 wp_unschedule_hook( self::EVENT_PREMIUM );
95
96 if ( true === $answer['success'] ) {
97 $license = self::update_license( true, $key );
98
99 self::schedule_verification();
100
101 wp_send_json_success( $license );
102 }
103
104 $error = array(
105 'success' => false,
106 'data' => __( 'Verification unsuccessful.', 'sharing-image' ),
107 );
108
109 if ( isset( $answer['result'] ) ) {
110 $error['code'] = $answer['result'];
111 }
112
113 self::update_license( false, $key );
114
115 wp_send_json( $error, 403 );
116 }
117
118 /**
119 * Revoke Premium access from AJAX request.
120 */
121 public static function revoke_premium_access() {
122 $check = check_ajax_referer( Settings::SETTINGS_NONCE, 'sharing_image_nonce', false );
123
124 if ( false === $check ) {
125 wp_send_json_error( __( 'Invalid security token. Please reload the page and try again.', 'sharing-image' ), 403 );
126 }
127
128 // Remove license verification event.
129 wp_unschedule_hook( self::EVENT_PREMIUM );
130
131 // Disable Premium license.
132 $license = self::update_license( false );
133
134 wp_send_json_success( $license );
135 }
136
137
138 /**
139 * Get plugin license settings.
140 *
141 * @return array List of plugin license settings.
142 */
143 public static function get_license() {
144 $license = get_option( self::OPTION_LICENSE, array() );
145
146 /**
147 * Check if the plugin in development mode.
148 *
149 * @param bool Current development state. Disabled by default.
150 */
151 $develop = apply_filters( 'sharing_image_develop', false );
152
153 if ( $develop ) {
154 $license['develop'] = true;
155 }
156
157 /**
158 * Filters license settings.
159 *
160 * @param array List of plugin license settings.
161 */
162 return apply_filters( 'sharing_image_get_license', $license );
163 }
164
165 /**
166 * Set license options.
167 *
168 * @param bool $premium Premium status.
169 * @param string $key License key.
170 * @param string $error Verification error code.
171
172 * @return array License options.
173 */
174 public static function update_license( $premium, $key = '', $error = '' ) {
175 $license = get_option( self::OPTION_LICENSE, array() );
176
177 if ( ! empty( $key ) ) {
178 $license['key'] = $key;
179 }
180
181 unset( $license['error'] );
182
183 if ( ! empty( $error ) ) {
184 $license['error'] = $error;
185 }
186
187 $license['premium'] = $premium;
188
189 // Save updated license settings in database.
190 update_option( self::OPTION_LICENSE, $license );
191
192 return $license;
193 }
194
195 /**
196 * Launch scheduled license verification event.
197 * Do not disable Premium if the verification server does not respond.
198 *
199 * @param string|null $key Optional. Legacy license key.
200 */
201 public static function launch_verification_event( $key = null ) {
202 if ( ! is_null( $key ) ) {
203 self::reschedule_verification();
204 }
205
206 $license = get_option( self::OPTION_LICENSE, array() );
207
208 if ( empty( $license['key'] ) ) {
209 return self::update_license( false, '' );
210 }
211
212 $args = array(
213 'body' => array(
214 'key' => $license['key'],
215 'domain' => wp_parse_url( site_url(), PHP_URL_HOST ),
216 ),
217 );
218
219 $response = wp_remote_post( self::REMOTE_LICENSES, $args );
220
221 if ( is_wp_error( $response ) ) {
222 return;
223 }
224
225 $answer = json_decode( $response['body'], true );
226
227 if ( ! isset( $answer['success'] ) ) {
228 return;
229 }
230
231 if ( true === $answer['success'] ) {
232 return self::update_license( true, $license['key'] );
233 }
234
235 if ( ! isset( $answer['result'] ) ) {
236 return self::update_license( false, $license['key'] );
237 }
238
239 self::update_license( false, $license['key'], $answer['result'] );
240 }
241
242 /**
243 * Check if Premium features availible.
244 *
245 * @return bool Whether premium features are enabled.
246 */
247 public static function is_premium_features() {
248 $license = self::get_license();
249
250 if ( ! empty( $license['premium'] ) || ! empty( $license['develop'] ) ) {
251 return true;
252 }
253
254 return false;
255 }
256
257 /**
258 * Reschedule license verification with current Premium key.
259 */
260 public static function check_verification_event() {
261 $license = self::get_license();
262
263 if ( empty( $license['premium'] ) ) {
264 return;
265 }
266
267 self::schedule_verification();
268 }
269
270 /**
271 * Schedule license verification.
272 */
273 public static function schedule_verification() {
274 if ( wp_next_scheduled( self::EVENT_PREMIUM ) ) {
275 return;
276 }
277
278 wp_schedule_event( time() + DAY_IN_SECONDS / 2, 'twicedaily', self::EVENT_PREMIUM );
279 }
280
281 /**
282 * Delete all events with the same name and schedule again.
283 */
284 public static function reschedule_verification() {
285 wp_unschedule_hook( self::EVENT_PREMIUM );
286
287 self::schedule_verification();
288 }
289 }
290