Skip to content

Instantly share code, notes, and snippets.

@thefuxia
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thefuxia/10922906 to your computer and use it in GitHub Desktop.
Save thefuxia/10922906 to your computer and use it in GitHub Desktop.
Basic concept for a flexible OOP breadcrumb setup
<?php
interface Wp_Breadcrumb_Strings_Interface {
public function get_text( $item );
}
class Wp_Breadcrumb_Strings implements Wp_Breadcrumb_Strings_Interface {
public function get_text( $item ) {}
}
interface Wp_Breadcrumb_Items_Interface {
public function get_items();
}
class Wp_Breadcrumb_Items implements Wp_Breadcrumb_Items_Interface {
public function get_items() {}
}
interface Wp_Breadcrumb_Renderer_Interface {
public function render();
}
class Wp_Breadcrumb_Renderer implements Wp_Breadcrumb_Renderer_Interface {
private $item_provider;
private $strings;
public function __construct(
Wp_Breadcrumb_Items_Interface $item_provider,
Wp_Breadcrumb_Strings_Interface $strings
) {
$this->item_provider = $item_provider;
$this->strings = $strings;
}
public function render() {
$items = $this->item_provider->get_items();
if ( empty ( $items ) )
return;
foreach ( $items as $item ) {
$text = $this->strings->get_text( $item );
// print item
}
}
}
function wp_nav_breadcrumb( $args = array() ) {
$defaults = array (
'strings' => 'Wp_Breadcrumb_Strings',
'item_provider' => 'Wp_Breadcrumb_Items',
'renderer' => 'Wp_Breadcrumb_Renderer',
);
$classes = wp_parse_args( $args, $defaults );
$strings = $classes['strings'];
if ( ! is_object( $strings )
or ! is_a( $strings, 'Wp_Breadcrumb_Strings_Interface' )
) {
$strings = new Wp_Breadcrumb_Strings;
}
$item_provider = $classes['item_provider'];
if ( ! is_object( $item_provider )
or ! is_a( $item_provider, 'Wp_Breadcrumb_Items_Interface' )
) {
$item_provider = new Wp_Breadcrumb_Items;
}
$renderer = $classes['renderer'];
if ( ! is_object( $renderer )
or ! is_a( $renderer, 'Wp_Breadcrumb_Renderer_Interface' )
) {
$renderer = new Wp_Breadcrumb_Renderer( $item_provider, $strings );
}
return $renderer->render();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment