*/ if ( ! defined( 'ABSPATH' ) ) { exit; // If this file is called directly, abort. } /** * Aarambha Kits Widget. * * Class Base_WP_Widget * * @extends WP_Widget */ abstract class Base_WP_Widget extends WP_Widget { /** * CSS class. * * @var string */ public $widget_cssclass; /** * Widget description. * * @var string */ public $widget_description; /** * Widget ID. * * @var string|bool */ public $widget_id = false; /** * Widget name. * * @var string */ public $widget_name; /** * Settings. * * @var array */ public $settings = array(); /** * Widget Control Options. * * @var array */ public $control_options = array(); /** * Widget customize selective refresh. * * @var bool */ public $customize_selective_refresh = true; /** * Constructor. */ public function __construct() { $widget_options = array( 'classname' => $this->widget_cssclass, 'description' => $this->widget_description, 'customize_selective_refresh' => $this->customize_selective_refresh, ); parent::__construct( $this->widget_id, $this->widget_name, $widget_options, $this->control_options ); } /** * Updates a particular instance of a widget. * * @param array $new_instance New instance. * @param array $old_instance Old instance. * * @return array * @see WP_Widget->update */ public function update( $new_instance, $old_instance ) { $instance = $old_instance; if ( empty( $this->settings ) ) { return $instance; } // Loop settings and get values to save. foreach ( $this->settings as $key => $setting ) { if ( ! isset( $setting['type'] ) ) { continue; } // Format the value based on settings type. switch ( $setting['type'] ) { case 'url': $instance[ $key ] = isset( $new_instance[ $key ] ) ? esc_url_raw( $new_instance[ $key ] ) : $setting['default']; break; case 'textarea': $instance[ $key ] = wp_kses( trim( wp_unslash( $new_instance[ $key ] ) ), wp_kses_allowed_html( 'post' ) ); break; case 'image': /** * Array of valid image file types. * * The array includes image mime types that are included in wp_get_mime_types() */ $mimes = array( 'jpg|jpeg|jpe' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', 'bmp' => 'image/bmp', 'tiff|tif' => 'image/tiff', 'ico' => 'image/x-icon', ); // Return an array with file extension and mime_type. $file = wp_check_filetype( $new_instance[ $key ], $mimes ); // If $new_instance[ $key ] has a valid mime_type, assign it to $instance[ $key ], otherwise, assign empty value to $instance[ $key ]. $instance[ $key ] = $file['ext'] ? $new_instance[ $key ] : $setting['default']; break; case 'checkbox': $instance[ $key ] = ! empty( $new_instance[ $key ] ) ? 1 : 0; break; case 'number': $instance[ $key ] = is_numeric( $new_instance[ $key ] ) ? intval( $new_instance[ $key ] ) : $setting['default']; if ( isset( $setting['input_attrs']['min'] ) && '' !== $setting['input_attrs']['min'] ) { $instance[ $key ] = max( $instance[ $key ], $setting['input_attrs']['min'] ); } if ( isset( $setting['input_attrs']['max'] ) && '' !== $setting['input_attrs']['max'] ) { $instance[ $key ] = min( $instance[ $key ], $setting['input_attrs']['max'] ); } break; case 'radio': case 'select': $new_instance[ $key ] = sanitize_key( $new_instance[ $key ] ); $available_choices = $setting['choices']; $instance[ $key ] = array_key_exists( $new_instance[ $key ], $available_choices ) ? $new_instance[ $key ] : $setting['default']; break; case 'dropdown_categories': $new_instance[ $key ] = ( '-1' == $new_instance[ $key ] ) ? '0' : absint( $new_instance[ $key ] ); $instance[ $key ] = term_exists( $new_instance[ $key ], 'category' ) ? $new_instance[ $key ] : $setting['default']; break; case 'dropdown_tags': $new_instance[ $key ] = absint( $new_instance[ $key ] ); $instance[ $key ] = term_exists( $new_instance[ $key ], 'post_tag' ) ? $new_instance[ $key ] : $setting['default']; break; case 'dropdown_users': $new_instance[ $key ] = absint( $new_instance[ $key ] ); $available_users = array(); $all_author_users = get_users( array( 'who' => 'authors', ) ); foreach ( $all_author_users as $author_user ) { $available_users[ $author_user->ID ] = $author_user->display_name; } $instance[ $key ] = array_key_exists( $new_instance[ $key ], $available_users ) ? $new_instance[ $key ] : $setting['default']; break; case 'checkboxes': $saved_data = array(); $instance[ $key ] = $new_instance[ $key ]; foreach ( $instance[ $key ] as $item => $value ) { $saved_data[ $item ] = isset( $item ) ? 1 : 0; } $instance[ $key ] = $saved_data; break; default: $instance[ $key ] = isset( $new_instance[ $key ] ) ? sanitize_text_field( $new_instance[ $key ] ) : $setting['default']; break; } /** * Sanitize the value of a setting. */ $instance[ $key ] = apply_filters( 'aarambha_kits_widget_settings_sanitize_option', $instance[ $key ], $new_instance, $key, $setting ); } return $instance; } /** * Outputs the settings update form. * * @param array $instance Instance. * * @see WP_Widget->form */ public function form( $instance ) { if ( empty( $this->settings ) ) { return; } foreach ( $this->settings as $key => $setting ) { $value = isset( $instance[ $key ] ) ? $instance[ $key ] : ( isset( $setting['default'] ) ? $setting['default'] : '' ); $field_type = 'text'; if ( isset( $setting['type'] ) ) { $field_type = $setting['type']; } if ( ! isset( $setting['class'] ) ) { $setting['class'] = ''; } if ( ! isset( $setting['css'] ) ) { $setting['css'] = ''; } switch ( $field_type ) { case 'text': ?>
/>
step="" min="" max="" />
$choices_value ) {
if ( 1 !== $count ) {
echo '
';
}
?>
/>
( isset( $setting['show_option_all'] ) ) ? esc_html( $setting['show_option_all'] ) : '', 'name' => $this->get_field_name( $key ), 'selected' => $value, 'class' => 'widefat postform', ) ); ?>
( isset( $setting['show_option_all'] ) ) ? esc_html( $setting['show_option_all'] ) : '', 'name' => $this->get_field_name( $key ), 'selected' => $value, 'taxonomy' => 'post_tag', 'class' => 'widefat postform', ) ); ?>
( isset( $setting['show_option_all'] ) ) ? esc_html( $setting['show_option_all'] ) : '', 'name' => $this->get_field_name( $key ), 'selected' => $value, 'orderby' => 'name', 'order' => 'ASC', 'who' => 'authors', 'class' => 'widefat postform', ) ); ?>
' . wp_kses_post( $text ) . '
'; } /** * Displays the post title within the widgets. */ public function the_title() { ?>