Block.php
3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace AIOSEO\Plugin\Common\Sitemap\Html;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles the HTML sitemap block.
*
* @since 4.1.3
*/
class Block {
/**
* Class constructor.
*
* @since 4.1.1
*/
public function __construct() {
$this->register();
}
/**
* Registers the block.
*
* @since 4.1.3
*
* @return void
*/
public function register() {
aioseo()->blocks->registerBlock(
'aioseo/html-sitemap', [
'attributes' => [
'default' => [
'type' => 'boolean',
'default' => true
],
'post_types' => [
'type' => 'string',
'default' => wp_json_encode( [ 'post', 'page' ] )
],
'post_types_all' => [
'type' => 'boolean',
'default' => true
],
'taxonomies' => [
'type' => 'string',
'default' => wp_json_encode( [ 'category', 'post_tag' ] )
],
'taxonomies_all' => [
'type' => 'boolean',
'default' => true
],
'show_label' => [
'type' => 'boolean',
'default' => true
],
'archives' => [
'type' => 'boolean',
'default' => false
],
'publication_date' => [
'type' => 'boolean',
'default' => true
],
'nofollow_links' => [
'type' => 'boolean',
'default' => false
],
'order_by' => [
'type' => 'string',
'default' => 'publish_date'
],
'order' => [
'type' => 'string',
'default' => 'asc'
],
'excluded_posts' => [
'type' => 'string',
'default' => wp_json_encode( [] )
],
'excluded_terms' => [
'type' => 'string',
'default' => wp_json_encode( [] )
],
'is_admin' => [
'type' => 'boolean',
'default' => false
]
],
'render_callback' => [ $this, 'render' ],
'editor_style' => 'aioseo-html-sitemap'
]
);
}
/**
* Renders the block.
*
* @since 4.1.3
*
* @param array $attributes The attributes.
* @return string The HTML sitemap code.
*/
public function render( $attributes ) {
if ( ! $attributes['default'] ) {
$jsonFields = [ 'post_types', 'taxonomies', 'excluded_posts', 'excluded_terms' ];
foreach ( $attributes as $k => $v ) {
if ( in_array( $k, $jsonFields, true ) ) {
$attributes[ $k ] = json_decode( $v );
}
}
$attributes['excluded_posts'] = $this->extractIds( $attributes['excluded_posts'] );
$attributes['excluded_terms'] = $this->extractIds( $attributes['excluded_terms'] );
if ( ! empty( $attributes['post_types_all'] ) ) {
$attributes['post_types'] = aioseo()->helpers->getPublicPostTypes( true );
}
if ( ! empty( $attributes['taxonomies_all'] ) ) {
$attributes['taxonomies'] = aioseo()->helpers->getPublicTaxonomies( true );
}
} else {
$attributes = [];
}
$attributes = aioseo()->htmlSitemap->frontend->getAttributes( $attributes );
return aioseo()->htmlSitemap->frontend->output( false, $attributes );
}
/**
* Extracts the IDs from the excluded objects.
*
* @since 4.1.3
*
* @param array $objects The objects.
* @return array The object IDs.
*/
private function extractIds( $objects ) {
return array_map( function ( $object ) {
$object = json_decode( $object );
return (int) $object->value;
}, $objects );
}
}