Files
display-featured-image-genesis/includes/widgets/displayfeaturedimagegenesis-taxonomy-widget.php
T

307 lines
11 KiB
PHP
Raw Normal View History

2015-01-05 17:54:56 -05:00
<?php
/**
2015-01-05 23:27:08 -05:00
* Dependent class to build a featured taxonomy widget
2015-01-05 17:54:56 -05:00
*
2015-01-05 23:27:08 -05:00
* @package DisplayFeaturedImageGenesis
* @author Robin Cornett <hello@robincornett.com>
* @license GPL-2.0+
* @link http://robincornett.com
* @copyright 2014 Robin Cornett Creative, LLC
2015-01-10 10:45:16 -05:00
* @since 2.0.0
2015-01-05 17:54:56 -05:00
*/
/**
2015-01-05 23:27:08 -05:00
* Genesis Featured Taxonomy widget class.
2015-01-05 17:54:56 -05:00
*
2015-01-10 10:45:16 -05:00
* @since 2.0.0
2015-01-05 17:54:56 -05:00
*
*/
2015-02-03 08:44:23 -05:00
class Display_Featured_Image_Genesis_Widget_Taxonomy extends WP_Widget {
2015-01-05 17:54:56 -05:00
/**
* Holds widget settings defaults, populated in constructor.
*
* @var array
*/
protected $defaults;
/**
* Constructor. Set the default widget options and create widget.
*
2015-01-10 10:45:16 -05:00
* @since 2.0.0
2015-01-05 17:54:56 -05:00
*/
function __construct() {
$this->defaults = array(
2015-02-13 17:03:32 -05:00
'title' => '',
'taxonomy' => 'category',
'term' => 'none',
'show_image' => 0,
'image_alignment' => '',
'image_size' => 'medium',
'show_title' => 0,
'show_content' => 0
2015-01-05 17:54:56 -05:00
);
$widget_ops = array(
'classname' => 'featured-term',
2015-01-10 10:35:43 -05:00
'description' => __( 'Displays a term with its featured image', 'display-featured-image-genesis' ),
2015-01-05 17:54:56 -05:00
);
$control_ops = array(
'id_base' => 'featured-taxonomy',
'width' => 505,
'height' => 350,
);
2015-01-10 10:35:43 -05:00
parent::__construct( 'featured-taxonomy', __( 'Display Featured Term Image', 'display-featured-image-genesis' ), $widget_ops, $control_ops );
2015-01-05 17:54:56 -05:00
2015-01-06 11:42:37 -05:00
add_action( 'wp_ajax_widget_selector', array( $this, 'term_action_callback' ) );
2015-01-05 20:32:18 -05:00
2015-01-05 17:54:56 -05:00
}
/**
* Echo the widget content.
*
2015-01-10 10:45:16 -05:00
* @since 2.0.0
2015-01-05 17:54:56 -05:00
*
*
* @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
* @param array $instance The settings for the particular instance of the widget
*/
function widget( $args, $instance ) {
global $wp_query, $_genesis_displayed_ids;
//* Merge with defaults
$instance = wp_parse_args( (array) $instance, $this->defaults );
$term_id = $instance['term'];
2015-01-08 12:32:16 -05:00
$term_meta = get_option( "displayfeaturedimagegenesis_$term_id" );
2015-01-05 17:54:56 -05:00
$term = get_term_by( 'id', $term_id, $instance['taxonomy'] );
2015-01-06 11:42:37 -05:00
if ( ! $term ) {
return;
}
$title = $term->meta['headline'];
if ( ! $title ) {
$title = $term->name;
}
2015-01-05 20:32:18 -05:00
$permalink = get_term_link( $term );
2015-01-05 17:54:56 -05:00
2015-03-03 16:27:46 -05:00
$args['before_widget'] = str_replace( 'class="widget ', 'class="widget ' . $term->slug . ' ', $args['before_widget'] );
2015-01-06 11:42:37 -05:00
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $args['after_title'];
}
2015-01-05 17:54:56 -05:00
if ( $term_meta ) {
2015-03-23 11:51:45 -04:00
$image_id = $term_meta['term_image'];
if ( ! is_numeric( $term_meta['term_image'] ) ) {
$image_id = Display_Featured_Image_Genesis_Common::get_image_id( $term_meta['term_image'] );
}
2015-01-05 17:54:56 -05:00
$image_src = wp_get_attachment_image_src( $image_id, $instance['image_size'] );
2015-01-06 11:42:37 -05:00
if ( $image_src ) {
2015-02-27 10:14:07 -05:00
$image = '<img src="' . esc_url( $image_src[0] ) . '" alt="' . esc_html( $title ) . '" />';
2015-01-06 11:42:37 -05:00
}
2015-01-05 17:54:56 -05:00
2015-01-06 11:42:37 -05:00
if ( $instance['show_image'] && $image ) {
2015-02-13 17:03:32 -05:00
printf( '<a href="%s" title="%s" class="%s">%s</a>', esc_url( $permalink ), esc_html( $title ), esc_attr( $instance['image_alignment'] ), $image );
2015-01-06 11:42:37 -05:00
}
2015-01-05 17:54:56 -05:00
}
2015-01-05 23:27:08 -05:00
if ( $instance['show_title'] ) {
2015-01-05 17:54:56 -05:00
if ( ! empty( $instance['show_title'] ) ) {
if ( genesis_html5() )
2015-02-13 17:03:32 -05:00
printf( '<h2 class="archive-title"><a href="%s">%s</a></h2>', esc_url( $permalink ), esc_html( $title ) );
2015-01-05 17:54:56 -05:00
else
2015-02-13 17:03:32 -05:00
printf( '<h2><a href="%s">%s</a></h2>', esc_url( $permalink ), esc_html( $title ) );
2015-01-05 17:54:56 -05:00
}
2015-01-05 23:27:08 -05:00
}
2015-01-05 17:54:56 -05:00
if ( $instance['show_content'] ) {
2015-01-05 17:54:56 -05:00
2015-01-10 10:26:52 -05:00
echo genesis_html5() ? '<div class="term-description">' : '';
2015-01-05 17:54:56 -05:00
2015-02-16 13:23:00 -05:00
$intro_text = apply_filters( 'display_featured_image_genesis_term_description', $term->meta['intro_text'] );
if ( ! $intro_text ) {
$intro_text = $term->description;
}
2015-01-05 17:54:56 -05:00
echo $intro_text;
echo genesis_html5() ? '</div>' : '';
}
echo $args['after_widget'];
}
/**
* Update a particular instance.
*
* This function should check that $new_instance is set correctly.
* The newly calculated value of $instance should be returned.
* If "false" is returned, the instance won't be saved/updated.
*
2015-01-10 10:45:16 -05:00
* @since 2.0.0
2015-01-05 17:54:56 -05:00
*
* @param array $new_instance New settings for this instance as input by the user via form()
* @param array $old_instance Old settings for this instance
* @return array Settings to save or bool false to cancel saving
*/
function update( $new_instance, $old_instance ) {
$new_instance['title'] = strip_tags( $new_instance['title'] );
return $new_instance;
}
/**
* Echo the settings update form.
*
2015-01-10 10:45:16 -05:00
* @since 2.0.0
2015-01-05 17:54:56 -05:00
*
* @param array $instance Current settings
*/
function form( $instance ) {
//* Merge with defaults
$instance = wp_parse_args( (array) $instance, $this->defaults );
?>
<p>
2015-01-10 14:12:30 -05:00
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'display-featured-image-genesis' ); ?> </label>
<input type="text" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat" />
2015-01-05 17:54:56 -05:00
</p>
<div class="genesis-widget-column">
<div class="genesis-widget-column-box genesis-widget-column-box-top">
<p>
2015-01-10 14:12:30 -05:00
<label for="<?php echo esc_attr( $this->get_field_id( 'taxonomy' ) ); ?>"><?php _e( 'Taxonomy:', 'display-featured-image-genesis' ); ?> </label>
<select id="<?php echo esc_attr( $this->get_field_id( 'taxonomy' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'taxonomy' ) ); ?>" onchange="term_postback('<?php echo esc_attr( $this->get_field_id( 'term' ) ); ?>', this.value);" >
2015-01-05 17:54:56 -05:00
<?php
2015-01-05 23:27:08 -05:00
$args = array(
2015-01-05 20:32:18 -05:00
'public' => true,
'show_ui' => true
2015-01-05 17:54:56 -05:00
);
2015-01-05 23:27:08 -05:00
$taxonomies = get_taxonomies( $args, 'objects' );
2015-01-05 20:32:18 -05:00
foreach ( $taxonomies as $taxonomy ) {
2015-01-10 14:12:30 -05:00
echo '<option value="' . esc_attr( $taxonomy->name ) . '"' . selected( esc_attr( $taxonomy->name ), $instance['taxonomy'], false ) . '>' . esc_attr( $taxonomy->label ) . '</option>';
2015-01-05 20:32:18 -05:00
} ?>
</select>
</p>
<p>
2015-01-10 14:12:30 -05:00
<label for="<?php echo esc_attr( $this->get_field_id( 'term' ) ); ?>"><?php _e( 'Term:', 'display-featured-image-genesis' ); ?> </label>
<select id="<?php echo esc_attr( $this->get_field_id( 'term' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'term' ) ); ?>" >
2015-01-05 20:32:18 -05:00
<?php
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false
);
$terms = get_terms( $instance['taxonomy'], $args );
2015-01-10 14:12:30 -05:00
echo '<option value="none"' . selected( 'none', $instance['term'], false ) . '>--</option>';
2015-01-05 20:32:18 -05:00
foreach ( $terms as $term ) {
2015-01-10 14:12:30 -05:00
echo '<option value="' . esc_attr( $term->term_id ) . '"' . selected( esc_attr( $term->term_id ), $instance['term'], false ) . '>' . esc_attr( $term->name ) . '</option>';
2015-01-05 20:32:18 -05:00
} ?>
</select>
2015-01-05 17:54:56 -05:00
</p>
</div>
<div class="genesis-widget-column-box">
2015-01-05 20:32:18 -05:00
<p>
2015-01-10 14:12:30 -05:00
<input id="<?php echo esc_attr( $this->get_field_id( 'show_title' ) ); ?>" type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_title' ) ); ?>" value="1" <?php checked( $instance['show_title'] ); ?>/>
2015-01-10 10:35:43 -05:00
<label for="<?php echo esc_attr( $this->get_field_id( 'show_title' ) ); ?>"><?php _e( 'Show Term Title', 'display-featured-image-genesis' ); ?></label>
2015-01-05 20:32:18 -05:00
</p>
<p>
2015-01-10 14:12:30 -05:00
<input id="<?php echo esc_attr( $this->get_field_id( 'show_content' ) ); ?>" type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_content' ) ); ?>" value="1" <?php checked( $instance['show_content'] ); ?>/>
2015-01-10 10:35:43 -05:00
<label for="<?php echo esc_attr( $this->get_field_id( 'show_content' ) ); ?>"><?php _e( 'Show Term Intro Text', 'display-featured-image-genesis' ); ?></label>
2015-01-05 20:32:18 -05:00
</p>
</div>
</div>
<div class="genesis-widget-column genesis-widget-column-right">
<div class="genesis-widget-column-box genesis-widget-column-box-top">
2015-01-05 17:54:56 -05:00
<p>
2015-01-10 14:12:30 -05:00
<input id="<?php echo esc_attr( $this->get_field_id( 'show_image' ) ); ?>" type="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'show_image' ) ); ?>" value="1" <?php checked( $instance['show_image'] ); ?>/>
2015-01-10 10:26:52 -05:00
<label for="<?php echo esc_attr( $this->get_field_id( 'show_image' ) ); ?>"><?php _e( 'Show Featured Image', 'display-featured-image-genesis' ); ?></label>
2015-01-05 17:54:56 -05:00
</p>
<p>
2015-01-10 14:12:30 -05:00
<label for="<?php echo esc_attr( $this->get_field_id( 'image_size' ) ); ?>"><?php _e( 'Image Size:', 'display-featured-image-genesis' ); ?> </label>
<select id="<?php echo esc_attr( $this->get_field_id( 'image_size' ) ); ?>" class="genesis-image-size-selector" name="<?php echo esc_attr( $this->get_field_name( 'image_size' ) ); ?>">
2015-01-05 17:54:56 -05:00
<?php
$sizes = genesis_get_image_sizes();
2015-01-10 10:26:52 -05:00
foreach( (array) $sizes as $name => $size ) {
2015-01-10 14:12:30 -05:00
echo '<option value="' . esc_attr( $name ) . '"' . selected( $name, $instance['image_size'], FALSE ) . '>' . esc_html( $name ) . ' ( ' . absint( $size['width'] ) . 'x' . absint( $size['height'] ) . ' )</option>';
2015-01-10 10:26:52 -05:00
} ?>
2015-01-05 17:54:56 -05:00
</select>
</p>
<p>
2015-01-10 10:26:52 -05:00
<label for="<?php echo esc_attr( $this->get_field_id( 'image_alignment' ) ); ?>"><?php _e( 'Image Alignment', 'display-featured-image-genesis' ); ?>:</label>
2015-01-10 14:12:30 -05:00
<select id="<?php echo esc_attr( $this->get_field_id( 'image_alignment' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'image_alignment' ) ); ?>">
2015-01-05 17:54:56 -05:00
<option value="alignnone">- <?php _e( 'None', 'display-featured-image-genesis' ); ?> -</option>
<option value="alignleft" <?php selected( 'alignleft', $instance['image_alignment'] ); ?>><?php _e( 'Left', 'display-featured-image-genesis' ); ?></option>
<option value="alignright" <?php selected( 'alignright', $instance['image_alignment'] ); ?>><?php _e( 'Right', 'display-featured-image-genesis' ); ?></option>
<option value="aligncenter" <?php selected( 'aligncenter', $instance['image_alignment'] ); ?>><?php _e( 'Center', 'display-featured-image-genesis' ); ?></option>
</select>
</p>
</div>
</div>
2015-01-05 20:32:18 -05:00
<?php
2015-01-05 17:54:56 -05:00
2015-01-05 20:32:18 -05:00
}
2015-01-05 17:54:56 -05:00
2015-01-05 20:32:18 -05:00
/**
* Handles the callback to populate the custom term dropdown. The
2015-01-05 23:26:44 -05:00
* selected post type is provided in $_POST['taxonomy'], and the
2015-01-05 20:32:18 -05:00
* calling script expects a JSON array of term objects.
*/
2015-01-05 23:26:44 -05:00
function term_action_callback() {
2015-01-05 17:54:56 -05:00
2015-01-05 20:32:18 -05:00
$args = array(
'orderby' => 'name',
'order' => 'ASC',
2015-01-06 11:42:37 -05:00
'hide_empty' => false
2015-01-05 20:32:18 -05:00
);
$terms = get_terms( $_POST['taxonomy'], $args );
2015-01-05 17:54:56 -05:00
2015-01-05 20:32:18 -05:00
// Build an appropriate JSON response containing this info
2015-01-10 14:12:30 -05:00
$list['none'] = '--';
2015-01-05 20:32:18 -05:00
foreach ( $terms as $term ) {
2015-02-16 13:23:00 -05:00
$list[$term->term_id] = esc_attr( $term->name );
2015-01-05 20:32:18 -05:00
}
2015-01-05 17:54:56 -05:00
2015-01-05 20:32:18 -05:00
// And emit it
2015-03-05 09:36:21 -05:00
if ( function_exists( 'wp_json_encode' ) ) {
echo wp_json_encode( $list );
}
else {
echo json_encode( $list );
}
2015-01-05 20:32:18 -05:00
die();
2015-01-05 17:54:56 -05:00
}
}