PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/events-manager/classes/em-taxonomy-frontend.php

https://gitlab.com/pankajmohale/chef2go
PHP | 177 lines | 123 code | 9 blank | 45 comment | 23 complexity | 52094770da8a3afdbffcae8d5a38af1b MD5 | raw file
  1. <?php
  2. class EM_Taxonomy_Frontend {
  3. /**
  4. * The name of this taxonomy, e.g. event-categories, which is defined in child class.
  5. * @var string
  6. */
  7. public static $taxonomy_name = 'EM_TAXONOMY_NAME';
  8. /**
  9. * The name of the child class, used for now whilst late static binding isn't guaranteed since we may be running on PHP <5.3
  10. * Once PHP 5.3 is a minimum requirement in WP, we can get rid of this one.
  11. * @var string
  12. */
  13. public static $this_class = 'EM_Taxonomy_Frontend';
  14. /**
  15. * Currently used to instantiate a class of the specific term. Eventually we could just use EM_Taxonomy since these will be standardized functions for any taxonomy.
  16. * @var string
  17. */
  18. public static $tax_class = 'EM_Taxonomy';
  19. /**
  20. * Name of taxonomy for reference in saving to database, e.g. category will be used to save category-image.
  21. * This may differ from the name of the taxonomy, such as event-category can be category
  22. * @var string
  23. */
  24. public static $option_name = 'taxonomy';
  25. public static $option_name_plural = 'taxonomies';
  26. public static function init(){
  27. if( !is_admin() ){
  28. add_filter('taxonomy_template', array(self::$this_class,'template'), 99);
  29. add_filter('parse_query', array(self::$this_class,'parse_query'));
  30. }
  31. }
  32. /**
  33. * Overrides archive pages e.g. locations, events, event categories, event tags based on user settings
  34. * @param string $template
  35. * @return string
  36. */
  37. public static function template($template = ''){
  38. global $wp_query, $wp_the_query, $em_the_query, $post;
  39. if( is_tax(self::$taxonomy_name) && !locate_template('taxonomy-'.self::$taxonomy_name.'.php') && get_option('dbem_cp_'. self::$option_name_plural .'_formats', true) ){
  40. $em_the_query = $wp_the_query; //use this for situations where other plugins need to access 'original' query data, which you can switch back/forth.
  41. $EM_Taxonomy = $GLOBALS[self::$tax_class] = EM_Taxonomy_Term::get($wp_query->queried_object->term_id, self::$tax_class);
  42. if( self::get_page_id() ){
  43. //less chance for things to go wrong with themes etc. so just reset the WP_Query to think it's a page rather than taxonomy
  44. $wp_query = new WP_Query(array('page_id'=> self::get_page_id()));
  45. $wp_query->queried_object = $wp_query->post;
  46. $wp_query->queried_object_id = $wp_query->post->ID;
  47. $wp_query->post->post_title = $wp_query->posts[0]->post_title = $wp_query->queried_object->post_title = $EM_Taxonomy->output(get_option('dbem_'. self::$option_name .'_page_title_format'));
  48. if( !function_exists('yoast_breadcrumb') ){ //not needed by WP SEO Breadcrumbs, we deal with it in a filter further down - wpseo_breadcrumb_links
  49. $wp_query->post->post_parent = $wp_query->posts[0]->post_parent = $wp_query->queried_object->post_parent = self::get_page_id();
  50. }
  51. $post = $wp_query->post;
  52. $wp_the_query = $wp_query; //we won't do this to the else section because we should deprecate it due to its instability
  53. }else{
  54. //we don't have a categories page, so we create a fake page
  55. $wp_query->posts = array();
  56. $wp_query->posts[0] = new stdClass();
  57. $wp_query->posts[0]->post_title = $wp_query->queried_object->post_title = $EM_Taxonomy->output(get_option('dbem_'. self::$option_name .'_page_title_format'));
  58. $post_array = array('ID', 'post_author', 'post_date','post_date_gmt','post_content','post_excerpt','post_status','comment_status','ping_status','post_password','post_name','to_ping','pinged','post_modified','post_modified_gmt','post_content_filtered','post_parent','guid','menu_order','post_type','post_mime_type','comment_count','filter');
  59. foreach($post_array as $post_array_item){
  60. $wp_query->posts[0]->$post_array_item = '';
  61. }
  62. $wp_query->post = $wp_query->posts[0];
  63. $wp_query->post_count = 1;
  64. $wp_query->found_posts = 1;
  65. $wp_query->max_num_pages = 1;
  66. //tweak flags for determining page type
  67. $wp_query->is_tax = 0;
  68. $wp_query->is_page = 1;
  69. $wp_query->is_single = 0;
  70. $wp_query->is_singular = 1;
  71. $wp_query->is_archive = 0;
  72. }
  73. //set taxonomy id to globals and query object
  74. $em_taxonomy_property = 'em_'. self::$option_name .'_id';
  75. $wp_query->{$em_taxonomy_property} = $wp_the_query->{$em_taxonomy_property} = $GLOBALS[$em_taxonomy_property] = $EM_Taxonomy->term_id; //we assign global taxononmy id just in case other themes/plugins do something out of the ordinary to WP_Query
  76. //set the template
  77. $template = locate_template(array('page.php','index.php'),false); //category becomes a page
  78. //sort out filters
  79. add_filter('wp_head', 'EM_Taxonomy_Frontend::remove_em_the_content', 10000);
  80. add_filter('the_content', array(self::$this_class,'the_content')); //come in slightly early and consider other plugins
  81. //Yoast WP SEO Tweals
  82. if( defined('WPSEO_VERSION') ){
  83. add_filter('wpseo_breadcrumb_links',array(self::$this_class,'wpseo_breadcrumb_links'));
  84. add_filter('wpseo_head', array(self::$this_class,'flip_the_query'), 1);
  85. add_filter('wpseo_head', array(self::$this_class,'flip_the_query'), 1000000);
  86. }
  87. do_action('em_'. self::$option_name .'_taxonomy_template');
  88. }
  89. return $template;
  90. }
  91. public static function the_content($content){
  92. global $wp_query, $post;
  93. $em_taxonomy_property = 'em_'. self::$option_name .'_id'; //could be em_category_name or em_tag_name
  94. $is_taxonomy_page = $post->ID == self::get_page_id();
  95. if( !empty($GLOBALS[$em_taxonomy_property]) ) $taxonomy_id = $GLOBALS[$em_taxonomy_property];
  96. if( !empty($wp_query->{$em_taxonomy_property}) ) $taxonomy_id = $wp_query->{$em_taxonomy_property};
  97. $taxonomy_flag = (!empty($wp_query->{$em_taxonomy_property}) || !empty($GLOBALS[$em_taxonomy_property]));
  98. if( ($is_taxonomy_page && !empty($taxonomy_id)) || (empty($post->ID) && !empty($taxonomy_id)) ){
  99. $GLOBALS[self::$tax_class] = EM_Taxonomy_Term::get($taxonomy_id, self::$tax_class);
  100. ob_start();
  101. em_locate_template('templates/'.self::$option_name.'-single.php',true);
  102. return ob_get_clean();
  103. }
  104. return $content;
  105. }
  106. /**
  107. * Removes the em_content filter from firing, which should be triggered by wp_head after EM has added this filter
  108. */
  109. public static function remove_em_the_content(){
  110. remove_filter('the_content', 'em_content');
  111. }
  112. /**
  113. * Parses the query on regular taxonomy archives so events are cronologically ordered.
  114. * @param WP_Query $wp_query
  115. */
  116. public static function parse_query( $wp_query ){
  117. global $post;
  118. if( !$wp_query->is_main_query() ) return;
  119. if( $wp_query->is_tax(self::$taxonomy_name) ){
  120. //Scope is future
  121. $today = strtotime(date('Y-m-d', current_time('timestamp')));
  122. if( get_option('dbem_events_current_are_past') ){
  123. $wp_query->query_vars['meta_query'][] = array( 'key' => '_start_ts', 'value' => $today, 'compare' => '>=' );
  124. }else{
  125. $wp_query->query_vars['meta_query'][] = array( 'key' => '_end_ts', 'value' => $today, 'compare' => '>=' );
  126. }
  127. if( get_option('dbem_'. self::$option_name_plural .'_default_archive_orderby') == 'title'){
  128. $wp_query->query_vars['orderby'] = 'title';
  129. }else{
  130. $wp_query->query_vars['orderby'] = 'meta_value_num';
  131. $wp_query->query_vars['meta_key'] = get_option('dbem_'. self::$option_name_plural .'_default_archive_orderby','_start_ts');
  132. }
  133. $wp_query->query_vars['order'] = get_option('dbem_'. self::$option_name_plural .'_default_archive_order','ASC');
  134. $post_types = $wp_query->get( 'post_type');
  135. $post_types = is_array($post_types) ? $post_types + array(EM_POST_TYPE_EVENT) : EM_POST_TYPE_EVENT;
  136. if( !get_option('dbem_cp_events_search_results') ) $wp_query->set( 'post_type', $post_types ); //in case events aren't publicly searchable due to 'bug' in WP - https://core.trac.wordpress.org/ticket/17592
  137. }elseif( !empty($wp_query->{'em_'.self::$option_name.'_id'}) ){
  138. $post = $wp_query->post;
  139. }
  140. }
  141. public static function get_page_id(){
  142. return get_option('dbem_'.self::$option_name_plural.'_page');
  143. }
  144. public static function wpseo_breadcrumb_links( $links ){
  145. global $wp_query;
  146. array_pop($links);
  147. if( self::get_page_id() ){
  148. $links[] = array('id'=> self::get_page_id());
  149. }
  150. $links[] = array('text'=> $wp_query->posts[0]->post_title);
  151. return $links;
  152. }
  153. /**
  154. * Switches the query back/forth from the original query if EM has interferred to add formatting for taxonomy pages.
  155. * Useful if you want plugins to temporarily access the old WP_Query which indicated we were looking at a taxonomy.
  156. * For example, with WordPress SEO by Yoast, for wpseo_head we can switch at priority 1 and switch back at a really low priority so meta data is correctly generated.
  157. * @param string $template
  158. * @return string
  159. */
  160. public static function flip_the_query(){
  161. global $wp_query, $wp_the_query, $em_the_query;
  162. if( !empty($em_the_query) ){
  163. $old_query = $wp_the_query;
  164. $wp_query = $wp_the_query = $em_the_query;
  165. $em_the_query = $old_query;
  166. }
  167. }
  168. }