print_render_attribute_string( 'wrapper' );
echo '>';
foreach ( $settings['sitemap_items'] as $sitemap_item ) {
// PHPCS - `render_sitemap_item` is safe.
echo $this->render_sitemap_item( $sitemap_item, $title_tag, $posts_query ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
echo '
';
}
private function get_list_title( $current_title, $item_type, $is_taxonomy ) {
if ( '' !== $current_title ) {
return $current_title;
}
if ( $is_taxonomy ) {
$obj = get_taxonomy( $item_type );
if ( false === $obj ) {
return '';
}
return $obj->label;
}
$obj = get_post_type_object( $item_type );
if ( null === $obj ) {
return '';
}
if ( '' === $obj->labels->name ) {
return $obj->labels->singular_name;
}
return $obj->labels->name;
}
private function render_sitemap_item( $sitemap_item, $title_tag, $query_args ) {
$hierarchical = 'yes' === $sitemap_item['sitemap_hierarchical'];
$max_depth = $sitemap_item['sitemap_depth'];
$query_args['orderby'] = $sitemap_item['sitemap_orderby_post_type'];
$query_args['order'] = $sitemap_item['sitemap_order'];
$is_taxonomy = 'taxonomy' === $sitemap_item['sitemap_type_selector'];
$item_type = $is_taxonomy ? $sitemap_item['sitemap_source_taxonomy'] : $sitemap_item['sitemap_source_post_type'];
$title = $this->get_list_title( $sitemap_item['sitemap_title'], $item_type, $is_taxonomy );
$this->add_render_attribute( [
'section' . $item_type => [
'class' => [
'elementor-sitemap-section',
],
],
'list' . $item_type => [
'class' => [
'elementor-sitemap-list',
'elementor-sitemap-' . $item_type . '-list',
],
],
$title_tag . $item_type => [
'class' => [
'elementor-sitemap-title',
'elementor-sitemap-' . $item_type . '-title',
],
],
'item' . $item_type => [
'class' => [
'elementor-sitemap-item',
'elementor-sitemap-item-' . $item_type,
],
],
] );
$items_html = '';
if ( $is_taxonomy ) {
$items_html .= $this->sitemap_html_taxonomies( $item_type, $hierarchical, $max_depth, $sitemap_item, $query_args );
} else {
$items_html .= $this->sitemap_html_post_types( $item_type, $hierarchical, $max_depth, $query_args );
}
$title = empty( $title ) ? '' : sprintf( '<%s %s>%s%1$s>', Utils::validate_html_tag( $title_tag ), $this->get_render_attribute_string( $title_tag . $item_type ), $title );
$html = sprintf( '%s', $this->get_render_attribute_string( 'section' . $item_type ), $title );
if ( empty( $items_html ) ) {
$html .= sprintf( '
%s', $this->get_render_attribute_string( 'list' . $item_type ), esc_html__( 'None', 'elementor-pro' ) );
} else {
$html .= sprintf( '
', $this->get_render_attribute_string( 'list' . $item_type ), $items_html );
}
$html .= '
';
return $html;
}
private function sitemap_html_taxonomies( $taxonomy, $hierarchical, $max_depth, $item_settings, $query_args ) {
$query_args['hide_empty'] = 'yes' === $item_settings['sitemap_hide_empty'];
$query_args['show_option_none'] = '';
$query_args['taxonomy'] = $taxonomy;
$query_args['title_li'] = '';
$query_args['echo'] = false;
$query_args['depth'] = $max_depth;
$query_args['hierarchical'] = $hierarchical;
$query_args['orderby'] = $item_settings['sitemap_orderby_taxonomy'];
$taxonomy_list = wp_list_categories( $query_args );
$taxonomy_list = $this->add_sitemap_item_classes( 'item' . $taxonomy, $taxonomy_list );
return $taxonomy_list;
}
/**
* @param string $post_type
* @param array $query_args
*
* @return \WP_Query
*/
private function query_by_post_type( $post_type, $query_args ) {
$args = [
'posts_per_page' => -1,
'update_post_meta_cache' => false,
'post_type' => $post_type,
'filter' => 'ids',
'post_status' => 'publish',
];
$args = array_merge( $query_args, $args );
$query = new \WP_Query( $args );
return $query;
}
/**
* @param string $post_type
* @param bool $hierarchical
* @param int $depth
* @param array $query_args
*
* @return string
*/
private function sitemap_html_post_types( $post_type, $hierarchical, $depth, $query_args ) {
$html = '';
$query_result = $this->query_by_post_type( $post_type, $query_args );
if ( empty( $query_result ) ) {
return '';
}
if ( $query_result->have_posts() ) {
if ( ! $hierarchical ) {
$depth = -1;
}
$walker = new \Walker_Page();
$walker->tree_type = $post_type;
$walker_str = $walker->walk( $query_result->posts, $depth );
$html .= $this->add_sitemap_item_classes( 'item' . $post_type, $walker_str );
}
return $html;
}
private function add_sitemap_item_classes( $element, $str ) {
$element_str = $this->get_render_attribute_string( $element );
/** remove trailing " */
$element_str = substr_replace( $element_str, ' ', -1, 1 );
$source = [
'class="',
];
$replace = [
$element_str,
];
if ( 'yes' === $this->get_settings_for_display( 'sitemap_add_nofollow' ) ) {
$source[] = 'href=';
$replace[] = 'rel="nofollow" href=';
}
return str_replace( $source, $replace, $str );
}
public function get_group_name() {
return 'theme-elements';
}
}