function hocwp_post_thumbnail($args = array()) { $post_id = isset($args['post_id']) ? $args['post_id'] : ''; if (empty($post_id)) { $post_id = get_the_ID(); } if (post_password_required($post_id) || is_attachment()) { return; } $args['post_id'] = $post_id; $transient_name = hocwp_build_transient_name('hocwp_cache_post_thumbnail_%s', $args); if (false === ($html = get_transient($transient_name))) { $cache = hocwp_get_value_by_key($args, 'cache', HOUR_IN_SECONDS); $thumbnail_url = hocwp_get_value_by_key($args, 'thumbnail_url'); if (empty($thumbnail_url)) { $large_size = hocwp_get_value_by_key($args, 'large_size'); if ($large_size) { $thumbnail_url = get_post_meta($post_id, 'large_thumbnail', true); $thumbnail_url = hocwp_sanitize_media_value($thumbnail_url); $thumbnail_url = $thumbnail_url['url']; if (empty($thumbnail_url)) { $thumbnail_url = hocwp_get_post_thumbnail_url($post_id); } } else { $thumbnail_url = hocwp_get_post_thumbnail_url($post_id); } } if (empty($thumbnail_url)) { return; } $bfi_thumb = isset($args['bfi_thumb']) ? $args['bfi_thumb'] : true; $bfi_thumb = apply_filters('hocwp_use_bfi_thumb', $bfi_thumb, $post_id); $size = hocwp_sanitize_size($args); $width = $size[0]; $height = $size[1]; $enlarge = apply_filters('hocwp_enlarge_post_thumbnail_on_mobile', false); if ($enlarge && wp_is_mobile()) { $ratio = 600 / $width; $ratio = round($ratio); if ($ratio > 1) { $width *= $ratio; $height *= $ratio; } } $original = $thumbnail_url; if ($bfi_thumb) { $params = isset($args['params']) ? $args['params'] : array(); if (is_numeric($width) && $width > 0) { $params['width'] = $width; } if (is_numeric($height) && $height > 0) { $params['height'] = $height; } $bfi_url = apply_filters('hocwp_pre_bfi_thumb', '', $thumbnail_url, $params); if (empty($bfi_url)) { if ($width > 0 || $height > 0) { $bfi_url = bfi_thumb($thumbnail_url, $params); } } if (!empty($bfi_url)) { $thumbnail_url = $bfi_url; } } $img = new HOCWP_HTML('img'); if (is_numeric($width) && $width > 0) { $img->set_attribute('width', $size[0]); } if (is_numeric($height) && $height > 0) { $img->set_attribute('height', $size[1]); } $img->set_attribute('data-original', $original); $lazyload = hocwp_get_value_by_key($args, 'lazyload', false); $img->set_attribute('alt', get_the_title($post_id)); $img->set_class('attachment-post-thumbnail wp-post-image img-responsive'); $img->set_attribute('src', $thumbnail_url); $centered = (bool) hocwp_get_value_by_key($args, 'centered', false); if ($centered) { $img->add_class('centered'); } $bk_img = ''; if ((bool) $lazyload) { $img->set_wrap_tag('noscript'); $bk_img = $img->build(); $img->set_wrap_tag(''); $loading_icon = hocwp_get_value_by_key($args, 'loading_icon'); if (!hocwp_is_image($loading_icon)) { $loading_icon = hocwp_get_image_url('transparent.gif'); } $img->set_image_src($loading_icon); $img->set_attribute('data-original', $thumbnail_url); $img->add_class('lazyload'); } $only_image = hocwp_get_value_by_key($args, 'only_image'); if ((bool) $only_image) { $html = $img->build(); if ((bool) $lazyload) { $html = $bk_img; } } else { $before = hocwp_get_value_by_key($args, 'before'); $after = hocwp_get_value_by_key($args, 'after'); $permalink = hocwp_get_value_by_key($args, 'permalink', get_permalink($post_id)); $loop = isset($args['loop']) ? $args['loop'] : true; $custom_html = isset($args['custom_html']) ? $args['custom_html'] : ''; $icon_video = hocwp_get_value_by_key($args, 'icon_video'); if (true === $icon_video) { $icon_video = '<i class="fa fa-play-circle-o" aria-hidden="true"></i>'; } $fancybox = (bool) hocwp_get_value_by_key($args, 'fancybox'); $a = new HOCWP_HTML('a'); $a->set_href($permalink); if (!empty($icon_video) && is_string($icon_video) && empty($custom_html)) { $a->set_text($icon_video); $custom_html = $a->build(); } $icon_image = hocwp_get_value_by_key($args, 'icon_image'); if (true === $icon_image) { $icon_image = '<i class="fa fa-camera" aria-hidden="true"></i>'; } if (!empty($icon_image) && is_string($icon_image) && empty($custom_html)) { $a->set_text($icon_image); $custom_html = $a->build(); } $cover = hocwp_get_value_by_key($args, 'cover'); $schema = ''; if (current_theme_supports('hocwp-schema')) { ob_start(); ?> <meta itemprop="url" content="<?php echo $thumbnail_url; ?> "> <meta itemprop="width" content="<?php echo $width; ?> "> <meta itemprop="height" content="<?php echo $height; ?> "> <?php $schema = ob_get_clean(); } $html = $before; if (is_singular() && !$loop) { ob_start(); ?> <div class="post-thumbnail entry-thumb"<?php hocwp_html_tag_attributes('div', 'entry_thumb'); ?> > <?php $img->output(); if ((bool) $lazyload) { echo $bk_img; } echo $custom_html; echo $schema; ?> </div> <?php $html = ob_get_clean(); } else { if (!empty($custom_html)) { $html .= '<div class="thumbnail-wrap">'; } $class = 'post-thumbnail-loop entry-thumb post-thumbnail'; $atts = ''; if ($fancybox) { hocwp_add_string_with_space_before($class, 'fancybox'); $atts = ' data-fancybox-group="gallery"'; $atts .= ' rel="gallery"'; $permalink = $original; } ob_start(); ?> <a class="<?php echo $class; ?> "<?php echo $atts; ?> href="<?php echo $permalink; ?> " aria-hidden="true"<?php hocwp_html_tag_attributes('a', 'entry_thumb'); ?> > <?php $img->output(); if ((bool) $lazyload) { echo $bk_img; } if ($cover) { echo '<span class="cover"></span>'; } echo $schema; ?> </a> <?php $html .= ob_get_clean(); $html .= $custom_html; if (!empty($custom_html)) { $html .= '</div>'; } } $html .= $after; } if (!empty($html)) { set_transient($transient_name, $html, $cache); } } echo $html; }
function hocwp_get_all_image_from_string($data, $output = 'img') { $output = trim($output); preg_match_all('/<img[^>]+>/i', $data, $matches); $matches = isset($matches[0]) ? $matches[0] : array(); if (!hocwp_array_has_value($matches) && !empty($data)) { if (false !== strpos($data, '//') && (false !== strpos($data, '.jpg') || false !== strpos($data, '.png') || false !== strpos($data, '.gif'))) { $sources = explode(PHP_EOL, $data); if (hocwp_array_has_value($sources)) { foreach ($sources as $src) { if (hocwp_is_image($src)) { if ('img' == $output) { $matches[] = '<img src="' . $src . '" alt="">'; } else { $matches[] = $src; } } } } } } elseif ('img' != $output && hocwp_array_has_value($matches)) { $tmp = array(); foreach ($matches as $img) { $src = hocwp_get_first_image_source($img); $tmp[] = $src; } $matches = $tmp; } return $matches; }
function hocwp_sanitize_media_value($value) { $url = isset($value['url']) ? $value['url'] : ''; $has_url = false; if (!empty($url)) { $has_url = true; } $id = isset($value['id']) ? $value['id'] : ''; $id = absint($id); if (0 < $id && hocwp_media_file_exists($id)) { $url = hocwp_get_media_image_url($id); } if (0 >= $id && !is_array($value) && !empty($value)) { $url = $value; } if ($has_url && empty($url)) { $url = wp_get_attachment_url($id); } $icon = wp_mime_type_icon($id); $size = hocwp_get_media_size($id); $result = array('id' => $id, 'url' => $url, 'type_icon' => $icon, 'is_image' => hocwp_is_image($url, $id), 'size' => $size, 'size_format' => hocwp_size_converter($size), 'mime_type' => get_post_mime_type($id)); return $result; }
function hocwp_post_gallery($args = array()) { $galleries = hocwp_get_value_by_key($args, 'galleries'); $id = hocwp_get_value_by_key($args, 'id'); $class = hocwp_get_value_by_key($args, 'class'); hocwp_add_string_with_space_before($class, 'post-gallery module'); if (!empty($id)) { $id = ' id="' . $id . '"'; } $title = hocwp_get_value_by_key($args, 'title'); if (!hocwp_array_has_value($galleries)) { if (is_string($args)) { $galleries = hocwp_get_all_image_from_string($args); } elseif (is_string($galleries) && !empty($galleries)) { $galleries = hocwp_get_all_image_from_string($galleries); } else { $galleries = $args; } } if (hocwp_array_has_value($galleries)) { ?> <div<?php echo $id; ?> class="<?php echo $class; ?> "> <?php if (!empty($title)) { ?> <div class="module-header"> <h4><?php echo $title; ?> </h4> </div> <?php } ?> <div class="module-body"> <div class="galleries"> <ul class="gallery hocwp-gallery list-unstyled cS-hidden clearfix row"> <?php $column = hocwp_get_value_by_key($args, 'column'); if (!hocwp_is_positive_number($column)) { $column = 2; } $pager = ''; $count = 0; foreach ($galleries as $img) { $src = hocwp_get_first_image_source($img); if (!hocwp_is_image($src)) { continue; } $li = new HOCWP_HTML('li'); $li->set_text($img); $li->set_attribute('data-thumb', $src); $li->add_class('col-xs-' . $column); $li->output(); } ?> </ul> </div> </div> </div> <?php } }