-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatherpress-feed-buttons.php
422 lines (354 loc) · 12.9 KB
/
gatherpress-feed-buttons.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php
/**
* Plugin Name: Gatherpress feed-buttons block
* Description: ...
* Version: 0.1.0-alpha
* Requires at least: 6.4
* Requires PHP: 8.1
* Author: The WordPress Contributors
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: gatherpress-feed-buttons
*
* @package create-block
*/
namespace GatherPressFeedButtons;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
use GatherPress\Core\Venue;
/**
* DEBUG ONLY
*
* Using a global here is just because im lazy.
* Will be removed before merge!!
*/
$gatherpress_TEMP_feed_type = '';
/**
* Start the engines.
*
* @return void
*/
function bootstrap(): void {
add_action( 'init', __NAMESPACE__ . '\\register_assets', 1 );
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\enqueue_assets' );
// God damn!!!!
//
// https://core.trac.wordpress.org/ticket/59551
// [...] This means anybody following the example usage of that filter could end up
// with their return not respected unless they change priority to higher than 10,
// which is not documented!
//
add_filter( 'pre_render_block', __NAMESPACE__ . '\\pre_render_post_terms_block', 11, 2 );
// https://developer.wordpress.org/reference/hooks/render_block_this-name/
add_filter( 'render_block_core/post-terms', __NAMESPACE__ . '\\render_post_terms_block', 10, 3 );
// God damn!!!!
//
// https://core.trac.wordpress.org/ticket/59551
// [...] This means anybody following the example usage of that filter could end up
// with their return not respected unless they change priority to higher than 10,
// which is not documented!
//
// Prevent block rendering if no 'online-event' term exists.
# add_filter( 'pre_render_block', __NAMESPACE__ . '\\pre_render_button_block', 11, 2 );
/**
* Filters the term link.
*
* @param string $termlink Term link URL.
* @param \WP_Term $term Term object.
* @param string $taxonomy Taxonomy slug.
* @return string Term link URL.
*/
add_filter('term_link',function( string $termlink, \WP_Term $term, string $taxonomy ) : string {
if ('_gatherpress_venue' !== $taxonomy ) {
return $termlink;
}
$gatherpress_venue = Venue::get_instance()->get_venue_post_from_term_slug( $term->slug );
// An Online-Event will have no Venue; prevent error on non-existent object.
// Feels weird to use a *_comments_* function here, but it delivers clean results
// in the form of "domain.tld/event/my-sample-event/feed/ical/".
// $href = ( $gatherpress_venue ) ? get_post_comments_feed_link( $gatherpress_venue->ID, self::ICAL_SLUG ) : null;
return get_permalink( $gatherpress_venue );
// return $termlink;
}, 10, 3 );
/**
* Filters the feed link for a taxonomy other than 'category' or 'post_tag'.
*
* @param string $link The taxonomy feed link.
* @param string $feed Feed type. Possible values include 'rss2', 'atom'.
* @param string $taxonomy The taxonomy name.
* @return string The taxonomy feed link.
*/
// add_filter('taxonomy_feed_link', function( string $link, string $feed, string $taxonomy ) : string {
// if ('_gatherpress_venue' !== $taxonomy ) {
// return $link;
// }
// $gatherpress_venue = Venue::get_instance()->get_venue_post_from_term_slug( $term->slug );
// // An Online-Event will have no Venue; prevent error on non-existent object.
// // Feels weird to use a *_comments_* function here, but it delivers clean results
// // in the form of "domain.tld/event/my-sample-event/feed/ical/".
// $link = ( $gatherpress_venue ) ? get_post_comments_feed_link( $gatherpress_venue->ID, $feed ) : null;
// return $link;
// }, 10, 3 );
/**
* ATTENTION
*
* This includes BREAKING CHANGES, because permalinks may change and must be reset.
*/
\add_filter(
'register_' . '_gatherpress_venue' . '_taxonomy_args',
function ( $args ) {
/**
* Event-Archives per Venue, like archives for categories or tags.
*
* Disable this option to:
* - avoid duplicated content issues,
* - keep your sitemaps clean,
* - avoid having a venue-tag-cloud (block),
* - and to keep the block-inserter free from unnecessary clutter.
*
* Depending on the used theme AND the Use-case, enabling archives maybe from interest:
*
* 1. Meetup Group
* Has only one physical venue, maybe online-events
* => Does not need Event-Archives per Venue.
*
* 2. Patricias Choir
* Has multiple venues
* => Could benefit from Event-Archives per Venue.
*
* 3. A theater (portal) website
* Has multiple venues, maybe hybrid & maybe online-events
* => Absolutely needs Event-Archives per Venue.
*/
/**
* QUESTION: Wasn't there a filter to add or remove settings?
*/
// $settings = Settings::get_instance();
// $with_venue_archives = $settings->get_value( 'general', 'general', 'with_venue_archives' ),
$with_venue_archives = true;
// $with_venue_archives = false;
// $args['rewrite'] = false; // Results in https://gatherpress.test/?_gatherpress_venue=_new-york
// $args['publicly_queryable'] = false; // Results in: nothing rendered in frontend & block says: "Term items not found".
// $args['publicly_queryable'] = \is_admin(); // Results in: nothing rendered in frontend & block says: "Term items not found".
// $args['public'] = $with_venue_archives;
// $args['publicly_queryable'] = $with_venue_archives ?? true;
$args['publicly_queryable'] = $with_venue_archives;
// $_args_rewrite = ( isset( $args['rewrite'] ) ) ? $args['rewrite'] : [];
// $args['rewrite'] = get_rewrite_argument( $with_venue_archives, $_args_rewrite );
// $args['rewrite'] = 'venue';
return $args;
}
);
}
bootstrap();
/**
* Get backend-only editor assets.
*
* @return string[]
*/
function get_editor_assets(): array {
return [
// 'feed-buttons',
];
}
/**
*
*
* @return void
*/
function register_assets(): void {
\array_map(
__NAMESPACE__ . '\\register_asset',
\array_merge(
get_editor_assets(),
[
'variations',
]
)
);
}
/**
* Enqueue all scripts.
*
* @return void
*/
function enqueue_assets(): void {
\array_map(
__NAMESPACE__ . '\\enqueue_asset',
// get_editor_assets()
[
'variations',
]
);
}
/**
* Enqueue a script.
*
* @param string $asset Slug of the block to load the frontend scripts for.
*
* @return void
*/
function enqueue_asset( string $asset ): void {
wp_enqueue_script( "gatherpress-feed-buttons--$asset" );
// wp_enqueue_style( "gatherpress-feed-buttons--$asset" );
}
/**
* Register a new script and sets translated strings for the script.
*
* @throws \Error If build-files doesn't exist errors out in local environments and writes to error_log otherwise.
*
* @param string $asset Slug of the block to register scripts and translations for.
*
* @return void
*/
function register_asset( string $asset ): void {
$dir = __DIR__;
$script_asset_path = "$dir/build/$asset/$asset.asset.php";
if ( ! \file_exists( $script_asset_path ) ) {
$error_message = "You need to run `npm start` or `npm run build` for the '$asset' block-asset first.";
if ( \in_array( wp_get_environment_type(), [ 'local', 'development' ], true ) ) {
throw new \Error( esc_html( $error_message ) );
} else {
// Should write to the \error_log( $error_message ); if possible.
return;
}
}
$index_js = "build/$asset/$asset.js";
$script_asset = require $script_asset_path; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingVariable
\wp_register_script(
"gatherpress-feed-buttons--$asset",
plugins_url( $index_js, __FILE__ ),
$script_asset['dependencies'],
$script_asset['version'],
true
);
// $index_css = "build/$asset/$asset.css";
// \wp_register_style(
// "gatherpress-feed-buttons--$asset",
// plugins_url( $index_css, __FILE__ ),
// [ 'wp-block-post-date','global-styles' ],
// time(),
// 'screen'
// );
wp_set_script_translations(
"gatherpress-feed-buttons--$asset",
'gatherpress',
"$dir/languages"
);
}
function append_feed( string $termlink, \WP_Term $term, string $taxonomy ) : string {
global $gatherpress_TEMP_feed_type;
$feed_type = ( 'rss' === $gatherpress_TEMP_feed_type ) ? '' : $gatherpress_TEMP_feed_type;
// if ('_gatherpress_venue' === $taxonomy ) {
// $gatherpress_venue = Venue::get_instance()->get_venue_post_from_term_slug( $term->slug );
// // An Online-Event will have no Venue; prevent error on non-existent object.
// // Feels weird to use a *_comments_* function here, but it delivers clean results
// // in the form of "domain.tld/event/my-sample-event/feed/ical/".
// return ( $gatherpress_venue ) ? get_post_comments_feed_link( $gatherpress_venue->ID, $feed_type ) : '';
// }
// Avoid regression
remove_filter( 'term_link', __NAMESPACE__ . '\\append_feed', 10 );
return get_term_feed_link( $term->term_id, $term->taxonomy, $feed_type );
// return $termlink;
}
function pre_render_post_terms_block( $pre_render, $block ) {
// if ( ! isset( $block['attrs']['className'] ) || false === \strpos( $block['attrs']['className'], 'gatherpress-feed-buttons' ) ) {
if ( ! isset( $block['attrs']['feed'] ) ) {
return $pre_render;
}
// error_log(var_export([
// // $pre_render,
// $block,
// ],true));
global $gatherpress_TEMP_feed_type;
$gatherpress_TEMP_feed_type = $block['attrs']['feed'];
/**
* Filters the term link.
*
* @param string $termlink Term link URL.
* @param \WP_Term $term Term object.
* @param string $taxonomy Taxonomy slug.
* @return string Term link URL.
*/
add_filter( 'term_link', __NAMESPACE__ . '\\append_feed', 10, 3 );
return $pre_render;
}
/**
* Filter the render_block to add the needed directives to the inner cover blocks.
*
* @see https://developer.wordpress.org/reference/hooks/render_block_this-name/
*
* @param string $block_content The content being rendered by the block.
*/
function render_post_terms_block( $block_content, $block, $instance ) {
// if ( ! isset( $block['attrs']['className'] ) || false === \strpos( $block['attrs']['className'], 'gatherpress-feed-buttons' ) ) {
if ( ! isset( $block['attrs']['feed'] ) ) {
return $block_content;
}
global $gatherpress_TEMP_feed_type;
unset( $gatherpress_TEMP_feed_type );
remove_filter( 'term_link', __NAMESPACE__ . '\\append_feed', 10 );
// error_log(var_export([
// $block_content,
// $block,
// ],true));
return $block_content;
}
/**
* Prevent block rendering if no 'online-event' term exists.
*
* Allows render_block() to be short-circuited, by returning a non-null value.
*/
function pre_render_button_block( $pre_render, $parsed_block ) {
if ( isset( $parsed_block['attrs']['className'] ) && false !== \strpos( $parsed_block['attrs']['className'], 'gatherpress-feed-buttons-button' ) ) {
// Will short-circuit if no 'online-event' term exists.
if ( ! \has_term( 'online-event', '_gatherpress_venue', get_post()->ID ) ) {
return false; // And do not render the block at all.
// @TODO
// Can still be a real venue.
// Maybe show the website of the venue here ???
// return '';
}
// DEMO & DEBUG ONLY !!!
$on_off = ( 1 === rand( 1, 2 ) ) ? '__return_true' : '__return_false';
\add_filter( 'gatherpress_force_online_event_link', $on_off );
// Enable modifications to the block.
add_filter( 'render_block_core/button', __NAMESPACE__ . '\\render_button_block', 10, 3 );
}
return $pre_render;
}
/**
* Filter the render_block to ...
*
* At this point we already know, that the event has the 'online-event' term.
*
* @see https://developer.wordpress.org/reference/hooks/render_block_this-name/
*
* @param string $block_content The content being rendered by the block.
*/
function render_button_block( $block_content, $block, $instance ) {
if ( ! isset( $block['attrs']['className'] ) || false === \strpos( $block['attrs']['className'], 'gatherpress-feed-buttons-button' ) ) {
return $block_content;
}
// Change behaviour ...
// ... or remove rendering.
// return '';
if ( empty( get_block_binding_values( [ 'key' => 'url' ], $instance ) ) ) {
$button = new \WP_HTML_Tag_Processor( $block_content );
if ( $button->next_tag( 'a' ) ) {
// Inform the user with a spinning cursor and a waiting message.
$button->set_attribute( 'style', 'cursor:wait;' . $button->get_attribute( 'style' ) );
$button->set_attribute( 'title', __( 'Link is visible to attendees only.', 'gatherpress' ) . '(CHANGES BY RANDOM FOR THE DEMO)' );
// Allow for styling with CSS.
$button->add_class( 'gatherpress-feed-buttons-button__disabled' );
// Prevent click & focus,
// by removing the href, which works better than 'disabled'.
$button->remove_attribute( 'href' );
// Return modified HTML.
$block_content = $button->get_updated_html();
}
}
// // // DEMO & DEBUG ONLY !!!
\remove_all_filters( 'gatherpress_force_online_event_link' );
return $block_content;
}