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( '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( 'Fotoblog' ) ); } } // 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( 'Entradas' ) ); } return $trail; } /* * Auto generate the post title if it's empty and post has content. */ 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' ) ); $num_categories = count( $categories ); // Review post only for Social, Twitter, Facebook & LinkedIn categories: $num_social_categories = count( array_intersect( array( 'Social', 'Twitter', 'Facebook', 'LinkedIn' ), $categories ) ); // Nothing to do if there aren't social categories: if ( $num_social_categories > 0 ) { // 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 cleaned post content: $content = preg_replace( '!\s+!', ' ', str_replace( ' ', ' ', $post->post_content ) ); $text_content = trim( strip_tags( $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: / 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 ); } // Preparing post excerpt for Twitter: $text_excerpt = $text_content; if ( mb_strpos( $text_content, $text_title ) !== 0 ) { $post_excerpt = $text_title; $post_excerpt .= empty( $text_content ) ? '.' : ". $text_content"; } $post_excerpt = html_entity_decode( $text_excerpt ); $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 ) { $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; } /* * 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 ' '; } } /* * 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' ); }