better variable handling

better image in content check
This commit is contained in:
Robin Cornett
2014-11-06 15:56:48 -05:00
parent 618d2ec3e6
commit 16a6f9dca1
2 changed files with 31 additions and 22 deletions
@@ -24,31 +24,45 @@ class Display_Featured_Image_Genesis_Common {
$displaysetting = get_option( 'displayfeaturedimagegenesis' );
$move_excerpts = $displaysetting['move_excerpts'];
$postspage_image = get_post_thumbnail_id( $postspage );
if ( is_singular() ) {
$post_thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'displayfeaturedimage_backstretch' );
$image_id = get_post_thumbnail_id( $post->ID );
$post_thumbnail = wp_get_attachment_image_src( $image_id, 'original' );
}
// variables used outside this function
$item->fallback = esc_attr( $displaysetting['default'] );
$item->fallback_id = self::get_image_id( $item->fallback );
$item->fallback = esc_attr( $displaysetting['default'] ); // url only
$item->fallback_id = self::get_image_id( $item->fallback ); // gets image id with attached metadata
$item->large = absint( get_option( 'large_size_w' ) );
$item->medium = absint( get_option( 'medium_size_w' ) );
$item->reduce = absint( $displaysetting['less_header'] );
// Set Featured Image Source
$item->original = wp_get_attachment_image_src( $item->fallback_id, 'displayfeaturedimage_backstretch' );
$image_id = $item->fallback_id; // set here with fallback preemptively
if ( is_home() && 'page' === $frontpage && !empty( $postspage_image ) ) { // if on the blog page and it has a post_thumbnail
$item->original = wp_get_attachment_image_src( $postspage_image, 'displayfeaturedimage_backstretch' );
if ( is_home() && 'page' === $frontpage && ! empty( $postspage_image ) ) { // if on the blog page and it has a post_thumbnail
$image_id = get_post_thumbnail_id( $postspage );
}
// any singular post/page/CPT with either a post_thumbnail larger than medium size OR there is no $item->fallback
elseif ( is_singular() && ( $post_thumbnail[1] > $item->medium || empty( $item->fallback ) ) && !in_array( get_post_type(), self::use_fallback_image() ) ) {
$item->original = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'displayfeaturedimage_backstretch' );
elseif ( is_singular() && ( $post_thumbnail[1] > $item->medium || empty( $item->fallback ) ) /*&& ! in_array( get_post_type(), self::use_fallback_image() )*/ ) {
$image_id = get_post_thumbnail_id( $post->ID );
}
$item->backstretch = wp_get_attachment_image_src( $image_id, 'displayfeaturedimage_backstretch' );
// declare this last so that $item->backstretch is set.
if ( ! is_admin() ) {
$fullsize = wp_get_attachment_image_src( $image_id, 'original' );
$item->content = strpos( $post->post_content, 'src="' . $fullsize[0] );
// reset backstretch image source to fallback if it exists and the featured image is being used in content.
if ( ( ! empty( $item->fallback ) && false !== $item->content ) || in_array( get_post_type(), self::use_fallback_image() ) ) {
$item->backstretch = wp_get_attachment_image_src( $item->fallback_id, 'displayfeaturedimage_backstretch' );
$item->content = strpos( $post->post_content, 'src="' . $item->backstretch[0] );
}
}
// Set Post/Page Title
$item->title = $item->description = '';
// Set Post/Page Title
if ( is_singular() ) {
$item->title = get_the_title();
if ( has_excerpt() ) {
@@ -56,7 +70,7 @@ class Display_Featured_Image_Genesis_Common {
}
}
elseif ( is_home() && 'page' === $frontpage ) {
$item->title = get_post( $postspage )->post_title;
$item->title = get_post( $postspage )->post_title;
$item->description = get_post( $postspage )->post_excerpt;
}
elseif ( is_category() || is_tag() || is_tax() ) {
@@ -77,11 +91,6 @@ class Display_Featured_Image_Genesis_Common {
$item->description = genesis_get_cpt_option( 'intro_text' );
}
// declare this last so that $item->original is set.
if ( ! is_admin() ) {
$item->content = strpos( $post->post_content, $item->original[0] );
}
return $item;
}
@@ -36,16 +36,16 @@ class Display_Featured_Image_Genesis_Output {
public function load_scripts() {
$item = Display_Featured_Image_Genesis_Common::get_image_variables();
if ( ( ! empty( $item->original ) && false === $item->content ) || ( ! is_singular() && ! empty( $item->original ) ) ) {
if ( ( ! empty( $item->backstretch ) && false === $item->content ) || ( ! is_singular() && ! empty( $item->backstretch ) ) ) {
wp_enqueue_style( 'displayfeaturedimage-style', plugins_url( 'includes/css/display-featured-image-genesis.css', dirname( __FILE__ ) ), array(), 1.0 );
if ( $item->original[1] > $item->large ) {
if ( $item->backstretch[1] > $item->large ) {
wp_enqueue_script( 'displayfeaturedimage-backstretch', plugins_url( '/includes/js/backstretch.js', dirname( __FILE__ ) ), array( 'jquery' ), '1.0.0' );
wp_enqueue_script( 'displayfeaturedimage-backstretch-set', plugins_url( '/includes/js/backstretch-set.js', dirname( __FILE__ ) ), array( 'jquery', 'displayfeaturedimage-backstretch' ), '1.0.0' );
wp_localize_script( 'displayfeaturedimage-backstretch-set', 'BackStretchVars', array(
'src' => esc_url( $item->original[0] ),
'src' => esc_url( $item->backstretch[0] ),
'height' => esc_attr( $item->reduce )
) );
@@ -53,7 +53,7 @@ class Display_Featured_Image_Genesis_Output {
}
elseif ( ( $item->original[1] <= $item->large ) && ( $item->original[1] > $item->medium ) ) {
elseif ( ( $item->backstretch[1] <= $item->large ) && ( $item->backstretch[1] > $item->medium ) ) {
add_action( 'genesis_before_entry', array( $this, 'do_large_image' ) ); // HTML5
add_action( 'genesis_before_post', array( $this, 'do_large_image' ) ); // XHTML
}
@@ -71,11 +71,11 @@ class Display_Featured_Image_Genesis_Output {
$item = Display_Featured_Image_Genesis_Common::get_image_variables();
if ( ( ! empty( $item->original ) && false === $item->content ) || ( ! is_singular() && ! empty( $item->original ) ) ) {
if ( $item->original[1] > $item->large ) {
if ( ( ! empty( $item->backstretch ) && false === $item->content ) || ( ! is_singular() && ! empty( $item->backstretch ) ) ) {
if ( $item->backstretch[1] > $item->large ) {
$classes[] = 'has-leader';
}
elseif ( ( $item->original[1] <= $item->large ) && ( $item->original[1] > $item->medium ) ) {
elseif ( ( $item->backstretch[1] <= $item->large ) && ( $item->backstretch[1] > $item->medium ) ) {
$classes[] = 'large-featured';
}
}