PluginProbe
Getwid – Gutenberg Blocks / 2.0.7
Getwid – Gutenberg Blocks v2.0.7
3.0.1 3.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.2.0 trunk 1.8.7 1.9.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
getwid / includes / settings-page.php

settings-page.php in Getwid – Gutenberg Blocks 2.0.7, at includes/settings-page.php

477 lines 17.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Getwid;
4
5 class SettingsPage {
6
7 public function __construct()
8 {
9 $this->addActions();
10 }
11
12 protected function addActions()
13 {
14 add_action('admin_menu', [$this, 'registerPage']);
15 add_action('admin_init', [$this, 'registerSettingsGroups']);
16 add_action('admin_init', [$this, 'registerFields']);
17 add_action('admin_init', [$this, 'checkInstagramQueryURL']);
18 }
19
20 public function getSettingsGroups()
21 {
22 return [
23 'general' => __('General', 'getwid'),
24 'appearance' => __('Appearance', 'getwid'),
25 'blocks' => __('Blocks', 'getwid'),
26 'post_templates' => __('Post Templates', 'getwid'),
27 ];
28 }
29
30 public function registerPage()
31 {
32 add_options_page(
33 esc_html_x('Getwid Settings' , 'Settings page title', 'getwid'),
34 esc_html_x('Getwid', 'Settings page title(in menu)', 'getwid'),
35 'manage_options',
36 'getwid',
37 [$this, 'renderPage']
38 );
39 }
40
41 public function registerSettingsGroups()
42 {
43 $settings_groups = $this->getSettingsGroups();
44
45 foreach ($settings_groups as $id => $title){
46 add_settings_section(
47 'getwid_' . $id,
48 '',
49 '',
50 'getwid_' . $id
51 );
52 }
53 }
54
55 public function renderPage()
56 {
57 if ( ! current_user_can( 'manage_options' ) ) {
58 return;
59 }
60
61 $active_tab_id = $this->getActiveTabID();
62 $settings_groups = $this->getSettingsGroups();
63
64 settings_errors('getwid_settings_errors');
65 ?>
66 <div class="wrap">
67 <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
68
69 <h2 class="nav-tab-wrapper">
70 <?php
71 foreach ($settings_groups as $tab_id => $tab_title) :
72 $active_tab_class = $tab_id == $active_tab_id ? 'nav-tab-active' : '';
73 ?>
74 <a href="<?php echo esc_url( $this->getTabUrl($tab_id) ); ?>" class="nav-tab <?php echo esc_attr($active_tab_class); ?>">
75 <?php echo esc_html($tab_title)?>
76 </a>
77 <?php
78 endforeach;
79 ?>
80 </h2>
81 <?php
82 if ( 'post_templates' == $active_tab_id ) :
83
84 $this->renderPostTemplatesTab();
85
86 else :
87 ?>
88 <form action="options.php" method="post">
89 <?php
90 settings_fields( 'getwid_' . $active_tab_id );
91 do_settings_sections( 'getwid_' . $active_tab_id );
92
93 submit_button( esc_html__('Save Changes', 'getwid') );
94 ?>
95 </form>
96 <?php
97 endif;
98 ?>
99 </div>
100 <?php
101 }
102
103 public function getwid_instagram_notice_success() {
104 ?>
105 <div class="notice notice-success">
106 <p><?php esc_html_e( 'Instagram: access token updated.', 'getwid' ); ?></p>
107 </div>
108 <?php
109 }
110
111 public function getwid_instagram_notice_error() {
112 ?>
113 <div class="notice notice-error">
114 <p><?php esc_html_e('Instagram: access denied.', 'getwid'); ?></p>
115 </div>
116 <?php
117 }
118
119 public function checkInstagramQueryURL() {
120 global $pagenow;
121
122 if ( $pagenow == 'options-general.php' && isset( $_GET['instagram-token'] ) && isset( $_GET['nonce'] ) ) {
123
124 if ( wp_verify_nonce( sanitize_key( $_GET['nonce'] ), 'getwid_nonce_save_instagram_token' ) && current_user_can( 'manage_options' ) ) {
125
126 // Update token
127 update_option( 'getwid_instagram_token', sanitize_text_field( wp_unslash( $_GET['instagram-token'] ) ) );
128 // Delete cache data
129 delete_transient( 'getwid_instagram_response_data' );
130 // Schedule token refresh
131 getwid()->instagramTokenManager()->schedule_token_refresh_event();
132
133 $redirect_url = add_query_arg(
134 [
135 'getwid-instagram-success' => true
136 ],
137 $this->getTabUrl('general')
138 );
139 } else {
140 $redirect_url = add_query_arg(
141 [
142 'instagram-error' => true
143 ],
144 $this->getTabUrl('general')
145 );
146 }
147
148 wp_redirect( $redirect_url );
149 }
150
151 if (isset($_GET['getwid-instagram-success'])) {
152 add_action( 'admin_notices', [$this, 'getwid_instagram_notice_success'] );
153 }
154
155 if (isset($_GET['instagram-error'])) {
156 add_action( 'admin_notices', [$this, 'getwid_instagram_notice_error'] );
157 }
158 }
159
160 public function registerFields() {
161
162 /* #region Section Content Width */
163 add_settings_field( 'getwid_section_content_width', __( 'Section Content Width', 'getwid' ),
164 [ $this, 'renderSectionContentWidth' ], 'getwid_appearance', 'getwid_appearance' );
165 register_setting( 'getwid_appearance', 'getwid_section_content_width', [ 'type' => 'number', 'default' => '' ] );
166 /* #endregion */
167
168 /* #region Animation */
169 add_settings_field( 'getwid_animation', __( 'Animation', 'getwid' ),
170 [ $this, 'renderAnimation' ], 'getwid_appearance', 'getwid_appearance' );
171 register_setting( 'getwid_appearance', 'getwid_smooth_animation', [ 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean' ] );
172 /* #endregion */
173
174 /* #region AssetsOptimization */
175 add_settings_field( 'getwid_assets_optimization', __( 'Performance Optimization', 'getwid' ),
176 [ $this, 'renderAssetsOptimization'], 'getwid_general', 'getwid_general' );
177 register_setting( 'getwid_general', 'getwid_load_assets_on_demand', [ 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean' ] );
178
179 register_setting( 'getwid_general', 'getwid_move_css_to_head', [ 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean' ] );
180 /* #endregion */
181
182 /* #region Instagram Access Token */
183 add_settings_field( 'getwid_instagram_token', __( 'Instagram Access Token', 'getwid' ),
184 [ $this, 'renderInstagramToken' ], 'getwid_general', 'getwid_general' );
185 register_setting( 'getwid_general', 'getwid_instagram_token', [ 'type' => 'text', 'default' => '' ] );
186 /* #endregion */
187
188 /* #region Instagram Cache Timeout */
189 add_settings_field( 'getwid_instagram_cache_timeout', __( 'Instagram Cache Timeout', 'getwid' ),
190 [ $this, 'renderInstagramCacheTimeout' ], 'getwid_general', 'getwid_general' );
191 register_setting( 'getwid_general', 'getwid_instagram_cache_timeout', [ 'type' => 'number', 'default' => 30 ] );
192 /* #endregion */
193
194 /* #region Google API Key */
195 add_settings_field( 'getwid_google_api_key', __( 'Google Maps API Key', 'getwid' ),
196 [ $this, 'renderGoogleApiKey' ], 'getwid_general', 'getwid_general' );
197 register_setting( 'getwid_general', 'getwid_google_api_key', [ 'type' => 'text', 'default' => '' ] );
198 /* #endregion */
199
200 /* #region Recaptcha Site Key */
201 add_settings_field( 'getwid_recaptcha_v2_site_key', __( 'Recaptcha Site Key', 'getwid' ),
202 [ $this, 'renderRecaptchaSiteKey' ], 'getwid_general', 'getwid_general' );
203 register_setting( 'getwid_general', 'getwid_recaptcha_v2_site_key', [ 'type' => 'text', 'default' => '' ] );
204 /* #endregion */
205
206 /* #region Recaptcha Secret Key */
207 add_settings_field( 'getwid_recaptcha_v2_secret_key', __( 'Recaptcha Secret Key', 'getwid' ),
208 [ $this, 'renderRecaptchaSecretKey' ], 'getwid_general', 'getwid_general' );
209 register_setting( 'getwid_general', 'getwid_recaptcha_v2_secret_key', [ 'type' => 'text', 'default' => '' ] );
210 /* #endregion */
211
212 /* #region Mailchimp Api Key */
213 add_settings_field( 'getwid_mailchimp_api_key', __( 'Mailchimp API Key', 'getwid' ),
214 [ $this, 'renderMailchimpApiKey' ], 'getwid_general', 'getwid_general' );
215 register_setting( 'getwid_general', 'getwid_mailchimp_api_key', [ 'type' => 'text', 'default' => '' ] );
216 /* #endregion */
217
218 /* #region Disabled Blocks */
219 add_settings_field( 'getwid_disabled_blocks', __( 'Disable Getwid Blocks', 'getwid' ),
220 [ $this, 'renderDisabledBlocks' ], 'getwid_blocks', 'getwid_blocks' );
221
222 $blocks = getwid()->blocksManager()->getBlocks();
223
224 foreach ($blocks as $name => $block) {
225 $option_name = $block->getDisabledOptionKey();
226 register_setting( 'getwid_blocks', $option_name, [ 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean' ] );
227 }
228 /* #endregion */
229 }
230
231 public function renderSectionContentWidth() {
232
233 $field_val = get_option( 'getwid_section_content_width', '' );
234
235 ?>
236 <input type="number" id="getwid_section_content_width" name="getwid_section_content_width" value="<?php echo esc_attr( $field_val ); ?>" />
237 <?php echo esc_html_x( 'px', 'pixels', 'getwid' ); ?>
238 <p class="description"><?php echo esc_html__( 'Default width of the content area in the Section block. Leave empty to use $content_width set in your theme. Set 0 to disable this option and control it via CSS.', 'getwid' ); ?></p>
239 <?php
240 }
241
242 public function renderInstagramToken() {
243
244 $encryption = new StringEncryption();
245 $field_val = $encryption->decrypt( get_option( 'getwid_instagram_token', '' ) );
246
247 $connectURL = add_query_arg(
248 ['nonce' => wp_create_nonce('getwid_nonce_save_instagram_token') ],
249 admin_url( 'options-general.php' )
250 );
251
252 $refreshURL = add_query_arg(
253 ['nonce' => wp_create_nonce('getwid_nonce_save_instagram_token') ],
254 admin_url( 'options-general.php' )
255 );
256
257 ?>
258 <input type="text" id="getwid_instagram_token" name="getwid_instagram_token" class="regular-text" value="<?php echo esc_attr( $field_val ); ?>" /><?php
259
260 if ( ! empty( $field_val ) ) {
261
262 try {
263
264 $profile_data_json = wp_remote_get(
265 'https://graph.instagram.com/me?fields=username&access_token=' . $field_val
266 );
267
268 if ( ( ! is_wp_error( $profile_data_json ) ) && ( 200 === wp_remote_retrieve_response_code( $profile_data_json ) ) ) {
269
270 $profile_data = json_decode( $profile_data_json['body'] );
271
272 if ( json_last_error() === JSON_ERROR_NONE ) {
273
274 if ( isset( $profile_data->username ) ) {
275
276 echo '<p class="description">' . sprintf(
277 //translators: %s is username or user id
278 esc_html__('Account: %s', 'getwid'),
279 esc_html( $profile_data->username )
280 ) . '<p>';
281
282 } elseif ( isset( $profile_data->id ) ) {
283
284 echo '<p class="description">' . sprintf(
285 //translators: %s is username or user id
286 esc_html__('Account: %s', 'getwid'),
287 esc_html( $profile_data->id )
288 ) . '</p>';
289 }
290 }
291 }
292 } catch ( Exception $profile_data_exception ) {
293
294 echo esc_html( $profile_data_exception->getMessage() );
295 }
296 }
297
298 ?>
299 <p><a href="<?php echo esc_url(
300 'https://api.instagram.com/oauth/authorize?client_id=910186402812397&redirect_uri=' .
301 'https://api.getmotopress.com/get_instagram_token.php&scope=user_profile,user_media&response_type=code&state=' .
302 $connectURL ); ?>" class="button button-default"><?php echo esc_html__( 'Connect Instagram Account', 'getwid' );?></a>
303 <?php
304 if ( ! empty( $field_val) ) {
305 ?>
306 <a href="<?php echo esc_url(
307 'https://api.getmotopress.com/refresh_instagram_token.php?access_token=' . $field_val . '&state=' .
308 $refreshURL ); ?>" class="button button-default"><?php echo esc_html__( 'Refresh Access Token', 'getwid' );?></a>
309 <?php
310 }
311 ?>
312 </p>
313 <?php
314 }
315
316 public function renderInstagramCacheTimeout() {
317
318 $field_val = get_option('getwid_instagram_cache_timeout');
319 ?>
320 <input type="number" id="getwid_instagram_cache_timeout" name="getwid_instagram_cache_timeout" value="<?php echo esc_attr( $field_val ); ?>" />
321 <p class="description"><?php echo esc_html__( 'Time until expiration of media data in minutes. Setting to 0 means no expiration.', 'getwid' ); ?></p>
322 <?php
323 }
324
325 public function renderGoogleApiKey() {
326
327 $field_val = get_option('getwid_google_api_key', '');
328 ?>
329 <input type="text" id="getwid_google_api_key" name="getwid_google_api_key" class="regular-text" value="<?php echo esc_attr( $field_val ); ?>" />
330 <?php
331 }
332
333 public function renderRecaptchaSiteKey() {
334
335 $field_val = get_option( 'getwid_recaptcha_v2_site_key', '' );
336 ?>
337 <input type="text" id="getwid_recaptcha_v2_site_key" name="getwid_recaptcha_v2_site_key" class="regular-text" value="<?php echo esc_attr( $field_val ); ?>" />
338 <?php
339 }
340
341 public function renderRecaptchaSecretKey() {
342
343 $field_val = get_option( 'getwid_recaptcha_v2_secret_key', '' );
344 ?>
345 <input type="text" id="getwid_recaptcha_v2_secret_key" name="getwid_recaptcha_v2_secret_key" class="regular-text" value="<?php echo esc_attr( $field_val ); ?>" />
346 <?php
347 }
348
349 public function renderMailchimpApiKey() {
350
351 $field_val = get_option( 'getwid_mailchimp_api_key', '' );
352 ?>
353 <input type="text" id="getwid_mailchimp_api_key" name="getwid_mailchimp_api_key" class="regular-text" value="<?php echo esc_attr( $field_val ); ?>" />
354 <?php
355 }
356
357 public function renderDisabledBlocks() {
358
359 $blocks = getwid()->blocksManager()->getBlocks();
360 $disabledBlocks = getwid()->blocksManager()->getDisabledBlocks();
361 ksort( $blocks );
362 ?>
363 <p class="description">
364 <?php
365 printf(
366 //translators: %1$s, %2$s is a number of total and disabled blocks
367 esc_html__('Total: %1$s, Disabled: %2$s', 'getwid'),
368 esc_html( sizeof($blocks) ),
369 esc_html( sizeof($disabledBlocks) )
370 );
371 ?><br/>
372 <input type="button" id="getwid-disabled-blocks-select-all" class="button button-link" value="<?php esc_html_e('Select All', 'getwid'); ?>" />
373 &nbsp;/&nbsp;
374 <input type="button" id="getwid-disabled-blocks-deselect-all" class="button button-link" value="<?php esc_html_e('Deselect All', 'getwid'); ?>" />
375 </p>
376 <fieldset id="getwid-disabled-blocks">
377 <?php
378 foreach ($blocks as $name => $block) {
379 $option_name = $block->getDisabledOptionKey();
380 ?>
381 <label for="<?php echo esc_attr( $option_name ); ?>">
382 <input type="checkbox" id="<?php echo esc_attr( $option_name ); ?>" name="<?php echo esc_attr( $option_name ); ?>" value="1" <?php
383 checked( '1', $block->isDisabled() ); ?> />
384 <?php echo esc_html( $block->getLabel() ); ?>
385 </label><br/>
386 <?php
387 }
388 ?>
389 </fieldset>
390 <script>
391 jQuery(document).ready(function(){
392 jQuery('#getwid-disabled-blocks-select-all').click(function(){
393 jQuery('#getwid-disabled-blocks input:checkbox').attr('checked','checked');
394 });
395 jQuery('#getwid-disabled-blocks-deselect-all').click(function(){
396 jQuery('#getwid-disabled-blocks input:checkbox').removeAttr('checked');
397 });
398 })
399 </script>
400 <?php
401 }
402
403 public function renderPostTemplatesTab() {
404 ?>
405 <p><?php esc_html_e( 'Post Templates are used for presenting posts in a certain format and style. You can change how a post looks by choosing a post template in the Custom Post Type and related blocks.', 'getwid' ); ?></p>
406 <a class="button button-primary" href="<?php echo esc_url( admin_url('edit.php?post_type=getwid_template_part') ); ?>"><?php esc_html_e( 'Manage Post Templates', 'getwid' ); ?></a>
407 <?php
408 }
409
410 public function renderAnimation() {
411
412 $field_val = get_option( 'getwid_smooth_animation', false );
413 ?>
414 <label for="getwid_smooth_animation">
415 <input type="checkbox" id="getwid_smooth_animation" name="getwid_smooth_animation" value="1" <?php
416 checked( '1', $field_val ); ?> />
417 <?php echo esc_html__('Enable smooth animation of blocks', 'getwid'); ?>
418 </label>
419 <p class="description"><?php
420 echo esc_html__('Hides block until the entrance animation starts. Prevents possible occurrence of horizontal scroll during the animation.', 'getwid');
421 ?></p>
422 <?php
423 }
424
425 public function renderAssetsOptimization() {
426
427 $getwid_load_assets_on_demand = get_option( 'getwid_load_assets_on_demand', false );
428 $getwid_move_css_to_head = get_option( 'getwid_move_css_to_head', false );
429 ?>
430 <fieldset>
431 <label for="getwid_load_assets_on_demand">
432 <input type="checkbox" id="getwid_load_assets_on_demand" name="getwid_load_assets_on_demand" value="1" <?php
433 checked( '1', $getwid_load_assets_on_demand ); ?> />
434 <?php echo esc_html__('Load CSS and JS of blocks on demand', 'getwid') . ' (Recomended)'; ?>
435 </label>
436 <p class="description"><?php
437 echo esc_html__('If this option is on, all CSS and JS files of blocks will be loaded on demand in footer. This will reduce the amount of heavy assets on the page.', 'getwid');
438 ?></p>
439 <br/>
440 <label for="getwid_move_css_to_head">
441 <input type="checkbox" id="getwid_move_css_to_head" name="getwid_move_css_to_head" value="1" <?php
442 checked( '1', $getwid_move_css_to_head ); ?> />
443 <?php echo esc_html__('Aggregate all CSS files of blocks in header', 'getwid') . ' (Recomended)'; ?>
444 </label>
445 <p class="description"><?php
446 echo esc_html__('If this option is on, all CSS files of blocks will be moved to header for better theme compatibility. If your theme has custom styling for Getwid blocks, its styles will be applied first.', 'getwid');
447 ?></p>
448 <br/>
449 <p class="description"><?php
450 echo esc_html__('These settings may break some blocks in posts loaded via Ajax. These settings may not work together with optimization plugins.', 'getwid');
451 ?></p>
452 </fieldset>
453 <script>
454 jQuery(document).ready(function(){
455 // set initial state
456 jQuery('#getwid_move_css_to_head').toggleClass("disabled", ! jQuery('#getwid_load_assets_on_demand').prop("checked") );
457 // bind state change
458 jQuery('#getwid_load_assets_on_demand').change(function(e) {
459 jQuery('#getwid_move_css_to_head').toggleClass("disabled", ! jQuery(this).prop("checked") );
460 });
461 })
462 </script>
463 <?php
464 }
465
466 public function getTabUrl( $tab = 'general' )
467 {
468 return add_query_arg( [ 'page' => 'getwid', 'active_tab' => $tab ], admin_url( 'options-general.php' ) );
469 }
470
471 private function getActiveTabID()
472 {
473 $tab_param_isset = isset( $_GET['active_tab'] ) && array_key_exists( $_GET['active_tab'], $this->getSettingsGroups() );
474 return $tab_param_isset ? sanitize_text_field( wp_unslash( $_GET['active_tab'] ) ) : 'general';
475 }
476 }
477