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, '


' ) ); + /* + Remove html attributes except for anchor tag: - // Adapting content for pure social posts: - if ( $num_categories == $num_social_categories ) { - $content = trim( str_replace( ' ', ' ', strip_tags( $data['post_content'], '


' ) ) ); -/* - Remove html attributes except for anchor tag: + / Start Pattern + < Match '<' at beginning of tags + ( Start Capture Group $1 - Tag Name + [a-z] Match 'a' through 'z' + [a-z0-9]+ Match 'a' through 'z' or '0' through '9' one or more times + ) End Capture Group + [^>]*? Match anything other than '>', Zero or More times, not-greedy (wont eat the /) + (\/?) Capture Group $2 - '/' if it is there + > Match '>' + /i End Pattern - Case Insensitive + */ + $post_content = preg_replace( "/<([a-z][a-z0-9]+)[^>]*?(\/?)>/i", '<$1$2>', $post_content ); + $post->post_content = str_replace( array( '', '', '', '' ), array( '', '', '', '' ), $post_content ); + } - / Start Pattern - < Match '<' at beginning of tags - ( Start Capture Group $1 - Tag Name - [a-z] Match 'a' through 'z' - [a-z0-9]+ Match 'a' through 'z' or '0' through '9' one or more times - ) End Capture Group - [^>]*? Match anything other than '>', Zero or More times, not-greedy (wont eat the /) - (\/?) Capture Group $2 - '/' if it is there - > Match '>' - /i End Pattern - Case Insensitive -*/ - $content = preg_replace( "/<([a-z][a-z0-9]+)[^>]*?(\/?)>/i", '<$1$2>', $content ); - $content = str_replace( array( '', '', '', '' ), array( '', '', '', '' ), $content ); - $data['post_content'] = $content; + // Auto generate title if it is empty: + if ( empty( $text_title ) ) { + $content_words = explode( ' ', $text_content ); + $text_title = implode( ' ', array_slice( $content_words, 0, 4 ) ); + $post->post_title = count( $content_words ) > 4 ? "$text_title …" : $text_title; + } + + // Preparing Tweet: + $post_excerpt = strpos( $text_content, $text_title ) === false ? $text_title . '. ' . $text_content : $text_content; + $limit = has_post_thumbnail( $post ) ? 140 - 25 : 140; + if ( strlen( $post_excerpt ) > $limit ) { + preg_match_all( '#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $post_excerpt, $matches ); + for ( $i = 0; $i < count( $matches[0] ); $i++ ) { + $post_excerpt = str_replace( $matches[0][$i], shorten_url( $matches[0][$i] ), $post_excerpt ); + } + if ( strlen( $post_excerpt ) > $limit ) { + $post_excerpt = substr( $post_excerpt, 0, $limit - 25 ); + $post_excerpt = substr( $post_excerpt, 0, strrpos( $post_excerpt, ' ' ) ) . '… '; + $permalink = get_permalink( $post ); + $post_excerpt .= ( strlen( $post_excerpt ) + strlen( $permalink ) ) <= $limit ? $permalink : shorten_url( $permalink ); + } + } + $post->post_excerpt = $post_excerpt; + + // Update the post, which calls save_post again: + wp_update_post( $post ); } } - return $data; + + // Re-hook this function: + add_action( 'save_post', 'adapting_social_post', 1, 2 ); +} + +function shorten_url( $long_url ) { + $bitly_login = 'o_7sko9rcpp7'; + $bitly_apikey = 'R_f414bf78d22443c6a6bad3c58921fd96'; + $bitly_shorten = "http://api.bit.ly/v3/shorten?login=" . $bitly_login . "&apiKey=" . $bitly_apikey . "&longUrl=" . $long_url . "&format=json"; + return json_decode( file_get_contents( $bitly_shorten ) )->data->url; } /*