Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. <?php
  2. /**
  3. * Utility functions
  4. */
  5. function is_element_empty($element) {
  6. $element = trim($element);
  7. return !empty($element);
  8. }
  9.  
  10. // Tell WordPress to use searchform.php from the templates/ directory
  11. function roots_get_search_form($form) {
  12. $form = '';
  13. locate_template('/templates/searchform.php', true, false);
  14. return $form;
  15. }
  16. add_filter('get_search_form', 'roots_get_search_form');
  17.  
  18.  
  19. //CONTENT HELPERS
  20.  
  21. function get_calendar_page($params = array()) {
  22.  
  23. $params = (!empty($params) ? $params : (!empty($_POST) ? $_POST : array()));
  24.  
  25. $month = ( !empty($params['month']) ? $params['month'] : date('m') );
  26. $year = ( !empty($params['year']) ? $params['year'] : date('Y') );
  27.  
  28. ob_start();
  29. include(locate_template('/templates/snippets/events-calendar.php'));
  30. $html = ob_get_contents();
  31.  
  32. ob_end_clean();
  33.  
  34. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
  35. die(json_encode($html));
  36. } else {
  37. echo $html;
  38. }
  39. return;
  40. }
  41. add_action("wp_ajax_nopriv_get_calendar_page", "get_calendar_page");
  42. add_action("wp_ajax_get_calendar_page", "get_calendar_page");
  43.  
  44.  
  45.  
  46. function get_events($params = array()) {
  47.  
  48. $params = (!empty($params) ? $params : (!empty($_POST) ? $_POST : array()));
  49.  
  50. if(!empty($params['paged'])) $custom_args['paged'] = $params['paged'];
  51. if(!empty($params['posts_per_page'])) $custom_args['posts_per_page'] = $params['posts_per_page'];
  52.  
  53. $meta_query = array('relation' => 'AND');
  54. $tax_query = array('relation' => 'AND');
  55.  
  56. if(!empty($params['tax'])):
  57. foreach ($params['tax'] as $tax_key => $tax):
  58. if(!empty($tax) && $tax && $tax != "null"):
  59. if(!is_array($tax)) $tax = array( $tax );
  60. $tax_query[] = array(
  61. 'taxonomy' => $tax_key,
  62. 'field' => 'term_id',
  63. 'terms' => $tax,
  64. );
  65. endif;
  66. endforeach;
  67. endif;
  68.  
  69. if( !empty($params['date']) && $params['date'] && $params['date'] != "null"):
  70. $date_start = strtotime( date('d-m-Y', $params['date']) );
  71. $date_end = strtotime( date('d-m-Y', $params['date']) . ' + 1day' );
  72. $meta_query[] = array(
  73. array(
  74. "key" => "event_start_time",
  75. "compare" => "BETWEEN",
  76. "value" => array( $date_start, $date_end ),
  77. )
  78. );
  79. endif;
  80.  
  81.  
  82. $default_args = array(
  83. 'post_type' => 'event',
  84. "orderby" => "event_start_time",
  85. "order" => "ASC",
  86. "posts_per_page" => -1,
  87. "paged" => 1,
  88. 'tax_query' => $tax_query,
  89. 'meta_query' => $meta_query,
  90. );
  91.  
  92.  
  93. $args = array_merge( $default_args, $custom_args );
  94.  
  95. $query = new WP_Query( $args );
  96.  
  97. ob_start();
  98.  
  99. /* ?><div class="result-item grid-item col-sm-12"><pre><?php
  100. print_r($query->request);
  101. ?></pre></div><?php */
  102.  
  103. if ( $query->have_posts() ): ?>
  104. <?php while ( $query->have_posts() ): $query->the_post(); ?>
  105. <?php get_template_part('templates/snippets/event-item'); ?>
  106. <?php endwhile; ?>
  107. <?php endif;
  108.  
  109. $html = ob_get_contents();
  110.  
  111. ob_end_clean();
  112.  
  113. wp_reset_query();
  114.  
  115. $return = array(
  116. 'found_posts' => $query->found_posts,
  117. 'max_num_pages' => $query->max_num_pages,
  118. 'query_id' => $_POST['query_id'],
  119. 'html' => $html,
  120. 'mq' => $meta_query,
  121. );
  122.  
  123. if(!empty($_POST['query_id'])):
  124. die(json_encode($return));
  125. else:
  126. return $return;
  127. endif;
  128.  
  129. }
  130.  
  131. add_action("wp_ajax_nopriv_get_events", "get_events");
  132. add_action("wp_ajax_get_events", "get_events");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement