class-fwp-plugin-base.php
1 year ago
class-fwp-register-hook-base.php
1 year ago
class-fwp-settings-section-base.php
3 years ago
class-fwp-template-tag-base.php
3 years ago
class-fwp-settings-section-base.php
273 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class FWP_Settings_Section_Base_1x0x0 |
| 4 | * |
| 5 | * @package FWP |
| 6 | * @category WordPress Library |
| 7 | * @version 1.0.0 |
| 8 | |
| 9 | * @link https://www.webfactoryltd.com/ |
| 10 | */ |
| 11 | abstract class FWP_Settings_Section_Base_1x0x0 extends WPRun_Base_1x0x0 |
| 12 | { |
| 13 | |
| 14 | /** |
| 15 | * @var array |
| 16 | */ |
| 17 | protected $default_settings = array( |
| 18 | 'section_id' => '', |
| 19 | 'title' => '', |
| 20 | 'description' => '', |
| 21 | 'page_id' => '', |
| 22 | 'option_name' => '', |
| 23 | 'option_group' => '', |
| 24 | 'html_fields_class' => 'FWP_HTML_Fields_1x0x0', |
| 25 | 'fields' => array( |
| 26 | //'key' => array( |
| 27 | // 'label' => '', |
| 28 | // 'class' => '', |
| 29 | // 'default_value' => '', |
| 30 | //), |
| 31 | ), |
| 32 | ); |
| 33 | |
| 34 | /** |
| 35 | * @var array |
| 36 | */ |
| 37 | private $settings = array(); |
| 38 | |
| 39 | /** |
| 40 | * @var array |
| 41 | */ |
| 42 | private $field_errors = array(); |
| 43 | |
| 44 | /** |
| 45 | * @var FWP_HTML_Fields_1x0x0 |
| 46 | */ |
| 47 | private $html_fields = null; |
| 48 | |
| 49 | /** |
| 50 | * @var array |
| 51 | */ |
| 52 | private $option_values = array(); |
| 53 | |
| 54 | /** |
| 55 | * Init |
| 56 | */ |
| 57 | protected function init() |
| 58 | { |
| 59 | $this->set_option_values(); |
| 60 | $this->set_html_fields(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @param string $key |
| 65 | * @return mixed |
| 66 | */ |
| 67 | final public function get_setting( $key ) |
| 68 | { |
| 69 | return $this->settings[ $key ]; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @param array $settings |
| 74 | */ |
| 75 | final protected function set_settings( array $settings ) |
| 76 | { |
| 77 | if ( empty( $this->settings ) ) { |
| 78 | $this->settings = $this->default_settings; |
| 79 | } |
| 80 | |
| 81 | $this->settings = wp_parse_args( $settings, $this->settings ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Action for "admin_init" |
| 86 | */ |
| 87 | protected function action_admin_init() |
| 88 | { |
| 89 | $description = $this->get_setting( 'description' ); |
| 90 | |
| 91 | add_settings_section( |
| 92 | $this->get_setting( 'section_id' ) // id |
| 93 | , $this->get_setting( 'title' ) // title |
| 94 | , function () use ( $description ) { // callback |
| 95 | WPEL_Plugin::wp_kses_wf($description); |
| 96 | } |
| 97 | , $this->get_setting( 'page_id' ) // page id |
| 98 | ); |
| 99 | |
| 100 | register_setting( |
| 101 | $this->get_setting( 'option_group' ) |
| 102 | , $this->get_setting( 'option_name' ) |
| 103 | , $this->get_callback( 'sanitize' ) |
| 104 | ); |
| 105 | |
| 106 | $this->add_fields(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Set option values |
| 111 | */ |
| 112 | private function set_option_values() |
| 113 | { |
| 114 | $saved_values = $this->get_saved_values(); |
| 115 | $default_values = $this->get_default_values(); |
| 116 | |
| 117 | $values = wp_parse_args( $saved_values, $default_values ); |
| 118 | $this->option_values = $values; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get option values |
| 123 | * @return array |
| 124 | */ |
| 125 | final public function get_option_values() |
| 126 | { |
| 127 | return $this->option_values; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get the default option values |
| 132 | * @return array |
| 133 | */ |
| 134 | final public function get_default_values() |
| 135 | { |
| 136 | $fields = $this->get_setting( 'fields' ); |
| 137 | |
| 138 | $default_values = array_map( function ( $arr ) { |
| 139 | if ( ! isset( $arr[ 'default_value' ] ) ) { |
| 140 | return ''; |
| 141 | } |
| 142 | |
| 143 | return $arr[ 'default_value' ]; |
| 144 | }, $fields ); |
| 145 | |
| 146 | return $default_values; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Get saved option values from database |
| 151 | * @return type |
| 152 | */ |
| 153 | final public function get_saved_values() |
| 154 | { |
| 155 | if ( is_network_admin() ) { |
| 156 | $option = get_site_option( $this->get_setting( 'option_name' ) ); |
| 157 | } else { |
| 158 | $option = get_option( $this->get_setting( 'option_name' ) ); |
| 159 | } |
| 160 | |
| 161 | $saved_values = is_array( $option ) ? $option : array(); |
| 162 | return $saved_values; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Create html fields |
| 167 | */ |
| 168 | private function set_html_fields() |
| 169 | { |
| 170 | $option_name = $this->get_setting( 'option_name' ); |
| 171 | |
| 172 | $html_fields_class = $this->get_setting( 'html_fields_class' ); |
| 173 | $this->html_fields = new $html_fields_class( |
| 174 | $this->option_values |
| 175 | , $option_name .'-%s' |
| 176 | , $option_name .'[%s]' |
| 177 | ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * @return FWP_HTML_Fields_1x0x0 |
| 182 | */ |
| 183 | final protected function get_html_fields() |
| 184 | { |
| 185 | return $this->html_fields; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Sanitize settings callback |
| 190 | * @param array $values |
| 191 | * @return array |
| 192 | */ |
| 193 | protected function sanitize( $values ) |
| 194 | { |
| 195 | $old_values = $this->option_values; |
| 196 | |
| 197 | $this->field_errors = array(); |
| 198 | |
| 199 | $new_values = $this->before_update( $values, $old_values ); |
| 200 | |
| 201 | if ( count ( $this->field_errors ) > 0 ) { |
| 202 | add_settings_error( |
| 203 | $this->get_setting( 'option_group' ) |
| 204 | , 'settings_updated' |
| 205 | , implode( '<br>', $this->field_errors ) |
| 206 | , 'error' |
| 207 | ); |
| 208 | } |
| 209 | |
| 210 | return $new_values; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Validate and sanitize user input before saving to databse |
| 215 | * @param array $new_values |
| 216 | * @param array $old_values |
| 217 | * @return array |
| 218 | */ |
| 219 | protected function before_update( array $new_values, array $old_values ) |
| 220 | { |
| 221 | return $new_values; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Add fields |
| 226 | */ |
| 227 | protected function add_fields() |
| 228 | { |
| 229 | $fields = $this->get_setting( 'fields' ); |
| 230 | |
| 231 | foreach ( $fields as $key => $field_settings ) { |
| 232 | $label = isset( $field_settings[ 'label' ] ) ? $field_settings[ 'label' ] : ''; |
| 233 | $class = isset( $field_settings[ 'class' ] ) ? $field_settings[ 'class' ] : ''; |
| 234 | |
| 235 | $label_for_exclude = array('wpel-external-link-settings-rel_noopener', 'wpel-internal-link-settings-rel_noopener', 'wpel-excluded-link-settings-rel_noopener'); |
| 236 | add_settings_field( |
| 237 | $key |
| 238 | , $label |
| 239 | , $this->get_callback( 'field_callback' ) |
| 240 | , $this->get_setting( 'page_id' ) |
| 241 | , $this->get_setting( 'section_id' ) |
| 242 | , array( |
| 243 | 'key' => $key, |
| 244 | 'label_for' => !in_array($this->html_fields->get_field_id( $key ), $label_for_exclude)?$this->html_fields->get_field_id( $key ):'', |
| 245 | 'class' => $class, |
| 246 | ) |
| 247 | ); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Show field callback |
| 253 | * @param array $args |
| 254 | */ |
| 255 | final protected function field_callback( array $args ) |
| 256 | { |
| 257 | $field_method = 'show_'. $args[ 'key' ]; |
| 258 | |
| 259 | if ( is_callable( array( $this, $field_method ) ) ) { |
| 260 | $this->{ $field_method }( $args ); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * @param string $message |
| 266 | */ |
| 267 | final protected function add_error( $message ) |
| 268 | { |
| 269 | $this->field_errors[] = $message; |
| 270 | } |
| 271 | |
| 272 | } |
| 273 |