Widget area. */ /** * Class MonsterInsights_Popular_Posts_Widget_Sidebar */ class MonsterInsights_Popular_Posts_Widget_Sidebar extends WP_Widget { /** * Hold widget settings defaults, populated in constructor. * * @since 7.12.0 * * @var array */ protected $defaults; /** * Hold widget options that are theme specific. * * @since 7.12.0 * * @var array */ protected $conditional_options; /** * Constructor * * @since 7.12.0 */ public function __construct() { // Widget defaults. $this->defaults = array( 'title' => '', 'display_title' => 'on', 'post_count' => 5, 'theme' => 'alpha', 'title_color' => '#393F4C', 'title_size' => 12, 'label_color' => '#EB5757', 'label_text' => 'Trending', 'meta_color' => '#99A1B3', 'meta_size' => '12', 'meta_author' => 'on', 'meta_date' => 'on', 'meta_comments' => 'on', 'background_color' => '#F0F2F4', 'border_color' => '#D3D7DE', 'columns' => '1', ); $this->conditional_options = array( 'title_color' => array( 'title', 'color' ), 'title_size' => array( 'title', 'size' ), 'label_color' => array( 'label', 'color' ), 'label_text' => array( 'label', 'text' ), 'background_color' => array( 'background', 'color' ), 'background_border' => array( 'background', 'border' ), 'meta_color' => array( 'meta', 'color' ), 'meta_size' => array( 'meta', 'size' ), 'meta_author' => array( 'meta', 'author' ), 'meta_date' => array( 'meta', 'date' ), 'meta_comments' => array( 'meta', 'comments' ), 'comments_color' => array( 'comments', 'color' ), ); // Widget Slug. $widget_slug = 'monsterinsights-popular-posts-widget'; // Widget basics. $widget_ops = array( 'classname' => $widget_slug, 'description' => esc_html_x( 'Display popular posts.', 'Widget', 'google-analytics-for-wordpress' ), ); // Widget controls. $control_ops = array( 'id_base' => $widget_slug, ); $this->add_scripts(); // Load widget. parent::__construct( $widget_slug, esc_html_x( 'Popular Posts - MonsterInsights', 'Widget', 'google-analytics-for-wordpress' ), $widget_ops, $control_ops ); } /** * Output the HTML for this widget. * * @param array $args An array of standard parameters for widgets in this theme. * @param array $instance An array of settings for this widget instance. * * @since 7.12.0 * */ public function widget( $args, $instance ) { echo $args['before_widget']; $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); if ( $instance['display_title'] && ! empty( $instance['title'] ) ) { echo $args['before_title']; echo wp_kses_post( $title ); echo $args['after_title']; } $atts = array( 'theme' => $instance['theme'], 'post_count' => $instance['post_count'], 'columns' => 1, // Sidebar is not wide so we always use the 1 column layout. 'widget_title' => false, // Override this in favor of sidebar-specific markup above. ); foreach ( $this->conditional_options as $key => $default ) { if ( ! empty( $instance[ $key ] ) ) { $atts[ $key ] = $instance[ $key ]; } } echo MonsterInsights_Popular_Posts_Widget()->shortcode_output( $atts ); echo $args['after_widget']; } /** * Deal with the settings when they are saved by the admin. Here is * where any validation should be dealt with. * * @param array $new_instance An array of new settings as submitted by the admin. * @param array $old_instance An array of the previous settings. * * @return array The validated and (if necessary) amended settings * @since 7.12.0 * */ public function update( $new_instance, $old_instance ) { $new_instance['title'] = wp_strip_all_tags( $new_instance['title'] ); $new_instance['theme'] = wp_strip_all_tags( $new_instance['theme'] ); $new_instance['display_title'] = isset( $new_instance['display_title'] ) ? wp_strip_all_tags( $new_instance['display_title'] ) : ''; $new_instance['post_count'] = absint( $new_instance['post_count'] ); // Theme-dependant options. $themes = new MonsterInsights_Popular_Posts_Themes( 'widget', ! empty( $old_instance['theme'] ) ? $old_instance['theme'] : '' ); $theme = $themes->get_theme(); foreach ( $this->conditional_options as $key => $obj ) { $new_instance = $this->maybe_remove_option( ! empty( $theme['styles'][ $obj[0] ][ $obj[1] ] ), $key, $new_instance ); } return $new_instance; } /** * Process dynamic and checkbox values so they are stored correctly and specific to the current theme. * * @param bool $is_used A check if this property is used in the currently selected theme. * @param string $key The key of the property we're checking. * @param array $instance The current widget instance, new instance. * * @return mixed */ public function maybe_remove_option( $is_used, $key, $instance ) { $checkboxes = array( 'meta_author', 'meta_date', 'meta_comments', ); if ( $is_used && ! isset( $instance[ $key ] ) && in_array( $key, $checkboxes ) ) { $instance[ $key ] = 'off'; } elseif ( ! $is_used && isset( $instance[ $key ] ) ) { unset( $instance[ $key ] ); } elseif ( $is_used && isset( $instance[ $key ] ) ) { $instance[ $key ] = wp_strip_all_tags( $instance[ $key ] ); } return $instance; } /** * Display the form for this widget on the Widgets page of the WP Admin area. * * @param array $instance An array of the current settings for this widget. * * @since 7.12.0 */ public function form( $instance ) { // Merge with defaults but use theme settings from Vue as defaults. $theme_name = empty( $instance['theme'] ) ? $this->defaults['theme'] : $instance['theme']; $themes = new MonsterInsights_Popular_Posts_Themes( 'widget', $theme_name ); $theme = $themes->get_theme(); $this->prepare_defaults_from_theme( $theme ); $instance = wp_parse_args( (array) $instance, $this->defaults ); $title_font_sizes = apply_filters( 'monsterinsights_popular_posts_widget_title_sizes', range( 10, 35 ) ); $this->text_input( 'title', _x( 'Widget Title:', 'Widget', 'google-analytics-for-wordpress' ), $instance ); $categories = array( array( 'name' => 'News', 'value' => '0', ), array( 'name' => 'Technology', 'value' => '1', ), ); ?>
/>
color_input( 'title_color', _x( 'Title Color:', 'Widget', 'google-analytics-for-wordpress' ), $instance ); ?> size_input( 'title_size', _x( 'Title Font Size:', 'Widget', 'google-analytics-for-wordpress' ), $instance, $title_font_sizes ); } if ( ! empty( $theme['styles']['label']['color'] ) ) { $this->color_input( 'label_color', _x( 'Label Color:', 'Widget', 'google-analytics-for-wordpress' ), $instance ); } if ( ! empty( $theme['styles']['label']['editable'] ) && ! empty( $theme['styles']['label']['text'] ) ) { $this->text_input( 'label_text', _x( 'Label Text:', 'Widget', 'google-analytics-for-wordpress' ), $instance ); } if ( ! empty( $theme['styles']['background']['border'] ) ) { $this->color_input( 'background_border', _x( 'Border Color:', 'Widget', 'google-analytics-for-wordpress' ), $instance ); } if ( ! empty( $theme['styles']['background']['color'] ) ) { $this->color_input( 'background_color', _x( 'Background Color:', 'Widget', 'google-analytics-for-wordpress' ), $instance ); } if ( ! empty( $theme['styles']['comments']['color'] ) ) { $this->color_input( 'comments_color', _x( 'Comments Count Color:', 'Widget', 'google-analytics-for-wordpress' ), $instance ); } ?>
conditional_options as $key => $obj ) { if ( ! empty( $theme['styles'][ $obj[0] ][ $obj[1] ] ) ) { $this->defaults[ $key ] = $theme['styles'][ $obj[0] ][ $obj[1] ]; } } } /** * Load specific widget scripts in the admin. */ public function add_scripts() { add_action( 'admin_enqueue_scripts', array( $this, 'load_widget_scripts' ) ); } /** * Load admin-specific widget scripts. */ public function load_widget_scripts() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : false; if ( ! isset( $screen->id ) || 'widgets' !== $screen->id ) { return; } $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min'; wp_enqueue_style( 'monsterinsights-admin-widget-setting-styles', plugins_url( 'assets/css/admin-widget-settings' . $suffix . '.css', MONSTERINSIGHTS_PLUGIN_FILE ), array( 'wp-color-picker', ), monsterinsights_get_asset_version() ); wp_register_script( 'monsterinsights-admin-widget-settings', plugins_url( 'assets/js/admin-widget-settings' . $suffix . '.js', MONSTERINSIGHTS_PLUGIN_FILE ), array( 'jquery', 'wp-color-picker', ), monsterinsights_get_asset_version(), true ); wp_enqueue_script( 'monsterinsights-admin-widget-settings' ); wp_localize_script( 'monsterinsights-admin-widget-settings', 'monsterinsights_pp', array( 'nonce' => wp_create_nonce( 'mi-admin-nonce' ), ) ); } }