Skip to content

Commit

Permalink
Issue #3 - add bw_terms shortcode to display the taxonomy term list
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbingwide committed Feb 3, 2017
1 parent 2d90b5e commit bbae9a0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 15 deletions.
24 changes: 20 additions & 4 deletions includes/class-oik-a2z-display.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
class OIK_a2z_display {
public $taxonomy;
public $post_type;
public $atts;
public $count;

function __construct() {
}
Expand All @@ -19,16 +21,26 @@ function __construct() {
* Displays links for the selected taxonomy
*
*/
function display( $taxonomy="letter" ) {
function display( $taxonomy="letter", $atts=array() ) {
if ( '' === $taxonomy ) {
$taxonomy = "letter";
}
$this->taxonomy = $taxonomy;
$this->parse_atts( $atts );
$terms = $this->get_terms();
$this->display_term_list( $terms );
//$this->enqueue_styles();
}

/**
* Parses the attributes
*
*/
function parse_atts( $atts ) {
$this->atts = $atts;
$this->count = bw_validate_torf( bw_array_get( $atts, "count", false ) );
}

/**
* Determines the active term
*
Expand Down Expand Up @@ -61,6 +73,8 @@ function query_term_class( $term ) {

/**
* Retrieves the terms for the taxonomy
*
*
*/
function get_terms() {
$args = array( "taxonomy" => $this->taxonomy
Expand Down Expand Up @@ -126,9 +140,11 @@ function display_term_item( $term ) {
*/
function term_string( $term ) {
$term_string = $term->name;
$term_string .= retstag( "span", "count" );
$term_string .= $term->count;
$term_string .= retetag( "span" );
if ( $this->count ) {
$term_string .= retstag( "span", "count" );
$term_string .= $term->count;
$term_string .= retetag( "span" );
}
return( $term_string );
}

Expand Down
19 changes: 8 additions & 11 deletions oik-a2z.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@
function oik_a2z_loaded() {
add_action( "init", "oik_a2z_init" );
add_action( "oik_fields_loaded", "oik_a2z_oik_fields_loaded" );
add_action( "oik_a2z_display", "oik_a2z_display", 10 );
add_action( "oik_a2z_display", "oik_a2z_display", 10, 2 );
add_action( "run_oik-a2z.php", "oik_a2z_run_oik_a2z" );
// Add further batch files if needed
//add_action( "run_filename", "oik_a2z_run_filename" );

//add_action( "oik_add_shortcodes", "oik_a2z_oik_add_shortcodes" );
add_action( "oik_add_shortcodes", "oik_a2z_oik_add_shortcodes" );
}

/**
Expand Down Expand Up @@ -69,11 +68,12 @@ function oik_a2z_oik_fields_loaded() {
* Use this action to display links for all the categories in the selected taxonomy.
*
* @param string $taxonomy
* @param array $atts - additional parameters - see [bw_terms] shortcode
*/
function oik_a2z_display( $taxonomy="letter" ) {
function oik_a2z_display( $taxonomy="letter", $atts=array() ) {
oik_require( "includes/class-oik-a2z-display.php", "oik-a2z" );
$oik_a2z_display = new OIK_a2z_display();
$oik_a2z_display->display( $taxonomy );
$oik_a2z_display->display( $taxonomy, $atts );
}

/**
Expand Down Expand Up @@ -151,25 +151,22 @@ function oik_a2z_run_oik_a2z() {
oik_a2z_lazy_run_oik_a2z();
}


/**
* Implements 'wp_insert_post' action for oik-a2z
*
* Lazy loads the logic when the request contains taxonomy term input.
*
* @param ID $post_ID ID of the post
* @param object $post the post object
* @param bool $update true if it's an update
*/
function oik_a2z_wp_insert_post( $post_ID, $post, $update ) {

if ( "auto-draft" !== $post->post_status && isset( $_REQUEST['tax_input'] ) ) {
oik_require( "admin/oik-a2z-letters.php", "oik-a2z" );
oik_a2z_set_letter_taxonomies( $post_ID, $post, $update );
}

}



/**
* Implement "oik_a2z_query_terms_$post_type_$taxonomy" with default logic
*
Expand Down Expand Up @@ -253,7 +250,7 @@ function oik_a2z_query_term_ids( $term_names, $taxonomy ) {
* Implement "oik_add_shortcodes" to add our own shortcodes
*/
function oik_a2z_oik_add_shortcodes() {
bw_add_shortcode( 'bw_terms', 'oik_a2z_terms', oik_path( "shortcodes/oik-a2z-terms.php", "oik-a2z" ) );
bw_add_shortcode( 'bw_terms', 'oik_a2z_terms', oik_path( "shortcodes/oik-a2z-terms.php", "oik-a2z" ), false );
}


Expand Down
46 changes: 46 additions & 0 deletions shortcodes/oik-a2z-terms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php // (C) Copyright Bobbing Wide 2017

/**
* Implements [bw_terms] shortcode
*
* The oik_a2z_display action echos the output
* This is not what we want for a shortcode.
* So we can either use output buffer logic or change the action.d
*
* @param array $atts shortcode parameters
* @param string $content not expected
* @param string $tag - shortcode tag
* @return string generated HTML
*/
function oik_a2z_terms( $atts, $content, $tag ) {
$taxonomy = bw_array_get_from( $atts, "taxonomy,0", "letter" );
ob_start();
do_action( "oik_a2z_display", $taxonomy, $atts );
$contents = ob_get_contents();
ob_end_clean();
return( $contents );
}

/**
* Help hook for bw_terms
*/
function bw_terms__help( $shortcode="bw_terms" ) {
return( "Display taxonomy terms links" );
}


/**
* Syntax hook for bw_terms
*
* @TODO Do we need orderby and order parms?
*
*/
function bw_terms__syntax( $shortcode="bw_terms" ) {
$syntax = array( "taxonomy" => bw_skv( "letter", "category|post_tag|<i>taxonomy_name</i>", "Taxonomy to display" )
, "count" => bw_skv( false, true, "Include term count" )
, "class" => bw_skv( "class", "<i>text</i>", "CSS class names" )
);
return( $syntax );
}

0 comments on commit bbae9a0

Please sign in to comment.