8000 Allow the hero to be filtered by wjhdev · Pull Request #724 · WPBuddy/largo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Allow the hero to be filtered #724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
272 changes: 256 additions & 16 deletions inc/featured-media.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php

if (!is_admin())
return;

/**
* Returns the default available featured media types
*/
function largo_default_featured_media_types() {

$media_types = apply_filters('largo_default_featured_media_types', array(
'embed' => array(
'title' => 'Featured embed code',
Expand All @@ -25,49 +23,291 @@ function largo_default_featured_media_types() {
'id' => 'gallery'
)
));

return array_values($media_types);

}

/**
* Prints DOM for hero image.
*
* Determines the type of featured media attached to a post,
* and generates the DOM for that type of media.
*
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @param String $classes Optional. Class string to apply to outer div.hero
*/
function largo_hero( $post = null, $classes = '' ) {

echo largo_get_hero( $post, $classes );

}

/**
* Return DOM for hero image.
*
* Determines the type of featured media attached to a post,
* and generates the DOM for that type of media.
*
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @param String $classes Optional. Class string to apply to outer div.hero
*/
function largo_get_hero($post = null,$classes = '') {

$post = get_post($post);
$hero_class = largo_hero_class($post->ID, false);

$ret = '';

if (largo_has_featured_media($post->ID) && $hero_class !== 'is-empty') {

$featured_media = largo_get_featured_media($post->ID);

if (in_array($featured_media['type'], array('embed-code', 'video'))) {

$ret = largo_get_featured_embed_hero($post->ID,$classes);

} else if ($featured_media['type'] == 'image') {

$ret = largo_get_featured_image_hero($post->ID,$classes);

} else if ($featured_media['type'] == 'gallery') {

$ret = largo_get_featured_gallery_hero($post->ID,$classes);

}

}

/**
* Filter the hero's DOM
*
* @since 0.5.1
*
* @param String $var DOM for hero.
* @param WP_Post $post post object.
*/
$ret = apply_filters('largo_get_hero',$ret,$post);

return $ret;

}

/**
* Template helpers
* Prints DOM for a featured image hero.
*
* @since 0.5.1
* @see largo_get_featured_image_hero()
*
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @param String $classes Optional. Class string to apply to outer div.hero
*/
function largo_featured_image_hero($post = null, $classes = '') {

echo largo_get_featured_image_hero($post,$classes);

}

/**
* Returns DOM for a featured image hero.
*
* @since 0.5.1
* @see largo_get_featured_image_hero()
*
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @param String $classes Optional. Class string to apply to outer div.hero
*/
function largo_get_featured_media($id) {
$ret = get_post_meta($id, 'featured_media', true);
function largo_get_featured_image_hero($post = null, $classes = '') {

$post = get_post($post);
$featured_media = largo_get_featured_media($post->ID);

if( $featured_media['type'] != 'image' ) {
return;
}

$hero_class = largo_hero_class($post->ID, false);
$classes = "hero $hero_class $classes";

$thumb_meta = null;
if ($thumb_id = get_post_thumbnail_id($post->ID)) {
$thumb_content = get_post($thumb_id);
$thumb_custom = get_post_custom($thumb_id);

$thumb_meta = array(
'caption' => (!empty($thumb_content->post_excerpt))? $thumb_content->post_excerpt : null,
'credit' => (!empty($thumb_custom['_media_credit'][0]))? $thumb_custom['_media_credit'][0] : null,
'organization' => (!empty($thumb_custom['_navis_media_credit_org'][0]))? $thumb_custom['_navis_media_credit_org'][0] : null
);
}

$context = array(
'classes' => $classes,
'thumb_meta' => $thumb_meta
);

ob_start();
largo_render_template('partials/hero', 'featured-image', $context);
$ret = ob_get_clean();

return $ret;
}


/**
* Prints DOM for an embed code hero.
*
* @since 0.5.1
* @see largo_get_featured_embed_hero()
*
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @param String $classes Optional. Class string to apply to outer div.hero
*/
function largo_featured_embed_hero($post = null, $classes = '') {

echo largo_get_featured_embed_hero($post,$classes);

}

/**
* Returns DOM for an embed code hero.
*
* @since 0.5.1
*
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @param String $classes Optional. Class string to apply to outer div.hero
*/
function largo_get_featured_embed_hero($post = null, $classes = '') {

$post = get_post($post);
$featured_media = largo_get_featured_media($post->ID);

if( !in_array($featured_media['type'], array('embed-code', 'video')) ) {
return;
}

$hero_class = largo_hero_class($post->ID, false);
$classes = "hero $hero_class $classes";

$context = array(
'classes' => $classes,
'featured_media' => $featured_media
);

ob_start();
largo_render_template('partials/hero', 'featured-embed', $context);
$ret = ob_get_clean();

return $ret;
}

/**
* Prints DOM for a featured gallery hero.
*
* @since 0.5.1
* @see largo_get_featured_gallery_hero()
*
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @param String $classes Optional. Class string to apply to outer div.hero
*/
function largo_featured_gallery_hero($post = null, $classes = '') {

echo largo_get_featured_gallery_hero($post, $classes);

}

/**
* Returns DOM for a featured gallery hero.
*
* @since 0.5.1
*
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @param String $classes Optional. Class string to apply to outer div.hero
*/
function largo_get_featured_gallery_hero( $post = null, $classes = '' ) {

$post = get_post($post);
$featured_media = largo_get_featured_media($post->ID);

if( $featured_media['type'] != 'gallery' ) {
return;
}

$hero_class = largo_hero_class($post->ID, false);
$classes = "hero $hero_class $classes";

$context = array(
'classes' => $classes,
'gallery_ids' => implode(',', $featured_media['gallery'])
);

ob_start();
largo_render_template('partials/hero', 'featured-gallery', $context);
$ret = ob_get_clean();

return $ret;

}

/**
* Returns information about the featured media.
*
* @since 0.4
*
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @return array $post_type {
*
* 'id' => int, // post id.
* 'type' => string, // the type of featured_media
*
* // ... other variables, dependent on what the type is.
*
* }
*/
function largo_get_featured_media( $post = null ) {

$post = get_post($post);

$ret = get_post_meta($post->ID, 'featured_media', true);

// Check if the post has a thumbnail/featured image set.
// If yes, send that back as the featured media.
$post_thumbnail = get_post_thumbnail_id($id);
$post_thumbnail = get_post_thumbnail_id($post->ID);
if (empty($ret) && !empty($post_thumbnail)) {
return array(
'id' => $id,
$ret = array(
'id' => $post->ID,
'attachment' => $post_thumbnail,
'type' => 'image'
);
}

// Backwards compatibility with posts that have a youtube_url set
$youtube_url = get_post_meta($id, 'youtube_url', true);
$youtube_url = get_post_meta($post->ID, 'youtube_url', true);
if (empty($ret) && !empty($youtube_url)) {
return array(
'id' => $id,
$ret = array(
'id' => $post->ID,
'url' => $youtube_url,
'embed' => wp_oembed_get($youtube_url),
'type' => 'video'
);
}

return get_post_meta($id, 'featured_media', true);
return $ret;
}

/**
* Helper function to tell if a post has featured media or not
* Does the post have featured media?
*
* @param string $id A post ID
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
* @return bool If a post ID has featured media or not.
*/
function largo_has_featured_media( $post = null ) {

$post = get_post($post);
$id = isset( $post->ID ) ? $post->ID : 0;

function largo_has_featured_media($id) {
$featured_media = largo_get_featured_media($id);

return !empty($featured_media);
}

Expand Down
20 changes: 5 additions & 15 deletions inc/post-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -674,28 +674,18 @@ function largo_hero_class( $post_id, $echo = TRUE ) {
}

/**
* Depricated 0.5.1.
*
* Returns the featured image for a post
* to be used as the hero image with caption and credit (if available)
*
* @since 0.4
*/
if ( ! function_exists( 'largo_hero_with_caption' ) ) {
function largo_hero_with_caption( $post_id ) {
echo get_the_post_thumbnail( $post_id, 'full' );
if ( $thumb = get_post_thumbnail_id( $post_id ) ) {
$thumb_content = get_post( $thumb );
$thumb_custom = get_post_custom( $thumb );
if ( isset($thumb_custom['_media_credit'][0]) ) {
echo '<p class="wp-media-credit">' . $thumb_custom['_media_credit'][0];
if ( $thumb_custom['_navis_media_credit_org'][0] ) {
echo '/' . $thumb_custom['_navis_media_credit_org'][0];
}
echo '</p>';
}
if ( $thumb_content->post_excerpt ) {
echo '<p class="wp-caption-text">' . $thumb_content->post_excerpt . '</p>';
}
}

largo_featured_image_hero($post_id);

}
}

Expand Down
5 changes: 3 additions & 2 deletions < F438 /span> inc/post-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,15 @@ function largo_remove_hero($content) {

$pattern = '/<img\s+[^>]*src="([^"]*)"[^>]*>/';
$hasImg = preg_match($pattern,$p[0],$matches);
$imgDom = $matches[0];
$src = $matches[1];

// 3: if there's no image, there's nothing to worry about.

if( !$hasImg )
return $content;

$imgDom = $matches[0];
$src = $matches[1];

// 4: Compare the src url to the feature image url.
// If they're the same, remove the top image.

Expand Down
2 changes: 1 addition & 1 deletion partials/content-single.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<?php
do_action('largo_after_post_header');

get_template_part( 'partials/single', 'hero' );
largo_hero(null,'span12');

do_action('largo_after_hero');
?>
Expand Down
14 changes: 14 additions & 0 deletions partials/hero-featured-embed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div class="<?php echo $classes; ?>">
<div class="embed-container">
<? echo $featured_media['embed']; ?>
</div>
<div class="embed-details wp-caption">
<?php if (!empty($featured_media['credit'])) { ?>
<p class='wp-media-credit featured-credit'><?php echo $featured_media['credit']; ?></p>
<?php }

if (!empty($featured_media['caption'])) { ?>
<p class='wp-caption-text featured-caption'><?php echo $featured_media['caption']; ?></p>
<?php } ?>
</div>
</div>
Loading
0