PluginProbe
Faust.js / 1.0.2
Faust.js v1.0.2
1.8.12 1.8.11 1.4.1 1.8.0 1.8.8 1.8.9 trunk 0.7.0 0.7.1 0.7.10 0.7.11 0.7.3 0.7.4 0.7.5 0.7.6 0.7.7 0.7.8 0.7.9 0.8.0 0.8.1 0.8.3 0.8.4 0.8.5 0.8.6 0.8.7 All 40 releases
faustwp / includes / settings / callbacks.php

callbacks.php in Faust.js 1.0.2, at includes/settings/callbacks.php

507 lines 13.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Settings related callbacks.
4 *
5 * @package FaustWP
6 */
7
8 namespace WPE\FaustWP\Settings;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 add_action( 'admin_notices', __NAMESPACE__ . '\\frontend_url_notice' );
15 /**
16 * Callback for WordPress 'admin_notices' action.
17 *
18 * Show frontend_url missing error if it doesn't exist.
19 *
20 * @return void
21 */
22 function frontend_url_notice() {
23 $screen = get_current_screen();
24
25 // Exit if not this plugin's settings page.
26 if ( 'settings_page_faustwp-settings' !== $screen->id ) {
27 return;
28 }
29
30 $frontend_url_setting = faustwp_get_setting( 'frontend_uri' );
31
32 if ( empty( $frontend_url_setting ) ) {
33 ?>
34 <div class="notice notice-error is-dismissible" style="background-color: #FFEAE9;">
35 <p><?php esc_html_e( 'Front-end site URL is required to utilize url rewrites and previews.', 'faustwp' ); ?></p>
36 <p><?php esc_html_e( 'It is highly recommended that you update it below to avoid unexpected behavior.', 'faustwp' ); ?></p>
37 <p>
38 <?php
39 printf(
40 /* translators: Link text */
41 esc_html__( 'See the %1$s Getting Started Documentation%2$s for more details.', 'faustwp' ),
42 '<a href="https://faustjs.org/docs/getting-started" target="_blank" rel="noopener noreferrer">',
43 '</a>'
44 );
45 ?>
46 </p>
47 </div>
48 <?php
49 }
50 }
51
52 add_action( 'admin_menu', __NAMESPACE__ . '\\register_settings_menu' );
53 /**
54 * Callback for WordPress 'admin_menu' action.
55 *
56 * Register the sub-menu.
57 *
58 * @uses add_submenu_page()
59 * @link https://developer.wordpress.org/reference/functions/add_submenu_page/
60 *
61 * @return void
62 */
63 function register_settings_menu() {
64 add_submenu_page(
65 'options-general.php',
66 __( 'Faust', 'faustwp' ),
67 __( 'Faust', 'faustwp' ),
68 'manage_options',
69 'faustwp-settings',
70 __NAMESPACE__ . '\\display_settings_page'
71 );
72 }
73
74 add_action( 'admin_init', __NAMESPACE__ . '\\register_settings' );
75 /**
76 * Callback for WordPress 'admin_init' action.
77 *
78 * Register settings with WordPress.
79 *
80 * @uses register_setting()
81 * @link https://developer.wordpress.org/reference/functions/register_setting/
82 *
83 * @return void
84 */
85 function register_settings() {
86 register_setting( 'faustwp_settings', 'faustwp_settings' );
87 }
88
89 add_action( 'admin_init', __NAMESPACE__ . '\\register_settings_section' );
90 /**
91 * Callback for WordPress 'admin_init' action.
92 *
93 * Register settings form sections.
94 *
95 * @uses add_settings_section()
96 * @link https://developer.wordpress.org/reference/functions/add_settings_section/
97 *
98 * @return void
99 */
100 function register_settings_section() {
101 add_settings_section(
102 'settings_section',
103 null,
104 null,
105 'faustwp-settings'
106 );
107 }
108
109 add_action( 'admin_init', __NAMESPACE__ . '\\register_settings_fields' );
110 /**
111 * Callback for WordPress 'admin_init' action.
112 *
113 * Add the settings fields.
114 *
115 * @uses add_settings_field()
116 * @link https://developer.wordpress.org/reference/functions/add_settings_field/
117 *
118 * @return void
119 */
120 function register_settings_fields() {
121 add_settings_field(
122 'frontend_uri',
123 __( 'Front-end site URL', 'faustwp' ),
124 __NAMESPACE__ . '\\display_frontend_uri_field',
125 'faustwp-settings',
126 'settings_section',
127 array(
128 'class' => 'align-middle',
129 'label_for' => 'frontend_uri',
130 )
131 );
132
133 add_settings_field(
134 'secret_key',
135 __( 'Secret Key', 'faustwp' ),
136 __NAMESPACE__ . '\\display_secret_key_field',
137 'faustwp-settings',
138 'settings_section',
139 array(
140 'class' => 'align-middle',
141 'label_for' => 'secret_key',
142 )
143 );
144
145 add_settings_field(
146 'menu_locations',
147 __( 'Menu Locations', 'faustwp' ),
148 __NAMESPACE__ . '\\display_menu_locations_field',
149 'faustwp-settings',
150 'settings_section',
151 array(
152 'class' => 'align-middle',
153 'label_for' => 'menu_locations',
154 )
155 );
156
157 add_settings_field(
158 'enable_disable',
159 __( 'Features', 'faustwp' ),
160 __NAMESPACE__ . '\\display_enable_disable_fields',
161 'faustwp-settings',
162 'settings_section'
163 );
164 }
165
166 add_filter( 'sanitize_option_faustwp_settings', __NAMESPACE__ . '\\sanitize_faustwp_settings', 10, 2 );
167 /**
168 * Validates and sanitizes Faust settings.
169 *
170 * The plugin settings page will display any relevant errors when
171 * rejecting invalid settings values. Updates to Faust settings that
172 * are not initiated from the plugin settings page will not return or
173 * display errors, but will still reject invalid values.
174 *
175 * Once settings are validated, the sanitized values are returned.
176 *
177 * @param array $settings Faust settings array to validate and sanitize.
178 * @param string $option WP option name where settings are saved.
179 * @return array Sanitized settings.
180 */
181 function sanitize_faustwp_settings( $settings, $option ) {
182 $errors = null;
183 $protocols = array( 'http', 'https' );
184 foreach ( $settings as $name => $value ) {
185 switch ( $name ) {
186 case 'frontend_uri':
187 if ( '' === $value || preg_match( '#http(s?)://(.+)#i', $value ) ) {
188 $settings[ $name ] = esc_url_raw( $value, $protocols );
189 } else {
190 $errors[ $name ] = __( 'The Front-end site URL you entered did not appear to be a valid URL. Please enter a valid URL.', 'faustwp' );
191 $settings[ $name ] = faustwp_get_setting( $name );
192 }
193 break;
194
195 case 'secret_key':
196 if ( ! wp_is_uuid( $value, 4 ) ) {
197 $errors[ $name ] = __( 'The secret key you entered did not appear to be a valid UUID.', 'faustwp' );
198 $settings[ $name ] = get_secret_key();
199 }
200 break;
201
202 case 'menu_locations':
203 $settings[ $name ] = sanitize_text_field( $value );
204 break;
205
206 case 'enable_redirects':
207 case 'enable_rewrites':
208 case 'disable_theme':
209 case 'enable_image_source':
210 if ( $value ) {
211 $settings[ $name ] = '1';
212 } else {
213 unset( $settings[ $name ] );
214 }
215 break;
216
217 default:
218 // Remove any settings we don't expect.
219 unset( $settings[ $name ] );
220 }
221 }
222
223 if ( null !== $errors && is_array( $errors ) ) {
224 foreach ( $errors as $name => $error ) {
225 add_settings_error( $option, "faustwp_invalid_{$name}", $error );
226 }
227 }
228
229 return $settings;
230 }
231
232 add_action( 'load-settings_page_faustwp-settings', __NAMESPACE__ . '\\handle_regenerate_secret_key', 5 );
233 /**
234 * Callback for WordPress 'load-{$page_hook}' action.
235 *
236 * Nonce set in WPE\FaustWP\Settings\display_secret_key_field().
237 *
238 * Regenerate the secret key.
239 *
240 * @return void
241 */
242 function handle_regenerate_secret_key() {
243 $screen = get_current_screen();
244 if ( 'settings_page_faustwp-settings' !== $screen->id ) {
245 return;
246 }
247
248 if ( empty( $_GET['regenerate_nonce'] ) ) {
249 return;
250 }
251
252 if ( ! current_user_can( 'manage_options' ) ) {
253 return;
254 }
255
256 check_admin_referer( 'regenerate_secret', 'regenerate_nonce' );
257
258 faustwp_update_setting( 'secret_key', wp_generate_uuid4() );
259
260 wp_safe_redirect(
261 admin_url( '/options-general.php?page=faustwp-settings' )
262 );
263
264 exit;
265 }
266
267 /**
268 * Callback for add_submenu_page() function.
269 *
270 * Display the settings page.
271 *
272 * @return void
273 */
274 function display_settings_page() {
275 require FAUSTWP_DIR . '/includes/settings/views/headless-settings.php';
276 }
277
278 /**
279 * Callback for WordPress add_settings_field() method parameter.
280 *
281 * Display the "Menu Locations" text field.
282 *
283 * @return void
284 */
285 function display_menu_locations_field() {
286 $menu_locations = faustwp_get_setting( 'menu_locations', 'Primary, Footer' );
287
288 ?>
289 <input type="text" id="menu_locations" name="faustwp_settings[menu_locations]" value="<?php echo esc_attr( $menu_locations ); ?>" class="regular-text" />
290
291 <p class="description">
292 <?php esc_html_e( 'A comma-separated list of menu locations. Assign menus to locations at Appearance → Menus.', 'faustwp' ); ?>
293 </p>
294 <?php
295 }
296
297 /**
298 * Callback for WordPress add_settings_field() method parameter.
299 *
300 * Display the "API Key" text field.
301 *
302 * Added hidden field to preserve value during settings save.
303 *
304 * @return void
305 */
306 function display_secret_key_field() {
307 $secret_key = get_secret_key();
308 $regenerate_url = wp_nonce_url(
309 admin_url( 'options-general.php?page=faustwp-settings' ),
310 'regenerate_secret',
311 'regenerate_nonce'
312 );
313
314 ?>
315 <input type="text" id="secret_key" value="<?php echo esc_attr( $secret_key ); ?>" class="regular-text code" readonly />
316 <input type="hidden" name="faustwp_settings[secret_key]" value="<?php echo esc_attr( $secret_key ); ?>" />
317
318 <a
319 href="<?php echo esc_url( $regenerate_url ); ?>"
320 title="<?php esc_attr_e( 'Regenerate Secret Key', 'faustwp' ); ?>"
321 onclick="confirm_regenerate_key( event )"
322 class="field-action"
323 >
324 <?php esc_html_e( 'Regenerate', 'faustwp' ); ?>
325 </a>
326
327 <script type="text/javascript">
328 function confirm_regenerate_key( event ) {
329 if ( ! confirm( 'Are you sure you want to regenerate your secret key?' ) ) {
330 event.preventDefault();
331 }
332 }
333 </script>
334
335 <p class="description">
336 <?php
337 printf(
338 /* translators: %s: Documentation URL. */
339 wp_kses_post( __( 'This key is used to enable <a href="%s" target="_blank" rel="noopener noreferrer">headless post previews</a>.', 'faustwp' ) ),
340 'https://faustjs.org/docs/next/guides/post-page-previews'
341 );
342 ?>
343 </p>
344 <?php
345 }
346
347 /**
348 * Callback for WordPress add_settings_field() method parameter.
349 *
350 * Display the Preview Base Address (URL) field.
351 *
352 * @return void
353 */
354 function display_frontend_uri_field() {
355 $frontend_uri = faustwp_get_setting( 'frontend_uri', '' );
356
357 ?>
358 <input type="text" id="frontend_uri" name="faustwp_settings[frontend_uri]" value="<?php echo esc_attr( $frontend_uri ); ?>" class="regular-text" />
359 <p class="description">
360 <?php esc_html_e( 'The full URL to your headless front-end, including https:// or http://. This is used for authenticated post previews and for rewriting links to point to your front-end site.', 'faustwp' ); ?>
361 </p>
362 <?php
363 }
364
365 /**
366 * Callback for WordPress add_settings_field() method parameter.
367 *
368 * Display the enable/disable features section.
369 *
370 * @return void
371 */
372 function display_enable_disable_fields() {
373 $disable_theme = is_themes_disabled();
374 $enable_rewrites = is_rewrites_enabled();
375 $enable_redirects = is_redirects_enabled();
376 $enable_image_source = is_image_source_replacement_enabled();
377
378 ?>
379 <fieldset>
380 <legend style="margin-bottom:5px;padding:0;">
381 <p class="description">
382 <?php
383 printf(
384 /* translators: %s: Documentation URL. */
385 wp_kses_post( __( 'Learn more about <a href="%s" target="_blank" rel="noopener noreferrer">features</a>.', 'faustwp' ) ),
386 'https://faustjs.org/docs/faustwp/settings'
387 );
388 ?>
389 </p>
390 </legend>
391 <label for="disable_theme">
392 <input type="checkbox" id="disable_theme" name="faustwp_settings[disable_theme]" value="1" <?php checked( $disable_theme ); ?> />
393 <?php esc_html_e( 'Disable WordPress theme admin pages', 'faustwp' ); ?>
394 </label>
395 <br />
396
397 <label for="enable_rewrites">
398 <input type="checkbox" id="enable_rewrites" name="faustwp_settings[enable_rewrites]" value="1" <?php checked( $enable_rewrites ); ?> />
399 <?php esc_html_e( 'Enable Post and Category URL rewrites', 'faustwp' ); ?>
400 </label>
401 <br />
402
403 <label for="enable_redirects">
404 <input type="checkbox" id="enable_redirects" name="faustwp_settings[enable_redirects]" value="1" <?php checked( $enable_redirects ); ?> />
405 <?php esc_html_e( 'Enable public route redirects', 'faustwp' ); ?>
406 </label>
407 <br />
408
409 <label for="enable_image_source">
410 <input type="checkbox" id="enable_image_source" name="faustwp_settings[enable_image_source]" value="1" <?php checked( $enable_image_source ); ?> />
411 <?php esc_html_e( 'Use the WordPress domain for media URLs in post content', 'faustwp' ); ?>
412 </label>
413 </fieldset>
414 <?php
415 }
416
417 add_action( 'load-settings_page_faustwp-settings', __NAMESPACE__ . '\\verify_graphql_dependency' );
418 /**
419 * Verifies the WP GraphQL dependency is met.
420 *
421 * If not, it adds an admin notice and related scripts
422 * for installing and activating the plugin.
423 *
424 * @return void
425 */
426 function verify_graphql_dependency() {
427 add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\\add_settings_assets' );
428 }
429
430 /**
431 * Enqueues the settings stylesheet and scripts at Settings → Headless.
432 *
433 * Callback for admin_enqueue_scripts.
434 */
435 function add_settings_assets() {
436 $plugin = get_plugin_data( FAUSTWP_FILE );
437
438 wp_enqueue_style(
439 'faustwp-settings',
440 FAUSTWP_URL . 'includes/settings/assets/style.css',
441 array(),
442 $plugin['Version']
443 );
444
445 if ( ! function_exists( 'graphql' ) ) {
446 wp_enqueue_script(
447 'faustwp-wpgraphql-install',
448 FAUSTWP_URL . 'includes/settings/assets/js/wpgraphql-install.js',
449 array( 'wp-a11y', 'wp-api-fetch' ),
450 $plugin['Version'],
451 true
452 );
453
454 $faustwp = array(
455 'wpgraphqlIsInstalled' => array_key_exists( 'wp-graphql/wp-graphql.php', get_plugins() ),
456 'icons' => array(
457 'checkSmall' => get_icon( 'check-small' ),
458 ),
459 'strings' => array(
460 'default' => esc_html__( 'Install and Activate', 'faustwp' ),
461 'installing' => esc_html__( 'Installing…', 'faustwp' ),
462 'active' => esc_html__( 'WPGraphQL is active', 'faustwp' ),
463 'failed' => esc_html__( 'Installation failed', 'faustwp' ),
464 ),
465 );
466
467 wp_localize_script(
468 'faustwp-wpgraphql-install',
469 'faustwp',
470 $faustwp
471 );
472 }
473 }
474
475 add_filter( 'plugin_action_links_faustwp/faustwp.php', __NAMESPACE__ . '\\add_action_link_settings' );
476 /**
477 * Adds a link to the Settings page on the Installed Plugins page.
478 *
479 * @param array $links The array of plugin action links.
480 */
481 function add_action_link_settings( $links ) {
482 $url = add_query_arg( 'page', 'faustwp-settings', admin_url( 'options-general.php' ) );
483 return array_merge(
484 array(
485 '<a href="' . esc_url( $url ) . '">' . esc_html__( 'Settings', 'faustwp' ) . '</a>',
486 ),
487 $links
488 );
489 }
490
491 add_filter( 'faustwp_get_setting', __NAMESPACE__ . '\\trim_frontend_uri_trailing_slash', 10, 2 );
492 /**
493 * Ensure that this plugin's frontend_uri setting does not have a trailing slash.
494 *
495 * @param mixed $value The setting value.
496 * @param string $name The setting name.
497 *
498 * @return string
499 */
500 function trim_frontend_uri_trailing_slash( $value, $name ) {
501 if ( 'frontend_uri' !== $name ) {
502 return $value;
503 }
504
505 return untrailingslashit( $value );
506 }
507