/** * Captures the HTML code with the featured media based on featured media linking method. * * @param array $args * @return string */ function g1_capture_entry_featured_media($args) { // Prepare arguments $defaults = array('size' => '', 'lightbox_group' => '', 'force_placeholder' => true); $r = wp_parse_args($args, $defaults); /* We need a static counter to generate unique ids for inline lightboxes */ static $inline_lightbox_counter = 0; global $post; $url = ''; $linking = ''; $holder = ''; $placeholder = ''; $out = ''; // Try to return a placeholder when an entry is password protected if (post_password_required($post)) { if (!$r['force_placeholder']) { return ''; } $out = sprintf('[placeholder icon="lock" size="%s"]', $r['size']); } else { $post_format = get_post_format($post->ID); switch ($post_format) { case 'audio': $audio = g1_capture_post_audio($post->ID, $r['size']); $audio = do_shortcode($audio); $js_id = 'g1_var_' . rand(); $js = '<script id="' . esc_attr($js_id) . '" class="g1-var">' . 'var ' . $js_id . ' = ' . json_encode(array('html_code' => $audio)) . ';' . '</script>'; // Thumbnail image triggers an inline lightbox with the audio player if (has_post_thumbnail($post->ID)) { $thumb = get_the_post_thumbnail($post->ID, $r['size']); // Compose the template $out = '<figure class="entry-featured-media">' . '[frame link="%LINK%"]' . '%THUMB%' . '[/frame]' . $js . '</figure>'; // Fill in the template $out = str_replace(array('%LINK%', '%THUMB%'), array('#', $thumb), $out); } else { if ($r['force_placeholder']) { $placeholder = '[placeholder size="' . $r['size'] . '"]'; } $out = '<figure class="entry-featured-media">' . $audio . $placeholder . $js . '</figure>'; } break; case 'image': // Thumbnail image triggers the full version if (has_post_thumbnail($post->ID)) { $thumb = get_the_post_thumbnail($post->ID, $r['size']); $image = get_post_meta($post->ID, '_format_image', true); // @todo get url from $image $url = $image; if (!strlen($url)) { $url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full'); $url = $url[0]; } // Compose the template $out = '<figure class="entry-featured-media">' . '[frame link="%LINK%"]' . '%THUMB%' . '[/frame]' . '</figure>'; // Fill in the template $out = str_replace(array('%LINK%', '%THUMB%'), array($url, $thumb), $out); } // WP 3.6 post format if (empty($out)) { $image = get_post_meta($post->ID, '_format_image', true); if (!empty($image)) { if (is_html_code($image)) { $out = '<figure class="entry-featured-media">' . $image . '</figure>'; } else { $out = '<figure class="entry-featured-media">' . '<img src="' . $image . '" alt="' . $post->post_title . '" />' . '</figure>'; } } } // post fromat < WP 3.6 if (empty($out)) { // first img tag $img_tag = G1_Post_Format::find_html_tag('img', $post->post_content); // first link tag $url = get_content_url($post->post_content); if (!empty($img_tag)) { $out = '<figure class="entry-featured-media">' . $img_tag . '</figure>'; } else { if (!empty($url)) { $out = '<figure class="entry-featured-media">' . '<img src="' . $url . '" alt="' . $post->post_title . '" />' . '</figure>'; } } } break; case 'gallery': // Thumbnail image triggers the full gallery if (has_post_thumbnail($post->ID)) { $thumb = get_the_post_thumbnail($post->ID, $r['size']); $url = apply_filters('the_permalink', get_permalink($post->ID)); $items = g1_get_post_format_gallery($post->ID, 'full'); // Compose gallery as a list of links $gallery_data = ''; foreach ($items as $item) { $gallery_data .= str_replace(array('%URL%', '%WIDTH%', '%HEIGHT%'), array(esc_url($item['url']), absint($item['width']), absint($item['height'])), '<li><a href="%URL%" data-g1-width="%WIDTH%" data-g1-height="%HEIGHT%"></a></li>'); } // Compose the template $out = '<figure class="entry-featured-media">' . '[frame link="%LINK%"]' . '%THUMB%' . '[/frame]' . '<ul class="g1-gallery-data">' . '%GALLERY_DATA%' . '</ul>' . '</figure>'; // Fill in the template $out = str_replace(array('%LINK%', '%THUMB%', '%GALLERY_DATA%'), array(esc_url($url), $thumb, $gallery_data), $out); } else { $items = g1_get_post_format_gallery($post->ID, $r['size']); $gallery = ''; foreach ($items as $item) { $gallery .= str_replace(array('%URL%', '%WIDTH%', '%HEIGHT%'), array(esc_url($item['url']), absint($item['width']), absint($item['height'])), '<li><img src="%URL%" width="%WIDTH%" height="%HEIGHT%" alt=""/></li>'); } // Compose gallery as a list of links $items = g1_get_post_format_gallery($post->ID, 'full'); $gallery_data = ''; foreach ($items as $item) { $gallery_data .= str_replace(array('%URL%', '%WIDTH%', '%HEIGHT%'), array(esc_url($item['url']), absint($item['width']), absint($item['height'])), '<li><a href="%URL%" data-g1-width="%WIDTH%" data-g1-height="%HEIGHT%"></a></li>'); } $out = '<figure class="entry-featured-media">' . '<ul class="g1-gallery-items">' . $gallery . '</ul>' . '<ul class="g1-gallery-data">' . $gallery_data . '</ul>' . '</figure>'; } break; case 'video': $big_video = g1_capture_post_video($post->ID, 'g1_max'); $big_video = do_shortcode($big_video); $js_id = 'g1_var_' . rand(); $js = '<script id="' . esc_attr($js_id) . '" class="g1-var">' . 'var ' . $js_id . ' = ' . json_encode(array('html_code' => $big_video)) . ';' . '</script>'; // Thumbnail image triggers the video if (has_post_thumbnail($post->ID)) { $thumb = get_the_post_thumbnail($post->ID, $r['size']); // Compose the template $out = '<figure class="entry-featured-media">' . "\n" . '[frame link="%LINK%"]' . '%THUMB%' . '[/frame]' . "\n" . $js . '</figure>'; $embed = get_post_meta($post->ID, '_format_video_embed', true); if (false === strpos($embed, 'http://') && false === strpos($embed, 'https://')) { $embed = the_permalink($post->ID); } // Fill in the template $out = str_replace(array('%LINK%', '%THUMB%'), array(esc_url($embed), $thumb), $out); } else { $small_video = g1_capture_post_video($post->ID, $r['size']); $small_video = do_shortcode($small_video); $out = '<figure class="entry-featured-media">' . $small_video . $js . '</figure>'; } break; case 'link': $linking = 'new-window'; // first link tag $url = get_content_url($post->post_content); default: if (!has_post_thumbnail($post->ID)) { if (!$r['force_placeholder']) { return ''; } $placeholder = sprintf('[placeholder size="%s" icon="post-format"]', $r['size']); } else { $holder = get_the_post_thumbnail($post->ID, $r['size']); } if (empty($url)) { $has_url = get_the_post_format_url($post->ID); $url = $has_url ? $has_url : apply_filters('the_permalink', get_permalink()); } // Compose the template $out = '<figure class="entry-featured-media">' . "\n" . '[frame link="%LINK%" linking="%LINKING%"]' . '%HOLDER%' . '%PLACEHOLDER%' . '[/frame]' . "\n" . '</figure>'; // Fill in the template $out = str_replace(array('%LINK%', '%LINKING%', '%HOLDER%', '%PLACEHOLDER%'), array($url, $linking, $holder, $placeholder), $out); break; } } // Apply shortcodes $out = do_shortcode($out); return $out; }
function g1_mediabox_lightbox($post_id, $size, $only_featured_media = false) { $items = array(); $post_format = get_post_format($post_id); switch ($post_format) { case 'audio': if ($size === 'full') { $size = 'g1_max'; } $audio = g1_capture_post_audio($post_id, $size); $audio = do_shortcode($audio); $script_id = 'g1_var_' . rand(); $html = '<script id="' . esc_attr($script_id) . '" class="g1-var">' . 'var ' . $script_id . ' = ' . json_encode(array('html_code' => $audio)) . ';' . '</script>'; if (!empty($audio)) { $items[] = array('class' => array(), 'html' => $html); } break; case 'video': if ($size === 'full') { $size = 'g1_max'; } $video = g1_capture_post_video($post_id, $size); $video = do_shortcode($video); if (!empty($video)) { $script_id = 'g1_var_' . rand(); $html = '<script id="' . esc_attr($script_id) . '" class="g1-var">' . 'var ' . $script_id . ' = ' . json_encode(array('html_code' => $video)) . ';' . '</script>'; $items[] = array('class' => array(), 'html' => $html); } break; default: break; } if ($only_featured_media && !in_array($post_format, array('audio', 'video')) && has_post_thumbnail($post_id)) { $html = get_the_post_thumbnail($post_id, $size); $items[] = array('class' => array(), 'html' => $html); } if ($only_featured_media) { return $items; } // Get all image|audio attachments not excluded from the media box $query_args = array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => array('image', 'audio'), 'orderby' => 'menu_order', 'order' => 'ASC', 'meta_query' => array('relation' => 'OR', array('key' => '_g1_type', 'value' => 'exclude', 'compare' => 'NOT EXISTS'), array('key' => '_g1_type', 'value' => 'exclude', 'compare' => '!='))); foreach (get_children($query_args) as $attachment_id => $attachment) { $title = esc_html($attachment->post_title); $caption = esc_html($attachment->post_excerpt); $description = esc_html($attachment->post_content); $mime_type = substr($attachment->post_mime_type, 0, strpos($attachment->post_mime_type, "/")); switch ($mime_type) { case 'image': $item = array('class' => array(), 'html' => ''); $src = wp_get_attachment_image_src($attachment_id, $size); $src = $src[0]; $item['html'] .= '<a href="' . esc_url($src) . '">' . '</a>'; $items[] = $item; break; case 'audio': $item = array('class' => array(), 'html' => ''); $audio = do_shortcode('[audio_player title="' . $caption . '" mp3="' . wp_get_attachment_url($attachment_id) . '"]') . ($js_id = 'g1_var_' . rand()); $item['html'] = '<script id="' . esc_attr($js_id) . '" class="g1-var">' . 'var ' . $js_id . ' = ' . json_encode(array('html_code' => $audio)) . ';' . '</script>'; $items[] = $item; break; } } return $items; }