'; print_r($array); echo ''; } // Live Resize of Post Thumbnail function un_post_thumbnail( $id=null, $width=null, $height=null, $crop=false, $html=true, $default=null ){ // Check ID if(!$id || empty($id) || !isset($id)) { $id = get_the_ID(); } // Check Sizes if(!$width || empty($width) || !isset($width)) { $width = $height; } if(!$height || empty($height) || !isset($height)) { $height = $width; } // Get Original URL $original_url = wp_get_attachment_url( get_post_thumbnail_id($id) ); if( $original_url ){ // Generate New URL $new_url = aq_resize( $original_url, $width, $height, $crop, true, true ); }else{ // Put the default value $new_url = $default; } if($new_url && $html == true){ return ''; }else{ return $new_url; } } // Post Original Image SRC function un_post_thumbnail_src( $id=null ){ // Check ID if(!$id || empty($id) || !isset($id)) { $id = get_the_ID(); } // Get Original URL $url = wp_get_attachment_url( get_post_thumbnail_id($id) ); return $url; } // Live Resize of Attachment function un_attachment( $att_id, $width=null, $height=null, $crop=false, $html=true, $default=null ){ // Check Sizes if(!$width || empty($width) || !isset($width)) { $width = $height; } if(!$height || empty($height) || !isset($height)) { $height = $width; } // Get Original URL $original_url = wp_get_attachment_url( $att_id ); // Generate New URL $new_url = aq_resize( $original_url, $width, $height, $crop, true, true ); if( !$new_url ){ $new_url = $default; } if($html == true){ return ''; }else{ return $new_url; } } // Build the term HTML function un_single_term($term_id, $taxonomy=null){ // Check Taxonomy if( !$taxonomy ){ $taxonomy = 'category'; } // Get the term data $term = get_term($term_id, $taxonomy); // Check the data if ( is_object( $term ) && count($term) > 0 ) { $url = esc_url(get_term_link( $term->slug, $taxonomy )); $name = $term->name; }else{ return ''; } return ''.$name.''; } // Get Category slug from ID function un_category_slug($cat_id){ if( term_exists(intval($cat_id)) ){ $cat = get_category( $cat_id ); return $cat->slug; }else{ return ''; } } // Related Posts Query function un_related_posts_query_args( $post_id, $taxonomy='category', $n = 4 ){ // Check Post Type $post_type = get_post_type( $post_id ); // Get the terms of the post $terms = wp_get_post_terms( $post_id, $taxonomy ); // Build an ids array if( $terms && count($terms) > 0 ) { $terms_ids = array(); foreach($terms as $term){ $terms_ids[] = $term->term_id; } }else{ return; } // Build Query Args $args = array( 'post_type' => $post_type, 'post_status' => 'publish', 'pagination' => false, 'posts_per_page' => $n, 'order' => 'ASC', 'orderby' => 'title', 'post__not_in' => array($post_id), 'tax_query' => array( array( 'taxonomy' => $taxonomy, 'field' => 'term_id', 'terms' => $terms_ids, ), ) ); // Return the $args return $args; } // Get post terms function un_post_terms_array( $post_id, $taxonomy='category', $limit=null ) { $terms = wp_get_post_terms( $post_id, $taxonomy ); $result = array(); foreach( $terms as $term ){ $result[] = array( 'name' => $term->name, 'slug' => $term->slug, 'id' => $term->term_id, 'permalink' => get_term_link( $term->slug, $taxonomy ), ); } if( $limit ){ $result = array_slice( $result, 0, $limit ); } return $result; } // Get post terms as list function un_post_terms_list( $post_id, $taxonomy=null, $label='name', $sep=',', $limit=9999, $link=false, $before=null, $after=null ) { if( !$taxonomy ){ $taxonomy = 'category'; } $terms = wp_get_post_terms( $post_id, $taxonomy ); $result = ''; $start_sep = ''; foreach( $terms as $index => $term ){ if( $index >= $limit ){ break; } if( $link ){ $term_output = $before.''.$term->$label.''.$after; }else{ $term_output = $before.$term->$label.$after; } $result .= $start_sep.$term_output; $start_sep = $sep; } return $result; } // Get post main term (label or link) function un_post_single_term( $post_id, $taxonomy=null, $label='name', $link=true ) { if( !$taxonomy ){ $taxonomy = 'category'; } $terms = wp_get_post_terms( $post_id, $taxonomy, array('orderby' => 'count', 'order' => 'DESC', 'fields' => 'all') ); $result = ''; if( count($terms) > 0 ){ $term = $terms[0]; if( $link ){ $result = ''.$term->$label.''; }else{ $result = $term->$label; } } return $result; } // Get taxonomy terms as array function un_get_terms_array( $taxonomies=array('category'), $custom_args=null ) { $terms = get_terms( $taxonomies, $custom_args ); $result = array(); foreach( $terms as $term ){ $result[] = array( 'name' => $term->name, 'slug' => $term->slug, 'id' => $term->term_id, 'url' => get_term_link( $term->slug, $term->taxonomy ), 'count' => $term->count, ); } return $result; } // Get Multiple Post Meta by Type // Types: category, categories, author, date, format, comments function un_get_post_meta_by_type($type, $cats_count=null, $cats_sep=null, $html=true){ // Data $data = ''; // Type Switch switch( $type ){ case 'category': $data = un_post_single_term( get_the_ID(), 'category', 'name', $html ); break; case 'categories': $data = un_post_terms_list( get_the_ID(), 'category', 'name', $cats_sep, $cats_count, $html ); break; case 'author': if( $html == true ){ $data = ''.get_the_author().''; }else{ $data = get_the_author(); } break; case 'date': if( $html == true ){ $archive_year = get_the_date('Y'); $archive_month = get_the_date('m'); $archive_day = get_the_date('d'); $data = ''.get_the_date().''; }else{ $data = get_the_date(); } break; case 'format': if( get_post_format() ){ $format = get_post_format(); }else{ $format = 'standard'; } if( $html == true ){ $data = ''.$format.''; }else{ $data = $format; } break; case 'comments': if( $html == true ){ $data = ''.get_comments_number( get_the_ID() ).esc_html__(' comments', 'adena').''; }else{ $data = get_comments_number( get_the_ID() ).esc_html__(' comments', 'adena'); } break; } return $data; } // FULL Pagination function un_full_pagination($custom_query = null, $range = 1) { $showitems = ($range * 2)+1; $html = ''; global $paged; if(empty($paged)) $paged = 1; if($custom_query == ''){ global $wp_query; $pages = $wp_query->max_num_pages; if(!$pages){ $pages = 1; } } else{ $pages = $custom_query->max_num_pages; if(!$pages){ $pages = 1; } } if(1 != $pages){ $html .= '
'; } return $html; } // SMALL Pagination function un_small_pagination($custom_query = null, $lang_domain) { if($custom_query == ''){ global $wp_query; $pages = $wp_query->max_num_pages; if(!$pages){ $pages = 1; } } else{ $pages = $custom_query->max_num_pages; if(!$pages){ $pages = 1; } } $html = ''; $newer = get_previous_posts_link( ''.esc_html__('Newer Posts', 'adena'), $pages ); $older = get_next_posts_link( esc_html__('older posts', 'adena').'', $pages ); if($newer || $older){ $html .= '
'; if($newer){ $html .= '
'.$newer.'
'; } if($older) { $html .= '
'.$older.'
'; } $html .= '
'; } return $html; } // BLOG STATS function un_blog_stats() { // POSTS // $posts_args = array( 'posts_per_page' => -1, 'post_type' => 'post', 'post_status' => 'publish', ); $posts = get_posts( $posts_args ); // Posts Number $posts_num = count( $posts ); // WORDS PER POST $words_num = 0; foreach( $posts as $post ){ $words_num = $words_num + un_post_words($post->ID); } // MEDIA // $media_args = array( 'posts_per_page' => -1, 'post_type' => 'attachment', ); $media = get_posts( $media_args ); // Media Number $media_num = count( $media ); // COMMENTS // $comments_args = array( 'count' => true, ); // Comments Number $comments_num = get_comments( $comments_args ); // VISITS // $total_visits = 0; foreach( $posts as $post ){ $total_visits = $total_visits + un_post_views( $post->ID, 'get' ); } // DATA ELABORATION // $data = array( 'posts' => $posts_num, 'visits' => $total_visits, 'media' => $media_num, 'words' => $words_num, 'commments' => $comments_num, ); $max_value = max( $data ); $max_width = 200; $coeff = $max_width / $max_value; foreach( $data as $label=>$value ){ $width = round($value * $coeff) + 140; $data[$label] = array( 'number' => $value, 'width' => $width ); } return $data; } // AUTHOR STATS function un_author_stats($author_id=null) { // Check ID if(!$author_id || empty($author_id) || !isset($author_id)) { $author_id = get_the_author_meta( 'ID' ); } // POSTS // $posts_args = array( 'posts_per_page' => -1, 'post_type' => 'post', 'author' => $author_id, 'post_status' => 'publish', ); $posts = get_posts( $posts_args ); // Posts Number $posts_num = count( $posts ); // WORDS PER POST $words_avg = 0; foreach( $posts as $post ){ $words_avg = $words_avg + un_post_words($post->ID); } $words_avg = round($words_avg / $posts_num); // MEDIA // $media_args = array( 'posts_per_page' => -1, 'post_type' => 'attachment', 'author' => $author_id, ); $media = get_posts( $media_args ); // Media Number $media_num = count( $media ); // COMMENTS // $comments_args = array( 'author__in' => array($author_id), 'count' => true, ); // Comments Number $comments_num = get_comments( $comments_args ); // VISITS // $total_visits = 0; foreach( $posts as $post ){ $total_visits = $total_visits + un_post_views( $post->ID, 'get' ); } // DATA ELABORATION // $data = array( 'posts' => $posts_num, 'words/post' => $words_avg, 'media' => $media_num, 'comments' => $comments_num, 'visits' => $total_visits ); $max_value = max( $data ); $max_width = 130; $coeff = $max_width / $max_value; foreach( $data as $label=>$value ){ $width = round($value * $coeff) + 120; $data[$label] = array( 'number' => $value, 'width' => $width ); } return $data; } // Authors List function un_authors_list(){ $args = array( 'blog_id' => $GLOBALS['blog_id'], 'orderby' => 'display_name', 'order' => 'ASC', 'who' => 'authors', ); return get_users( $args ); } // Author Avatar URL function un_get_avatar_url($avatar){ preg_match('/< *img[^>]*src *= *["\']?([^"\']*)/i', $avatar, $matches); if( count($matches) > 0 ) { return $matches[1]; }else{ return ''; } } // URL EXPLODE function un_url_explode($url){ // Check IF is valid if (filter_var($url, FILTER_VALIDATE_URL) === false) { return 'You url is not valid, probably is empty or is missing the http://'; } // Parse URL $url_parsed = parse_url($url); if( isset($url_parsed['host']) ){ $host = $url_parsed['host']; }else{ $host = ''; } if( isset($url_parsed['path']) ) { $path = substr($url_parsed['path'], 1); }else{ $path = ''; } return array( 'url' => $url, 'prot' => $url_parsed['scheme'].'://', 'host' => $host, 'path' => $path, 'query' => '?'.$url_parsed['query'], ); } // Sanitize String function un_sanitize_string( $string ) { // Remove special accented characters $clean_string = strtr($string, ' ŠšŸÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝàáâãäåçèéêëìíîïñòóôõöøùúûüýÿ', 'SZszYAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy'); $clean_string = strtr($clean_string, array('&Thorn;' => 'TH', 'þ' => 'th', '&Eth;' => 'DH', ' ð' => 'dh', 'ß' => 'ss', 'Œ' => 'OE', 'œ' => 'oe', '&Aelig;' => 'AE', 'æ' => 'ae', 'µ' => 'u')); $clean_string = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('-', '.', ''), $clean_string); $clean_string = strtolower($clean_string); return $clean_string; } // Sanitize HTML function un_sanitize_html($string, $post_capability = false) { if($post_capability == false) { return wp_kses($string, array()); }else{ $tags = wp_kses_allowed_html( 'post' ); // script $tags['script'] = array( 'language' => 1, 'src' => 1, 'type' => 1 ); // iframe $tags['iframe'] = array( 'src' => array(), 'height' => array(), 'width' => array(), 'frameborder' => array(), 'allowfullscreen' => array(), ); // form fields - input $tags['input'] = array( 'class' => array(), 'id' => array(), 'name' => array(), 'value' => array(), 'type' => array(), ); // select $tags['select'] = array( 'class' => array(), 'id' => array(), 'name' => array(), 'value' => array(), 'type' => array(), ); // select options $tags['option'] = array( 'selected' => array(), ); // style $tags['style'] = array( 'types' => array(), ); return wp_kses($string, $tags); } } // A secure and valid ECHO function function un_echo($data, $context=null, $tags=array()){ switch( $context ){ case 'html': echo un_sanitize_html( $data, true ); break; case 'string': echo wp_kses( $data, array() ); break; case 'sanitize': echo un_sanitize_string( wp_kses($data, array()) ); break; case 'attr': echo esc_attr( $data ); break; case 'js': echo esc_js( $data ); break; case 'url': echo esc_url( $data ); break; case 'blockquote': $tags = array( 'a' => array( 'href' => array(), 'title' => array(), 'target' => array(), 'class' => array(), 'id' => array(), ), 'br' => array( 'class' => array(), 'id' => array(), ), 'em' => array( 'class' => array(), 'id' => array(), ), 'strong' => array( 'class' => array(), 'id' => array(), ), 'span' => array( 'class' => array(), 'id' => array(), ), ); echo wp_kses( $data, $tags ); break; case 'custom': echo wp_kses( $data, $tags ); break; case 'full': echo ''.$data; break; default: echo un_sanitize_html( $data, true ); break; } } // Check if is_woocommerce function un_is_woocommerce() { if( (function_exists('is_woocommerce') && is_woocommerce()) || (function_exists('is_cart') && is_cart()) || (function_exists('is_checkout') && is_checkout()) || (function_exists('is_account_page') && is_account_page()) || (function_exists('is_add_payment_method_page') && is_add_payment_method_page()) || (function_exists('is_checkout_pay_page') && is_checkout_pay_page()) || (function_exists('is_order_received_page') && is_order_received_page()) || (function_exists('is_view_order_page') && is_view_order_page()) || (function_exists('is_checkout') && is_checkout()) || (function_exists('is_product') && is_product()) || (function_exists('is_shop') && is_shop()) ) { return true; }else{ return false; } } // Post Views function un_post_views($id=null, $mode) { // Get the ID from the Loop if( !$id || empty($id) ){ $id = get_the_ID(); } // Still empty? Stop Function if( !$id || empty($id) ){ return; } // Views Key $count_key = 'un_post_views_count'; // Views Value $count = get_post_meta($id, $count_key, true); if($count == '' || empty( $count ) || $count == '0' ){ $count = 0; // Delete Key delete_post_meta($id, $count_key); // Add Key and reset it to 1 add_post_meta($id, $count_key, '1'); }else{ if( $mode == 'set' || $mode == 'both' ){ // Update Key Value $count++; update_post_meta($id, $count_key, $count); } } if( $mode == 'get' || $mode == 'both' ){ return $count; } } // Next Post Permalink function un_next_post_permalink(){ // Check if Exixts if ( is_object( get_next_post()) ){ $next_post = get_next_post(); $next_url = get_permalink($next_post->ID); } // Return if( !empty($next_url) ){ return $next_url; } } // Prev Post Permalink function un_prev_post_permalink(){ // Check if Exixts if ( is_object( get_previous_post()) ){ $prev_post = get_previous_post(); $prev_url = get_permalink($prev_post->ID); } // Return if( !empty($prev_url) ){ return $prev_url; } } // Next Post Title function un_next_post_title(){ // Check if Exixts if ( is_object( get_next_post()) ){ $next_post = get_next_post(); $next_title = $next_post->post_title; } // Return if( !empty($next_title) ){ return $next_title; } } // Prev Post Title function un_prev_post_title(){ // Check if Exixts if ( is_object( get_previous_post()) ){ $prev_post = get_previous_post(); $prev_title = $prev_post->post_title; } // Return if( !empty($prev_title) ){ return $prev_title; } } // Post Words Count function un_post_words($id=null){ // Get the ID from the Loop if( !$id || empty($id) ){ $id = get_the_ID(); } // Still empty? Stop Function if( !$id || empty($id) ){ return; } $content = get_post_field( 'post_content', $id ); $words_count = str_word_count( strip_tags( $content ) ); return $words_count; } // Post Excerpt function un_post_exerpt($post_id=null, $words=null, $html=false, $more=' [...]' ){ // Get the ID from the Loop if( !$post_id || empty($post_id) ){ $post_id = get_the_ID(); } // Still empty? Stop Function if( !$post_id || empty($post_id) ){ return; } // Get the words if( !$words || empty($words) ){ $words = 100; } // Retrieve the content $excerpt = get_post( $post_id ); $excerpt = $excerpt->post_content; // Delete all shortcode tags from the content. $excerpt = strip_shortcodes( $excerpt ); if( $html == true ){ // Sanitize the content $excerpt = apply_filters('the_content', $excerpt); // Replace the tags $excerpt = str_replace(']]>', ']]>', $excerpt); // Set allowed html tags $allowed_tags = ',,
,'; // Strip all tags but not the allowed tags $excerpt = strip_tags($excerpt, $allowed_tags); // Split in words, cut the content and re-implode all in a string $words_splitted = preg_split("/[\n\r\t ]+/", $excerpt, $words + 1, PREG_SPLIT_NO_EMPTY); if ( count($words_splitted) > $words ) { array_pop($words_splitted); $excerpt = implode(' ', $words_splitted); $excerpt = $excerpt . $more; } else { $excerpt = implode(' ', $words_splitted); } return $excerpt; }else{ return wp_trim_words( $excerpt, $words, $more ); } } // Transform date to time ago function un_date_to_time_ago($date){ $ptime = strtotime($date); $etime = time() - $ptime; if ($etime < 1) { return '0 seconds'; } $a = array( 365 * 24 * 60 * 60 => 'year', 30 * 24 * 60 * 60 => 'month', 24 * 60 * 60 => 'day', 60 * 60 => 'hour', 60 => 'minute', 1 => 'second' ); $a_plural = array( 'year' => 'years', 'month' => 'months', 'day' => 'days', 'hour' => 'hours', 'minute' => 'minutes', 'second' => 'seconds' ); foreach ($a as $secs => $str){ $d = $etime / $secs; if ($d >= 1){ $r = round($d); return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago'; } } } // Get the imge SRC url from html function un_get_src_by_img($image){ //preg_match_all("/ $value ){ foreach ( $repeater_array as $key => $data ){ if($key != 'redux_repeater_data') { $return[$index][$key] = $repeater_array[$key][$index]; } } } return $return; } // Redux BG data Builder function un_redux_bg_builder($bg_data){ // Final Style $style = ''; if( isset($bg_data['background-color']) && $bg_data['background-color'] ){ $style .= 'background-color: '.$bg_data['background-color'].'; '; } if( isset($bg_data['background-repeat']) && $bg_data['background-repeat'] ){ $style .= 'background-repeat: '.$bg_data['background-repeat'].'; '; } if( isset($bg_data['background-size']) && $bg_data['background-size'] ){ $style .= 'background-size: '.$bg_data['background-size'].'; '; } if( isset($bg_data['background-attachment']) && $bg_data['background-attachment'] ){ $style .= 'background-attachment: '.$bg_data['background-attachment'].'; '; } if( isset($bg_data['background-position']) && $bg_data['background-position'] ){ $style .= 'background-position: '.$bg_data['background-position'].'; '; } if( isset($bg_data['background-image']) && $bg_data['background-image'] ){ $style .= 'background-image: url('.$bg_data['background-image'].'); '; } return $style; } // Advanced Title function function un_get_the_title(){ $title = ''; // Page if( is_page() ){ $title = get_the_title(); } // Post if( is_single() ){ $title = get_the_title(); } // Archive if( is_archive() ){ $title = get_the_archive_title(); } // Category if( is_category() ){ $title = single_cat_title( '', false ); } // Tag if( is_tag() ){ $title = single_tag_title( '', false ); } // Custom Tax if( is_tax() ){ $title = single_term_title( '', false ); } // Search if( is_search() ){ $title = esc_html__('Results for: ', 'adena') . get_search_query(); } // 404 if( is_404() ){ $title = '404 Error'; } // Author if( is_author() ){ $title = get_the_author(); } // Attachment if( is_attachment() ){ $title = get_the_title(); } // Woocommerce if( un_is_woocommerce() ){ $title = woocommerce_page_title( false ); } // Home & Blog if ( is_front_page() && is_home() ) { $title = esc_html__('Homepage', 'adena'); } elseif ( is_front_page() ) { $title = esc_html__('Homepage', 'adena'); } elseif ( is_home() ) { $title = esc_html__('Blog', 'adena'); } return $title; } // Twitter Timeline function un_twitter_timeline($username, $limit, $consumer_key, $consumer_secret, $oauth_access_token=null, $oauth_access_token_secret=null) { // Set Data $key = $consumer_key; $secret = $consumer_secret; $api_endpoint = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=' . $username . '&count=' . $limit; // Create Request $encoding = 'base64'.'_encode'; $basic_credentials = $encoding($key.':'.$secret); $opts = array('http' => array( 'method' => 'POST', 'header' => 'Authorization: Basic '.$basic_credentials."\r\n". "Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n", 'content' => 'grant_type=client_credentials' ) ); $context = stream_context_create($opts); $f_get_contents = 'file_'.'get_contents'; if( $context ){ // Send Request $pre_token = $f_get_contents('https://api.twitter.com/oauth2/token', false, $context); }else{ return ''; } $token = json_decode($pre_token, true); if (isset($token["token_type"]) && $token["token_type"] == "bearer"){ $opts = array('http' => array( 'method' => 'GET', 'header' => 'Authorization: Bearer '.$token["access_token"] ) ); $context = stream_context_create($opts); $data = $f_get_contents($api_endpoint, false, $context); return json_decode($data); } return ''; } // Instagram Images function un_instagram_images($username, $access_token) { $f_get_contents = 'file_'.'get_contents'; // Security Check if( !$username || !$access_token ){ return; } // Get User ID $user_id = ''; $username = strtolower($username); // sanitization $url_user = "https://api.instagram.com/v1/users/search?q=".$username."&access_token=".$access_token; $result = $f_get_contents($url_user); $json = json_decode($result); foreach($json->data as $user){ if($user->username == $username){ $user_id = $user->id; } } $url_images = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token='.$access_token; $content = $f_get_contents($url_images); $images = json_decode($content, true); return $images['data']; } // Hex Color to RGB Color function un_hex2rgb($hex) { $hex = str_replace("#", "", $hex); if(strlen($hex) == 3) { $r = hexdec(substr($hex,0,1).substr($hex,0,1)); $g = hexdec(substr($hex,1,1).substr($hex,1,1)); $b = hexdec(substr($hex,2,1).substr($hex,2,1)); } else { $r = hexdec(substr($hex,0,2)); $g = hexdec(substr($hex,2,2)); $b = hexdec(substr($hex,4,2)); } $rgb = array($r, $g, $b); //return implode(",", $rgb); // returns the rgb values separated by commas return $rgb; // returns an array with the rgb values } // Is Hex color Dark? function un_is_hex_dark($hex){ $hex = str_replace($hex, '#', ''); $r = hexdec(substr($hex,0,2)); $g = hexdec(substr($hex,2,2)); $b = hexdec(substr($hex,4,2)); if($r + $g + $b > 382){ return false; }else{ return true; } }