diff --git a/enfold-cille/functions.php b/enfold-cille/functions.php index c0de3a6..7582f91 100644 --- a/enfold-cille/functions.php +++ b/enfold-cille/functions.php @@ -161,75 +161,93 @@ function avia_change_breadcrumb( $trail, $args ) { /* * Adapting content for social networks; and auto generate title if it is empty. */ -add_filter( 'wp_insert_post_data', 'review_social_post', 5, 2 ); -function review_social_post( $data, $postarr ) { +add_action( 'save_post', 'adapting_social_post', 1, 2 ); +function adapting_social_post( $post_id, $post ) { + // Unhook this function so it doesn't loop infinitely: + remove_action( 'save_post', 'adapting_social_post', 1, 2 ); + // Count post categories: - $categories = array(); - if ( !empty( $postarr['post_category'] ) ) { - $categories = $postarr['post_category']; - } - elseif ( !empty( $postarr['tax_input']['category'] ) ) { - $categories = $postarr['tax_input']['category']; - } - $categories = is_array( $categories ) ? array_filter( $categories ) : array( $categories ); + $post_categories = get_the_category( $post_id ); + $categories = array_values( array_column( $post_categories, 'name' ) ); $num_categories = count( $categories ); // Review post only for Social, Twitter, Facebook & LinkedIn categories: - $num_social_categories = count( array_intersect( array( 1561, 1562, 1563, 1564 ), $categories ) ); + $num_social_categories = count( array_intersect( array( 'Social', 'Twitter', 'Facebook', 'LinkedIn' ), $categories ) ); + // Nothing to do if there aren't social categories: if ( $num_social_categories > 0 ) { // Get clean post title: - $post_title = trim( str_replace( ' ', ' ', strip_tags( $data['post_title'] ) ) ); + $title = preg_replace( '!\s+!', ' ', str_replace( ' ', ' ', $post->post_title ) ); + $text_title = trim( strip_tags( $title ) ); + if ( mb_strrpos( $text_title, ' …' ) == mb_strlen( $text_title ) - 2 ) { + $text_title = mb_substr( $text_title, 0, mb_strlen( $text_title ) - 2 ); + } // Get clean post content: - $post_content = trim( str_replace( ' ', ' ', strip_tags( $data['post_content'] ) ) ); + $content = preg_replace( '!\s+!', ' ', str_replace( ' ', ' ', $post->post_content ) ); + $text_content = trim( strip_tags( $content ) ); - // Auto generate title if it is empty: - $isfull_title = false; - $title_maxchar = 140 - 28; - if ( empty( $post_title ) ) { - if ( empty( $post_content ) ) { - $post_title = 'Sin título en el morral, sea añejo o fugaz, lo mismo da'; - } - else { - $post_title = substr( $post_content, 0, $title_maxchar ); - $post_title = substr( $post_title, 0, strrpos( $post_title, ' ' ) ); - $isfull_title = strlen( $post_title ) > strlen( $post_content ); - } - } - elseif ( strlen( $post_title ) > $title_maxchar ) { - if ( strpos( $post_content, $post_title ) !== 0 ) { - $data['post_content'] = $post_title . '. ' . $data['post_content']; - } - $post_title = substr( $post_title, 0, $title_maxchar ); - $post_title = substr( $post_title, 0, strrpos( $post_title, ' ' ) ); - $isfull_title = true; - } - $data['post_title'] = $isfull_title ? $post_title . '…' : $post_title; + // Nothing to do if no title and no content: + if ( ! empty( $text_title ) || ! empty( $text_content ) ) { + // Adapting content for pure social posts: + if ( $num_categories == $num_social_categories ) { + $post_content = trim( strip_tags( $content, '