From 83e46c5e31f77526ac64c30f1ddf4f7da790b45f Mon Sep 17 00:00:00 2001 From: Robin Cornett Date: Sun, 4 Jan 2015 16:05:25 -0500 Subject: [PATCH] move tax save function to settings class this cuts redundancy, since the image validation function is already in this class. having the taxonomy save in its own class required duplicating the validation functions. --- ...s-displayfeaturedimagegenesis-settings.php | 120 +++++++++++++----- ...displayfeaturedimagegenesis-taxonomies.php | 97 -------------- .../class-displayfeaturedimagegenesis.php | 20 ++- 3 files changed, 107 insertions(+), 130 deletions(-) diff --git a/includes/class-displayfeaturedimagegenesis-settings.php b/includes/class-displayfeaturedimagegenesis-settings.php index 157f3fc..b0241b9 100644 --- a/includes/class-displayfeaturedimagegenesis-settings.php +++ b/includes/class-displayfeaturedimagegenesis-settings.php @@ -225,38 +225,6 @@ class Display_Featured_Image_Genesis_Settings { ) . '

'; } - /** - * Default image uploader - * - * @return image - * - * @since 1.2.1 - */ - public function set_cpt_image() { - - $item = Display_Featured_Image_Genesis_Common::get_image_variables(); - - foreach ( $this->post_types as $post ) { - - $post_type = $post->name; - if ( empty( $this->displaysetting['post_type'][$post_type] ) ) { - $this->displaysetting['post_type'][$post_type] = ''; - } - echo '

' . $post->label . '

'; - if ( ! empty( $this->displaysetting['post_type'][$post_type] ) ) { - $id = Display_Featured_Image_Genesis_Common::get_image_id( $this->displaysetting['post_type'][$post_type] ); - $preview = wp_get_attachment_image_src( $id, 'medium' ); - echo '
'; - echo ''; - echo '
'; - } - echo ''; - echo ''; - - } - - } - /** * option to exclude featured image on front page * @return 0 1 checkbox @@ -301,6 +269,71 @@ class Display_Featured_Image_Genesis_Settings { echo ''; } + /** + * Custom Post Type image uploader + * + * @return image + * + * @since x.y.z + */ + public function set_cpt_image() { + + $item = Display_Featured_Image_Genesis_Common::get_image_variables(); + + foreach ( $this->post_types as $post ) { + + $post_type = $post->name; + if ( empty( $this->displaysetting['post_type'][$post_type] ) ) { + $this->displaysetting['post_type'][$post_type] = ''; + } + echo '

' . $post->label . '

'; + if ( ! empty( $this->displaysetting['post_type'][$post_type] ) ) { + $id = Display_Featured_Image_Genesis_Common::get_image_id( $this->displaysetting['post_type'][$post_type] ); + $preview = wp_get_attachment_image_src( $id, 'medium' ); + echo '
'; + echo ''; + echo '
'; + } + echo ''; + echo ''; + + } + + } + + /** + * Save extra taxonomy fields callback function. + * @param term id $term_id the id of the term + * @return updated option updated option for term featured image + * + * @since x.y.z + */ + public function save_taxonomy_custom_meta( $term_id ) { + + if ( isset( $_POST['term_meta'] ) ) { + $t_id = $term_id; + $term_meta = get_option( "taxonomy_$t_id" ); + $cat_keys = array_keys( $_POST['term_meta'] ); + $is_updated = false; + foreach ( $cat_keys as $key ) { + if ( isset ( $_POST['term_meta'][$key] ) ) { + $term_meta[$key] = $_POST['term_meta'][$key]; + if ( $_POST['term_meta']['dfig_image'] === $term_meta[$key] ) { + $term_meta[$key] = $this->validate_taxonomy_image( $_POST['term_meta'][$key] ); + if ( false !== $term_meta[$key] ) { + $is_updated = true; + } + } + } + } + //* Save the option array. + if ( $is_updated ) { + update_option( "taxonomy_$t_id", $term_meta ); + } + } + + } + /** * validate all inputs * @param string $new_value various settings @@ -432,6 +465,29 @@ class Display_Featured_Image_Genesis_Settings { return $new_value; } + /** + * Returns false value for image if not correct file type/size + * @param string $new_value New value + * @return string New value or false, depending on allowed image size. + * @since x.y.z + */ + protected function validate_taxonomy_image( $new_value ) { + + $new_value = esc_url( $new_value ); + $valid = $this->is_valid_img_ext( $new_value ); + $large = get_option( 'large_size_w' ); + $id = Display_Featured_Image_Genesis_Common::get_image_id( $new_value ); + $metadata = wp_get_attachment_metadata( $id ); + $width = $metadata['width']; + + // ok for field to be empty + if ( $new_value && ( ! $valid || $width <= $large ) ) { + $new_value = false; + } + + return $new_value; + } + /** * returns file extension * @since 1.2.2 diff --git a/includes/class-displayfeaturedimagegenesis-taxonomies.php b/includes/class-displayfeaturedimagegenesis-taxonomies.php index a06a814..7744b1f 100644 --- a/includes/class-displayfeaturedimagegenesis-taxonomies.php +++ b/includes/class-displayfeaturedimagegenesis-taxonomies.php @@ -8,23 +8,6 @@ */ class Display_Featured_Image_Genesis_Taxonomies { - /** - * set up all actions for adding featured images to taxonomies - */ - public function set_taxonomy_meta() { - $args = array( - 'public' => true - ); - $output = 'names'; - $taxonomies = get_taxonomies( $args, $output ); - foreach ( $taxonomies as $taxonomy ) { - add_action( "{$taxonomy}_add_form_fields", array( $this, 'add_taxonomy_meta_fields' ), 5, 2 ); - add_action( "{$taxonomy}_edit_form_fields", array( $this, 'edit_taxonomy_meta_fields' ), 5, 2 ); - add_action( "edited_{$taxonomy}", array( $this, 'save_taxonomy_custom_meta' ), 10, 2 ); - add_action( "create_{$taxonomy}", array( $this, 'save_taxonomy_custom_meta' ), 10, 2 ); - } - } - /** * add featured image uploader to new taxonomy add */ @@ -71,84 +54,4 @@ class Display_Featured_Image_Genesis_Taxonomies { echo ''; } - /** - * Save extra taxonomy fields callback function. - * @param term id $term_id the id of the term - * @return updated option updated option for term featured image - * - * @since x.y.z - */ - public function save_taxonomy_custom_meta( $term_id ) { - - if ( isset( $_POST['term_meta'] ) ) { - $t_id = $term_id; - $term_meta = get_option( "taxonomy_$t_id" ); - $cat_keys = array_keys( $_POST['term_meta'] ); - $is_updated = false; - foreach ( $cat_keys as $key ) { - if ( isset ( $_POST['term_meta'][$key] ) ) { - $term_meta[$key] = $_POST['term_meta'][$key]; - if ( $_POST['term_meta']['dfig_image'] === $term_meta[$key] ) { - $term_meta[$key] = $this->validate_image( $_POST['term_meta'][$key] ); - if ( false !== $term_meta[$key] ) { - $is_updated = true; - } - } - } - } - //* Save the option array. - if ( $is_updated ) { - update_option( "taxonomy_$t_id", $term_meta ); - } - } - - } - - /** - * Returns previous value for image if not correct file type/size - * @param string $new_value New value - * @return string New or previous value, depending on allowed image size. - * @since x.y.z - */ - protected function validate_image( $new_value ) { - - $new_value = esc_url( $new_value ); - $valid = $this->is_valid_img_ext( $new_value ); - $large = get_option( 'large_size_w' ); - $id = Display_Featured_Image_Genesis_Common::get_image_id( $new_value ); - $metadata = wp_get_attachment_metadata( $id ); - $width = $metadata['width']; - - // ok for field to be empty - if ( $new_value && ( ! $valid || $width <= $large ) ) { - $new_value = false; - } - - return $new_value; - } - - /** - * returns file extension - * @since 1.2.2 - */ - protected function get_file_ext( $file ) { - $parsed = @parse_url( $file, PHP_URL_PATH ); - return $parsed ? strtolower( pathinfo( $parsed, PATHINFO_EXTENSION ) ) : false; - } - - /** - * check if file type is image - * @return file check file extension against list - * @since 1.2.2 - */ - protected function is_valid_img_ext( $file ) { - $file_ext = $this->get_file_ext( $file ); - - $this->valid = empty( $this->valid ) - ? (array) apply_filters( 'displayfeaturedimage_valid_img_types', array( 'jpg', 'jpeg', 'png', 'gif' ) ) - : $this->valid; - - return ( $file_ext && in_array( $file_ext, $this->valid ) ); - } - } diff --git a/includes/class-displayfeaturedimagegenesis.php b/includes/class-displayfeaturedimagegenesis.php index 8c60c5d..a842f20 100644 --- a/includes/class-displayfeaturedimagegenesis.php +++ b/includes/class-displayfeaturedimagegenesis.php @@ -35,7 +35,7 @@ class Display_Featured_Image_Genesis { add_action( 'init', array( $this, 'add_plugin_supports' ) ); add_action( 'admin_init', array( $this, 'check_settings' ) ); - add_action( 'admin_init', array( $this->taxonomies, 'set_taxonomy_meta' ) ); + add_action( 'admin_init', array( $this, 'set_taxonomy_meta' ) ); add_action( 'admin_init', array( $this->admin, 'set_up_columns' ) ); add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) ); add_action( 'admin_menu', array( $this->settings, 'do_submenu_page' ) ); @@ -141,6 +141,24 @@ class Display_Featured_Image_Genesis { return update_option( $setting, wp_parse_args( $new, get_option( $setting ) ) ); } + /** + * set up all actions for adding featured images to taxonomies + * @since x.y.z + */ + public function set_taxonomy_meta() { + $args = array( + 'public' => true + ); + $output = 'names'; + $taxonomies = get_taxonomies( $args, $output ); + foreach ( $taxonomies as $taxonomy ) { + add_action( "{$taxonomy}_add_form_fields", array( $this->taxonomies, 'add_taxonomy_meta_fields' ), 5, 2 ); + add_action( "{$taxonomy}_edit_form_fields", array( $this->taxonomies, 'edit_taxonomy_meta_fields' ), 5, 2 ); + add_action( "edited_{$taxonomy}", array( $this->settings, 'save_taxonomy_custom_meta' ), 10, 2 ); + add_action( "create_{$taxonomy}", array( $this->settings, 'save_taxonomy_custom_meta' ), 10, 2 ); + } + } + /** * Set up text domain for translations *