-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathplanet4-gutenberg-blocks.php
335 lines (295 loc) · 9.88 KB
/
planet4-gutenberg-blocks.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
<?php
/**
* Plugin Name: Planet4 - Gutenberg Blocks
* Description: Contains the Gutenberg blocks that are used by Planet4 project.
* Plugin URI: http://github.com/greenpeace/planet4-plugin-gutenberg-blocks
* Php Version: 7.0
*
* Author: Greenpeace International
* Author URI: http://www.greenpeace.org/
* Text Domain: planet4-blocks
*
* License: GPLv3
* Copyright (C) 2018 Greenpeace International
*
* @package P4GBKS
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || die( 'Direct access is forbidden !' );
// This plugin vendor dir.
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
require_once __DIR__ . '/vendor/autoload.php';
}
// Local env base vendor dir.
if ( file_exists( WP_CONTENT_DIR . '/vendor/autoload.php' ) ) {
require_once WP_CONTENT_DIR . '/vendor/autoload.php';
}
// Prod env base vendor dir.
if ( file_exists( dirname( ABSPATH ) . '/vendor/autoload.php' ) ) {
require_once dirname( ABSPATH ) . '/vendor/autoload.php';
}
/*
========================
C O N S T A N T S
========================
*/
if ( ! defined( 'P4GBKS_REQUIRED_PHP' ) ) {
define( 'P4GBKS_REQUIRED_PHP', '8.1' );
}
if ( ! defined( 'P4GBKS_PLUGIN_BASENAME' ) ) {
define( 'P4GBKS_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
}
if ( ! defined( 'P4GBKS_PLUGIN_DIRNAME' ) ) {
define( 'P4GBKS_PLUGIN_DIRNAME', dirname( P4GBKS_PLUGIN_BASENAME ) );
}
if ( ! defined( 'P4GBKS_PLUGIN_DIR' ) ) {
define( 'P4GBKS_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . P4GBKS_PLUGIN_DIRNAME );
}
if ( ! defined( 'P4GBKS_PLUGIN_URL' ) ) {
define( 'P4GBKS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
if ( ! defined( 'P4GBKS_PLUGIN_NAME' ) ) {
define( 'P4GBKS_PLUGIN_NAME', 'Planet4 - Gutenberg Blocks' );
}
if ( ! defined( 'P4GBKS_PLUGIN_SHORT_NAME' ) ) {
define( 'P4GBKS_PLUGIN_SHORT_NAME', 'Blocks' );
}
if ( ! defined( 'P4GBKS_PLUGIN_SLUG_NAME' ) ) {
define( 'P4GBKS_PLUGIN_SLUG_NAME', 'plugin_blocks_report' );
}
if ( ! defined( 'P4GBKS_INCLUDES_DIR' ) ) {
define( 'P4GBKS_INCLUDES_DIR', P4GBKS_PLUGIN_DIR . '/templates/' );
}
if ( ! defined( 'P4GBKS_TEMPLATE_OVERRIDE_SUBDIR' ) ) {
define( 'P4GBKS_TEMPLATE_OVERRIDE_SUBDIR', '/templates/plugins/planet4-plugin-gutenberg-blocks/includes/' );
}
if ( ! defined( 'P4GBKS_ADMIN_DIR' ) ) {
define( 'P4GBKS_ADMIN_DIR', plugins_url( P4GBKS_PLUGIN_DIRNAME . '/admin/' ) );
}
if ( ! defined( 'P4GBKS_LANGUAGES' ) ) {
define(
'P4GBKS_LANGUAGES',
[
'en_US' => 'English',
'el_GR' => 'Ελληνικά',
]
);
}
if ( ! defined( 'P4GBKS_ALLOWED_PAGETYPE' ) ) {
define( 'P4GBKS_ALLOWED_PAGETYPE', [ 'page', 'campaign' ] );
}
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
define( 'WP_UNINSTALL_PLUGIN', P4GBKS_PLUGIN_BASENAME );
}
require_once __DIR__ . '/classes/class-loader.php';
require_once ABSPATH . 'wp-admin/includes/plugin.php';
/**
* @param array $block the block being rendered.
* For the "link_new_tab" field the type was initially incorrectly set to
* string instead of boolean. As a result we need to catch all empty strings here and
* turn them into false.
*
* Note that this DOES NOT get called when a block is rendered using the REST API column rendered *sigh*.
* Even better, there is no hook that allows to modify the request before the parameters are validated.
* As this is only for the block renderer of columns, a good option would be to replace the invalid argument
* on the frontend before the API is called.
* This should be sufficient as we can fix the data after this fix hsa been applied, after which we can remove the workaround.
*
* @return array
*/
function empty_string_to_false_in_link_new_tab_in_columns_blocks( $block ): array {
// Yes, that's right, WordPress doesn't follow its own rules here so we have a camel among snakes.
if ( 'planet4-blocks/columns' === $block['blockName'] ?? null ) {
foreach ( $block['attrs']['columns'] ?? [] as $key => $column ) {
if ( isset( $column['link_new_tab'] ) && true !== $column['link_new_tab'] ) {
$block['attrs']['columns'][ $key ]['link_new_tab'] = false;
}
}
}
return $block;
}
add_filter( 'render_block_data', 'empty_string_to_false_in_link_new_tab_in_columns_blocks' );
// Add a filter to prevent the main query from being run on the DB or ES. This is needed because we actually executed a
// separate query for our results and never use the result of the main query. Returning an empty array in
// posts_pre_query for the main query on a search page short circuits the main query. However we also need to remove the
// filter that is set up by ElasticPress. Then we call it manually in our filter when we're not performing the main
// query of search.
// Even though all instances have ElasticPress plugin, this is run during unit tests where the ElasticPress plugin is
// not (yet?) loaded, causing a fatal error.
if ( class_exists( Elasticpress\Indexables::class ) ) {
remove_filter(
'posts_pre_query',
[
\ElasticPress\Indexables::factory()->get( 'post' )->query_integration,
'get_es_posts',
10,
]
);
add_filter(
'posts_pre_query',
function ( $posts, $query ) {
if ( is_search() && ! is_admin() && $query->is_main_query() ) {
return [];
}
return \ElasticPress\Indexables::factory()->get( 'post' )->query_integration->get_es_posts(
$posts,
$query
);
},
10,
2
);
}
/*
==========================
L O A D P L U G I N
==========================
*/
P4GBKS\Loader::get_instance();
\P4GBKS\Rest\Rest_Api::add_endpoints();
$remove_rtl_fix = function (): void {
global $sitepress;
// This RTL fix does not seem a good idea. Probably it was a bad attempt at solving the issues `url_to_postid`
// creates.
remove_action( 'wp_head', [ $sitepress, 'rtl_fix' ] );
remove_action( 'admin_print_styles', [ $sitepress, 'rtl_fix' ] );
// This caused `switch_lang` to get called. As a result the RTL fix messed up.
remove_filter( 'url_to_postid', [ $sitepress, 'url_to_postid' ] );
};
$remove_rtl_fix();
add_action( 'wpml_after_startup', $remove_rtl_fix, 10, 0 );
$breakpoints = [
[
'screen' => 1600,
'width' => '1320px',
],
[
'screen' => 1200,
'width' => '1140px',
],
[
'screen' => 992,
'width' => '960px',
],
[
'screen' => 768,
'width' => '720px',
],
[
'screen' => 601,
'width' => '540px',
],
[
'screen' => 577,
'width' => '540px',
],
];
add_filter(
'render_block_core/query-pagination',
function ( $content ) {
// Check if prev and next buttons are disabled.
if (
str_contains( $content, 'wp-block-query-pagination-previous disabled' )
&& str_contains( $content, 'wp-block-query-pagination-next disabled' )
) {
return null;
}
return $content;
},
10,
3
);
add_filter(
'render_block_core/query-pagination-previous',
function ( $content, $parsed, $block ) {
$button_label = __( 'Prev', 'planet4-blocks' );
if ( ! array_key_exists( 'label', $block->attributes ) ) {
$block->attributes['label'] = $button_label;
return $block->render();
}
// Check if the button isn't rendered, then return it.
if ( empty( $content ) ) {
return '<a href="/" class="wp-block-query-pagination-previous disabled">' . $button_label . '</a>';
}
return $content;
},
10,
3
);
add_filter(
'render_block_core/query-pagination-next',
function ( $content, $parsed, $block ) {
$button_label = __( 'Next', 'planet4-blocks' );
if ( ! array_key_exists( 'label', $block->attributes ) ) {
$block->attributes['label'] = $button_label;
return $block->render();
}
// Check if the button isn't rendered, then return it.
if ( empty( $content ) ) {
return '<a href="/" class="wp-block-query-pagination-next disabled">' . $button_label . '</a>';
}
return $content;
},
10,
3
);
add_filter(
'render_block',
function ( $block_content, $block, WP_Block $instance ) use ( $breakpoints ) {
if ( 'core/query' === $block['blockName'] ) {
$column_count = $instance->attributes['displayLayout']['columns'] ?? null;
if ( ! $column_count || 1 === $column_count ) {
return $block_content;
}
$sizes = array_map(
function ( $breakpoint ) use ( $column_count ) {
$screen = $breakpoint['screen'];
$container = $breakpoint['width'];
$cols_minus_one = $column_count - 1;
return "(min-width: ${screen}px) calc($container / $column_count - 1.25em * $cols_minus_one)";
},
$breakpoints
);
$sizes_attr = 'sizes="' . implode( ', ', array_merge( $sizes, [ 'calc(100vw - 24px)' ] ) ) . '"';
// Assume all images are full width in a container.
$block_content = preg_replace( '/sizes=".*"/', $sizes_attr, $block_content );
}
if ( 'core/media-text' === $block['blockName'] && array_key_exists( 'mediaId', $instance->attributes ) ) {
$media_id = $instance->attributes['mediaId'];
$media_width = $instance->attributes['mediaWidth'] ?? 50;
$srcset = wp_get_attachment_image_srcset( $media_id, 'full' );
if ( 'full' === $instance->attributes['align'] ) {
$sizes = ! $instance->attributes['isStackedOnMobile'] ? "${media_width}vw"
: "(min-width: 601px) {$media_width}vw, 100vw";
$sizes_attr = "sizes=\"{$sizes}\"";
} else {
$default = ! $instance->attributes['isStackedOnMobile'] ? "calc((100vw - 24px) * $media_width / 100)"
: 'calc(100vw - 24px)';
$sizes = implode(
',',
array_map(
function ( $breakpoint ) use ( $instance, $media_width ) {
$screen = $breakpoint['screen'];
$container = $breakpoint['width'];
$should_stack = $screen < 600 && $instance->attributes['isStackedOnMobile'];
$fraction = $should_stack ? 1 : round( 100 / $media_width, 4 );
// Currently, we need to subtract 24px for Bootstrap container.
return "(min-width: ${screen}px) calc(($container - 24px) / $fraction)";
},
$breakpoints
)
);
$sizes_attr = "sizes=\"{$sizes}, {$default}\"";
}
$image_class_start = "class=\"wp-image-$media_id ";
$block_content = str_replace(
$image_class_start,
"$sizes_attr srcset=\"$srcset\" $image_class_start",
$block_content
);
}
return $block_content;
},
10,
3
);