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