# catch-breadcrumb/1.1/includes/shortcodes.php

Catch Breadcrumb, version 1.1. 244 lines.

- Page: https://pluginprobe.com/plugins/catch-breadcrumb/1.1/code/includes/shortcodes.php
- Raw: https://pluginprobe.com/plugins/catch-breadcrumb/1.1/raw/includes/shortcodes.php
- Modified: 2018-12-12T11:29:08+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/catch-breadcrumb/1.1/code/includes/shortcodes.php#L10-L20`.

```php
<?php
/**
 *
 *
 * @link        https://catchplugins.com/plugins
 * @since      1.0.0
 *
 * @package    breadcrumb
 * @subpackage Breadcrumb/includes
 */

/**
 *
 *
 * @package    Breadcrumb
 * @subpackage Breadcrumb/includes
 * @author     Catch Plugins <info@catchplugins.com
 */


class Catch_Breadcrumb_Shortcode {
	public function __construct() {
		add_shortcode( 'catch-breadcrumb', array( $this, 'catch_breadcrumb_shortcode' ) );
		}

	public function catch_display_breadcrumb(){
		do_shortcode( '[catch-breadcrumb]' );
	}

	public function catch_breadcrumb_shortcode( $atts,$content ) {
		if ( function_exists( 'woocommerce_breadcrumb' ) && ( is_woocommerce() || is_shop() ) ) : ?>
            <div class="breadcrumb-area">
			<div class="section-wrapper">
				<?php
					$catch_breadcrumb_options = catch_breadcrumb_get_options();
					$breadcrumb_separator = $catch_breadcrumb_options['breadcrumb_separator'];

					if ( $breadcrumb_separator ) {
						$breadcrumb_separator = '<span class="sep">' . $breadcrumb_separator . '</span>';
					}

					$args = array(
						'delimiter' => $breadcrumb_separator,
						'before'    => '<span class="breadcrumb">',
						'after'     => '</span>',
					);

					woocommerce_breadcrumb( $args );
				?>
				</div><!-- .wrapper -->
			</div><!-- .breadcrumb-area -->
		<?php 
		else:
			self::breadcrumb( $atts,$content );
		endif;
	}
		
	public function breadcrumb( $atts,$content ) {
		$defaults = array(
		'before' => '<span class="breadcrumb-current">',// code before the breadcrumb section
		'after' => '</span>',    // code after the breadcrumb section
		'showCurrent'=> 1,// 1 - show current post/page title in breadcrumbs, 0 - don't show
		//'echo' => true,
		'linkBefore' => '<span class="breadcrumb" typeof="v:Breadcrumb">',
		'linkAfter'  => '</span>',
	);
		$atts = wp_parse_args( $atts, $defaults );
		$content= $atts['before'];
		$catch_breadcrumb_options = catch_breadcrumb_get_options();
		if ( $catch_breadcrumb_options['breadcrumb_separator'] ) {
			$catch_breadcrumb_options['breadcrumb_separator'] = '<span class="sep">' . $catch_breadcrumb_options['breadcrumb_separator'] . '</span>';
		}

		/* === OPTIONS === */
		$text['home']     = esc_html__( 'Home', 'catch-breadcrumb' ); // text for the 'Home' link
		/* translators: 1: before text/html, 2: after text/html. */
		$text['category'] = esc_html__( '%1$s Archive for %2$s', 'catch-breadcrumb' ); // text for a category page
		/* translators: 1: before text/html, 2: after text/html. */
		$text['search']   = esc_html__( '%1$sSearch results for: %2$s', 'catch-breadcrumb' ); // text for a search results page
		/* translators: 1: before text/html, 2: after text/html. */
		$text['tag']      = esc_html__( '%1$sPosts tagged %2$s', 'catch-breadcrumb' ); // text for a tag page
		/* translators: 1: before text/html, 2: after text/html. */
		$text['author']   = esc_html__( '%1$sView all posts by %2$s', 'catch-breadcrumb' ); // text for an author page
		$text['404']      = esc_html__( 'Error 404', 'catch-breadcrumb' ); // text for the 404 page
		/* === END OF OPTIONS === */
		global $post, $paged, $page;
		$homeLink   = home_url( '/' );
		$style = '';
		if( $style ){
			echo '<style>' . $style . '</style>';
		}
		$linkAttr   = ' rel="v:url" property="v:title"';
		$home_icon = '';

		if ( $catch_breadcrumb_options['breadcrumb_home_icon'] ){
			$home_icon = '<span class="fa fa-home"></span>';
		}
		
		$link_home       = $atts['linkBefore'] . '<a' . $linkAttr . ' href="%1$s">'. $home_icon . '%2$s</a>' . wp_kses_post( $catch_breadcrumb_options['breadcrumb_separator'] ) . $atts['linkAfter'];

		$link       = $atts['linkBefore'] . '<a' . $linkAttr . ' href="%1$s">' . '%2$s</a>' . wp_kses_post( $catch_breadcrumb_options['breadcrumb_separator'] ) . $atts['linkAfter'];
		
		if ( is_front_page() ) {
			if ( $catch_breadcrumb_options['breadcrumb_display_home'] ) {
				echo '
				<div class="catch-breadcrumb breadcrumb-area custom">
				<nav class="entry-breadcrumbs">';
				echo $atts['linkBefore'] . '<a href="' . esc_url( $homeLink ) . '">' . $home_icon . $text['home'] . '</a>' . $atts['linkAfter']; 
				echo '</nav><!-- .entry-breadcrumbs -->
				</div><!-- .breadcrumb-area -->';$atts['linkAfter'];
			}
		} else {

			echo '<div class="catch-breadcrumb breadcrumb-area custom">
				  <nav class="entry-breadcrumbs">';
				echo sprintf( $link_home, esc_url( $homeLink ), $text['home'] );  // WPCS: XSS OK.

				if ( is_home() ) {
				echo $content . esc_html( get_the_title( get_option( 'page_for_posts', true ) ) ) .    $atts['after']; // WPCS: XSS OK.
			} elseif ( is_category() ) {
				$thisCat = get_category( get_query_var( 'cat' ), false );
				if ( $thisCat->parent != 0 ) {
					$cats = get_category_parents( $thisCat->parent, true, false );
					$cats = str_replace( '<a',  $atts['linkBefore'] . '<a' . $linkAttr, $cats );
					$cats = str_replace( '</a>', '</a>' . wp_kses_post( $catch_breadcrumb_options['breadcrumb_separator']) . $atts['linkAfter'], $cats );
					echo $cats;
					 // WPCS: XSS OK.
				}
				
			the_archive_title( $content . sprintf( $text['category'], '<span class="archive-text">', '</span>' , $atts['after'] ));

			} elseif ( is_search() ) {
			echo $content . sprintf( $text['search'], '<span class="search-text">', '</span>' . get_search_query() ) .  $atts['after']; 
			// WPCS: XSS OK.
			} elseif ( is_day() ) {
			echo sprintf( $link, esc_url( get_year_link( get_the_time( __( 'Y', 'catch-breadcrumb' ) ) ) ), esc_html( get_the_time( __( 'Y', 'catch-breadcrumb' ) ) ) ) ; 
			// WPCS: XSS OK.
			echo sprintf( $link, esc_url( get_month_link( get_the_time( __( 'Y', 'catch-breadcrumb' ) ) ) ), esc_html(get_the_time( __( 'F', 'catch-breadcrumb' ) ) ) ); 
		   // WPCS: XSS OK.
		    echo $content . esc_html( get_the_time( __( 'd', 'catch-breadcrumb' ) ) ) .  $atts['after']; 
		   // WPCS: XSS OK.
		   } elseif ( is_month() ) {
		   	echo sprintf( $link, esc_url( get_year_link( get_the_time( __( 'Y', 'catch-breadcrumb' ) ) ) ), esc_html( get_the_time( __( 'Y', 'catch-breadcrumb' ) ) ) ) ; 
	       // WPCS: XSS OK.
	        echo $content . esc_html( get_the_time( __( 'F', 'catch-breadcrumb' ) ) ) . $atts['after']; 
	       // WPCS: XSS OK.
	       } elseif ( is_year() ) {
	       	echo $content . esc_html( get_the_time( __( 'Y', 'catch-breadcrumb' ) ) ) .  $atts['after']; 
	      // WPCS: XSS OK.
	      } elseif ( is_single() && !is_attachment() ) {
	      	if ( get_post_type() != 'post' ) {
	       			$post_type = get_post_type_object( get_post_type() );
	       			$post_link = get_post_type_archive_link( $post_type->name );
	       			printf( $link, esc_url( $post_link ), esc_html( $post_type->labels->singular_name ) ); 
	       			// WPCS: XSS OK.
	       			echo $content . esc_html( get_the_title() ) . $atts['after']; 
	       			// WPCS: XSS OK
	       		}
				else {
					$cat  = get_the_category();
					$cat  = $cat[0];
					$cats = get_category_parents( $cat, true, '' );
					$cats = preg_replace( "#^(.+)$#", "$1", $cats );
					$cats = str_replace( '<a',  $atts['linkBefore'] . '<a' . $linkAttr, $cats );
					$cats = str_replace( '</a>', '</a>' . wp_kses_post( $catch_breadcrumb_options['breadcrumb_separator'] ) . $atts['linkAfter'], $cats );
					echo $cats;
					  // WPCS: XSS OK.

					echo $content . esc_html( get_the_title() ) . $atts['after'];  
					// WPCS: XSS OK.
					}
				} elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
				$post_type = get_post_type_object( get_post_type() );
				echo isset( $post_type->labels->singular_name ) ? $content. esc_html( $post_type->labels->singular_name ) .  $atts['after']: '';  
				// WPCS: XSS OK.
				} elseif ( is_attachment() ) {
				$parent = get_post( $post->post_parent );
				$cat    = get_the_category( $parent->ID );
				if ( isset( $cat[0] ) ) {
					$cat = $cat[0];
				}
				if ( $cat ) {
					$cats = get_category_parents( $cat, true );
					$cats = str_replace( '<a', $atts['linkBefore'] . '<a' . $linkAttr, $cats );
					$cats = str_replace( '</a>', '</a>' . wp_kses_post( $catch_breadcrumb_options['breadcrumb_separator'] ) . $atts['linkAfter'], $cats );
					echo $cats; 
			    // WPCS: XSS OK.
			    }
				printf( $link, esc_url( get_permalink( $parent ) ), esc_html( $parent->post_title ) );
			   // WPCS: XSS OK.
				echo $content . esc_html( get_the_title() ) . $atts['after'];
				 // WPCS: XSS OK.
			} elseif ( is_page() && ! $post->post_parent ) {
				echo $content. esc_html( get_the_title() ) . $atts['after']; // WPCS: XSS O
			} elseif ( is_page() && $post->post_parent ) {
				$parent_id   = $post->post_parent;
				$breadcrumbs = array();
				while( $parent_id ) {
					$page_child    = get_post( $parent_id );
					$breadcrumbs[] = sprintf( $link, esc_url( get_permalink( $page_child->ID ) ), esc_html( get_the_title( $page_child->ID ) ) );
					$parent_id     = $page_child->post_parent;
				}
				$breadcrumbs = array_reverse( $breadcrumbs );
				for( $i = 0; $i < count( $breadcrumbs ); $i++ ) {
				echo $breadcrumbs[$i];  
			// WPCS: XSS OK.
			}
			echo $content . esc_html( get_the_title() ) . $atts['after']; 
			// WPCS: XSS OK.
			} elseif ( is_tag() ) {
				the_archive_title( $content . sprintf( $text['tag'], '<span class="tag-text">', '</span>' ), $atts['after'] ); 
		   // WPCS: XSS OK
		   } elseif ( is_author() ) {
		   	global $author;
		   	$userdata = get_userdata( $author );
		   	echo $content . sprintf( $text['author'], '<span class="author-text">', '</span>' . $userdata->display_name ) .  $atts['after']; 
		  // WPCS: XSS OK
		  } elseif ( is_404() ) {
		  	echo $content . $text['404'] .  $atts['after']; 
		  // WPCS: XSS OK.
		  }
		  if ( get_query_var( 'paged' ) ) {
		  	if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) {
					echo ' (';
				}

				/* translators: %s: current page number. */
				echo sprintf( esc_html__( 'Page %s', 'catch-breadcrumb' ), absint( max( $paged, $page ) ) );
				if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) {
					echo ')';
				}

			}
			echo '</nav><!-- .entry-breadcrumbs -->
			</div><!-- .breadcrumb-area -->';
		}
	}

}

new Catch_Breadcrumb_Shortcode();  



```
