| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The public-facing functionality of the plugin. |
| 5 |
* |
| 6 |
* @link https://catchplugins.com/plugins |
| 7 |
* @since 1.0.0 |
| 8 |
* |
| 9 |
* @package Catch_Breadcrumb |
| 10 |
* @subpackage Catch_Breadcrumb/public |
| 11 |
*/ |
| 12 |
|
| 13 |
/** |
| 14 |
* The public-facing functionality of the plugin. |
| 15 |
* |
| 16 |
* Defines the plugin name, version, and two examples hooks for how to |
| 17 |
* enqueue the public-facing stylesheet and JavaScript. |
| 18 |
* |
| 19 |
* @package Catch_Breadcrumb |
| 20 |
* @subpackage Catch_Breadcrumb/public |
| 21 |
* @author Catch Plugins <info@catchplugins.com> |
| 22 |
*/ |
| 23 |
class Catch_Breadcrumb_Public { |
| 24 |
|
| 25 |
/** |
| 26 |
* The ID of this plugin. |
| 27 |
* |
| 28 |
* @since 1.0.0 |
| 29 |
* @access private |
| 30 |
* @var string $plugin_name The ID of this plugin. |
| 31 |
*/ |
| 32 |
private $plugin_name; |
| 33 |
|
| 34 |
/** |
| 35 |
* The version of this plugin. |
| 36 |
* |
| 37 |
* @since 1.0.0 |
| 38 |
* @access private |
| 39 |
* @var string $version The current version of this plugin. |
| 40 |
*/ |
| 41 |
private $version; |
| 42 |
|
| 43 |
/** |
| 44 |
* Initialize the class and set its properties. |
| 45 |
* |
| 46 |
* @since 1.0.0 |
| 47 |
* @param string $plugin_name The name of the plugin. |
| 48 |
* @param string $version The version of this plugin. |
| 49 |
*/ |
| 50 |
public function __construct( $plugin_name, $version ) { |
| 51 |
|
| 52 |
$this->plugin_name = $plugin_name; |
| 53 |
$this->version = $version; |
| 54 |
$this->include(); |
| 55 |
} |
| 56 |
|
| 57 |
public function include() { |
| 58 |
|
| 59 |
require plugin_dir_path( __FILE__ ) . 'partials/class-catch-breadcrumb-json-ld-schema.php'; |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Register the stylesheets for the public-facing side of the site. |
| 65 |
* |
| 66 |
* @since 1.0.0 |
| 67 |
*/ |
| 68 |
public function enqueue_styles() { |
| 69 |
|
| 70 |
/** |
| 71 |
* This function is provided for demonstration purposes only. |
| 72 |
* |
| 73 |
* An instance of this class should be passed to the run() function |
| 74 |
* defined in Catch_Breadcrumb_Loader as all of the hooks are defined |
| 75 |
* in that particular class. |
| 76 |
* |
| 77 |
* The Catch_Breadcrumb_Loader will then create the relationship |
| 78 |
* between the defined hooks and the functions defined in this |
| 79 |
* class. |
| 80 |
*/ |
| 81 |
|
| 82 |
wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/catch-breadcrumb-public.css', array(), $this->version, 'all' ); |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Register the JavaScript for the public-facing side of the site. |
| 88 |
* |
| 89 |
* @since 1.0.0 |
| 90 |
*/ |
| 91 |
public function enqueue_scripts() { |
| 92 |
|
| 93 |
/** |
| 94 |
* This function is provided for demonstration purposes only. |
| 95 |
* |
| 96 |
* An instance of this class should be passed to the run() function |
| 97 |
* defined in Catch_Breadcrumb_Loader as all of the hooks are defined |
| 98 |
* in that particular class. |
| 99 |
* |
| 100 |
* The Catch_Breadcrumb_Loader will then create the relationship |
| 101 |
* between the defined hooks and the functions defined in this |
| 102 |
* class. |
| 103 |
*/ |
| 104 |
|
| 105 |
wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/catch-breadcrumb-public.js', array( 'jquery' ), $this->version, false ); |
| 106 |
|
| 107 |
} |
| 108 |
|
| 109 |
public function catch_breadcrumb_localize() { |
| 110 |
|
| 111 |
$catch_breadcrumb_options = catch_breadcrumb_get_options(); |
| 112 |
// Escaping JS before localizing. |
| 113 |
$escaped_option = array(); |
| 114 |
|
| 115 |
foreach ( $catch_breadcrumb_options as $key => $option ) { |
| 116 |
|
| 117 |
$escaped_option[ $key ] = esc_js( $option ); |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
wp_localize_script( $this->plugin_name, 'catch_breadcrumb_object', $escaped_option ); |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
public function build_breadcrumb() { |
| 126 |
|
| 127 |
$catch_breadcrumb_options = catch_breadcrumb_get_options(); |
| 128 |
|
| 129 |
if ( function_exists( 'woocommerce_breadcrumb' ) && ( is_woocommerce() || is_shop() ) ) : |
| 130 |
|
| 131 |
ob_start(); |
| 132 |
|
| 133 |
?> |
| 134 |
|
| 135 |
<div id="catch-breadcrumb" class="catch-breadcrumb breadcrumb-area"> |
| 136 |
<div class="section-wrapper"> |
| 137 |
|
| 138 |
<?php |
| 139 |
|
| 140 |
$catch_breadcrumb_options = catch_breadcrumb_get_options(); |
| 141 |
$breadcrumb_separator = $catch_breadcrumb_options['breadcrumb_separator']; |
| 142 |
if ( $breadcrumb_separator ) { |
| 143 |
|
| 144 |
$breadcrumb_separator = '<span class="sep">' . esc_html( $breadcrumb_separator ) . '</span>'; |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
$args = array( |
| 149 |
'delimiter' => $breadcrumb_separator, |
| 150 |
'before' => '<span class="breadcrumb">', |
| 151 |
'after' => '</span>', |
| 152 |
); |
| 153 |
|
| 154 |
woocommerce_breadcrumb( $args ); |
| 155 |
|
| 156 |
?> |
| 157 |
|
| 158 |
</div><!-- .wrapper --> |
| 159 |
</div><!-- .breadcrumb-area --> |
| 160 |
|
| 161 |
<?php |
| 162 |
|
| 163 |
$breadcrumb = ob_get_clean(); |
| 164 |
|
| 165 |
else : |
| 166 |
|
| 167 |
$breadcrumb = self::custom_breadcrumb(); |
| 168 |
|
| 169 |
endif; |
| 170 |
|
| 171 |
return $breadcrumb; |
| 172 |
} |
| 173 |
|
| 174 |
public function show_breadcrumb() { |
| 175 |
|
| 176 |
echo '<div id="catch-breadcrumb-container">' . wp_kses( $this->build_breadcrumb(), 'post' ) . '</div>'; |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
public function custom_breadcrumb() { |
| 181 |
|
| 182 |
$atts = array( |
| 183 |
'before' => '<span class="breadcrumb-current">', // code before the breadcrumb section |
| 184 |
'after' => '</span>', // code after the breadcrumb section |
| 185 |
'show_current' => 1, // 1 - show current post/page title in breadcrumbs, 0 - don't show |
| 186 |
'echo' => true, |
| 187 |
'link_before' => '<span class="breadcrumb">', |
| 188 |
'link_after' => '</span>', |
| 189 |
); |
| 190 |
|
| 191 |
$catch_breadcrumb_options = catch_breadcrumb_get_options(); |
| 192 |
|
| 193 |
if ( $catch_breadcrumb_options['breadcrumb_separator'] ) { |
| 194 |
|
| 195 |
$catch_breadcrumb_options['breadcrumb_separator'] = '<span class="sep">' . esc_html( $catch_breadcrumb_options['breadcrumb_separator'] ) . '</span>'; |
| 196 |
|
| 197 |
} |
| 198 |
|
| 199 |
/* === OPTIONS === */ |
| 200 |
$text['home'] = esc_html__( 'Home', 'catch-breadcrumb' ); // text for the 'Home' link |
| 201 |
/* translators: 1: before text/html, 2: after text/html. */ |
| 202 |
$text['category'] = esc_html__( '%1$s Archive for %2$s', 'catch-breadcrumb' ); // text for a category page |
| 203 |
/* translators: 1: before text/html, 2: after text/html. */ |
| 204 |
$text['search'] = esc_html__( '%1$sSearch results for: %2$s', 'catch-breadcrumb' ); // text for a search results page |
| 205 |
/* translators: 1: before text/html, 2: after text/html. */ |
| 206 |
$text['tag'] = esc_html__( '%1$sPosts tagged %2$s', 'catch-breadcrumb' ); // text for a tag page |
| 207 |
/* translators: 1: before text/html, 2: after text/html. */ |
| 208 |
$text['author'] = esc_html__( '%1$sView all posts by %2$s', 'catch-breadcrumb' ); // text for an author page |
| 209 |
$text['404'] = esc_html__( 'Error 404', 'catch-breadcrumb' ); // text for the 404 page |
| 210 |
/* === END OF OPTIONS === */ |
| 211 |
global $post, $paged, $page; |
| 212 |
$home_link = home_url( '/' ); |
| 213 |
|
| 214 |
ob_start(); |
| 215 |
$home_icon = ''; |
| 216 |
|
| 217 |
if ( isset( $catch_breadcrumb_options['breadcrumb_home_icon'] ) && '1' == $catch_breadcrumb_options['breadcrumb_home_icon'] ) { |
| 218 |
|
| 219 |
$home_icon = '<span class="fa fa-home"></span> '; |
| 220 |
|
| 221 |
} |
| 222 |
|
| 223 |
$link_home_front = $atts['link_before'] . '<a href="%1$s">' . wp_kses_post( $home_icon ) . '%2$s</a>' . $atts['link_after']; |
| 224 |
|
| 225 |
$link_home = $atts['link_before'] . '<a href="%1$s">' . wp_kses_post($home_icon) . '%2$s</a>' . wp_kses_post( $catch_breadcrumb_options['breadcrumb_separator'] ) . $atts['link_after']; |
| 226 |
|
| 227 |
$link = $atts['link_before'] . '<a href="%1$s">' . '%2$s</a>' . wp_kses_post( $catch_breadcrumb_options['breadcrumb_separator'] ) . $atts['link_after']; |
| 228 |
|
| 229 |
if ( is_front_page() ) { |
| 230 |
|
| 231 |
if ( $catch_breadcrumb_options['breadcrumb_display_home'] ) { |
| 232 |
|
| 233 |
?> |
| 234 |
|
| 235 |
<div id="catch-breadcrumb" class="catch-breadcrumb breadcrumb-area custom"><nav class="entry-breadcrumbs"> |
| 236 |
|
| 237 |
<?php |
| 238 |
|
| 239 |
echo sprintf( $link_home_front, esc_url( home_url( '/' ) ), $text['home'] ); // WPCS: XSS OK. |
| 240 |
|
| 241 |
?> |
| 242 |
|
| 243 |
</nav><!-- .entry-breadcrumbs --></div><!-- .breadcrumb-area --> |
| 244 |
|
| 245 |
<?php |
| 246 |
|
| 247 |
} |
| 248 |
} else { |
| 249 |
|
| 250 |
?> |
| 251 |
|
| 252 |
<div id="catch-breadcrumb" class="catch-breadcrumb breadcrumb-area custom"><nav class="entry-breadcrumbs"> |
| 253 |
|
| 254 |
<?php |
| 255 |
|
| 256 |
echo sprintf( $link_home, esc_url( $home_link ), $text['home'] ); // WPCS: XSS OK. |
| 257 |
|
| 258 |
if ( is_home() ) { |
| 259 |
|
| 260 |
echo $atts['before'] . esc_html( get_the_title( get_option( 'page_for_posts', true ) ) ) . $atts['after']; |
| 261 |
// WPCS: XSS OK. |
| 262 |
|
| 263 |
} elseif ( is_category() ) { |
| 264 |
|
| 265 |
$this_cat = get_category( get_query_var( 'cat' ), false ); |
| 266 |
|
| 267 |
if ( 0 != $this_cat->parent ) { |
| 268 |
|
| 269 |
$cats = get_category_parents( $this_cat->parent, true, false ); |
| 270 |
$cats = str_replace( '<a', $atts['link_before'] . '<a', $cats ); |
| 271 |
$cats = str_replace( '</a>', '</a>' . wp_kses_post( $catch_breadcrumb_options['breadcrumb_separator'] ), $cats ); |
| 272 |
$cats = str_replace( '</a>', '</a>' . $atts['link_after'], $cats ); |
| 273 |
echo $cats; // WPCS: XSS OK |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
the_archive_title( $atts['before'] . sprintf( $text['category'], '<span class="archive-text">', '</span>', $atts['after'] ) ); // WPCS: XSS OK. |
| 278 |
|
| 279 |
} elseif ( is_search() ) { |
| 280 |
|
| 281 |
echo $atts['before'] . sprintf( $text['search'], '<span class="search-text">', '</span>' . get_search_query() ) . $atts['after']; |
| 282 |
// WPCS: XSS OK. |
| 283 |
|
| 284 |
} elseif ( is_day() ) { |
| 285 |
|
| 286 |
echo sprintf( $link, esc_url( get_year_link( get_the_time( esc_html__( 'Y', 'catch-breadcrumb' ) ) ) ), esc_html( get_the_time( esc_html__( 'Y', 'catch-breadcrumb' ) ) ) ); // WPCS: XSS OK. |
| 287 |
echo sprintf( $link, esc_url( get_month_link( get_the_time( esc_html__( 'Y', 'catch-breadcrumb' ) ), get_the_time( esc_html__( 'm', 'catch-breadcrumb-pro' ) ) ) ), esc_html( get_the_time( esc_html__( 'F', 'catch-breadcrumb' ) ) ) ); // WPCS: XSS OK. |
| 288 |
echo $atts['before'] . esc_html( get_the_time( esc_html__( 'd', 'catch-breadcrumb' ) ) ) . $atts['after']; // WPCS: XSS OK. |
| 289 |
|
| 290 |
} elseif ( is_month() ) { |
| 291 |
|
| 292 |
echo sprintf( $link, esc_url( get_year_link( get_the_time( esc_html__( 'Y', 'catch-breadcrumb' ) ) ) ), esc_html( get_the_time( esc_html__( 'Y', 'catch-breadcrumb' ) ) ) ); // WPCS: XSS OK. |
| 293 |
echo $atts['before'] . esc_html( get_the_time( esc_html__( 'F', 'catch-breadcrumb' ) ) ) . $atts['after']; // WPCS: XSS OK |
| 294 |
|
| 295 |
} elseif ( is_year() ) { |
| 296 |
|
| 297 |
echo $atts['before'] . esc_html( get_the_time( esc_html__( 'Y', 'catch-breadcrumb' ) ) ) . $atts['after']; // WPCS: XSS OK |
| 298 |
|
| 299 |
} elseif ( is_single() && ! is_attachment() ) { |
| 300 |
|
| 301 |
if ( get_post_type() != 'post' ) { |
| 302 |
|
| 303 |
$post_type = get_post_type_object( get_post_type() ); |
| 304 |
$post_link = get_post_type_archive_link( $post_type->name ); |
| 305 |
printf( $link, esc_url( $post_link ), esc_html( $post_type->labels->singular_name ) ); // WPCS: XSS OK. |
| 306 |
echo $atts['before'] . esc_html( get_the_title() ) . $atts['after']; // WPCS: XSS OK. |
| 307 |
|
| 308 |
} else { |
| 309 |
|
| 310 |
$cat = get_the_category(); |
| 311 |
$cat = $cat[0]; |
| 312 |
|
| 313 |
if ( ! empty( $cat ) ) { |
| 314 |
|
| 315 |
$cats = get_category_parents( $cat, true, '' ); |
| 316 |
$cats = preg_replace( '#^(.+)$#', '$1', $cats ); |
| 317 |
$cats = str_replace( '<a', $atts['link_before'] . '<a', $cats ); |
| 318 |
$cats = str_replace( '</a>', '</a>' . wp_kses_post( $catch_breadcrumb_options['breadcrumb_separator'] ), $cats ); |
| 319 |
$cats = str_replace( '</a>', '</a>' . $atts['link_after'], $cats ); |
| 320 |
echo $cats; // WPCS: XSS OK. |
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
echo $atts['before'] . esc_html( get_the_title() ) . $atts['after']; // WPCS: XSS OK. |
| 325 |
|
| 326 |
} |
| 327 |
|
| 328 |
} elseif ( ! is_single() && ! is_page() && get_post_type() != 'post' && ! is_404() ) { |
| 329 |
|
| 330 |
$post_type = get_post_type_object( get_post_type() ); |
| 331 |
echo isset( $post_type->labels->singular_name ) ? $atts['before'] . esc_html( $post_type->labels->singular_name ) . $atts['after'] : ''; // WPCS: XSS OK. |
| 332 |
|
| 333 |
} elseif ( is_attachment() ) { |
| 334 |
|
| 335 |
$parent = get_post( $post->post_parent ); |
| 336 |
$cat = get_the_category( $parent->ID ); |
| 337 |
|
| 338 |
if ( isset( $cat[0] ) ) { |
| 339 |
|
| 340 |
$cat = $cat[0]; |
| 341 |
|
| 342 |
} |
| 343 |
|
| 344 |
if ( $cat ) { |
| 345 |
|
| 346 |
$cats = get_category_parents( $cat, true ); |
| 347 |
$cats = str_replace( '<a', $atts['link_before'] . '<a', $cats ); |
| 348 |
$cats = str_replace( '</a>', '</a>' . wp_kses_post( $catch_breadcrumb_options['breadcrumb_separator'] ), $cats ); |
| 349 |
$cats = str_replace( '</a>', '</a>' . $atts['link_after'], $cats ); |
| 350 |
echo $cats; // WPCS: XSS OK. |
| 351 |
|
| 352 |
} |
| 353 |
|
| 354 |
printf( $link, esc_url( get_permalink( $parent ) ), esc_html( $parent->post_title ) ); // WPCS: XSS OK. |
| 355 |
echo $atts['before'] . esc_html( get_the_title() ) . $atts['after']; // WPCS: XSS OK. |
| 356 |
|
| 357 |
} elseif ( is_page() && ! $post->post_parent ) { |
| 358 |
|
| 359 |
echo $atts['before'] . esc_html( get_the_title() ) . $atts['after']; // WPCS: XSS OK. |
| 360 |
|
| 361 |
} elseif ( is_page() && $post->post_parent ) { |
| 362 |
|
| 363 |
$parent_id = $post->post_parent; |
| 364 |
$breadcrumbs = array(); |
| 365 |
|
| 366 |
while ( $parent_id ) { |
| 367 |
|
| 368 |
$page_child = get_post( $parent_id ); |
| 369 |
$breadcrumbs[] = sprintf( $link, esc_url( get_permalink( $page_child->ID ) ), esc_html( get_the_title( $page_child->ID ) ) ); |
| 370 |
$parent_id = $page_child->post_parent; |
| 371 |
|
| 372 |
} |
| 373 |
|
| 374 |
$breadcrumbs = array_reverse( $breadcrumbs ); |
| 375 |
|
| 376 |
for ( $i = 0; $i < count( $breadcrumbs ); $i++ ) { |
| 377 |
|
| 378 |
echo $breadcrumbs[ $i ]; // WPCS: XSS OK. |
| 379 |
|
| 380 |
} |
| 381 |
|
| 382 |
echo $atts['before'] . esc_html( get_the_title() ) . $atts['after']; // WPCS: XSS OK. |
| 383 |
|
| 384 |
} elseif ( is_tag() ) { |
| 385 |
|
| 386 |
the_archive_title( $atts['before'] . sprintf( $text['tag'], '<span class="tag-text">', '</span>' ), $atts['after'] ); // WPCS: XSS OK |
| 387 |
|
| 388 |
} elseif ( is_author() ) { |
| 389 |
|
| 390 |
global $author; |
| 391 |
$userdata = get_userdata( $author ); |
| 392 |
echo $atts['before'] . sprintf( $text['author'], '<span class="author-text">', '</span>' . $userdata->display_name ) . $atts['after']; // WPCS: XSS OK. |
| 393 |
|
| 394 |
} elseif ( is_404() ) { |
| 395 |
|
| 396 |
echo $atts['before'] . $text['404'] . $atts['after']; // WPCS: XSS OK. |
| 397 |
|
| 398 |
} |
| 399 |
|
| 400 |
if ( get_query_var( 'paged' ) ) { |
| 401 |
|
| 402 |
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) { |
| 403 |
|
| 404 |
echo' ('; |
| 405 |
|
| 406 |
} |
| 407 |
|
| 408 |
/* translators: %s: current page number. */ |
| 409 |
echo sprintf( esc_html__( 'Page %s', 'catch-breadcrumb' ), absint( max( $paged, $page ) ) ); |
| 410 |
|
| 411 |
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) { |
| 412 |
|
| 413 |
echo ')'; |
| 414 |
|
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
?> |
| 419 |
|
| 420 |
</nav><!-- .entry-breadcrumbs --></div><!-- .breadcrumb-area --> |
| 421 |
|
| 422 |
<?php |
| 423 |
} |
| 424 |
|
| 425 |
$string = ob_get_clean(); |
| 426 |
return $string; |
| 427 |
|
| 428 |
} |
| 429 |
|
| 430 |
} |
| 431 |
|