| 1 |
<?php |
| 2 |
namespace ULTP\blocks; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || exit; |
| 5 |
|
| 6 |
class Post_Breadcrumb { |
| 7 |
public function __construct() { |
| 8 |
add_action( 'init', array( $this, 'register' ) ); |
| 9 |
} |
| 10 |
public function get_attributes() { |
| 11 |
|
| 12 |
return array( |
| 13 |
'blockId' => '', |
| 14 |
|
| 15 |
/* |
| 16 |
============================ |
| 17 |
Breadcrumb Settings |
| 18 |
============================*/ |
| 19 |
'bcrumbSeparator' => true, |
| 20 |
'bcrumbName' => true, |
| 21 |
'bcrumbRootText' => 'Home', |
| 22 |
|
| 23 |
/* |
| 24 |
============================ |
| 25 |
Separator Style |
| 26 |
============================*/ |
| 27 |
'bcrumbSeparatorIcon' => 'dash', |
| 28 |
|
| 29 |
/* |
| 30 |
============================ |
| 31 |
Advance Settings |
| 32 |
============================*/ |
| 33 |
'advanceId' => '', |
| 34 |
'advanceZindex' => '', |
| 35 |
'hideExtraLarge' => false, |
| 36 |
'hideDesktop' => false, |
| 37 |
'hideTablet' => false, |
| 38 |
'hideMobile' => false, |
| 39 |
'advanceCss' => '', |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
public function register() { |
| 44 |
register_block_type( |
| 45 |
'ultimate-post/post-breadcrumb', |
| 46 |
array( |
| 47 |
'editor_script' => 'ultp-blocks-editor-script', |
| 48 |
'editor_style' => 'ultp-blocks-editor-css', |
| 49 |
'render_callback' => array( $this, 'content' ), |
| 50 |
) |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
public function content( $attr, $noAjax ) { |
| 55 |
$attr = wp_parse_args( $attr, $this->get_attributes() ); |
| 56 |
global $post; |
| 57 |
$block_name = 'post-breadcrumb'; |
| 58 |
$wrapper_before = $wrapper_after = $content = ''; |
| 59 |
|
| 60 |
$attr['className'] = isset($attr['className']) && $attr['className'] ? preg_replace('/[^A-Za-z0-9_ -]/', '', $attr['className']) : ''; |
| 61 |
$attr['align'] = isset($attr['align']) && $attr['align'] ? preg_replace('/[^A-Za-z0-9_ -]/', '', $attr['align']) : ''; |
| 62 |
$attr['advanceId'] = isset($attr['advanceId']) ? sanitize_html_class( $attr['advanceId'] ) : ''; |
| 63 |
$attr['blockId'] = isset($attr['blockId']) ? sanitize_html_class( $attr['blockId'] ) : ''; |
| 64 |
$attr['bcrumbRootText'] = wp_kses($attr['bcrumbRootText'], ultimate_post()->ultp_allowed_html_tags()); |
| 65 |
$separator = ''; |
| 66 |
|
| 67 |
$wrapper_before .= '<div '.( $attr['advanceId'] ? 'id="'.$attr['advanceId'].'" ':'' ).' class="wp-block-ultimate-post-'.$block_name.' ultp-block-'.$attr["blockId"].( $attr["className"] ?' '.$attr["className"]:'' ).''.( $attr["align"] ? ' align' .$attr["align"]:'' ).'">'; |
| 68 |
$wrapper_before .= '<div class="ultp-block-wrapper">'; |
| 69 |
$content .= '<ul class="ultp-builder-breadcrumb ultp-breadcrumb-'.sanitize_html_class( $attr['bcrumbSeparatorIcon'] ).'">'; |
| 70 |
if ($attr['bcrumbSeparator']) { |
| 71 |
$separator .= '<li class="ultp-breadcrumb-separator"></li>'; |
| 72 |
} |
| 73 |
$content .= '<li><a href="'.esc_url(home_url('/')).'">'.__( strlen($attr['bcrumbRootText']) > 0 ? $attr['bcrumbRootText'] : 'Home', 'ultimate-post').'</a></li>'; |
| 74 |
if (is_category() || is_single() || is_tag() || is_author()) { |
| 75 |
if (is_category()) { |
| 76 |
$cat = get_queried_object(); |
| 77 |
if (isset($cat) && is_object($cat)) { |
| 78 |
$parents = []; |
| 79 |
$parent_id = $cat->parent; |
| 80 |
while ($parent_id) { |
| 81 |
$parent = get_category($parent_id); |
| 82 |
if ($parent && $parent->term_id != 0) { |
| 83 |
array_unshift($parents, $parent); // Add to the beginning |
| 84 |
$parent_id = $parent->parent; |
| 85 |
} else { |
| 86 |
break; |
| 87 |
} |
| 88 |
} |
| 89 |
foreach ($parents as $parent_cat) { |
| 90 |
$content .= $separator.'<li><a href="'.esc_url(get_term_link($parent_cat->term_id, 'category')).'">'.esc_html($parent_cat->name).'</a></li>'; |
| 91 |
} |
| 92 |
} |
| 93 |
$content .= $separator.'<li>'.single_cat_title('', false).'</li>'; |
| 94 |
} |
| 95 |
if (is_tag()) { |
| 96 |
$content .= $separator.'<li>'.single_tag_title('', false).'</li>'; |
| 97 |
} |
| 98 |
if (is_author()) { |
| 99 |
$content .= $separator.'<li>'.get_the_author_meta('display_name', false).'</li>'; |
| 100 |
} |
| 101 |
if (is_single()) { |
| 102 |
$cat = get_the_category(); |
| 103 |
if (isset($cat[0])) { |
| 104 |
$content .= $separator.'<li><a href="'.get_term_link($cat[0]->term_id).'">'.$cat[0]->name.'</a></li>'; |
| 105 |
} |
| 106 |
if ($attr['bcrumbName']) { |
| 107 |
$content .= $separator.'<li>'.get_the_title().'</li>'; |
| 108 |
} |
| 109 |
} |
| 110 |
} elseif (is_page()) { |
| 111 |
if ($post->post_parent) { |
| 112 |
$ancestor = get_post_ancestors($post->post_parent); |
| 113 |
if (isset($ancestor[0])) { |
| 114 |
$content = $separator.'<li><a href="'.get_permalink($ancestor[0]).'">'.get_the_title($ancestor[0]).'</a></li>'; |
| 115 |
} |
| 116 |
} |
| 117 |
if ($attr['bcrumbName']) { |
| 118 |
$content .= $separator.'<li>'.get_the_title().'</li>'; |
| 119 |
} |
| 120 |
} elseif (is_search()) { |
| 121 |
$content .= $separator.'<li>'.get_search_query().'</li>'; |
| 122 |
} |
| 123 |
$content .= '</ul>'; |
| 124 |
$wrapper_after .= '</div>'; |
| 125 |
$wrapper_after .= '</div>'; |
| 126 |
|
| 127 |
return $wrapper_before . $content . $wrapper_after; |
| 128 |
} |
| 129 |
} |
| 130 |
|