settings_sections = $sections; return $this; } function add_section( $section ) { $this->settings_sections[] = $section; return $this; } function set_fields( $fields ) { $this->settings_fields = $fields; return $this; } function add_field( string $section, $field ) { $defaults = [ 'name' => '', 'label' => '', 'desc' => '', 'type' => 'text', ]; $this->settings_fields[ $section ][] = wp_parse_args( $field, $defaults ); return $this; } function admin_init() { foreach ( $this->settings_sections as $section ) { if ( false == get_option( $section['id'] ) ) { add_option( $section['id'] ); } if ( isset( $section['desc'] ) && ! empty( $section['desc'] ) ) { $section['desc'] = '
' . $section['desc'] . '
'; $callback = create_function( '', ' "' . str_replace( '"', '\"', esc_html ($section['desc'] )) . '";' ); } else { $callback = '__return_false'; } add_settings_section( $section['id'], $section['title'], $callback, $section['id'] ); } foreach ( $this->settings_fields as $section => $field ) { foreach ( $field as $option ) { $type = isset( $option['type'] ) ? $option['type'] : 'text'; $args = [ 'id' => $option['name'], 'desc' => isset( $option['desc'] ) ? $option['desc'] : '', 'desc2' => isset( $option['desc2'] ) ? $option['desc2'] : '', 'label_for' => $type != 'html' && isset( $option['name'] ) ? $section . '[' . $option['name'] . ']' : '', 'name' => @$option['label'], 'section' => $section, 'size' => isset( $option['size'] ) ? $option['size'] : null, 'row' => isset( $option['row'] ) ? $option['row'] : '', 'ltr' => isset( $option['ltr'] ) ? $option['ltr'] : false, 'options' => isset( $option['options'] ) ? $option['options'] : '', 'br' => isset( $option['br'] ) ? $option['br'] : '', 'std' => isset( $option['default'] ) ? $option['default'] : '', 'sanitize_callback' => isset( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : '', ]; add_settings_field( $section . '[' . esc_html(sanitize_text_field($option['name'])) . ']', @esc_html(sanitize_text_field($option['label'])), [ $this, 'callback_' . $type, ], $section, $section, $args ); } } foreach ( $this->settings_sections as $section ) { register_setting( $section['id'], $section['id'], [ $this, 'sanitize_options' ] ); } } function callback_text( $args ) { $kses_WooNotifytext = array( 'input' => array( 'type' => array(), 'class' => array(), 'id' => array(), 'name' => array(), 'value' => array(), 'style' => array() ), 'div' => array( 'class' => array(), 'style' => array() ), ); $value = esc_html( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular'; $style = ''; if ( ! empty( $args['ltr'] ) && $args['ltr'] === true ) { $style .= 'text-align:left !important; direction:ltr !important;'; } $html = (sprintf( '',$size,$args['section'],$args['id'],$value , $style )); $html .= (sprintf( '
desc_style)) . '> %s
', $args['desc'] )); echo wp_kses(($html),$kses_WooNotifytext); } function get_option( $option, $section, $default = '' ) { $options = get_option( $section ); if ( isset( $options[ $option ] ) ) { return $options[ $option ]; } return $default; } function callback_checkbox( $args ) { $kses_WooNotifycheckbox = array( 'label' => array( 'for' => array() ), 'input' => array( 'type' => array(), 'class' => array(), 'id' => array(), 'name' => array(), 'value' => array(), 'checked' => array() ), 'div' => array( 'class' => array(), 'style' => array() ), 'p' => array(), ); $value = esc_html( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); $html = (sprintf( '', $args['section'], $args['id'] )); $html .= (sprintf( '',$args['section'], $args['id'], $value, checked( $value, 'on', false ) )); $html .= (sprintf( '', $args['section'], $args['id'], $args['desc'] )); if ( ! empty( $args['desc2'] ) ) { $html .= (sprintf( '
desc_style)) . '> %s
', $args['desc2'] )); } //echo wp_kses($html , $kses_WooNotifycheckbox); echo wp_kses(($html),$kses_WooNotifycheckbox); } function callback_multicheck( $args ) { $kses_WooNotifymulticheck = array( 'input' => array( 'type' => array(), 'class' => array(), 'id' => array(), 'name' => array(), 'value' => array(), 'checked' => array() ), 'label' => array( 'for' => array() ), 'div' => array( 'class' => array(), 'style' => array() ), 'p' => array(), 'br' => array() ); $value = $this->get_option( $args['id'], $args['section'], $args['std'] ); $html = ''; foreach ( $args['options'] as $key => $label ) { $checked = isset( $value[ $key ] ) ? $value[ $key ] : '0'; $html .= (sprintf( '', esc_html( $args['section'] ), esc_html( $args['id'] ), esc_html( $key ), checked( $checked, $key, false ) )); $html .= (sprintf( '
', esc_html( $args['section'] ), esc_html( $args['id'] ), esc_html( $label ), esc_html( $key ) )); } $html .= (sprintf( '
desc_style)) . '> %s
', esc_html( $args['desc'] ) )); echo wp_kses( $html, $kses_WooNotifymulticheck ); } function callback_radio( $args ) { $kses_radio = array( 'label' => array( 'style' => array(), 'style' => array(), 'for' => array() ), 'input' => array( 'type' => array(), 'class' => array(), 'id' => array(), 'name' => array(), 'checked' => array(), 'value' => array() ), 'div' => array( 'class' => array(), 'style' => array() ), 'p' => array(), ); $value = $this->get_option( $args['id'], $args['section'], $args['std'] ); $html = ''; $style = is_rtl() ? 'margin-left:10px;' : 'margin-right:10px;'; foreach ( $args['options'] as $key => $label ) { $html .= (sprintf( ''; if ( ! empty( $args['br'] ) ) { $html .= '

'; } } if ( ! empty( $args['br'] ) ) { $html .= '

'; } $html .= (sprintf( '
desc_style)) . '> %s
', $args['desc'])); //echo wp_kses( $html , $kses_radio ) ; echo wp_kses(($html),$kses_radio); } function callback_select( $args ) { $kses_select = array( 'option' => array( 'value' => array(), 'style' => array(), 'selected' => array() ), 'select' => array( 'class' => array(), 'name' => array(), 'id' => array(), 'dir' => array() ), 'div' => array( 'class' => array(), 'style' => array() ), 'p' => array(), ); $value = esc_html( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'wc-enhanced-select regular'; $ltr = ! empty( $args['ltr'] ) && $args['ltr'] === true ? 'dir="ltr"' : ''; $html = (sprintf( '
' ); $html .= (sprintf( '
desc_style)) . '> %s
', $args['desc'] )); //echo wp_kses($html,$kses_select); echo wp_kses(($html),$kses_select); } function callback_textarea( $args ) { $kses_textarea = array( 'textarea' => array( 'rows' => array(), 'style' => array(), 'class' => array(), 'id' => array(), 'name' => array() ), 'div' => array( 'class' => array(), 'style' => array() ), 'br' => array(), ); $value = esc_textarea( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : 'regular'; $row = ! empty( $args['row'] ) ? $args['row'] : '6'; $style = 'width: 50%'; $html = (sprintf( '', esc_html( $size ), esc_html( $args['section'] ), esc_html( $args['id'] ), esc_textarea( $value ), esc_html( $style ), esc_html( $row ) )); $html .= sprintf( '
%s
', esc_html(sanitize_text_field($this->desc_style)), wp_kses( $args['desc'], $kses_textarea ) ); echo wp_kses( $html, $kses_textarea ); } function callback_html( $args ) { $kses_description = array( 'a' => array( 'href' => array(), 'onclick' => array(), 'style' => array() ), 'div' => array( 'class' => array(), 'style' => array(), ), 'strong' => array(), 'code' => array(), 'br' => array(), ); echo sprintf( ('
%s
'), esc_html( sanitize_text_field($this->desc_style) ), wp_kses( $args['desc'], $kses_description ) ); } function callback_wysiwyg( $args ) { $value = $this->get_option( $args['id'], $args['section'], $args['std'] ); $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? $args['size'] : '500px'; echo '
'; wp_editor( $value, $args['section'] . '-' . $args['id'] . '', array( 'teeny' => true, 'textarea_name' => $args['section'] . '[' . $args['id'] . ']', 'textarea_rows' => 10, ) ); echo '
'; echo (sprintf( ('
%s
'), esc_html(sanitize_text_field($this->desc_style)), wp_kses_post( $args['desc'] ) )); } function callback_file( $args ) { $kses_uploadfile = array( 'input' => array( 'type' => array(), 'class' => array(), 'id' => array(), 'name' => array(), 'value' => array() ), 'div' => array( 'class' => array(), 'style' => array() ), 'br' => array(), ); $value = esc_html(esc_html( $this->get_option( $args['id'], $args['section'], $args['std'] ) )); $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? esc_html(sanitize_text_field($args['size'])) : esc_html('regular'); // Generate the input field for file URL $input_field =(sprintf( '', esc_html( $size ), esc_html( $args['section'] ), esc_html( $args['id'] ), esc_html( $value ) )); // Generate the "Browse" button $browse_button = (sprintf( '', esc_html(__( 'Browse' )) )); // Generate the description $description = ( sprintf( '
%s
', esc_html(sanitize_text_field($this->desc_style)), wp_kses_post( $args['desc'] ) ) ); // Combine all elements into the final HTML $html = $input_field . $browse_button . $description; echo wp_kses( $html, $kses_uploadfile ); } function callback_password( $args ) { $kses_password = array( 'input' => array( 'type' => array(), 'class' => array(), 'id' => array(), 'name' => array(), 'value' => array() ), 'div' => array( 'class' => array(), 'style' => array() ), ); $value = esc_html( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? esc_html(sanitize_text_field($args['size'])) : esc_html('regular'); // Generate the password input field $input_field = (sprintf( '', esc_html( $size ), esc_html( $args['section'] ), esc_html( $args['id'] ), esc_html( $value ) )); // Generate the description $description = (sprintf( '
%s
', esc_html(sanitize_text_field($this->desc_style)), wp_kses_post( $args['desc'] ) )); // Combine all elements into the final HTML $html = $input_field . $description; echo wp_kses( $html, $kses_password ); } function callback_color( $args ) { $kses_color = array( 'input' => array( 'type' => array(), 'class' => array(), 'id' => array(), 'name' => array(), 'data-default-color' => array(), 'value' => array() ), 'div' => array( 'class' => array(), 'style' => array() ), ); $value = esc_html( $this->get_option( $args['id'], $args['section'], $args['std'] ) ); $size = isset( $args['size'] ) && ! is_null( $args['size'] ) ? esc_html(sanitize_text_field($args['size'])) : esc_html('regular'); // Generate the color picker input field $input_field = (sprintf( '', esc_html( $size ), esc_html( $args['section'] ), esc_html( $args['id'] ), esc_html( $value ), esc_html( $args['std'] ) )); // Generate the description $description = (sprintf( '
%s
', esc_html(sanitize_text_field($this->desc_style)), wp_kses_post( $args['desc'] ) )); // Combine all elements into the final HTML $html = $input_field . $description; echo wp_kses( $html, $kses_color ); } function sanitize_options( $options ) { foreach ( $options as $option_slug => $option_value ) { $sanitize_callback = $this->get_sanitize_callback( $option_slug ); if ( $sanitize_callback ) { $options[ $option_slug ] = call_user_func( $sanitize_callback, $option_value ); continue; } } return $options; } function get_sanitize_callback( $slug = '' ) { if ( empty( $slug ) ) { return false; } foreach ( $this->settings_fields as $section => $options ) { foreach ( $options as $option ) { if ( $option['name'] != $slug ) { continue; } return isset( $option['sanitize_callback'] ) && is_callable( $option['sanitize_callback'] ) ? $option['sanitize_callback'] : false; } } return false; } function show_navigation() { $kses_navigation = array( 'nav' => array( 'class' => array() ), 'a' => array( 'href' => array(), 'class' => array(), 'id' => array() ), ); $html = ''; echo wp_kses( $html, $kses_navigation ); } function show_forms() { if ( defined( 'WC_VERSION' ) ) { wp_enqueue_style( 'woocommerce_admin_styles', WC()->plugin_url() . '/assets/css/admin.css', [], WC_VERSION ); wp_enqueue_script( 'wc-enhanced-select' ); } $success_message = ( get_locale() == 'fa_IR' ) ? esc_html('تنظمیات ذخیره شدند') : esc_html('Settings saved'); $updated = isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == 'true'; if ( $updated ) : ?>

settings_sections as $form ) { @$tab_number ++; $display = false; if ( empty( $current_tab ) ) { $display = ( $tab_number == 1 ); } elseif ( stripos( $form['id'], $current_tab ) !== false ) { $display = true; } if ( ! $display ) { continue; } ?>
script(); } function script() { ?>