236 | | |
237 | | /** |
238 | | * Don't display post titles for asides and status posts on the front end. |
239 | | * |
240 | | * @since 3.6.0 |
241 | | * @access private |
242 | | */ |
243 | | function _post_formats_title( $title, $post_id = 0 ) { |
244 | | if ( ! $post_id || is_admin() || is_feed() || ! in_array( get_post_format( $post_id ), array( 'aside', 'status' ) ) ) |
245 | | return $title; |
246 | | |
247 | | // Return an empty string only if the title is auto-generated. |
248 | | $post = get_post( $post_id ); |
249 | | if ( $title == _post_formats_generate_title( $post->post_content, get_post_format( $post_id ) ) ) |
250 | | $title = ''; |
251 | | |
252 | | return $title; |
253 | | } |
254 | | |
255 | | /** |
256 | | * Generate a title from the post content or format. |
257 | | * |
258 | | * @since 3.6.0 |
259 | | * @access private |
260 | | */ |
261 | | function _post_formats_generate_title( $content, $post_format = '' ) { |
262 | | $title = wp_trim_words( strip_shortcodes( $content ), 8, '' ); |
263 | | |
264 | | if ( empty( $title ) ) |
265 | | $title = get_post_format_string( $post_format ); |
266 | | |
267 | | return $title; |
268 | | } |
269 | | |
270 | | /** |
271 | | * Fixes empty titles for aside and status formats. |
272 | | * |
273 | | * Passes a generated post title to the 'wp_insert_post_data' filter. |
274 | | * |
275 | | * @since 3.6.0 |
276 | | * @access private |
277 | | * |
278 | | * @uses _post_formats_generate_title() |
279 | | */ |
280 | | function _post_formats_fix_empty_title( $data, $postarr ) { |
281 | | if ( 'auto-draft' == $data['post_status'] || ! post_type_supports( $data['post_type'], 'post-formats' ) ) |
282 | | return $data; |
283 | | |
284 | | $post_id = ( isset( $postarr['ID'] ) ) ? absint( $postarr['ID'] ) : 0; |
285 | | $post_format = ''; |
286 | | |
287 | | if ( $post_id ) |
288 | | $post_format = get_post_format( $post_id ); |
289 | | |
290 | | if ( isset( $postarr['post_format'] ) ) |
291 | | $post_format = ( in_array( $postarr['post_format'], get_post_format_slugs() ) ) ? $postarr['post_format'] : ''; |
292 | | |
293 | | if ( ! in_array( $post_format, array( 'aside', 'status' ) ) ) |
294 | | return $data; |
295 | | |
296 | | if ( $data['post_title'] == _post_formats_generate_title( $data['post_content'], $post_format ) ) |
297 | | return $data; |
298 | | |
299 | | // If updating an existing post, check whether the title was auto-generated. |
300 | | if ( $post_id && $post = get_post( $post_id ) ) |
301 | | if ( $post->post_title == $data['post_title'] && $post->post_title == _post_formats_generate_title( $post->post_content, get_post_format( $post->ID ) ) ) |
302 | | $data['post_title'] = ''; |
303 | | |
304 | | if ( empty( $data['post_title'] ) ) |
305 | | $data['post_title'] = _post_formats_generate_title( $data['post_content'], $post_format ); |
306 | | |
307 | | return $data; |
308 | | } |