2014-10-21 18:16:00 -04:00
< ? php
/**
* Common functions for plugin
2016-04-01 20:18:20 -04:00
* Class Display_Featured_Image_Genesis_Common
2014-10-21 18:16:00 -04:00
*
2016-04-01 20:18:20 -04:00
* @package DisplayFeaturedImageGenesis
* @author Robin Cornett <hello@robincornett.com>
* @license GPL-2.0+
2016-07-03 07:42:41 -04:00
* @link https://robincornett.com
2017-02-17 09:43:46 -05:00
* @copyright 2014-2017 Robin Cornett Creative, LLC
2014-10-21 18:16:00 -04:00
* @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
*/
2017-10-31 17:24:14 -04:00
public $version = '2.7.0' ;
2014-11-18 09:33:20 -05:00
2014-10-24 10:01:43 -04:00
/**
2015-12-30 15:53:19 -05:00
* set and retrieve variables for the featured image.
2016-04-26 09:41:24 -04:00
* @return \stdClass $item
2014-10-24 10:01:43 -04:00
*
* @since 1.1.0
*/
2015-06-06 21:30:42 -04:00
public static function get_image_variables () {
2014-11-19 09:43:44 -05:00
2014-10-24 10:01:43 -04:00
$item = new stdClass ();
2015-05-05 12:19:08 -04:00
$item -> backstretch = '' ;
2015-10-16 16:10:28 -04:00
add_filter ( 'jetpack_photon_override_image_downsize' , '__return_true' ); // turn Photon off so we can get the correct image
2015-05-05 12:19:08 -04:00
2017-12-27 15:17:30 -05:00
$image_id = self :: set_image_id ();
$image_size = self :: image_size ();
2015-05-05 12:19:08 -04:00
$item -> backstretch = wp_get_attachment_image_src ( $image_id , $image_size );
// set a content variable so backstretch doesn't show if full size image exists in post.
2016-04-20 15:14:21 -04:00
$item -> content = false ;
2015-05-05 12:19:08 -04:00
// declare this last so that $item->backstretch is set.
if ( ! is_admin () && is_singular () ) {
$post = get_post ();
2016-04-20 15:14:21 -04:00
$item -> content = strpos ( $post -> post_content , 'src="' . $item -> backstretch [ 0 ] );
2015-05-05 12:19:08 -04:00
}
2015-05-06 13:05:01 -04:00
// $title is set by new title function
2015-06-06 21:30:42 -04:00
$title = self :: set_item_title ();
2015-05-06 13:05:01 -04:00
/**
* Optional filter to change the title text
2015-05-07 20:39:19 -04:00
* @since 2.2.0
2015-05-06 13:05:01 -04:00
*/
2015-05-05 12:19:08 -04:00
$item -> title = apply_filters ( 'display_featured_image_genesis_title' , $title );
return $item ;
}
/**
* retrieve image ID for output
2016-04-26 09:41:24 -04:00
*
2015-05-05 12:19:08 -04:00
* @param string $image_id variable, ID of featured image
*
2015-05-07 20:39:19 -04:00
* @since 2.2.1
2016-04-26 09:41:24 -04:00
* @return int|string $image_id
2015-05-05 12:19:08 -04:00
*/
2015-07-27 16:11:01 -04:00
public static function set_image_id ( $image_id = '' ) {
2015-05-05 12:19:08 -04:00
2016-04-30 13:57:52 -04:00
$setting = displayfeaturedimagegenesis_get_setting ();
2016-03-31 11:35:12 -04:00
$fallback = $setting [ 'default' ];
$fallback_id = displayfeaturedimagegenesis_check_image_id ( $fallback );
2014-10-24 10:01:43 -04:00
2014-11-17 13:51:44 -05:00
// set here with fallback preemptively, if it exists
2015-02-26 15:38:01 -05:00
if ( ! empty ( $fallback ) ) {
2016-01-12 09:13:25 -05:00
/**
* Creates display_featured_image_genesis_use_default filter to check
* whether get_post_type array should use default image.
* @uses is_in_array()
*/
2015-05-05 12:19:08 -04:00
$image_id = $fallback_id ;
2016-07-02 14:02:54 -04:00
if ( self :: is_in_array ( 'use_default' ) || $setting [ 'always_default' ] ) {
2015-08-15 15:18:27 -04:00
return ( int ) $image_id ;
2015-05-05 12:19:08 -04:00
}
2014-11-17 13:51:44 -05:00
}
2015-02-15 20:57:24 -05:00
// outlier: if it's a home page with a static front page, and there is a featured image set on the home page
2015-06-08 15:22:35 -04:00
// also provisionally sets featured image for posts, similar to CPT archives
2015-12-16 14:05:26 -05:00
$frontpage = get_option ( 'show_on_front' ); // either 'posts' or 'page'
2017-10-25 12:33:45 -04:00
$postspage = get_option ( 'page_for_posts' );
if ( 'page' === $frontpage && $postspage ) {
2016-03-31 11:35:12 -04:00
$postspage_image = get_post_thumbnail_id ( $postspage );
$setting [ 'post_type' ][ 'post' ] = ( int ) $postspage_image ;
2014-10-24 10:01:43 -04:00
}
2015-01-03 09:10:38 -05:00
2015-12-14 17:21:04 -05:00
// if a post type image exists, it takes priority over the fallback. check that next.
$post_type = get_post_type ();
2017-10-25 12:33:45 -04:00
if ( isset ( $setting [ 'post_type' ][ $post_type ] ) && $setting [ 'post_type' ][ $post_type ] ) {
2016-01-12 09:13:25 -05:00
/**
* Creates display_featured_image_genesis_use_post_type_image filter to check
* whether get_post_type array should use the post type image.
* @uses is_in_array()
*/
2016-03-31 11:35:12 -04:00
$image_id = displayfeaturedimagegenesis_check_image_id ( $setting [ 'post_type' ][ $post_type ] );
2016-01-07 13:52:08 -05:00
if ( self :: is_in_array ( 'use_post_type_image' ) ) {
2015-12-14 17:21:04 -05:00
return ( int ) $image_id ;
2015-01-03 09:10:38 -05:00
}
}
2015-06-07 12:04:36 -04:00
if ( is_author () ) {
2015-07-26 16:44:11 -04:00
$image_id = get_the_author_meta ( 'displayfeaturedimagegenesis' , ( int ) get_query_var ( 'author' ) );
2015-06-07 12:04:36 -04:00
}
2014-12-31 08:31:23 -05:00
// taxonomy
2015-02-15 20:57:24 -05:00
if ( is_category () || is_tag () || is_tax () ) {
2015-12-15 07:13:45 -05:00
$object = get_queried_object ();
$image = displayfeaturedimagegenesis_get_term_image ( $object -> term_id );
if ( $image ) {
$image_id = $image ;
2015-11-17 16:50:44 -05:00
}
2015-05-05 12:19:08 -04:00
}
2015-01-05 11:04:44 -05:00
2016-03-11 12:34:23 -05:00
// search page
2016-07-12 09:49:05 -04:00
if ( is_search () && ! empty ( $setting [ 'post_type' ][ 'search' ] ) ) {
2016-03-31 11:35:12 -04:00
$image_id = $setting [ 'post_type' ][ 'search' ];
2016-03-11 12:34:23 -05:00
}
// 404
2016-07-12 09:49:05 -04:00
if ( is_404 () && ! empty ( $setting [ 'post_type' ][ 'fourohfour' ] ) ) {
2016-03-31 11:35:12 -04:00
$image_id = $setting [ 'post_type' ][ 'fourohfour' ];
2016-03-11 12:34:23 -05:00
}
2015-05-05 12:19:08 -04:00
// any singular post/page/CPT
2015-05-06 13:05:01 -04:00
if ( is_singular () ) {
2016-04-06 14:43:41 -04:00
$image_id = self :: get_singular_post_image ( $image_id , $setting , $post_type );
2014-12-31 08:31:23 -05:00
}
2014-12-30 17:12:21 -05:00
2015-06-08 15:22:17 -04:00
/**
* filter to use a different image id
* @var $image_id
*
* @since 2.2.0
*/
$image_id = apply_filters ( 'display_featured_image_genesis_image_id' , $image_id );
2015-04-09 22:15:41 -04:00
// make sure the image id is an integer
2015-06-06 18:01:07 -04:00
$image_id = is_numeric ( $image_id ) ? ( int ) $image_id : '' ;
2015-04-09 22:15:41 -04:00
2015-05-05 12:19:08 -04:00
return $image_id ;
2016-04-03 15:30:06 -04:00
}
/**
* Get the featured image for the singular post
2016-04-26 09:41:24 -04:00
* @param $image_id int|string
* @param $setting array
2016-04-03 15:30:06 -04:00
* @param $post_type
*
* @return string
*
* @since 2.5.0
*/
2016-04-06 14:43:41 -04:00
protected static function get_singular_post_image ( $image_id , $setting , $post_type ) {
2016-04-03 15:30:06 -04:00
$term_image = display_featured_image_genesis_get_term_image_id ();
if ( ! empty ( $term_image ) ) {
/**
* Creates display_featured_image_genesis_use_taxonomy filter to check
* whether get_post_type array should use the term image.
* @uses is_in_array()
*/
$image_id = $term_image ;
if ( self :: is_in_array ( 'use_taxonomy' ) ) {
return $image_id ;
}
}
2014-10-24 10:01:43 -04:00
2016-04-30 14:20:30 -04:00
// If this post type is set to always use a fallback image, stop here and use it.
2016-04-06 14:43:41 -04:00
if ( isset ( $setting [ 'fallback' ][ $post_type ] ) && $setting [ 'fallback' ][ $post_type ] ) {
2016-04-03 15:30:06 -04:00
return $image_id ;
}
$thumb_metadata = wp_get_attachment_metadata ( get_post_thumbnail_id ( get_the_ID () ) ); // needed only for the next line
$width = $thumb_metadata ? $thumb_metadata [ 'width' ] : '' ;
$medium = ( int ) apply_filters ( 'displayfeaturedimagegenesis_set_medium_width' , get_option ( 'medium_size_w' ) );
if ( has_post_thumbnail () && $width > $medium ) {
$image_id = get_post_thumbnail_id ( get_the_ID () );
2016-04-20 15:14:21 -04:00
$image_id = self :: singular_reset_image ( $image_id );
}
return $image_id ;
}
/**
* Check the post content for the featured image. If it's there (full size), reset the featured image to a fallback.
* @param $image_id
*
* @return string
* @since 2.5.0
*/
2017-12-27 15:17:30 -05:00
public static function singular_reset_image ( $image_id ) {
2016-04-20 15:14:21 -04:00
$fullsize = wp_get_attachment_image_src ( $image_id , 'full' );
$post = get_post ( get_the_ID () );
$item_content = strpos ( $post -> post_content , 'src="' . $fullsize [ 0 ] );
2016-04-20 15:30:16 -04:00
if ( false === $item_content ) {
return $image_id ;
}
$term_image = display_featured_image_genesis_get_term_image_id ();
$default_image = display_featured_image_genesis_get_default_image_id ();
// reset backstretch image source to term image if it exists and the featured image is being used in content.
if ( $term_image ) {
$image_id = $term_image ;
} elseif ( $default_image ) {
// else, reset backstretch image source to fallback.
$image_id = $default_image ;
2016-04-03 15:30:06 -04:00
}
2016-04-20 15:14:21 -04:00
2016-04-03 15:30:06 -04:00
return $image_id ;
2014-10-24 10:01:43 -04:00
}
2016-01-28 10:59:34 -05:00
/**
* @param string $title
*
2017-12-27 15:17:30 -05:00
* @return string
2016-01-28 10:59:34 -05:00
*/
2015-06-06 21:30:42 -04:00
protected static function set_item_title ( $title = '' ) {
2015-05-05 12:50:02 -04:00
$frontpage = get_option ( 'show_on_front' ); // either 'posts' or 'page'
$postspage = get_option ( 'page_for_posts' );
2015-05-06 13:18:42 -04:00
$a11ycheck = current_theme_supports ( 'genesis-accessibility' , array ( 'headings' ) );
2015-05-05 12:50:02 -04:00
if ( is_singular () ) {
$title = get_the_title ();
2015-06-06 17:57:28 -04:00
} elseif ( is_home () && 'page' === $frontpage ) {
2015-05-05 12:50:02 -04:00
$title = get_post ( $postspage ) -> post_title ;
2015-06-06 17:57:28 -04:00
} elseif ( is_category () || is_tag () || is_tax () ) {
2015-05-05 12:50:02 -04:00
$term = is_tax () ? get_term_by ( 'slug' , get_query_var ( 'term' ), get_query_var ( 'taxonomy' ) ) : get_queried_object ();
2016-03-02 21:44:01 -05:00
if ( ! $term ) {
2017-12-27 15:17:30 -05:00
return '' ;
2015-05-05 12:50:02 -04:00
}
2016-03-02 21:44:01 -05:00
$title = displayfeaturedimagegenesis_get_term_meta ( $term , 'headline' );
2015-05-06 13:18:42 -04:00
if ( empty ( $title ) && $a11ycheck ) {
2015-05-06 13:05:01 -04:00
$title = $term -> name ;
}
2015-06-06 17:57:28 -04:00
} elseif ( is_author () ) {
2015-05-05 12:50:02 -04:00
$title = get_the_author_meta ( 'headline' , ( int ) get_query_var ( 'author' ) );
2015-05-06 13:18:42 -04:00
if ( empty ( $title ) && $a11ycheck ) {
2015-05-06 13:05:01 -04:00
$title = get_the_author_meta ( 'display_name' , ( int ) get_query_var ( 'author' ) );
}
2015-06-06 17:57:28 -04:00
} elseif ( is_post_type_archive () && genesis_has_post_type_archive_support () ) {
2015-05-05 12:50:02 -04:00
$title = genesis_get_cpt_option ( 'headline' );
2015-05-06 13:18:42 -04:00
if ( empty ( $title ) && $a11ycheck ) {
2015-05-06 13:05:01 -04:00
$title = post_type_archive_title ( '' , false );
}
2015-05-05 12:50:02 -04:00
}
2016-01-28 10:59:34 -05:00
return apply_filters ( 'display_featured_image_genesis_title_text' , $title );
2015-05-05 12:50:02 -04:00
}
2014-10-21 18:16:00 -04:00
/**
* Get the ID of each image dynamically.
*
2016-04-26 09:41:24 -04:00
* @since 1.2.0
2014-10-21 18:16:00 -04:00
*
* @author Philip Newcomer
* @link http://philipnewcomer.net/2012/11/get-the-attachment-id-from-an-image-url-in-wordpress/
2016-04-26 09:41:24 -04:00
*
* @param string $attachment_url
*
* @return bool|int|mixed|string
2014-10-21 18:16:00 -04:00
*/
2015-06-06 21:30:42 -04:00
public static function get_image_id ( $attachment_url = '' ) {
2015-04-29 15:02:35 -04:00
2014-10-21 18:16:00 -04:00
$attachment_id = false ;
2015-12-30 16:04:17 -05:00
// as of 2.2.0, if a (new) image id is passed to the function, or if it's empty, return it as is.
if ( is_numeric ( $attachment_url ) || '' === $attachment_url ) {
2015-04-10 21:28:23 -04:00
return $attachment_url ;
}
2017-12-14 10:59:58 -05:00
_deprecated_function ( __FUNCTION__ , '2.7.0' );
2016-03-31 11:37:45 -04:00
// if we're running 4.0 or later, we can do this all using a new core function.
if ( function_exists ( 'attachment_url_to_postid' ) ) {
2016-07-07 17:40:45 -04:00
$url_stripped = preg_replace ( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i' , '' , $attachment_url );
$attachment_id = attachment_url_to_postid ( $url_stripped );
return $attachment_id > 0 ? $attachment_id : false ;
2016-03-31 11:37:45 -04:00
}
2015-04-10 21:28:23 -04:00
2014-10-21 18:16:00 -04:00
// Get the upload directory paths
$upload_dir_paths = wp_upload_dir ();
2015-10-20 12:30:19 -04:00
$base_url = wp_make_link_relative ( $upload_dir_paths [ 'baseurl' ] );
$attachment_url = wp_make_link_relative ( $attachment_url );
2014-10-21 18:16:00 -04:00
// Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image
2015-10-20 12:30:19 -04:00
if ( false !== strpos ( $attachment_url , $base_url ) ) {
2014-10-21 18:16:00 -04:00
// Remove the upload path base directory from the attachment URL
2015-10-20 12:30:19 -04:00
$attachment_url = str_replace ( $base_url . '/' , '' , $attachment_url );
2015-04-29 15:02:35 -04:00
// If this is the URL of an auto-generated thumbnail, get the URL of the original image
$url_stripped = preg_replace ( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i' , '' , $attachment_url );
2014-10-21 18:16:00 -04:00
// Finally, run a custom database query to get the attachment ID from the modified attachment URL
2015-06-12 08:30:47 -04:00
$attachment_id = self :: fetch_image_id_query ( $url_stripped , $attachment_url );
2014-10-21 18:16:00 -04:00
}
return $attachment_id ;
}
2014-10-24 10:01:43 -04:00
2015-04-29 15:02:35 -04:00
/**
* Fetch image ID from database
2016-04-26 09:41:24 -04:00
* @param $url_stripped string url without WP resize (eg 150x150)
* @param $attachment_url string url
2015-04-29 15:02:35 -04:00
* @return int (image id) image ID, or false
*
* @since 2.2.0
*
* @author hellofromtonya
*/
2015-06-12 08:30:47 -04:00
protected static function fetch_image_id_query ( $url_stripped , $attachment_url ) {
2015-04-29 15:02:35 -04:00
global $wpdb ;
$query_sql = $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 IN ( %s, %s ) AND wposts.post_type = 'attachment'
" ,
$url_stripped , $attachment_url
);
$result = $wpdb -> get_col ( $query_sql );
return empty ( $result ) || ! is_numeric ( $result [ 0 ] ) ? false : intval ( $result [ 0 ] );
}
2016-01-04 10:05:23 -05:00
/**
* Set up filter to check for post type rules. Variable, based on $value passed in.
* @param $value string for filter name
* @param array $post_types affected post types (empty array by default)
* @return bool
*
2016-03-11 12:34:23 -05:00
* @since 2.5.0
2016-01-04 10:05:23 -05:00
*/
2016-01-07 13:52:08 -05:00
public static function is_in_array ( $value , $post_types = array () ) {
2016-05-11 08:27:49 -04:00
$post_types = apply_filters ( " display_featured_image_genesis_ { $value } " , $post_types );
return in_array ( get_post_type (), $post_types );
2016-01-04 10:05:23 -05:00
}
2015-03-22 16:34:25 -04:00
/**
* add a filter to change the minimum width required for backstretch image
* @return integer sets the minimum width for backstretch effect
*
* @since 2.2.0
*/
2015-06-03 14:36:16 -04:00
public function minimum_backstretch_width () {
2015-03-22 16:34:25 -04:00
$large = apply_filters ( 'display_featured_image_genesis_set_minimum_backstretch_width' , get_option ( 'large_size_w' ) );
if ( ! is_numeric ( $large ) ) {
$large = get_option ( 'large_size_w' );
}
return absint ( intval ( $large ) );
}
2016-02-04 14:38:05 -05:00
/**
* Select which image size to use. Can be filtered to use a custom size.
2017-11-01 08:08:53 -04:00
* @return string
2016-03-02 22:18:33 -05:00
* @since 2.5.0
2016-02-04 14:38:05 -05:00
*/
2016-05-19 16:31:30 -04:00
public static function image_size () {
2017-11-21 09:40:36 -05:00
$setting = displayfeaturedimagegenesis_get_setting ();
$image_size = $setting [ 'image_size' ];
2017-11-21 11:21:13 -05:00
$post_meta = get_post_meta ( get_the_ID (), '_displayfeaturedimagegenesis_disable' , true );
2017-12-14 10:59:41 -05:00
if ( $post_meta && ! is_numeric ( $post_meta ) ) {
2017-11-21 11:21:13 -05:00
return $post_meta ;
2017-11-13 11:09:20 -05:00
}
2016-02-04 14:38:05 -05:00
/**
* Creates display_featured_image_genesis_use_large_image filter to check
* whether get_post_type array should use large image instead of backstretch.
* @uses is_in_array()
*/
2017-11-18 15:30:51 -05:00
if ( self :: is_in_array ( 'use_large_image' ) || self :: use_large_image_singular () ) {
2016-04-06 14:47:26 -04:00
return 'large' ;
2016-02-04 14:38:05 -05:00
}
2017-11-01 08:08:53 -04:00
return apply_filters ( 'displayfeaturedimagegenesis_image_size' , $image_size );
2016-02-04 14:38:05 -05:00
}
2017-11-13 11:09:20 -05:00
2017-11-18 15:30:51 -05:00
/**
* Determine if a singular post/page should use a large image instead of backstretch.
* @return bool
*/
protected static function use_large_image_singular () {
2017-11-13 11:09:20 -05:00
if ( ! is_singular () ) {
return false ;
}
$setting = displayfeaturedimagegenesis_get_setting ();
$post_type = get_post_type ();
$post_meta = ( int ) get_post_meta ( get_the_ID (), '_displayfeaturedimagegenesis_disable' , true );
return ( isset ( $setting [ 'large' ][ $post_type ] ) && $setting [ 'large' ][ $post_type ] ) || 3 === $post_meta ;
}
2014-10-21 18:16:00 -04:00
}