manuel.cillero.es/www/wp-content/themes/enfold-cille/functions.php
Manuel Cillero 5348d987b6 Modifica y simplifica funciones personalizadas
Se elimina la función ssl_srcset() porque ya no aplica. Se añade la
función exclude_taxonomy_terms() para ocultar términos de taxonomía para
ayudar en la publicación de contenidos. También se simplifica la función
avia_change_breadcrumb(), y, sobre todo, la función before_save_post()
para la publicación de contenidos en redes sociales. Sólo se requiere
que el título sea obligatorio.

Y se extienden y corrigen estilos para la visualización de contenidos.
2020-04-19 09:34:42 +02:00

610 lines
22 KiB
PHP

<?php
/*
* Add your own functions here.
* You can also copy some of the theme functions into this file.
* Wordpress will use those functions instead of the original functions then.
*/
if ( !function_exists( 'write_log' ) ) {
function write_log( $log ) {
# if ( true === WP_DEBUG ) {
if ( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ), 3, '/tmp/manuel.cillero.errors.log' );
}
else {
error_log( "$log\n", 3, '/tmp/manuel.cillero.errors.log' );
}
# }
}
}
/*
* 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( 'body_class', 'add_category_name' );
function add_category_name( $classes = '' ) {
if ( is_single() ) {
$category = get_the_category();
$classes[] = 'category-' . $category[0]->slug;
}
return $classes;
}
/*
* Add EXIF data only in photoblog posts using a filter.
* See https://ithemes.com/2011/03/20/automatically-add-content-to-your-wordpress-posts-and-pages#Using_Filters
*/
add_filter( 'the_content', 'add_exif_data' );
function add_exif_data( $content ) {
if ( function_exists('exifography_display_exif') && is_single() && has_category( 976 ) ) {
$content .= exifography_display_exif();
}
return $content;
}
/*
* Visual consolidation of tag clouds.
* See https://codex.wordpress.org/Function_Reference/wp_tag_cloud
*/
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'] = 38; // Largest tag.
$args['smallest'] = 12; // Smallest tag.
$args['unit'] = 'px'; // Tag font unit.
return $args;
}
/*
* Remove Pingbacks from Recent Comments Widget.
* http://ronangelo.com/remove-pingbacks-on-recent-comments-widget
*/
add_filter( 'widget_comments_args', 'remove_pingbacks_recent_comments' );
function remove_pingbacks_recent_comments( $array ) {
$array['type'] = 'comment';
return $array;
}
/*
* 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 ) {
$title = $args['title'];
if ( is_single() ) {
if ( get_post_type() == 'quote' ) {
$args['title'] = 'Libro de citas';
}
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;
}
/*
* Tag page with excerpt and featured image.
* See http://www.kriesi.at/support/topic/archive-page-with-excerpt-and-featured-image/
*/
add_filter( 'avf_blog_style', 'avia_change_tag_page_layout', 10, 2 );
function avia_change_tag_page_layout( $layout, $context ) {
if ( is_tag() ) {
$layout = 'single-small';
}
return $layout;
}
/*
* Register new icons as a theme icons fot GitHub and GitLab.
* See https://kriesi.at/documentation/enfold/custom-social-icons/
*/
add_filter( 'avf_default_icons', 'avia_add_custom_icon', 10, 1 );
function avia_add_custom_icon( $icons ) {
$icons['github'] = array( 'font' =>'fontello', 'icon' => 'uf09b' );
$icons['gitlab'] = array( 'font' =>'fontello', 'icon' => 'uf296' );
return $icons;
}
add_filter( 'avf_social_icons_options', 'avia_add_custom_social_icon', 10, 1 );
function avia_add_custom_social_icon( $icons ) {
$icons['GitHub'] = 'github';
$icons['GitLab'] = 'gitlab';
return $icons;
}
/*
* 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( 'avf_post_nav_settings', 'avia_same_category_filter', 10, 1 );
function avia_same_category_filter( $settings ) {
$settings['same_category'] = true;
return $settings;
}
/*
* 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_tag() ) {
array_splice( $trail, 1, 0, array( '<a href="/tags">Etiquetas</a>' ) );
}
elseif ( is_category() ) {
$trail = array();
}
return $trail;
}
/*
* Auto generate the post title if it's empty and post has content. And share
* content in social networks if required.
*/
add_filter( 'wp_insert_post_data', 'before_save_post', 1, 2 );
function before_save_post( $data, $postarr ) {
$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 {
$text_excerpt = text_with_limit( $text_content );
$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 {
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 );
}
$data['post_title'] = $text_title;
$data['post_excerpt'] = $text_excerpt;
return $data;
}
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 '&nbsp;' with space character:
$content = str_replace( '&nbsp;', ' ', $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 );
function social_post_defautl_thumbnail( $html, $post_id, $post_thumbnail_id, $size, $attr ) {
global $wpdb;
if ( empty( $html ) ) {
// Return you Yoast SEO default image if the post thumbnail html is empty:
$opt = get_option( 'wpseo_social' );
$url = $opt['og_default_image'];
if ( $id !== '' ) {
// Retrieves the attachment ID from the file URL:
$attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid = '%s';", $url ) );
$id = $attachment[0];
return wp_get_attachment_image( $id, $size );
}
}
return $html;
} */
/*
* 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', '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(){
var total_length = 140;
var urls_regexp = new RegExp(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi);
function htmlDecode(input){
input = input.replace(/(<[a-zA-Z\/][^<>]*>|\[([^\]]+)\])/ig,"");
// https://stackoverflow.com/a/34064434
var doc = new DOMParser().parseFromString(input, "text/html");
return doc.documentElement.textContent;
}
function get_content_length(){
var title_length = htmlDecode(jQuery("#title").val()).length;
var content = "";
// Are we using visual editor?
if ((typeof tinyMCE != "undefined") && tinyMCE.activeEditor && !tinyMCE.activeEditor.isHidden()){
content = htmlDecode(tinyMCE.activeEditor.getContent());
}
// Or html editor?
else {
content = htmlDecode(jQuery("#content").val());
}
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 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;
var urls = jQuery("#excerpt").val().match(urls_regexp);
if (urls) for (i = 0; i < urls.length; i++) tuit_length = tuit_length - urls[i].length + 23;
tuit_length = total_length - 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>';
}
}
/*
* New widget for Enfold theme. Display the single post header with the slider,
* title and meta info.
*/
add_action( 'widgets_init', function() {
register_widget( 'enfold_post_header_widget' );
});
class enfold_post_header_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID:
'enfold_post_header_widget',
// Widget name will appear in UI:
__( 'Enfold Cille Post Header', 'avia_framework' ),
// Widget description:
array( 'description' => __( 'Display the single post header with the slider, title and meta info', 'avia_framework' ) )
);
}
public function widget( $args, $instance ) {
global $avia_config;
echo $args['before_widget'];
$blog_style = avia_get_option( 'single_post_style', 'single-big' );
$blog_global_style = avia_get_option( 'blog_global_style', '' );
$the_id = get_the_ID();
// Get the current post id, the current post class and current post format:
$url = '';
$current_post = array();
$current_post['the_id'] = $the_id;
$current_post['post_type'] = get_post_type( $current_post['the_id'] );
$current_post['post_class'] = 'post-entry-' . $current_post['the_id'] . ' ' . $blog_style;
$current_post['post_class'] .= $current_post['post_type'] == 'post' ? '' : ' post';
$current_post['post_format'] = get_post_format() ? get_post_format() : 'standard';
$current_post['post_layout'] = avia_layout_class( 'main', false );
// Retrieve slider and title for this post...
$size = strpos( $blog_style, 'big' ) ? strpos( $current_post['post_layout'], 'sidebar' ) !== false ? 'entry_with_sidebar' : 'entry_without_sidebar' : 'square';
if ( ! empty( $avia_config['preview_mode'] ) && ! empty( $avia_config['image_size'] ) && $avia_config['preview_mode'] == 'custom' ) {
$size = $avia_config['image_size'];
}
$current_post['slider'] = get_the_post_thumbnail( $current_post['the_id'], $size );
if ( get_post_meta( $current_post['the_id'], '_avia_hide_featured_image', true ) ) {
$current_post['slider'] = '';
}
$current_post['title'] = get_the_title();
// Now apply a filter, based on the post type... (filter function is located in includes/helper-post-format.php):
$current_post = apply_filters( 'post-format-' . $current_post['post_format'], $current_post );
// Extract the variables so that $current_post['slider'] becomes $slider, $current_post['title'] becomes $title, etc...
extract( $current_post );
// Default link and preview image description:
$link = avia_image_by_id( get_post_thumbnail_id(), 'large', 'url' );
$desc = get_post( get_post_thumbnail_id() );
if ( is_object( $desc ) ) {
$desc = $desc->post_excerpt;
}
$featured_img_desc = $desc != '' ? $desc : the_title_attribute( 'echo=0' );
// Echo preview image:
if ( strpos( $blog_global_style, 'elegant-blog' ) === false ) {
if ( strpos( $blog_style, 'big' ) !== false ) {
if ( $slider ) $slider = "<a href=\"$link\" title=\"$featured_img_desc\">$slider</a>";
if ( $slider ) echo "<div class=\"big-preview $blog_style\">$slider</div>";
}
}
echo '<div class="blog-meta">';
if ( strpos( $blog_style, 'multi' ) !== false ) {
$author_name = apply_filters( 'avf_author_name', get_the_author_meta( 'display_name', $post->post_author ), $post->post_author );
$author_email = apply_filters( 'avf_author_email', get_the_author_meta( 'email', $post->post_author ), $post->post_author );
$link = get_author_posts_url( $post->post_author );
}
echo '</div>';
echo '<header class="entry-content-header">';
$taxonomies = get_object_taxonomies( get_post_type( $the_id ) );
$cats = '';
$excluded_taxonomies = array_merge( get_taxonomies( array( 'public' => false ) ), array( 'post_tag', 'post_format' ) );
$excluded_taxonomies = apply_filters( 'avf_exclude_taxonomies', $excluded_taxonomies, get_post_type( $the_id ), $the_id );
if ( ! empty( $taxonomies ) ) {
foreach ( $taxonomies as $taxonomy ) {
if ( ! in_array( $taxonomy, $excluded_taxonomies ) ) {
$cats .= get_the_term_list( $the_id, $taxonomy, '', ', ', '') . ' ';
}
}
}
if ( strpos( $blog_global_style, 'elegant-blog' ) !== false ) {
$cat_output = '';
if ( ! empty( $cats ) ) {
$cat_output .= '<span class="blog-categories minor-meta">';
$cat_output .= $cats;
$cat_output .= '</span>';
$cats = '';
}
echo strpos( $blog_global_style, 'modern-blog' ) === false ? $cat_output . $title : $title . $cat_output;
echo '<span class="av-vertical-delimiter"></span>';
if ( strpos( $blog_style, 'big' ) !== false ) {
if ( $slider ) $slider = "<a href=\"$link\" title=\"$featured_img_desc\">$slider</a>";
if ( $slider ) echo "<div class=\"big-preview $blog_style\">$slider</div>";
}
$cats = '';
$title = '';
}
echo $title;
echo '<span class="post-meta-infos">';
echo '<time class="date-container minor-meta updated">' . get_the_time( get_option( 'date_format' ) ) . '</time>';
echo '<span class="text-sep text-sep-date">/</span>';
if ( get_comments_number() != '0' || comments_open() ) {
echo '<span class="comment-container minor-meta">';
comments_popup_link( '0 ' . __( 'Comments', 'avia_framework' ),
'1 ' . __( 'Comment' , 'avia_framework' ),
'% ' . __( 'Comments', 'avia_framework' ), 'comments-link',
'' . __( 'Comments Disabled', 'avia_framework' ) );
echo '</span>';
echo '<span class="text-sep text-sep-comment">/</span>';
}
if ( ! empty( $cats ) ) {
echo '<span class="blog-categories minor-meta">' . __( 'in', 'avia_framework' ) . ' ';
echo $cats;
echo '</span><span class="text-sep text-sep-cat">/</span>';
}
echo '<span class="blog-author minor-meta">' . __( 'by', 'avia_framework' ) . ' ';
echo '<span class="entry-author-link">';
echo '<span class="vcard author"><span class="fn">';
the_author_posts_link();
echo '</span></span>';
echo '</span>';
echo '</span>';
echo '</span>';
echo '</header>';
echo $args['after_widget'];
}
}
/*
* New widget for Enfold theme. Display the single post footer with the tags,
* social share links and related posts.
* See http://www.wpbeginner.com/wp-tutorials/how-to-create-a-custom-wordpress-widget/
*/
add_action( 'widgets_init', function() {
register_widget( 'enfold_post_footer_widget' );
});
class enfold_post_footer_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID:
'enfold_post_footer_widget',
// Widget name will appear in UI:
__( 'Enfold Cille Post Footer', 'avia_framework' ),
// Widget description:
array( 'description' => __( 'Display the single post footer with the tags, social share links and related posts', 'avia_framework' ) )
);
}
public function widget( $args, $instance ) {
echo $args['before_widget'];
echo '<footer class="entry-footer">';
if ( has_tag() ) {
echo '<span class="blog-tags minor-meta">';
echo the_tags( '<strong>' . __( 'Tags:', 'avia_framework' ) . '</strong><span> ' );
echo '</span></span>';
}
avia_social_share_links();
echo '</footer>';
echo '<div style="margin-top: 172px;">';
echo get_template_part( '../enfold/includes/related-posts');
echo '</div>';
echo $args['after_widget'];
}
}
/*
* Enable a new widget sidebar to display widgets after the loop.
* See https://stackoverflow.com/questions/39181634/how-to-add-a-widget-after-post-content
* See https://wordpress.stackexchange.com/questions/107113/output-before-and-after-the-loop
*/
register_sidebar( array(
'name' => 'Displayed After Loop',
'id' => 'after-loop',
'description' => '',
'before_widget' => '<div class="quotes-after-loop">',
'after_widget' => '</div>',
'before_title' => '<h5>',
'after_title' => '</h5>',
) );
add_action( 'loop_end', 'wp_content_after_loop' );
function wp_content_after_loop( $query ) {
if ( $query->is_main_query() && is_active_sidebar( 'after-loop' ) ) {
dynamic_sidebar( 'after-loop' );
}
}
/*
* Restore the font size in the tag cloud widget using a copy of the entire Avia
* stylesheet "enfold/css/avia-snippet-widget.css" from Enfold 4.5, and removing
* the font size line in the tag cloud styles.
* See https://kriesi.at/support/topic/tag-cloud-widget-display/#post-684196
*/
add_action( 'wp_enqueue_scripts', 'wp_change_layoutcss', 20 );
function wp_change_layoutcss() {
wp_dequeue_style( 'avia-widget-css' );
wp_enqueue_style( 'avia-widget-css-child', get_stylesheet_directory_uri() . '/avia-snippet-widget.css' );
}
/*
* Remove Avia Framework debug information in child theme.
* See http://www.kriesi.at/support/topic/removal-of-theme-debug-info/#post-386207
*/
add_action( 'init', 'avia_remove_debug' );
function avia_remove_debug() {
remove_action( 'wp_head', 'avia_debugging_info', 1000 );
}
/*
* Disable the W3 Total Cache Footer Comment.
* See https://www.dylanbarlett.com/2014/01/disabling-w3-total-cache-footer-comment/
*/
add_filter( 'w3tc_can_print_comment', '__return_false', 10, 1 );
/*
* Adding legal information in socket using a shortcode.
* See http://www.kriesi.at/support/topic/how-do-i-add-a-dynamic-current-year-to-socket
*/
add_shortcode( 'infolegal', 'infolegal_func' );
function infolegal_func( $atts ) {
$legal = '<div id="legal">';
$legal .= '<span class="legal-notice"><a href="/legal">Aviso legal</a></span>';
$legal .= '<span class="legal-terms"> &nbsp;|&nbsp; <a href="/legal/condiciones-de-uso">Condiciones de uso</a></span>';
$legal .= '<span class="legal-privacy"> &nbsp;|&nbsp; <a href="/legal/politica-de-privacidad">Política de privacidad</a></span>';
$legal .= '<span class="legal-cookies"> &nbsp;|&nbsp; <a href="/legal/politica-de-cookies">Uso de cookies</a></span>';
$legal .= '</div> ';
$legal .= '2009-' . date( 'Y' ) . ' &copy; manuel.cillero.es';
return $legal;
}
/*
* Hide text or emails from Spam Bots using a shortcode.
* Use: [antibots]text or email[/antibots].
* See https://developer.wordpress.org/reference/functions/antispambot/#comment-756
*/
add_shortcode( 'antibots', 'hide_content_antibots_shortcode' );
function hide_content_antibots_shortcode( $atts, $content = null ) {
if ( is_email( $content ) ) {
return '<a href="' . antispambot( 'mailto:' . $content ) . '">' . esc_html( antispambot( $content ) ) . '</a>';
}
return antispambot( $content );
}