Se separa la creación del título de la preparación del contenido a divulgar
This commit is contained in:
parent
7321acbf4e
commit
670dad099f
1 changed files with 105 additions and 65 deletions
|
@ -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, '<p><a><strong><b><em><i><br />' ) );
|
||||
/*
|
||||
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, '<p><a><strong><b><em><i><br />' ) );
|
||||
/*
|
||||
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( '<b>', '</b>', '<i>', '</i>' ), array( '<strong>', '</strong>', '<em>', '</em>' ), $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( '<b>', '</b>', '<i>', '</i>' ), array( '<strong>', '</strong>', '<em>', '</em>' ), $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 '
|
||||
<script>jQuery(document).ready(function(){
|
||||
jQuery("#postexcerpt .handlediv").after("<div style=\"position:absolute;top:12px;right:34px;color:#666;\"><span id=\"excerpt_counter\"></span></div>");
|
||||
<script>
|
||||
jQuery(document).ready(function(){
|
||||
var urls_regexp = new RegExp(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi);
|
||||
|
||||
function get_content_length(){
|
||||
var content = "";
|
||||
// Are we using visual editor?
|
||||
if ((typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden()){
|
||||
content = tinyMCE.activeEditor.getContent();
|
||||
}
|
||||
// Or html editor?
|
||||
else {
|
||||
content = jQuery("#content").val();
|
||||
}
|
||||
content = content.replace(/(<[a-zA-Z\/][^<>]*>|\[([^\]]+)\])/ig,"");
|
||||
var content_length = content.length;
|
||||
var tuit_length = content_length;
|
||||
var urls = content.match(urls_regexp);
|
||||
if (urls) for (i = 0; i < urls.length; i++) tuit_length = tuit_length - urls[i].length + 23;
|
||||
var title_length = jQuery("#title").val().replace(/(<[a-zA-Z\/][^<>]*>|\[([^\]]+)\])/ig,"").length;
|
||||
var return_text = "El título tiene " + title_length.toString() + " caracteres.";
|
||||
return_text += " Y el contenido " + content_length.toString();
|
||||
return_text += content_length != tuit_length ? " (" + tuit_length.toString() + " en Twitter)." : ".";
|
||||
return return_text;
|
||||
}
|
||||
jQuery("#wp-content-media-buttons").after("<button id=\"content-counter\" class=\"button\" title=\"Número de caracteres\" href=\"javascript:;\"><strong>#</strong></button>");
|
||||
jQuery("#content-counter").click(function(e){ alert(get_content_length()); e.preventDefault(); });
|
||||
|
||||
function get_excerpt_length(){
|
||||
var excerpt_length = jQuery("#excerpt").val().length;
|
||||
var tuit_length = excerpt_length;
|
||||
|
@ -291,9 +329,11 @@ function excerpt_count_js() {
|
|||
tuit_length = 140 - tuit_length;
|
||||
return excerpt_length.toString() + " / " + tuit_length.toString() + " (Twitter)";
|
||||
}
|
||||
jQuery("#postexcerpt .handlediv").after("<div style=\"position:absolute;top:12px;right:34px;color:#666;\"><span id=\"excerpt_counter\"></span></div>");
|
||||
jQuery("span#excerpt_counter").text(get_excerpt_length());
|
||||
jQuery("#excerpt").keyup(function(){ jQuery("span#excerpt_counter").text(get_excerpt_length()); });
|
||||
});</script>';
|
||||
});
|
||||
</script>';
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue