diff --git a/www/wp-content/themes/enfold-cille/functions.php b/www/wp-content/themes/enfold-cille/functions.php index 2c39b5c..e1b26ac 100644 --- a/www/wp-content/themes/enfold-cille/functions.php +++ b/www/wp-content/themes/enfold-cille/functions.php @@ -21,15 +21,16 @@ if ( !function_exists( 'write_log' ) ) { } /* - * Force URLs in srcset attributes into HTTPS scheme. - * See http://wptavern.com/how-to-fix-images-not-loading-in-wordpress-4-4-while-using-ssl + * Add a post's category name to body class. + * See https://bavotasan.com/2011/add-a-posts-category-name-to-body-class-in-wordpress/ */ -add_filter( 'wp_calculate_image_srcset', 'ssl_srcset' ); -function ssl_srcset( $sources ) { - foreach ( $sources as &$source ) { - $source['url'] = set_url_scheme( $source['url'], 'https' ); +add_filter( 'body_class', 'add_category_name' ); +function add_category_name( $classes = '' ) { + if ( is_single() ) { + $category = get_the_category(); + $classes[] = 'category-' . $category[0]->slug; } - return $sources; + return $classes; } /* @@ -51,15 +52,11 @@ function add_exif_data( $content ) { add_filter( 'widget_tag_cloud_args', 'custom_tag_cloud_widget' ); function custom_tag_cloud_widget( $args ) { $args['number'] = 0; // Adding a 0 will display all tags. - $args['largest'] = 26; // Largest tag. - $args['smallest'] = 11; // Smallest tag. + $args['largest'] = 38; // Largest tag. + $args['smallest'] = 12; // Smallest tag. $args['unit'] = 'px'; // Tag font unit. return $args; } -add_filter( 'wp_generate_tag_cloud', 'xf_tag_cloud', 10, 3 ); -function xf_tag_cloud( $tag_string ) { - return preg_replace( "/style='font-size:.+px/", "$0 !important", $tag_string ); -} /* * Remove Pingbacks from Recent Comments Widget. @@ -72,36 +69,33 @@ function remove_pingbacks_recent_comments( $array ) { } /* - * Force avia_post_nav() to cycling categories. - * See http://www.kriesi.at/support/topic/avia_post_nav-function-not-cycling-categories-even-when-set-to-true - */ -add_filter( 'avia_post_nav_settings', 'avia_same_category_filter', 10, 1 ); -function avia_same_category_filter( $settings ) { - $settings['same_category'] = true; - return $settings; -} - -/* - * By default the post single page title will be “Blog – Latest News”. This - * function alter this title to reflect the post category instead. + * This function alter default title. * See http://www.kriesi.at/documentation/enfold/replace-the-default-blog-latest-news-title */ add_filter( 'avf_title_args', 'fix_single_post_title', 10, 2 ); function fix_single_post_title( $args, $id ) { - if (is_single()) { - $post_type = get_post_type(); - $category = get_the_category(); - if ( ! empty( $category ) && $post_type == 'post' ) { - // Social title for Twitter, Facebook & LinkedIn categories: - $args['title'] = in_array( $category[0]->name, array( 'Twitter', 'Facebook', 'LinkedIn' ) ) ? 'Social' : $category[0]->name; - $args['link'] = NULL; - $args['heading'] = 'h1'; - } - elseif ( $post_type == 'quote' ) { + $title = $args['title']; + if ( is_single() ) { + if ( get_post_type() == 'quote' ) { $args['title'] = 'Libro de citas'; - $args['link'] = NULL; - $args['heading'] = 'h1'; - } + } + else { + // Post categories by name: + $categories = array_values( array_column( get_the_category(), 'name' ) ); + if ( in_array( $categories[0], array( 'Blog', 'Archivo de notas', 'Apuntes', 'Álbum de fotos' ) ) ) { + $args['title'] = $categories[0]; + } + } + } + elseif ( is_category() || is_tag() ) { + $args['title'] = 'Publicado en: ' . mb_substr( $args['title'], mb_strpos( $args['title'], ': ' ) + 2 ); + } + elseif ( is_tax( 'quote_author_tag' ) ) { + $args['title'] = 'Citas de ' . mb_substr( $args['title'], mb_strpos( $args['title'], ': ' ) + 2 ); + } + if ( $args['title'] != $title ) { + $args['link'] = NULL; + $args['heading'] = 'h1'; } return $args; } @@ -136,49 +130,46 @@ function avia_add_custom_social_icon( $icons ) { } /* - * Add new class in Links and Social, Twitter, Facebook & LinkedIn posts. - * See https://tommcfarlin.com/add-class-to-single-post/ + * Force avia_post_nav() to cycling categories. + * See http://www.kriesi.at/support/topic/avia_post_nav-function-not-cycling-categories-even-when-set-to-true */ -add_filter( 'post_class', 'add_simple_class' ); -function add_simple_class( $classes ) { - $category = get_the_category(); - if ( !empty( $category ) && in_array( $category[0]->term_id, array( 1569, 1561, 1562, 1563, 1564 ) ) ) { - $classes[] = 'post-entry-simple'; - } - return $classes; +add_filter( 'avf_post_nav_settings', 'avia_same_category_filter', 10, 1 ); +function avia_same_category_filter( $settings ) { + $settings['same_category'] = true; + return $settings; } /* - * Breadcrumb formatting for social and categories content. + * Hide some terms in taxonomy list under post in wordpress. + * See https://stackoverflow.com/questions/11245378/how-to-hide-some-categories-in-category-list-under-post-in-wordpress + */ +add_filter( 'get_the_terms', 'exclude_taxonomy_terms', 10, 3 ); +function exclude_taxonomy_terms( $terms, $post, $taxonomy ) { + // List of taxonomy terms (slugs) to exclude: + $exclude_slugs = array( 'portada', 'pelis-y-series', 'lecturas', 'twitter', 'linkedin', 'facebook' ); + + if ( !is_admin() ) { + foreach( $terms as $key => $term ) { + if ( $term->taxonomy == "post_tag" && in_array( $term->slug, $exclude_slugs ) ) { + unset( $terms[$key] ); + } + } + } + return $terms; + +} + +/* + * Breadcrumb formatting for categories content. * See http://www.kriesi.at/support/topic/how-to-modify-in-child_theme-the-function-avia_breadcrumbs-finished-with-s */ add_filter( 'avia_breadcrumbs_trail', 'avia_change_breadcrumb', 50, 2 ); function avia_change_breadcrumb( $trail, $args ) { - if ( is_single() ) { - $category = get_the_category(); - if ( !empty( $category ) ) { - // Correct breadcrumb for Blog, Bloc de notas & Fotoblog single posts: - if ( in_array( $category[0]->term_id, array( 982, 978, 976 ) ) && strpos( $trail[1], 'social' ) ) { - switch ( $category[0]->term_id ) { - case 982: // Blog - array_splice( $trail, 1, 2, array( 'Blog' ) ); - break; - case 978: // Bloc de notas - array_splice( $trail, 1, 2, array( 'Bloc de notas' ) ); - break; - case 976: // Fotoblog - array_splice( $trail, 1, 2, array( 'Álbum de fotos' ) ); - } - } - // Social breadcrumb for Twitter, Facebook & LinkedIn content: - elseif ( in_array( $category[0]->term_id, array( 1562, 1563, 1564 ) ) ) { - unset ( $trail[2] ); - } - } + if ( is_tag() ) { + array_splice( $trail, 1, 0, array( 'Etiquetas' ) ); } - elseif ( is_tag() || is_category( array( 982, 978, 976 ) ) ) { - // Posts breadcrumb for Blog, Bloc de notas & Fotoblog categories: - array_splice( $trail, 1, 0, array( 'Entradas' ) ); + elseif ( is_category() ) { + $trail = array(); } return $trail; } @@ -189,126 +180,91 @@ function avia_change_breadcrumb( $trail, $args ) { */ add_filter( 'wp_insert_post_data', 'before_save_post', 1, 2 ); function before_save_post( $data, $postarr ) { - // Title for Instagram captures: - if ( $data['post_title'] == 'INSTAGRAM' ) { - $dotpos = strpos( $data['post_content'], '. ' ); - $title_instagram = $dotpos > 0 ? substr( $data['post_content'], 0, $dotpos ) : $data['post_content']; - $title_instagram = trim( strip_tags( preg_replace( '!\s+!', ' ', str_replace( ' ', ' ', $title_instagram ) ) ) ); - $title_words = explode( ' ', $title_instagram ); - if ( count( $title_words ) > 12 ) { - $title_instagram = implode( ' ', array_slice( $title_words, 0, 12 ) ) . ' …'; + $text_title = clean_content( $data['post_title'] ); + $text_content = clean_content( $data['post_content'] ); + $text_excerpt = clean_content( $data['post_excerpt'] ); + + if ( $text_title == 'INSTAGRAM' ) { + // Instagram captures: + if ( empty( $text_content ) ) { + $text_title = "He publicado en Instagram"; + $text_excerpt = $text_title; } else { - $data['post_content'] = substr( $data['post_content'], $dotpos + 2 ); - } - $data['post_title'] = $title_instagram; - } + $text_excerpt = text_with_limit( $text_content ); - $title = preg_replace( '!\s+!', ' ', str_replace( ' ', ' ', $data['post_title'] ) ); - $text_title = trim( strip_tags( $title ) ); - - $content = preg_replace( '!\s+!', ' ', str_replace( ' ', ' ', $data['post_content'] ) ); - $text_content = trim( strip_tags( $content ) ); - - if ( empty( $text_title ) ) { - 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; - } - } - - // Retrieve the list of categories for the post: - $categories = $postarr['post_category']; - if ( is_array( $categories ) ) { - if ( $categories[0] == 0 ) { - array_shift( $categories ); + $dotpos = strpos( $data['post_content'], '. ' ); + $text_title = $dotpos > 0 ? substr( $data['post_content'], 0, $dotpos ) : $data['post_content']; + $text_title = clean_content( $text_title ); + $title_words = words_from_text( $text_title ); + if ( count( $title_words ) > 10 ) { + $text_title = text_with_words( $title_words, 8 ); + } + else { + $data['post_content'] = substr( $data['post_content'], $dotpos + 2 ); + } } } else { - // For posts sent from the WordPress App: - $categories = $postarr['tax_input']['category']; - if ( ! is_array( $categories ) ) { - $categories = array(); + if ( empty( $text_title ) && ! empty( $text_content ) ) { + $text_title = text_with_words( words_from_text( $text_content ), 4 ); } + $text_excerpt = text_with_limit( $text_content, 140 - strlen( $text_title ) - 2 ); } - // Count post categories: - $num_categories = count( $categories ); - - // Review post only for Álbum de fotos, Social, Twitter, Facebook & LinkedIn categories: - $num_social_categories = count( array_intersect( array( 976, 1561, 1562, 1563, 1564 ), $categories ) ); - - // 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 ); -# $data['post_content'] = str_replace( array( '', '', '', '' ), array( '', '', '', '' ), $post_content ); -# } - - // Nothing to do if there aren't social categories: - if ( $num_social_categories > 0 ) { - // Preparing post excerpt for Twitter: - $text_excerpt = $text_content; - if ( ! empty( $text_title ) && mb_strpos( $text_content, $text_title ) !== 0 ) { - $text_excerpt = empty( $text_content ) ? $text_title : "$text_title. $text_content"; - } - $text_excerpt = html_entity_decode( $text_excerpt ); - - // Límite máximo. - // Debería ser de 280 caracteres menos los enlaces al blog y a la imagen - // destacada; pero como el plugin SNAP sigue acortando a 140 se opta por - // no restar nada ya que hay margen suficiente hasta los 280: - $limit = 140; -/* // Se le resta el enlace al blog: - $limit -= 25; - // Se le resta el enlace a la imagen destacada (si la tiene): - if (has_post_thumbnail()) { - $limit -= 25; - } -*/ - // Se prepara el texto que se puede mostrar: - if ( strlen( $text_excerpt ) > $limit ) { - preg_match_all( '#\bhttps?://[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/))#', $text_excerpt, $matches ); - for ( $i = 0; $i < count( $matches[0] ); $i++ ) { - $text_excerpt = str_replace( $matches[0][$i], shorten_url( $matches[0][$i] ), $text_excerpt ); - } - if ( strlen( $text_excerpt ) > $limit ) { - $text_excerpt = mb_substr( $text_excerpt, 0, $limit - 1 ); - $text_excerpt = mb_substr( $text_excerpt, 0, mb_strrpos( $text_excerpt, ' ' ) ) . '…'; - } - } - $data['post_excerpt'] = $text_excerpt; - } + $data['post_title'] = $text_title; + $data['post_excerpt'] = $text_excerpt; return $data; } -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; +function clean_content( $content ) { + // Remove [av_...] Avia layout shortcodes to get only text: + $content = preg_replace( '/\[\/?av_one[^\]]*\]/', '', $content ); + $content = preg_replace( '/\[\/?av_two[^\]]*\]/', '', $content ); + $content = preg_replace( '/\[\/?av_three[^\]]*\]/', '', $content ); + $content = preg_replace( '/\[\/?av_four[^\]]*\]/', '', $content ); + $content = preg_replace( '/\[\/?av_section[^\]]*\]/', '', $content ); + $content = preg_replace( '/\[\/?av_layout[^\]]*\]/', '', $content ); + $content = preg_replace( '/\[\/?av_cell[^\]]*\]/', '', $content ); + $content = preg_replace( '/\[\/?av_tab[^\]]*\]/', '', $content ); + $content = preg_replace( '/\[\/?av_textblock[^\]]*\]/', '', $content ); + // And then remove all shortcodes: + $content = strip_shortcodes( $content ); + + // Replace ' ' with space character: + $content = str_replace( ' ', ' ', $content ); + // Convert more than one space in one: + $content = preg_replace( '!\s+!', ' ', $content ); + + // Return clean content: + return trim( strip_tags( $content ) ); +} +function words_from_text( $text ) { + return explode( ' ', $text ); +} +function text_with_words( $words, $number_of_words = 0 ) { + if ( $number_of_words > 0 && count( $words ) > $number_of_words ) { + return implode( ' ', array_slice( $words, 0, $number_of_words ) ) . ' …'; + } + return implode( ' ', $words ); +} +function text_with_limit( $text, $limit = 140 ) { + $text = mb_substr( $text, 0, $limit ); + if ( mb_strlen( $text ) == $limit ) { + $text = mb_substr( $text, 0, mb_strrpos( $text, ' ' ) ); + if ( mb_substr( $text, -1, 1 ) != '.' ) { + $text .= ' …'; + } + } + return $text; } /* * Default image fallback. * See https://www.artifaktdigital.com/use-yoast-seo-ogimage-as-a-fallback-featured-image-a-tutorial/ * -# add_filter( 'post_thumbnail_html', 'social_post_defautl_thumbnail', 20, 5 ); +add_filter( 'post_thumbnail_html', 'social_post_defautl_thumbnail', 20, 5 ); function social_post_defautl_thumbnail( $html, $post_id, $post_thumbnail_id, $size, $attr ) { global $wpdb; @@ -337,6 +293,7 @@ function char_counter_js() { if ( get_post_type() == 'post' ) { echo '