option_group, $this->option_name ); } /** * Render fields. * * @param array $section ID of the section to render fields for. * @param array $fields List of fields to render. * * @return bool True on success, false on failure. */ public function render_fields( $section, $fields ) { if ( empty( $fields ) ) { return false; } add_settings_section( $section['id'], $section['name'], isset( $section['callback'] ) ? $section['callback'] : null, $this->page_slug ); foreach ( $fields as $field ) { $args = isset( $field['args'] ) ? $field['args'] : []; if ( ! isset( $args['label_for'] ) ) { $args['label_for'] = $field['id']; } if ( isset( $field['type'] ) && ! isset( $args['type'] ) ) { $args['type'] = $field['type']; } if ( ! isset( $args['description'] ) ) { $args['description'] = $field['description']; } if ( isset( $field['options'] ) ) { $args['options'] = $field['options']; } if ( isset( $field['before'] ) ) { $args['before'] = $field['before']; } if ( isset( $field['after'] ) ) { $args['after'] = $field['after']; } add_settings_field( $field['id'], $field['title'], [ $this, 'page_html' ], $this->page_slug, $section['id'], $args ); } return true; } /** * Helper to render select. * * @param array $args List of passed arguments. * * @return string */ public function input_select( $args ) { $for = $args['for']; $name = $args['name']; $options = $args['options']; $options_html = ''; foreach ( $options as $key => $value ) { $option_value = $this->get_option( $for ); $selected = isset( $this->get_options()[ $for ] ) ? ( selected( $option_value, $key, false ) ) : ( '' ); $options_html .= <<$value EOL; } return <<$options_html EOL; } /** * Helper to render checkbox. * * @param array $args List of passed arguments. * * @return string */ public function input_checkbox( $args ) { $for = $args['for']; $name = $args['name']; $value = $args['value']; $title = $args['title']; $description = $args['description']; $checked_attribute = $value !== null ? 'checked="checked"' : ''; return <<

$description

EOL; } /** * Helper to render input color. * * @param array $args List of passed arguments. * * @return string */ public function input_color( $args ) { $for = $args['for']; $name = $args['name']; $value = $args['value']; return << EOL; } /** * Helper to render input text. * * @param array $args List of passed arguments. * * @return string */ public function input_text( $args ) { $type = $args['type']; $for = $args['for']; $name = $args['name']; $value = $args['value']; return << EOT; } /** * Helper to render textarea. * * @param array $args List of passed arguments. * * @return string */ public function input_textarea( $args ) { $for = $args['for']; $name = $args['name']; $value = $args['value']; return <<$value EOT; } /** * top level menu: * callback functions * * @param bool $wrapper Whether to wrap for with header or not. */ public function page_html( $wrapper = true ) { if ( ! current_user_can( 'manage_options' ) ) { return; } if ( isset( $_GET['settings-updated'] ) ) { add_settings_error( $this->alert_key, 'anycomment_message', __( 'Settings Saved', 'anycomment' ), 'updated' ); } settings_errors( $this->alert_key ); ?>

option_group ); $this->do_tab_sections( $this->page_slug ); submit_button( __( 'Save', 'anycomment' ) ); ?>
'; $i = 0; foreach ( (array) $wp_settings_sections[ $page ] as $section ) { $activeClass = $i === 0 ? 'class="current"' : ''; echo '
  • ' . $section['title'] . '
  • '; $i ++; } echo ''; ?> {$section['title']}"; } if ( $includeHeader && $section['callback'] ) { call_user_func( $section['callback'], $section ); } if ( ! isset( $wp_settings_fields ) || ! isset( $wp_settings_fields[ $page ] ) || ! isset( $wp_settings_fields[ $page ][ $section['id'] ] ) ) { continue; } echo '
    '; echo '
    '; $this->do_settings_fields( $page, $section['id'] ); echo '
    '; echo '
    '; $i ++; } } /** * Print out the settings fields for a particular settings section * * Part of the Settings API. Use this in a settings page to output * a specific section. Should normally be called by do_settings_sections() * rather than directly. * * @global $wp_settings_fields Storage array of settings fields and their pages/sections * * @since 2.7.0 * * @param string $page Slug title of the admin page who's settings fields you want to show. * @param string $section Slug title of the settings section who's fields you want to show. */ public function do_settings_fields( $page, $section ) { global $wp_settings_fields; if ( ! isset( $wp_settings_fields[ $page ][ $section ] ) ) { return; } foreach ( (array) $wp_settings_fields[ $page ][ $section ] as $field ) { echo $this->do_field( $field ); } } public function do_field( $field ) { $html = ''; $args = $field['args']; $type = trim( $args['type'] ); $field_data = [ 'type' => $type, 'for' => esc_attr( $args['label_for'] ), 'title' => $field['title'], 'description' => isset( $args['description'] ) ? trim( $args['description'] ) : '', 'value' => $this->get_option( $args['label_for'] ), 'name' => $this->option_name . '[' . $args['label_for'] . ']', 'options' => isset( $args['options'] ) ? $args['options'] : null ]; $label = ''; $description = ! empty( $field_data['description'] ) ? '

    ' . $field_data['description'] . '

    ' : ''; $html .= '
    '; if ( isset( $args['before'] ) ) { $html .= is_callable( $args['before'] ) ? call_user_func( $args['before'] ) : $args['before']; } switch ( $type ) { case 'text': case 'number': case 'numeric': $html .= $label; $html .= $description; $html .= $this->input_text( $field_data ); break; case 'toggle': case 'checkbox': $html .= $this->input_checkbox( $field_data ); break; case 'dropdown': case 'list': case 'select': if ( isset( $field_data['options'] ) ) { $html .= $label; $html .= $description; $html .= $this->input_select( $field_data ); } break; case 'textarea': $html .= $label; $html .= $description; $html .= $this->input_textarea( $field_data ); break; case 'color': $html .= $label; $html .= $description; $html .= $this->input_color( $field_data ); break; default: } if ( isset( $args['after'] ) ) { $html .= is_callable( $args['after'] ) ? call_user_func( $args['after'] ) : $args['after']; } $html .= '
    '; return $html; } /** * Check whether there are any options set on model. * * @return bool */ public function has_options() { $options = $this->get_options(); if ( $options === null ) { return false; } $nonEmptyCount = 0; foreach ( $options as $key => $optionValue ) { if ( ! empty( $optionValue ) ) { $nonEmptyCount ++; } } return $nonEmptyCount > 0; } /** * Get single option. * * @param string $name Options name to search for. * * @return mixed|null */ public function get_option( $name ) { $options = $this->get_options(); $optionValue = isset( $options[ $name ] ) ? trim( $options[ $name ] ) : null; return ! empty( $optionValue ) ? $optionValue : null; } /** * Get list of social options. * @return array|null */ public function get_options() { if ( $this->options === null ) { $this->options = get_option( $this->option_name, null ); } if ( ! empty( $this->default_options ) ) { foreach ( $this->default_options as $key => $optionValue ) { $setDefault = ! isset( $this->options[ $key ] ) && ! strpos( $key, 'toggle' ) || isset( $this->options[ $key ] ) && empty( $this->options[ $key ] ); if ( $setDefault ) { $this->options[ $key ] = $optionValue; } } } return $this->options; } /** * Get instance of currently running class. * @return self */ public static function instance() { $className = get_called_class(); if ( ! isset( self::$_instances[ $className ] ) ) { self::$_instances[ $className ] = new $className( false ); } return self::$_instances[ $className ]; } } endif;