Advertisement
Guest User

class-tf-module-archive-loop.php

a guest
Oct 27th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.39 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Module Text.
  4.  *
  5.  * Show text blocks with rich editor.
  6.  * @package ThemifyFlow
  7.  * @since 1.0.0
  8.  */
  9. class TF_Module_Archive_Loop extends TF_Module {
  10.  
  11.     var $query_parameters;
  12.  
  13.     /**
  14.      * Constructor.
  15.      */
  16.     public function __construct() {
  17.         parent::__construct(array(
  18.             'name' => __('Archive Post', 'themify-flow'),
  19.             'slug' => 'archive-loop',
  20.             'shortcode' => 'tf_archive_loop',
  21.             'description' => 'Archive Post module',
  22.             'category' => 'archive'
  23.         ));
  24.     }
  25.  
  26.     /**
  27.      * Module settings field
  28.      *
  29.      * @since 1.0.0
  30.      * @access public
  31.      * @return array
  32.      */
  33.     public function fields() {
  34.         global $TF;
  35.  
  36.         $image_base = $TF->framework_uri() . '/assets/img/builder';
  37.         return apply_filters( 'tf_module_archive_loop_fields', array(
  38.             'layout'  => array(
  39.                 'type'       => 'layout',
  40.                 'label'      => __('Post Layout', 'themify-flow'),
  41.                 'options'    => array(
  42.                     array( 'img' => $image_base . '/list-post.png', 'value' => 'list_post', 'label' => __('List Post', 'themify-flow'), 'selected' => true ),
  43.                     array( 'img' => $image_base . '/grid3.png', 'value' => 'grid3', 'label' => __('Grid 3', 'themify-flow')),
  44.                     array( 'img' => $image_base . '/grid2.png', 'value' => 'grid2', 'label' => __('Grid 2', 'themify-flow')),
  45.                     array( 'img' => $image_base . '/grid4.png', 'value' => 'grid4', 'label' => __('Grid 4', 'themify-flow'))
  46.                 )
  47.             ),
  48.             'order' => array(
  49.                 'type' => 'select',
  50.                 'label' => __('Order', 'themify-flow'),
  51.                 'options' => array(
  52.                     array( 'name' => __('Descending','themify-flow'), 'value' => 'desc' ),
  53.                     array( 'name' => __('Ascending','themify-flow'), 'value' => 'asc' )
  54.                 )
  55.             ),
  56.             'orderby' => array(
  57.                 'type' => 'select',
  58.                 'label' => __('Order By', 'themify-flow'),
  59.                 'options' => array(
  60.                     array( 'name' => __('Date','themify-flow'), 'value' => 'date' ),
  61.                     array( 'name' => __('Id','themify-flow'), 'value' => 'id' ),
  62.                     array( 'name' => __('Author','themify-flow'), 'value' => 'author' ),
  63.                     array( 'name' => __('Title','themify-flow'), 'value' => 'title' ),
  64.                     array( 'name' => __('Name','themify-flow'), 'value' => 'name' ),
  65.                     array( 'name' => __('Modified','themify-flow'), 'value' => 'modified' ),
  66.                     array( 'name' => __('Rand','themify-flow'), 'value' => 'rand' ),
  67.                     array( 'name' => __('Comment Count','themify-flow'), 'value' => 'comment_count' ),
  68.                 )
  69.             ),
  70.             'pagination' => array(
  71.                 'type' => 'radio',
  72.                 'label' => __('Pagination', 'themify-flow'),
  73.                 'options' => array(
  74.                     array( 'name' => __('Yes','themify-flow'), 'value' => 'yes', 'selected' => true ),
  75.                     array( 'name' => __('No','themify-flow'), 'value' => 'no' )
  76.                 )
  77.             ),
  78.             'content' => array(
  79.                 'type' => 'builder',
  80.                 'wrapper' => 'no',
  81.                 'options' => array(
  82.                     'category' => 'loop'
  83.                 ),
  84.                 'default' => '[tf_back_row][tf_back_column grid="fullwidth"][tf_post_title][tf_post_excerpt_content][/tf_back_column][/tf_back_row]'
  85.             )
  86.         ) );
  87.     }
  88.  
  89.     /**
  90.      * Module style selectors.
  91.      *
  92.      * Hold module stye selectors to be used in Styling Panel.
  93.      *
  94.      * @since 1.0.0
  95.      * @access public
  96.      * @return array
  97.      */
  98.     public function styles() {
  99.         return apply_filters( 'tf_module_archive_loop_styles', array(
  100.             'tf_module_archive_loop_wrapper' => array(
  101.                 'label' => __('Posts Wrapper', 'themify-flow' ),
  102.                 'selector' => '.tf_loops_wrapper',
  103.                 'basic_styling' => array( 'background', 'font', 'padding', 'margin', 'border' ),
  104.             ),
  105.             'tf_module_archive_loop_container' => array(
  106.                 'label' => __('Post Container', 'themify-flow' ),
  107.                 'selector' => '.tf_post',
  108.                 'basic_styling' => array( 'background', 'font', 'padding', 'margin', 'border' ),
  109.             ),
  110.             'tf_module_archive_loop_post_link' => array(
  111.                 'label' => __( 'Post Link', 'themify-flow' ),
  112.                 'selector' => '.tf_post a',
  113.                 'basic_styling' => array( 'font' ),
  114.             ),
  115.             'tf_module_archive_loop_post_link_hover' => array(
  116.                 'label' => __( 'Post Link Hover', 'themify-flow' ),
  117.                 'selector' => '.tf_post a:hover',
  118.                 'basic_styling' => array( 'font' ),
  119.             ),
  120.             'tf_module_archive_loop_post_title' => array(
  121.                 'label' => __( 'Post Title', 'themify-flow' ),
  122.                 'selector' => '.tf_post_title',
  123.                 'basic_styling' => array(  'font' ),
  124.             ),
  125.             'tf_module_archive_loop_post_title_link' => array(
  126.                 'label' => __( 'Post Title Link', 'themify-flow' ),
  127.                 'selector' => '.tf_post_title a',
  128.                 'basic_styling' => array( 'font' ),
  129.             ),
  130.             'tf_module_archive_loop_post_title_link_hover' => array(
  131.                 'label' => __( 'Post Title Link Hover', 'themify-flow' ),
  132.                 'selector' => '.tf_post_title a:hover',
  133.                 'basic_styling' => array( 'font' ),
  134.             ),
  135.             'tf_module_archive_loop_post_content' => array(
  136.                 'label' => __( 'Post Content', 'themify-flow' ),
  137.                 'selector' => '.tf_post_content',
  138.                 'basic_styling' => array( 'border', 'font', 'margin', 'padding', 'background' ),
  139.             ),
  140.             'tf_module_archive_loop_post_meta' => array(
  141.                 'label' => __( 'Post Meta Container', 'themify-flow' ),
  142.                 'selector' => '.tf_post_meta',
  143.                 'basic_styling' => array( 'border', 'font', 'margin' ),
  144.             ),
  145.             'tf_module_archive_loop_post_meta_link' => array(
  146.                 'label' => __( 'Post Meta Link', 'themify-flow' ),
  147.                 'selector' => '.tf_post_meta a',
  148.                 'basic_styling' => array( 'font' ),
  149.             ),
  150.             'tf_module_archive_loop_post_meta_link_hover' => array(
  151.                 'label' => __( 'Post Meta Link Hover', 'themify-flow' ),
  152.                 'selector' => '.tf_post_meta a:hover',
  153.                 'basic_styling' => array( 'font' ),
  154.             ),
  155.             'tf_module_archive_loop_post_date' => array(
  156.                 'label' => __( 'Post Meta - Date', 'themify-flow' ),
  157.                 'selector' => '.tf_post_date',
  158.                 'basic_styling' => array( 'font' ),
  159.             ),
  160.             'tf_module_archive_loop_post_category' => array(
  161.                 'label' => __( 'Post Meta - Category Link', 'themify-flow' ),
  162.                 'selector' => '.tf_post_category a',
  163.                 'basic_styling' => array( 'font' ),
  164.             ),
  165.             'tf_module_archive_loop_post_category_hover' => array(
  166.                 'label' => __( 'Post Meta - Category Link Hover', 'themify-flow' ),
  167.                 'selector' => '.tf_post_category a:hover',
  168.                 'basic_styling' => array( 'font' ),
  169.             ),
  170.             'tf_module_archive_loop_post_tag' => array(
  171.                 'label' => __( 'Post Meta - Tag Link', 'themify-flow' ),
  172.                 'selector' => '.tf_post_tag a',
  173.                 'basic_styling' => array( 'font' ),
  174.             ),
  175.             'tf_module_archive_loop_post_tag_hover' => array(
  176.                 'label' => __( 'Post Meta - Tag Link Hover', 'themify-flow' ),
  177.                 'selector' => '.tf_post_tag a:hover',
  178.                 'basic_styling' => array( 'font' ),
  179.             ),
  180.             'tf_module_archive_loop_post_author' => array(
  181.                 'label' => __( 'Post Meta - Author', 'themify-flow' ),
  182.                 'selector' => '.tf_post_author a',
  183.                 'basic_styling' => array( 'font' ),
  184.             ),
  185.             'tf_module_archive_loop_post_author_hover' => array(
  186.                 'label' => __( 'Post Meta - Author Hover', 'themify-flow' ),
  187.                 'selector' => '.tf_post_author a:hover',
  188.                 'basic_styling' => array( 'font' ),
  189.             ),
  190.             'tf_module_archive_loop_post_comment' => array(
  191.                 'label' => __( 'Post Meta - Comment Count', 'themify-flow' ),
  192.                 'selector' => '.tf_post_comment a',
  193.                 'basic_styling' => array( 'font' ),
  194.             ),
  195.             'tf_module_archive_loop_post_comment_hover' => array(
  196.                 'label' => __( 'Post Meta - Comment Count Hover', 'themify-flow' ),
  197.                 'selector' => '.tf_post_comment a:hover',
  198.                 'basic_styling' => array( 'font' ),
  199.             ),
  200.             'tf_module_archive_loop_pagination_container' => array(
  201.                 'label' => __( 'Pagination Container', 'themify-flow' ),
  202.                 'selector' => '.tf_pagination',
  203.                 'basic_styling' => array( 'background', 'border', 'padding', 'margin' ),
  204.             ),
  205.             'tf_module_archive_loop_pagination_link' => array(
  206.                 'label' => __( 'Pagination Link', 'themify-flow' ),
  207.                 'selector' => '.tf_pagination a, .tf_pagination span',
  208.                 'basic_styling' => array( 'border', 'font', 'margin' ),
  209.             ),
  210.             'tf_module_archive_loop_pagination_link_hover' => array(
  211.                 'label' => __( 'Pagination Link Hover', 'themify-flow' ),
  212.                 'selector' => '.tf_pagination a:hover',
  213.                 'basic_styling' => array( 'border', 'font', 'margin' ),
  214.             )
  215.         ) );
  216.     }
  217.  
  218.     /**
  219.      * Render main shortcode.
  220.      *
  221.      * @since 1.0.0
  222.      * @access public
  223.      * @param array $original_atts
  224.      * @param string $content
  225.      * @return string
  226.      */
  227.     public function render_shortcode( $original_atts, $content = null ) {
  228.         global $wp_query, $query_string, $TF;
  229.  
  230.         $atts = shortcode_atts( array(
  231.             'layout' => 'post-list',
  232.             'order' => 'DESC',
  233.             'orderby' => 'date',
  234.             'pagination' => 'yes',
  235.         ), $original_atts, $this->shortcode );
  236.  
  237.         $output = '';
  238.        
  239.         $build_query = array(
  240.             'posts_per_page' =>get_option('posts_per_page'),
  241.             'order' => $atts['order'],
  242.             'orderby' => $atts['orderby']
  243.         );
  244.         if ( TF_Model::is_template_page()) {
  245.             query_posts( build_query( $build_query ) );
  246.         } else {
  247.             // reset the query parameters set by the module
  248.             $this->query_parameters = $build_query;
  249.             add_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
  250.             $wp_query->get_posts();
  251.             remove_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
  252.         }
  253.         if ( have_posts() ) {
  254.             ob_start();
  255.             ?>
  256.             <!-- loopswrapper -->
  257.             <div class="tf_loops_wrapper clearfix <?php echo esc_attr( $atts['layout'] ); ?>">
  258.                 <?php
  259.                 $TF->in_archive_loop = true;
  260.                 while ( have_posts() ) {
  261.                     the_post(); ?>
  262.  
  263.                     <?php do_action( 'tf_archive_loop_before_post' ); ?>
  264.  
  265.                     <article <?php echo tf_get_attr( 'post', $original_atts ); ?>>
  266.  
  267.                         <?php do_action( 'tf_archive_loop_start_post' ); ?>
  268.  
  269.                         <?php echo do_shortcode( $content ); ?>
  270.  
  271.                         <?php do_action( 'tf_archive_loop_end_post' ); ?>
  272.                                                 <meta itemprop="datePublished" content="<?php the_modified_date('c')?>"/>
  273.                     </article>
  274.  
  275.                     <?php do_action( 'tf_archive_loop_after_post' ); ?>
  276.  
  277.                     <?php
  278.                 }
  279.                 $TF->in_archive_loop = false; ?>
  280.             </div><!-- /tf_loops_wrapper -->
  281.             <?php
  282.             // Pagination links
  283.             if( 'yes' == $atts['pagination']) {
  284.                 get_template_part( 'includes/pagination', $wp_query->query_vars['post_type'] );
  285.             }
  286.             $output = ob_get_contents();
  287.             ob_get_clean();
  288.         }
  289.  
  290.         wp_reset_query();
  291.  
  292.         wp_reset_postdata();
  293.  
  294.         return $output;
  295.     }
  296.  
  297.     function pre_get_posts( $query ) {
  298.         if( $query->is_main_query() ) {
  299.             $query->set( 'posts_per_page', $this->query_parameters['posts_per_page'] );
  300.             $query->set( 'order', $this->query_parameters['order'] );
  301.             $query->set( 'orderby', $this->query_parameters['orderby'] );
  302.         }
  303.     }
  304. }
  305.  
  306. new TF_Module_Archive_Loop();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement