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

437 lines
14 KiB
PHP
Raw Normal View History

2014-09-17 22:09:05 -04:00
<?php
/**
* @package DisplayFeaturedImageGenesis
* @author Robin Cornett <hello@robincornett.com>
* @license GPL-2.0+
2016-07-03 07:42:41 -04:00
* @link https://robincornett.com
2016-04-01 20:18:20 -04:00
* @copyright 2014-2016 Robin Cornett Creative, LLC
2014-09-17 22:09:05 -04:00
*/
class Display_Featured_Image_Genesis_Output {
2014-09-30 14:19:47 -04:00
2016-01-29 17:07:52 -05:00
/**
* @var Display_Featured_Image_Genesis_Common $common
*/
2015-06-06 17:57:28 -04:00
protected $common;
2016-01-29 17:07:52 -05:00
/**
* @var Display_Featured_Image_Genesis_Description $description
*/
2015-06-06 17:57:28 -04:00
protected $description;
2016-01-29 17:07:52 -05:00
/**
* @var
*/
2016-03-31 11:35:12 -04:00
protected $setting;
2016-01-29 17:07:52 -05:00
/**
* @var
*/
2015-06-06 21:30:42 -04:00
protected $item;
2015-06-06 17:57:28 -04:00
2014-09-30 14:19:47 -04:00
/**
* set parameters for scripts, etc. to run.
*
* @since 1.1.3
*/
public function manage_output() {
2015-01-05 11:04:44 -05:00
2016-03-31 11:35:12 -04:00
$this->setting = displayfeaturedimagegenesis_get_setting();
if ( $this->quit_now() ) {
return;
}
$this->common = new Display_Featured_Image_Genesis_Common();
$this->item = Display_Featured_Image_Genesis_Common::get_image_variables();
2015-10-16 16:10:28 -04:00
add_filter( 'jetpack_photon_override_image_downsize', '__return_true' );
2014-11-03 14:06:24 -05:00
add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) );
2014-09-30 14:19:47 -04:00
}
2014-09-17 22:09:05 -04:00
/**
* enqueue plugin styles and scripts.
* @return enqueue
*
* @since 1.0.0
*/
public function load_scripts() {
if ( ! $this->can_do_things() ) {
return;
}
2015-06-06 17:57:28 -04:00
$css_file = apply_filters( 'display_featured_image_genesis_css_file', plugin_dir_url( __FILE__ ) . 'css/display-featured-image-genesis.css' );
wp_enqueue_style( 'displayfeaturedimage-style', esc_url( $css_file ), array(), $this->common->version );
2016-07-02 09:13:30 -04:00
if ( $this->setting['max_height'] ) {
$this->add_inline_style();
}
add_filter( 'body_class', array( $this, 'add_body_class' ) );
2014-09-17 22:09:05 -04:00
$large = $this->common->minimum_backstretch_width();
$width = absint( $this->item->backstretch[1] );
2016-01-12 09:13:25 -05:00
/**
* Creates display_featured_image_genesis_force_backstretch filter to check
* whether get_post_type array should force the backstretch effect for this post type.
* @uses is_in_array()
*/
2016-01-07 13:52:08 -05:00
if ( $width > $large || Display_Featured_Image_Genesis_Common::is_in_array( 'force_backstretch' ) ) {
$this->do_backstretch_image_things();
2015-11-13 10:53:57 -05:00
} else {
$this->do_large_image_things();
2014-09-17 22:09:05 -04:00
}
}
2016-07-02 09:13:30 -04:00
/**
* Add max_height to output via inline style.
*
* @since 2.6.0
*/
public function add_inline_style() {
2016-07-02 14:11:23 -04:00
$css = sprintf( '.big-leader { max-height: %spx; }', $this->setting['max_height'] );
2016-07-02 09:13:30 -04:00
wp_add_inline_style( 'displayfeaturedimage-style', strip_tags( $css ) );
}
2014-09-17 22:09:05 -04:00
/**
* set body class if featured images are displayed using the plugin
* @param filter $classes body_class
*
* @since 1.0.0
*/
public function add_body_class( $classes ) {
if ( ! $this->can_do_things() ) {
return $classes;
}
$large = $this->common->minimum_backstretch_width();
$width = (int) $this->item->backstretch[1];
2015-06-06 21:30:42 -04:00
if ( false === $this->item->content || ! is_singular() ) {
2015-02-27 09:10:06 -05:00
if ( $width > $large ) {
2014-09-17 22:09:05 -04:00
$classes[] = 'has-leader';
2015-06-06 17:57:28 -04:00
} elseif ( $width <= $large ) {
2014-09-17 22:09:05 -04:00
$classes[] = 'large-featured';
}
}
2015-02-12 19:18:11 -05:00
return apply_filters( 'display_featured_image_genesis_classes', $classes );
2014-09-17 22:09:05 -04:00
}
/**
* All actions required to output the backstretch image
2015-11-12 19:10:47 -05:00
* @since 2.3.4
*/
protected function do_backstretch_image_things() {
wp_register_script( 'displayfeaturedimage-backstretch', plugins_url( '/includes/js/backstretch.js', dirname( __FILE__ ) ), array( 'jquery' ), $this->common->version, true );
wp_enqueue_script( 'displayfeaturedimage-backstretch-set', plugins_url( '/includes/js/backstretch-set.js', dirname( __FILE__ ) ), array( 'jquery', 'displayfeaturedimage-backstretch' ), $this->common->version, true );
add_action( 'wp_print_scripts', array( $this, 'localize_scripts' ) );
2015-11-10 09:58:13 -05:00
$hook = apply_filters( 'display_featured_image_move_backstretch_image', 'genesis_after_header' );
$priority = apply_filters( 'display_featured_image_move_backstretch_image_priority', 10 );
add_action( esc_attr( $hook ), array( $this, 'do_backstretch_image_title' ), $priority );
}
2014-09-17 22:09:05 -04:00
/**
* Pass variables through to our js
* @return $output variable array to send to js
2014-09-17 22:09:05 -04:00
*
2015-07-12 11:35:08 -04:00
* @since 2.3.0
2014-09-17 22:09:05 -04:00
*/
public function localize_scripts() {
2015-02-13 14:54:25 -05:00
// backstretch settings which can be filtered
$backstretch_vars = apply_filters( 'display_featured_image_genesis_backstretch_variables', array(
2015-02-13 14:54:25 -05:00
'centeredX' => true,
'centeredY' => true,
'fade' => 750,
2015-07-27 16:11:01 -04:00
) );
$image_id = Display_Featured_Image_Genesis_Common::set_image_id();
$large = wp_get_attachment_image_src( $image_id, 'large' );
$medium_large = wp_get_attachment_image_src( $image_id, 'medium_large' );
$output = array(
'source' => array(
'backstretch' => esc_url( $this->item->backstretch[0] ),
'large' => $large[3] ? esc_url( $large[0] ) : '',
'medium_large' => $medium_large[3] ? esc_url( $medium_large[0] ) : '',
),
'width' => array(
'backstretch' => $this->item->backstretch[1],
'large' => $large[3] ? $large[1] : '',
'medium_large' => $medium_large[3] ? $medium_large[1] : '',
),
2016-03-31 11:35:12 -04:00
'height' => (int) $this->setting['less_header'],
2016-04-02 12:05:19 -04:00
'centeredX' => (bool) $backstretch_vars['centeredX'],
'centeredY' => (bool) $backstretch_vars['centeredY'],
'fade' => (int) $backstretch_vars['fade'],
2016-06-24 13:02:39 -04:00
'title' => esc_attr( $this->item->title ),
2015-02-13 14:54:25 -05:00
);
wp_localize_script( 'displayfeaturedimage-backstretch-set', 'BackStretchVars', $output );
}
/**
* backstretch image title ( for images which are larger than Media Settings > Large )
* @return image
*
* @since 1.0.0
*/
public function do_backstretch_image_title() {
2015-02-10 16:54:13 -05:00
$this->description = new Display_Featured_Image_Genesis_Description();
if ( $this->move_title() ) {
2015-08-31 07:46:05 -04:00
$this->remove_title_descriptions();
}
2014-10-28 18:12:44 -04:00
echo '<div class="big-leader">';
echo '<div class="wrap">';
2014-10-28 18:12:44 -04:00
do_action( 'display_featured_image_genesis_before_title' );
if ( $this->move_excerpts() ) {
2014-10-28 18:12:44 -04:00
2015-08-30 17:05:37 -04:00
$this->do_title_descriptions();
2014-10-28 18:12:44 -04:00
} elseif ( $this->move_title() ) { // if titles are being moved to overlay the image
2014-11-03 14:06:24 -05:00
2015-08-31 22:12:17 -04:00
if ( ! empty( $this->item->title ) && $this->do_the_title() ) {
2015-08-30 17:01:04 -04:00
echo wp_kses_post( $this->do_the_title() );
2014-10-28 18:12:44 -04:00
}
2015-08-30 17:05:37 -04:00
add_action( 'genesis_before_loop', array( $this, 'add_descriptions' ) );
2014-11-03 14:06:24 -05:00
2014-10-27 15:21:15 -04:00
}
do_action( 'display_featured_image_genesis_after_title' );
2015-05-08 09:32:50 -04:00
// close wrap
echo '</div>';
2015-05-08 09:32:50 -04:00
// if javascript not enabled, do a fallback background image
2015-06-06 21:30:42 -04:00
printf( '<noscript><div class="backstretch no-js" style="background-image: url(%s); }"></div></noscript>', esc_url( $this->item->backstretch[0] ) );
2015-05-08 09:32:50 -04:00
// close big-leader
echo '</div>';
add_filter( 'jetpack_photon_override_image_downsize', '__return_false' ); // TODO remove
}
/**
* All actions required to output the large image
2015-11-12 19:10:47 -05:00
* @since 2.3.4
*/
protected function do_large_image_things() {
remove_action( 'genesis_before_loop', 'genesis_do_cpt_archive_title_description' );
add_action( 'genesis_before_loop', 'genesis_do_cpt_archive_title_description', 15 );
2016-05-09 14:54:08 -04:00
$hook = apply_filters( 'display_featured_image_genesis_move_large_image', 'genesis_before_loop' );
if ( ! is_singular() || is_page_template( 'page_blog.php' ) ) {
$check = strpos( $hook, 'entry' ) || strpos( $hook, 'post' );
if ( false !== $check ) {
$hook = 'genesis_before_loop';
}
}
2015-11-10 09:58:13 -05:00
$priority = apply_filters( 'display_featured_image_genesis_move_large_image_priority', 12 );
add_action( esc_attr( $hook ), array( $this, 'do_large_image' ), $priority ); // works for both HTML5 and XHTML
2014-09-17 22:09:05 -04:00
}
/**
* Large image, centered above content
* @return image
*
* @since 1.0.0
*/
public function do_large_image() {
2015-10-24 12:42:54 -04:00
$image_id = Display_Featured_Image_Genesis_Common::set_image_id();
$attr['class'] = 'aligncenter featured';
$attr['alt'] = $this->item->title;
2016-05-19 16:31:30 -04:00
$image_size = apply_filters( 'display_featured_image_large_image_size', Display_Featured_Image_Genesis_Common::image_size() );
2016-03-19 20:26:53 -04:00
$image = wp_get_attachment_image( $image_id, $image_size, false, $attr );
2015-10-24 12:42:54 -04:00
$image = apply_filters( 'display_featured_image_genesis_large_image_output', $image );
2015-05-08 09:32:50 -04:00
echo wp_kses_post( $image );
2014-09-17 22:09:05 -04:00
}
2015-08-30 17:01:04 -04:00
/**
* Return the title.
* @return string title with markup.
*
* @since 2.3.1
*/
protected function do_the_title() {
2015-09-01 20:34:25 -04:00
if ( is_front_page() && ! $this->description->show_front_page_title() ) {
2015-08-30 17:01:04 -04:00
return;
}
$class = is_singular() ? 'entry-title' : 'archive-title';
$itemprop = genesis_html5() ? 'itemprop="headline"' : '';
$title = $this->item->title;
$title_output = sprintf( '<h1 class="%s featured-image-overlay" %s>%s</h1>', $class, $itemprop, $title );
2015-09-01 13:34:15 -04:00
return apply_filters( 'display_featured_image_genesis_modify_title_overlay', $title_output, esc_attr( $class ), esc_attr( $itemprop ), $title );
2015-08-30 17:01:04 -04:00
}
2014-10-28 18:12:44 -04:00
/**
* Separate archive titles from descriptions. Titles show in leader image
* area; descriptions show before loop.
*
* @return descriptions
*
* @since 1.3.0
*
*/
2015-08-30 17:05:37 -04:00
public function add_descriptions() {
2014-10-28 18:12:44 -04:00
2015-06-03 14:36:16 -04:00
$this->description->do_tax_description();
$this->description->do_author_description();
$this->description->do_cpt_archive_description();
2014-11-03 14:06:24 -05:00
2014-10-28 18:12:44 -04:00
}
2015-08-31 07:46:05 -04:00
/**
* Do title and description together (for excerpt output)
* @return title/description/excerpt
*
* @since 2.3.1
*/
2015-08-30 17:05:37 -04:00
protected function do_title_descriptions() {
$this->description->do_front_blog_excerpt();
$this->description->do_excerpt();
genesis_do_taxonomy_title_description();
genesis_do_author_title_description();
genesis_do_cpt_archive_title_description();
}
2015-08-31 07:46:05 -04:00
/**
* Remove Genesis titles/descriptions
* @since 2.3.1
*/
protected function remove_title_descriptions() {
2015-08-31 09:30:43 -04:00
if ( is_singular() && ! is_page_template( 'page_blog.php' ) ) {
remove_action( 'genesis_entry_header', 'genesis_do_post_title' ); // HTML5
remove_action( 'genesis_post_title', 'genesis_do_post_title' ); // XHTML
}
2015-08-30 17:05:37 -04:00
remove_action( 'genesis_before_loop', 'genesis_do_taxonomy_title_description', 15 );
remove_action( 'genesis_before_loop', 'genesis_do_author_title_description', 15 );
remove_action( 'genesis_before_loop', 'genesis_do_cpt_archive_title_description' );
2015-09-01 13:34:15 -04:00
remove_action( 'genesis_before_loop', 'genesis_do_blog_template_heading' );
2015-09-08 11:31:21 -04:00
remove_action( 'genesis_before_loop', 'genesis_do_posts_page_heading' );
2015-08-30 17:05:37 -04:00
}
/**
* Check plugin settings/filters to see if the featured image should output on this post/etc. at all.
* Returns true to quit now; false to carry on.
* @return bool
*/
protected function quit_now() {
$exclude_front = is_front_page() && $this->setting['exclude_front'];
$post_type = get_post_type();
$skip_post_type = is_singular() && isset( $this->setting['skip'][ $post_type ] ) && $this->setting['skip'][ $post_type ] ? true : false;
/**
* Creates display_featured_image_genesis_skipped_posttypes filter to check
* whether get_post_type array should not run plugin on this post type.
* @uses is_in_array()
*/
$post_types = array( 'attachment', 'revision', 'nav_menu_item' );
2016-06-21 21:14:06 -04:00
if ( is_admin() || Display_Featured_Image_Genesis_Common::is_in_array( 'skipped_posttypes', $post_types ) || $skip_post_type || $exclude_front || $this->check_post_meta( '_displayfeaturedimagegenesis_disable' ) ) {
return true;
}
return false;
}
/**
* Check whether plugin can output backstretch or large image
* @return boolean checks featured image size. returns true if can proceed; false if cannot
*
2015-11-12 19:10:47 -05:00
* @since 2.3.4
*/
public function can_do_things() {
2016-02-23 08:03:54 -05:00
$can_do = true;
2015-11-13 10:53:57 -05:00
$medium = (int) apply_filters( 'displayfeaturedimagegenesis_set_medium_width', get_option( 'medium_size_w' ) );
$width = (int) $this->item->backstretch[1];
// check if they have enabled display on subsequent pages
2016-03-31 11:35:12 -04:00
$is_paged = ! empty( $this->setting['is_paged'] ) ? $this->setting['is_paged'] : 0;
// if there is no backstretch image set, or it is too small, or the image is in the content, or it's page 2+ and they didn't change the setting, die
if ( empty( $this->item->backstretch ) || $width <= $medium || ( is_paged() && ! $is_paged ) || ( is_singular() && false !== $this->item->content ) ) {
2016-02-23 08:03:54 -05:00
$can_do = false;
}
2016-02-23 08:03:54 -05:00
return apply_filters( 'displayfeaturedimagegenesis_can_do', $can_do );
}
/**
* create a filter to not move excerpts if move excerpts is enabled
* @var filter
* @since 2.0.0 (deprecated old function from 1.3.3)
*/
protected function move_excerpts() {
2016-03-31 11:35:12 -04:00
$move_excerpts = $this->setting['move_excerpts'];
2016-01-12 09:13:25 -05:00
/**
* Creates display_featured_image_genesis_omit_excerpt filter to check
* whether get_post_type array should not move excerpts for this post type.
* @uses is_in_array()
*/
2016-01-07 13:52:08 -05:00
if ( $move_excerpts && ! Display_Featured_Image_Genesis_Common::is_in_array( 'omit_excerpt' ) ) {
return true;
}
return false;
}
/**
* filter to maybe move titles, or not
* @var filter
* @since 2.2.0
*/
protected function move_title() {
$keep_titles = $this->setting['keep_titles'];
2016-01-12 09:13:25 -05:00
/**
* Creates display_featured_image_genesis_do_not_move_titles filter to check
* whether get_post_type array should not move titles to overlay the featured image.
* @uses is_in_array()
*/
2016-06-21 21:14:06 -04:00
if ( $keep_titles || Display_Featured_Image_Genesis_Common::is_in_array( 'do_not_move_titles' ) || $this->check_post_meta( '_displayfeaturedimagegenesis_move' ) ) {
return false;
}
2016-06-21 21:14:06 -04:00
return true;
}
/**
* If there is no image to use for the post thumbnail in archives,
* optionally use the term or post type image as a fallback instead.
*
* @param $defaults
*
* @return mixed
* @since 2.5.0
*/
public function change_thumbnail_fallback( $defaults ) {
2016-03-31 11:35:12 -04:00
if ( ! isset( $this->setting['thumbnails'] ) || ! $this->setting['thumbnails'] ) {
return $defaults;
}
remove_action( 'genesis_entry_content', 'display_featured_image_genesis_add_archive_thumbnails', 5 );
$args = array(
'post_mime_type' => 'image',
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
);
$attached_images = get_children( $args );
if ( $attached_images ) {
return $defaults;
}
$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 ( $image_id ) {
$defaults['fallback'] = $image_id;
}
return $defaults;
}
2016-06-21 21:14:06 -04:00
/**
* Check the post_meta for singular posts/pages/posts page.
* @param $meta_key string the post_meta key to check
*
* @return bool
* @since 2.5.2
*/
protected function check_post_meta( $meta_key ) {
$post_id = get_option( 'page_for_posts' ) && is_home() ? get_option( 'page_for_posts' ) : get_the_ID();
$post_meta = (bool) get_post_meta( $post_id, $meta_key, true );
return (bool) ( is_home() || is_singular() ) && $post_meta;
}
}