/
post-formats.php
246 lines (197 loc) · 6.73 KB
/
post-formats.php
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
// Prevent file from being loaded directly
if ( ! defined( 'ABSPATH' ) ) {
die( '-1' );
}
if ( !defined( 'ET_POST_FORMAT' ) ) {
define( 'ET_POST_FORMAT', 'et_post_format' );
}
if ( !defined( 'ET_POST_FORMAT_PREFIX' ) ) {
define( 'ET_POST_FORMAT_PREFIX', 'et-post-format-' );
}
function et_register_post_format_taxonomy(){
global $shortname;
register_taxonomy( ET_POST_FORMAT, 'post', array(
'public' => true,
'hierarchical' => false,
'labels' => array(
'name' => _x( 'Format', $shortname ),
'singular_name' => _x( 'Format', $shortname ),
),
'query_var' => true,
'rewrite' => false,
'show_ui' => false,
'show_in_nav_menus' => false,
) );
add_post_type_support( 'post', 'et-post-formats' );
}
add_action( 'init', 'et_register_post_format_taxonomy', 1 );
function et_get_post_format( $post = null ) {
if ( ! $post = get_post( $post ) ) {
return false;
}
if ( ! post_type_supports( $post->post_type, 'et-post-formats' ) ) {
return false;
}
$_format = get_the_terms( $post->ID, ET_POST_FORMAT );
if ( empty( $_format ) ) {
return false;
}
$format = array_shift( $_format );
$post_format_string = str_replace( ET_POST_FORMAT_PREFIX, '', $format->slug );
$post_format = in_array( $post_format_string, array_keys( et_get_post_format_strings() ) ) ? $post_format_string : false;
return apply_filters( 'et_get_post_format', $post_format, $post->ID );
}
function et_has_post_format( $format = array(), $post = null ) {
$prefixed = array();
if ( $format ) {
foreach ( (array) $format as $single ) {
$prefixed[] = ET_POST_FORMAT_PREFIX . sanitize_key( $single );
}
}
return has_term( $prefixed, ET_POST_FORMAT, $post );
}
function et_set_post_format( $post, $format ) {
$post = get_post( $post );
if ( empty( $post ) )
return new WP_Error( 'invalid_post', __( 'Invalid post' ) );
if ( ! empty( $format ) ) {
$format = sanitize_key( $format );
if ( 'standard' === $format || ! in_array( $format, et_get_post_format_slugs() ) )
$format = '';
else
$format = ET_POST_FORMAT_PREFIX . $format;
}
return wp_set_post_terms( $post->ID, $format, ET_POST_FORMAT );
}
function et_get_post_format_strings() {
$strings = array(
'standard' => __( 'Standard', 'extra' ), // Special case. any value that evals to false will be considered standard
'aside' => __( 'Aside', 'extra' ),
'chat' => __( 'Chat', 'extra' ),
'gallery' => __( 'Gallery', 'extra' ),
'link' => __( 'Link', 'extra' ),
'image' => __( 'Image', 'extra' ),
'quote' => __( 'Quote', 'extra' ),
'status' => __( 'Status', 'extra' ),
'video' => __( 'Video', 'extra' ),
'audio' => __( 'Audio', 'extra' ),
'map' => __( 'Map', 'extra' ),
);
$strings = apply_filters( 'et_post_formats_strings', $strings );
return $strings;
}
function et_get_post_format_slugs() {
$slugs = array_keys( et_get_post_format_strings() );
return array_combine( $slugs, $slugs );
}
function et_get_theme_post_format_slugs() {
$theme_supported_post_formats = get_theme_support( 'et-post-formats' );
$post_formats = array_intersect( $theme_supported_post_formats[0], array_keys( et_get_post_format_slugs() ) );
return array_combine( $post_formats, $post_formats );
}
function et_get_post_format_string( $slug ) {
$strings = et_get_post_format_strings();
if ( !$slug ) {
return $strings['standard'];
} else {
return ( isset( $strings[$slug] ) ) ? $strings[$slug] : '';
}
}
function et_post_format_body_class( $body_class ) {
if ( $post_format = et_get_post_format() ) {
$body_class[] = 'et-post-format';
$body_class[] = 'et-post-format-' . $post_format;
}
return $body_class;
}
add_filter( 'body_class', 'et_post_format_body_class' );
function _et_post_format_get_term( $term ) {
if ( isset( $term->slug ) ) {
$term->name = et_get_post_format_string( str_replace( ET_POST_FORMAT_PREFIX, '', $term->slug ) );
}
return $term;
}
add_filter( 'get_post_format', '_et_post_format_get_term' );
function _et_post_format_get_terms( $terms, $taxonomies, $args ) {
if ( in_array( ET_POST_FORMAT, (array) $taxonomies ) ) {
if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) {
foreach ( $terms as $order => $name ) {
$terms[$order] = et_get_post_format_string( str_replace( ET_POST_FORMAT_PREFIX, '', $name ) );
}
} else {
foreach ( (array) $terms as $order => $term ) {
if ( isset( $term->taxonomy ) && ET_POST_FORMAT == $term->taxonomy ) {
$terms[$order]->name = et_get_post_format_string( str_replace( ET_POST_FORMAT_PREFIX, '', $term->slug ) );
}
}
}
}
return $terms;
}
add_filter( 'get_terms', '_et_post_format_get_terms', 10, 3 );
function _et_post_format_wp_get_object_terms( $terms ) {
foreach ( (array) $terms as $order => $term ) {
if ( isset( $term->taxonomy ) && ET_POST_FORMAT == $term->taxonomy ) {
$terms[$order]->name = et_get_post_format_string( str_replace( ET_POST_FORMAT_PREFIX, '', $term->slug ) );
}
}
return $terms;
}
add_filter( 'wp_get_object_terms', '_et_post_format_wp_get_object_terms' );
function et_has_format_content( $post_id = 0 ) {
if ( empty( $post_id ) ) {
$post_id = get_the_ID();
}
if ( 'post' === get_post_type( $post_id ) ) {
switch ( et_get_post_format( $post_id ) ) {
case 'video':
$meta_key = '_video_format_urls';
break;
case 'audio':
$meta_key = '_audio_format_file_url';
break;
case 'quote':
$meta_key = '_quote_format_quote';
break;
case 'gallery':
$meta_key = '_gallery_format_attachment_ids';
break;
case 'link':
$meta_key = '_link_format_link_url';
break;
case 'map':
$meta_key = '_map_format_lat';
break;
default:
$meta_key = '';
break;
}
if ( !empty( $meta_key ) ) {
$has_format_content_setting = get_post_meta( $post_id, $meta_key, true );
} else {
$has_format_content_setting = has_post_thumbnail();
}
$has_format_content = $has_format_content_setting ? true : false;
} else {
$has_format_content = false;
}
return apply_filters( 'et_has_format_content', $has_format_content, $post_id );
}
function et_has_format_content_class( $classes, $class, $post_id ) {
$has_format_content = et_has_format_content( $post_id );
if ( $has_format_content ) {
$classes[] = 'et-has-post-format-content';
} elseif ( 'post' === get_post_type( $post_id ) ) {
$classes[] = 'et-doesnt-have-format-content';
}
return $classes;
}
add_filter( 'post_class', 'et_has_format_content_class', 10, 3 );
function et_set_post_format_default_class( $classes, $class, $post_id ) {
if ( 'post' === get_post_type( $post_id ) && ! has_term( array(), ET_POST_FORMAT, $post_id ) ) {
$classes[] = 'et_post_format-et-post-format-standard';
}
return $classes;
}
add_filter( 'post_class', 'et_set_post_format_default_class', 10, 3 );