manuel.cillero.es/enfold-cille/functions.php
2017-10-12 07:24:23 +02:00

323 lines
12 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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' );
}
# }
}
}
/*
* 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_filter( 'wp_calculate_image_srcset', 'ssl_srcset' );
function ssl_srcset( $sources ) {
foreach ( $sources as &$source ) {
$source['url'] = set_url_scheme( $source['url'], 'https' );
}
return $sources;
}
/*
* 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'] = 26; // Largest tag.
$args['smallest'] = 11; // 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.
* 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;
}
/*
* 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.
* 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 ) {
$category = get_the_category();
if ( $args['title'] == __( 'Blog - Latest News', 'avia_framework' ) && !empty( $category ) ) {
// Social title for Twitter, Facebook & LinkedIn categories:
$args['title'] = in_array( $category[0]->term_id, array( 1562, 1563, 1564 ) ) ? 'Social' : $category[0]->name;
$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;
}
/*
* Add new class in Social, Twitter, Facebook & LinkedIn posts.
* See https://tommcfarlin.com/add-class-to-single-post/
*/
add_filter( 'post_class', 'add_social_class' );
function add_social_class( $classes ) {
$category = get_the_category();
if ( !empty( $category ) && in_array( $category[0]->term_id, array( 1561, 1562, 1563, 1564 ) ) ) {
$classes[] = 'post-entry-social';
}
return $classes;
}
/*
* Breadcrumb formatting for social and 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( '<a href="/blog/category/blog">Blog</a>' ) );
break;
case 978: // Bloc de notas
array_splice( $trail, 1, 2, array( '<a href="/blog/category/notebook">Bloc de notas</a>' ) );
break;
case 976: // Fotoblog
array_splice( $trail, 1, 2, array( '<a href="/blog/category/photoblog">Fotoblog</a>' ) );
}
}
// Social breadcrumb for Twitter, Facebook & LinkedIn content:
elseif ( in_array( $category[0]->term_id, array( 1562, 1563, 1564 ) ) ) {
unset ( $trail[2] );
}
}
}
elseif ( is_tag() || is_category( array( 982, 978, 976 ) ) ) {
// Posts breadcrumb for Blog, Bloc de notas & Fotoblog categories:
array_splice( $trail, 1, 0, array( '<a href="/posts" title="Todas las entradas">Entradas</a>' ) );
}
return $trail;
}
/*
* 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 ) {
// 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 );
$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 ) );
if ( $num_social_categories > 0 ) {
// Get clean post title:
$post_title = trim( str_replace( '&nbsp;', ' ', strip_tags( $data['post_title'] ) ) );
// Get clean post content:
$post_content = trim( str_replace( '&nbsp;', ' ', strip_tags( $data['post_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;
// Adapting content for pure social posts:
if ( $num_categories == $num_social_categories ) {
$content = trim( str_replace( '&nbsp;', ' ', strip_tags( $data['post_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
*/
$content = preg_replace( "/<([a-z][a-z0-9]+)[^>]*?(\/?)>/i", '<$1$2>', $content );
$content = str_replace( array( '<b>', '</b>', '<i>', '</i>' ), array( '<strong>', '</strong>', '<em>', '</em>' ), $content );
$data['post_content'] = $content;
}
}
return $data;
}
/*
* 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 Character Counter to the WordPress 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() {
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>");
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_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 = 140 - tuit_length;
return excerpt_length.toString() + " / " + tuit_length.toString() + " (Twitter)";
}
jQuery("span#excerpt_counter").text(get_excerpt_length());
jQuery("#excerpt").keyup(function(){ jQuery("span#excerpt_counter").text(get_excerpt_length()); });
});</script>';
}
}
/*
* Ensure that AccessPress Social Auto Post works in all cases.
*
add_action( 'publish_post', 'publish_social_post', 10, 2 );
function publish_social_post( $ID, $post ) {
$auto_post = $_POST['asap_auto_post'];
if ( $auto_post == 'yes' || $auto_post == '' ) {
$plugin_asap_dir = ABSPATH . 'wp-content/plugins/accesspress-social-auto-post/';
// Only Facebook and LinkedIn:
include_once( $plugin_asap_dir . 'api/facebook/facebook.php' );
# include_once( $plugin_asap_dir . 'api/twitter/codebird.php' );
# include_once( $plugin_asap_dir . 'api/tumblr/TumblrAPIClient.php' );
include_once( $plugin_asap_dir . 'api/linkedin/liOAuth.php' );
include( $plugin_asap_dir . 'inc/cores/auto-post.php' );
$check = update_post_meta( $post->ID, 'asap_auto_post', 'no' );
$_POST['asap_auto_post'] = 'no';
}
} */
/*
* 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 a dynamic current year to socket.
* See http://www.kriesi.at/support/topic/how-do-i-add-a-dynamic-current-year-to-socket
*/
add_shortcode( 'y', 'current_year_func' );
function current_year_func( $atts ) {
return date( 'Y' );
}