%s', esc_url( Functions::get_settings_url() ), _x( 'Settings', 'Plugin settings link on the plugins page', 'alerts-dlx' ) ); array_unshift( $links, $settings_link ); return $links; } /** * Add admin body class on the settings screen. * * @param string $classes Admin body classes. * * @return string */ public function add_admin_body_class( $classes ) { $screen = get_current_screen(); if ( $screen && 'settings_page_alerts-dlx' === $screen->id ) { $classes .= ' alerts-dlx-body'; } return $classes; } /** * Retrieve settings via AJAX. */ public function ajax_retrieve_settings() { if ( ! wp_verify_nonce( filter_input( INPUT_POST, 'nonce', FILTER_DEFAULT ), 'alerts_dlx_retrieve_settings' ) || ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'Security check failed', 'alerts-dlx' ) ) ); } $options = Options::get_plugin_options( true ); wp_send_json_success( array( 'values' => $this->map_defaults_to_js( stripslashes_deep( $options ) ), 'blockStyleOptions' => $this->get_block_style_options_for_js(), 'headlineStyleOptions' => $this->get_headline_style_options_for_js(), ) ); } /** * Save settings via AJAX. */ public function ajax_save_settings() { if ( ! wp_verify_nonce( filter_input( INPUT_POST, 'nonce', FILTER_DEFAULT ), 'alerts_dlx_save_settings' ) || ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'Security check failed', 'alerts-dlx' ) ) ); } $form_data = filter_input( INPUT_POST, 'form_data', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); if ( ! is_array( $form_data ) ) { wp_send_json_error( array( 'message' => __( 'Invalid form data', 'alerts-dlx' ) ) ); } $form_data = Functions::to_underlines_recursive( $form_data ); $form_data = Functions::sanitize_array_recursive( $form_data ); $headline_style = isset( $form_data['headline_style'] ) ? sanitize_key( $form_data['headline_style'] ) : 'h2'; if ( ! in_array( $headline_style, Options::get_allowed_headline_styles(), true ) ) { $headline_style = 'h2'; } $enabled_block_styles = Options::sanitize_enabled_block_styles( $form_data['enabled_block_styles'] ?? array() ); if ( empty( $enabled_block_styles ) ) { wp_send_json_error( array( 'message' => __( 'At least one alert theme must remain enabled.', 'alerts-dlx' ) ) ); } $debug_mode = filter_var( $form_data['debug_mode'] ?? false, FILTER_VALIDATE_BOOLEAN ); $headline_custom_classes_result = Options::sanitize_headline_custom_classes( $form_data['headline_custom_classes'] ?? '' ); if ( is_wp_error( $headline_custom_classes_result ) ) { wp_send_json_error( array( 'message' => $headline_custom_classes_result->get_error_message() ) ); } $headline_force_size = filter_var( $form_data['headline_force_size'] ?? false, FILTER_VALIDATE_BOOLEAN ); $settings = array( 'headline_style' => $headline_style, 'headline_custom_classes' => $headline_custom_classes_result['value'], 'headline_force_size' => $headline_force_size, 'enabled_block_styles' => $enabled_block_styles, 'debug_mode' => $debug_mode, 'options_version' => Options::get_defaults()['options_version'], ); update_option( 'alerts_dlx', $settings ); wp_send_json_success( array( 'values' => $this->map_defaults_to_js( stripslashes_deep( Options::get_plugin_options( true ) ) ), ) ); } /** * Reset settings via AJAX. */ public function ajax_reset_settings() { if ( ! wp_verify_nonce( filter_input( INPUT_POST, 'nonce', FILTER_DEFAULT ), 'alerts_dlx_reset_settings' ) || ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'Security check failed', 'alerts-dlx' ) ) ); } delete_option( 'alerts_dlx' ); wp_send_json_success( array( 'values' => $this->map_defaults_to_js( stripslashes_deep( Options::get_plugin_options( true ) ) ), ) ); } /** * Register the settings page. */ public function add_admin_menu() { add_options_page( _x( 'AlertsDLX', 'Plugin Name - Settings Page Title', 'alerts-dlx' ), _x( 'AlertsDLX', 'Plugin Name - Menu Item', 'alerts-dlx' ), 'manage_options', 'alerts-dlx', array( $this, 'options_page' ) ); } /** * Render the settings page shell. */ public function options_page() { ?>
wp_create_nonce( 'alerts_dlx_retrieve_settings' ), 'saveNonce' => wp_create_nonce( 'alerts_dlx_save_settings' ), 'resetNonce' => wp_create_nonce( 'alerts_dlx_reset_settings' ), ) ); } /** * Get loading SVG markup. * * @return string */ private function get_loading_svg() { return ''; } /** * Maps PHP option keys to JS camelCase keys. * * @param array $options Array of options and values. * * @return array */ private function map_defaults_to_js( $options ) { $js_option_names = array(); foreach ( $options as $option_name => $option_value ) { if ( is_array( $option_value ) ) { $option_value = $this->map_defaults_to_js( $option_value ); } $js_option_names[ Functions::to_camelcase( $option_name ) ] = $option_value; } return $js_option_names; } /** * Get alert theme options for the admin UI. * * @return array */ private function get_block_style_options_for_js() { $definitions = Options::get_block_style_definitions(); $options = array(); foreach ( $definitions as $value => $label ) { $options[] = array( 'label' => $label, 'value' => $value, ); } return $options; } /** * Get headline style options for the admin UI. * * @return array */ private function get_headline_style_options_for_js() { $options = array(); foreach ( Options::get_allowed_headline_styles() as $style ) { $options[] = array( 'label' => strtoupper( $style ), 'value' => $style, ); } return $options; } }