[wp-trac] [WordPress Trac] #17276: custom post type problems needs addressed
WordPress Trac
wp-trac at lists.automattic.com
Sun May 1 20:27:29 UTC 2011
#17276: custom post type problems needs addressed
-------------------------+------------------------------
Reporter: squeeky | Owner: squeeky
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Taxonomy | Version:
Severity: normal | Resolution:
Keywords: needs-patch |
-------------------------+------------------------------
Comment (by squeeky):
Just for "Ha Ha's" I think I figured out the flush thing > not good, but I
think I figured it out / how to auto-flush - ONLY WHEN 'NEEDED'
.....................
here's some code as a sample of the IDEA I am speaking of - It is the idea
that is IMPORTANT - not the code - not the example - but the idea
it is a plugin which if better coded could replace the current Categories
widget > so that IF custom hierarchy taxonomies exist it then presents the
option to use for them...
-- please don't be distracted by my rants within the comments...
{{{
<?php
/*
*
* @file: post-type-categories-widget.php
*
*
Plugin Name: Post Type Categories Widget Test
Plugin URI: http://mywebwizards.com/wordpress-tests/plugins/post-type-
categories-widget/
Description: This is just a plugin for testing - it symbolizes the hope
and enthusiasm of an entire custom post type generation summed up in one
widget supplied by WordPress and altered by squeeky: When activated you
will see a new widget named <cite>Post Type Categories</cite> in the
"Appearence" > "Widgets" page.
Author: squeeky
Version: 0.1
Author URI: http://mywebwizards.com/
______________________________________________
**********************************************
Categories widget
for relavant Taxonomies
of ALL relavant Post Types */
class Post_Type_Categories_Widget extends WP_Widget {
function Post_Type_Categories_Widget() {
$widget_ops = array( 'classname' => 'widget_categories',
'description' => __( "A list or dropdown of categories / and optiionally
post type categories" ) );
$this->WP_Widget('post_type_categories', __('* Post Type
Categories'), $widget_ops);
}
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters('widget_title', empty(
$instance['title'] ) ? __( 'Post Type Categories' ) : $instance['title'],
$instance, $this->id_base);
$pt_cats = $instance['ptcats']; // ****** $pt_cats
$instance ******
$c = $instance['count'] ? '1' : '0';
$h = $instance['hierarchical'] ? '1' : '0';
$d = $instance['dropdown'] ? '1' : '0';
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
// In case past taxonomy was deactivated
if ( taxonomy_exists($pt_cats) )
$pt_cat_tax = $pt_cats;
else
$pt_cat_tax = 'category';
$cat_args = array('taxonomy' => $pt_cat_tax, 'orderby' =>
'name', 'show_count' => $c, 'hierarchical' => $h);
if ( $d ) {
$cat_args['show_option_none'] = __('Select
Category');
wp_dropdown_categories(apply_filters('widget_categories_dropdown_args',
$cat_args));
?>
<script type='text/javascript'>
/* <![CDATA[ */
var dropdown = document.getElementById("cat");
function onCatChange() {
if ( dropdown.options[dropdown.selectedIndex].value > 0 )
{
location.href = "<?php echo home_url();
?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
}
}
dropdown.onchange = onCatChange;
/* ]]> */
</script>
<?php
} else {
?>
<ul>
<?php
$cat_args['title_li'] = '';
wp_list_categories(apply_filters('widget_categories_args',
$cat_args));
?>
</ul>
<?php
}
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['ptcats'] = $new_instance['ptcats']; // ******
$pt_cats $instance ******
$instance['count'] = !empty($new_instance['count']) ? 1 :
0;
$instance['hierarchical'] =
!empty($new_instance['hierarchical']) ? 1 : 0;
$instance['dropdown'] = !empty($new_instance['dropdown'])
? 1 : 0;
return $instance;
}
function form( $instance ) {
//Defaults
$instance = wp_parse_args( (array) $instance, array(
'title' => '') );
$title = esc_attr( $instance['title'] );
$pt_cats = esc_attr( $instance['ptcats'] ); // ******
$pt_cats $instance ******
$count = isset($instance['count']) ? (bool)
$instance['count'] :false;
$hierarchical = isset( $instance['hierarchical'] ) ?
(bool) $instance['hierarchical'] : false;
$dropdown = isset( $instance['dropdown'] ) ? (bool)
$instance['dropdown'] : false;
?>
<p><label for="<?php echo $this->get_field_id('title');
?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo
$this->get_field_id('title'); ?>" name="<?php echo
$this->get_field_name('title'); ?>" type="text" value="<?php echo $title;
?>" /></p>
<?php
// set arguments for custom post types
$args=array(
'public' => true,
'_builtin' => false,
);
/* ______________________________________________
**********************************************
Probe custom post types and related taxonomies
* This function arrangement, or something in this
direction,
* should really be deeper within the WordPress core,
* ( perhaps in post.php and/or taxonomy.php / depending )
* where it could be reached by "WordPress features" or
even
* plugins and themes that could be utilized by custom
post
* types and/or custom taxonomies.
*
* The idea behind this function arrangement should be
* expanded upon to determine other aspects of custom
* post types and custom taxonomies that may be easily
* intergrated into other existing WordPress, plugin,
* or theme features where such would make sense -
* like a sitemap template for example - ETC., ETC.
*
* Note on function's current state :
* I probably could have gone straight to the taxonomies,
* but I wanted to illustrate how a broad gathering of
* custom post types could be narrowed down to a specific
* feature - to hint at the notion of how other
* custom post type aspects and custom taxonomy aspects
* could be focused upon - and "pulled" where desired.
* This relates to my comment about how 'This function
* should be expanded upon' - perhaps in the format of
* a SWITCH where feature probes could be targeted more
* directly / more efficiantly...
*
*
* Here is some more BRAIN-STORMING (please excuse)...
*
* I guess in a way I am proposing a kind of custom post
* type / taxonomy API - or more accurately, a kind of
* custom post type / taxonomy SDK - making all easier
* for coders who in turn can then make it easier for
* end-users - I mean think about it: wouldn't it be
* cool if you could simple pop up something like:
*
* <?php cpt_info('list') ?>
*
* and/or maybe a member function
*
* <?php this->get_cpt_info('taxonomy') ?>
*
* with alternatively 'ctax_info' and 'get_ctax_info'
* where you could do things like:
*
* <?php if(my_type->get_ctax_info('hierarchical')) : ?>
*
* sort of like 'bloginfo' and 'get_bloginfo' instead of
* sorting through all those functions for this or that.
* OR maybe a custon_post_tax CLASS to reach into......
* (???????????????????????????????????????????????????)
*
* OPPS --- SO SORRY - I'm getting really carried away.
*
* --- Back to the functions at hand....
*/
// IF custom post types exists
if( get_post_types( $args ) ) {
// array to collect custom post-type hierarchical
taxonomies
// - in other words > the "custom post-type
categories"
$custom_post_type_cats = (array)
$custom_post_type_cats;
// 'foreach' custom post-type
foreach( get_post_types($args) as $cptype ) {
// run through 'foreach' custom post-
type's taxonomy
// in case custom post-type has more than
one taxonomy
foreach( get_object_taxonomies($cptype) as
$cptype_tax ) {
// collect the taxonomies that are
hierarchical
if(
is_taxonomy_hierarchical($cptype_tax) )
$custom_post_type_cats[] =
$cptype_tax;
}
}
}
// did we collect any "custom post types categories"
if( $custom_post_type_cats ) {
$current_pt_cats = $instance['ptcats']; // ******
$pt_cats $instance ******
?>
<p><label for="<?php echo $this->get_field_id('ptcats');
?>"><?php _e('Select taxonomy:') ?></label>
<select class="widefat" id="<?php echo
$this->get_field_id('ptcats'); ?>" name="<?php echo
$this->get_field_name('ptcats'); ?>">
<option value="category" <?php
selected('category', $current_pt_cats) ?>><?php _e('categories ( default /
for post )') ?></option>
<?php foreach ( $custom_post_type_cats as $pt_cats ) : ?>
<option value="<?php echo esc_attr($pt_cats) ?>"
<?php selected($pt_cats, $current_pt_cats) ?>><?php echo $pt_cats;
?></option>
<?php endforeach; ?>
</select></p>
<?php
} else {
// set to our default:
$pt_cats = 'category';
}
?>
<p><input type="checkbox" class="checkbox" id="<?php echo
$this->get_field_id('dropdown'); ?>" name="<?php echo
$this->get_field_name('dropdown'); ?>"<?php checked( $dropdown ); ?> />
<label for="<?php echo $this->get_field_id('dropdown');
?>"><?php _e( 'Display as dropdown' ); ?></label><br />
<input type="checkbox" class="checkbox" id="<?php echo
$this->get_field_id('count'); ?>" name="<?php echo
$this->get_field_name('count'); ?>"<?php checked( $count ); ?> />
<label for="<?php echo $this->get_field_id('count');
?>"><?php _e( 'Show post counts' ); ?></label><br />
<input type="checkbox" class="checkbox" id="<?php echo
$this->get_field_id('hierarchical'); ?>" name="<?php echo
$this->get_field_name('hierarchical'); ?>"<?php checked( $hierarchical );
?> />
<label for="<?php echo
$this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy' );
?></label></p>
<?php
}
}
/**
* Register widgets - Then call 'widgets_init' action
* after the widget has been registered.
*
* @since 3.2.0 [ HOPEFULLY ]
*/
function post_type_widgets_init() {
if ( !is_blog_installed() )
return;
register_widget('Post_Type_Categories_Widget');
do_action('widgets_init');
}
add_action('init', 'post_type_widgets_init', 1);
?>
}}}
--
Ticket URL: <http://core.trac.wordpress.org/ticket/17276#comment:10>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list