From 22537649d1e96dfe1560ad610f12c8b0973fdf36 Mon Sep 17 00:00:00 2001 From: Robin Cornett Date: Wed, 17 Sep 2014 22:09:05 -0400 Subject: [PATCH] refactor, Media setting added, textdomain --- README.md | 16 +- display-featured-image-genesis.php | 26 ++- ...ass-displayfeaturedimagegenesis-output.php | 158 ++++++++++++++++++ ...s-displayfeaturedimagegenesis-settings.php | 83 +++++++++ .../class-displayfeaturedimagegenesis.php | 157 +++-------------- includes/js/backstretch-set.js | 3 +- languages/display-featured-image-genesis.pot | 70 ++++++++ 7 files changed, 370 insertions(+), 143 deletions(-) create mode 100644 includes/class-displayfeaturedimagegenesis-output.php create mode 100644 includes/class-displayfeaturedimagegenesis-settings.php create mode 100644 languages/display-featured-image-genesis.pot diff --git a/README.md b/README.md index 37318fa..d9c8387 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Display Featured Image for Genesis -This plugin works within the Genesis Framework, to display your post/page featured images in new and fun ways. For now, an HTML5 theme is required. +This plugin works within the Genesis Framework, to display your post/page featured images in new and fun ways. It should work with either HTML5 or XHTML themes, but older themes may have a width set on elements which may not allow the full backstretch experience. ## Description @@ -55,13 +55,27 @@ function rgc_skip_post_types( $post_types ) { return $post_types; } ``` +### The backstretch image is a little too tall. +If you do not want the height of the backstretch image to be quite the height of the user's window, you can reduce it by just a hair. Go to Settings > Media and change the 'Reduction amount' number from the default of 0. The higher this number is, the shorter your image will be. Feel free to experiment, as no images are harmed by changing this number. + +Additionally/alternatively, you could set a max-height for the backstretch image via css: + +```css +.backstretch { + max-height: 700px; +} +``` ## Credits * Built by [Robin Cornett](http://robincornett.com/) ## Changelog +###1.1.1 +* added a setting in the admin to optionally reduce the height of the backstretch image +* refactoring + ###1.0.1 * added the filter for certain post types, and optional filter for other custom post types diff --git a/display-featured-image-genesis.php b/display-featured-image-genesis.php index 8060812..c729f8e 100644 --- a/display-featured-image-genesis.php +++ b/display-featured-image-genesis.php @@ -19,11 +19,23 @@ * License URI: http://www.gnu.org/licenses/gpl-2.0.txt */ -require plugin_dir_path( __FILE__ ) . 'includes/class-displayfeaturedimagegenesis.php'; - -add_action( 'init', 'displayfeaturedimagegenesis_instantiate' ); -function displayfeaturedimagegenesis_instantiate() { - if ( basename( get_template_directory() ) == 'genesis' ) { - new Display_Featured_Image_Genesis(); - } +// If this file is called directly, abort. +if ( ! defined( 'WPINC' ) ) { + die; } + +// Include classes +require plugin_dir_path( __FILE__ ) . 'includes/class-displayfeaturedimagegenesis.php'; +require plugin_dir_path( __FILE__ ) . 'includes/class-displayfeaturedimage-output.php'; +require plugin_dir_path( __FILE__ ) . 'includes/class-displayfeaturedimage-settings.php'; + +// Instantiate dependent classes +$displayfeaturedimagegenesis_output = new Display_Featured_Image_Genesis_Output(); +$displayfeaturedimagegenesis_settings = new Display_Featured_Image_Genesis_Settings(); + +$displayfeaturedimage = new Display_Featured_Image_Genesis( + $displayfeaturedimagegenesis_output, + $displayfeaturedimagegenesis_settings +); + +$displayfeaturedimage->run(); diff --git a/includes/class-displayfeaturedimagegenesis-output.php b/includes/class-displayfeaturedimagegenesis-output.php new file mode 100644 index 0000000..698cec9 --- /dev/null +++ b/includes/class-displayfeaturedimagegenesis-output.php @@ -0,0 +1,158 @@ + + * @license GPL-2.0+ + * @link http://robincornett.com + * @copyright 2014 Robin Cornett Creative, LLC + */ + +class Display_Featured_Image_Genesis_Output { + /** + * set and retreive variables for the featured image. + * @return $image + * + * @since 1.1.0 + */ + protected function get_image_variables() { + $image = new stdClass(); + global $post; + if ( is_home() ) { + $postspage = get_option( 'page_for_posts' ); + $image->original = wp_get_attachment_image_src( get_post_thumbnail_id( $postspage ), 'original' ); + } + else { + $image->original = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'original' ); + } + $image->large = get_option( 'large_size_w' ); + $image->medium = get_option( 'medium_size_w' ); + $image->content = strpos( $post->post_content, $image->original[0] ); + + return $image; + + } + + /** + * skip certain post types + * @return filter creates a new filter for themes/plugins to use to skip certain post types + * + * @since 1.0.1 + */ + public function get_skipped_posttypes() { + return apply_filters( 'display_featured_image_genesis_skipped_posttypes', array( 'attachment', 'revision', 'nav_menu_item' ) ); + } + + /** + * enqueue plugin styles and scripts. + * @return enqueue + * + * @since 1.0.0 + */ + public function load_scripts() { + + if ( !is_home() && in_array( get_post_type(), $this->get_skipped_posttypes() ) ) { + return; + } + + $image = $this->get_image_variables(); + if ( ( has_post_thumbnail() && $image->content === false ) || is_home() ) { + + wp_enqueue_style( 'displayfeaturedimage-style', plugins_url( 'includes/css/display-featured-image-genesis.css', dirname( __FILE__ ) ), array(), 1.0 ); + + add_action( 'genesis_before', array( $this, 'do_featured_image' ) ); + + if ( ( $image->original[1] ) > $image->large ) { + wp_enqueue_script( 'displayfeaturedimage-backstretch', plugins_url( '/includes/js/backstretch.js', dirname( __FILE__ ) ), array( 'jquery' ), '1.0.0' ); + wp_enqueue_script( 'displayfeaturedimage-backstretch-set', plugins_url( '/includes/js/backstretch-set.js', dirname( __FILE__ ) ), array( 'jquery', 'displayfeaturedimage-backstretch' ), '1.0.0' ); + + wp_localize_script( 'displayfeaturedimage-backstretch-set', 'BackStretchImg', array( 'src' => $image->original[0] ) ); + + $headerheight = get_option( 'displayfeaturedimage_less_header', 0 ); + wp_localize_script( 'displayfeaturedimage-backstretch-set', 'HeaderHeight', array( 'height' => esc_attr( $headerheight ) ) ); + + } + } + } + + /** + * 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 ) { + global $post; + + $image = $this->get_image_variables(); + + if ( $image->content === false ) { + if ( ( has_post_thumbnail() || is_home() ) && $image->original[1] > $image->large ) { + $classes[] = 'has-leader'; + } + elseif ( has_post_thumbnail() && ( ( $image->original[1] <= $image->large ) && ( $image->original[1] > $image->medium ) ) ) { + $classes[] = 'large-featured'; + } + } + return $classes; + } + + /** + * do the featured image + * @return image + * + * @since 1.0.0 + */ + public function do_featured_image() { + global $post; + + $image = $this->get_image_variables(); + + if ( $image->content === false ) { + if ( $image->original[1] > $image->large ) { + add_action( 'genesis_after_header', array( $this, 'do_backstretch_image' ) ); + } + elseif ( ( $image->original[1] <= $image->large ) && ( $image->original[1] > $image->medium ) ) { + add_action( 'genesis_before_entry', array( $this, 'do_large_image' ) ); + } + } + + } + + /** + * backstretch image (for images which are larger than Media Settings > Large ) + * @return image + * + * @since 1.0.0 + */ + public function do_backstretch_image() { + + $image = $this->get_image_variables(); + + if ( ! is_home() ) { + remove_action( 'genesis_entry_header', 'genesis_do_post_title' ); + } + + echo '
'; + if ( is_home() ) { + $title = get_post( get_option( 'page_for_posts' ) )->post_title; + echo '

' . $title . '

'; + } + else { + echo '

' . get_the_title() . '

'; + } + + echo '
'; + } + + /** + * Large image, centered above content + * @return image + * + * @since 1.0.0 + */ + public function do_large_image() { + global $post; + echo get_the_post_thumbnail( $post->ID, 'original', array( 'class' => 'aligncenter', 'alt' => the_title_attribute( 'echo=0' ) ) ); + } + +} \ No newline at end of file diff --git a/includes/class-displayfeaturedimagegenesis-settings.php b/includes/class-displayfeaturedimagegenesis-settings.php new file mode 100644 index 0000000..047aed7 --- /dev/null +++ b/includes/class-displayfeaturedimagegenesis-settings.php @@ -0,0 +1,83 @@ + + * @license GPL-2.0+ + * @link http://robincornett.com + * @copyright 2014 Robin Cornett Creative, LLC + */ + +class Display_Featured_Image_Genesis_Settings { + + /** + * Settings for media screen + * @return settings for backstretch image options + * + * @since 1.1.0 + */ + public function register_settings() { + register_setting( 'media', 'displayfeaturedimage_less_header', 'absint' ); + + add_settings_section( + 'display_featured_image_section', + __( 'Display Featured Image for Genesis', 'display-featured-image-genesis' ), + array( $this, 'section_description'), + 'media' + ); + + add_settings_field( + 'displayfeaturedimage_less_header', + '', + array( $this, 'header_size' ), + 'media', + 'display_featured_image_section' + ); + } + + /** + * Section description + * @return section description + * + * @since 1.1.0 + */ + public function section_description() { + echo '

' . __( 'Change this setting to reduce the maximum height of the backstretch image.', 'display-featured-image-genesis' ) . '

'; + } + + /** + * Setting for reduction amount + * @return number of pixels to remove in backstretch-set.js + * + * @since 1.1.0 + */ + public function header_size() { + $value = get_option( 'displayfeaturedimage_less_header', 0 ); + + echo ''; + echo ''; + echo '

' . __( 'Changing this number will reduce the backstretch image height by this number of pixels. Default is zero.', 'display-featured-image-genesis' ) . '

'; + + } + + /** + * Help tab for media screen + * @return help tab with verbose information for plugin + * + * @since 1.1.0 + */ + public function help() { + $screen = get_current_screen(); + + $displayfeaturedimage_help = + '

' . __( 'Reducto!', 'display-featured-image-genesis' ) . '

' . + '

' . __( 'Depending on how your header/nav are set up, or if you just do not want your backstretch image to extend to the bottom of the user screen, you may want to change this number. It will raise the bottom line of the backstretch image, making it shorter.', 'display-featured-image-genesis' ) . '

'; + + $screen->add_help_tab( array( + 'id' => 'displayfeaturedimage-help', + 'title' => __( 'Display Featured Image for Genesis', 'display-featured-image-genesis' ), + 'content' => $displayfeaturedimage_help, + ) ); + + } + +} diff --git a/includes/class-displayfeaturedimagegenesis.php b/includes/class-displayfeaturedimagegenesis.php index 4ade9ed..5f550ff 100644 --- a/includes/class-displayfeaturedimagegenesis.php +++ b/includes/class-displayfeaturedimagegenesis.php @@ -15,154 +15,43 @@ * @package DisplayFeaturedImageGenesis */ class Display_Featured_Image_Genesis { - function __construct() { - add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) ); - add_filter( 'body_class', array( $this, 'add_body_class' ) ); + function __construct( $output, $settings ) { + $this->output = $output; + $this->settings = $settings; } - /** - * set and retreive variables for the featured image. - * @return $image - * - * @since 1.0.0 - */ - protected function get_image_variables() { - $image = new stdClass(); - global $post; - if ( is_home() ) { - $postspage = get_option( 'page_for_posts' ); - $image->original = wp_get_attachment_image_src( get_post_thumbnail_id( $postspage ), 'original' ); - } - else { - $image->original = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'original' ); - } - $image->large = get_option( 'large_size_w' ); - $image->medium = get_option( 'medium_size_w' ); - $image->content = strpos( $post->post_content, $image->original[0] ); - - return $image; - - } - - - /** - * skip certain post types - * @return filter creates a new filter for themes/plugins to use to skip certain post types - * - * @since 1.0.1 - */ - public function get_skipped_posttypes() { - return apply_filters( 'display_featured_image_genesis_skipped_posttypes', array( 'attachment', 'revision', 'nav_menu_item' ) ); - } - - /** - * enqueue plugin styles and scripts. - * @return enqueue - * - * @since 1.0.0 - */ - public function load_scripts() { - - if ( !is_home() && in_array( get_post_type(), $this->get_skipped_posttypes() ) ) { + public function run() { + if ( basename( get_template_directory() ) !== 'genesis' ) { + add_action( 'admin_notices', array( $this, 'error_message' ) ); return; } - - $image = $this->get_image_variables(); - if ( ( has_post_thumbnail() && $image->content === false ) || is_home() ) { - - wp_enqueue_style( 'displayfeaturedimage-style', plugins_url( 'includes/css/display-featured-image-genesis.css', dirname( __FILE__ ) ), array(), 1.0 ); - - add_action( 'genesis_before', array( $this, 'do_featured_image' ) ); - - if ( ( $image->original[1] ) > $image->large ) { - wp_enqueue_script( 'displayfeaturedimage-backstretch', plugins_url( '/includes/js/backstretch.js', dirname( __FILE__ ) ), array( 'jquery' ), '1.0.0' ); - wp_enqueue_script( 'displayfeaturedimage-backstretch-set', plugins_url( '/includes/js/backstretch-set.js', dirname( __FILE__ ) ), array( 'jquery', 'displayfeaturedimage-backstretch' ), '1.0.0' ); - - wp_localize_script( 'displayfeaturedimage-backstretch-set', 'BackStretchImg', array( 'src' => $image->original[0] ) ); - - } - } + add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) ); + add_action( 'admin_init', array( $this->settings, 'register_settings' ) ); + add_action( 'load-options-media.php', array( $this->settings, 'help' ) ); + add_action( 'wp_enqueue_scripts', array( $this->output, 'load_scripts' ) ); + add_filter( 'body_class', array( $this->output, 'add_body_class' ) ); } /** - * set body class if featured images are displayed using the plugin - * @param filter $classes body_class + * Error message if we're not using the Genesis Framework. * - * @since 1.0.0 + * @since 1.1.0 */ - public function add_body_class( $classes ) { - global $post; - - $image = $this->get_image_variables(); - - if ( $image->content === false ) { - if ( ( has_post_thumbnail() || is_home() ) && $image->original[1] > $image->large ) { - $classes[] = 'has-leader'; - } - elseif ( has_post_thumbnail( $post->ID ) && ( ( $image->original[1] <= $image->large ) && ( $image->original[1] > $image->medium ) ) ) { - $classes[] = 'large-featured'; - } - } - return $classes; + public function error_message() { + echo '

' . sprintf( + __( 'Sorry, Display Featured Image for Genesis works only with the Genesis Framework. You can deactivate the plugin, since it is not working anyway, or you can activate a Genesis child theme.', 'display-featured-image-genesis' ), + esc_url( admin_url( 'plugins.php' ) ), + esc_url( admin_url( 'themes.php' ) ) + ) . '

'; } /** - * do the featured image - * @return image + * Set up text domain for translations * - * @since 1.0.0 + * @since 1.1.0 */ - public function do_featured_image() { - global $post; - - $image = $this->get_image_variables(); - - if ( $image->content === false ) { - if ( $image->original[1] > $image->large ) { - add_action( 'genesis_after_header', array( $this, 'do_backstretch_image' ) ); - } - elseif ( ( $image->original[1] <= $image->large ) && ( $image->original[1] > $image->medium ) ) { - add_action( 'genesis_before_entry', array( $this, 'do_large_image' ) ); - } - } - - } - - /** - * backstretch image (for images which are larger than Media Settings > Large ) - * @return image - * - * @since 1.0.0 - */ - public function do_backstretch_image() { - - $image = $this->get_image_variables(); - - if ( ! is_home() ) { - remove_action( 'genesis_entry_header', 'genesis_do_post_title' ); - } - - echo '
'; - if ( is_home() ) { - $title = get_post( get_option( 'page_for_posts' ) )->post_title; - echo '

' . $title . '

'; - } - else { - echo '

' . get_the_title() . '

'; - } - - echo '
'; - } - - /** - * Large image, centered above content - * @return image - * - * @since 1.0.0 - */ - public function do_large_image() { - global $post; - echo get_the_post_thumbnail( $post->ID, 'original', array( 'class' => 'aligncenter', 'alt' => the_title_attribute( 'echo=0' ) ) ); + public function load_textdomain() { + load_plugin_textdomain( 'display-featured-image-genesis', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); } } diff --git a/includes/js/backstretch-set.js b/includes/js/backstretch-set.js index 161e43b..be424a8 100644 --- a/includes/js/backstretch-set.js +++ b/includes/js/backstretch-set.js @@ -1,4 +1,5 @@ jQuery(document).ready(function($) { - $(".big-leader").css({'height':($(window).height())+'px'}); + + $(".big-leader").css({'height':($(window).height())-([HeaderHeight.height])+'px'}); $(".big-leader").backstretch([BackStretchImg.src],{'positionType':'fixed','fade':750,'centeredY':false}); }); \ No newline at end of file diff --git a/languages/display-featured-image-genesis.pot b/languages/display-featured-image-genesis.pot new file mode 100644 index 0000000..12f069a --- /dev/null +++ b/languages/display-featured-image-genesis.pot @@ -0,0 +1,70 @@ +# Copyright (C) 2014 +# This file is distributed under the GPL-2.0+. +# Robin Cornett , 2014. +# +msgid "" +msgstr "" +"Project-Id-Version: Display Featured Image for Genesis 1.1.0\n" +"POT-Creation-Date: 2014-09-17 21:11-0500\n" +"PO-Revision-Date: 2014-09-17 21:11-0500\n" +"Last-Translator: Robin Cornett \n" +"Language-Team: Robin Cornett \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"X-Generator: Poedit 1.6.7\n" +"X-Poedit-Basepath: .\n" +"X-Poedit-Language: English\n" +"X-Poedit-Country: UNITED STATES\n" +"Plural-Forms: nplurals=2; plural=(n != 1);\n" +"X-Poedit-KeywordsList: " +"__;_e;_x:1,2c;_ex:1,2c;_n:1,2;_nx:1,2,4c;_n_noop:1,2;_nx_noop:1,2,3c;esc_" +"attr__;esc_html__;esc_attr_e;esc_html_e;esc_attr_x:1,2c;esc_html_x:1,2c;\n" +"X-Poedit-Bookmarks: \n" +"X-Poedit-SearchPath-0: .\n" +"X-Textdomain-Support: yes\n" + +#: ../includes/class-displayfeaturedimage-settings.php:23 +#: ../includes/class-displayfeaturedimage-settings.php:77 +msgid "Display Featured Image for Genesis" +msgstr "" + +#: ../includes/class-displayfeaturedimage-settings.php:30 +msgid "Height" +msgstr "" + +#: ../includes/class-displayfeaturedimage-settings.php:44 +msgid "" +"Change this setting to reduce the maximum height of the backstretch image." +msgstr "" + +#: ../includes/class-displayfeaturedimage-settings.php:56 +msgid "Pixels to remove " +msgstr "" + +#: ../includes/class-displayfeaturedimage-settings.php:58 +msgid "" +"Changing this number will reduce the backstretch image height by this number " +"of pixels. Default is zero." +msgstr "" + +#: ../includes/class-displayfeaturedimage-settings.php:72 +msgid "Reducto!" +msgstr "" + +#: ../includes/class-displayfeaturedimage-settings.php:73 +msgid "" +"Depending on how your header/nav are set up, or if you just do not want your " +"backstretch image to extend to the bottom of the user screen, you may want " +"to change this number. It will raise the bottom line of the backstretch " +"image, making it shorter." +msgstr "" + +#: ../includes/class-displayfeaturedimagegenesis.php:42 +#, php-format +msgid "" +"Sorry, Display Featured Image for Genesis works only with the Genesis " +"Framework. You can deactivate the plugin, since it is " +"not working anyway, or you can activate a Genesis child " +"theme." +msgstr ""