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