em-location-post.php
5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
class EM_Location_Post {
public static function init(){
//Front Side Modifiers
if( !is_admin() ){
//override single page with formats?
add_filter('the_content', array('EM_Location_Post','the_content'));
//override excerpts?
if( get_option('dbem_cp_locations_excerpt_formats') ){
add_filter('get_the_excerpt', array('EM_Location_Post','the_excerpt'), 9);
}
//excerpts can trigger the_content which isn't ideal, so we disable the_content between the first and last excerpt calls within WP logic
add_filter('get_the_excerpt', array('EM_Location_Post','disable_the_content'), 1);
add_filter('get_the_excerpt', array('EM_Location_Post','enable_the_content'), 100);
//display as page or other template?
if( get_option('dbem_cp_locations_template') ){
add_filter('single_template',array('EM_Location_Post','single_template'));
}
//add classes to body and post_class()
if( get_option('dbem_cp_locations_post_class') ){
add_filter('post_class', array('EM_Location_Post','post_class'), 10, 3);
}
if( get_option('dbem_cp_locations_body_class') ){
add_filter('body_class', array('EM_Location_Post','body_class'), 10, 3);
}
}
add_action('parse_query', array('EM_Location_Post','parse_query'));
}
/**
* Overrides the default post format of a location and can display a location as a page, which uses the page.php template.
* @param string $template
* @return string
*/
public static function single_template($template){
global $post;
if( !locate_template('single-'.EM_POST_TYPE_LOCATION.'.php') && $post->post_type == EM_POST_TYPE_LOCATION ){
//do we have a default template to choose for events?
if( get_option('dbem_cp_locations_template') == 'page' ){
$post_templates = array('page.php','index.php');
}else{
$post_templates = array(get_option('dbem_cp_locations_template'));
}
if( !empty($post_templates) ){
$post_template = locate_template($post_templates,false);
if( !empty($post_template) ) $template = $post_template;
}
}
return $template;
}
public static function post_class( $classes, $class, $post_id ){
$post = get_post($post_id);
if( $post->post_type == EM_POST_TYPE_LOCATION ){
foreach( explode(' ', get_option('dbem_cp_locations_post_class')) as $class ){
$classes[] = esc_attr($class);
}
}
return $classes;
}
public static function body_class( $classes ){
if( em_is_location_page() ){
foreach( explode(' ', get_option('dbem_cp_locations_body_class')) as $class ){
$classes[] = esc_attr($class);
}
}
return $classes;
}
/**
* Overrides the_excerpt if this is an location post type
*/
public static function get_the_excerpt($content){
global $post;
if( $post->post_type == EM_POST_TYPE_LOCATION ){
$EM_Location = em_get_location($post);
$output = !empty($EM_Location->post_excerpt) ? get_option('dbem_location_excerpt_format'):get_option('dbem_location_excerpt_alt_format');
$content = $EM_Location->output($output);
}
return $content;
}
public static function the_excerpt($content){ return self::get_the_excerpt($content); }
public static function enable_the_content( $content ){
add_filter('the_content', array('EM_Location_Post','the_content'));
return $content;
}
public static function disable_the_content( $content ){
remove_filter('the_content', array('EM_Location_Post','the_content'));
return $content;
}
public static function the_content( $content ){
global $post, $EM_Location;
if( !empty($post) && $post->post_type == EM_POST_TYPE_LOCATION ){
if( is_archive() || is_search() ){
if( get_option('dbem_cp_locations_archive_formats') ){
$EM_Location = em_get_location($post);
$content = $EM_Location->output(get_option('dbem_location_list_item_format'));
}
}else{
if( get_option('dbem_cp_locations_formats') && !post_password_required() ){
$EM_Location = em_get_location($post);
if( !empty($_REQUEST['preview']) ){
//we don't do extra checks here because WP will have already done the work for us here...
$EM_Location->post_content = $post->post_content;
$EM_Location->post_content_filtered = $post->post_content_filtered;
}else{
$EM_Location->post_content = $content;
}
ob_start();
em_locate_template('templates/location-single.php',true);
$content = ob_get_clean();
}
}
}
return $content;
}
public static function parse_query(){
global $wp_query;
if( !empty($wp_query->query_vars['post_type']) && $wp_query->query_vars['post_type'] == EM_POST_TYPE_LOCATION ){
if( is_admin() ){
$wp_query->query_vars['orderby'] = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby']:'title';
$wp_query->query_vars['order'] = (!empty($_REQUEST['order'])) ? $_REQUEST['order']:'ASC';
}else{
if( get_option('dbem_locations_default_archive_orderby') == 'title'){
$wp_query->query_vars['orderby'] = 'title';
}else{
$wp_query->query_vars['orderby'] = 'meta_value_num';
$wp_query->query_vars['meta_key'] = get_option('dbem_locations_default_archive_orderby','_location_country');
}
$wp_query->query_vars['order'] = get_option('dbem_locations_default_archive_order','ASC');
}
}
}
}
EM_Location_Post::init();