2015-02-16 13:23:45 -05:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Helper functions for Display Featured Image for Genesis
|
|
|
|
|
*
|
|
|
|
|
* @package DisplayFeaturedImageGenesis
|
|
|
|
|
* @author Robin Cornett <hello@robincornett.com>
|
|
|
|
|
* @link https://github.com/robincornett/display-featured-image-genesis/
|
2020-08-23 10:31:02 -04:00
|
|
|
* @copyright 2015-2020 Robin Cornett
|
2015-02-16 13:23:45 -05:00
|
|
|
* @license GPL-2.0+
|
|
|
|
|
*/
|
|
|
|
|
|
2015-12-07 12:50:00 -05:00
|
|
|
/**
|
|
|
|
|
* Helper function to retrieve the term image ID, whether as term_meta or wp_options
|
|
|
|
|
* @param int $term_id term ID
|
|
|
|
|
* @param string $image_id image ID
|
|
|
|
|
* @return int image ID
|
|
|
|
|
* @since 2.4.0
|
|
|
|
|
*/
|
|
|
|
|
function displayfeaturedimagegenesis_get_term_image( $term_id, $image_id = '' ) {
|
|
|
|
|
if ( function_exists( 'get_term_meta' ) ) {
|
|
|
|
|
$image_id = get_term_meta( $term_id, 'displayfeaturedimagegenesis', true );
|
|
|
|
|
}
|
|
|
|
|
if ( ! $image_id ) {
|
2017-12-27 15:17:30 -05:00
|
|
|
$term_meta = get_option( "displayfeaturedimagegenesis_{$term_id}" );
|
2015-12-07 12:50:00 -05:00
|
|
|
if ( $term_meta ) {
|
2018-05-28 14:57:05 -04:00
|
|
|
$image_id = $term_meta['term_image'];
|
2015-12-07 12:50:00 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $image_id;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-16 13:23:45 -05:00
|
|
|
/**
|
|
|
|
|
* gets the term image ID
|
2017-01-20 16:52:10 -05:00
|
|
|
* @return int reusable function to get a post's term image, if it exists
|
2015-02-18 18:28:07 -05:00
|
|
|
*
|
2017-01-20 16:52:10 -05:00
|
|
|
* @param $image_id string
|
2015-02-18 18:28:07 -05:00
|
|
|
* @since 2.1.0
|
2015-02-16 13:23:45 -05:00
|
|
|
*/
|
2015-08-15 15:18:27 -04:00
|
|
|
function display_featured_image_genesis_get_term_image_id( $image_id = '' ) {
|
2015-05-07 20:36:35 -04:00
|
|
|
|
2017-01-20 16:52:10 -05:00
|
|
|
$post = get_post( get_the_ID() );
|
|
|
|
|
$taxonomies = array();
|
2017-04-08 15:38:25 -04:00
|
|
|
$object_tax = get_object_taxonomies( $post, 'names' );
|
|
|
|
|
foreach ( $object_tax as $taxonomy ) {
|
2017-01-20 16:52:10 -05:00
|
|
|
$taxonomies[] = $taxonomy;
|
|
|
|
|
}
|
2017-02-07 16:47:41 -05:00
|
|
|
$args = array(
|
|
|
|
|
'orderby' => 'count',
|
|
|
|
|
'order' => 'DESC',
|
|
|
|
|
);
|
|
|
|
|
$terms = apply_filters( 'displayfeaturedimagegenesis_object_terms', wp_get_object_terms( get_the_ID(), $taxonomies, $args ), $taxonomies, $args );
|
2015-02-16 13:23:45 -05:00
|
|
|
|
|
|
|
|
foreach ( $terms as $term ) {
|
2015-11-17 16:37:22 -05:00
|
|
|
$term_id = $term->term_id;
|
2015-12-07 12:50:00 -05:00
|
|
|
$image_id = displayfeaturedimagegenesis_get_term_image( $term_id );
|
2015-11-17 16:37:22 -05:00
|
|
|
if ( $image_id ) {
|
2015-02-16 13:23:45 -05:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-15 15:18:27 -04:00
|
|
|
return (int) $image_id;
|
2015-02-16 13:23:45 -05:00
|
|
|
}
|
2015-02-26 12:10:28 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function to get the term image URL.
|
|
|
|
|
* @param string $size image size
|
2017-12-27 15:17:30 -05:00
|
|
|
* @return string URL associated with the term image
|
2015-02-26 12:10:28 -05:00
|
|
|
*
|
|
|
|
|
* @since 2.1.0
|
|
|
|
|
*/
|
2019-10-23 18:47:15 -04:00
|
|
|
function display_featured_image_genesis_get_term_image_url( $size = '2048x2048' ) {
|
2015-02-26 12:10:28 -05:00
|
|
|
|
|
|
|
|
$image_id = display_featured_image_genesis_get_term_image_id();
|
|
|
|
|
$image_url = wp_get_attachment_image_src( $image_id, $size );
|
|
|
|
|
|
2015-04-19 16:27:57 -04:00
|
|
|
return esc_url( $image_url[0] );
|
2015-02-26 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function to get the default image ID.
|
2017-12-27 15:17:30 -05:00
|
|
|
*
|
|
|
|
|
* @param string $image_id
|
|
|
|
|
*
|
|
|
|
|
* @return int ID associated with the fallback/default image
|
2015-02-26 12:10:28 -05:00
|
|
|
*
|
|
|
|
|
* @since 2.1.0
|
|
|
|
|
*/
|
2015-08-15 15:18:27 -04:00
|
|
|
function display_featured_image_genesis_get_default_image_id( $image_id = '' ) {
|
2015-02-26 12:10:28 -05:00
|
|
|
|
2016-04-30 13:57:52 -04:00
|
|
|
$displaysetting = displayfeaturedimagegenesis_get_setting();
|
2015-02-26 12:10:28 -05:00
|
|
|
|
2018-05-28 14:57:05 -04:00
|
|
|
return (int) $displaysetting['default'];
|
2015-02-26 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function to get the default image URL.
|
2017-12-27 15:17:30 -05:00
|
|
|
* @param string size $size image size to retrieve
|
|
|
|
|
* @return string URL associated with the term image
|
2015-02-26 12:10:28 -05:00
|
|
|
*
|
|
|
|
|
* @since 2.1.0
|
|
|
|
|
*/
|
2019-10-23 18:47:15 -04:00
|
|
|
function display_featured_image_genesis_get_default_image_url( $size = '2048x2048' ) {
|
2015-02-26 12:10:28 -05:00
|
|
|
|
|
|
|
|
$image_id = display_featured_image_genesis_get_default_image_id();
|
2015-02-26 17:55:06 -05:00
|
|
|
$image_url = wp_get_attachment_image_src( $image_id, $size );
|
|
|
|
|
|
2015-04-19 16:27:57 -04:00
|
|
|
return esc_url( $image_url[0] );
|
2015-02-26 17:55:06 -05:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get custom post type featured image ID.
|
2017-12-27 15:17:30 -05:00
|
|
|
*
|
|
|
|
|
* @param string $image_id
|
|
|
|
|
*
|
|
|
|
|
* @return int Gets the ID of the image assigned as the custom post type featured image.
|
2015-02-26 17:55:06 -05:00
|
|
|
*
|
|
|
|
|
* @since 2.1.0
|
|
|
|
|
*/
|
2015-08-15 15:18:27 -04:00
|
|
|
function display_featured_image_genesis_get_cpt_image_id( $image_id = '' ) {
|
2015-02-26 17:55:06 -05:00
|
|
|
|
2015-08-15 15:18:27 -04:00
|
|
|
$post_type = '';
|
2019-05-13 14:05:15 -04:00
|
|
|
$displaysetting = displayfeaturedimagegenesis_get_setting( 'post_type' );
|
2015-02-26 17:55:06 -05:00
|
|
|
$object = get_queried_object();
|
2015-03-05 09:21:01 -05:00
|
|
|
if ( ! $object || is_admin() ) {
|
2017-12-27 15:17:30 -05:00
|
|
|
return '';
|
2015-03-05 09:21:01 -05:00
|
|
|
}
|
2015-02-26 17:55:06 -05:00
|
|
|
if ( $object->name ) { // results in post type on cpt archive
|
|
|
|
|
$post_type = $object->name;
|
2015-06-06 21:30:42 -04:00
|
|
|
} elseif ( $object->taxonomy ) { // on a tax/term/category
|
2015-02-26 17:55:06 -05:00
|
|
|
$tax_object = get_taxonomy( $object->taxonomy );
|
|
|
|
|
$post_type = $tax_object->object_type[0];
|
2015-06-06 21:30:42 -04:00
|
|
|
} elseif ( $object->post_type ) { // on singular
|
2015-02-26 17:55:06 -05:00
|
|
|
$post_type = $object->post_type;
|
|
|
|
|
}
|
2019-05-13 14:05:15 -04:00
|
|
|
if ( ! empty( $displaysetting[ $post_type ] ) ) {
|
|
|
|
|
$image_id = $displaysetting[ $post_type ];
|
2015-02-26 17:55:06 -05:00
|
|
|
}
|
|
|
|
|
|
2015-08-15 15:18:27 -04:00
|
|
|
return (int) $image_id;
|
2015-02-26 17:55:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the custom post type featured image URL.
|
|
|
|
|
* @param string $size image size
|
2017-12-27 15:17:30 -05:00
|
|
|
* @return string returns the image URL for the custom post type featured image
|
2015-02-26 17:55:06 -05:00
|
|
|
*
|
|
|
|
|
* @since 2.1.0
|
|
|
|
|
*/
|
2019-10-23 18:47:15 -04:00
|
|
|
function display_featured_image_genesis_get_cpt_image_url( $size = '2048x2048' ) {
|
2015-02-26 17:55:06 -05:00
|
|
|
|
|
|
|
|
$image_id = display_featured_image_genesis_get_cpt_image_id();
|
2015-02-26 12:10:28 -05:00
|
|
|
$image_url = wp_get_attachment_image_src( $image_id, $size );
|
|
|
|
|
|
2015-04-19 16:27:57 -04:00
|
|
|
return esc_url( $image_url[0] );
|
2015-02-26 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-05-03 14:23:38 -04:00
|
|
|
* Add term/default image to blog/archive pages. Use:
|
|
|
|
|
* add_action( 'genesis_entry_content', 'display_featured_image_genesis_add_archive_thumbnails', 5 );
|
2017-12-27 15:17:30 -05:00
|
|
|
*
|
|
|
|
|
* If a post doesn't have its own thumbnail, you can use this function to add one to archive pages.
|
2015-02-26 12:10:28 -05:00
|
|
|
*
|
|
|
|
|
* @since 2.1.0
|
|
|
|
|
*/
|
|
|
|
|
function display_featured_image_genesis_add_archive_thumbnails() {
|
|
|
|
|
|
2015-03-17 10:13:43 -04:00
|
|
|
$show_thumbs = genesis_get_option( 'content_archive_thumbnail' );
|
|
|
|
|
|
|
|
|
|
if ( is_singular() || is_admin() || is_404() || ! $show_thumbs ) {
|
2015-02-26 12:10:28 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-27 15:17:30 -05:00
|
|
|
$args = array(
|
2015-04-27 11:26:29 -04:00
|
|
|
'post_mime_type' => 'image',
|
2015-04-27 11:59:47 -04:00
|
|
|
'post_parent' => get_the_ID(),
|
2015-04-27 11:26:29 -04:00
|
|
|
'post_type' => 'attachment',
|
|
|
|
|
);
|
|
|
|
|
$attached_images = get_children( $args );
|
|
|
|
|
|
|
|
|
|
if ( has_post_thumbnail() || $attached_images ) {
|
2015-02-26 12:10:28 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-03 14:23:38 -04:00
|
|
|
$image_id = display_featured_image_genesis_get_term_image_id();
|
|
|
|
|
if ( empty( $image_id ) ) {
|
|
|
|
|
$image_id = display_featured_image_genesis_get_cpt_image_id();
|
|
|
|
|
if ( empty( $image_id ) ) {
|
|
|
|
|
$image_id = display_featured_image_genesis_get_default_image_id();
|
2015-02-26 17:55:06 -05:00
|
|
|
}
|
2015-02-26 12:10:28 -05:00
|
|
|
}
|
|
|
|
|
|
2015-05-03 14:23:38 -04:00
|
|
|
if ( empty( $image_id ) ) {
|
2015-02-26 12:10:28 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-03 14:23:38 -04:00
|
|
|
$image = genesis_get_image( array(
|
|
|
|
|
/**
|
|
|
|
|
* Filter the fallback image ID
|
|
|
|
|
*
|
|
|
|
|
* @since 2.2.0
|
|
|
|
|
*/
|
|
|
|
|
'fallback' => apply_filters( 'display_featured_image_genesis_fallback_archive_thumbnail', $image_id ),
|
|
|
|
|
'size' => genesis_get_option( 'image_size' ),
|
|
|
|
|
'attr' => genesis_parse_attr( 'entry-image', array( 'alt' => get_the_title() ) ),
|
|
|
|
|
'context' => 'archive',
|
|
|
|
|
) );
|
|
|
|
|
|
|
|
|
|
$permalink = get_permalink();
|
|
|
|
|
printf( '<a href="%1$s" aria-hidden="true">%2$s</a>',
|
2015-03-01 14:41:46 -05:00
|
|
|
esc_url( $permalink ),
|
2015-05-07 20:36:35 -04:00
|
|
|
wp_kses_post( $image )
|
2015-03-01 14:41:46 -05:00
|
|
|
);
|
2015-02-26 12:10:28 -05:00
|
|
|
}
|
2015-08-15 15:18:27 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* function to check image_id value, convert from URL if necessary
|
|
|
|
|
* @param string $image_id int or URL string
|
|
|
|
|
* @return int image ID
|
|
|
|
|
*
|
|
|
|
|
* @since 2.3.0
|
|
|
|
|
*/
|
|
|
|
|
function displayfeaturedimagegenesis_check_image_id( $image_id = '' ) {
|
2019-05-14 14:25:46 -04:00
|
|
|
return is_numeric( $image_id ) || ! $image_id ? $image_id : displayfeaturedimagegenesis_get()->get_image_id( $image_id );
|
2015-08-15 15:18:27 -04:00
|
|
|
}
|
2016-01-17 11:40:58 -05:00
|
|
|
|
|
|
|
|
/**
|
2016-02-23 08:03:54 -05:00
|
|
|
* Helper function to get the plugin settings.
|
2019-05-13 14:05:15 -04:00
|
|
|
*
|
|
|
|
|
* @param string $key
|
2017-10-31 17:22:57 -04:00
|
|
|
* @return mixed
|
2016-01-17 11:40:58 -05:00
|
|
|
*
|
|
|
|
|
* @since 2.4.2
|
|
|
|
|
*/
|
2019-05-13 14:05:15 -04:00
|
|
|
function displayfeaturedimagegenesis_get_setting( $key = '' ) {
|
|
|
|
|
return apply_filters( 'displayfeaturedimagegenesis_get_setting', $key );
|
2016-02-23 08:03:13 -05:00
|
|
|
}
|
2016-03-02 21:44:01 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the term meta (generally headline or intro text). Backwards compatible,
|
|
|
|
|
* but uses new term meta (as of Genesis 2.2.7)
|
|
|
|
|
* @param $term object the term
|
|
|
|
|
* @param $key string meta key to retrieve
|
|
|
|
|
* @param string $value string output of the term meta
|
|
|
|
|
*
|
|
|
|
|
* @return mixed|string
|
|
|
|
|
*
|
|
|
|
|
* @ since 2.5.0
|
|
|
|
|
*/
|
|
|
|
|
function displayfeaturedimagegenesis_get_term_meta( $term, $key, $value = '' ) {
|
|
|
|
|
if ( ! $term ) {
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
if ( function_exists( 'get_term_meta' ) ) {
|
|
|
|
|
$value = get_term_meta( $term->term_id, $key, true );
|
|
|
|
|
}
|
|
|
|
|
if ( ! $value && isset( $term->meta[ $key ] ) ) {
|
|
|
|
|
$value = $term->meta[ $key ];
|
|
|
|
|
}
|
|
|
|
|
return $value;
|
|
|
|
|
}
|