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