Settings.php
154 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Settings page |
| 4 | * |
| 5 | * @package FrontBlocks |
| 6 | * @author Closemarketing |
| 7 | * @copyright 2025 Closemarketing |
| 8 | * @version 1.0.0 |
| 9 | */ |
| 10 | |
| 11 | namespace FrontBlocks\Admin; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * Settings class |
| 17 | */ |
| 18 | class Settings { |
| 19 | |
| 20 | /** |
| 21 | * Option key for testimonials feature. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | private $option_enable_testimonials = 'enable_testimonials'; |
| 26 | |
| 27 | /** |
| 28 | * Page slug. |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | private $page_slug = 'frontblocks-settings'; |
| 33 | |
| 34 | /** |
| 35 | * Constructor. |
| 36 | */ |
| 37 | public function __construct() { |
| 38 | add_action( 'admin_menu', array( $this, 'register_menu' ) ); |
| 39 | add_action( 'admin_init', array( $this, 'register_settings' ) ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Register options page under Appearance. |
| 44 | * |
| 45 | * @return void |
| 46 | */ |
| 47 | public function register_menu() { |
| 48 | add_theme_page( |
| 49 | __( 'Frontblocks Settings', 'frontblocks' ), |
| 50 | __( 'Frontblocks', 'frontblocks' ), |
| 51 | 'edit_theme_options', |
| 52 | $this->page_slug, |
| 53 | array( $this, 'render_page' ) |
| 54 | ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Register settings, sections and fields. |
| 59 | * |
| 60 | * @return void |
| 61 | */ |
| 62 | public function register_settings() { |
| 63 | register_setting( |
| 64 | 'frontblocks_settings', |
| 65 | 'frontblocks_settings', |
| 66 | array( |
| 67 | 'type' => 'array', |
| 68 | 'sanitize_callback' => array( $this, 'sanitize_settings' ), |
| 69 | 'default' => array(), |
| 70 | 'show_in_rest' => false, |
| 71 | ) |
| 72 | ); |
| 73 | |
| 74 | add_settings_section( |
| 75 | 'frontblocks_section_features', |
| 76 | __( 'Features', 'frontblocks' ), |
| 77 | function () { |
| 78 | echo '<p>' . esc_html__( 'Enable or disable Frontblocks features.', 'frontblocks' ) . '</p>'; |
| 79 | }, |
| 80 | $this->page_slug |
| 81 | ); |
| 82 | |
| 83 | add_settings_field( |
| 84 | $this->option_enable_testimonials, |
| 85 | __( 'Enable testimonials', 'frontblocks' ), |
| 86 | array( $this, 'field_enable_testimonials' ), |
| 87 | $this->page_slug, |
| 88 | 'frontblocks_section_features' |
| 89 | ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Sanitize settings array. |
| 94 | * |
| 95 | * @param array $value Raw value. |
| 96 | * @return array |
| 97 | */ |
| 98 | public function sanitize_settings( $value ) { |
| 99 | if ( ! is_array( $value ) ) { |
| 100 | return array(); |
| 101 | } |
| 102 | |
| 103 | $sanitized = array(); |
| 104 | foreach ( $value as $key => $val ) { |
| 105 | if ( $this->option_enable_testimonials === $key ) { |
| 106 | $sanitized[ $key ] = (bool) $val; |
| 107 | } else { |
| 108 | $sanitized[ $key ] = sanitize_text_field( $val ); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return $sanitized; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Render checkbox field for enable testimonials. |
| 117 | * |
| 118 | * @return void |
| 119 | */ |
| 120 | public function field_enable_testimonials() { |
| 121 | $options = get_option( 'frontblocks_settings', array() ); |
| 122 | $enabled = (bool) ( $options[ $this->option_enable_testimonials ] ?? false ); |
| 123 | echo '<label for="' . esc_attr( $this->option_enable_testimonials ) . '">'; |
| 124 | echo '<input type="checkbox" id="' . esc_attr( $this->option_enable_testimonials ) . '" name="frontblocks_settings[' . esc_attr( $this->option_enable_testimonials ) . ']" value="1" ' . checked( true, $enabled, false ) . ' /> '; |
| 125 | echo esc_html__( 'Enable testimonials', 'frontblocks' ); |
| 126 | echo '</label>'; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Render settings page. |
| 131 | * |
| 132 | * @return void |
| 133 | */ |
| 134 | public function render_page() { |
| 135 | if ( ! current_user_can( 'edit_theme_options' ) ) { |
| 136 | return; |
| 137 | } |
| 138 | ?> |
| 139 | <div class="wrap"> |
| 140 | <h1><?php echo esc_html__( 'Frontblocks Settings', 'frontblocks' ); ?></h1> |
| 141 | </div> |
| 142 | |
| 143 | <form method="post" action="options.php" style="margin-top:20px;"> |
| 144 | <?php |
| 145 | settings_fields( 'frontblocks_settings' ); |
| 146 | do_settings_sections( $this->page_slug ); |
| 147 | submit_button(); |
| 148 | ?> |
| 149 | </form> |
| 150 | </div> |
| 151 | <?php |
| 152 | } |
| 153 | } |
| 154 |