<commit_before><?php
/**
 * Class FWP_Settings_Section_Fields_1x0x0
 *
 * @package  FWP
 * @category WordPress Library
 * @version  1.0.0
 * @author   <NAME>
 * @link     http://www.finewebdev.com
 * @link     https://github.com/freelancephp/WPRun-WordPress-Development
 * @license  Dual licensed under the MIT and GPLv2+ licenses
 */
abstract class FWP_Settings_Section_Fields_1x0x0 extends WPRun_Base_1x0x0
{

    /**
     * @var array
     */
    protected $default_settings = array(
        'section_id'        => '',
        'title'             => '',
        'description'       => '',
        'page_id'           => '',
        'option_name'       => '',
        'option_group'      => '',
        'network_site'      => false,
        'html_fields_class' => 'FWP_HTML_Fields_1x0x0',
        'fields'            => array(
            //'key' => array(
            //    'label'             => '',
            //    'class'             => '',
            //    'default_value'     => '',
            //),
        ),
    );

    /**
     * @var array
     */
    private $field_errors = array();

    /**
     * @var FWP_HTML_Fields_1x0x0
     */
    private $html_fields = null;

    /**
     * @var array
     */
    private $option_values = array();

    
    protected function after_init()
    {
        $this->set_option_values();
        $this->set_html_fields();
    }

    /**
     * Action for "admin_init"
     */
    protected function action_admin_init()
    {
        $description = $this->get_setting( 'description' );
        
        add_settings_section(
            $this->get_setting( 'section_id' )      // id
            , $this->get_setting( 'title' )         // title
            , function () use ( $description ) {    // callback
                echo $description;
            }
            , $this->get_setting( 'page_id' )       // page id
        );

        register_setting(
            $this->get_setting( 'option_group' )
            , $this->get_setting( 'option_name' )
            , $this->get_callback( 'sanitize' )
        );

        $this->add_fields();
    }

    private function set_option_values()
    {
        $saved_values = $this->get_saved_values();
        $default_values = $this->get_default_values();

        $values = wp_parse_args( $saved_values, $default_values );
        $this->option_values = $this->prepare_field_values( $values );
    }

    final public function update_option_values( $values )
    {
        $sanitized_values = $this->sanitize( $values );
        $default_values = $this->get_default_values();

        $update_values = wp_parse_args( $sanitized_values, $default_values );

         if ( true === $this->get_setting( 'network_site' ) ) {
            $updated = update_site_option( $this->get_setting( 'option_name' ), $update_values );
        } else {
            $updated = update_option( $this->get_setting( 'option_name' ), $update_values );
        }

        if ( $updated ) {
            $this->option_values = $values;
        }

        return $updated;
    }

    final public function delete_option_values()
    {
         if ( true === $this->get_setting( 'network_site' ) ) {
            return delete_site_option( $this->get_setting( 'option_name' ) );
        }

        return delete_option( $this->get_setting( 'option_name' ) );
    }

    final public function get_option_values()
    {
        return $this->option_values;
    }

    final public function get_default_values()
    {
        $fields = $this->get_setting( 'fields' );

        $default_values = array_map( function ( $arr ) {
            if ( ! isset( $arr[ 'default_value' ] ) ) {
                return '';
            }

            return $arr[ 'default_value' ];
        }, $fields );

        return $default_values;
    }

    final public function get_saved_values()
    {<commit_msg>prepare values only on public method<commit_after><?php
/**
 * Class FWP_Settings_Section_Fields_1x0x0
 *
 * @package  FWP
 * @category WordPress Library
 * @version  1.0.0
 * @author   <NAME>
 * @link     http://www.finewebdev.com
 * @link     https://github.com/freelancephp/WPRun-WordPress-Development
 * @license  Dual licensed under the MIT and GPLv2+ licenses
 */
abstract class FWP_Settings_Section_Fields_1x0x0 extends WPRun_Base_1x0x0
{

    /**
     * @var array
     */
    protected $default_settings = array(
        'section_id'        => '',
        'title'             => '',
        'description'       => '',
        'page_id'           => '',
        'option_name'       => '',
        'option_group'      => '',
        'network_site'      => false,
        'html_fields_class' => 'FWP_HTML_Fields_1x0x0',
        'fields'            => array(
            //'key' => array(
            //    'label'             => '',
            //    'class'             => '',
            //    'default_value'     => '',
            //),
        ),
    );

    /**
     * @var array
     */
    private $field_errors = array();

    /**
     * @var FWP_HTML_Fields_1x0x0
     */
    private $html_fields = null;

    /**
     * @var array
     */
    private $option_values = array();

    
    protected function after_init()
    {
        $this->set_option_values();
        $this->set_html_fields();
    }

    /**
     * Action for "admin_init"
     */
    protected function action_admin_init()
    {
        $description = $this->get_setting( 'description' );
        
        add_settings_section(
            $this->get_setting( 'section_id' )      // id
            , $this->get_setting( 'title' )         // title
            , function () use ( $description ) {    // callback
                echo $description;
            }
            , $this->get_setting( 'page_id' )       // page id
        );

        register_setting(
            $this->get_setting( 'option_group' )
            , $this->get_setting( 'option_name' )
            , $this->get_callback( 'sanitize' )
        );

        $this->add_fields();
    }

    private function set_option_values()
    {
        $saved_values = $this->get_saved_values();
        $default_values = $this->get_default_values();

        $values = wp_parse_args( $saved_values, $default_values );
        $this->option_values = $values;
    }

    final public function update_option_values( $values )
    {
        $sanitized_values = $this->sanitize( $values );
        $default_values = $this->get_default_values();

        $update_values = wp_parse_args( $sanitized_values, $default_values );

         if ( true === $this->get_setting( 'network_site' ) ) {
            $updated = update_site_option( $this->get_setting( 'option_name' ), $update_values );
        } else {
            $updated = update_option( $this->get_setting( 'option_name' ), $update_values );
        }

        if ( $updated ) {
            $this->option_values = $values;
        }

        return $updated;
    }

    final public function delete_option_values()
    {
         if ( true === $this->get_setting( 'network_site' ) ) {
            return delete_site_option( $this->get_setting( 'option_name' ) );
        }

        return delete_option( $this->get_setting( 'option_name' ) );
    }

    final public function get_option_values( $prepared = true )
    {
        if ( false === $prepared ) {
            return $this->option_values;
        }

        return $this->prepare_field_values( $this->option_values );
    }

    final public function get_default_values()
    {
        $fields = $this->get_setting( 'fields' );

        $default_values = array_map( function ( $arr ) {
            if ( ! isset( $arr[ 'default_value' ] ) ) {
                return '';
            }

            return $arr[ 'default_value' ];
        }, $fields );

        return $default_values;
    }

    final public function get_saved_values()
    {
{'fraction_non_alphanumeric': 0.13620162932790225, 'fraction_numerical': 0.004837067209775967, 'mean_word_length': 1.8060714285714285, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 82, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7487279', 'n_tokens_mistral': 2395, 'n_tokens_neox': 2347, 'n_words': 418}