Skip to content

Instantly share code, notes, and snippets.

@rgadon107
Last active January 29, 2021 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgadon107/af80d42e020e24f4184f1225c00cbb16 to your computer and use it in GitHub Desktop.
Save rgadon107/af80d42e020e24f4184f1225c00cbb16 to your computer and use it in GitHub Desktop.
Solution to Code Challenge 4 from "Refactoring Tweaks Workbook"
/* Code Challenge 4, from “Refactoring Tweaks Workbook” by Tonya Mork ( 2016, LeanPub (https://leanpub.com/littlegreenbookofrefactoringtweaks-workbook) ). */
/***************************************************
*
* Original code to be refactored.
*
**************************************************/
class Prefix_Shortcode_Course_Categories implements Prefix_Shortcode_Interface {
/**
* Setup the shortcode object
*
* @since 1.9.0
*
* @param array $attributes
* @param string $content
* @param string $shortcode the shortcode that was called for this instance
*/
public function __construct( $attributes, $content, $shortcode ) {
$this->orderby = isset( $attributes['orderby'] ) ? $attributes['orderby'] : 'name';
$this->order = isset( $attributes['order'] ) ? $attributes['order'] : 'ASC';
$this->number = isset( $attributes['number'] ) ? $attributes['number'] : '100';
$this->parent = isset( $attributes['parent'] ) ? $attributes['parent'] : '';
$include = isset( $attributes['include'] ) ? explode( ',', $attributes['include'] ) : '';
$this->include = $this->generate_term_ids( $include );
$exclude = isset( $attributes['exclude'] ) ? explode( ',', $attributes['exclude'] ) : '';
$this->exclude = $this->generate_term_ids( $exclude );
// make sure we handle string true/false values correctly with respective defaults
$hide_empty = isset( $attributes['hide_empty'] ) ? $attributes['hide_empty'] : 'false';
$this->hide_empty = 'true' == $hide_empty ? true : false;
$this->setup_course_categories();
}
}
/**************************************************
*
* Refactored Code
*
**************************************************/
class Prefix_Shortcode_Course_Categories implements Prefix_Shortcode_Interface {
/**
* Setup the shortcode object
*
* @param array $attributes
* @param string $content
* @param string $shortcode the shortcode that was called for this instance
* @since 1.9.0
*
*/
public function __construct( $attributes, $content, $shortcode ) {
$this->orderby = $this->check_attribute_isset( 'orderby' );
$this->order = $this->check_attribute_isset( 'order', 'ASC' );
$this->number = $this->check_attribute_isset( 'number', '100' );
$this->parent = $this->check_attribute_isset( 'parent' );
$include = $this->check_attribute_isset( 'include' );
$this->include = $this->generate_term_ids( $include );
$exclude = $this->check_attribute_isset( 'exclude' );
$this->exclude = $this->generate_term_ids( $exclude );
$hide_empty = $this->check_attribute_isset( 'hide_empty' );
$this->hide_empty = ( $hide_empty ) ? true : false;
$this->setup_course_categories();
}
protected function check_attribute_isset( $att, $default = '' ) {
if ( empty( $attributes[ $att ] ) ) {
return $default;
}
if ( is_array( $attributes[ $att ] ) ) {
return explode( ',', $attributes[ $att ] );
}
return $attributes[ $att ];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment