* @license GPL-3.0 * @copyright Barn2 Media Ltd * @version 1.4 */ class Settings_API_Helper implements Registerable, Conditional { /** * @var Plugin The plugin object. */ private $plugin; /** * @var Settings_Scripts Responsible for registering any additional settings scripts. */ private $scripts; public function __construct( Plugin $plugin ) { $this->plugin = $plugin; $this->scripts = new Settings_Scripts( $plugin ); } public function is_required() { return Util::is_admin(); } public function register() { $this->scripts->register(); } public static function add_settings_section( $section, $page, $title, $description_callback, $settings = false ) { if ( ! is_callable( $description_callback ) ) { $description_callback = '__return_false'; } add_settings_section( $section, $title, $description_callback, $page ); self::add_settings_fields( $settings, $section, $page ); } public static function add_settings_fields( $settings, $section, $page ) { if ( ! $settings || ! is_array( $settings ) ) { return; } foreach ( $settings as $setting ) { if ( ! is_array( $setting ) || empty( $setting['id'] ) ) { continue; } $args = wp_parse_args( $setting, array_fill_keys( array( 'id', 'type', 'desc', 'label', 'title', 'class', 'field_class', 'default', 'suffix', 'custom_attributes' ), '' ) ); $args['input_class'] = $args['class']; unset( $args['class'] ); $args['class'] = $args['field_class']; $args['label_for'] = $args['id']; $setting_callback = array( __CLASS__, 'settings_field_' . $args['type'] ); if ( is_callable( $setting_callback ) ) { add_settings_field( $args['id'], $args['title'], $setting_callback, $page, $section, $args ); } } } public static function settings_field_text( $args ) { $class = ! empty( $args['input_class'] ) ? $args['input_class'] : 'regular-text'; $type = ! empty( $args['type'] ) ? $args['type'] : 'text'; ?> />
$label ) : ?>
$option ) : $current_value = self::get_value( sprintf( '%1$s[%2$s]', $args['id'], $value ), $args['default'][$value] ); ?>
/>
0, 'size' => 4 ], $args['custom_attributes'] ); $size_attributes = self::get_custom_attributes( $args ); ?>
/>
' . $args['desc'] . '

'; } } private static function field_tooltip( $args ) { if ( ! empty( $args['desc_tip'] ) ) { wp_enqueue_script( 'barn2-tiptip' ); $tip = self::sanitize_tooltip( $args['desc_tip'] ); echo ''; } } private static function sanitize_tooltip( $content ) { return htmlspecialchars( wp_kses( html_entity_decode( $content ), array( 'br' => [], 'em' => [], 'strong' => [], 'small' => [], 'span' => [], 'ul' => [], 'li' => [], 'ol' => [], 'p' => [], 'a' => [], ) ) ); } private static function custom_attributes( $args ) { echo self::get_custom_attributes( $args ); } private static function get_custom_attributes( $args ) { if ( empty( $args['custom_attributes'] ) ) { return ''; } $custom_atts = $args['custom_attributes']; $result = ''; foreach ( $custom_atts as $att => $value ) { $result .= sprintf( ' %s="%s"', sanitize_key( $att ), esc_attr( $value ) ); } return $result; } private static function get_value( $option, $default = false ) { $value = ''; $matches = array(); $subkey_match = preg_match( '/(\w+)\[(\w+)\]/U', $option, $matches ); if ( $subkey_match && isset( $matches[1], $matches[2] ) ) { $subkey = $matches[2]; $parent_option = get_option( $matches[1], array() ); $value = isset( $parent_option[$subkey] ) ? $parent_option[$subkey] : $default; } else { $value = get_option( $option, $default ); } return $value; } }