/** * Construct the widget * * Skip right to the grandparent constructor. * * @since 1.0.0 */ public function __construct() { $widget_ops = array('classname' => 'widget_recent_entries vgsr_recent_posts', 'description' => __('Your site’s most recent Posts with post thumbnails.', 'vgsr-widgets'), 'customize_selective_refresh' => true); WP_Widget::__construct('vgsr-recent-posts', __('Recent Posts 2', 'vgsr-widgets'), $widget_ops); $this->alt_option_name = 'widget_recent_entries'; // Enqueue style wp_enqueue_style('vgsr-recent-posts-widget', vgsr_widgets()->assets_url . 'css/vgsr-recent-posts-widget.css'); }
// Walk our shortcodes foreach ($this->shortcodes() as $class_name) { // Define widget file location $file = $this->shortcodes_dir . 'class-' . strtolower(str_replace('_', '-', $class_name)) . '.php'; // Load widget file if (file_exists($file)) { require_once $file; } // Register widget if (class_exists($class_name)) { $shortcode = new $class_name(); $shortcode->register(); } } } } /** * Return single instance of this main plugin class * * @since 1.0.0 * * @return VGSR_Widgets */ function vgsr_widgets() { return VGSR_Widgets::instance(); } // Initiate vgsr_widgets(); } // class_exists
/** * Setup support for Shortcode UI * * @since 1.0.0 * * @uses shortcode_ui_register_for_shortcode() */ private function add_shortcode_ui() { // Bail when Shortcode UI is not available if (!class_exists('Shortcode_UI')) { return; } // Define UI args $args = $this->args; $args['atts'] = $this->atts; // Register for UI shortcode_ui_register_for_shortcode($this->name, $args); // Shortcode has a stylesheet $stylesheet = 'assets/css/' . strtolower(str_replace('_', '-', get_class($this))) . '.css'; if (file_exists(vgsr_widgets()->plugin_dir . $stylesheet)) { // Enqueue editor stylesheet add_filter('editor_stylesheets', function ($stylesheets) use($stylesheet) { $stylesheets[] = vgsr_widgets()->plugin_url . $stylesheet; return $stylesheets; }); } }
/** * Shortcode callback * * @since 1.0.0 * * @uses wp_parse_id_list() * @uses WP_Query() * @uses the_title() * @uses get_the_permalink() * @uses the_content() * * @param array $atts Shortcode instance attributes */ public function shortcode($atts = array()) { // Define local variable $query_args = array('posts_per_page' => -1, 'post_status' => 'publish'); // Define query args if ($atts['pages']) { $query_args['post__in'] = wp_parse_id_list($atts['pages']); $query_args['post_type'] = 'any'; } else { $query_args['post_parent'] = get_the_ID(); $query_args['post_type'] = $atts['post_type']; } // Bail when the query is invalid if (!($query = new WP_Query($query_args))) { return; } // Bail when no posts were found if (!$query->have_posts()) { return; } // Construct content filters add_filter('the_content', 'strip_shortcodes', 5); add_filter('the_content', array($this, 'limit_word_count'), 5); add_filter('the_content', 'balanceTags', 6); // Set class globals $this->word_count = absint($atts['words']); // Use teasers with <!--more--> tags global $more; $more = 0; // Start output buffer ob_start(); ?> <div class="vgsr-subpages"> <?php while ($query->have_posts()) { $query->the_post(); ?> <div <?php post_class($query->current_post % 2 ? 'even' : 'odd'); ?> > <header class="subpage-header"> <h3 class="subpage-title"><?php the_title(sprintf('<a href="%s">', get_the_permalink()), '</a>'); ?> </h3> </header> <div class="subpage-content"> <?php /* translators: %s: Name of current post */ ?> <?php the_content(sprintf(__('Continue reading %s <span class="meta-nav">→</span>', 'vgsr-widgets'), the_title('<span class="screen-reader-text">"', '"</span>', false))); ?> </div> </div> <?php } ?> </div> <?php // End output buffer $html = ob_get_clean(); // Reset class globals $this->word_count = 55; // Deconstruct content filters remove_filter('the_content', 'balanceTags', 6); remove_filter('the_content', array($this, 'limit_word_count'), 5); remove_filter('the_content', 'strip_shortcodes', 5); // Reset `$post` and other globals (like `$more`) wp_reset_postdata(); // Enqueue styles wp_enqueue_style('vgsr-subpages', vgsr_widgets()->plugin_url . 'assets/css/vgsr-shortcode-subpages.css'); return $html; }