From f2abb5d4caadafc4c01642a3e4bd8530bc12f327 Mon Sep 17 00:00:00 2001 From: Robin Cornett Date: Sat, 18 May 2019 15:45:38 -0400 Subject: [PATCH] Create an image settings class I am sure that right now this breaks meta classes. --- ...ass-displayfeaturedimagegenesis-helper.php | 89 +++------ ...ayfeaturedimagegenesis-settings-images.php | 172 ++++++++++++++++++ ...s-displayfeaturedimagegenesis-settings.php | 76 -------- 3 files changed, 193 insertions(+), 144 deletions(-) create mode 100644 includes/settings/class-displayfeaturedimagegenesis-settings-images.php diff --git a/includes/settings/class-displayfeaturedimagegenesis-helper.php b/includes/settings/class-displayfeaturedimagegenesis-helper.php index 2f4d28e..7b22678 100644 --- a/includes/settings/class-displayfeaturedimagegenesis-helper.php +++ b/includes/settings/class-displayfeaturedimagegenesis-helper.php @@ -13,6 +13,12 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG */ protected $page = 'displayfeaturedimagegenesis'; + /** + * The image settings class. + * @var $images \DisplayFeaturedImageGenesisSettingsImages + */ + private $images; + /** * Generic function to add settings sections * @@ -229,6 +235,21 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG echo ''; } + /** + * Maybe instantiate the image settings class and do the image field. + * + * @param $args + * + * @since 3.1.0 + */ + protected function do_image( $args ) { + if ( ! isset( $this->images ) ) { + include_once 'class-displayfeaturedimagegenesis-settings-images.php'; + $this->images = new DisplayFeaturedImageGenesisSettingsImages( $this->setting ); + } + $this->images->do_image( $args ); + } + /** * Generic callback to display a field description. * @@ -248,51 +269,6 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG printf( '

%s

', wp_kses_post( $description ) ); } - /** - * display image preview - * - * @param int $id featured image ID - * @param $alt string description for alt text - * - * @return string - * - * @since 2.3.0 - */ - public function render_image_preview( $id, $alt = '' ) { - if ( empty( $id ) ) { - return ''; - } - - /* translators: the placeholder refers to which featured image */ - $alt_text = sprintf( __( '%s featured image', 'display-featured-image-genesis' ), esc_attr( $alt ) ); - $preview = wp_get_attachment_image_src( (int) $id, 'medium' ); - - return sprintf( '
%s
', esc_url( $preview[0] ), esc_attr( $alt_text ) ); - } - - /** - * show image select/delete buttons - * - * @param int $id image ID - * @param string $name name for value/ID/class - * - * @since 2.3.0 - */ - public function render_buttons( $id, $name ) { - $id = $id ? (int) $id : ''; - printf( '', esc_attr( $name ), esc_attr( $id ) ); - printf( - '', - esc_attr( $name ), - esc_attr__( 'Select Image', 'display-featured-image-genesis' ) - ); - printf( - ' ', - empty( $id ) ? 'style="display:none;"' : '', - esc_attr__( 'Delete Image', 'display-featured-image-genesis' ) - ); - } - /** * Get all public content types, not including built in. * @@ -365,29 +341,6 @@ class Display_Featured_Image_Genesis_Helper extends DisplayFeaturedImageGenesisG return $value; } - /** - * check if file type is image. updated to use WP function. - * @return bool - * @since 1.2.2 - * @since 2.5.0 - */ - protected function is_valid_img_ext( $file ) { - $valid = wp_check_filetype( $file ); - - return (bool) in_array( $valid['ext'], $this->allowed_file_types(), true ); - } - - /** - * Define the array of allowed image/file types. - * @return array - * @since 2.5.0 - */ - protected function allowed_file_types() { - $allowed = apply_filters( 'displayfeaturedimage_valid_img_types', array( 'jpg', 'jpeg', 'png', 'gif' ) ); - - return is_array( $allowed ) ? $allowed : explode( ',', $allowed ); - } - /** * Determines if the user has permission to save the information from the submenu * page. diff --git a/includes/settings/class-displayfeaturedimagegenesis-settings-images.php b/includes/settings/class-displayfeaturedimagegenesis-settings-images.php new file mode 100644 index 0000000..106d09b --- /dev/null +++ b/includes/settings/class-displayfeaturedimagegenesis-settings-images.php @@ -0,0 +1,172 @@ +setting = $setting; + } + + /** + * Do the image settings field. + * + * @param $args + * + * @since 3.1.0 + */ + public function do_image( $args ) { + $posts_page = $this->has_page_for_posts( $args['id'] ); + if ( $posts_page ) { + /* translators: the link is to edit the posts page. */ + printf( wp_kses_post( __( 'You may set a fallback image for Posts on your posts page.', 'display-featured-image-genesis' ) ), esc_url( $posts_page ) ); + + return; + } + $value = $this->get_image_value( $args['id'] ); + $name = $this->get_image_name( $args['id'] ); + if ( ! empty( $value ) ) { + echo wp_kses_post( $this->render_image_preview( $value, $args['id'] ) ); + } + $this->render_buttons( $value, $name ); + + if ( empty( $id ) || in_array( $args['id'], array( 'default', 'search', 'fourohfour', 'post' ), true ) ) { + return; + } + $this->do_cpt_description( $args ); + } + + /** + * Print the CPT description. + * + * @param $args + */ + private function do_cpt_description( $args ) { + $archive_link = get_post_type_archive_link( $args['id'] ); + if ( ! $archive_link ) { + return; + } + $description = sprintf( + /* translators: placeholder is the post type name. */ + __( 'View your %2$s archive.', 'display-featured-image-genesis' ), + esc_url( $archive_link ), + esc_attr( $args['title'] ) + ); + printf( '

%s

', wp_kses_post( $description ) ); + } + + /** + * Check if a posts page has been set: if so, return the edit link for it, otherwise return false. + * + * @param $id + * + * @return bool|string|null + * @since 3.1.0 + */ + private function has_page_for_posts( $id ) { + if ( 'post' !== $id ) { + return false; + } + $show_on_front = get_option( 'show_on_front' ); + $posts_page = get_option( 'page_for_posts' ); + + return ( 'page' === $show_on_front && $posts_page ) ? get_edit_post_link( $posts_page ) : false; + } + + /** + * Get the image setting value. + * @since 3.1.0 + * @param $id + * + * @return string + */ + private function get_image_value( $id ) { + $value = ''; + if ( 'default' === $id && $this->setting[ $id ] ) { + $value = $this->setting[ $id ]; + } elseif ( ! empty( $this->setting['post_type'][ $id ] ) ) { + $value = $this->setting['post_type'][ $id ]; + } + + return $value; + } + + /** + * Get the image setting name. + * @since 3.1.0 + * @param $id + * + * @return string + */ + private function get_image_name( $id ) { + if ( 'default' === $id ) { + $name = "{$this->page}[{$id}]"; + } else { + $name = "{$this->page}[post_type][{$id}]"; + } + + return $name; + } + + /** + * display image preview + * + * @param int $id featured image ID + * @param $alt string description for alt text + * + * @return string + * + * @since 2.3.0 + */ + public function render_image_preview( $id, $alt = '' ) { + if ( empty( $id ) ) { + return ''; + } + + /* translators: the placeholder refers to which featured image */ + $alt_text = sprintf( __( '%s featured image', 'display-featured-image-genesis' ), esc_attr( $alt ) ); + $preview = wp_get_attachment_image_src( (int) $id, 'medium' ); + + return sprintf( '
%s
', esc_url( $preview[0] ), esc_attr( $alt_text ) ); + } + + /** + * show image select/delete buttons + * + * @param int $id image ID + * @param string $name name for value/ID/class + * + * @since 2.3.0 + */ + public function render_buttons( $id, $name ) { + $id = $id ? (int) $id : ''; + printf( '', esc_attr( $name ), esc_attr( $id ) ); + printf( + '', + esc_attr( $name ), + esc_attr__( 'Select Image', 'display-featured-image-genesis' ) + ); + printf( + ' ', + empty( $id ) ? 'style="display:none;"' : '', + esc_attr__( 'Delete Image', 'display-featured-image-genesis' ) + ); + } +} diff --git a/includes/settings/class-displayfeaturedimagegenesis-settings.php b/includes/settings/class-displayfeaturedimagegenesis-settings.php index a45621b..b789945 100644 --- a/includes/settings/class-displayfeaturedimagegenesis-settings.php +++ b/includes/settings/class-displayfeaturedimagegenesis-settings.php @@ -204,82 +204,6 @@ class Display_Featured_Image_Genesis_Settings extends Display_Featured_Image_Gen return __( 'Optionally, change the hook location and priority of the featured image output. Use with caution. Note: this will change the hook/priority of the featured image sitewide. If you need to make changes based on content type, check the readme for code examples.', 'display-featured-image-genesis' ); } - /** - * Default image uploader - * - * @since 1.2.1 - * - * @param $args - */ - public function set_default_image( $args ) { - - $id = $this->setting[ $args['id'] ] ? $this->setting[ $args['id'] ] : ''; - $name = $this->page . '[' . $args['id'] . ']'; - if ( ! empty( $id ) ) { - echo wp_kses_post( $this->render_image_preview( $id, $args['id'] ) ); - } - $this->render_buttons( $id, $name ); - $this->do_description( $args ); - } - - /** - * Custom Post Type image uploader - * - * @since 2.0.0 - * - * @param $args - */ - public function set_cpt_image( $args ) { - - $this->do_image_buttons( $args ); - - if ( empty( $id ) || in_array( $args['id'], array( 'search', 'fourohfour', 'post' ), true ) ) { - return; - } - - $this->do_cpt_description( $args ); - } - - /** - * Print the featured image preview and buttons. - * - * @param $args - */ - protected function do_image_buttons( $args ) { - $show_on_front = get_option( 'show_on_front' ); - $posts_page = get_option( 'page_for_posts' ); - if ( 'page' === $show_on_front && $posts_page && 'post' === $args['id'] ) { - $link = get_edit_post_link( $posts_page ); - /* translators: the link is to edit the posts page. */ - printf( wp_kses_post( __( 'You may set a fallback image for Posts on your posts page.', 'display-featured-image-genesis' ) ), esc_url( $link ) ); - return; - } - $id = isset( $this->setting['post_type'][ $args['id'] ] ) && $this->setting['post_type'][ $args['id'] ] ? $this->setting['post_type'][ $args['id'] ] : ''; - $name = $this->page . '[post_type][' . esc_attr( $args['id'] ) . ']'; - if ( $id ) { - echo wp_kses_post( $this->render_image_preview( $id, $args['id'] ) ); - } - $this->render_buttons( $id, $name ); - } - - /** - * Print the CPT description. - * - * @param $args - */ - protected function do_cpt_description( $args ) { - $archive_link = get_post_type_archive_link( $args['id'] ); - if ( ! $archive_link ) { - return; - } - /* translators: placeholder is the post type name. */ - $description = sprintf( __( 'View your %2$s archive.', 'display-featured-image-genesis' ), - esc_url( $archive_link ), - esc_attr( $args['title'] ) - ); - printf( '

%s

', wp_kses_post( $description ) ); - } - /** * @return array */