<?php
/**
* Module to build and show breadcrumbs
*
* Version: 1.0.3
*
* @author Cherry Team <support@cherryframework.com>, Justin Tadlock <justin@justintadlock.com>
* @copyright Copyright (c) 2008 - 2014, Justin Tadlock
* @link http://themehybrid.com/plugins/breadcrumb-trail
* @license http://www.gnu.org/licenses/gpl-3.0.html
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( ! class_exists( 'CX_Breadcrumbs' ) ) {
/**
* Breadcrumbs builder class.
* Class is based on Breadcrumb Trail plugin by Justin Tadlock.
*
* @since 1.0.0
*/
class CX_Breadcrumbs {
/**
* Indexed array of breadcrumb trail items.
*
* @since 1.0.0
* @var array
*/
public $items = array();
/**
* Breadcrumb arguments.
*
* @since 1.0.0
* @var array
*/
public $args = array();
/**
* Page title.
*
* @since 1.0.0
* @var array
*/
public $page_title = null;
/**
* Breadcrumbs CSS.
*
* @since 1.0.0
* @var array
*/
public $css = array(
'module' => 'cherry-breadcrumbs',
'content' => 'cherry-breadcrumbs_content',
'wrap' => 'cherry-breadcrumbs_wrap',
'browse' => 'cherry-breadcrumbs_browse',
'item' => 'cherry-breadcrumbs_item',
'separator' => 'cherry-breadcrumbs_item_sep',
'link' => 'cherry-breadcrumbs_item_link',
'target' => 'cherry-breadcrumbs_item_target',
);
/**
* Check if Breadcrumbs class was extended.
*
* @since 1.0.0
* @var boolean
*/
public $is_extend = false;
/**
* Constructor of class.
*/
public function __construct( $args = array() ) {
$defaults = array(
'separator' => '/',
'before' => '',
'after' => '',
'show_mobile' => true,
'show_tablet' => true,
'wrapper_format' => '<div>%1$s</div><div>%2$s</div>',
'page_title_format' => '<h1 class="page-title">%s</h1>',
'item_format' => '<div class="%2$s">%1$s</div>',
'home_format' => '<a href="%4$s" class="%2$s is-home" rel="home" title="%3$s">%1$s</a>',
'link_format' => '<a href="%4$s" class="%2$s" rel="tag" title="%3$s">%1$s</a>',
'target_format' => '<span class="%2$s">%1$s</span>',
'show_on_front' => true,
'network' => false,
'show_title' => true,
'show_items' => true,
'show_browse' => true,
'echo' => true,
'strings' => array(),
'date_labels' => array(),
// Cherry team editing start
'action' => 'cx_breadcrumbs/render',
'css_namespace' => array(),
'path_type' => 'full',
// Cherry team editing end
'post_taxonomy' => apply_filters(
'cx_breadcrumbs/trail_taxonomies',
array(
'post' => 'category',
)
),
);
$this->args = apply_filters( 'cx_breadcrumbs/args', wp_parse_args( $args, $defaults ) );
// Cherry team editing start
if ( isset( $this->args['css_namespace'] ) && ! empty( $this->args['css_namespace'] ) ) {
$this->css = wp_parse_args( $this->args['css_namespace'], $this->css );
}
// Cherry team editing end
if ( ! empty( $args['date_labels'] ) ) {
$this->args['date_labels'] = wp_parse_args( $args['date_labels'], $this->default_date_labels() );
} else {
$this->args['date_labels'] = $this->default_date_labels();
}
if ( is_front_page() && false === $this->args['show_on_front'] ) {
return;
}
$this->build_trail();
if ( ! $this->is_extend ) {
add_action( $this->args['action'], array( $this, 'get_trail' ) );
}
}
/**
* Formats and outputs the breadcrumb trail.
*
* @since 1.0.0
*/
public function get_trail() {
if ( is_front_page() && false === $this->args['show_on_front'] ) {
return;
}
$breadcrumb = $this->get_items();
$title = $this->get_title();
$wrapper_classes = array(
esc_attr( $this->css['module'] )
);
if ( false === $this->args['show_mobile'] ) {
$wrapper_classes[] = 'hidden-xs';
}
if ( false === $this->args['show_tablet'] ) {
$wrapper_classes[] = 'hidden-sm';
}
$wrapper_classes = apply_filters( 'cx_breadcrumbs/wrapper_classes', $wrapper_classes );
$wrapper_classes = array_unique( $wrapper_classes );
$wrapper_classes = array_map( 'sanitize_html_class', $wrapper_classes );
$wrapper_css = implode( ' ', $wrapper_classes );
/* Open the breadcrumb trail containers. */
$result = "\n\t\t" . '<div class="' . esc_attr( $wrapper_css ) . '">';
$result .= sprintf( $this->args['wrapper_format'], $title, $breadcrumb );
/* Close the breadcrumb trail containers. */
$result .= "\n\t\t" . '</div>';
if ( true === $this->args['echo'] ) {
echo $result;
} else {
return $result;
}
}
/**
* Get breacrumbs list items.
*
* @since 1.0.0
*/
public function get_items() {
if ( false == $this->args['show_items'] ) {
return;
}
$breadcrumb = '';
/* Connect the breadcrumb trail if there are items in the trail. */
if ( empty( $this->items ) && ! is_array( $this->items ) ) {
return;
}
if ( is_front_page() && false === $this->args['show_on_front'] ) {
return;
}
/* Make sure we have a unique array of items. */
$this->items = array_unique( $this->items );
if ( $this->args['before'] ) {
$breadcrumb .= $this->args['before'];
}
/* Open the breadcrumb trail containers. */
$breadcrumb .= "\n\t\t" . '<div class="' . esc_attr( $this->css['content'] ) . '">';
/* Add 'browse' label if it should be shown. */
if ( true === $this->args['show_browse'] && $this->get_strings( 'browse' ) ) {
$browse = $this->prepare_label( $this->get_strings( 'browse' ) );
$breadcrumb .= "\n\t\t\t" . '<div class="' . esc_attr( $this->css['browse'] ) . '">' . $browse . '</div> ';
}
$breadcrumb .= "\n\t\t" . '<div class="' . esc_attr( $this->css['wrap'] ) . '">';
/* Format the separator. */
$separator = ! empty( $this->args['separator'] )
? $this->args['separator']
: '/';
$separator = $this->prepare_label( $separator );
$separator = '<div class="' . esc_attr( $this->css['separator'] ) . '">' . $separator . '</div>';
$separator = sprintf( $this->args['item_format'], $separator, $this->css['item'] );
/* Join the individual trail items into a single string. */
$breadcrumb .= join( "\n\t\t\t {$separator} ", $this->items );
$breadcrumb .= "\n\t\t" . '</div>';
$breadcrumb .= "\n\t\t" . '</div>';
if ( $this->args['after'] ) {
$breadcrumb .= $this->args['after'];
}
/* Allow developers to filter the breadcrumb trail HTML and page title. */
$breadcrumb = apply_filters( 'cx_breadcrumbs/trail', $breadcrumb, $this->args );
return $breadcrumb;
}
/**
* Get page title.
*
* @since 1.0.0
*/
public function get_title() {
if ( false == $this->args['show_title'] || ! $this->page_title ) {
return;
}
$title = apply_filters(
'cx_breadcrumbs/page_title',
sprintf( $this->args['page_title_format'], $this->page_title ), $this->args
);
return $title;
}
/**
* Returns an array of the default labels.
*
* @since 1.0.0
*/
public function default_date_labels() {
$labels = array(
'archive_minute_hour' => 'g:i a', // minute and hour archives time format
'archive_minute' => 'i', // minute archives time format
'archive_hour' => 'g a', // hour archives time format
'archive_year' => 'Y', // yearly archives date format
'archive_month' => 'F', // monthly archives date format
'archive_day' => 'j', // daily archives date format
'archive_week' => 'W', // weekly archives date format
);
return $labels;
}
public function get_strings( $key = null ) {
$defaults = array(
'browse' => 'Browse:',
'home' => 'Home',
'error_404' => '404 Not Found',
'archives' => 'Archives',
'search' => 'Search results for “%s”',
'paged' => 'Page %s',
'archive_minute' => 'Minute %s',
'archive_week' => 'Week %s',
/* "%s" is replaced with the translated date/time format. */
'archive_minute_hour' => '%s',
'archive_hour' => '%s',
'archive_day' => '%s',
'archive_month' => '%s',
'archive_year' => '%s',
);
if ( isset( $this->args['strings'][ $key ] ) ) {
return $this->args['strings'][ $key ];
} elseif ( isset( $defaults[ $key ] ) ) {
return $defaults[ $key ];
} else {
return '';
}
}
/**
* Returns home title
*
* @return string
*/
public function home_title() {
$title = $this->get_strings( 'home' );
$use_custom_title = apply_filters( 'cx_breadcrumbs/custom_home_title', true );
if ( $use_custom_title ) {
$page_on_front_id = get_option( 'page_on_front' );
$page_title = false;
if ( $page_on_front_id ) {
$page_title = get_the_title( $page_on_front_id );
}
if ( ! empty( $page_title ) ) {
$title = $page_title;
}
}
Loading ...