| 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 |
__( 'Headless', 'faustwp' ), |
| 29 |
__( 'Headless', '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_action( 'load-settings_page_faustwp-settings', __NAMESPACE__ . '\\handle_regenerate_secret_key', 5 ); |
| 129 |
/** |
| 130 |
* Callback for WordPress 'load-{$page_hook}' action. |
| 131 |
* |
| 132 |
* Nonce set in WPE\FaustWP\Settings\display_secret_key_field(). |
| 133 |
* |
| 134 |
* Regenerate the secret key. |
| 135 |
* |
| 136 |
* @return void |
| 137 |
*/ |
| 138 |
function handle_regenerate_secret_key() { |
| 139 |
$screen = get_current_screen(); |
| 140 |
if ( 'settings_page_faustwp-settings' !== $screen->id ) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
if ( empty( $_GET['regenerate_nonce'] ) ) { |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
check_admin_referer( 'regenerate_secret', 'regenerate_nonce' ); |
| 153 |
|
| 154 |
faustwp_update_setting( 'secret_key', wp_generate_uuid4() ); |
| 155 |
|
| 156 |
wp_safe_redirect( |
| 157 |
admin_url( '/options-general.php?page=faustwp-settings' ) |
| 158 |
); |
| 159 |
|
| 160 |
exit; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Callback for add_submenu_page() function. |
| 165 |
* |
| 166 |
* Display the settings page. |
| 167 |
* |
| 168 |
* @return void |
| 169 |
*/ |
| 170 |
function display_settings_page() { |
| 171 |
require FAUSTWP_DIR . '/includes/settings/views/headless-settings.php'; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Callback for WordPress add_settings_field() method parameter. |
| 176 |
* |
| 177 |
* Display the "Menu Locations" text field. |
| 178 |
* |
| 179 |
* @return void |
| 180 |
*/ |
| 181 |
function display_menu_locations_field() { |
| 182 |
$menu_locations = faustwp_get_setting( 'menu_locations', 'Primary, Footer' ); |
| 183 |
|
| 184 |
?> |
| 185 |
<input type="text" id="menu_locations" name="faustwp_settings[menu_locations]" value="<?php echo esc_attr( $menu_locations ); ?>" class="regular-text" /> |
| 186 |
|
| 187 |
<p class="description"> |
| 188 |
<?php esc_html_e( 'A comma-separated list of menu locations. Assign menus to locations at Appearance → Menus.', 'faustwp' ); ?> |
| 189 |
</p> |
| 190 |
<?php |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Callback for WordPress add_settings_field() method parameter. |
| 195 |
* |
| 196 |
* Display the "API Key" text field. |
| 197 |
* |
| 198 |
* Added hidden field to preserve value during settings save. |
| 199 |
* |
| 200 |
* @return void |
| 201 |
*/ |
| 202 |
function display_secret_key_field() { |
| 203 |
$secret_key = get_secret_key(); |
| 204 |
$regenerate_url = wp_nonce_url( |
| 205 |
admin_url( 'options-general.php?page=faustwp-settings' ), |
| 206 |
'regenerate_secret', |
| 207 |
'regenerate_nonce' |
| 208 |
); |
| 209 |
|
| 210 |
?> |
| 211 |
<input type="text" id="secret_key" value="<?php echo esc_attr( $secret_key ); ?>" class="regular-text code" readonly /> |
| 212 |
<input type="hidden" name="faustwp_settings[secret_key]" value="<?php echo esc_attr( $secret_key ); ?>" /> |
| 213 |
|
| 214 |
<a |
| 215 |
href="<?php echo esc_url( $regenerate_url ); ?>" |
| 216 |
title="<?php esc_attr_e( 'Regenerate Secret Key', 'faustwp' ); ?>" |
| 217 |
onclick="confirm_regenerate_key( event )" |
| 218 |
class="field-action" |
| 219 |
> |
| 220 |
<?php esc_html_e( 'Regenerate', 'faustwp' ); ?> |
| 221 |
</a> |
| 222 |
|
| 223 |
<script type="text/javascript"> |
| 224 |
function confirm_regenerate_key( event ) { |
| 225 |
if ( ! confirm( 'Are you sure you want to regenerate your secret key?' ) ) { |
| 226 |
event.preventDefault(); |
| 227 |
} |
| 228 |
} |
| 229 |
</script> |
| 230 |
|
| 231 |
<p class="description"> |
| 232 |
<?php |
| 233 |
printf( |
| 234 |
/* translators: %s: Documentation URL. */ |
| 235 |
wp_kses_post( __( 'This key is used to enable <a href="%s" target="_blank" rel="noopener noreferrer">headless post previews</a>.', 'faustwp' ) ), |
| 236 |
'https://faustjs.org/docs/next/guides/post-page-previews' |
| 237 |
); |
| 238 |
?> |
| 239 |
</p> |
| 240 |
<?php |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Callback for WordPress add_settings_field() method parameter. |
| 245 |
* |
| 246 |
* Display the Preview Base Address (URL) field. |
| 247 |
* |
| 248 |
* @return void |
| 249 |
*/ |
| 250 |
function display_frontend_uri_field() { |
| 251 |
$frontend_uri = faustwp_get_setting( 'frontend_uri', '' ); |
| 252 |
|
| 253 |
?> |
| 254 |
<input type="text" id="frontend_uri" name="faustwp_settings[frontend_uri]" value="<?php echo esc_attr( $frontend_uri ); ?>" class="regular-text" /> |
| 255 |
<p class="description"> |
| 256 |
<?php esc_html_e( 'The URL to your headless front-end. This is used for authenticated post previews and for rewriting links to point to your front-end site.', 'faustwp' ); ?> |
| 257 |
</p> |
| 258 |
<?php |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Callback for WordPress add_settings_field() method parameter. |
| 263 |
* |
| 264 |
* Display the enable/disable features section. |
| 265 |
* |
| 266 |
* @return void |
| 267 |
*/ |
| 268 |
function display_enable_disable_fields() { |
| 269 |
$disable_theme = is_themes_disabled(); |
| 270 |
$enable_rewrites = is_rewrites_enabled(); |
| 271 |
$enable_redirects = is_redirects_enabled(); |
| 272 |
$enable_image_source = is_image_source_replacement_enabled(); |
| 273 |
|
| 274 |
?> |
| 275 |
<fieldset> |
| 276 |
<label for="disable_theme"> |
| 277 |
<input type="checkbox" id="disable_theme" name="faustwp_settings[disable_theme]" value="1" <?php checked( $disable_theme ); ?> /> |
| 278 |
<?php esc_html_e( 'Disable WordPress theme admin pages', 'faustwp' ); ?> |
| 279 |
</label> |
| 280 |
<br /> |
| 281 |
|
| 282 |
<label for="enable_rewrites"> |
| 283 |
<input type="checkbox" id="enable_rewrites" name="faustwp_settings[enable_rewrites]" value="1" <?php checked( $enable_rewrites ); ?> /> |
| 284 |
<?php esc_html_e( 'Enable Post and Category URL rewrites', 'faustwp' ); ?> |
| 285 |
</label> |
| 286 |
<br /> |
| 287 |
|
| 288 |
<label for="enable_redirects"> |
| 289 |
<input type="checkbox" id="enable_redirects" name="faustwp_settings[enable_redirects]" value="1" <?php checked( $enable_redirects ); ?> /> |
| 290 |
<?php esc_html_e( 'Enable public route redirects', 'faustwp' ); ?> |
| 291 |
</label> |
| 292 |
<br /> |
| 293 |
|
| 294 |
<label for="enable_image_source"> |
| 295 |
<input type="checkbox" id="enable_image_source" name="faustwp_settings[enable_image_source]" value="1" <?php checked( $enable_image_source ); ?> /> |
| 296 |
<?php esc_html_e( 'Use the WordPress domain for media URLs in post content', 'faustwp' ); ?> |
| 297 |
</label> |
| 298 |
</fieldset> |
| 299 |
<?php |
| 300 |
} |
| 301 |
|
| 302 |
add_action( 'load-settings_page_faustwp-settings', __NAMESPACE__ . '\\verify_graphql_dependency' ); |
| 303 |
/** |
| 304 |
* Verifies the WP GraphQL dependency is met. |
| 305 |
* |
| 306 |
* If not, it adds an admin notice and related scripts |
| 307 |
* for installing and activating the plugin. |
| 308 |
* |
| 309 |
* @return void |
| 310 |
*/ |
| 311 |
function verify_graphql_dependency() { |
| 312 |
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\\add_settings_assets' ); |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Enqueues the settings stylesheet and scripts at Settings → Headless. |
| 317 |
* |
| 318 |
* Callback for admin_enqueue_scripts. |
| 319 |
*/ |
| 320 |
function add_settings_assets() { |
| 321 |
$plugin = get_plugin_data( FAUSTWP_FILE ); |
| 322 |
|
| 323 |
wp_enqueue_style( |
| 324 |
'faustwp-settings', |
| 325 |
FAUSTWP_URL . 'includes/settings/assets/style.css', |
| 326 |
array(), |
| 327 |
$plugin['Version'] |
| 328 |
); |
| 329 |
|
| 330 |
if ( ! function_exists( 'graphql' ) ) { |
| 331 |
wp_enqueue_script( |
| 332 |
'faustwp-wpgraphql-install', |
| 333 |
FAUSTWP_URL . 'includes/settings/assets/js/wpgraphql-install.js', |
| 334 |
array( 'wp-a11y', 'wp-api-fetch' ), |
| 335 |
$plugin['Version'], |
| 336 |
true |
| 337 |
); |
| 338 |
|
| 339 |
$faustwp = array( |
| 340 |
'wpgraphqlIsInstalled' => array_key_exists( 'wp-graphql/wp-graphql.php', get_plugins() ), |
| 341 |
'strings' => array( |
| 342 |
'default' => esc_html__( 'Install and Activate', 'faustwp' ), |
| 343 |
'installing' => esc_html__( 'Installing…', 'faustwp' ), |
| 344 |
'active' => esc_html__( 'WPGraphQL is active', 'faustwp' ), |
| 345 |
'failed' => esc_html__( 'Installation failed', 'faustwp' ), |
| 346 |
), |
| 347 |
); |
| 348 |
|
| 349 |
wp_localize_script( |
| 350 |
'faustwp-wpgraphql-install', |
| 351 |
'faustwp', |
| 352 |
$faustwp |
| 353 |
); |
| 354 |
} |
| 355 |
} |
| 356 |
|