Files
display-featured-image-genesis/includes/class-displayfeaturedimagegenesis-common.php
T

347 lines
11 KiB
PHP
Raw Normal View History

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 {
/**
* current plugin version
* @var string
2014-11-18 10:54:48 -05:00
* @since 1.4.3
*/
2015-06-03 14:36:16 -04:00
public $version = '2.2.2';
2014-10-24 10:01:43 -04:00
/**
* set and retreive variables for the featured image.
* @return $item
*
* @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();
// 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 );
2015-03-23 11:51:45 -04:00
$fallback = $displaysetting['default']; // url only
2015-05-05 12:19:08 -04:00
// sitewide variables used outside this function
$item->backstretch = '';
// turn Photon off so we can get the correct image
$photon_removed = '';
if ( class_exists( 'Jetpack' ) && Jetpack::is_module_active( 'photon' ) ) {
$photon_removed = remove_filter( 'image_downsize', array( Jetpack_Photon::instance(), 'filter_image_downsize' ) );
}
/**
* create a filter for user to optionally force post types to use the large image instead of backstretch
* @var filter
*
* @since 2.0.0
*/
2015-06-06 17:57:28 -04:00
$use_large_image = apply_filters( 'display_featured_image_genesis_use_large_image', array() );
2015-05-05 12:19:08 -04:00
$image_size = 'displayfeaturedimage_backstretch';
if ( in_array( get_post_type(), $use_large_image ) ) {
$image_size = 'large';
}
2015-06-06 18:01:07 -04:00
/**
* filter to use a different image id
* @var $image_id
*
* @since 2.2.0
*/
2015-06-06 21:30:42 -04:00
$image_id = apply_filters( 'display_featured_image_genesis_image_id', self::set_image_id() );
2015-05-06 13:05:01 -04:00
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.
$item->content = '';
// declare this last so that $item->backstretch is set.
if ( ! is_admin() && is_singular() ) {
$fullsize = wp_get_attachment_image_src( $image_id, 'original' );
$post = get_post();
$item->content = strpos( $post->post_content, 'src="' . $fullsize[0] );
if ( false !== $item->content ) {
2015-06-06 17:57:28 -04:00
$source_id = '';
2015-05-05 12:19:08 -04:00
$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 ( ! empty( $term_image ) ) {
$source_id = $term_image;
2015-06-06 17:57:28 -04:00
} elseif ( ! empty( $fallback ) ) {
// else, reset backstretch image source to fallback.
2015-05-05 12:19:08 -04:00
$source_id = $default_image;
}
$item->backstretch = wp_get_attachment_image_src( $source_id, $image_size );
$item->content = strpos( $post->post_content, 'src="' . $item->backstretch[0] );
}
}
// turn Photon back on
if ( $photon_removed ) {
add_filter( 'image_downsize', array( Jetpack_Photon::instance(), 'filter_image_downsize' ), 10, 3 );
}
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
* @param string $image_id variable, ID of featured image
*
2015-05-07 20:39:19 -04:00
* @since 2.2.1
2015-05-05 12:19:08 -04:00
*/
2015-06-06 21:30:42 -04:00
protected static function set_image_id( $image_id = '' ) {
2015-05-05 12:19:08 -04:00
$frontpage = get_option( 'show_on_front' ); // either 'posts' or 'page'
$postspage = get_option( 'page_for_posts' );
$displaysetting = get_option( 'displayfeaturedimagegenesis' );
$postspage_image = get_post_thumbnail_id( $postspage );
$fallback = $displaysetting['default'];
2015-02-27 09:10:06 -05:00
$medium = absint( get_option( 'medium_size_w' ) );
2014-11-06 15:56:48 -05:00
if ( is_singular() ) { // just checking for handling conditional variables set by width
2014-12-31 11:00:15 -05:00
$thumb_metadata = wp_get_attachment_metadata( get_post_thumbnail_id( get_the_ID() ) ); // needed only for the next line
2014-11-18 21:00:56 -05:00
$width = '';
if ( $thumb_metadata ) {
$width = $thumb_metadata['width'];
}
2014-10-24 10:01:43 -04:00
}
2015-03-23 11:51:45 -04:00
$fallback_id = $fallback;
2015-03-23 12:16:57 -04:00
if ( ! is_numeric( $fallback ) ) {
2015-06-06 21:30:42 -04:00
$fallback_id = self::get_image_id( $fallback ); // gets image id with attached metadata
2015-03-23 11:51:45 -04:00
}
2015-05-05 12:19:08 -04:00
$fallback_id = absint( $fallback_id );
2014-10-24 10:01:43 -04:00
2015-01-05 11:04:44 -05:00
/**
* create a filter to use the fallback image
* @var filter
2015-01-10 10:45:16 -05:00
* @since 2.0.0 (deprecated old use_fallback_image function from 1.2.2)
2015-01-05 11:04:44 -05:00
*/
2015-06-06 17:57:28 -04:00
$use_fallback = apply_filters( 'display_featured_image_genesis_use_default', array() );
2015-01-05 11:04:44 -05: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 ) ) {
2015-05-05 12:19:08 -04:00
$image_id = $fallback_id;
if ( in_array( get_post_type(), $use_fallback ) ) {
return $image_id;
}
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
2014-11-17 13:51:44 -05:00
if ( is_home() && 'page' === $frontpage && ! empty( $postspage_image ) ) {
$image_id = $postspage_image;
2014-10-24 10:01:43 -04:00
}
2015-01-03 09:10:38 -05:00
2015-02-15 20:57:24 -05:00
$object = get_queried_object();
2015-05-06 15:23:50 -04:00
// singular or archive CPT
2015-03-04 08:51:22 -05:00
if ( $object && is_main_query() && ! is_admin() ) {
$post_type = '';
2015-02-15 20:57:24 -05:00
if ( $object->name ) { // results in post type on cpt archive
$post_type = $object->name;
2015-06-06 17:57:28 -04:00
} elseif ( $object->taxonomy ) { // on a tax/term/category
2015-02-15 20:57:24 -05:00
$tax_object = get_taxonomy( $object->taxonomy );
$post_type = $tax_object->object_type[0];
2015-06-06 17:57:28 -04:00
} elseif ( $object->post_type ) { // on singular
2015-02-15 20:57:24 -05:00
$post_type = $object->post_type;
}
2015-04-24 17:02:47 -04:00
if ( ! empty( $displaysetting['post_type'][ $post_type ] ) ) {
2015-06-06 21:30:42 -04:00
$image_id = is_numeric( $displaysetting['post_type'][ $post_type ] ) ? $displaysetting['post_type'][ $post_type ] : self::get_image_id( $displaysetting['post_type'][ $post_type ] );
2015-05-05 12:50:19 -04:00
/**
* use the custom post type featured image
2015-05-06 13:05:01 -04:00
*
2015-05-07 20:39:19 -04:00
* @since 2.2.1
2015-05-05 12:50:19 -04:00
*/
2015-06-06 17:57:28 -04:00
$use_cpt = apply_filters( 'displayfeaturedimagegenesis_use_post_type_image', array() );
2015-05-05 12:50:19 -04:00
if ( in_array( get_post_type(), $use_cpt ) ) {
return $image_id;
}
2015-01-03 09:10:38 -05:00
}
}
2015-06-07 12:04:36 -04:00
if ( is_author() ) {
$image_id = get_the_author_meta( 'displayfeaturedimagegenesis_author', (int) get_query_var( 'author' ) );
}
// taxonomy
2015-02-15 20:57:24 -05:00
if ( is_category() || is_tag() || is_tax() ) {
2015-01-04 16:01:11 -05:00
$t_id = $object->term_id;
2015-01-07 19:08:49 -05:00
$term_meta = get_option( "displayfeaturedimagegenesis_$t_id" );
2015-02-15 20:57:24 -05:00
// if there is a term image
if ( ! empty( $term_meta['term_image'] ) ) {
2015-06-06 21:30:42 -04:00
$image_id = is_numeric( $term_meta['term_image'] ) ? $term_meta['term_image'] : self::get_image_id( $term_meta['term_image'] );
2015-01-08 13:24:54 -05:00
}
2015-05-05 12:19:08 -04:00
}
2015-01-05 11:04:44 -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() ) {
2015-05-05 12:19:08 -04:00
2015-05-07 19:00:06 -04:00
$term_image = display_featured_image_genesis_get_term_image_id();
if ( ! empty( $term_image ) ) {
$image_id = $term_image;
/**
* create filter to use taxonomy image if single post doesn't have a thumbnail, but one of its terms does.
* @var filter
*/
2015-06-06 17:57:28 -04:00
$use_tax_image = apply_filters( 'display_featured_image_genesis_use_taxonomy', array() );
2015-05-07 19:00:06 -04:00
if ( in_array( get_post_type(), $use_tax_image ) ) {
return $image_id;
}
2015-05-06 15:23:50 -04:00
}
2015-06-06 18:01:07 -04:00
if ( has_post_thumbnail() && $width >= $medium ) {
$image_id = get_post_thumbnail_id( get_the_ID() );
2014-12-31 10:02:02 -05:00
}
}
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;
2014-10-24 10:01:43 -04: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();
if ( ! $term || ! isset( $term->meta ) ) {
return;
}
$title = $term->meta['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
}
return $title;
}
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/
*/
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-04-10 21:28:23 -04:00
// as of 2.2.0, if a (new) image id is passed to the function, return it as is.
2015-04-29 15:02:35 -04:00
if ( is_numeric( $attachment_url ) ) {
2015-04-10 21:28:23 -04:00
return $attachment_url;
}
2015-03-23 10:10:32 -04:00
// If there is no url, return.
2015-06-06 17:57:28 -04:00
if ( '' === $attachment_url ) {
2015-03-23 10:10:32 -04:00
return;
}
2014-10-21 18:16:00 -04:00
// 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'] ) ) {
// Remove the upload path base directory from the attachment URL
2015-04-29 15:02:35 -04:00
$attachment_url = str_replace( trailingslashit( $upload_dir_paths['baseurl'] ), '', $attachment_url );
// 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-03 14:36:16 -04:00
$attachment_id = $this->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
* @param var $url_stripped image url without WP resize string (eg 150x150)
* @param var $attachment_url image url
* @return int (image id) image ID, or false
*
* @since 2.2.0
*
* @author hellofromtonya
*/
2015-06-03 14:36:16 -04:00
protected 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] );
}
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 ) );
}
2014-10-21 18:16:00 -04:00
}