* @copyright Copyright (c) 2018, Chris Baldelomar * @link https://webplantmedia.com/product/brimstone-wordpress-theme/ * @license http://www.gnu.org/licenses/gpl-2.0.html */ /** * Class: Widget base. * * @since Brimstone 1.01 * * @see WP_Widget */ class Brimstone_Widget extends WP_Widget { public $widget_description; public $widget_id; public $widget_name; public $settings; public $control_ops; public $selective_refresh = true; /** * __construct * * @since Brimstone 1.01 * * @return void */ public function __construct() { $widget_ops = array( 'classname' => $this->widget_id, 'description' => $this->widget_description, 'customize_selective_refresh' => $this->selective_refresh, ); parent::__construct( $this->widget_id, $this->widget_name, $widget_ops, $this->control_ops ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); add_action( 'wp_ajax_brimstone_post_lookup', array( &$this, 'post_lookup_callback' ) ); add_action( 'wp_ajax_brimstone_page_list_refresh', array( &$this, 'page_list_refresh' ) ); } /** * Echo post title and id for ajax request. Used in widget for searching * for post by title. * * @since Brimstone 1.01 * * @return void */ public function page_list_refresh() { global $wpdb; /* get access to the WordPress database object variable. */ if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'brimstone-admin-page-list-refresh' ) ) { die( __( 'Security check', 'brimstone' ) ); } // get names of all businesses. $page_value = stripslashes( sanitize_text_field( $_POST['value'] ) ); $this->the_pages_options_list( $page_value ); die(); /* stop "0" from being output. */ } /** * Echo post title and id for ajax request. Used in widget for searching * for post by title. * * @since Brimstone 1.01 * * @return void */ public function post_lookup_callback() { global $wpdb; /* get access to the WordPress database object variable. */ if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['_wpnonce'] ), 'brimstone-admin-post-select' ) ) { die( __( 'Security check', 'brimstone' ) ); } // get names of all businesses. $request = '%' . $wpdb->esc_like( stripslashes( sanitize_text_field( $_POST['request'] ) ) ) . '%'; /* escape for use in LIKE statement. */ $post_type = stripslashes( sanitize_text_field( $_POST['post_type'] ) ); $results = $wpdb->get_results( $wpdb->prepare( "select ID, post_title from $wpdb->posts where post_title like %s and post_type=%s and post_status='publish' order by post_title ASC limit 0,30 ", $request, $post_type ) ); // copy the business titles to a simple array. $titles = array(); $i = 0; foreach ( $results as $r ) { $titles[ $i ]['label'] = $r->post_title . ' (' . $r->ID . ')'; $titles[ $i ]['value'] = $r->ID; $i++; } if ( empty( $titles ) ) { $titles[0]['label'] = sprintf( __( 'No results found in post type "%s".', 'brimstone' ), $post_type ); $titles[0]['value'] = '0'; } echo wp_json_encode( $titles ); /* encode into JSON format and output. */ die(); /* stop "0" from being output. */ } /** * Enqueue Scripts * * @since Brimstone 1.01 * * @param string $hook_suffix * @return void */ public function enqueue_scripts( $hook_suffix ) { if ( 'widgets.php' !== $hook_suffix ) { return; } wp_enqueue_style( 'wp-color-picker' ); wp_enqueue_style( 'brimstone-admin-widgets', get_parent_theme_file_uri() . '/css/admin/admin-widgets.css', array(), BRIMSTONE_VERSION ); wp_enqueue_script( 'wp-color-picker' ); wp_enqueue_script( 'jquery-ui' ); wp_enqueue_script( 'jquery-ui-autocomplete' ); wp_enqueue_script( 'jquery-ui-accordion' ); wp_enqueue_script( 'brimstone-admin-widgets', get_template_directory_uri() . '/js/admin/admin-widgets.js', array(), BRIMSTONE_VERSION, true ); wp_enqueue_script( 'brimstone-post-select', get_template_directory_uri() . '/js/admin/admin-post-select.js', array(), BRIMSTONE_VERSION, true ); wp_enqueue_script( 'brimstone-page-refresh', get_template_directory_uri() . '/js/admin/admin-page-refresh.js', array(), BRIMSTONE_VERSION, true ); } /** * Sanitize options. * * @since Brimstone 1.01 * * @param array $instance * @return array */ public function sanitize( $instance ) { if ( ! $this->settings ) { return $instance; } if ( isset( $instance['repeater'] ) && is_array( $instance['repeater'] ) ) { $repeater_instances = $instance['repeater']; unset( $instance['repeater'] ); // turn on to test default widget settings. /* $repeater_instances = $this->settings['repeater']['default']; */ } else { if ( isset( $this->settings['repeater']['default'] ) ) { $repeater_instances = $this->settings['repeater']['default']; } else { $repeater_instances[1] = array(); } } foreach ( $this->settings as $key => $setting ) { if ( 'panels' === $key ) { foreach ( $setting as $panel ) { foreach ( $panel['fields'] as $panel_field_key => $panel_field_setting ) { $value = $this->default_sanitize_value( $panel_field_key, $instance, $panel_field_setting ); $instance[ $panel_field_key ] = $this->sanitize_instance( $panel_field_setting, $value, 'display' ); } } } elseif ( 'repeater' === $key ) { foreach ( $repeater_instances as $repeater_count => $repeater_instance ) { foreach ( $setting['fields'] as $repeater_field_key => $repeater_field_setting ) { $value = $this->default_sanitize_value( $repeater_field_key, $repeater_instance, $repeater_field_setting ); $instance['repeater'][ $repeater_count ][ $repeater_field_key ] = $this->sanitize_instance( $repeater_field_setting, $value, 'display' ); } } } else { $value = $this->default_sanitize_value( $key, $instance, $setting ); // turn on to test default widget settings. /* $value = $setting['std']; */ $instance[ $key ] = $this->sanitize_instance( $setting, $value, 'display' ); } } return $instance; } /** * Check if default value needs to be returned. * * @since Brimstone 1.01 * * @param string $key * @param array $instance * @param array $setting * @return array */ public function default_sanitize_value( $key, $instance, $setting ) { if ( array_key_exists( $key, $instance ) ) { return $instance[ $key ]; } else { return $setting['std']; } } /** * Properly save user input. * * @since Brimstone 1.01 * * @param string $key * @param array $instance * @param array $setting * @return mixed */ public function default_update_value( $key, $instance, $setting ) { if ( array_key_exists( $key, $instance ) ) { return $instance[ $key ]; } else { if ( 'checkbox' === $setting['type'] ) { return 0; } else { return $setting['std']; } } } /** * Update * * @since Brimstone 1.01 * * @param array $new_instance * @param array $old_instance * @return array */ public function update( $new_instance, $old_instance ) { $instance = array(); $repeater_count = 0; if ( ! $this->settings ) { return $instance; } if ( isset( $new_instance['repeater'] ) && is_array( $new_instance['repeater'] ) ) { $repeater_instances = $new_instance['repeater']; unset( $new_instance['repeater'] ); } else { if ( isset( $this->settings['repeater']['default'] ) ) { $repeater_instances = $this->settings['repeater']['default']; } else { $repeater_instances[1] = array(); } } foreach ( $this->settings as $key => $setting ) { if ( 'panels' === $key ) { foreach ( $setting as $panel ) { foreach ( $panel['fields'] as $panel_field_key => $panel_field_setting ) { $value = $this->default_update_value( $panel_field_key, $new_instance, $panel_field_setting ); $instance[ $panel_field_key ] = $this->sanitize_instance( $panel_field_setting, $value ); } } } elseif ( 'repeater' === $key ) { foreach ( $repeater_instances as $repeater_instance ) { $repeater_count++; foreach ( $setting['fields'] as $repeater_field_key => $repeater_field_setting ) { $value = $this->default_update_value( $repeater_field_key, $repeater_instance, $repeater_field_setting ); $instance['repeater'][ $repeater_count ][ $repeater_field_key ] = $this->sanitize_instance( $repeater_field_setting, $value ); } } } else { $value = $this->default_update_value( $key, $new_instance, $setting ); $instance[ $key ] = $this->sanitize_instance( $setting, $value ); } } return $instance; } /** * Sanitize Instance * * @since Brimstone 1.01 * * @param array $setting * @param mixed $new_value * @param string $action * @return mixed */ public function sanitize_instance( $setting, $new_value, $action = 'update' ) { if ( ! isset( $setting['sanitize'] ) ) { return $new_value; } $value = ''; switch ( $setting['sanitize'] ) { case 'html': $value = wp_kses_post( $new_value ); break; case 'multicheck': $value = maybe_serialize( $new_value ); break; case 'checkbox': $value = 1 === intval( $new_value ) ? 1 : 0; break; case 'text': $value = sanitize_text_field( $new_value ); break; case 'absint': $value = absint( $new_value ); break; case 'number': $value = intval( $new_value ); break; case 'number_blank': if ( '' === $new_value ) { $value = ''; } else { $value = intval( $new_value ); } break; case 'color': $value = sanitize_hex_color( $new_value ); break; case 'url': $value = esc_url_raw( $new_value ); if ( 'display' === $action ) { $value = $this->sanitize_url_for_customizer( $new_value ); } break; case 'background_size': $value = $this->sanitize_background_size( $new_value ); break; case 'woocommerce_image_sizes': $value = $this->sanitize_woocommerce_image_sizes( $new_value ); break; case 'ids': case 'post_ids': $value = $this->sanitize_ids( $new_value ); break; case 'slugs': $value = $this->sanitize_slugs( $new_value ); break; default: $value = $new_value; break; } return $value; } /** * This functions provides the big picture logic * for displaying each type of user input field. * * @since Brimstone 1.01 * * @param array $instance * @return void */ public function form( $instance ) { if ( ! $this->settings ) { return; } $display_panels = false; $display_repeater = false; $panel_count = 0; if ( isset( $instance['repeater'] ) && is_array( $instance['repeater'] ) ) { $repeater_instances = $instance['repeater']; unset( $instance['repeater'] ); } else { if ( isset( $this->settings['repeater']['default'] ) ) { $repeater_instances = $this->settings['repeater']['default']; } else { $repeater_instances[1] = array(); } } ?>
id ) . ' .panel-repeater-container'; ?> id ) . ' .panel-container'; ?>