Settings.php
167 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 | if ( ! frbl_is_pro_active() ) { |
| 92 | add_settings_section( |
| 93 | 'frontblocks_section_woocommerce_features', |
| 94 | __( 'WooCommerce Features', 'frontblocks' ), |
| 95 | function () { |
| 96 | echo '<p>' . esc_html__( 'WooCommerce FrontBlocks PRO is a premium plugin that adds more features to WooCommerce FrontBlocks. Customize your WooCommerce store with more features.', 'frontblocks' ) . '</p>'; |
| 97 | echo '<p><a href="https://close.technology/wordpress-plugins/front-blocks-pro/?ref=WordPressPlugin" target="_blank" class="button button-secondary">' . esc_html__( 'Buy WooCommerce FrontBlocks PRO', 'frontblocks' ) . '</a></p>'; |
| 98 | }, |
| 99 | $this->page_slug |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | do_action( 'frontblocks_register_settings' ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Render settings page. |
| 108 | * |
| 109 | * @return void |
| 110 | */ |
| 111 | public function render_page() { |
| 112 | if ( ! current_user_can( 'edit_theme_options' ) ) { |
| 113 | return; |
| 114 | } |
| 115 | ?> |
| 116 | <div class="wrap"> |
| 117 | <h1><?php echo esc_html__( 'Frontblocks Settings', 'frontblocks' ); ?></h1> |
| 118 | |
| 119 | <form method="post" action="options.php" style="margin-top:20px;"> |
| 120 | <?php |
| 121 | settings_fields( 'frontblocks_settings' ); |
| 122 | do_settings_sections( $this->page_slug ); |
| 123 | submit_button(); |
| 124 | ?> |
| 125 | </form> |
| 126 | </div> |
| 127 | <?php |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Render checkbox field for enable testimonials. |
| 132 | * |
| 133 | * @return void |
| 134 | */ |
| 135 | public function field_enable_testimonials() { |
| 136 | $options = get_option( 'frontblocks_settings', array() ); |
| 137 | $enabled = (bool) ( $options[ $this->option_enable_testimonials ] ?? false ); |
| 138 | echo '<label for="' . esc_attr( $this->option_enable_testimonials ) . '">'; |
| 139 | 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 ) . ' /> '; |
| 140 | echo esc_html__( 'Enable testimonials', 'frontblocks' ); |
| 141 | echo '</label>'; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Sanitize settings array. |
| 146 | * |
| 147 | * @param array $value Raw value. |
| 148 | * @return array |
| 149 | */ |
| 150 | public function sanitize_settings( $value ) { |
| 151 | if ( ! is_array( $value ) ) { |
| 152 | return array(); |
| 153 | } |
| 154 | |
| 155 | $sanitized = array(); |
| 156 | foreach ( $value as $key => $val ) { |
| 157 | if ( $this->option_enable_testimonials === $key ) { |
| 158 | $sanitized[ $key ] = (bool) $val; |
| 159 | } else { |
| 160 | $sanitized[ $key ] = sanitize_text_field( $val ); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return $sanitized; |
| 165 | } |
| 166 | } |
| 167 |