Guest User

Untitled

a guest
Mar 30th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.67 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  
  5. * Plugin Name: Display Posts Shortcode
  6.  
  7. * Plugin URI: http://www.billerickson.net/shortcode-to-display-posts/
  8.  
  9. * Description: Display a listing of posts using the [display-posts] shortcode
  10.  
  11. * Version: 2.4
  12.  
  13. * Author: Bill Erickson
  14.  
  15. * Author URI: http://www.billerickson.net
  16.  
  17. *
  18.  
  19. * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
  20.  
  21. * General Public License version 2, as published by the Free Software Foundation. You may NOT assume
  22.  
  23. * that you can use any other version of the GPL.
  24.  
  25. *
  26.  
  27. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
  28.  
  29. * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  30.  
  31. *
  32.  
  33. * @package Display Posts
  34.  
  35. * @version 2.4
  36.  
  37. * @author Bill Erickson <[email protected]>
  38.  
  39. * @copyright Copyright (c) 2011, Bill Erickson
  40.  
  41. * @link http://www.billerickson.net/shortcode-to-display-posts/
  42.  
  43. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  44.  
  45. */
  46.  
  47.  
  48.  
  49.  
  50.  
  51. /**
  52.  
  53. * To Customize, use the following filters:
  54.  
  55. *
  56.  
  57. * `display_posts_shortcode_args`
  58.  
  59. * For customizing the $args passed to WP_Query
  60.  
  61. *
  62.  
  63. * `display_posts_shortcode_output`
  64.  
  65. * For customizing the output of individual posts.
  66.  
  67. * Example: https://gist.github.com/1175575#file_display_posts_shortcode_output.php
  68.  
  69. *
  70.  
  71. * `display_posts_shortcode_wrapper_open`
  72.  
  73. * display_posts_shortcode_wrapper_close`
  74.  
  75. * For customizing the outer markup of the whole listing. By default it is a <ul> but
  76.  
  77. * can be changed to <ol> or <div> using the 'wrapper' attribute, or by using this filter.
  78.  
  79. * Example: https://gist.github.com/1270278
  80.  
  81. */
  82.  
  83.  
  84.  
  85. // Create the shortcode
  86.  
  87. add_shortcode( 'display-posts', 'be_display_posts_shortcode' );
  88.  
  89. function be_display_posts_shortcode( $atts ) {
  90.  
  91.  
  92.  
  93. // Original Attributes, for filters
  94.  
  95. $original_atts = $atts;
  96.  
  97.  
  98.  
  99. // Pull in shortcode attributes and set defaults
  100.  
  101. $atts = shortcode_atts( array(
  102.  
  103. 'title' => '',
  104.  
  105. 'author' => '',
  106.  
  107. 'category' => '',
  108.  
  109. 'date_format' => '(n/j/Y)',
  110.  
  111. 'display_posts_off' => false,
  112.  
  113. 'exclude_current' => false,
  114.  
  115. 'id' => false,
  116.  
  117. 'ignore_sticky_posts' => false,
  118.  
  119. 'image_size' => false,
  120.  
  121. 'include_title' => true,
  122.  
  123. 'include_author' => false,
  124.  
  125. 'include_content' => false,
  126.  
  127. 'include_date' => false,
  128.  
  129. 'include_excerpt' => false,
  130.  
  131. 'meta_key' => '',
  132.  
  133. 'meta_value' => '',
  134.  
  135. 'no_posts_message' => '',
  136.  
  137. 'offset' => 0,
  138.  
  139. 'order' => 'DESC',
  140.  
  141. 'orderby' => 'date',
  142.  
  143. 'post_parent' => false,
  144.  
  145. 'post_status' => 'publish',
  146.  
  147. 'post_type' => 'post',
  148.  
  149. 'posts_per_page' => '10',
  150.  
  151. 'tag' => '',
  152.  
  153. 'tax_operator' => 'IN',
  154.  
  155. 'tax_term' => false,
  156.  
  157. 'taxonomy' => false,
  158.  
  159. 'wrapper' => 'ul',
  160.  
  161. 'wrapper_class' => 'display-posts-listing',
  162.  
  163. 'wrapper_id' => false,
  164.  
  165. ), $atts, 'display-posts' );
  166.  
  167.  
  168.  
  169. // End early if shortcode should be turned off
  170.  
  171. if( $atts['display_posts_off'] )
  172.  
  173. return;
  174.  
  175.  
  176.  
  177. $shortcode_title = sanitize_text_field( $atts['title'] );
  178.  
  179. $author = sanitize_text_field( $atts['author'] );
  180.  
  181. $category = sanitize_text_field( $atts['category'] );
  182.  
  183. $date_format = sanitize_text_field( $atts['date_format'] );
  184.  
  185. $exclude_current = be_display_posts_bool( $atts['exclude_current'] );
  186.  
  187. $id = $atts['id']; // Sanitized later as an array of integers
  188.  
  189. $ignore_sticky_posts = be_display_posts_bool( $atts['ignore_sticky_posts'] );
  190.  
  191. $image_size = sanitize_key( $atts['image_size'] );
  192.  
  193. $include_title = be_display_posts_bool( $atts['include_title'] );
  194.  
  195. $include_author = be_display_posts_bool( $atts['include_author'] );
  196.  
  197. $include_content = be_display_posts_bool( $atts['include_content'] );
  198.  
  199. $include_date = be_display_posts_bool( $atts['include_date'] );
  200.  
  201. $include_excerpt = be_display_posts_bool( $atts['include_excerpt'] );
  202.  
  203. $meta_key = sanitize_text_field( $atts['meta_key'] );
  204.  
  205. $meta_value = sanitize_text_field( $atts['meta_value'] );
  206.  
  207. $no_posts_message = sanitize_text_field( $atts['no_posts_message'] );
  208.  
  209. $offset = intval( $atts['offset'] );
  210.  
  211. $order = sanitize_key( $atts['order'] );
  212.  
  213. $orderby = sanitize_key( $atts['orderby'] );
  214.  
  215. $post_parent = $atts['post_parent']; // Validated later, after check for 'current'
  216.  
  217. $post_status = $atts['post_status']; // Validated later as one of a few values
  218.  
  219. $post_type = sanitize_text_field( $atts['post_type'] );
  220.  
  221. $posts_per_page = intval( $atts['posts_per_page'] );
  222.  
  223. $tag = sanitize_text_field( $atts['tag'] );
  224.  
  225. $tax_operator = $atts['tax_operator']; // Validated later as one of a few values
  226.  
  227. $tax_term = sanitize_text_field( $atts['tax_term'] );
  228.  
  229. $taxonomy = sanitize_key( $atts['taxonomy'] );
  230.  
  231. $wrapper = sanitize_text_field( $atts['wrapper'] );
  232.  
  233. $wrapper_class = sanitize_html_class( $atts['wrapper_class'] );
  234.  
  235. if( !empty( $wrapper_class ) )
  236.  
  237. $wrapper_class = ' class="' . $wrapper_class . '"';
  238.  
  239. $wrapper_id = sanitize_html_class( $atts['wrapper_id'] );
  240.  
  241. if( !empty( $wrapper_id ) )
  242.  
  243. $wrapper_id = ' id="' . $wrapper_id . '"';
  244.  
  245.  
  246.  
  247.  
  248.  
  249. // Set up initial query for post
  250.  
  251. $args = array(
  252.  
  253. 'category_name' => $category,
  254.  
  255. 'order' => $order,
  256.  
  257. 'orderby' => $orderby,
  258.  
  259. 'post_type' => explode( ',', $post_type ),
  260.  
  261. 'posts_per_page' => $posts_per_page,
  262.  
  263. 'tag' => $tag,
  264.  
  265. );
  266.  
  267.  
  268.  
  269. // Ignore Sticky Posts
  270.  
  271. if( $ignore_sticky_posts )
  272.  
  273. $args['ignore_sticky_posts'] = true;
  274.  
  275.  
  276.  
  277. // Meta key (for ordering)
  278.  
  279. if( !empty( $meta_key ) )
  280.  
  281. $args['meta_key'] = $meta_key;
  282.  
  283.  
  284.  
  285. // Meta value (for simple meta queries)
  286.  
  287. if( !empty( $meta_value ) )
  288.  
  289. $args['meta_value'] = $meta_value;
  290.  
  291.  
  292.  
  293. // If Post IDs
  294.  
  295. if( $id ) {
  296.  
  297. $posts_in = array_map( 'intval', explode( ',', $id ) );
  298.  
  299. $args['post__in'] = $posts_in;
  300.  
  301. }
  302.  
  303.  
  304.  
  305. // If Exclude Current
  306.  
  307. if( $exclude_current )
  308.  
  309. $args['post__not_in'] = array( get_the_ID() );
  310.  
  311.  
  312.  
  313. // Post Author
  314.  
  315. if( !empty( $author ) )
  316.  
  317. $args['author_name'] = $author;
  318.  
  319.  
  320.  
  321. // Offset
  322.  
  323. if( !empty( $offset ) )
  324.  
  325. $args['offset'] = $offset;
  326.  
  327.  
  328.  
  329. // Post Status
  330.  
  331. $post_status = explode( ', ', $post_status );
  332.  
  333. $validated = array();
  334.  
  335. $available = array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash', 'any' );
  336.  
  337. foreach ( $post_status as $unvalidated )
  338.  
  339. if ( in_array( $unvalidated, $available ) )
  340.  
  341. $validated[] = $unvalidated;
  342.  
  343. if( !empty( $validated ) )
  344.  
  345. $args['post_status'] = $validated;
  346.  
  347.  
  348.  
  349.  
  350.  
  351. // If taxonomy attributes, create a taxonomy query
  352.  
  353. if ( !empty( $taxonomy ) && !empty( $tax_term ) ) {
  354.  
  355.  
  356.  
  357. // Term string to array
  358.  
  359. $tax_term = explode( ', ', $tax_term );
  360.  
  361.  
  362.  
  363. // Validate operator
  364.  
  365. if( !in_array( $tax_operator, array( 'IN', 'NOT IN', 'AND' ) ) )
  366.  
  367. $tax_operator = 'IN';
  368.  
  369.  
  370.  
  371. $tax_args = array(
  372.  
  373. 'tax_query' => array(
  374.  
  375. array(
  376.  
  377. 'taxonomy' => $taxonomy,
  378.  
  379. 'field' => 'slug',
  380.  
  381. 'terms' => $tax_term,
  382.  
  383. 'operator' => $tax_operator
  384.  
  385. )
  386.  
  387. )
  388.  
  389. );
  390.  
  391.  
  392.  
  393. // Check for multiple taxonomy queries
  394.  
  395. $count = 2;
  396.  
  397. $more_tax_queries = false;
  398.  
  399. while(
  400.  
  401. isset( $original_atts['taxonomy_' . $count] ) && !empty( $original_atts['taxonomy_' . $count] ) &&
  402.  
  403. isset( $original_atts['tax_' . $count . '_term'] ) && !empty( $original_atts['tax_' . $count . '_term'] )
  404.  
  405. ):
  406.  
  407.  
  408.  
  409. // Sanitize values
  410.  
  411. $more_tax_queries = true;
  412.  
  413. $taxonomy = sanitize_key( $original_atts['taxonomy_' . $count] );
  414.  
  415. $terms = explode( ', ', sanitize_text_field( $original_atts['tax_' . $count . '_term'] ) );
  416.  
  417. $tax_operator = isset( $original_atts['tax_' . $count . '_operator'] ) ? $original_atts['tax_' . $count . '_operator'] : 'IN';
  418.  
  419. $tax_operator = in_array( $tax_operator, array( 'IN', 'NOT IN', 'AND' ) ) ? $tax_operator : 'IN';
  420.  
  421.  
  422.  
  423. $tax_args['tax_query'][] = array(
  424.  
  425. 'taxonomy' => $taxonomy,
  426.  
  427. 'field' => 'slug',
  428.  
  429. 'terms' => $terms,
  430.  
  431. 'operator' => $tax_operator
  432.  
  433. );
  434.  
  435.  
  436.  
  437. $count++;
  438.  
  439.  
  440.  
  441. endwhile;
  442.  
  443.  
  444.  
  445. if( $more_tax_queries ):
  446.  
  447. $tax_relation = 'AND';
  448.  
  449. if( isset( $original_atts['tax_relation'] ) && in_array( $original_atts['tax_relation'], array( 'AND', 'OR' ) ) )
  450.  
  451. $tax_relation = $original_atts['tax_relation'];
  452.  
  453. $args['tax_query']['relation'] = $tax_relation;
  454.  
  455. endif;
  456.  
  457.  
  458.  
  459. $args = array_merge( $args, $tax_args );
  460.  
  461. }
  462.  
  463.  
  464.  
  465. // If post parent attribute, set up parent
  466.  
  467. if( $post_parent ) {
  468.  
  469. if( 'current' == $post_parent ) {
  470.  
  471. global $post;
  472.  
  473. $post_parent = get_the_ID();
  474.  
  475. }
  476.  
  477. $args['post_parent'] = intval( $post_parent );
  478.  
  479. }
  480.  
  481.  
  482.  
  483. // Set up html elements used to wrap the posts.
  484.  
  485. // Default is ul/li, but can also be ol/li and div/div
  486.  
  487. $wrapper_options = array( 'ul', 'ol', 'div' );
  488.  
  489. if( ! in_array( $wrapper, $wrapper_options ) )
  490.  
  491. $wrapper = 'ul';
  492.  
  493. $inner_wrapper = 'div' == $wrapper ? 'div' : 'li';
  494.  
  495.  
  496.  
  497.  
  498.  
  499. $listing = new WP_Query( apply_filters( 'display_posts_shortcode_args', $args, $original_atts ) );
  500.  
  501. if ( ! $listing->have_posts() )
  502.  
  503. return apply_filters( 'display_posts_shortcode_no_results', wpautop( $no_posts_message ) );
  504.  
  505.  
  506.  
  507. $inner = '';
  508.  
  509. while ( $listing->have_posts() ): $listing->the_post(); global $post;
  510.  
  511.  
  512.  
  513. $image = $date = $author = $excerpt = $content = '';
  514.  
  515.  
  516.  
  517. if ( $include_title )
  518.  
  519. $title = '<a class="title" href="' . apply_filters( 'the_permalink', get_permalink() ) . '">' . get_the_title() . '</a>';
  520.  
  521.  
  522.  
  523. if ( $image_size && has_post_thumbnail() )
  524.  
  525. $image = '<a class="image" href="' . get_permalink() . '">' . get_the_post_thumbnail( get_the_ID(), $image_size ) . '</a> ';
  526.  
  527.  
  528.  
  529. if ( $include_date )
  530.  
  531. $date = ' <span class="date">' . get_the_date( $date_format ) . '</span>';
  532.  
  533.  
  534.  
  535. if( $include_author )
  536.  
  537. $author = apply_filters( 'display_posts_shortcode_author', ' <span class="author">by ' . get_the_author() . '</span>' );
  538.  
  539.  
  540.  
  541. if ( $include_excerpt )
  542.  
  543. $excerpt = ' <span class="excerpt-dash">-</span> <span class="excerpt">' . get_the_excerpt() . '</span>';
  544.  
  545.  
  546.  
  547. if( $include_content ) {
  548.  
  549. add_filter( 'shortcode_atts_display-posts', 'be_display_posts_off', 10, 3 );
  550.  
  551. $content = '<div class="content">' . apply_filters( 'the_content', get_the_content() ) . '</div>';
  552.  
  553. remove_filter( 'shortcode_atts_display-posts', 'be_display_posts_off', 10, 3 );
  554.  
  555. }
  556.  
  557.  
  558.  
  559. $class = array( 'listing-item' );
  560.  
  561. $class = sanitize_html_class( apply_filters( 'display_posts_shortcode_post_class', $class, $post, $listing, $original_atts ) );
  562.  
  563. $output = '<' . $inner_wrapper . ' class="' . implode( ' ', $class ) . '">' . $image . $title . $date . $author . $excerpt . $content . '</' . $inner_wrapper . '>';
  564.  
  565.  
  566.  
  567. // If post is set to private, only show to logged in users
  568.  
  569. if( 'private' == get_post_status( get_the_ID() ) && !current_user_can( 'read_private_posts' ) )
  570.  
  571. $output = '';
  572.  
  573.  
  574.  
  575. $inner .= apply_filters( 'display_posts_shortcode_output', $output, $original_atts, $image, $title, $date, $excerpt, $inner_wrapper, $content, $class );
  576.  
  577.  
  578.  
  579. endwhile; wp_reset_postdata();
  580.  
  581.  
  582.  
  583. $open = apply_filters( 'display_posts_shortcode_wrapper_open', '<' . $wrapper . $wrapper_class . $wrapper_id . '>', $original_atts );
  584.  
  585. $close = apply_filters( 'display_posts_shortcode_wrapper_close', '</' . $wrapper . '>', $original_atts );
  586.  
  587.  
  588.  
  589. $return = $open;
  590.  
  591.  
  592.  
  593. if( $shortcode_title ) {
  594.  
  595.  
  596.  
  597. $title_tag = apply_filters( 'display_posts_shortcode_title_tag', 'h2', $original_atts );
  598.  
  599.  
  600.  
  601. $return .= '<' . $title_tag . ' class="display-posts-title">' . $shortcode_title . '</' . $title_tag . '>' . "\n";
  602.  
  603. }
  604.  
  605.  
  606.  
  607. $return .= $inner . $close;
  608.  
  609.  
  610.  
  611. return $return;
  612.  
  613. }
  614.  
  615.  
  616.  
  617. /**
  618.  
  619. * Turn off display posts shortcode
  620.  
  621. * If display full post content, any uses of [display-posts] are disabled
  622.  
  623. *
  624.  
  625. * @param array $out, returned shortcode values
  626.  
  627. * @param array $pairs, list of supported attributes and their defaults
  628.  
  629. * @param array $atts, original shortcode attributes
  630.  
  631. * @return array $out
  632.  
  633. */
  634.  
  635. function be_display_posts_off( $out, $pairs, $atts ) {
  636.  
  637. $out['display_posts_off'] = true;
  638.  
  639. return $out;
  640.  
  641. }
  642.  
  643.  
  644.  
  645. /**
  646.  
  647. * Convert string to boolean
  648.  
  649. * because (bool) "false" == true
  650.  
  651. *
  652.  
  653. */
  654.  
  655. function be_display_posts_bool( $value ) {
  656.  
  657. return !empty( $value ) && 'true' == $value ? true : false;
  658.  
  659. }
Advertisement
Add Comment
Please, Sign In to add comment