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