| | 798 | |
| | 799 | /** |
| | 800 | * Don't display post titles for asides and status posts on the front end. |
| | 801 | * |
| | 802 | * @since 3.6.0 |
| | 803 | * @access private |
| | 804 | */ |
| | 805 | function _post_formats_title( $title, $post_id ) { |
| | 806 | if ( is_admin() || ! in_array( get_post_format( $post_id ), array( 'aside', 'status' ) ) ) |
| | 807 | return $title; |
| | 808 | |
| | 809 | // Return an empty string only if the title is auto-generated. |
| | 810 | $post = get_post( $post_id ); |
| | 811 | if ( $title == _post_formats_generate_title( $post->post_content, get_post_format( $post_id ) ) ) |
| | 812 | $title = ''; |
| | 813 | |
| | 814 | return $title; |
| | 815 | } |
| | 816 | |
| | 817 | /** |
| | 818 | * Generate a title from the post content or format. |
| | 819 | * |
| | 820 | * @since 3.6.0 |
| | 821 | * @access private |
| | 822 | */ |
| | 823 | function _post_formats_generate_title( $content, $post_format = '' ) { |
| | 824 | $title = wp_trim_words( strip_shortcodes( $content ), 8, '' ); |
| | 825 | |
| | 826 | if ( empty( $title ) ) |
| | 827 | $title = get_post_format_string( $post_format ); |
| | 828 | |
| | 829 | return $title; |
| | 830 | } |
| | 831 | |
| | 832 | /** |
| | 833 | * Runs during save_post, fixes empty titles for asides and statuses. |
| | 834 | * |
| | 835 | * @since 3.6.0 |
| | 836 | * @access private |
| | 837 | */ |
| | 838 | function _post_formats_fix_empty_title( $data, $postarr ) { |
| | 839 | if ( 'auto-draft' == $data['post_status'] || ! post_type_supports( $data['post_type'], 'post-formats' ) ) |
| | 840 | return $data; |
| | 841 | |
| | 842 | $post_id = ( isset( $postarr['ID'] ) ) ? absint( $postarr['ID'] ) : 0; |
| | 843 | $post_format = ( in_array( $postarr['post_format'], array_keys( get_post_format_slugs() ) ) ) ? $postarr['post_format'] : ''; |
| | 844 | |
| | 845 | if ( ! in_array( $post_format, array( 'aside', 'status' ) ) ) |
| | 846 | return $data; |
| | 847 | |
| | 848 | if ( $data['post_title'] == _post_formats_generate_title( $data['post_content'], $post_format ) ) |
| | 849 | return $data; |
| | 850 | |
| | 851 | // If updating an existing post, check whether the title was auto-generated. |
| | 852 | if ( $post_id && $post = get_post( $post_id ) ) |
| | 853 | if ( $post->post_title == $data['post_title'] && $post->post_title == _post_formats_generate_title( $post->post_content, get_post_format( $post->ID ) ) ) |
| | 854 | $data['post_title'] = ''; |
| | 855 | |
| | 856 | if ( empty( $data['post_title'] ) ) |
| | 857 | $data['post_title'] = _post_formats_generate_title( $data['post_content'], $post_format ); |
| | 858 | |
| | 859 | return $data; |
| | 860 | } |
| | 861 | No newline at end of file |