mage-eventpress/Admin/settings/global/MAGE_Setting_API.php

1 line
22 KiB
PHP
Raw Normal View History

2023-08-29 02:17:31 -04:00
<?php /** * MagePeople Settings API wrapper class */ if ( !class_exists( 'MAGE_Setting_API' ) ): class MAGE_Setting_API { /** * settings sections array * * @var array */ protected $settings_sections = array(); /** * Settings fields array * * @var array */ protected $settings_fields = array(); public function __construct() { add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); } /** * Enqueue scripts and styles */ function admin_enqueue_scripts() { wp_enqueue_style( 'wp-color-picker' ); wp_enqueue_media(); wp_enqueue_script( 'wp-color-picker' ); wp_enqueue_script( 'jquery' ); } /** * Set settings sections * * @param array $sections setting sections array */ function set_sections( $sections ) { $this->settings_sections = $sections; return $this; } /** * Add a single section * * @param array $section */ function add_section( $section ) { $this->settings_sections[] = $section; return $this; } /** * Set settings fields * * @param array $fields settings fields array */ function set_fields( $fields ) { $this->settings_fields = $fields; return $this; } function add_field( $section, $field ) { $defaults = array( 'name' => '', 'label' => '', 'desc' => '', 'type' => 'text' ); $arg = wp_parse_args( $field, $defaults ); $this->settings_fields[$section][] = $arg; return $this; } /** * Initialize and registers the settings sections and fileds to WordPress * * Usually this should be called at `admin_init` hook. * * This function gets the initiated settings sections and fields. Then * registers them to WordPress and ready for use. */ function admin_init() { //register settings sections foreach ( $this->settings_sections as $section ) { if ( false == get_option( $section['id'] ) ) { add_option( $section['id'] ); } if ( isset($section['desc']) && !empty($section['desc']) ) { $section['desc'] = '<div class="inside">' . $section['desc'] . '</div>'; $callback = function() use ( $section ) { echo str_replace( '"', '\"', esc_html($section['desc']) ); }; } else if ( isset( $section['callback'] ) ) { $callback = $section['callback']; } else { $callback = null; } add_settings_section( $section['id'], $section['title'], $callback, $section['id'] ); } //register settings fields foreach ( $this->settings_fields as $section => $field ) { foreach ( $field as $option ) { $name = $option['name']; $type = isset( $option['type'] ) ? $option['type'] : 'text'; $label = isset( $option['label'] ) ? $option['label'] : ''; $callback = isset( $option['callback'] ) ? $option['callback'] : array( $this, 'callback_' . $type ); $args = array( 'id' => $name, 'class' => isset( $option['class'] ) ? $option['class'] : $name, 'label_for' => "{$section}[{$name}]", 'desc' => isset( $option['desc'] ) ? $option['desc'] : '', 'name' => $label, 'section' => $section, 'size' => isset( $option['size'] ) ? $option['size'] : null, 'options' => isset( $option['options'] ) ? $option['options'] : '', 'std' => isset( $option['default'] ) ? $option['default'] : '', 'sanitize_callback' => isset( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : '', 'type' => $type, 'placeholder' => isset( $option['placeholder'] ) ? $option['placeholder'] : '', 'min' => isset( $option['min'] ) ? $option['min'] : '', 'max' => isset( $option['max'] ) ? $option['max'] : '',