Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.54 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Functionality relevant to posts (single and multiple)
  5.  */
  6. class Bunyad_Posts
  7. {
  8.         public $more_text;
  9.         public $more_html;
  10.        
  11.         public function __construct()
  12.         {
  13.                 // add offsets support
  14.                 add_filter('found_posts', array($this, 'fix_offset_pagination'), 10, 2);
  15.                 add_action('pre_get_posts', array($this, 'pre_get_posts'));
  16.         }
  17.        
  18.         /**
  19.          * Custom excerpt function - utilize existing wordpress functions to add real support for <!--more-->
  20.          * to excerpts which is missing in the_excerpt().
  21.          *
  22.          * Maintain plugin functionality by utilizing wordpress core functions and filters.
  23.          *
  24.          * @param  string|null  $text
  25.          * @param  integer  $length
  26.          * @param  array   $options   add_more: add more if needed, more_text = more link anchor, force_more: always add more link
  27.          * @return string
  28.          */
  29.         public function excerpt($text = null, $length = 55, $options = array())
  30.         {
  31.                 global $more;
  32.  
  33.                 // add defaults
  34.                 $options = array_merge(array('add_more' => null, 'force_more' => null), $options);
  35.                
  36.                 // add support for <!--more--> cut-off on custom home-pages and the like
  37.                 $old_more = $more;
  38.                 $more = false;
  39.                
  40.                 // override options
  41.                 $more_text = $this->more_text;
  42.                 extract($options);
  43.  
  44.                 // set global more_text - used by excerpt_read_more()
  45.                 $this->more_text = $more_text;
  46.                
  47.                 if (!$text) {
  48.                        
  49.                         // have a manual excerpt?
  50.                         if (has_excerpt()) {
  51.                                 return apply_filters('the_excerpt', get_the_excerpt()) . ($force_more ? $this->excerpt_read_more() : '');
  52.                         }
  53.                        
  54.                         // don't add "more" link
  55.                         $text = get_the_content('');
  56.                 }
  57.                
  58.                 $text = strip_shortcodes(apply_filters('bunyad_excerpt_pre_strip_shortcodes', $text));
  59.                 $text = str_replace(']]>', ']]&gt;', $text);
  60.                
  61.                 // get plaintext excerpt trimmed to right length
  62.                 $excerpt = wp_trim_words($text, $length, apply_filters('bunyad_excerpt_hellip', '&hellip;') . ($add_more !== false ? $this->excerpt_read_more() : ''));
  63.  
  64.  
  65.                 /*
  66.                  * Force "More" link?
  67.                  *
  68.                  * wp_trim_words() will only add the read more link if it's needed - if the length actually EXCEEDS. In some cases,
  69.                  * for styling, read more has to be always present.
  70.                  *
  71.                  */
  72.                 if ($force_more) {
  73.                        
  74.                         $read_more = $this->excerpt_read_more();
  75.  
  76.                         if (substr($excerpt, -strlen($read_more)) !== $read_more) {
  77.                                 $excerpt .= $this->excerpt_read_more();
  78.                         }
  79.                 }
  80.                
  81.                 // fix extra spaces
  82.                 $excerpt = trim(str_replace('&nbsp;', ' ', $excerpt));
  83.                
  84.                 // apply filters after to prevent added html functionality from being stripped
  85.                 // REMOVED: the_content filters often clutter the HTML - use the_excerpt filter instead
  86.                 // $excerpt = apply_filters('the_content', $excerpt);
  87.                
  88.                 $excerpt = apply_filters('the_excerpt', apply_filters('get_the_excerpt', $excerpt));
  89.                
  90.                 // revert
  91.                 $more = $old_more;
  92.                
  93.                 return $excerpt;
  94.         }
  95.        
  96.         /**
  97.          * Wrapper for the_content()
  98.          *
  99.          * @see the_content()
  100.          */
  101.         public function the_content($more_link_text = null, $strip_teaser = false)
  102.         {
  103.                 // add <!--more--> support on static pages when not using excerpts
  104.                 if (is_page()) {
  105.                         global $more;
  106.                         $more = 0;
  107.                 }
  108.                
  109.                 // get the content
  110.                 $content = get_the_content($more_link_text, $strip_teaser);
  111.                
  112.                 // delete first gallery shortcode if featured area is enabled
  113.                 if (get_post_format() == 'gallery' && !$this->meta('featured_disable')) {
  114.                         $content = $this->_strip_shortcode_gallery($content);
  115.                 }
  116.  
  117.                 // apply bunyad_main_content filters first - for page builder
  118.                 $content = apply_filters('the_content',  $content, 'bunyad_main_content');
  119.                 $content = str_replace(']]>', ']]&gt;', $content);
  120.  
  121.                 echo $content;   // pre-filtered/escaped content from get_the_content()
  122.         }
  123.  
  124.         /**
  125.          * Deletes first gallery shortcode and returns content
  126.          */
  127.         public function _strip_shortcode_gallery($content)
  128.         {
  129.             preg_match_all('/'. get_shortcode_regex() .'/s', $content, $matches, PREG_SET_ORDER);
  130.            
  131.             if (!empty($matches))
  132.             {
  133.                 foreach ($matches as $shortcode)
  134.                 {
  135.                     if ('gallery' === $shortcode[2])
  136.                     {
  137.                         $pos = strpos($content, $shortcode[0]);
  138.                         if ($pos !== false) {
  139.                             return substr_replace($content, '', $pos, strlen($shortcode[0]));
  140.                         }
  141.                     }
  142.                 }
  143.             }
  144.            
  145.             return $content;
  146.         }
  147.        
  148.        
  149.         public function get_first_gallery_ids($content = null)
  150.         {
  151.                 if (!$content) {
  152.                         $content = get_the_content();
  153.                 }
  154.                
  155.             preg_match_all('/'. get_shortcode_regex() .'/s', $content, $matches, PREG_SET_ORDER);
  156.            
  157.             if (!empty($matches))
  158.             {
  159.                 foreach ($matches as $shortcode)
  160.                 {
  161.                     if ('gallery' === $shortcode[2])
  162.                     {
  163.                         $atts = shortcode_parse_atts($shortcode[3]);
  164.                        
  165.                         if (!empty($atts['ids'])) {
  166.                                 $ids = explode(',', $atts['ids']);
  167.                                
  168.                                 return $ids;
  169.                         }
  170.                     }
  171.                 }
  172.             }
  173.            
  174.             return false;
  175.         }
  176.        
  177.         /**
  178.          * Get custom post meta
  179.          *
  180.          * @param string|null $key
  181.          * @param integer|null $post_id
  182.          * @param boolean $defaults  whether or not to use default options mapped to certain keys - only when $key is set
  183.          */
  184.         public function meta($key = null, $post_id = null, $defaults = true)
  185.         {
  186.                 $prefix = Bunyad::options()->get_config('meta_prefix') . '_';
  187.                
  188.                 if (!$post_id) {
  189.                         global $post;
  190.                         $post_id = $post->ID;
  191.                 }
  192.                
  193.                 if (is_string($key)) {
  194.                        
  195.                         $meta = get_post_meta($post_id, $prefix . $key, true);
  196.  
  197.                         if ($defaults) {
  198.                        
  199.                                 $default_map = array('featured_disable' => array('key' => 'show_featured', 'bool_inverse' => true));
  200.                                
  201.                                 // have a key association with theme settings?
  202.                                 if (!$meta && array_key_exists($key, $default_map)) {
  203.                                        
  204.                                         $expression = Bunyad::options()->get($default_map[$key]['key']);
  205.                                                                                
  206.                                         return ($default_map[$key]['bool_inverse'] ? !$expression : $expression);
  207.                                 }
  208.                         }
  209.                        
  210.                         return apply_filters('bunyad_meta_' . $key, $meta);
  211.                        
  212.                 }
  213.                 else
  214.                 {
  215.                         $meta     = get_post_custom($post_id);
  216.                         $new_meta = array();
  217.                         foreach ($meta as $key => $value)
  218.                         {
  219.                                 // prefix at beginning?
  220.                                 if (strpos($key, $prefix) === 0) {
  221.                                         $new_meta[$key] = $this->_fix_meta_value($value);
  222.                                 }
  223.                                
  224.                         }
  225.                        
  226.                         return $new_meta;
  227.                 }
  228.         }
  229.        
  230.         // helper to fix meta value
  231.         private function _fix_meta_value($value) {
  232.                 if (count($value) === 1) {
  233.                         return $value[0];
  234.                 }
  235.                
  236.                 return $value;
  237.         }
  238.        
  239.        
  240.         /**
  241.          * Get meta for page first if available
  242.          */
  243.         public function page_meta($key = null, $post_id = null)
  244.         {
  245.                 global $page;
  246.                
  247.                 if (!$post_id) {
  248.                         $post_id = $page->ID;
  249.                 }
  250.                
  251.                 return $this->meta($key, $post_id);
  252.         }
  253.        
  254.         /**
  255.          * Get related posts
  256.          *
  257.          * @param integer $count number of posts to return
  258.          * @param integer|null $post_id
  259.          */
  260.         public function get_related($count = 5, $post_id = null)
  261.         {
  262.                 if (!$post_id) {
  263.                         global $post;
  264.                         $post_id = $post->ID;
  265.                 }
  266.                                
  267.                 $args = array(
  268.                         'numberposts' => $count,
  269.                         'post__not_in' => array($post_id)
  270.                 );
  271.                
  272.                 // get related posts using tags or categories?        
  273.                 if (Bunyad::options()->related_posts_by == 'tags') {
  274.                         $args['tag__in'] = wp_get_post_tags($post_id, array('fields' => 'ids'));
  275.                 }
  276.                 else {
  277.                         $args['category__in'] = wp_get_post_categories($post_id);
  278.                 }
  279.                
  280.                 $related = get_posts(apply_filters('bunyad_get_related_query', $args));
  281.  
  282.                 return (array) $related;
  283.                
  284.         }
  285.        
  286.         /**
  287.          * Custom pagination
  288.          *
  289.          * @param array $options extend options for paginate_links()
  290.          * @see paginate_links()
  291.          */
  292.         public function paginate($options = array(), $query = null)
  293.         {
  294.                 global $wp_rewrite;
  295.  
  296.                 if (!$query) {
  297.                         global $wp_query;
  298.                         $query = $wp_query;
  299.                 }
  300.                
  301.                 $total_pages = $query->max_num_pages;
  302.                 if ($total_pages <= 1) {
  303.                         return '';
  304.                 }
  305.                
  306.                 // use page on static front-page and paged on others
  307.                 // non-static home-page uses paged as well
  308.                 $paged = (get_query_var('paged') ? get_query_var('paged') : get_query_var('page'));
  309.                
  310.                 $args = array(
  311.                         'base'    => add_query_arg('paged', '%#%'),
  312.                         'format'  => '',  
  313.                         'current' => max(1, $paged),
  314.                         'total'   => $total_pages,
  315.  
  316.                         // accessibility + fontawesome for pagination links
  317.                         'next_text' => '<span class="visuallyhidden">' . _x('Next', 'pagination', 'bunyad') . '</span><i class="fa fa-angle-right"></i>',
  318.                         'prev_text' => '<i class="fa fa-angle-left"></i><span class="visuallyhidden">' . _x('Previous', 'pagination', 'bunyad') . '</span>'
  319.                 );
  320.        
  321.                 if ($wp_rewrite->using_permalinks()) {
  322.                         $args['base'] = user_trailingslashit(trailingslashit(remove_query_arg('s', get_pagenum_link(1))) . 'page/%#%', 'paged');
  323.                 }
  324.                
  325.                 if (is_search()) {
  326.                         $args['add_args'] = array('s' => urlencode(get_query_var('s')));
  327.                 }
  328.                
  329.                 $pagination = paginate_links(array_merge($args, $options));
  330.  
  331.                 // remove first paged=1 from the first link
  332.                 $pagination = preg_replace('/&#038;paged=1(\'|")/', '\\1', trim($pagination));
  333.                
  334.                 // add wrapper?
  335.                 if (!empty($options['wrapper_before'])) {
  336.                         $pagination = $options['wrapper_before'] . $pagination . $options['wrapper_after'];
  337.                 }
  338.        
  339.                 return $pagination;    
  340.         }
  341.        
  342.         /**
  343.          * Fix query LIMIT when using offsets
  344.          *
  345.          * @param object $query
  346.          */
  347.         public function fix_query_offset(&$query)
  348.         {
  349.                 if (empty($query->query_vars['offset']) OR empty($query->query_vars['orig_offset'])) {
  350.                         return;
  351.                 }
  352.                
  353.                 if ($query->is_paged) {
  354.                
  355.                         // manually determine page query offset (offset + current page (minus one) x posts per page)
  356.                         $page_offset = $query->query_vars['offset'] + (($query->query_vars['paged'] - 1) * $query->query_vars['posts_per_page']);
  357.                         $query->set('offset', $page_offset);
  358.                                
  359.                 }
  360.                 else {
  361.                         // first page? just use the offset
  362.                         $query->set('offset', $query->query_vars['offset']);
  363.                 }
  364.         }
  365.        
  366.         /**
  367.          * Preserve original offset query var as it will be changed
  368.          *
  369.          * @param object $query
  370.          */
  371.         public function add_query_offset($query = array())
  372.         {
  373.                 if (isset($query['offset'])) {
  374.                         $query['orig_offset'] = $query['offset'];
  375.                 }
  376.                
  377.                 return $query;
  378.         }      
  379.        
  380.         /**
  381.          * A wrapper for common pre_get_posts filters
  382.          *
  383.          * @param object $query
  384.          */
  385.         public function pre_get_posts(&$query)
  386.         {
  387.                 $this->fix_query_offset($query);
  388.         }
  389.        
  390.  
  391.         /**
  392.          * Fix found_posts when an offset is set
  393.          *
  394.          * WordPress found_posts doesn't account for offset.
  395.  
  396.          * @param integer $found_posts
  397.          * @param object $query
  398.          */
  399.         public function fix_offset_pagination($found_posts, $query)
  400.         {
  401.  
  402.                 if (empty($query->query_vars['offset']) OR empty($query->query_vars['orig_offset'])) {
  403.                         return $found_posts;
  404.                 }
  405.                
  406.                 $offset = $query->query_vars['orig_offset'];
  407.        
  408.                 // reduce WordPress's found_posts count by the offset...
  409.                 return $found_posts - $offset;
  410.         }
  411.        
  412.         /**
  413.          * Add the read more text to excerpts
  414.          */
  415.         public function excerpt_read_more()
  416.         {
  417.                 global $post;
  418.                
  419.                 if (is_feed()) {
  420.                         return ' [...]';
  421.                 }
  422.  
  423.                 // add more link if enabled in options
  424.                 if (Bunyad::options()->read_more) {
  425.                        
  426.                         $text = $this->more_text;
  427.                         if (!$text) {
  428.                                 $text = __('', 'bunyad');
  429.                         }
  430.                        
  431.                         if (empty($this->more_html)) {
  432.                                 $this->more_html = '';
  433.                         }
  434.  
  435.                         return sprintf(apply_filters('bunyad_read_more_html', $this->more_html), get_permalink($post->ID), esc_attr($text), $text);
  436.                 }
  437.                
  438.                 return '';
  439.         }
  440. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement