Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions widget.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

<p>{{{mode-input}}}</p>

<p>{{{suffix-input}}}</p>

<p>{{{taxonomies-label}}}</p>

<ul class="qmt-taxonomies">
Expand Down
96 changes: 59 additions & 37 deletions widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static function init( $file ) {
parent::init( __CLASS__, $file, 'taxonomy-drill-down' );

if ( !class_exists( 'Mustache' ) )
require dirname(__FILE__) . '/mustache/Mustache.php';
require dirname( __FILE__ ) . '/mustache/Mustache.php';

add_action( 'load-widgets.php', array( __CLASS__, '_init' ) );
}
Expand Down Expand Up @@ -62,24 +62,31 @@ function form( $instance ) {

$data = array(
'title-input' => $this->input( array(
'name' => 'title',
'type' => 'text',
'desc' => __( 'Title:', 'query-multiple-taxonomies' ),
'extra' => array( 'class' => 'widefat' )
), $instance ),
'name' => 'title',
'type' => 'text',
'desc' => __( 'Title:', 'query-multiple-taxonomies' ),
'extra' => array( 'class' => 'widefat' )
), $instance ),

'mode-input' => $this->input( array(
'type' => 'select',
'name' => 'mode',
'values' => array(
'lists' => __( 'lists', 'query-multiple-taxonomies' ),
'checkboxes' => __( 'checkboxes', 'query-multiple-taxonomies' ),
'dropdowns' => __( 'dropdowns', 'query-multiple-taxonomies' ),
),
'text' => false,
'desc' => __( 'Mode:', 'query-multiple-taxonomies' ),
'extra' => array( 'class' => 'widefat' )
), $instance ),
'type' => 'select',
'name' => 'mode',
'values' => array(
'lists' => __( 'lists', 'query-multiple-taxonomies' ),
'checkboxes' => __( 'checkboxes', 'query-multiple-taxonomies' ),
'dropdowns' => __( 'dropdowns', 'query-multiple-taxonomies' ),
),
'text' => false,
'desc' => __( 'Mode:', 'query-multiple-taxonomies' ),
'extra' => array( 'class' => 'widefat' )
), $instance ),

'suffix-input' => $this->input( array(
'name' => 'suffix',
'type' => 'text',
'desc' => __( 'Template Suffix (optional):', 'query-multiple-taxonomies' ),
'extra' => array( 'class' => 'widefat' )
), $instance ),

'taxonomies-label' => __( 'Taxonomies:', 'query-multiple-taxonomies' )
);
Expand All @@ -104,17 +111,17 @@ function form( $instance ) {
$data['taxonomies'][] = array(
'title' => sprintf( _n( 'Post type: %s', 'Post types: %s', count( $tax_obj->object_type ), 'query-multiple-taxonomies' ), implode( ', ', $tax_obj->object_type ) ),
'input' => $this->input( array(
'type' => 'checkbox',
'name' => 'taxonomies[]',
'value' => $tax_name,
'checked'=> in_array( $tax_name, $selected_taxonomies ),
'desc' => $tax_obj->label,
) )
'type' => 'checkbox',
'name' => 'taxonomies[]',
'value' => $tax_name,
'checked'=> in_array( $tax_name, $selected_taxonomies ),
'desc' => $tax_obj->label,
) )
);
}

$m = new Mustache;
echo $m->render( file_get_contents( dirname(__FILE__) . '/widget.html' ), $data );
echo $m->render( file_get_contents( dirname( __FILE__ ) . '/widget.html' ), $data );
}

function content( $instance ) {
Expand All @@ -131,9 +138,10 @@ function content( $instance ) {
html( 'p', __( 'No taxonomies selected!', 'query-multiple-taxonomies' ) );
} else {
echo call_user_func( array( __CLASS__, "generate_$mode" ), $taxonomies, array(
'reset-text' => __( 'Reset', 'query-multiple-taxonomies' ),
'reset-url' => QMT_URL::get(),
) );
'reset-text' => __( 'Reset', 'query-multiple-taxonomies' ),
'reset-url' => QMT_URL::get(),
'suffix' => $suffix,
) );
}
}

Expand Down Expand Up @@ -177,10 +185,10 @@ private function generate_lists( $taxonomies, $data ) {

private function generate_dropdowns( $taxonomies, $data ) {
$data = array_merge( $data, array(
'base-url' => QMT_URL::get_base(),
'submit-text' => __( 'Submit', 'query-multiple-taxonomies' ),
'any-text' => '&mdash; ' . __( 'any', 'query-multiple-taxonomies' ) . ' &mdash;',
) );
'base-url' => QMT_URL::get_base(),
'submit-text' => __( 'Submit', 'query-multiple-taxonomies' ),
'any-text' => '&mdash; ' . __( 'any', 'query-multiple-taxonomies' ) . ' &mdash;',
) );

foreach ( $taxonomies as $taxonomy ) {
$terms = get_terms( $taxonomy );
Expand All @@ -205,9 +213,9 @@ private function generate_dropdowns( $taxonomies, $data ) {

private function generate_checkboxes( $taxonomies, $data ) {
$data = array_merge( $data, array(
'base-url' => QMT_URL::get_base(),
'submit-text' => __( 'Submit', 'query-multiple-taxonomies' ),
) );
'base-url' => QMT_URL::get_base(),
'submit-text' => __( 'Submit', 'query-multiple-taxonomies' ),
) );

foreach ( $taxonomies as $taxonomy ) {
$terms = $this->get_terms( $taxonomy );
Expand Down Expand Up @@ -237,12 +245,26 @@ static function test_tax( $tax_name ) {
}

static function mustache_render( $file, $data ) {
$template_path = locate_template( 'qmt-templates/' . $file );
$templates = array( 'qmt-templates/' . $file );

// use template suffix if available
if ( is_array( $data ) && !empty( $data['suffix'] ) ) {
// split the base template into its components and throw away the
// dirname if it is '/' or '.'
extract( pathinfo( $templates[0] ) );
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think replacing all that with a call to sanitize_title_with_dashes( $data['suffix'] ) would be both cleaner and more secure.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or rather, call sanitize_title_with_dashes(), and construct the $templates array later, instead of constructing and then destructuring it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, and probably even better would be to use sanitize_file_name, I'll adjust and commit.

$dir = ( trim( $dirname, './' ) ) ? $dirname . '/' : '';
$suffix = ltrim( $data['suffix'], '-' );

// add new template name to the beginning of the templates list
array_unshift( $templates, "{$dir}{$filename}-{$suffix}.{$extension}" );
}


$template_path = locate_template( $templates );
if ( !$template_path )
$template_path = dirname(__FILE__) . '/templates/' . $file;
$template_path = dirname( __FILE__ ) . '/templates/' . $file;

$m = new Mustache;
return $m->render( file_get_contents( $template_path ), $data );
}
}