Guest User

Untitled

a guest
Sep 8th, 2014
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.70 KB | None | 0 0
  1. <?php
  2. class td_data_source {
  3.  
  4. static $fake_loop_offset = 0; //used by the found row hook in templates to fix pagination. The blocks do not use this since we use custom pagination there.
  5.  
  6.  
  7. /**
  8. * converts a pagebuilde array to a wordpress query args array
  9. * creates the $args array from shortcodes - used by the pagebuilde + widgets + by the metabox_to_args
  10. * @param string $atts : the shortcode string
  11. * @param string $paged : page number /1 or /2
  12. * @return array
  13. */
  14. static function shortcode_to_args($atts = '', $paged = '') {
  15. extract(shortcode_atts(
  16. array(
  17. 'category_ids' => '',
  18. 'category_id' => '',
  19. 'tag_slug' => '',
  20. 'sort' => '',
  21. 'limit' => '', /*'limit' => 5,*/
  22. 'autors_id' => '',
  23. 'installed_post_types' => '',
  24. 'posts_per_page' => '',
  25. 'offset' => ''
  26. ),
  27. $atts
  28. )
  29. );
  30.  
  31. //init the array
  32. $wp_query_args = array(
  33. 'ignore_sticky_posts' => 1
  34. );
  35.  
  36. //the query goes only via $category_ids - for both options ($category_ids and $category_id) also $category_ids overwrites $category_id
  37. if (!empty($category_id) and empty($category_ids)) {
  38. $category_ids = $category_id;
  39. }
  40.  
  41.  
  42. if (!empty($category_ids)) {
  43. $wp_query_args['cat'] = $category_ids;
  44. }
  45.  
  46. if (!empty($tag_slug)) {
  47. $wp_query_args['tag'] = str_replace(' ', '-', $tag_slug);
  48. }
  49.  
  50. // $current_day = date('j');
  51.  
  52. switch ($sort) {
  53. case 'featured':
  54. if (!empty($category_ids)) {
  55. //for each category, get the object and compose the slug
  56. $cat_id_array = explode (',', $category_ids);
  57.  
  58. foreach ($cat_id_array as &$cat_id) {
  59. $cat_id = trim($cat_id);
  60.  
  61. //get the category object
  62. $td_tmp_cat_obj = get_category($cat_id);
  63.  
  64. //make the $args
  65. if (empty($wp_query_args['category_name'])) {
  66. $wp_query_args['category_name'] = $td_tmp_cat_obj->slug; //get by slug (we get the children categories too)
  67. } else {
  68. $wp_query_args['category_name'] .= ',' . $td_tmp_cat_obj->slug; //get by slug (we get the children categories too)
  69. }
  70. unset($td_tmp_cat_obj);
  71. }
  72. }
  73.  
  74. $wp_query_args['cat'] = get_cat_ID(TD_FEATURED_CAT); //add the fetured cat
  75. break;
  76. case 'popular':
  77. $wp_query_args['meta_key'] = td_page_views::$post_view_counter_key;
  78. $wp_query_args['orderby'] = 'meta_value_num';
  79. $wp_query_args['order'] = 'DESC';
  80. break;
  81. case 'popular7':
  82. $wp_query_args['meta_key'] = td_page_views::$post_view_counter_7_day_total;
  83. $wp_query_args['orderby'] = 'meta_value_num';
  84. $wp_query_args['order'] = 'DESC';
  85. break;
  86. case 'review_high':
  87. $wp_query_args['meta_key'] = td_review::$td_review_key;
  88. $wp_query_args['orderby'] = 'meta_value_num';
  89. $wp_query_args['order'] = 'DESC';
  90. break;
  91. case 'random_posts':
  92. $wp_query_args['orderby'] = 'rand';
  93. break;
  94. case 'alphabetical_order':
  95. $wp_query_args['orderby'] = 'title';
  96. $wp_query_args['order'] = 'ASC';
  97. break;
  98. case 'comment_count':
  99. $wp_query_args['orderby'] = 'comment_count';
  100. $wp_query_args['order'] = 'DESC';
  101. break;
  102. case 'random_today':
  103. $wp_query_args['orderby'] = 'rand';
  104. $wp_query_args['day'] = date('j');
  105. break;
  106. case 'random_7_day':
  107. $wp_query_args['orderby'] = 'rand';
  108. $wp_query_args['date_query'] = array(
  109. 'column' => 'post_date_gmt',
  110. 'after' => '1 week ago'
  111. );
  112. break;
  113. }
  114.  
  115. if (!empty($autors_id)) {
  116. $wp_query_args['author'] = $autors_id;
  117. }
  118.  
  119. //add post_type to query
  120. if (!empty($installed_post_types)) {
  121. $array_selected_post_types = array();
  122. $expl_installed_post_types = explode(',', $installed_post_types);
  123.  
  124. foreach ($expl_installed_post_types as $val_this_post_type) {
  125. if (trim($val_this_post_type) != '') {
  126. $array_selected_post_types[] = trim($val_this_post_type);
  127. }
  128. }
  129.  
  130. $wp_query_args['post_type'] = $array_selected_post_types;//$installed_post_types;
  131. }
  132.  
  133. //only show published posts
  134. $wp_query_args['post_status'] = 'publish';
  135.  
  136. //show only unique posts if that setting is enabled on the template
  137. if (td_unique_posts::$unique_articles_enabled == true) {
  138. $wp_query_args['post__not_in'] = td_unique_posts::$rendered_posts_ids;
  139. }
  140.  
  141.  
  142.  
  143. //custom pagination limit
  144. if (empty($limit)) {
  145. $limit = get_option('posts_per_page');
  146. }
  147. $wp_query_args['posts_per_page'] = $limit;
  148.  
  149.  
  150. //custom pagination
  151. if (!empty($paged)) {
  152. $wp_query_args['paged'] = $paged;
  153. } else {
  154. $wp_query_args['paged'] = 1;
  155. }
  156.  
  157.  
  158. // offset + custom pagination - if we have offset, wordpress overwrites the pagination and works with offset + limit
  159. if (!empty($offset) and $paged > 1) {
  160. $wp_query_args['offset'] = $offset + ( ($paged - 1) * $limit) ;
  161. } else {
  162. $wp_query_args['offset'] = $offset ;
  163. }
  164.  
  165.  
  166. //set this variable to pass it to the filter that fixes the pagination on the templates with fake loops. It is not used on blocks because the blocks have custom pagination
  167. self::$fake_loop_offset = $offset;
  168.  
  169.  
  170. //print_r($wp_query_args);
  171.  
  172. return $wp_query_args;
  173. }
  174.  
  175.  
  176.  
  177.  
  178. /**
  179. * converts a post metabox value array to a wordpress query args array
  180. * @param $td_homepage_loop_filter - the post loop filer metadata array
  181. * @param string $paged
  182. * @return array
  183. */
  184. static function metabox_to_args($td_homepage_loop_filter, $paged = '') {
  185.  
  186.  
  187. $wp_query_args = self::shortcode_to_args($td_homepage_loop_filter, $paged);
  188.  
  189.  
  190.  
  191. //$wp_query_args['paged'] = $paged;
  192.  
  193. if (!empty($td_homepage_loop_filter['show_featured_posts'])) {
  194. if (empty($wp_query_args['cat'])) {
  195. $wp_query_args['cat'] = '-' . get_cat_ID(TD_FEATURED_CAT);
  196. } else {
  197. $wp_query_args['cat'] .= ',-' . get_cat_ID(TD_FEATURED_CAT);
  198. }
  199. }
  200.  
  201.  
  202. $wp_query_args['ignore_sticky_posts'] = 0;
  203.  
  204. // custom pagination for the fake template loops
  205. if (isset($wp_query_args['offset']) and $wp_query_args['offset'] > 0) {
  206. //fix reported posts for the fake loops
  207. add_filter('found_posts', array(__CLASS__, 'hook_fix_offset_pagination'), 1, 2 );
  208. }
  209.  
  210.  
  211. //print_r($wp_query_args);
  212.  
  213. return $wp_query_args;
  214. }
  215.  
  216. // custom pagination for the fake template loops - used by hook
  217. static function hook_fix_offset_pagination($found_posts, $query) {
  218. remove_filter('found_posts','hook_fix_offset_pagination');
  219. return $found_posts - td_data_source::$fake_loop_offset;
  220. }
  221.  
  222.  
  223.  
  224.  
  225.  
  226. /**
  227. * is used by all the blocks
  228. * @param string $atts
  229. * @param string $paged - is used by ajax
  230. * @return WP_Query
  231. */
  232. static function &get_wp_query ($atts = '', $paged = '') { //by ref
  233. $args = self::shortcode_to_args($atts, $paged);
  234. $td_query = new WP_Query($args);
  235. return $td_query;
  236. }
  237.  
  238.  
  239. /**
  240. * used by the ajax search feature
  241. * @param $search_string
  242. * @return WP_Query
  243. */
  244. static function &get_wp_query_search($search_string) {
  245. $args = array(
  246. 's' => $search_string,
  247. 'post_type' => array('post'),
  248. 'posts_per_page' => 4,
  249. 'post_status' => 'publish'
  250. );
  251.  
  252. $td_query = new WP_Query($args);
  253. return $td_query;
  254. }
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262. }
Advertisement
Add Comment
Please, Sign In to add comment