<?php
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Text_Stroke;
use Elementor\Group_Control_Text_Shadow;
use Elementor\Icons_Manager;
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
class Motiox_Elementor_Breadcrumb extends Elementor\Widget_Base {
public function get_name() {
return 'motiox-breadcrumb';
}
public function get_title() {
return esc_html__('Motiox Breadcrumbs', 'motiox');
}
public function get_icon() {
return 'eicon-product-breadcrumbs';
}
public function get_keywords() {
return ['breadcrumbs'];
}
public function get_categories() {
return array('motiox-addons');
}
protected function register_controls() {
$this->start_controls_section(
'section_content',
[
'label' => esc_html__('Style Breadcrumbs', 'motiox'),
'tab' => Controls_Manager::TAB_CONTENT,
]
);
$this->add_control(
'display_title',
[
'label' => esc_html__('Display Title', 'motiox'),
'type' => \Elementor\Controls_Manager::SWITCHER,
'default' => 'yes',
]
);
$this->add_control(
'display_nav',
[
'label' => esc_html__('Display Nav', 'motiox'),
'type' => \Elementor\Controls_Manager::SWITCHER,
'default' => 'yes',
]
);
$this->add_control(
'separator',
[
'label' => esc_html__( 'Separator', 'motiox' ),
'type' => Controls_Manager::ICONS,
'label_block' => false,
'skin' => 'inline',
'fa4compatibility' => 'icon',
'default' => [
'value' => 'motiox-icon-arr-right',
'library' => 'motiox-icon',
],
'condition' => [
'display_nav' => 'yes',
],
]
);
$this->add_control(
'home_text',
[
'label' => esc_html__( 'Home Page', 'motiox' ),
'type' => Controls_Manager::TEXT,
'default' => esc_html__( 'Motiox', 'motiox' ),
'condition' => [
'display_nav' => 'yes',
],
]
);
$this->add_control(
'search_text',
[
'label' => esc_html__( 'Search', 'motiox' ),
'type' => Controls_Manager::TEXT,
'default' => esc_html__( 'Search results for:', 'motiox' ),
'condition' => [
'display_nav' => 'yes',
],
]
);
$this->add_control(
'error_text',
[
'label' => esc_html__( '404 Page', 'motiox' ),
'type' => Controls_Manager::TEXT,
'default' => esc_html__( '404', 'motiox' ),
'condition' => [
'display_nav' => 'yes',
],
]
);
$this->end_controls_section();
$this->start_controls_section(
'section_product_rating_style_title',
[
'label' => esc_html__('Style Title', 'motiox'),
'tab' => Controls_Manager::TAB_STYLE,
]
);
$this->add_responsive_control(
'alignment',
[
'label' => esc_html__('Alignment', 'motiox'),
'type' => Controls_Manager::CHOOSE,
'options' => [
'left' => [
'title' => esc_html__('Left', 'motiox'),
'icon' => 'eicon-text-align-left',
],
'center' => [
'title' => esc_html__('Center', 'motiox'),
'icon' => 'eicon-text-align-center',
],
'right' => [
'title' => esc_html__('Right', 'motiox'),
'icon' => 'eicon-text-align-right',
],
],
'selectors' => [
'{{WRAPPER}} .breadcrumb' => 'text-align: {{VALUE}}',
'{{WRAPPER}} .motiox-title' => 'text-align: {{VALUE}}',
],
]
);
$this->add_control(
'text_color_title',
[
'label' => esc_html__('Title Color', 'motiox'),
'type' => Controls_Manager::COLOR,
'selectors' => [
'{{WRAPPER}} .motiox-title' => 'color: {{VALUE}}',
],
]
);
$this->add_group_control(
Group_Control_Typography::get_type(),
[
'name' => 'title_typography',
'selector' => '{{WRAPPER}} .motiox-title',
]
);
$this->add_group_control(
Group_Control_Text_Stroke::get_type(),
[
'name' => 'text_stroke',
'selector' => '{{WRAPPER}} .motiox-title',
]
);
$this->add_group_control(
Group_Control_Text_Shadow::get_type(),
[
'name' => 'title_shadow',
'selector' => '{{WRAPPER}} .elementor-accordion-title',
]
);
$this->add_responsive_control(
'title_margin',
[
'label' => esc_html__('Margin', 'motiox'),
'type' => Controls_Manager::DIMENSIONS,
'size_units' => ['px', '%', 'em'],
'selectors' => [
'{{WRAPPER}} .motiox-title' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
],
]
);
$this->end_controls_section();
$this->start_controls_section(
'section_style',
[
'label' => esc_html__('Style Breadcrumbs Item', 'motiox'),
'tab' => Controls_Manager::TAB_STYLE,
'condition' => [
'display_nav' => 'yes',
],
]
);
$this->add_control(
'text_color',
[
'label' => esc_html__('Text Color', 'motiox'),
'type' => Controls_Manager::COLOR,
'selectors' => [
'{{WRAPPER}} .breadcrumb' => 'color: {{VALUE}}',
],
]
);
$this->add_control(
'link_color',
[
'label' => esc_html__('Link Color', 'motiox'),
'type' => Controls_Manager::COLOR,
'selectors' => [
'{{WRAPPER}} .breadcrumb a' => 'color: {{VALUE}}',
],
]
);
$this->add_control(
'link_color_hover',
[
'label' => esc_html__('Link Color Hover', 'motiox'),
'type' => Controls_Manager::COLOR,
'selectors' => [
'{{WRAPPER}} .breadcrumb a:hover' => 'color: {{VALUE}}',
],
]
);
$this->add_group_control(
Group_Control_Typography::get_type(),
[
'name' => 'text_typography',
'selector' => '{{WRAPPER}} .breadcrumb',
]
);
$this->add_responsive_control(
'size_separator',
[
'label' => esc_html__('Separator Size', 'motiox'),
'type' => Controls_Manager::SLIDER,
'size_units' => ['px', 'em', 'rem', 'custom'],
'range' => [
'px' => [
'min' => 0,
'max' => 100,
],
],
'selectors' => [
'{{WRAPPER}} .breadcrumbs-separator i' => 'font-size: {{SIZE}}{{UNIT}}',
],
'condition' => [
'separator[value]!' => '',
],
]
);
$this->add_control(
'separator_color',
[
'label' => esc_html__('Separator Color', 'motiox'),
'type' => Controls_Manager::COLOR,
'condition' => [
'separator[value]!' => '',
],
'selectors' => [
'{{WRAPPER}} .breadcrumbs-separator i' => 'color: {{VALUE}}',
'{{WRAPPER}} .breadcrumbs-separator svg' => 'color: {{VALUE}}',
],
]
);
$this->end_controls_section();
}
public static function get_page_title($include_context = true) {
$title = '';
if (is_singular()) {
/* translators: %s: Search term. */
$title = get_the_title();
if ($include_context) {
$post_type_obj = get_post_type_object(get_post_type());
$title = sprintf('%s 12: %s', $post_type_obj->labels->singular_name, $title);
}
} elseif (is_home()) {
$title = esc_html__('Blog', 'motiox');
} elseif (is_search()) {
/* translators: %s: Search term. */
$title = sprintf(esc_html__('Search Results for: %s', 'motiox'), get_search_query());
if (get_query_var('paged')) {
/* translators: %s: Page number. */
$title .= sprintf(esc_html__(' – Page %s', 'motiox'), get_query_var('paged'));
}
} elseif (is_category()) {
$title = single_cat_title('', false);
if ($include_context) {
/* translators: Category archive title. %s: Category name. */
$title = sprintf(esc_html__('Category: %s', 'motiox'), $title);
}
} elseif (is_tag()) {
$title = single_tag_title('', false);
if ($include_context) {
/* translators: Tag archive title. %s: Tag name. */
$title = sprintf(esc_html__('Tag: %s', 'motiox'), $title);
}
} elseif (is_author()) {
$title = '<span class="vcard">' . get_the_author() . '</span>';
if ($include_context) {
/* translators: Author archive title. %s: Author name. */
$title = sprintf(esc_html__('Author: %s', 'motiox'), $title);
}
} elseif (is_year()) {
$title = get_the_date(_x('Y', 'yearly archives date format', 'motiox'));
if ($include_context) {
/* translators: Yearly archive title. %s: Year. */
$title = sprintf(esc_html__('Year: %s', 'motiox'), $title);
}
} elseif (is_month()) {
$title = get_the_date(_x('F Y', 'monthly archives date format', 'motiox'));
if ($include_context) {
/* translators: Monthly archive title. %s: Month name and a year. */
$title = sprintf(esc_html__('Month: %s', 'motiox'), $title);
}
} elseif (is_day()) {
$title = get_the_date(_x('F j, Y', 'daily archives date format', 'motiox'));
if ($include_context) {
/* translators: Daily archive title. %s: Date. */
$title = sprintf(esc_html__('Day: %s', 'motiox'), $title);
}
} elseif (is_tax('post_format')) {
if (is_tax('post_format', 'post-format-aside')) {
$title = _x('Asides', 'post format archive title', 'motiox');
} elseif (is_tax('post_format', 'post-format-gallery')) {
$title = _x('Galleries', 'post format archive title', 'motiox');
} elseif (is_tax('post_format', 'post-format-image')) {
$title = _x('Images', 'post format archive title', 'motiox');
} elseif (is_tax('post_format', 'post-format-video')) {
$title = _x('Videos', 'post format archive title', 'motiox');
} elseif (is_tax('post_format', 'post-format-quote')) {
$title = _x('Quotes', 'post format archive title', 'motiox');
} elseif (is_tax('post_format', 'post-format-link')) {
$title = _x('Links', 'post format archive title', 'motiox');
} elseif (is_tax('post_format', 'post-format-status')) {
$title = _x('Statuses', 'post format archive title', 'motiox');
} elseif (is_tax('post_format', 'post-format-audio')) {
$title = _x('Audio', 'post format archive title', 'motiox');
} elseif (is_tax('post_format', 'post-format-chat')) {
$title = _x('Chats', 'post format archive title', 'motiox');
}
} elseif (is_post_type_archive()) {
$title = post_type_archive_title('', false);
if ($include_context) {
/* translators: Post type archive title. %s: Post type name. */
$title = sprintf(esc_html__('Archives: %s', 'motiox'), $title);
}
} elseif (is_tax()) {
$title = single_term_title('', false);
if ($include_context) {
$tax = get_taxonomy(get_queried_object()->taxonomy);
/* translators: Taxonomy term archive title. 1: Taxonomy singular name, 2: Current taxonomy term. */
$title = sprintf(esc_html__('%1$s: %2$s', 'motiox'), $tax->labels->singular_name, $title);
}
} elseif (is_archive()) {
$title = esc_html__('Archives', 'motiox');
} elseif (is_404()) {
$title = esc_html__('Page Not Found', 'motiox');
} // End if().
/**
* Page title.
*
* Filters the title of the page.
*
* By default different pages have different titles depending of the page
* context (archive, singular, 404 etc.). This hook allows developers to
* alter those titles.
*
* @param string $title Page title to be displayed.
* @since 1.0.0
*
*/
$title = apply_filters('motiox_get_the_breadcrumbs_title', $title);
return $title;
}
protected function render() {
$settings = $this->get_settings_for_display();
$title = $this->get_page_title(false);
?>
<div class="breadcrumb" typeof="BreadcrumbList" vocab="https://schema.org/">
<?php if (isset($settings['display_title']) && $settings['display_title'] == 'yes'): ?>
<h1 class="motiox-title"> <?php echo wp_kses_post($title); ?></h1>
<?php endif; ?>
<?php
if (isset($settings['display_nav']) && $settings['display_nav'] == 'yes') {
$this->breadcrumb_item();
}
?>
</div>
<?php
}
public function breadcrumb_item() {
$settings = $this->get_settings_for_display();
$breadcrumbs = [];
$delimiter = '';
if ( isset($settings['separator']) ) {
ob_start();
Icons_Manager::render_icon( $settings['separator'], [ 'aria-hidden' => 'true' ] );
$delimiter = ob_get_clean();
}
$defaults = [
'home' => isset( $settings['home_text'] ) ? $settings['home_text'] : __( 'Motiox', 'motiox' ),
'404_title' => isset( $settings['error_text'] ) ? $settings['error_text'] : __( 'Page not found', 'motiox' ),
'search_title' => isset( $settings['search_text'] ) ? $settings['search_text'] : __( 'Search results for: ', 'motiox' ),
];
$breadcrumbs[] = [
'title' => $defaults['home'],
'url' => esc_url( home_url() ),
'class' => 'hfe-breadcrumbs-first',
];
if ( ! is_front_page() ) {
if ( is_home()) {
$breadcrumbs[] = [
'title' => get_the_title( get_option( 'page_for_posts' ) ),
'url' => '',
'class' => '',
];
} elseif ( is_single() ) {
$post_type = get_post_type();
$post_type_object = get_post_type_object( $post_type );
if ( $post_type_object ) {
$breadcrumbs[] = [
'title' => $post_type_object->labels->name,
'url' => esc_url( get_post_type_archive_link( $post_type ) ),
'class' => '',
];
}
if ( $post_type === 'post' ) {
$categories = get_the_category();
} else {
$taxonomy = 'category';
if ( $post_type !== 'post' ) {
$taxonomy = $post_type . '_cat';
}
$categories = get_the_terms( get_the_ID(), $taxonomy );
}
if ( $categories && ! is_wp_error( $categories ) ) {
$selected_category = null;
foreach ( $categories as $category ) {
if ( $category->parent === 0 ) {
$selected_category = $category;
break;
}
}
if ( ! $selected_category ) {
$selected_category = $categories[0];
}
if ( $selected_category ) {
$breadcrumbs[] = [
'title' => $selected_category->name,
'url' => esc_url( get_term_link( $selected_category ) ),
'class' => '',
];
}
}
$breadcrumbs[] = [
'title' => get_the_title(),
'url' => '',
'class' => 'hfe-breadcrumbs-last',
];
} elseif ( is_page() && ! is_front_page() ) {
$parents = get_post_ancestors( get_the_ID() );
foreach ( array_reverse( $parents ) as $parent ) {
$breadcrumbs[] = [
'title' => get_the_title( $parent ),
'url' => esc_url( get_permalink( $parent ) ),
'class' => '',
];
}
$breadcrumbs[] = [
'title' => get_the_title(),
'url' => '',
'class' => 'hfe-breadcrumbs-last',
];
} elseif ( is_category() || is_archive() ) {
$post_type = get_post_type();
$post_type_object = get_post_type_object( $post_type );
if ( $post_type_object ) {
$archive_link = get_post_type_archive_link( $post_type );
if ( $archive_link ) {
$breadcrumbs[] = [
'title' => $post_type_object->labels->name,
'url' => esc_url( $archive_link ),
'class' => '',
];
}
}
$current_category = single_cat_title( '', false );
if ( $current_category ) {
$breadcrumbs[] = [
'title' => esc_html( $current_category ),
'url' => '',
'class' => 'hfe-breadcrumbs-last',
];
}
} elseif ( is_tag() ) {
$breadcrumbs[] = [
'title' => single_tag_title( '', false ),
'url' => '',
'class' => 'hfe-breadcrumbs-last',
];
} elseif ( is_author() ) {
$breadcrumbs[] = [
'title' => get_the_author(),
'url' => '',
'class' => 'hfe-breadcrumbs-last',
];
} elseif ( is_search() ) {
$breadcrumbs[] = [
'title' => $defaults['search_title'] . get_search_query(),
'url' => '',
'class' => 'hfe-breadcrumbs-last',
];
} elseif ( is_404() ) {
$breadcrumbs[] = [
'title' => $defaults['404_title'],
'url' => '',
'class' => 'hfe-breadcrumbs-last',
];
}
}
$output = '<div class="breadcrumb-listItem">';
foreach ( $breadcrumbs as $index => $breadcrumb ) {
$output .= '<span class="breadcrumbs-item ' . esc_attr( $breadcrumb['class'] ) . '">';
if ( $breadcrumb['url'] ) {
$output .= '<a href="' . esc_url( $breadcrumb['url'] ) . '"><span class="breadcrumbs-text">' . wp_kses_post( $breadcrumb['title'] ) . '</span></a>';
} else {
$output .= '<span class="breadcrumbs-text">' . wp_kses_post( $breadcrumb['title'] ) . '</span>';
}
$output .= '</span>';
if ( $index < count( $breadcrumbs ) - 1 ) {
$output .= '<span class="breadcrumbs-separator">';
$output .= $delimiter;
$output .= '</span>';
}
}
$output .= '</div>';
echo wp_kses_post($output);
}
}
$widgets_manager->register(new Motiox_Elementor_Breadcrumb());