Files
display-featured-image-genesis/includes/class-displayfeaturedimagegenesis-taxonomies.php
T

239 lines
7.9 KiB
PHP
Raw Normal View History

2014-12-30 16:27:56 -05:00
<?php
/**
* settings for taxonomy pages
*
2016-04-01 20:18:20 -04:00
* @package DisplayFeaturedImageGenesis
* @author Robin Cornett <hello@robincornett.com>
* @license GPL-2.0+
* @link http://robincornett.com
* @copyright 2014-2016 Robin Cornett Creative, LLC
2015-01-10 09:42:54 -05:00
*
2015-01-10 10:45:16 -05:00
* @since 2.0.0
2014-12-30 16:27:56 -05:00
*/
2015-11-17 14:03:41 -05:00
class Display_Featured_Image_Genesis_Taxonomies extends Display_Featured_Image_Genesis_Helper {
2014-12-30 16:27:56 -05:00
2015-06-03 14:36:16 -04:00
protected $settings;
2015-07-12 12:28:21 -04:00
/**
* set up all actions for adding featured images to taxonomies
* @since 2.0.0
*/
public function set_taxonomy_meta() {
2015-11-17 16:37:22 -05:00
register_meta( 'term', 'displayfeaturedimagegenesis', array( $this, 'validate_taxonomy_image' ) );
2015-07-12 12:28:21 -04:00
$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 );
2015-11-17 16:37:22 -05:00
add_action( "edited_{$taxonomy}", array( $this, 'save_taxonomy_custom_meta' ) );
add_action( "create_{$taxonomy}", array( $this, 'save_taxonomy_custom_meta' ) );
2015-11-24 10:11:55 -05:00
add_action( "edit_{$taxonomy}", array( $this, 'save_taxonomy_custom_meta' ) );
2015-07-12 12:28:21 -04:00
add_action( 'load-edit-tags.php', array( $this, 'help' ) );
}
2015-08-08 07:28:13 -04:00
add_action( 'split_shared_term', array( $this, 'split_shared_term' ) );
2015-06-03 14:36:16 -04:00
}
2015-01-01 19:17:41 -05:00
/**
2015-01-10 09:42:54 -05:00
* add featured image uploader to new term add
2015-01-01 19:17:41 -05:00
*/
public function add_taxonomy_meta_fields() {
2014-12-30 16:27:56 -05:00
2015-08-11 08:36:24 -04:00
?>
<div class="form-field term-image-wrap">
2015-11-17 16:37:22 -05:00
<?php wp_nonce_field( 'displayfeaturedimagegenesis', 'displayfeaturedimagegenesis' ); ?>
2015-08-11 08:36:24 -04:00
<label for="displayfeaturedimagegenesis[term_image]"><?php esc_attr_e( 'Featured Image', 'display-featured-image-genesis' ); ?></label>
<input type="hidden" class="upload_image_id" id="term_image_id" name="displayfeaturedimagegenesis[term_image]" />
<input id="upload_default_image" type="button" class="upload_default_image button-secondary" value="<?php esc_attr_e( 'Select Image', 'display-featured-image-genesis' ); ?>" />
<input type="button" class="delete_image button-secondary" value="<?php esc_attr_e( 'Delete Image', 'display-featured-image-genesis' ); ?>" />
<p class="description"><?php esc_attr_e( 'Set Featured Image for new term.', 'display-featured-image-genesis' ); ?></p>
</div>
<?php
2014-12-30 16:27:56 -05:00
}
2015-01-01 19:17:41 -05:00
/**
2016-04-26 11:34:55 -04:00
* upload/preview featured image for term. edit term page
* @param object $term featured image input/display for individual term page
2015-01-10 09:42:54 -05:00
*
2015-01-10 10:45:16 -05:00
* @since 2.0.0
2015-01-01 19:17:41 -05:00
*/
public function edit_taxonomy_meta_fields( $term ) {
2014-12-30 16:27:56 -05:00
2015-11-17 16:37:22 -05:00
$term_id = $term->term_id;
2015-12-07 12:50:00 -05:00
$image_id = displayfeaturedimagegenesis_get_term_image( $term_id );
2015-01-01 19:17:41 -05:00
2015-03-25 10:51:51 -04:00
echo '<tr class="form-field term-image-wrap">';
2015-04-24 17:02:47 -04:00
printf( '<th scope="row" valign="top"><label for="displayfeaturedimagegenesis[term_image]">%s</label></th>',
2015-08-08 07:28:13 -04:00
esc_attr__( 'Featured Image', 'display-featured-image-genesis' )
2015-04-24 17:02:47 -04:00
);
echo '<td>';
2015-05-30 17:27:45 -04:00
$name = 'displayfeaturedimagegenesis[term_image]';
2015-11-17 16:37:22 -05:00
if ( $image_id ) {
2016-04-26 11:34:55 -04:00
echo wp_kses_post( $this->render_image_preview( $image_id, $term->name ) );
}
2015-11-17 16:37:22 -05:00
$this->render_buttons( $image_id, $name );
2015-04-24 17:02:47 -04:00
echo '<p class="description">';
printf(
2015-08-08 07:28:13 -04:00
esc_attr__( 'Set Featured Image for %1$s.', 'display-featured-image-genesis' ),
2015-04-24 17:02:47 -04:00
esc_attr( $term->name )
);
echo '</p>';
echo '</td>';
2014-12-30 16:27:56 -05:00
echo '</tr>';
}
2015-11-17 14:03:41 -05:00
/**
* 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 2.0.0
*/
public function save_taxonomy_custom_meta( $term_id ) {
2015-12-08 09:29:06 -05:00
if ( ! isset( $_POST['displayfeaturedimagegenesis'] ) ) {
2015-11-17 14:03:41 -05:00
return;
}
2015-12-08 09:29:06 -05:00
$input = $_POST['displayfeaturedimagegenesis'];
2015-11-24 10:11:55 -05:00
$displaysetting = get_option( "displayfeaturedimagegenesis_$term_id", false );
2015-11-19 14:36:22 -05:00
$action = function_exists( 'get_term_meta' ) ? 'update_term_meta' : 'update_options_meta';
$this->$action( $term_id, $input, $displaysetting );
2015-11-19 09:58:16 -05:00
}
2015-11-19 14:36:22 -05:00
/**
* update/delete term meta
* @param int $term_id term id
* @param array $displaysetting old option, if it exists
* @return term_meta
*
* @since 2.4.0
*/
protected function update_term_meta( $term_id, $input, $displaysetting ) {
$new_image = $this->validate_taxonomy_image( $input['term_image'] );
2015-11-19 09:58:16 -05:00
if ( null === $new_image ) {
// if the new image is empty, delete term_meta and old option
delete_term_meta( $term_id, 'displayfeaturedimagegenesis' );
delete_option( "displayfeaturedimagegenesis_$term_id" );
2015-11-19 14:36:22 -05:00
} elseif ( false !== $new_image ) {
// if the new image is different from the existing term meta
2015-11-19 09:58:16 -05:00
$current_setting = get_term_meta( $term_id, 'displayfeaturedimagegenesis' );
if ( $current_setting !== $new_image ) {
2015-11-17 17:46:38 -05:00
update_term_meta( $term_id, 'displayfeaturedimagegenesis', (int) $new_image );
2015-11-19 09:58:16 -05:00
}
// if there is a valid image, and the old setting exists
if ( $displaysetting ) {
2015-11-17 17:46:38 -05:00
delete_option( "displayfeaturedimagegenesis_$term_id" );
2015-11-17 16:37:22 -05:00
}
2015-11-19 09:58:16 -05:00
}
}
2015-11-19 14:36:22 -05:00
/**
* update term option (for sites running WP below 4.4)
* @param int $term_id term id
* @param array $displaysetting previous option, if it exists
* @return array updated option
*
* @since 2.4.0
*/
protected function update_options_meta( $term_id, $input, $displaysetting ) {
$cat_keys = array_keys( $input );
2015-11-19 09:58:16 -05:00
$is_updated = false;
foreach ( $cat_keys as $key ) {
2015-11-19 14:36:22 -05:00
if ( isset( $input[ $key ] ) ) {
$displaysetting[ $key ] = $input[ $key ];
if ( $input['term_image'] !== $displaysetting[ $key ] ) {
2015-11-19 09:58:16 -05:00
break;
}
2015-11-19 14:36:22 -05:00
$displaysetting[ $key ] = $this->validate_taxonomy_image( $input[ $key ] );
2015-11-19 09:58:16 -05:00
if ( null === $displaysetting[ $key ] ) {
delete_option( "displayfeaturedimagegenesis_$term_id" );
2015-11-19 14:36:22 -05:00
} elseif ( false !== $displaysetting[ $key ] ) {
2015-11-19 09:58:16 -05:00
$is_updated = true;
2015-11-17 14:03:41 -05:00
}
}
2015-11-19 09:58:16 -05:00
}
// Save the option array.
if ( $is_updated ) {
update_option( "displayfeaturedimagegenesis_$term_id", $displaysetting );
2015-11-17 14:03:41 -05:00
}
}
/**
* 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 2.0.0
*/
2015-11-19 14:36:22 -05:00
protected function validate_taxonomy_image( $new_value ) {
2015-11-17 14:03:41 -05:00
2015-11-20 18:18:36 -05:00
if ( ! $new_value ) {
return null;
}
2015-11-19 14:36:22 -05:00
$medium = get_option( 'medium_size_w' );
$source = wp_get_attachment_image_src( $new_value, 'full' );
$valid = $this->is_valid_img_ext( $source[0] );
$width = $source[1];
2015-11-17 14:03:41 -05:00
2015-11-20 18:18:36 -05:00
if ( $valid && $width > $medium ) {
2015-11-19 09:56:27 -05:00
return (int) $new_value;
2015-11-17 14:03:41 -05:00
}
2015-11-19 14:36:22 -05:00
return false;
2015-11-17 14:03:41 -05:00
}
2015-01-10 09:42:54 -05:00
/**
* Help tab for media screen
* @return help tab with verbose information for plugin
*
2015-01-10 10:45:16 -05:00
* @since 2.0.0
2015-01-10 09:42:54 -05:00
*/
public function help() {
$screen = get_current_screen();
2015-08-15 14:07:39 -04:00
$term_help = '<h3>' . __( 'Set Featured Image', 'display-featured-image-genesis' ) . '</h3>';
2015-05-30 09:54:32 -04:00
$term_help .= '<p>' . __( 'You may set a featured image for your terms. This image will be used on the term archive page, and as a fallback image on a single post page if it does not have a featured image of its own.', 'display-featured-image-genesis' ) . '</p>';
2015-01-10 09:42:54 -05:00
$screen->add_help_tab( array(
'id' => 'displayfeaturedimage_term-help',
'title' => __( 'Featured Image', 'display-featured-image-genesis' ),
'content' => $term_help,
) );
}
2015-08-08 07:28:13 -04:00
/**
* Create new term meta record for split terms.
*
* When WordPress splits terms, ensure that the term meta gets preserved for the newly created term.
*
* @since 2.3.0
*
* @param integer @old_term_id The ID of the term being split.
* @param integer @new_term_id The ID of the newly created term.
*
*/
2015-11-17 16:37:22 -05:00
public function split_shared_term( $old_term_id, $new_term_id ) {
2015-08-08 07:28:13 -04:00
$old_setting = get_option( "displayfeaturedimagegenesis_$old_term_id" );
$new_setting = get_option( "displayfeaturedimagegenesis_$new_term_id" );
if ( ! isset( $old_setting ) ) {
return;
}
$new_setting = $old_setting;
update_option( "displayfeaturedimagegenesis_$new_term_id", $new_setting );
}
2015-01-01 17:28:19 -05:00
}