PluginProbe
Friends / 2.8.9
Friends v2.8.9
4.3.2 4.3.1 4.3.0 4.2.2 4.2.1 4.2.0 4.1.0 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.0 2.9.1 All 88 releases
friends / libs / blocks-everywhere / classes / class-editor.php

class-editor.php in Friends 2.8.9, at libs/blocks-everywhere/classes/class-editor.php

355 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Friends\Blocks_Everywhere;
4
5 /**
6 * Provides functions to load Gutenberg assets
7 */
8 class Editor {
9 /**
10 * Can upload?
11 *
12 * @var boolean
13 */
14 private $can_upload = false;
15
16 /**
17 * Constructor
18 */
19 public function __construct() {
20 if ( ! class_exists( 'WP_Theme_JSON_Data_Gutenberg' ) ) {
21 return null;
22 }
23 add_action( 'template_redirect', [ $this, 'setup_media' ] );
24 add_filter( 'block_editor_settings_all', [ $this, 'block_editor_settings_all' ] );
25 add_filter( 'should_load_block_editor_scripts_and_styles', '__return_true' );
26 add_filter( 'wp_theme_json_data_theme', [ $this, 'wp_theme_json_data_theme' ] );
27 }
28
29 /**
30 * Provide theme.json
31 *
32 * @param \WP_Theme_JSON_Data_Gutenberg $json JSON.
33 * @return \WP_Theme_JSON_Data_Gutenberg
34 */
35 public function wp_theme_json_data_theme( $json ) {
36 $theme = new \WP_Theme_JSON_Data_Gutenberg(
37 [
38 'version' => 2,
39 'settings' => [
40 'color' => [
41 'background' => false,
42 'custom' => false,
43 'customDuotone' => false,
44 'customGradient' => false,
45 'defaultGradients' => false,
46 'defaultPalette' => false,
47 'text' => false,
48 ],
49 'typography' => [
50 'customFontSize' => false,
51 'dropCap' => false,
52 'fontStyle' => false,
53 'fontWeight' => false,
54 'letterSpacing' => false,
55 'lineHeight' => false,
56 'textDecoration' => false,
57 'textTransform' => false,
58 'fontSizes' => [],
59 'fontFamilies' => [],
60 ],
61 ],
62 ]
63 );
64
65 return $theme;
66 }
67
68 /**
69 * Restrict TinyMCE to the basics
70 *
71 * @param array $settings TinyMCE settings.
72 * @return array
73 */
74 public function tiny_mce_before_init( $settings ) {
75 $settings['toolbar1'] = 'bold,italic,bullist,numlist,blockquote,pastetext,removeformat,undo,redo';
76 $settings['toolbar2'] = '';
77
78 return $settings;
79 }
80
81 /**
82 * Load Gutenberg
83 *
84 * Based on wp-admin/edit-form-blocks.php
85 *
86 * @param array $settings Plugin settings.
87 * @return void
88 */
89 public function load( $settings ) {
90 global $post;
91
92 $this->can_upload = isset( $settings['editor']['hasUploadPermissions'] ) && $settings['editor']['hasUploadPermissions'];
93 $this->load_extra_blocks();
94
95 // Restrict tinymce buttons
96 add_filter( 'tiny_mce_before_init', [ $this, 'tiny_mce_before_init' ] );
97
98 // Gutenberg scripts
99 wp_enqueue_script( 'wp-block-library' );
100 wp_enqueue_script( 'wp-format-library' );
101 wp_enqueue_script( 'wp-editor' );
102 wp_enqueue_script( 'wp-plugins' );
103
104 // Gutenberg styles
105 wp_enqueue_style( 'wp-edit-post' );
106 wp_enqueue_style( 'wp-format-library' );
107
108 // Keep Jetpack out of things
109 add_filter(
110 'jetpack_blocks_variation',
111 function() {
112 return 'no-post-editor';
113 }
114 );
115
116 wp_tinymce_inline_scripts();
117 wp_enqueue_editor();
118
119 do_action( 'enqueue_block_editor_assets' );
120
121 add_action( 'wp_print_footer_scripts', array( '_WP_Editors', 'print_default_editor_scripts' ), 45 );
122
123 $this->setup_rest_api();
124
125 set_current_screen( 'front' );
126 wp_styles()->done = array( 'wp-reset-editor-styles' );
127
128 $categories = wp_json_encode( get_block_categories( $post ) );
129
130 if ( $categories !== false ) {
131 wp_add_inline_script(
132 'wp-blocks',
133 sprintf( 'wp.blocks.setCategories( %s );', $categories ),
134 'after'
135 );
136 }
137
138 /**
139 * @psalm-suppress PossiblyFalseOperand
140 */
141 wp_add_inline_script(
142 'wp-blocks',
143 'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
144 );
145
146 $this->setup_media();
147 }
148
149 /**
150 * Load any third-party blocks
151 *
152 * @return void
153 */
154 private function load_extra_blocks() {
155 // phpcs:ignore
156 $GLOBALS['hook_suffix'] = '';
157
158 /**
159 * @psalm-suppress MissingFile
160 */
161 require_once ABSPATH . 'wp-admin/includes/class-wp-screen.php';
162 /**
163 * @psalm-suppress MissingFile
164 */
165 require_once ABSPATH . 'wp-admin/includes/screen.php';
166 /**
167 * @psalm-suppress MissingFile
168 */
169 require_once ABSPATH . 'wp-admin/includes/post.php';
170
171 // Fake a WP_Screen object so we can pretend we're in the block editor, and therefore other block libraries load
172 set_current_screen();
173
174 $current_screen = get_current_screen();
175 if ( $current_screen ) {
176 $current_screen->is_block_editor( true );
177 }
178 }
179
180 /**
181 * Override some features that probably don't make sense in an isolated editor
182 *
183 * @param array $settings Settings array.
184 * @return array
185 */
186 public function block_editor_settings_all( array $settings ) {
187 $settings['availableLegacyWidgets'] = (object) [];
188 $settings['hasPermissionsToManageWidgets'] = false;
189
190 return $settings;
191 }
192
193 /**
194 * Set up Gutenberg editor settings
195 *
196 * @return Array
197 */
198 public function get_editor_settings() {
199 global $post;
200
201 // phpcs:ignore
202 $body_placeholder = apply_filters( 'write_your_story', null, $post );
203
204 $editor_settings = array(
205 'availableTemplates' => [],
206 'disablePostFormats' => ! current_theme_supports( 'post-formats' ),
207 /** This filter is documented in wp-admin/edit-form-advanced.php */
208 // phpcs:ignore
209 'titlePlaceholder' => apply_filters( 'enter_title_here', __( 'Add title', 'blocks-everywhere' ), $post ),
210 'bodyPlaceholder' => $body_placeholder,
211 'autosaveInterval' => AUTOSAVE_INTERVAL,
212 'styles' => get_block_editor_theme_styles(),
213 'richEditingEnabled' => user_can_richedit(),
214 'postLock' => false,
215 'supportsLayout' => \WP_Theme_JSON_Resolver::theme_has_support(),
216 '__experimentalBlockPatterns' => [],
217 '__experimentalBlockPatternCategories' => [],
218 'supportsTemplateMode' => current_theme_supports( 'block-templates' ),
219 'enableCustomFields' => false,
220 'generateAnchors' => true,
221 'canLockBlocks' => false,
222 );
223
224 $editor_settings['__unstableResolvedAssets'] = $this->wp_get_iframed_editor_assets();
225
226 $block_editor_context = new \WP_Block_Editor_Context( array( 'post' => $post ) );
227 return get_block_editor_settings( $editor_settings, $block_editor_context );
228 }
229
230 /**
231 * Set up the Gutenberg REST API and preloaded data
232 *
233 * @return void
234 */
235 public function setup_rest_api() {
236 global $post;
237
238 $post_type = 'post';
239
240 // Preload common data.
241 $preload_paths = array(
242 '/',
243 '/wp/v2/types?context=edit',
244 '/wp/v2/taxonomies?per_page=-1&context=edit',
245 '/wp/v2/themes?status=active',
246 sprintf( '/wp/v2/types/%s?context=edit', $post_type ),
247 sprintf( '/wp/v2/users/me?post_type=%s&context=edit', $post_type ),
248 array( '/wp/v2/media', 'OPTIONS' ),
249 array( '/wp/v2/blocks', 'OPTIONS' ),
250 );
251
252 /**
253 * @psalm-suppress TooManyArguments
254 */
255 $preload_paths = apply_filters( 'block_editor_preload_paths', $preload_paths, $post );
256 $preload_data = array_reduce( $preload_paths, 'rest_preload_api_request', array() );
257
258 $encoded = wp_json_encode( $preload_data );
259 if ( $encoded !== false ) {
260 wp_add_inline_script(
261 'wp-editor',
262 sprintf( 'wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( %s ) );', $encoded ),
263 'after'
264 );
265 }
266 }
267
268 /**
269 * Ensure media works in Gutenberg
270 *
271 * @return void
272 */
273 public function setup_media() {
274 if ( ! $this->can_upload ) {
275 return;
276 }
277
278 // If we've already loaded the media stuff then don't do it again
279 if ( did_action( 'wp_enqueue_media' ) > 0 ) {
280 return;
281 }
282
283 /**
284 * @psalm-suppress MissingFile
285 */
286 require_once ABSPATH . 'wp-admin/includes/media.php';
287
288 wp_enqueue_media();
289 }
290
291 public function wp_get_iframed_editor_assets() {
292 $script_handles = array();
293 $style_handles = array(
294 'wp-block-editor',
295 'wp-block-library',
296 'wp-edit-blocks',
297 );
298
299 if ( current_theme_supports( 'wp-block-styles' ) ) {
300 $style_handles[] = 'wp-block-library-theme';
301 }
302
303 $block_registry = \WP_Block_Type_Registry::get_instance();
304
305 foreach ( $block_registry->get_all_registered() as $block_type ) {
306 if ( ! empty( $block_type->style ) ) {
307 $style_handles = array_merge( $style_handles, (array) $block_type->style );
308 }
309
310 if ( ! empty( $block_type->editor_style ) ) {
311 $style_handles = array_merge( $style_handles, (array) $block_type->editor_style );
312 }
313
314 if ( ! empty( $block_type->script ) ) {
315 $script_handles = array_merge( $script_handles, (array) $block_type->script );
316 }
317
318 if ( ! empty( $block_type->view_script ) ) {
319 $script_handles = array_merge( $script_handles, (array) $block_type->view_script );
320 }
321 }
322
323 $style_handles = apply_filters( 'blocks_everywhere_editor_styles', $style_handles );
324 $style_handles = array_unique( $style_handles );
325 $done = wp_styles()->done;
326
327 ob_start();
328
329 // We do not need reset styles for the iframed editor.
330 wp_styles()->done = array( 'wp-reset-editor-styles' );
331 wp_styles()->do_items( $style_handles );
332 wp_styles()->done = $done;
333
334 $styles = ob_get_clean();
335
336 $script_handles = array_unique( apply_filters( 'blocks_everywhere_editor_scripts', $script_handles ) );
337 $done = wp_scripts()->done;
338
339 ob_start();
340
341 wp_scripts()->done = array();
342 wp_scripts()->do_items( $script_handles );
343 wp_scripts()->done = $done;
344
345 $scripts = ob_get_clean();
346
347 return wp_json_encode(
348 [
349 'styles' => $styles,
350 'scripts' => $scripts,
351 ]
352 );
353 }
354 }
355