| | 934 | |
| | 935 | /** |
| | 936 | * Don't display post titles for asides and status posts on the front end. |
| | 937 | * |
| | 938 | * @since 3.6.0 |
| | 939 | * @access private |
| | 940 | */ |
| | 941 | function _post_formats_title( $title, $post_id ) { |
| | 942 | if ( is_admin() || is_feed() || ! in_array( get_post_format( $post_id ), array( 'aside', 'status' ) ) ) |
| | 943 | return $title; |
| | 944 | |
| | 945 | // Return an empty string only if the title is auto-generated. |
| | 946 | $post = get_post( $post_id ); |
| | 947 | if ( $title == _post_formats_generate_title( $post->post_content, get_post_format( $post_id ) ) ) |
| | 948 | $title = ''; |
| | 949 | |
| | 950 | return $title; |
| | 951 | } |
| | 952 | |
| | 953 | /** |
| | 954 | * Generate a title from the post content or format. |
| | 955 | * |
| | 956 | * @since 3.6.0 |
| | 957 | * @access private |
| | 958 | */ |
| | 959 | function _post_formats_generate_title( $content, $post_format = '' ) { |
| | 960 | $title = wp_trim_words( strip_shortcodes( $content ), 8, '' ); |
| | 961 | |
| | 962 | if ( empty( $title ) ) |
| | 963 | $title = get_post_format_string( $post_format ); |
| | 964 | |
| | 965 | return $title; |
| | 966 | } |
| | 967 | |
| | 968 | /** |
| | 969 | * Runs during save_post, fixes empty titles for asides and statuses. |
| | 970 | * |
| | 971 | * @since 3.6.0 |
| | 972 | * @access private |
| | 973 | */ |
| | 974 | function _post_formats_fix_empty_title( $data, $postarr ) { |
| | 975 | if ( 'auto-draft' == $data['post_status'] || ! post_type_supports( $data['post_type'], 'post-formats' ) ) |
| | 976 | return $data; |
| | 977 | |
| | 978 | $post_id = ( isset( $postarr['ID'] ) ) ? absint( $postarr['ID'] ) : 0; |
| | 979 | |
| | 980 | if ( $post_id ) |
| | 981 | $post_format = get_post_format( $post_id ); |
| | 982 | |
| | 983 | if ( isset( $postarr['post_format'] ) ) |
| | 984 | $post_format = ( in_array( $postarr['post_format'], get_post_format_slugs() ) ) ? $postarr['post_format'] : ''; |
| | 985 | |
| | 986 | if ( ! in_array( $post_format, array( 'aside', 'status' ) ) ) |
| | 987 | return $data; |
| | 988 | |
| | 989 | if ( $data['post_title'] == _post_formats_generate_title( $data['post_content'], $post_format ) ) |
| | 990 | return $data; |
| | 991 | |
| | 992 | // If updating an existing post, check whether the title was auto-generated. |
| | 993 | if ( $post_id && $post = get_post( $post_id ) ) |
| | 994 | if ( $post->post_title == $data['post_title'] && $post->post_title == _post_formats_generate_title( $post->post_content, get_post_format( $post->ID ) ) ) |
| | 995 | $data['post_title'] = ''; |
| | 996 | |
| | 997 | if ( empty( $data['post_title'] ) ) |
| | 998 | $data['post_title'] = _post_formats_generate_title( $data['post_content'], $post_format ); |
| | 999 | |
| | 1000 | return $data; |
| | 1001 | } |
| | 1002 | No newline at end of file |