author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 17:39:30 +0200 | |
changeset 7 | cf61fcea0001 |
parent 5 | 5e2f62d02dcd |
child 9 | 177826044cd9 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* Post format functions. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Post |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* Retrieve the format slug for a post |
|
11 |
* |
|
12 |
* @since 3.1.0 |
|
13 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* @param int|object|null $post Post ID or post object. Optional, default is the current post from the loop. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
15 |
* @return string|false The format if successful. False otherwise. |
0 | 16 |
*/ |
17 |
function get_post_format( $post = null ) { |
|
18 |
if ( ! $post = get_post( $post ) ) |
|
19 |
return false; |
|
20 |
||
21 |
if ( ! post_type_supports( $post->post_type, 'post-formats' ) ) |
|
22 |
return false; |
|
23 |
||
24 |
$_format = get_the_terms( $post->ID, 'post_format' ); |
|
25 |
||
26 |
if ( empty( $_format ) ) |
|
27 |
return false; |
|
28 |
||
5 | 29 |
$format = reset( $_format ); |
0 | 30 |
|
31 |
return str_replace('post-format-', '', $format->slug ); |
|
32 |
} |
|
33 |
||
34 |
/** |
|
5 | 35 |
* Check if a post has any of the given formats, or any format. |
0 | 36 |
* |
37 |
* @since 3.1.0 |
|
38 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
39 |
* @param string|array $format Optional. The format or formats to check. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
40 |
* @param object|int|null $post Optional. The post to check. If not supplied, defaults to the current post if used in the loop. |
5 | 41 |
* @return bool True if the post has any of the given formats (or any format, if no format specified), false otherwise. |
0 | 42 |
*/ |
5 | 43 |
function has_post_format( $format = array(), $post = null ) { |
44 |
$prefixed = array(); |
|
0 | 45 |
|
5 | 46 |
if ( $format ) { |
47 |
foreach ( (array) $format as $single ) { |
|
48 |
$prefixed[] = 'post-format-' . sanitize_key( $single ); |
|
49 |
} |
|
0 | 50 |
} |
51 |
||
52 |
return has_term( $prefixed, 'post_format', $post ); |
|
53 |
} |
|
54 |
||
55 |
/** |
|
56 |
* Assign a format to a post |
|
57 |
* |
|
58 |
* @since 3.1.0 |
|
59 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
60 |
* @param int|object $post The post for which to assign a format. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
* @param string $format A format to assign. Use an empty string or array to remove all formats from the post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
62 |
* @return array|WP_Error|false WP_Error on error. Array of affected term IDs on success. |
0 | 63 |
*/ |
64 |
function set_post_format( $post, $format ) { |
|
65 |
$post = get_post( $post ); |
|
66 |
||
67 |
if ( empty( $post ) ) |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
68 |
return new WP_Error( 'invalid_post', __( 'Invalid post.' ) ); |
0 | 69 |
|
70 |
if ( ! empty( $format ) ) { |
|
71 |
$format = sanitize_key( $format ); |
|
72 |
if ( 'standard' === $format || ! in_array( $format, get_post_format_slugs() ) ) |
|
73 |
$format = ''; |
|
74 |
else |
|
75 |
$format = 'post-format-' . $format; |
|
76 |
} |
|
77 |
||
78 |
return wp_set_post_terms( $post->ID, $format, 'post_format' ); |
|
79 |
} |
|
80 |
||
81 |
/** |
|
82 |
* Returns an array of post format slugs to their translated and pretty display versions |
|
83 |
* |
|
84 |
* @since 3.1.0 |
|
85 |
* |
|
86 |
* @return array The array of translated post format names. |
|
87 |
*/ |
|
88 |
function get_post_format_strings() { |
|
89 |
$strings = array( |
|
90 |
'standard' => _x( 'Standard', 'Post format' ), // Special case. any value that evals to false will be considered standard |
|
91 |
'aside' => _x( 'Aside', 'Post format' ), |
|
92 |
'chat' => _x( 'Chat', 'Post format' ), |
|
93 |
'gallery' => _x( 'Gallery', 'Post format' ), |
|
94 |
'link' => _x( 'Link', 'Post format' ), |
|
95 |
'image' => _x( 'Image', 'Post format' ), |
|
96 |
'quote' => _x( 'Quote', 'Post format' ), |
|
97 |
'status' => _x( 'Status', 'Post format' ), |
|
98 |
'video' => _x( 'Video', 'Post format' ), |
|
99 |
'audio' => _x( 'Audio', 'Post format' ), |
|
100 |
); |
|
101 |
return $strings; |
|
102 |
} |
|
103 |
||
104 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
105 |
* Retrieves the array of post format slugs. |
0 | 106 |
* |
107 |
* @since 3.1.0 |
|
108 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
109 |
* @return array The array of post format slugs as both keys and values. |
0 | 110 |
*/ |
111 |
function get_post_format_slugs() { |
|
112 |
$slugs = array_keys( get_post_format_strings() ); |
|
113 |
return array_combine( $slugs, $slugs ); |
|
114 |
} |
|
115 |
||
116 |
/** |
|
117 |
* Returns a pretty, translated version of a post format slug |
|
118 |
* |
|
119 |
* @since 3.1.0 |
|
120 |
* |
|
121 |
* @param string $slug A post format slug. |
|
122 |
* @return string The translated post format name. |
|
123 |
*/ |
|
124 |
function get_post_format_string( $slug ) { |
|
125 |
$strings = get_post_format_strings(); |
|
126 |
if ( !$slug ) |
|
127 |
return $strings['standard']; |
|
128 |
else |
|
129 |
return ( isset( $strings[$slug] ) ) ? $strings[$slug] : ''; |
|
130 |
} |
|
131 |
||
132 |
/** |
|
133 |
* Returns a link to a post format index. |
|
134 |
* |
|
135 |
* @since 3.1.0 |
|
136 |
* |
|
137 |
* @param string $format The post format slug. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
138 |
* @return string|WP_Error|false The post format term link. |
0 | 139 |
*/ |
140 |
function get_post_format_link( $format ) { |
|
141 |
$term = get_term_by('slug', 'post-format-' . $format, 'post_format' ); |
|
142 |
if ( ! $term || is_wp_error( $term ) ) |
|
143 |
return false; |
|
144 |
return get_term_link( $term ); |
|
145 |
} |
|
146 |
||
147 |
/** |
|
148 |
* Filters the request to allow for the format prefix. |
|
149 |
* |
|
150 |
* @access private |
|
151 |
* @since 3.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
* @param array $qvs |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
154 |
* @return array |
0 | 155 |
*/ |
156 |
function _post_format_request( $qvs ) { |
|
157 |
if ( ! isset( $qvs['post_format'] ) ) |
|
158 |
return $qvs; |
|
159 |
$slugs = get_post_format_slugs(); |
|
160 |
if ( isset( $slugs[ $qvs['post_format'] ] ) ) |
|
161 |
$qvs['post_format'] = 'post-format-' . $slugs[ $qvs['post_format'] ]; |
|
162 |
$tax = get_taxonomy( 'post_format' ); |
|
163 |
if ( ! is_admin() ) |
|
164 |
$qvs['post_type'] = $tax->object_type; |
|
165 |
return $qvs; |
|
166 |
} |
|
167 |
||
168 |
/** |
|
169 |
* Filters the post format term link to remove the format prefix. |
|
170 |
* |
|
171 |
* @access private |
|
172 |
* @since 3.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
173 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
174 |
* @global WP_Rewrite $wp_rewrite |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
175 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
176 |
* @param string $link |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
177 |
* @param object $term |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
178 |
* @param string $taxonomy |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
179 |
* @return string |
0 | 180 |
*/ |
181 |
function _post_format_link( $link, $term, $taxonomy ) { |
|
182 |
global $wp_rewrite; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
183 |
if ( 'post_format' != $taxonomy ) { |
0 | 184 |
return $link; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
185 |
} |
0 | 186 |
if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) { |
187 |
return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link ); |
|
188 |
} else { |
|
189 |
$link = remove_query_arg( 'post_format', $link ); |
|
190 |
return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link ); |
|
191 |
} |
|
192 |
} |
|
193 |
||
194 |
/** |
|
195 |
* Remove the post format prefix from the name property of the term object created by get_term(). |
|
196 |
* |
|
197 |
* @access private |
|
198 |
* @since 3.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
199 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
200 |
* @param object $term |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
201 |
* @return object |
0 | 202 |
*/ |
203 |
function _post_format_get_term( $term ) { |
|
204 |
if ( isset( $term->slug ) ) { |
|
205 |
$term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) ); |
|
206 |
} |
|
207 |
return $term; |
|
208 |
} |
|
209 |
||
210 |
/** |
|
211 |
* Remove the post format prefix from the name property of the term objects created by get_terms(). |
|
212 |
* |
|
213 |
* @access private |
|
214 |
* @since 3.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
215 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
216 |
* @param array $terms |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
217 |
* @param string|array $taxonomies |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
218 |
* @param array $args |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
219 |
* @return array |
0 | 220 |
*/ |
221 |
function _post_format_get_terms( $terms, $taxonomies, $args ) { |
|
222 |
if ( in_array( 'post_format', (array) $taxonomies ) ) { |
|
223 |
if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
224 |
foreach ( $terms as $order => $name ) { |
0 | 225 |
$terms[$order] = get_post_format_string( str_replace( 'post-format-', '', $name ) ); |
226 |
} |
|
227 |
} else { |
|
228 |
foreach ( (array) $terms as $order => $term ) { |
|
229 |
if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) { |
|
230 |
$terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) ); |
|
231 |
} |
|
232 |
} |
|
233 |
} |
|
234 |
} |
|
235 |
return $terms; |
|
236 |
} |
|
237 |
||
238 |
/** |
|
239 |
* Remove the post format prefix from the name property of the term objects created by wp_get_object_terms(). |
|
240 |
* |
|
241 |
* @access private |
|
242 |
* @since 3.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
243 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
244 |
* @param array $terms |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
245 |
* @return array |
0 | 246 |
*/ |
247 |
function _post_format_wp_get_object_terms( $terms ) { |
|
248 |
foreach ( (array) $terms as $order => $term ) { |
|
249 |
if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) { |
|
250 |
$terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) ); |
|
251 |
} |
|
252 |
} |
|
253 |
return $terms; |
|
254 |
} |