PluginProbe
Getwid – Gutenberg Blocks / 2.1.1
Getwid – Gutenberg Blocks v2.1.1
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.1.1, at includes/settings-page.php

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