* @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 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( [ '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 = [ __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_number( $args ) { $args['input_class'] = ! empty( $args['input_class'] ) ? $args['input_class'] : 'small-text'; $args['type'] = 'number'; self::field_tooltip( $args ); self::settings_field_text( $args ); } 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 ), [ 'br' => [], 'em' => [], 'strong' => [], 'small' => [], 'span' => [], 'ul' => [], 'li' => [], 'ol' => [], 'p' => [], 'a' => [], ] ) ); } public static function settings_field_text( $args ) { $class = ! empty( $args['input_class'] ) ? $args['input_class'] : 'regular-text'; $type = ! empty( $args['type'] ) ? $args['type'] : 'text'; ?> /> $value ) { $result .= sprintf( ' %s="%s"', sanitize_key( $att ), esc_attr( $value ) ); } return $result; } private static function field_description( $args ) { if ( ! empty( $args['desc'] ) ) { echo '
' . $args['desc'] . '
'; } } public static function settings_field_textarea( $args ) { $class = ! empty( $args['input_class'] ) ? $args['input_class'] : 'large-text'; $rows = isset( $args['rows'] ) ? absint( $args['rows'] ) : 4; ?> />