Skip to content
This repository has been archived by the owner on Sep 18, 2019. It is now read-only.

Commit

Permalink
Merge pull request #6 from robincornett/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
robincornett committed Oct 30, 2014
2 parents f9f0ccf + 922149c commit eeddcba
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 46 deletions.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

WordPress plugin that adds a widget to display Featured Custom Post Types for the Genesis Framework

## Description
## Description

WordPress plugin that adds a widget to display Featured Custom Post Types for the Genesis Framework. Supports Custom Taxonomies.

Expand All @@ -12,6 +12,7 @@ Genesis 2.0+

## Credits
Most of the code in this plugin is from the <a href="http://www.studiopress.com/">StudioPress</a> Genesis Featured Post Widget and I've just added Custom Post Type Support.

Thanks to <a href="https://github.com/ahnlak">Pete Favelle</a> for adding Custom Taxonomy support.

## Installation
Expand Down Expand Up @@ -39,3 +40,17 @@ Using git, browse to your /wp-content/plugins/ directory and clone this reposito
git clone [email protected]:calliaweb/genesis-featured-custom-post-type-widget.git

Then go to your Plugins screen and click Activate.

### Credits
* [Jo Waltham](http://calliaweb.co.uk/)
* with help from [Pete Favelle](https://github.com/ahnlak)
* and [Robin Cornett](http://robincornett.com)

### Changelog

#### 1.2.0
* new option for CPT archive link
* set ajax to load conditionally

#### 1.0.0
* initial release on GH
10 changes: 5 additions & 5 deletions featured-custom-post-type-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Plugin Name: Featured Custom Post Type Widget for Genesis
* Plugin URI: http://calliaweb.co.uk/featured-custom-post-type-widget-genesis/
* Description: Widget to Display Featured Custom Post Types - uses code from Genesis Featured Post Widget and adds support for custom post types and custom taxonomies
* Version: 1.1.1
* Version: 1.2.0
* Author: Jo Waltham
* Author URI: http://calliaweb.co.uk/
* Text Domain: featured-custom-post-type-widget-for-genesis
Expand All @@ -19,14 +19,14 @@
*/

// if this file is called directly abort
if ( ! defined('WPINC' )) {
if ( ! defined('WPINC' ) ) {
die;
}

// Register the widget
add_action( 'widgets_init', 'gfcptw_register_widget' );
function gfcptw_register_widget() {
register_widget( 'Genesis_Featured_Custom_Post_Type');
register_widget( 'Genesis_Featured_Custom_Post_Type' );
}

require plugin_dir_path( __FILE__ ) . 'includes/class-featured-custom-post-type-widget-registrations.php';
93 changes: 53 additions & 40 deletions includes/class-featured-custom-post-type-widget-registrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,21 @@
* Featured Custom Post Type Widget For Genesis
*
* @package FeaturedCustomPostTypeWidgetForGenesis
* @author StudioPress
* @author Jo Waltham
* @author StudioPress
* @author Jo Waltham
* @author Pete Favelle
* @author Robin Cornett
* @license GPL-2.0+
*
*/

/**
* Register Featured Custom Post Type Widget For Genesis
*
* @package FeaturedCustomPostTypeWidgetForGenesis
* @author StudioPress
* @author Jo Waltham
* @author Pete Favelle
*/


/**
* Please note that most of this code is from the Genesis Featured Post Widget included in the Genesis Framework.
* I have just added support for Custom Post Types.
* Pete has added support for Custom Taxonomies.
*/


class Genesis_Featured_Custom_Post_Type extends WP_Widget {

/**
Expand All @@ -43,7 +35,7 @@ class Genesis_Featured_Custom_Post_Type extends WP_Widget {
function __construct() {

$this->defaults = array(
'title' => '',
'title' => '',
'post_type' => 'post',
'tax_term' => '',
'posts_num' => 1,
Expand All @@ -67,6 +59,8 @@ function __construct() {
'extra_title' => '',
'more_from_category' => '',
'more_from_category_text' => __( 'More Posts from this Category', 'genesis-featured-custom-post-type-widget' ),
'archive_link' => '',
'archive_text' => __( 'View Custom Post Type Archive', 'genesis-featured-custom-post-type-widget' ),
);

$widget_ops = array(
Expand Down Expand Up @@ -166,12 +160,12 @@ function widget( $args, $instance ) {
echo genesis_html5() ? '<header class="entry-header">' : '';

if ( ! empty( $instance['show_title'] ) ) {

if ( genesis_html5() )
printf( '<h2 class="entry-title"><a href="%s" title="%s">%s</a></h2>', get_permalink(), the_title_attribute( 'echo=0' ), get_the_title() );
else
printf( '<h2><a href="%s" title="%s">%s</a></h2>', get_permalink(), the_title_attribute( 'echo=0' ), get_the_title() );

}

if ( ! empty( $instance['show_byline'] ) && ! empty( $instance['post_info'] ) )
Expand Down Expand Up @@ -261,7 +255,7 @@ function widget( $args, $instance ) {
wp_reset_query();
}

if ( ! empty( $instance['more_from_category'] )
if ( ! empty( $instance['more_from_category'] )
&& ( 'category' == substr( $instance['tax_term'], 0, 8 ) ) ) {
$post_cat = get_cat_ID( substr( $instance['tax_term'], 9 ) );
printf(
Expand All @@ -272,6 +266,15 @@ function widget( $args, $instance ) {
);
}

if ( ! empty( $instance['archive_link'] ) && ! empty( $instance['archive_text'] ) ) {
printf(
'<p class="more-from-category"><a href="%1$s">%2$s</a></p>',
get_post_type_archive_link( $instance['post_type'] ),
esc_html( $instance['archive_text'] )
);

}

echo $after_widget;

}
Expand Down Expand Up @@ -309,18 +312,18 @@ function form( $instance ) {

//* Merge with defaults
$instance = wp_parse_args( (array) $instance, $this->defaults );

// Fetch a list of possible post types
$args = array(
'public' => true,
'public' => true,
'_builtin' => false,
);
$output = 'names';
$operator = 'and';
$post_type_list = get_post_types( $args, $output, $operator );
// Add posts to that post_type_list
$post_type_list[ 'post' ] = 'post';

// Add posts to that post_type_list
$post_type_list['post'] = 'post';

// And a list of available taxonomies for the current post type
if ( 'any' == $instance['post_type'] ) {
Expand All @@ -330,8 +333,8 @@ function form( $instance ) {
}

// And from there, a list of available terms in that tax
$tax_args = array(
'hide_empty' => 0,
$tax_args = array(
'hide_empty' => 0,
);
$tax_term_list = get_terms( $taxonomies, $tax_args );
usort( $tax_term_list, array( $this, 'tax_term_compare' ) );
Expand All @@ -345,20 +348,20 @@ function form( $instance ) {
<div class="genesis-widget-column">

<div class="genesis-widget-column-box genesis-widget-column-box-top">

<p>
<label for="<?php echo $this->get_field_id( 'post_type' ); ?>"><?php _e( 'Post Type', 'genesis-featured-custom-post-type-widget' ); ?>:</label>
<select id="<?php echo $this->get_field_id( 'post_type' ); ?>" name="<?php echo $this->get_field_name( 'post_type' ); ?>" onchange="tax_term_postback('<?php echo $this->get_field_id( 'tax_term' ); ?>', this.value);" >

<?php
foreach ( $post_type_list as $post_type_item )
echo '<option style="padding-right:10px;" value="'. esc_attr( $post_type_item ) .'" '. selected( esc_attr( $post_type_item ), $instance['post_type'], false ) .'>'. esc_attr( $post_type_item ) .'</option>';
foreach ( $post_type_list as $post_type_item )
echo '<option style="padding-right:10px;" value="'. esc_attr( $post_type_item ) .'" '. selected( esc_attr( $post_type_item ), $instance['post_type'], false ) .'>'. esc_attr( $post_type_item ) .'</option>';

echo '<option style="padding-right:10px;" value="any" '. selected( 'any', $instance['post_type'], false ) .'>'. __( 'any', 'genesis-featured-custom-post-type-widget' ) .'</option>';
?>
echo '<option style="padding-right:10px;" value="any" '. selected( 'any', $instance['post_type'], false ) .'>'. __( 'any', 'genesis-featured-custom-post-type-widget' ) .'</option>';
?>
</select>
</p>

<p>
<label for="<?php echo $this->get_field_id( 'tax_term' ); ?>"><?php _e( 'Category/Term', 'genesis-featured-custom-post-type-widget' ); ?>:</label>
<select id="<?php echo $this->get_field_id( 'tax_term' ); ?>" name="<?php echo $this->get_field_name( 'tax_term' ); ?>">
Expand All @@ -367,11 +370,11 @@ function form( $instance ) {
foreach ( $tax_term_list as $tax_term_item ) {
$tax_term_desc = $tax_term_item->taxonomy . '/' . $tax_term_item->name;
$tax_term_slug = $tax_term_item->taxonomy . '/' . $tax_term_item->slug;
echo '<option style="padding-right:10px;" value="'. esc_attr( $tax_term_slug ) .'" '. selected( esc_attr( $tax_term_slug ), $instance['tax_term'], false ) .'>'. esc_attr( $tax_term_desc ) .'</option>';
echo '<option style="padding-right:10px;" value="'. esc_attr( $tax_term_slug ) .'" '. selected( esc_attr( $tax_term_slug ), $instance['tax_term'], false ) .'>'. esc_attr( $tax_term_desc ) .'</option>';
}

echo '<option style="padding-right:10px;" value="any" '. selected( 'any', $instance['tax_term'], false ) .'>'. __( 'any', 'genesis-featured-custom-post-type-widget' ) .'</option>';
?>
echo '<option style="padding-right:10px;" value="any" '. selected( 'any', $instance['tax_term'], false ) .'>'. __( 'any', 'genesis-featured-custom-post-type-widget' ) .'</option>';
?>
</select>
</p>

Expand Down Expand Up @@ -451,9 +454,8 @@ function form( $instance ) {
<p>
<label for="<?php echo $this->get_field_id( 'image_size' ); ?>"><?php _e( 'Image Size', 'genesis-featured-custom-post-type-widget' ); ?>:</label>
<select id="<?php echo $this->get_field_id( 'image_size' ); ?>" class="genesis-image-size-selector" name="<?php echo $this->get_field_name( 'image_size' ); ?>">
<option value="thumbnail">thumbnail (<?php echo get_option( 'thumbnail_size_w' ); ?>x<?php echo get_option( 'thumbnail_size_h' ); ?>)</option>
<?php
$sizes = genesis_get_additional_image_sizes();
$sizes = genesis_get_image_sizes();
foreach( (array) $sizes as $name => $size )
echo '<option value="'.esc_attr( $name ).'" '.selected( $name, $instance['image_size'], FALSE ).'>'.esc_html( $name ).' ( '.$size['width'].'x'.$size['height'].' )</option>';
?>
Expand Down Expand Up @@ -539,6 +541,15 @@ function form( $instance ) {
<input type="text" id="<?php echo $this->get_field_id( 'more_from_category_text' ); ?>" name="<?php echo $this->get_field_name( 'more_from_category_text' ); ?>" value="<?php echo esc_attr( $instance['more_from_category_text'] ); ?>" class="widefat" />
</p>

<p>
<input id="<?php echo $this->get_field_id( 'archive_link' ); ?>" type="checkbox" name="<?php echo $this->get_field_name( 'archive_link' ); ?>" value="1" <?php checked( $instance['archive_link'] ); ?>/>
<label for="<?php echo $this->get_field_id( 'archive_link' ); ?>"><?php _e( 'Show Archive Link', 'genesis-featured-sermon-widget' ); ?></label>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'archive_text' ); ?>"><?php _e( 'Link Text', 'genesis-featured-custom-post-type-widget' ); ?>:</label>
<input type="text" id="<?php echo $this->get_field_id( 'archive_text' ); ?>" name="<?php echo $this->get_field_name( 'archive_text' ); ?>" value="<?php echo esc_attr( $instance['archive_text'] ); ?>" class="widefat" />
</p>

</div>

</div>
Expand All @@ -562,9 +573,12 @@ function tax_term_compare( $a, $b ) {
* Enqueues the small bit of Javascript which will handle the Ajax
* callback to correctly populate the custom term dropdown.
*/
function admin_enqueue( $hook ) {
wp_enqueue_script( 'tax-term-ajax-script', plugins_url( '/ajax_handler.js', __FILE__ ), array('jquery') );
wp_localize_script( 'tax-term-ajax-script', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
function admin_enqueue() {
$screen = get_current_screen()->id;
if ( $screen === 'widgets' || $screen === 'customize' ) {
wp_enqueue_script( 'tax-term-ajax-script', plugins_url( '/ajax_handler.js', __FILE__ ), array('jquery') );
wp_localize_script( 'tax-term-ajax-script', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
}

/**
Expand All @@ -582,7 +596,7 @@ function tax_term_action_callback() {
}

// And from there, a list of available terms in that tax
$tax_args = array(
$tax_args = array(
'hide_empty' => 0,
);
$tax_term_list = get_terms( $taxonomies, $tax_args );
Expand All @@ -599,4 +613,3 @@ function tax_term_action_callback() {
die();
}
}

0 comments on commit eeddcba

Please sign in to comment.