refactor variables into utility class

This commit is contained in:
Robin Cornett
2014-10-24 10:01:43 -04:00
parent 8662a76621
commit 4801a582cb
4 changed files with 98 additions and 94 deletions
@@ -8,6 +8,68 @@
*/
class Display_Featured_Image_Genesis_Common {
/**
* set and retreive variables for the featured image.
* @return $item
*
* @since 1.1.0
*/
static function get_image_variables() {
$item = new stdClass();
global $post;
// variables internal to this function
$frontpage = get_option( 'show_on_front' ); // either 'posts' or 'page'
$postspage = get_option( 'page_for_posts' );
$postspage_image = get_post_thumbnail_id( $postspage );
if ( is_singular() ) {
$post_thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'displayfeaturedimage_backstretch' );
}
// variables used outside this function
$item->fallback = get_option( 'displayfeaturedimage_default' );
$item->fallback_id = self::get_image_id( $item->fallback );
$item->large = get_option( 'large_size_w' );
$item->medium = get_option( 'medium_size_w' );
$item->reduce = get_option( 'displayfeaturedimage_less_header', 0 );
// Set Featured Image Source
if ( is_home() && $frontpage === 'page' && !empty( $postspage_image ) ) { // if on the blog page and it has a post_thumbnail
$item->original = wp_get_attachment_image_src( $postspage_image, 'displayfeaturedimage_backstretch' );
}
// any singular post/page/CPT with either a post_thumbnail larger than medium size OR there is no $item->fallback
elseif ( is_singular() && ( $post_thumbnail[1] > $item->medium || empty( $item->fallback ) ) && !in_array( get_post_type(), self::use_fallback_image() ) ) {
$item->original = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'displayfeaturedimage_backstretch' );
}
// otherwise use $item->fallback. should include !is_singular AND $item->fallback, and is_singular with either a small image or no post_thumbnail
else {
$item->original = wp_get_attachment_image_src( $item->fallback_id, 'displayfeaturedimage_backstretch' );
}
// Set Post/Page Title
if ( is_singular() && ! is_front_page() ) {
$item->title = get_the_title();
}
elseif ( is_home() && $frontpage === 'page' ) {
$item->title = get_post( $postspage )->post_title;
}
else {
$item->title = '';
}
// declare this last so that $item->original is set.
if ( !empty( $post->post_content ) ) {
$item->content = strpos( $post->post_content, $item->original[0] );
}
else {
$item->content = '';
}
return $item;
}
/**
* Get the ID of each image dynamically.
*
@@ -39,4 +101,26 @@ class Display_Featured_Image_Genesis_Common {
return $attachment_id;
}
/**
* skip certain post types
* @return filter creates a new filter for themes/plugins to use to skip certain post types
*
* @since 1.0.1
*/
static function get_skipped_posttypes() {
return apply_filters( 'display_featured_image_genesis_skipped_posttypes', array( 'attachment', 'revision', 'nav_menu_item' ) );
}
/**
* use fallback image as backstretch
* @return filter creates a new filter for themes/plugins to use to use the fallback image even if a large featured image is in place
*
* @since 1.2.0
*/
static function use_fallback_image() {
return apply_filters( 'display_featured_image_genesis_use_default', array( 'attachment', 'revision', 'nav_menu_item' ) );
}
}
@@ -16,7 +16,7 @@ class Display_Featured_Image_Genesis_Output {
*/
public function manage_output() {
$fallback = get_option( 'displayfeaturedimage_default' );
if ( ( empty( $fallback ) && !is_home() && !is_singular() ) || ( in_array( get_post_type(), $this->get_skipped_posttypes() ) ) ) {
if ( ( empty( $fallback ) && !is_home() && !is_singular() ) || ( in_array( get_post_type(), Display_Featured_Image_Genesis_Common::get_skipped_posttypes() ) ) ) {
return;
}
else {
@@ -25,83 +25,6 @@ class Display_Featured_Image_Genesis_Output {
}
}
/**
* set and retreive variables for the featured image.
* @return $item
*
* @since 1.1.0
*/
protected function get_image_variables() {
$item = new stdClass();
global $post;
$fallback = get_option( 'displayfeaturedimage_default' );
$fallback_id = Display_Featured_Image_Genesis_Common::get_image_id( $fallback );
$frontpage = get_option( 'show_on_front' ); // either 'posts' or 'page'
$postspage = get_option( 'page_for_posts' );
$postspage_image = get_post_thumbnail_id( $postspage );
if ( is_singular() ) {
$originalthumb = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'displayfeaturedimage_backstretch' );
}
$item->large = get_option( 'large_size_w' );
$item->medium = get_option( 'medium_size_w' );
$item->reduce = get_option( 'displayfeaturedimage_less_header', 0 );
// Set Featured Image Source
if ( is_home() && $frontpage === 'page' && !empty( $postspage_image ) ) { // if on the blog page and it has a post_thumbnail
$item->original = wp_get_attachment_image_src( $postspage_image, 'displayfeaturedimage_backstretch' );
}
// any singular post/page/CPT with either a post_thumbnail larger than medium size OR there is no $fallback
elseif ( is_singular() && ( $originalthumb[1] > $item->medium || empty( $fallback ) ) && !in_array( get_post_type(), $this->use_fallback_image() ) ) {
$item->original = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'displayfeaturedimage_backstretch' );
}
// otherwise use $fallback. should include !is_singular AND $fallback, and is_singular with either a small image or no post_thumbnail
else {
$item->original = wp_get_attachment_image_src( $fallback_id, 'displayfeaturedimage_backstretch' );
}
// Set Post/Page Title
if ( is_singular() && ! is_front_page() ) {
$item->title = get_the_title();
}
elseif ( is_home() && $frontpage === 'page' ) {
$item->title = get_post( $postspage )->post_title;
}
else {
$item->title = '';
}
// declare this last so that $item->original is set.
if ( !empty( $post->post_content ) ) {
$item->content = strpos( $post->post_content, $item->original[0] );
}
else {
$item->content = '';
}
return $item;
}
/**
* 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' ) );
}
/**
* use fallback image as backstretch
* @return filter creates a new filter for themes/plugins to use to use the fallback image even if a large featured image is in place
*
* @since 1.2.1
*/
public function use_fallback_image() {
return apply_filters( 'display_featured_image_genesis_use_default', array( 'attachment', 'revision', 'nav_menu_item' ) );
}
/**
* enqueue plugin styles and scripts.
@@ -111,7 +34,7 @@ class Display_Featured_Image_Genesis_Output {
*/
public function load_scripts() {
$item = $this->get_image_variables();
$item = Display_Featured_Image_Genesis_Common::get_image_variables();
if ( ( !empty( $item->original ) && $item->content === false ) || ( !is_singular() && !empty( $item->original ) ) ) {
wp_enqueue_style( 'displayfeaturedimage-style', plugins_url( 'includes/css/display-featured-image-genesis.css', dirname( __FILE__ ) ), array(), 1.0 );
@@ -145,7 +68,7 @@ class Display_Featured_Image_Genesis_Output {
public function add_body_class( $classes ) {
global $post;
$item = $this->get_image_variables();
$item = Display_Featured_Image_Genesis_Common::get_image_variables();
if ( ( !empty( $item->original ) && $item->content === false ) || ( !is_singular() && !empty( $item->original ) ) ) {
if ( $item->original[1] > $item->large ) {
@@ -166,7 +89,7 @@ class Display_Featured_Image_Genesis_Output {
*/
public function do_backstretch_image_title() {
$item = $this->get_image_variables();
$item = Display_Featured_Image_Genesis_Common::get_image_variables();
if ( is_singular() && !is_front_page() ) {
remove_action( 'genesis_entry_header', 'genesis_do_post_title' ); // HTML5
@@ -80,21 +80,17 @@ class Display_Featured_Image_Genesis_Settings {
* @since 1.2.1
*/
public function set_default_image() {
$value = get_option( 'displayfeaturedimage_default' );
$id = Display_Featured_Image_Genesis_Common::get_image_id( $value );
$url = wp_get_attachment_image_src( $id, 'medium' );
$original = wp_get_attachment_image_src( $id, 'original' );
$large = get_option( 'large_size_w' );
$item = Display_Featured_Image_Genesis_Common::get_image_variables();
if ( $value && $original[1] >= $large ) {
if ( $item->fallback && $item->original[1] >= $item->large ) {
echo '<div id="upload_logo_preview">';
echo '<img src="' . $url[0] . '" style="max-width:400px" />';
echo '<img src="' . $item->original[0] . '" style="max-width:400px" />';
echo '</div>';
}
elseif ( $value && $original[1] <= $large ) {
elseif ( $item->fallback && $item->original[1] <= $item->large ) {
echo '<div class="error"><p>' . __( 'Sorry, your image is too small to serve as the default featured image.', 'display-featured-image-genesis' ) . '</p></div>';
}
echo '<input type="text" id="default_image_url" name="displayfeaturedimage_default" value="' . $value . '" />';
echo '<input type="text" id="default_image_url" name="displayfeaturedimage_default" value="' . $item->fallback . '" />';
echo '<input id="upload_default_image" type="button" class="upload_default_image button" value="' . __( 'Select Default Image', 'display-featured-image-genesis' ) . '" />';
echo '<p class="description">' . __( 'If you would like to use a default image for the featured image, upload it here. Must be a backstretch sized image.', 'display-featured-image-genesis' ) . '</p>';
}
@@ -110,7 +106,10 @@ class Display_Featured_Image_Genesis_Settings {
$displayfeaturedimage_help =
'<h3>' . __( 'Reducto!', 'display-featured-image-genesis' ) . '</h3>' .
'<p>' . __( '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' ) . '</p>';
'<p>' . __( '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' ) . '</p>' .
'<h3>' . __( 'Set a Default Featured Image', 'display-featured-image-genesis' ) . '</h3>' .
'<p>' . __( 'You may set a large image to be used sitewide if a featured image is not available. This image will show on posts, pages, and archives. It must be larger than your site&#39;s Large Image setting, or else it will not display. This is for a backstretch image only.', 'display-featured-image-genesis' ) . '</p>';
$screen->add_help_tab( array(
'id' => 'displayfeaturedimage-help',
@@ -16,8 +16,6 @@
*/
class Display_Featured_Image_Genesis {
public static $instance;
function __construct( $common, $output, $settings ) {
$this->common = $common;
$this->output = $output;
@@ -68,7 +66,7 @@ class Display_Featured_Image_Genesis {
echo '<div class="error"><p>' . sprintf(
__( 'Sorry, Display Featured Image for Genesis works only with the Genesis Framework. It has been deactivated. But since we&#39;re talking anyway, did you know that your server is running PHP version %1$s, which is outdated? You should ask your host to update that for you.', 'display-featured-image-genesis' ),
PHP_VERSION
) . '</p></div>';
) . '</p></div>';
}
if ( isset( $_GET['activate'] ) ) {