__( 'Displays a post loop.', 'siteorigin-panels' ), ), array( 'width' => 800, ) ); } /** * Are we currently rendering a post loop * * @return bool */ static function is_rendering_loop() { return self::$rendering_loop; } /** * Which post loop is currently being rendered * * @return array */ static function get_current_loop_template() { return self::$current_loop_template; } /** * Which post loop is currently being rendered * * @return array */ static function get_current_loop_instance() { return self::$current_loop_instance; } /** * Update the widget * * @param array $new * @param array $old * @return array */ function update( $new, $old ){ if( class_exists( 'SiteOrigin_Widget' ) && class_exists( 'SiteOrigin_Widget_Field_Posts' ) ) { $helper = $this->get_helper_widget( $this->get_loop_templates() ); return $helper->update( $new, $old ); } else { $new['more'] = !empty( $new['more'] ); return $new; } } /** * @param array $args * @param array $instance */ function widget( $args, $instance ) { if( empty( $instance['template'] ) ) return; if( is_admin() ) return; static $depth = 0; $depth++; if( $depth > 1 ) { // Because of infinite loops, don't render this post loop if its inside another $depth--; echo $args['before_widget'].$args['after_widget']; return; } $query_args = $instance; //If Widgets Bundle post selector is available and a posts query has been saved using it. if ( function_exists( 'siteorigin_widget_post_selector_process_query' ) && ! empty( $instance['posts'] ) ) { $query_args = siteorigin_widget_post_selector_process_query($instance['posts']); $query_args['additional'] = empty($instance['additional']) ? array() : $instance['additional']; } else { if ( ! empty( $instance['posts'] ) ) { // This is using the new WB 1.9 posts field $query_args = wp_parse_args( $instance['posts'], $query_args ); } if( ! empty( $query_args['sticky'] ) ) { switch( $query_args['sticky'] ){ case 'ignore' : $query_args['ignore_sticky_posts'] = 1; break; case 'only' : $query_args['post__in'] = get_option( 'sticky_posts' ); break; case 'exclude' : $query_args['post__not_in'] = get_option( 'sticky_posts' ); break; } } unset($query_args['template']); unset($query_args['title']); unset($query_args['sticky']); if (empty($query_args['additional'])) { $query_args['additional'] = array(); } } $query_args = wp_parse_args($query_args['additional'], $query_args); unset($query_args['additional']); global $wp_rewrite; if( $wp_rewrite->using_permalinks() ) { if( get_query_var('paged') ) { // When the widget appears on a sub page. $query_args['paged'] = get_query_var('paged'); } elseif( strpos( $_SERVER['REQUEST_URI'], '/page/' ) !== false ) { // When the widget appears on the home page. preg_match('/\/page\/([0-9]+)\//', $_SERVER['REQUEST_URI'], $matches); if(!empty($matches[1])) $query_args['paged'] = intval($matches[1]); else $query_args['paged'] = 1; } else $query_args['paged'] = 1; } else { // Get current page number when we're not using permalinks $query_args['paged'] = isset($_GET['paged']) ? intval($_GET['paged']) : 1; } // Exclude the current post to prevent possible infinite loop global $siteorigin_panels_current_post; if( !empty($siteorigin_panels_current_post) ){ if( !empty( $query_args['post__not_in'] ) ){ if( !is_array( $query_args['post__not_in'] ) ){ $query_args['post__not_in'] = explode( ',', $query_args['post__not_in'] ); $query_args['post__not_in'] = array_map( 'intval', $query_args['post__not_in'] ); } $query_args['post__not_in'][] = $siteorigin_panels_current_post; } else { $query_args['post__not_in'] = array( $siteorigin_panels_current_post ); } } if( !empty($query_args['post__in']) && !is_array($query_args['post__in']) ) { $query_args['post__in'] = explode(',', $query_args['post__in']); $query_args['post__in'] = array_map('intval', $query_args['post__in']); } // Create the query query_posts( apply_filters( 'siteorigin_panels_postloop_query_args', $query_args ) ); echo $args['before_widget']; // Filter the title $instance['title'] = apply_filters('widget_title', $instance['title'], $instance, $this->id_base); if ( !empty( $instance['title'] ) ) { echo $args['before_title'] . $instance['title'] . $args['after_title']; } global $more; $old_more = $more; $more = empty($instance['more']); self::$rendering_loop = true; self::$current_loop_instance = $instance; self::$current_loop_template = $instance['template']; if(strpos('/'.$instance['template'], '/content') !== false) { while( have_posts() ) { the_post(); locate_template($instance['template'], true, false); } } else { locate_template($instance['template'], true, false); } self::$rendering_loop = false; self::$current_loop_instance = null; self::$current_loop_template = null; echo $args['after_widget']; // Reset everything wp_reset_query(); $depth--; } /** * Display the form for the post loop. * * @param array $instance * @return string|void */ function form( $instance ) { $templates = $this->get_loop_templates(); if( empty($templates) ) { ?>
get_helper_widget( $templates ); $helper->form( $instance ); } else { $instance = wp_parse_args( $instance, array( 'title' => '', 'template' => 'loop.php', // Query args 'post_type' => 'post', 'posts_per_page' => '', 'order' => 'DESC', 'orderby' => 'date', 'sticky' => '', 'additional' => '', 'more' => false, ) ); ?>
/>
$1', __('Additional query arguments. See 1{query_posts}.', 'siteorigin-panels') ) ?>
$template) { $invalid = false; // Ensure the provided file has a valid name and path if ( validate_file( $template ) != 0 ) { $invalid = true; } // Don't expect non-PHP files if ( substr( $template, -4 ) != '.php' ) { $invalid = true; } $template = locate_template( $template ); if ( empty( $template ) || $invalid ) { unset( $templates[ $template_key ] ); } } // Update array indexes to ensure logical indexing sort( $templates ); sort( $templates ); return $templates; } /** * Get the helper widget based on the Widgets Bundle's classes. * * @param $templates array Blog loop templates. * * @return mixed */ private function get_helper_widget( $templates ) { if ( empty( $this->helper ) && class_exists( 'SiteOrigin_Widget' ) && class_exists( 'SiteOrigin_Widget_Field_Posts' ) ) { $this->helper = new SiteOrigin_Panels_Widgets_PostLoop_Helper( $templates ); } // These ensure the form fields name attributes are correct. $this->helper->id_base = $this->id_base; $this->helper->id = $this->id; $this->helper->number = $this->number; return $this->helper; } }