From 670dad099f268776ca7dabee77e14463c63454be Mon Sep 17 00:00:00 2001 From: Manuel Cillero Date: Thu, 19 Oct 2017 19:14:08 +0200 Subject: [PATCH] =?UTF-8?q?Se=20separa=20la=20creaci=C3=B3n=20del=20t?= =?UTF-8?q?=C3=ADtulo=20de=20la=20preparaci=C3=B3n=20del=20contenido=20a?= =?UTF-8?q?=20divulgar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- enfold-cille/functions.php | 170 +++++++++++++++++++++++-------------- 1 file changed, 105 insertions(+), 65 deletions(-) diff --git a/enfold-cille/functions.php b/enfold-cille/functions.php index 7582f91..29c21bf 100644 --- a/enfold-cille/functions.php +++ b/enfold-cille/functions.php @@ -159,13 +159,32 @@ function avia_change_breadcrumb( $trail, $args ) { } /* - * Adapting content for social networks; and auto generate title if it is empty. + * Auto generate the post title if it's empty and post has content. */ -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 ); +add_filter( 'wp_insert_post_data', 'autogenerate_title', 1, 2 ); +function autogenerate_title( $data, $postarr ) { + $title = preg_replace( '!\s+!', ' ', str_replace( ' ', ' ', $data['post_title'] ) ); + $text_title = trim( strip_tags( $title ) ); + if ( empty( $text_title ) ) { + $content = preg_replace( '!\s+!', ' ', str_replace( ' ', ' ', $data['post_content'] ) ); + $text_content = trim( strip_tags( $content ) ); + + if ( ! empty( $text_content ) ) { + $content_words = explode( ' ', $text_content ); + $text_title = implode( ' ', array_slice( $content_words, 0, 4 ) ); + $data['post_title'] = count( $content_words ) > 4 ? "$text_title …" : $text_title; + } + } + + return $data; +} + +/* + * Sharing content in social networks if required. + */ +add_action( 'save_post', 'share_post', 1, 2 ); +function share_post( $post_id, $post ) { // Count post categories: $post_categories = get_the_category( $post_id ); $categories = array_values( array_column( $post_categories, 'name' ) ); @@ -176,71 +195,64 @@ function adapting_social_post( $post_id, $post ) { // Nothing to do if there aren't social categories: if ( $num_social_categories > 0 ) { - // Get clean post title: + // Get cleaned 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: + // Get cleaned post content: $content = preg_replace( '!\s+!', ' ', str_replace( ' ', ' ', $post->post_content ) ); $text_content = trim( strip_tags( $content ) ); - // 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 ) { + $post_content = trim( strip_tags( $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 ); - } - - // 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 ); + / 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 ); } - } - // Re-hook this function: - add_action( 'save_post', 'adapting_social_post', 1, 2 ); + // Preparing post excerpt for Twitter: + $post_excerpt = mb_strpos( $text_content, $text_title ) === 0 ? $text_content : $text_title . ( empty( $text_content ) ? '.' : ". $text_content" ); + $limit = has_post_thumbnail( $post ) ? 140 - 25 : 140; + if ( mb_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 ( mb_strlen( $post_excerpt ) > $limit ) { + $post_excerpt = mb_substr( $post_excerpt, 0, $limit - 25 ); + $post_excerpt = mb_substr( $post_excerpt, 0, mb_strrpos( $post_excerpt, ' ' ) ) . '… '; + $permalink = get_permalink( $post ); + $post_excerpt .= ( mb_strlen( $post_excerpt ) + mb_strlen( $permalink ) ) <= $limit ? $permalink : shorten_url( $permalink ); + } + } + $post->post_excerpt = $post_excerpt; + + // Unhook this function so it doesn't loop infinitely: + remove_action( 'save_post', 'share_post', 1, 2 ); + + // Update the post, which calls save_post again: + wp_update_post( $post ); + + // Re-hook this function: + add_action( 'save_post', 'share_post', 1, 2 ); + } } function shorten_url( $long_url ) { @@ -273,16 +285,42 @@ function social_post_defautl_thumbnail( $html, $post_id, $post_thumbnail_id, $si } */ /* - * Add a Character Counter to the WordPress Excerpt Box (with no limit). + * Add a button to display the number of characters in Title and Content editor; + * and also add a character counter to the Excerpt box (with no limit). * See https://premium.wpmudev.org/blog/character-counter-excerpt-box */ -add_action( 'admin_head-post.php', 'excerpt_count_js' ); -add_action( 'admin_head-post-new.php', 'excerpt_count_js' ); -function excerpt_count_js() { +add_action( 'admin_head-post.php', 'char_counter_js' ); +add_action( 'admin_head-post-new.php', 'char_counter_js' ); +function char_counter_js() { if ( get_post_type() == 'post' ) { echo ' -'; +}); +'; } }