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.
This commit is contained in:
Robin Cornett
2015-01-04 16:05:25 -05:00
parent 4e923f99ac
commit 83e46c5e31
3 changed files with 107 additions and 130 deletions
@@ -225,38 +225,6 @@ class Display_Featured_Image_Genesis_Settings {
) . '</p>';
}
/**
* 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 '<h4>' . $post->label . '</h4>';
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 '<div id="upload_logo_preview">';
echo '<img src="' . esc_url( $preview[0] ) . '" />';
echo '</div>';
}
echo '<input type="url" class="upload_image_url" id="displayfeaturedimagegenesis[post_type][' . $post_type . ']" name="displayfeaturedimagegenesis[post_type][' . $post_type . ']" value="' . esc_url( $this->displaysetting['post_type'][$post_type] ) . '" />';
echo '<input id="displayfeaturedimagegenesis[post_type][' . $post_type . ']" type="button" class="upload_default_image button" value="' . __( 'Select Image', 'display-featured-image-genesis' ) . '" />';
}
}
/**
* option to exclude featured image on front page
* @return 0 1 checkbox
@@ -301,6 +269,71 @@ class Display_Featured_Image_Genesis_Settings {
echo '<label for="displayfeaturedimagegenesis[feed_image]"><input type="checkbox" name="displayfeaturedimagegenesis[feed_image]" id="displayfeaturedimagegenesis[feed_image]" value="1"' . checked( 1, esc_attr( $this->displaysetting['feed_image'] ), false ) . ' class="code" />' . __( 'Optionally, add the featured image to your RSS feed.', 'display-featured-image-genesis' ) . '</label>';
}
/**
* 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 '<h4>' . $post->label . '</h4>';
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 '<div id="upload_logo_preview">';
echo '<img src="' . esc_url( $preview[0] ) . '" />';
echo '</div>';
}
echo '<input type="url" class="upload_image_url" id="displayfeaturedimagegenesis[post_type][' . $post_type . ']" name="displayfeaturedimagegenesis[post_type][' . $post_type . ']" value="' . esc_url( $this->displaysetting['post_type'][$post_type] ) . '" />';
echo '<input id="displayfeaturedimagegenesis[post_type][' . $post_type . ']" type="button" class="upload_default_image button" value="' . __( 'Select Image', 'display-featured-image-genesis' ) . '" />';
}
}
/**
* 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
@@ -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 '</tr>';
}
/**
* 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 ) );
}
}
+19 -1
View File
@@ -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
*