-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathWidget_Area.php
344 lines (293 loc) · 10 KB
/
Widget_Area.php
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php // phpcs:ignore WordPress.Files.FileName
/**
* Migrate arbitrary content to blocks
*
* @since 3.0.0
* @package gridd
*/
namespace Gridd\Upgrades\Block;
/**
* The block-migrator object.
*
* @since 3.0.0
*/
class Widget_Area extends \Gridd\Upgrades\Block_Migrator {
/**
* The widget-area slug.
*
* @access protected
* @since 3.0.0
* @var string
*/
protected $widget_area_slug;
/**
* An array of our widgets.
*
* @access protected
* @since 3.0.0
* @var array
*/
protected $widgets;
/**
* Constructor.
*
* @access public
* @param array $args The constructor arguments.
* @since 3.0.0
*/
public function __construct( $args ) {
// Set the sidebar title & slug.
$this->set_object_params( $args );
parent::__construct();
}
/**
* Set the object params.
*
* @access protected
* @since 3.0.0
* @param array $args The arguments passed-on from the constructor.
* @return void
*/
public function set_object_params( $args ) {
$this->widget_area_slug = $args[0];
$this->widgets = $args[1];
$this->slug = 'gridd-' . str_replace( '_', '-', $this->widget_area_slug );
$this->title = 'Gridd: ' . $this->widget_area_slug;
}
/**
* Get the contents of the block we want to add.
*
* @access protected
* @since 3.0.0
* @return string
*/
protected function get_content() {
global $wp_registered_widgets;
$content = '';
// Loop widget areas.
foreach ( $this->widgets as $widget_id ) {
$class = $wp_registered_widgets[ $widget_id ]['callback'][0];
$class_name = get_class( $class );
$method_name = 'get_contents_' . strtolower( $class_name );
if ( is_callable( [ $this, $method_name ] ) ) {
$content .= $this->$method_name( $widget_id, $class );
}
}
return $content;
}
/**
* Get the widget title.
*
* @access protected
* @since 3.0.0
* @param array $args The widget arguments.
* @return string
*/
protected function get_widget_title( $args ) {
if ( isset( $args['title'] ) && ! empty( trim( $args['title'] ) ) ) {
return '<!-- wp:heading {"level":3} --><h3>' . $args['title'] . '</h3><!-- /wp:heading -->';
}
return '';
}
/**
* Gets the block contents for WP_Widget_Archives.
*
* @access protected
* @since 3.0.0
* @param string $widget_id The widget-ID.
* @param WP_Widget_Archives $class The widget class.
* @return string
*/
protected function get_contents_wp_widget_archives( $widget_id, $class ) {
// Get the block settings.
$settings = $class->get_settings()[ absint( str_replace( 'archives-', '', $widget_id ) ) ];
// Build the arguments.
$args = [
'displayAsDropdown' => ( isset( $settings['dropdown'] ) && $settings['dropdown'] ),
'showPostCounts' => ( isset( $settings['count'] ) && $settings['count'] ),
];
$content = $this->get_widget_title( $settings );
$content .= '<!-- wp:archives ' . wp_json_encode( $args ) . ' /-->';
return $content;
}
/**
* Gets the block contents for WP_Widget_Categories.
*
* @access protected
* @since 3.0.0
* @param string $widget_id The widget-ID.
* @param WP_Widget_Categories $class The widget class.
* @return string
*/
protected function get_contents_wp_widget_categories( $widget_id, $class ) {
// Get the block settings.
$settings = $class->get_settings()[ absint( str_replace( 'categories-', '', $widget_id ) ) ];
// Build the arguments.
$args = [
'displayAsDropdown' => ( isset( $settings['dropdown'] ) && $settings['dropdown'] ),
'showHierarchy' => ( isset( $settings['hierarchical'] ) && $settings['hierarchical'] ),
'showPostCounts' => ( isset( $settings['count'] ) && $settings['count'] ),
];
$content = $this->get_widget_title( $settings );
$content .= '<!-- wp:categories ' . wp_json_encode( $args ) . ' /-->';
return $content;
}
/**
* Gets the block contents for WP_Widget_Media_Audio.
*
* @access protected
* @since 3.0.0
* @param string $widget_id The widget-ID.
* @param WP_Widget_Media_Audio $class The widget class.
* @return string
*/
protected function get_contents_wp_widget_media_audio( $widget_id, $class ) {
// Get the block settings.
$settings = $class->get_settings()[ absint( str_replace( 'media_audio-', '', $widget_id ) ) ];
$url = false;
foreach ( [ 'mp3', 'ogg', 'flac', 'm4a', 'wav' ] as $filetype ) {
if ( isset( $settings[ $filetype ] ) && ! empty( $settings[ $filetype ] ) ) {
$url = $settings[ $filetype ];
}
}
$content = $this->get_widget_title( $settings );
$content .= '<!-- wp:audio {"id":' . $settings['attachment_id'] . '} -->';
$content .= '<figure class="wp-block-audio">';
$content .= '<audio controls src="' . $url . '" autoplay';
if ( isset( $settings['loop'] ) && $settings['loop'] ) {
$content .= ' loop';
}
if ( isset( $settings['preload'] ) ) {
$content .= ' preload="' . $settings['preload'] . '">';
}
$content .= '</audio></figure><!-- /wp:audio -->';
return $content;
}
/**
* Gets the block contents for WP_Widget_Calendar.
*
* @access protected
* @since 3.0.0
* @param string $widget_id The widget-ID.
* @param WP_Widget_Calendar $class The widget class.
* @return string
*/
protected function get_contents_wp_widget_calendar( $widget_id, $class ) {
// Get the block settings.
$settings = $class->get_settings()[ absint( str_replace( 'calendar-', '', $widget_id ) ) ];
$content = $this->get_widget_title( $settings );
$content .= '<!-- wp:calendar /-->';
return $content;
}
/**
* Gets the block contents for WP_Widget_Custom_HTML.
*
* @access protected
* @since 3.0.0
* @param string $widget_id The widget-ID.
* @param WP_Widget_Custom_HTML $class The widget class.
* @return string
*/
protected function get_contents_wp_widget_custom_html( $widget_id, $class ) {
// Get the block settings.
$settings = $class->get_settings()[ absint( str_replace( 'custom_html-', '', $widget_id ) ) ];
$content = $this->get_widget_title( $settings );
$content .= '<!-- wp:html -->' . $settings['content'] . '<!-- /wp:html -->';
return $content;
}
/**
* Gets the block contents for WP_Widget_Media_Gallery.
*
* @access protected
* @since 3.0.0
* @param string $widget_id The widget-ID.
* @param WP_Widget_Media_Gallery $class The widget class.
* @return string
*/
protected function get_contents_wp_widget_media_gallery( $widget_id, $class ) {
// Get the block settings.
$settings = $class->get_settings()[ absint( str_replace( 'media_gallery-', '', $widget_id ) ) ];
$content = $this->get_widget_title( $settings );
$content .= '<!-- wp:gallery ' . wp_json_encode( [ 'ids' => $settings['ids'] ] ) . ' -->';
$content .= '<figure class="wp-block-gallery columns-' . $settings['columns'] . ' is-cropped">';
$content .= '<ul class="blocks-gallery-grid">';
foreach ( $settings['ids'] as $img_id ) {
$meta = wp_get_attachment_metadata( $img_id );
$content .= '<li class="blocks-gallery-item"><figure>';
$content .= '<img src="' . $meta['sizes'][ $settings['size'] ] . '" alt="" data-id="' . $img_id . '" data-full-url="' . wp_get_attachment_url( $img_id ) . '" data-link="' . wp_get_attachment_url( $img_id ) . '" class="wp-image-' . $img_id . '"/>';
$content .= '</figure></li>';
}
$content .= '</ul></figure><!-- /wp:gallery -->';
return $content;
}
/**
* Gets the block contents for WP_Widget_Media_Image.
*
* @access protected
* @since 3.0.0
* @param string $widget_id The widget-ID.
* @param WP_Widget_Media_Image $class The widget class.
* @return string
*/
protected function get_contents_wp_widget_media_image( $widget_id, $class ) {
// Get the block settings.
$settings = $class->get_settings()[ absint( str_replace( 'media_image-', '', $widget_id ) ) ];
$image_args = [
'id' => $settings['attachment_id'],
'width' => $settings['width'],
'height' => $settings['height'],
'sizeSlug' => $settings['size'],
];
$content = $this->get_widget_title( $settings );
$content .= '<!-- wp:image ' . wp_json_encode( $image_args ) . ' -->';
$content .= '<figure class="wp-block-image size-' . $settings['size'] . ' is-resized">';
if ( $settings['link_url'] ) {
$content .= '<a href="' . $settings['link_url'] . '">';
}
$content .= '<img src="' . $settings['url'] . '" alt="' . $settings['alt'] . '" class="wp-image-' . $settings['attachment_id'] . '" width="' . $settings['width'] . '" height="' . $settings['height'] . '"/>';
if ( $settings['link_url'] ) {
$content .= '</a>';
}
if ( $settings['caption'] ) {
$content .= '<figcaption>' . $settings['caption'] . '</figcaption>';
}
$content .= '</figure><!-- /wp:image -->';
return $content;
}
/**
* Gets the block contents for WP_Widget_Recent_Comments.
*
* @access protected
* @since 3.0.0
* @param string $widget_id The widget-ID.
* @param WP_Widget_Recent_Comments $class The widget class.
* @return string
*/
protected function get_contents_wp_widget_recent_comments( $widget_id, $class ) {
// Get the block settings.
$settings = $class->get_settings()[ absint( str_replace( 'recent-comments-', '', $widget_id ) ) ];
return '<!-- wp:latest-comments {"commentsToShow":' . $settings['number'] . '} /-->';
}
/**
* Gets the block contents for WP_Widget_RSS.
*
* @access protected
* @since 3.0.0
* @param string $widget_id The widget-ID.
* @param WP_Widget_RSS $class The widget class.
* @return string
*/
protected function get_contents_wp_widget_rss( $widget_id, $class ) {
// Get the block settings.
$settings = $class->get_settings()[ absint( str_replace( 'rss-', '', $widget_id ) ) ];
$args = [
'feedURL' => $settings['url'],
'itemsToShow' => $settings['items'],
'displayExcerpt' => (bool) $settings['show_summary'],
'displayAuthor' => (bool) $settings['show_author'],
'displayDate' => (bool) $settings['show_date'],
'excerptLength' => 50,
];
return '<!-- wp:rss ' . wp_json_encode( $args ) . ' /-->';
}
}