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

304 lines
10 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+
* @link http://robincornett.com
* @copyright 2014 Robin Cornett Creative, LLC
*/
class Display_Featured_Image_Genesis_Output {
2014-09-30 14:19:47 -04:00
2015-06-06 17:57:28 -04:00
protected $common;
protected $description;
protected $displaysetting;
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
$settings = new Display_Featured_Image_Genesis_Settings();
$this->displaysetting = $settings->get_display_setting();
$skip = $this->displaysetting['exclude_front'];
$post_types = array( 'attachment', 'revision', 'nav_menu_item' );
$skipped_types = apply_filters( 'display_featured_image_genesis_skipped_posttypes', $post_types );
2015-01-05 11:04:44 -05:00
2015-04-07 12:07:20 -04:00
if ( is_admin() || ( in_array( get_post_type(), $skipped_types ) ) || ( $skip && is_front_page() ) ) {
2014-09-30 14:19:47 -04:00
return;
}
2014-11-03 14:06:24 -05:00
$this->common = new Display_Featured_Image_Genesis_Common();
$this->item = Display_Featured_Image_Genesis_Common::get_image_variables();
2014-11-03 14:06:24 -05:00
add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) );
add_filter( 'body_class', array( $this, 'add_body_class' ) );
2014-09-30 14:19:47 -04:00
}
2014-10-21 18:16:00 -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() {
2015-06-03 14:36:16 -04:00
$version = $this->common->version;
$large = $this->common->minimum_backstretch_width();
2015-02-27 09:10:06 -05:00
$medium = absint( get_option( 'medium_size_w' ) );
2015-06-06 21:30:42 -04:00
$width = absint( $this->item->backstretch[1] );
2014-09-17 22:09:05 -04:00
2015-04-09 22:16:36 -04:00
// check if they have enabled display on subsequent pages
2015-06-06 17:57:28 -04:00
$is_paged = ! empty( $this->displaysetting['is_paged'] ) ? $this->displaysetting['is_paged'] : 0;
2015-04-09 22:16:36 -04:00
2015-06-06 17:57:28 -04:00
// 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
2015-06-06 21:30:42 -04:00
if ( empty( $this->item->backstretch ) || $width <= $medium || ( is_paged() && ! $is_paged ) || ( is_singular() && false !== $this->item->content ) ) {
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(), $version );
2014-09-17 22:09:05 -04:00
2015-06-06 17:57:28 -04:00
$force_backstretch = apply_filters( 'display_featured_image_genesis_force_backstretch', array() );
// check if the image is large enough for backstretch
if ( $width > $large || in_array( get_post_type(), $force_backstretch ) ) {
2014-12-08 13:25:52 -05:00
2015-06-06 17:57:28 -04:00
wp_enqueue_script( 'displayfeaturedimage-backstretch', plugins_url( '/includes/js/backstretch.js', dirname( __FILE__ ) ), array( 'jquery' ), $version, true );
wp_enqueue_script( 'displayfeaturedimage-backstretch-set', plugins_url( '/includes/js/backstretch-set.js', dirname( __FILE__ ) ), array( 'jquery', 'displayfeaturedimage-backstretch' ), $version, true );
2014-09-17 22:09:05 -04:00
add_action( 'wp_print_scripts', array( $this, 'localize_scripts' ) );
2015-06-06 17:57:28 -04:00
$hook = apply_filters( 'display_featured_image_move_backstretch_image', 'genesis_after_header' );
add_action( esc_attr( $hook ), array( $this, 'do_backstretch_image_title' ) );
2014-10-16 08:53:41 -04:00
2015-06-06 17:57:28 -04:00
} elseif ( $width <= $large ) { // otherwise it's a large image.
2015-01-05 11:04:44 -05:00
2015-06-06 17:57:28 -04:00
remove_action( 'genesis_before_loop', 'genesis_do_cpt_archive_title_description' );
add_action( 'genesis_before_loop', 'genesis_do_cpt_archive_title_description', 15 );
2015-06-06 17:57:28 -04:00
$hook = 'genesis_before_loop';
if ( is_singular() && ! is_page_template( 'page_blog.php' ) ) {
$hook = apply_filters( 'display_featured_image_genesis_move_large_image', $hook );
2014-09-17 22:09:05 -04:00
}
2015-06-06 17:57:28 -04:00
add_action( esc_attr( $hook ), array( $this, 'do_large_image' ), 12 ); // works for both HTML5 and XHTML
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 ) {
2015-06-03 14:36:16 -04:00
$large = $this->common->minimum_backstretch_width();
2015-08-30 17:05:37 -04:00
$medium = (int) get_option( 'medium_size_w' );
$width = (int) $this->item->backstretch[1];
2014-09-17 22:09:05 -04:00
2015-04-09 22:16:36 -04:00
// check if they have enabled display on subsequent pages
2015-06-06 17:57:28 -04:00
$is_paged = ! empty( $this->displaysetting['is_paged'] ) ? $this->displaysetting['is_paged'] : 0;
2015-04-09 22:16:36 -04:00
// if there is no backstretch image set, or it is too small, or it's page 2+ and they didn't change the setting, die
2015-06-06 21:30:42 -04:00
if ( empty( $this->item->backstretch ) || $width <= $medium || ( is_paged() && ! $is_paged ) ) {
return $classes;
}
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
}
/**
* Pass variables through to our js
* @return backstretchVars 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
2015-07-27 16:11:01 -04:00
$backstretch_variables = 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' );
$output = array(
'src' => esc_url( $this->item->backstretch[0] ),
'largesrc' => esc_url( $large[0] ),
'width' => $large[1],
'height' => (int) $this->displaysetting['less_header'],
'centeredX' => (bool) $backstretch_variables['centeredX'],
'centeredY' => (bool) $backstretch_variables['centeredY'],
'fade' => (int) $backstretch_variables['fade'],
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();
2015-04-18 09:00:26 -04:00
/**
* filter to maybe move titles, or not
* @var filter
* @since 2.2.0
*/
2015-06-06 17:57:28 -04:00
$do_not_move_title = apply_filters( 'display_featured_image_genesis_do_not_move_titles', array() );
$keep_titles = $this->displaysetting['keep_titles'];
2015-04-18 09:00:26 -04:00
2015-02-13 17:03:32 -05:00
// if titles will be moved to overlay backstretch image
2015-04-18 09:00:26 -04:00
if ( ! $keep_titles && ! in_array( get_post_type(), $do_not_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' );
2015-06-06 17:57:28 -04:00
$move_excerpts = $this->displaysetting['move_excerpts'];
2015-01-05 11:04:44 -05:00
/**
* create a filter to not move excerpts if move excerpts is enabled
* @var filter
2015-01-10 10:45:16 -05:00
* @since 2.0.0 (deprecated old function from 1.3.3)
2015-01-05 11:04:44 -05:00
*/
2015-06-06 17:57:28 -04:00
$omit_excerpt = apply_filters( 'display_featured_image_genesis_omit_excerpt', array() );
2014-10-28 18:12:44 -04:00
2015-05-08 09:32:50 -04:00
// if move excerpts is enabled
2015-01-05 11:04:44 -05:00
if ( $move_excerpts && ! in_array( get_post_type(), $omit_excerpt ) ) {
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
2015-06-06 17:57:28 -04:00
} elseif ( ! $keep_titles && ! in_array( get_post_type(), $do_not_move_title ) ) { // if titles are being moved to overlay the image
2014-11-03 14:06:24 -05:00
2015-08-30 17:01:04 -04:00
if ( ! empty( $this->item->title && $this->do_the_title() ) ) {
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>';
2014-09-17 22:09:05 -04:00
}
/**
* Large image, centered above content
* @return image
*
* @since 1.0.0
*/
public function do_large_image() {
$image = sprintf( '<img src="%1$s" class="aligncenter featured" alt="%2$s" />',
2015-06-06 21:30:42 -04:00
esc_url( $this->item->backstretch[0] ),
esc_attr( $this->item->title )
2014-11-18 21:01:25 -05:00
);
2015-05-08 09:32:50 -04:00
$image = apply_filters( 'display_featured_image_genesis_large_image_output', $image );
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-08-31 09:31:33 -04:00
/**
* filter to show title on front page
* @return boolean true/false
*
* @since 2.3.0
*/
$show_front_title = apply_filters( 'display_featured_image_genesis_excerpt_show_front_page_title', false );
$show_front_title = true === $show_front_title ? $show_front_title : false;
if ( ( is_front_page() && ! $show_front_title ) || ( is_page_template( 'page_blog.php' ) && genesis_a11y( 'headings' ) ) ) {
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 );
$title_output = apply_filters( 'display_featured_image_genesis_modify_title_overlay', $title_output, esc_attr( $class ), esc_attr( $itemprop ), $title );
return $title_output;
}
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' );
}
}