$label ) { if ( ! empty( $registered_settings[ $id ] ) ) { $tabs[ $id ] = array( 'label' => $label, 'active' => $id == $active_tab, 'url' => add_query_arg( array( 'settings-updated' => false, 'tab' => $id, ) ), ); } } } return $tabs; } /** * Check for the active / current tab. * * @return bool|string */ public static function active_tab() { static $active; if ( ! isset( $active ) ) { $active = false; foreach ( static::tabs() as $id => $tab ) { if ( $tab['active'] ) { $active = $id; } } } return $active; } /** * Retrieve the array of plugin settings * * @since 1.0.0 * @return array */ public static function registered_settings() { static $settings; if ( ! isset( $settings ) ) { /** * 'Whitelisted' settings, filters are provided for each settings. */ $settings = array( /** General Settings */ 'restrictions' => apply_filters( static::$_prefix . 'settings_restrictions', array( 'restrictions' => array( 'id' => 'restrictions', 'type' => 'hook', ), ) ), 'general' => apply_filters( static::$_prefix . 'settings_general', array( 'default_denial_message' => array( 'id' => 'default_denial_message', 'label' => __( 'Default Denial Message', 'content-control' ), 'type' => 'rich_editor', ), ) ), /** Addon Settings */ //'addons' => apply_filters( static::$_prefix . 'settings_addons', array() ), //'licenses' => apply_filters( static::$_prefix . 'settings_licenses', array() ), /** Misc Settings */ 'misc' => apply_filters( static::$_prefix . 'settings_misc', array() ), ); $settings = apply_filters( static::$_prefix . 'registered_settings', $settings ); } return $settings; } public static function register_settings() { if ( false == get_option( static::$_prefix . 'settings' ) ) { add_option( static::$_prefix . 'settings' ); } foreach ( static::registered_settings() as $tab => $settings ) { $page = static::$_prefix . 'settings_' . $tab; add_settings_section( $page, __return_null(), '__return_false', $page ); foreach ( $settings as $id => $option ) { $name = isset( $option['label'] ) ? $option['label'] : ''; if ( method_exists( '\\JP\CC\Admin\Setting_Callbacks', $option['type'] ) ) { $callback = array( '\\JP\CC\Admin\Setting_Callbacks', $option['type'] ); } elseif ( function_exists( static::$_prefix . $option['type'] . '_callback' ) ) { $callback = static::$_prefix . $option['type'] . '_callback'; } else { $callback = array( '\\JP\CC\Setting_Callbacks', 'missing_callback' ); } add_settings_field( static::$_prefix . 'settings_' . $option['id'], $name, $callback, $page, $page, array( 'section' => $tab, 'id' => isset( $option['id'] ) ? $option['id'] : $id, 'desc' => ! empty( $option['desc'] ) ? $option['desc'] : '', // TODO replace the hardcoded names in Setting_Callbacks with this value. 'name' => isset( $option['name'] ) ? $option['name'] : null, 'size' => isset( $option['size'] ) ? $option['size'] : null, 'options' => isset( $option['options'] ) ? $option['options'] : '', 'std' => isset( $option['std'] ) ? $option['std'] : '', 'min' => isset( $option['min'] ) ? $option['min'] : null, 'max' => isset( $option['max'] ) ? $option['max'] : null, 'step' => isset( $option['step'] ) ? $option['step'] : null, 'chosen' => isset( $option['chosen'] ) ? $option['chosen'] : null, 'placeholder' => isset( $option['placeholder'] ) ? $option['placeholder'] : null, 'allow_blank' => isset( $option['allow_blank'] ) ? $option['allow_blank'] : true, 'readonly' => isset( $option['readonly'] ) ? $option['readonly'] : false, ) ); } } // Creates our settings in the options table register_setting( static::$_prefix . 'settings', static::$_prefix . 'settings', array( __CLASS__, 'settings_sanitize' ) ); } public static function settings_sanitize( $input ) { if ( empty( $_POST['_wp_http_referer'] ) ) { return $input; } $current_values = Options::get_all(); parse_str( $_POST['_wp_http_referer'], $referrer ); $prefix = static::$_prefix; $tab = isset( $referrer['tab'] ) ? $referrer['tab'] : static::active_tab(); $settings = static::registered_settings(); $input = $input ? $input : array(); // Apply a filter per tab like jp_cc_settings_general_sanitize $input = apply_filters( "{$prefix}settings_{$tab}_sanitize", $input ); // Loop through each setting being saved and pass it through a sanitization filter foreach ( $input as $key => $value ) { // Field id specific filter $value = apply_filters( "{$prefix}settings_sanitize_field_{$key}", $value ); // Get the setting type (checkbox, select, etc) $type = isset( $settings[ $tab ][ $key ]['type'] ) ? $settings[ $tab ][ $key ]['type'] : false; if ( $type ) { // Field type specific filter $value = apply_filters( "{$prefix}settings_sanitize_{$type}", $value, $key ); } // General filter $input[ $key ] = apply_filters( "{$prefix}settings_sanitize", $value, $key ); } // Loop through the whitelist and unset any that are empty for the tab being saved if ( ! empty( $settings[ $tab ] ) ) { foreach ( $settings[ $tab ] as $key => $value ) { // settings used to have numeric keys, now they have keys that match the option ID. This ensures both methods work if ( is_numeric( $key ) ) { $key = $value['id']; } if ( empty( $input[ $key ] ) ) { unset( $current_values[ $key ] ); } } } // Merge our new settings with the existing $output = array_merge( $current_values, $input ); add_settings_error( 'jp-cc-notices', '', __( 'Settings updated.', 'content-control' ), 'updated' ); return $output; } public static function sanitize_restrictions( $restrictions = array() ) { if ( ! empty( $restrictions ) ) { foreach ( $restrictions as $key => $restriction ) { if ( is_string( $restriction ) ) { try { $restriction = json_decode( $restriction ); } catch ( \Exception $e ) {}; } $restrictions[ $key ] = Helpers::object_to_array( $restriction ); } } return $restrictions; } /** * Sanitize text fields * * @param array $input The field value * * @return string $input Sanitizied value */ public static function sanitize_text_field( $input ) { return trim( $input ); } }