PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-content/themes/vtte-base/inc/search_filter.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 225 lines | 213 code | 7 blank | 5 comment | 26 complexity | 9f4d83ba14c5f9cd3a6181a2172620e1 MD5 | raw file
  1. <?php
  2. /*
  3. Search filter class
  4. */
  5. class search_filter {
  6. public $query;
  7. private $post_type;
  8. private $taxonomies;
  9. private $meta;
  10. private $date;
  11. private $event_date;
  12. private $posts_per_page;
  13. private $page;
  14. private $search_text;
  15. private $taxonomy_name;
  16. private $tax_array;
  17. function __construct() {
  18. //$this->search($args);
  19. }
  20. function get_current_taxonomy() {
  21. $meta = get_queried_object();
  22. if ( empty($this->taxonomy_name) ) {
  23. $tax = $meta->taxonomy;
  24. } else {
  25. $tax = $this->taxonomy_name;
  26. }
  27. return $tax;
  28. }
  29. function set_taxonomy_name($tax) {
  30. $this->taxonomy_name = $tax;
  31. }
  32. function default_post_type() {
  33. return 'post';
  34. }
  35. function set_search_text($text) {
  36. $this->search_text = $text;
  37. }
  38. function get_search_text() {
  39. return $this->search_text;
  40. }
  41. function set_page($page) {
  42. $this->page = $page;
  43. }
  44. function get_page() {
  45. if ( !empty( $this->page ) ) {
  46. return $this->page;
  47. }
  48. }
  49. function get_query() {
  50. return $this->query;
  51. }
  52. function set_taxonomies($taxonomies) {
  53. $this->taxonomies = $taxonomies;
  54. }
  55. function set_array_taxonomies($taxonomies) {
  56. $this->tax_array = $taxonomies;
  57. }
  58. function get_taxonomies() {
  59. return $this->taxonomies;
  60. }
  61. function set_post_type($post_type) {
  62. $this->post_type = $post_type;
  63. }
  64. function get_date() {
  65. return $this->date;
  66. }
  67. function set_date($date_array) {
  68. $this->date = $date_array;
  69. }
  70. function set_event_date($date_array)
  71. {
  72. $this->event_date = $date_array;
  73. }
  74. function get_post_type() {
  75. if ( !empty( $this->post_type ) ) {
  76. return $this->post_type;
  77. } else {
  78. return $this->default_post_type();
  79. }
  80. }
  81. function get_default_args() {
  82. $meta = get_queried_object();
  83. $default = array(
  84. 'post_type' => $this->get_post_type(),
  85. 'posts_per_page' => get_option('posts_per_page'),
  86. 'posts_status' => 'publish',
  87. );
  88. if (!empty($this->date)) {
  89. $default['date_query'] = array($this->date);
  90. }
  91. if (!empty($this->search_text)) {
  92. $default['s'] = $this->search_text;
  93. }
  94. if (!empty($this->page)) {
  95. $default['paged'] = $this->page;
  96. }
  97. if ( ( get_class($meta) == 'WP_Term') && ( empty( $this->taxonomies ) ) ) {
  98. $default['tax_query'] = array(
  99. array(
  100. 'taxonomy' => $meta->taxonomy,
  101. 'field' => 'slug',
  102. 'terms' => $meta->slug
  103. )
  104. );
  105. } else if ( !empty($this->taxonomies[0]) ) {
  106. $taxonomies = array();
  107. foreach ($this->taxonomies as $tax) {
  108. $taxonomies[] = array(
  109. 'taxonomy' => $this->get_current_taxonomy(),
  110. 'field' => 'slug',
  111. 'terms' => $tax
  112. );
  113. }
  114. $default['tax_query'] = $taxonomies;
  115. }
  116. if (!empty($this->tax_array) && is_array($this->tax_array)) {
  117. foreach ($this->tax_array as $tax => $term) {
  118. $taxonomies[] = array(
  119. 'taxonomy' => $tax,
  120. 'field' => 'slug',
  121. 'terms' => $term
  122. );
  123. }
  124. $default['tax_query'] = $taxonomies;
  125. }
  126. if ( !empty( $this->meta ) ) {
  127. $default['meta_query'] = $this->meta;
  128. }
  129. if ( !empty( $this->event_date ) ) {
  130. $default['meta_query'] = array();
  131. $date = $this->event_date;
  132. if (!empty($date['year']) || !empty($date['month'])) {
  133. $month = (!empty($date['month'])) ? $date['month'] : '01';
  134. $year = (!empty($date['year'])) ? $date['year'] : date('Y');
  135. $default['meta_query'][] = array(
  136. 'key' => 'event_date',
  137. 'value' => $year.'-'.$month.'-01',
  138. 'compare' => '>=',
  139. 'type' => 'DATE'
  140. );
  141. $month = (!empty($date['month'])) ? $date['month'] : '12';
  142. $default['meta_query'][] = array(
  143. 'key' => 'event_date',
  144. 'value' => $year . '-'.$month.'-31',
  145. 'compare' => '<=',
  146. 'type' => 'DATE'
  147. );
  148. }
  149. $default['meta_key'] = 'event_date';
  150. $default['orderby'] = 'meta_value';
  151. $default['order'] = 'DESC';
  152. }
  153. //echo '<pre>'; print_r($default); echo '</pre>';
  154. return $default;
  155. }
  156. function search($args = null) {
  157. $default = wp_parse_args($this->get_default_args(),$args=false);
  158. $this->query = new WP_Query($default);
  159. return $this->query;
  160. }
  161. function get_years($from='2012') {
  162. $years = array();
  163. for ( $x = $from; $x <= date(Y); $x++) {
  164. $years[] = $x;
  165. }
  166. return $years;
  167. }
  168. function get_years_select($attributes=null, $selected=null) {
  169. if ( !empty($attributes ) && is_array( $attributes ) ) {
  170. $print_attr = '';
  171. foreach ($attributes as $key => $val) {
  172. $print_attr .= ' '.$key.'="'.$val.'" ';
  173. }
  174. }
  175. $years = $this->get_years();
  176. $out = '<select '.$print_attr.' >';
  177. $out .= '<option value="">Año</option>';
  178. foreach ( $years as $year ) {
  179. $select = ($year == $selected ) ? ' selected="selected"': '';
  180. $out .= '<option value="'.$year.'"'.$select.'>'.$year.'</option>';
  181. }
  182. $out .='</select>';
  183. return $out;
  184. }
  185. function get_months() {
  186. return array(
  187. 1 => 'Ene',
  188. 2 => 'Feb',
  189. 3 => 'Mar',
  190. 4 => 'Abr',
  191. 5 => 'May',
  192. 6 => 'Jun',
  193. 7 => 'Jul',
  194. 8 => 'Ago',
  195. 9 => 'Sept',
  196. 10 => 'Oct',
  197. 11 => 'Nov',
  198. 12 => 'Dic'
  199. );
  200. }
  201. function get_months_select($attributes=null, $selected=null) {
  202. if ( !empty($attributes ) && is_array( $attributes ) ) {
  203. $print_attr = '';
  204. foreach ($attributes as $key => $val) {
  205. $print_attr .= ' '.$key.'="'.$val.'" ';
  206. }
  207. }
  208. $months = $this->get_months();
  209. $out = '<select '.$print_attr.' >';
  210. $out .= '<option value="">Mes</option>';
  211. foreach ( $months as $key => $month ) {
  212. $select = ($key == $selected ) ? ' selected="selected"': '';
  213. $out .= '<option value="'.$key.'"'.$select.'>'.$month.'</option>';
  214. }
  215. $out .='</select>';
  216. return $out;
  217. }
  218. }