2014-10-21 18:16:00 -04:00
< ? php
/**
* Common functions for plugin
*
* @package DisplayFeaturedImageGenesis
* @since 1.2.1
*/
class Display_Featured_Image_Genesis_Common {
2014-11-18 09:33:20 -05:00
/**
* current plugin version
* @var string
2014-11-18 10:54:48 -05:00
* @since 1.4.3
2014-11-18 09:33:20 -05:00
*/
2014-11-18 10:45:59 -05:00
public static $version = '1.4.3' ;
2014-11-18 09:33:20 -05:00
2014-10-24 10:01:43 -04:00
/**
* set and retreive variables for the featured image.
* @return $item
*
* @since 1.1.0
*/
2014-10-24 16:13:22 -04:00
public static function get_image_variables () {
2014-10-24 10:01:43 -04:00
$item = new stdClass ();
global $post ;
// variables internal to this function
2014-10-28 18:12:44 -04:00
$frontpage = get_option ( 'show_on_front' ); // either 'posts' or 'page'
$postspage = get_option ( 'page_for_posts' );
2014-11-04 21:14:33 -05:00
$displaysetting = get_option ( 'displayfeaturedimagegenesis' );
$move_excerpts = $displaysetting [ 'move_excerpts' ];
2014-10-28 18:12:44 -04:00
$postspage_image = get_post_thumbnail_id ( $postspage );
2014-11-06 15:56:48 -05:00
2014-11-18 19:48:16 -05:00
if ( is_singular () ) { // just checking for handling conditional variables set by width
2014-11-18 21:00:56 -05:00
$thumb_metadata = wp_get_attachment_metadata ( get_post_thumbnail_id ( $post -> ID ) ); // needed only for the next line
$width = '' ;
if ( $thumb_metadata ) {
$width = $thumb_metadata [ 'width' ];
}
2014-10-24 10:01:43 -04:00
}
2014-11-18 19:48:16 -05:00
// sitewide variables used outside this function
2014-11-06 15:56:48 -05:00
$item -> fallback = esc_attr ( $displaysetting [ 'default' ] ); // url only
$item -> fallback_id = self :: get_image_id ( $item -> fallback ); // gets image id with attached metadata
2014-11-01 20:06:54 -04:00
$item -> large = absint ( get_option ( 'large_size_w' ) );
$item -> medium = absint ( get_option ( 'medium_size_w' ) );
2014-11-04 21:14:33 -05:00
$item -> reduce = absint ( $displaysetting [ 'less_header' ] );
2014-10-24 10:01:43 -04:00
2014-11-17 13:51:44 -05:00
// Set Featured Image source ID
$image_id = '' ; // blank if nothing else
2014-11-02 19:20:45 -05:00
2014-11-17 13:51:44 -05:00
// set here with fallback preemptively, if it exists
if ( ! empty ( $item -> fallback ) ) {
$image_id = $item -> fallback_id ;
}
// if it's a home page with a static front page, and there is a featured image set on the home page
if ( is_home () && 'page' === $frontpage && ! empty ( $postspage_image ) ) {
2014-11-18 19:48:16 -05:00
$image_id = $postspage_image ;
2014-10-24 10:01:43 -04:00
}
// any singular post/page/CPT with either a post_thumbnail larger than medium size OR there is no $item->fallback
2014-11-18 19:48:16 -05:00
elseif ( is_singular () && ( $width > $item -> medium || empty ( $item -> fallback ) ) && ! in_array ( get_post_type (), self :: use_fallback_image () ) ) {
2014-11-06 15:56:48 -05:00
$image_id = get_post_thumbnail_id ( $post -> ID );
2014-10-24 10:01:43 -04:00
}
2014-11-17 13:51:44 -05:00
//now actually set the backstretch image source, which includes some metadata
2014-11-18 19:48:16 -05:00
$metadata = wp_get_attachment_metadata ( $image_id );
2014-11-06 15:56:48 -05:00
$item -> backstretch = wp_get_attachment_image_src ( $image_id , 'displayfeaturedimage_backstretch' );
2014-11-18 19:48:16 -05:00
$item -> width = '' ;
if ( ! empty ( $item -> backstretch ) ) {
$item -> width = $metadata [ 'width' ];
}
2014-10-24 10:01:43 -04:00
2014-11-06 22:02:48 -05:00
// set a content variable so backstretch doesn't show if full size image exists in post.
$item -> content = '' ;
2014-11-06 15:56:48 -05:00
// declare this last so that $item->backstretch is set.
2014-11-06 22:02:48 -05:00
if ( ! is_admin () && is_singular () ) {
2014-11-06 15:56:48 -05:00
$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.
2014-11-17 13:51:44 -05:00
if ( ! empty ( $item -> fallback ) && false !== $item -> content ) {
2014-11-06 15:56:48 -05:00
$item -> backstretch = wp_get_attachment_image_src ( $item -> fallback_id , 'displayfeaturedimage_backstretch' );
$item -> content = strpos ( $post -> post_content , 'src="' . $item -> backstretch [ 0 ] );
}
}
2014-11-01 20:17:28 -04:00
2014-10-24 10:01:43 -04:00
// Set Post/Page Title
2014-11-06 15:56:48 -05:00
$item -> title = $item -> description = '' ;
2014-11-01 20:06:54 -04:00
if ( is_singular () ) {
2014-10-24 10:01:43 -04:00
$item -> title = get_the_title ();
2014-10-28 18:12:44 -04:00
if ( has_excerpt () ) {
$item -> description = get_the_excerpt ();
}
2014-10-24 10:01:43 -04:00
}
2014-11-01 20:06:54 -04:00
elseif ( is_home () && 'page' === $frontpage ) {
2014-11-06 15:56:48 -05:00
$item -> title = get_post ( $postspage ) -> post_title ;
2014-11-01 20:06:54 -04:00
$item -> description = get_post ( $postspage ) -> post_excerpt ;
2014-10-24 10:01:43 -04:00
}
2014-10-28 18:12:44 -04:00
elseif ( is_category () || is_tag () || is_tax () ) {
$term = is_tax () ? get_term_by ( 'slug' , get_query_var ( 'term' ), get_query_var ( 'taxonomy' ) ) : get_queried_object ();
if ( ! $term || ! isset ( $term -> meta ) ) {
return ;
}
$item -> title = $term -> meta [ 'headline' ];
$item -> description = $term -> meta [ 'intro_text' ];
}
elseif ( is_author () ) {
$item -> title = get_the_author_meta ( 'headline' , ( int ) get_query_var ( 'author' ) );
$item -> description = get_the_author_meta ( 'intro_text' , ( int ) get_query_var ( 'author' ) );
}
2014-11-01 20:06:54 -04:00
elseif ( is_post_type_archive () && genesis_has_post_type_archive_support () && ! empty ( $item -> fallback ) ) {
2014-10-28 18:12:44 -04:00
$item -> title = genesis_get_cpt_option ( 'headline' );
$item -> description = genesis_get_cpt_option ( 'intro_text' );
}
2014-10-24 10:01:43 -04:00
return $item ;
}
2014-10-21 18:16:00 -04:00
/**
* Get the ID of each image dynamically.
*
* @since 1.2.0
*
* @author Philip Newcomer
* @link http://philipnewcomer.net/2012/11/get-the-attachment-id-from-an-image-url-in-wordpress/
*/
2014-10-27 10:36:46 -04:00
public static function get_image_id ( $attachment_url ) {
2014-10-21 18:16:00 -04:00
global $wpdb ;
$attachment_id = false ;
// Get the upload directory paths
$upload_dir_paths = wp_upload_dir ();
// Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image
if ( false !== strpos ( $attachment_url , $upload_dir_paths [ 'baseurl' ] ) ) {
// If this is the URL of an auto-generated thumbnail, get the URL of the original image
$attachment_url = preg_replace ( '(-\d{3,4}x\d{3,4}.)' , '.' , $attachment_url );
// Remove the upload path base directory from the attachment URL
$attachment_url = str_replace ( $upload_dir_paths [ 'baseurl' ] . '/' , '' , $attachment_url );
// Finally, run a custom database query to get the attachment ID from the modified attachment URL
$attachment_id = $wpdb -> get_var ( $wpdb -> prepare ( " SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment' " , $attachment_url ) );
}
return $attachment_id ;
}
2014-10-24 10:01:43 -04:00
/**
* skip certain post types
* @return filter creates a new filter for themes/plugins to use to skip certain post types
*
* @since 1.0.1
*/
2014-10-24 16:13:22 -04:00
public static function get_skipped_posttypes () {
2014-11-03 14:06:24 -05:00
2014-11-04 21:14:33 -05:00
$displaysetting = get_option ( 'displayfeaturedimagegenesis' );
$skip = $displaysetting [ 'exclude_front' ];
2014-11-03 14:06:24 -05:00
$post_types = array ();
$post_types [] = 'attachment' ;
$post_types [] = 'revision' ;
$post_types [] = 'nav_menu_item' ;
if ( $skip ) $post_types [] = is_front_page ();
return apply_filters ( 'display_featured_image_genesis_skipped_posttypes' , $post_types );
2014-10-24 10:01:43 -04:00
}
/**
* use fallback image as backstretch
* @return filter creates a new filter for themes/plugins to use to use the fallback image even if a large featured image is in place
*
* @since 1.2.0
*/
2014-10-24 16:13:22 -04:00
public static function use_fallback_image () {
2014-11-03 14:06:24 -05:00
$post_types = array ();
$post_types [] = 'attachment' ;
$post_types [] = 'revision' ;
$post_types [] = 'nav_menu_item' ;
return apply_filters ( 'display_featured_image_genesis_use_default' , $post_types );
2014-10-24 10:01:43 -04:00
}
2014-10-27 17:17:13 -04:00
/**
* don't show excerpts even if they exist.
* @return filter creates a new filter for themes/plugins to omit the excerpt on a post type even if an excerpt exists.
*
* @since 1.3.0
*/
public static function omit_excerpt () {
2014-11-03 14:06:24 -05:00
$post_types = array ();
$post_types [] = 'attachment' ;
$post_types [] = 'revision' ;
$post_types [] = 'nav_menu_item' ;
return apply_filters ( 'display_featured_image_genesis_omit_excerpt' , $post_types );
2014-10-27 17:17:13 -04:00
}
2014-10-21 18:16:00 -04:00
}