PluginProbe
Faust.js / 0.8.1
Faust.js v0.8.1
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 0.8.1, at includes/settings/callbacks.php

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