PluginProbe
Getwid – Gutenberg Blocks / 1.8.7
Getwid – Gutenberg Blocks v1.8.7
3.0.2 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 1.8.7, at includes/settings-page.php

437 lines 16.8 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 {
121 global $pagenow;
122
123 if ( $pagenow == 'options-general.php' && isset( $_GET['instagram-token'] ) && isset( $_GET['nonce'] ) ) {
124
125 if ( wp_verify_nonce( sanitize_key( $_GET['nonce'] ), 'getwid_nonce_save_instagram_token' ) && current_user_can( 'manage_options' ) ) {
126
127 // Update token
128 update_option( 'getwid_instagram_token', sanitize_text_field( wp_unslash( $_GET['instagram-token'] ) ) );
129 // Delete cache data
130 delete_transient( 'getwid_instagram_response_data' );
131 // Schedule token refresh
132 getwid()->instagramTokenManager()->schedule_token_refresh_event();
133
134 $redirect_url = add_query_arg(
135 [
136 'getwid-instagram-success' => true
137 ],
138 $this->getTabUrl('general')
139 );
140 } else {
141 $redirect_url = add_query_arg(
142 [
143 'instagram-error' => true
144 ],
145 $this->getTabUrl('general')
146 );
147 }
148
149 wp_redirect( $redirect_url );
150 }
151
152 if (isset($_GET['getwid-instagram-success'])) {
153 add_action( 'admin_notices', [$this, 'getwid_instagram_notice_success'] );
154 }
155
156 if (isset($_GET['instagram-error'])) {
157 add_action( 'admin_notices', [$this, 'getwid_instagram_notice_error'] );
158 }
159 }
160
161 public function registerFields() {
162
163 /* #region Section Content Width */
164 add_settings_field( 'getwid_section_content_width', __( 'Section Content Width', 'getwid' ),
165 [ $this, 'renderSectionContentWidth' ], 'getwid_appearance', 'getwid_appearance' );
166 register_setting( 'getwid_appearance', 'getwid_section_content_width', [ 'type' => 'number', 'default' => '' ] );
167 /* #endregion */
168
169 /* #region Animation */
170 add_settings_field( 'getwid_animation', __( 'Animation', 'getwid' ),
171 [ $this, 'renderAnimation' ], 'getwid_appearance', 'getwid_appearance' );
172 register_setting( 'getwid_appearance', 'getwid_smooth_animation', [ 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean' ] );
173 /* #endregion */
174
175 /* #region AssetsOptimization */
176 add_settings_field( 'getwid_assets_optimization', __( 'Performance Optimization', 'getwid' ),
177 [ $this, 'renderAssetsOptimization'], 'getwid_general', 'getwid_general' );
178 register_setting( 'getwid_general', 'getwid_load_assets_on_demand', [ 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean' ] );
179
180 register_setting( 'getwid_general', 'getwid_move_css_to_head', [ 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean' ] );
181 /* #endregion */
182
183 /* #region Instagram Access Token */
184 add_settings_field( 'getwid_instagram_token', __( 'Instagram Access Token', 'getwid' ),
185 [ $this, 'renderInstagramToken' ], 'getwid_general', 'getwid_general' );
186 register_setting( 'getwid_general', 'getwid_instagram_token', [ 'type' => 'text', 'default' => '' ] );
187 /* #endregion */
188
189 /* #region Instagram Cache Timeout */
190 add_settings_field( 'getwid_instagram_cache_timeout', __( 'Instagram Cache Timeout', 'getwid' ),
191 [ $this, 'renderInstagramCacheTimeout' ], 'getwid_general', 'getwid_general' );
192 register_setting( 'getwid_general', 'getwid_instagram_cache_timeout', [ 'type' => 'number', 'default' => 30 ] );
193 /* #endregion */
194
195 /* #region Google API Key */
196 add_settings_field( 'getwid_google_api_key', __( 'Google Maps API Key', 'getwid' ),
197 [ $this, 'renderGoogleApiKey' ], 'getwid_general', 'getwid_general' );
198 register_setting( 'getwid_general', 'getwid_google_api_key', [ 'type' => 'text', 'default' => '' ] );
199 /* #endregion */
200
201 /* #region Recaptcha Site Key */
202 add_settings_field( 'getwid_recaptcha_v2_site_key', __( 'Recaptcha Site Key', 'getwid' ),
203 [ $this, 'renderRecaptchaSiteKey' ], 'getwid_general', 'getwid_general' );
204 register_setting( 'getwid_general', 'getwid_recaptcha_v2_site_key', [ 'type' => 'text', 'default' => '' ] );
205 /* #endregion */
206
207 /* #region Recaptcha Secret Key */
208 add_settings_field( 'getwid_recaptcha_v2_secret_key', __( 'Recaptcha Secret Key', 'getwid' ),
209 [ $this, 'renderRecaptchaSecretKey' ], 'getwid_general', 'getwid_general' );
210 register_setting( 'getwid_general', 'getwid_recaptcha_v2_secret_key', [ 'type' => 'text', 'default' => '' ] );
211 /* #endregion */
212
213 /* #region Mailchimp Api Key */
214 add_settings_field( 'getwid_mailchimp_api_key', __( 'Mailchimp API Key', 'getwid' ),
215 [ $this, 'renderMailchimpApiKey' ], 'getwid_general', 'getwid_general' );
216 register_setting( 'getwid_general', 'getwid_mailchimp_api_key', [ 'type' => 'text', 'default' => '' ] );
217 /* #endregion */
218
219 /* #region Disabled Blocks */
220 add_settings_field( 'getwid_disabled_blocks', __( 'Disable Getwid Blocks', 'getwid' ),
221 [ $this, 'renderDisabledBlocks' ], 'getwid_blocks', 'getwid_blocks' );
222
223 $blocks = getwid()->blocksManager()->getBlocks();
224
225 foreach ($blocks as $name => $block) {
226 $option_name = $block->getDisabledOptionKey();
227 register_setting( 'getwid_blocks', $option_name, [ 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean' ] );
228 }
229 /* #endregion */
230 }
231
232 public function renderSectionContentWidth() {
233
234 $field_val = get_option( 'getwid_section_content_width', '' );
235
236 ?>
237 <input type="number" id="getwid_section_content_width" name="getwid_section_content_width" value="<?php echo esc_attr( $field_val ); ?>" />
238 <?php echo esc_html_x( 'px', 'pixels', 'getwid' ); ?>
239 <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>
240 <?php
241 }
242
243 public function renderInstagramToken() {
244
245 $field_val = 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 ); ?>" />
259 <p><a href="<?php echo esc_url(
260 'https://api.instagram.com/oauth/authorize?client_id=910186402812397&redirect_uri=' .
261 'https://api.getmotopress.com/get_instagram_token.php&scope=user_profile,user_media&response_type=code&state=' .
262 $connectURL ); ?>" class="button button-default"><?php echo esc_html__( 'Connect Instagram Account', 'getwid' );?></a>
263 <?php
264 if ( ! empty( $field_val) ) {
265 ?>
266 <a href="<?php echo esc_url(
267 'https://api.getmotopress.com/refresh_instagram_token.php?access_token=' . $field_val . '&state=' .
268 $refreshURL ); ?>" class="button button-default"><?php echo esc_html__( 'Refresh Access Token', 'getwid' );?></a>
269 <?php
270 }
271 ?>
272 </p>
273 <?php
274 }
275
276 public function renderInstagramCacheTimeout() {
277
278 $field_val = get_option('getwid_instagram_cache_timeout');
279 ?>
280 <input type="number" id="getwid_instagram_cache_timeout" name="getwid_instagram_cache_timeout" value="<?php echo esc_attr( $field_val ); ?>" />
281 <p class="description"><?php echo esc_html__( 'Time until expiration of media data in minutes. Setting to 0 means no expiration.', 'getwid' ); ?></p>
282 <?php
283 }
284
285 public function renderGoogleApiKey() {
286
287 $field_val = get_option('getwid_google_api_key', '');
288 ?>
289 <input type="text" id="getwid_google_api_key" name="getwid_google_api_key" class="regular-text" value="<?php echo esc_attr( $field_val ); ?>" />
290 <?php
291 }
292
293 public function renderRecaptchaSiteKey() {
294
295 $field_val = get_option( 'getwid_recaptcha_v2_site_key', '' );
296 ?>
297 <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 ); ?>" />
298 <?php
299 }
300
301 public function renderRecaptchaSecretKey() {
302
303 $field_val = get_option( 'getwid_recaptcha_v2_secret_key', '' );
304 ?>
305 <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 ); ?>" />
306 <?php
307 }
308
309 public function renderMailchimpApiKey() {
310
311 $field_val = get_option( 'getwid_mailchimp_api_key', '' );
312 ?>
313 <input type="text" id="getwid_mailchimp_api_key" name="getwid_mailchimp_api_key" class="regular-text" value="<?php echo esc_attr( $field_val ); ?>" />
314 <?php
315 }
316
317 public function renderDisabledBlocks() {
318
319 $blocks = getwid()->blocksManager()->getBlocks();
320 $disabledBlocks = getwid()->blocksManager()->getDisabledBlocks();
321 ksort( $blocks );
322 ?>
323 <p class="description">
324 <?php
325 printf(
326 //translators: %1$s, %2$s is a number of total and disabled blocks
327 esc_html__('Total: %1$s, Disabled: %2$s', 'getwid'),
328 esc_html( sizeof($blocks) ),
329 esc_html( sizeof($disabledBlocks) )
330 );
331 ?><br/>
332 <input type="button" id="getwid-disabled-blocks-select-all" class="button button-link" value="<?php esc_html_e('Select All', 'getwid'); ?>" />
333 &nbsp;/&nbsp;
334 <input type="button" id="getwid-disabled-blocks-deselect-all" class="button button-link" value="<?php esc_html_e('Deselect All', 'getwid'); ?>" />
335 </p>
336 <fieldset id="getwid-disabled-blocks">
337 <?php
338 foreach ($blocks as $name => $block) {
339 $option_name = $block->getDisabledOptionKey();
340 ?>
341 <label for="<?php echo esc_attr( $option_name ); ?>">
342 <input type="checkbox" id="<?php echo esc_attr( $option_name ); ?>" name="<?php echo esc_attr( $option_name ); ?>" value="1" <?php
343 checked( '1', $block->isDisabled() ); ?> />
344 <?php echo esc_html( $block->getLabel() ); ?>
345 </label><br/>
346 <?php
347 }
348 ?>
349 </fieldset>
350 <script>
351 jQuery(document).ready(function(){
352 jQuery('#getwid-disabled-blocks-select-all').click(function(){
353 jQuery('#getwid-disabled-blocks input:checkbox').attr('checked','checked');
354 });
355 jQuery('#getwid-disabled-blocks-deselect-all').click(function(){
356 jQuery('#getwid-disabled-blocks input:checkbox').removeAttr('checked');
357 });
358 })
359 </script>
360 <?php
361 }
362
363 public function renderPostTemplatesTab() {
364 ?>
365 <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>
366 <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>
367 <?php
368 }
369
370 public function renderAnimation() {
371
372 $field_val = get_option( 'getwid_smooth_animation', false );
373 ?>
374 <label for="getwid_smooth_animation">
375 <input type="checkbox" id="getwid_smooth_animation" name="getwid_smooth_animation" value="1" <?php
376 checked( '1', $field_val ); ?> />
377 <?php echo esc_html__('Enable smooth animation of blocks', 'getwid'); ?>
378 </label>
379 <p class="description"><?php
380 echo esc_html__('Hides block until the entrance animation starts. Prevents possible occurrence of horizontal scroll during the animation.', 'getwid');
381 ?></p>
382 <?php
383 }
384
385 public function renderAssetsOptimization() {
386
387 $getwid_load_assets_on_demand = get_option( 'getwid_load_assets_on_demand', false );
388 $getwid_move_css_to_head = get_option( 'getwid_move_css_to_head', false );
389 ?>
390 <fieldset>
391 <label for="getwid_load_assets_on_demand">
392 <input type="checkbox" id="getwid_load_assets_on_demand" name="getwid_load_assets_on_demand" value="1" <?php
393 checked( '1', $getwid_load_assets_on_demand ); ?> />
394 <?php echo esc_html__('Load CSS and JS of blocks on demand', 'getwid') . ' (Recomended)'; ?>
395 </label>
396 <p class="description"><?php
397 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');
398 ?></p>
399 <br/>
400 <label for="getwid_move_css_to_head">
401 <input type="checkbox" id="getwid_move_css_to_head" name="getwid_move_css_to_head" value="1" <?php
402 checked( '1', $getwid_move_css_to_head ); ?> />
403 <?php echo esc_html__('Aggregate all CSS files of blocks in header', 'getwid') . ' (Recomended)'; ?>
404 </label>
405 <p class="description"><?php
406 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');
407 ?></p>
408 <br/>
409 <p class="description"><?php
410 echo esc_html__('These settings may break some blocks in posts loaded via Ajax. These settings may not work together with optimization plugins.', 'getwid');
411 ?></p>
412 </fieldset>
413 <script>
414 jQuery(document).ready(function(){
415 // set initial state
416 jQuery('#getwid_move_css_to_head').toggleClass("disabled", ! jQuery('#getwid_load_assets_on_demand').prop("checked") );
417 // bind state change
418 jQuery('#getwid_load_assets_on_demand').change(function(e) {
419 jQuery('#getwid_move_css_to_head').toggleClass("disabled", ! jQuery(this).prop("checked") );
420 });
421 })
422 </script>
423 <?php
424 }
425
426 public function getTabUrl( $tab = 'general' )
427 {
428 return add_query_arg( [ 'page' => 'getwid', 'active_tab' => $tab ], admin_url( 'options-general.php' ) );
429 }
430
431 private function getActiveTabID()
432 {
433 $tab_param_isset = isset( $_GET['active_tab'] ) && array_key_exists( $_GET['active_tab'], $this->getSettingsGroups() );
434 return $tab_param_isset ? sanitize_text_field( wp_unslash( $_GET['active_tab'] ) ) : 'general';
435 }
436 }
437