Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[61] Re-introduce block editor visual preview #111

Merged
merged 31 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
17c6f26
feat: re-introduce block editor preview
g-elwell Aug 23, 2024
8323951
fix: ensure invalid or undefined colours fall back to set value as an…
g-elwell Aug 23, 2024
c22e85f
feat: adds gradient picker component as part of color component
chrishall0 Aug 29, 2024
62c87ad
feat: adds support for disabling custom gradients
chrishall0 Aug 29, 2024
9c96d13
docs: updated code comment order
chrishall0 Aug 29, 2024
d531bdf
feat: heading translation
chrishall0 Aug 30, 2024
bb4b7bf
docs: multiline comment
chrishall0 Aug 30, 2024
e933c14
feat: useMemo for gradients, changes to check before render
chrishall0 Aug 30, 2024
cec036e
feat: adding contrast checker for text and bg
thatisrich Sep 20, 2024
e711792
fix: avoiding custom palette display issue
thatisrich Sep 20, 2024
7c957dc
feat: restricting event listeners to container
thatisrich Sep 24, 2024
c0a13fc
feat: switching event listener to debounce
thatisrich Sep 24, 2024
6516130
feat: reducing complexity of approach
thatisrich Sep 24, 2024
631b7f6
feat: moving debounce to custom hook
thatisrich Sep 25, 2024
043d2de
fix: removing unnecessary comment line
thatisrich Sep 25, 2024
f586922
fix: improve code view styling
g-elwell Sep 30, 2024
a7295b4
feat: update preview styling, re-introduce size selector, add mode to…
g-elwell Sep 30, 2024
f82eb93
fix: remove unused code, i18n strings
g-elwell Oct 1, 2024
cd34541
fix: ensure block editor preview is 100% height of container
g-elwell Oct 1, 2024
de63fe6
chore: remove code comment
g-elwell Oct 1, 2024
63ddbb9
refactor: improve code organisation
g-elwell Oct 1, 2024
4cef7f9
refactor: extract topbar into dedicated component
g-elwell Oct 1, 2024
25af41c
fix: update block editor preloading
g-elwell Oct 1, 2024
acafa73
chore: correct comment wording
g-elwell Oct 1, 2024
af491f1
Merge branch 'main' into feature/block-preview
g-elwell Oct 1, 2024
379fb21
fix: phpcs errors
g-elwell Oct 1, 2024
d171c7d
fix: phpcs errors
g-elwell Oct 1, 2024
4e79efe
chore: remove unused variable
g-elwell Oct 1, 2024
b21d3df
docs: add missing JSDoc info
g-elwell Oct 1, 2024
6f6dbc5
feat: update dependencies and min WP version
g-elwell Oct 1, 2024
74be1bb
fix: remove interface package and migrate components as for 6.4> compat
g-elwell Oct 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Themer is a WordPress plugin that provides a UI for users to edit the [theme.jso

### Prerequisites

- **WordPress:** 6.2
- **WordPress:** 6.4
- **PHP:** 8.0

### Via Composer
Expand Down
37 changes: 32 additions & 5 deletions inc/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,34 @@ public function __construct() {
add_action( 'admin_menu', array( $this, 'create_admin_screen' ), 1 );
}

/**
* Enqueue any required assets for the plugin.
*
* @return void
*/
public function enqueue_assets() : void {

$plugin_name = basename( THEMER_DIR );
$asset_file = include THEMER_DIR . '/build/index.asset.php';

wp_enqueue_style( 'wp-components' );

wp_enqueue_script(
'themer',
plugins_url( $plugin_name . '/build/index.js', $plugin_name ),
$asset_file['dependencies'],
$asset_file['version'],
false
);

wp_enqueue_style(
'themer',
plugins_url( $plugin_name . '/build/index.css', $plugin_name ),
array(),
$asset_file['version']
);
}

/**
* Create an admin screen for theme settings.
*
Expand All @@ -29,16 +57,15 @@ public function create_admin_screen() : void {
'Styles Editor',
'edit_theme_options',
'styles_editor',
array( $this, 'theme_render_settings' ),
array( $this, 'render_settings_page' ),
);
}

/**
* Render the content for the theme settings page.
*/
public function theme_render_settings() {
?>
<div id="themer-admin-screen" class="theme-settings-page-wrapper wrap"></div>
<?php
public function render_settings_page() {
$this->enqueue_assets();
require __DIR__ . '/edit-form-themer.php';
}
}
62 changes: 0 additions & 62 deletions inc/class-loader.php

This file was deleted.

127 changes: 127 additions & 0 deletions inc/edit-form-themer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/**
* A page template for displaying the themer admin page.
*
* Handles the enqueing of block editor assets and initialization of the editor within themer view.
*
* Adapted from wp-admin/edit-form-blocks.php
*
* @package themer
*/

declare( strict_types = 1 );

if ( ! defined( 'ABSPATH' ) ) {
die( '-1' );
}

global $editor_styles;

/** WordPress Administration Bootstrap */
require_once ABSPATH . 'wp-admin/admin.php';

if ( ! current_user_can( 'edit_theme_options' ) ) {
wp_die(
esc_html( '<h1>' . __( 'You need a higher level of permission.', 'default' ) . '</h1>' ) .
esc_html( '<p>' . __( 'Sorry, you are not allowed to edit styles on this site.', 'themer' ) . '</p>' ),
403
);
}

if ( ! wp_is_block_theme() ) {
wp_die( esc_html__( 'The theme you are currently using is not compatible with the Site Editor.', 'default' ) );
}

$title = _x( 'Styles Editor', 'themer' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Required to set the page title for this view.

$current_screen = get_current_screen(); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Required flag we're loading the block editor.
$current_screen->is_block_editor( true );

$block_editor_context = new WP_Block_Editor_Context( array( 'name' => 'core/edit-site' ) );

/*
* Emoji replacement is disabled for now, until it plays nicely with React.
*/
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );

/*
* Block editor implements its own Options menu for toggling Document Panels.
*/
add_filter( 'screen_options_show_screen', '__return_false' );

// Preload server-registered block schemas.
wp_add_inline_script(
'wp-blocks',
'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
);

$editor_settings = array(
'hasFixedToolbar' => true, // effectively hides the toolbar since we don't render the fixed toolbar in the themer view.
'templateLock' => 'all', // prevents moving/removing blocks.
'richEditingEnabled' => false,
'codeEditingEnabled' => false,
'focusMode' => false,
'enableCustomFields' => false,
'styles' => get_block_editor_theme_styles(),
);

$active_global_styles_id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
$active_theme = get_stylesheet();

$preload_paths = array(
array( rest_get_route_for_post_type_items( 'attachment' ), 'OPTIONS' ),
array( rest_get_route_for_post_type_items( 'page' ), 'OPTIONS' ),
'/wp/v2/types?context=view',
'/wp/v2/types/wp_template?context=edit',
'/wp/v2/types/wp_template_part?context=edit',
'/wp/v2/templates?context=edit&per_page=-1',
'/wp/v2/template-parts?context=edit&per_page=-1',
'/wp/v2/themes?context=edit&status=active',
'/wp/v2/global-styles/' . $active_global_styles_id . '?context=edit',
'/wp/v2/global-styles/' . $active_global_styles_id,
'/wp/v2/global-styles/themes/' . $active_theme,
'themer/v1/styles',
);

block_editor_rest_api_preload( $preload_paths, $block_editor_context );

$editor_settings = get_block_editor_settings( $editor_settings, $block_editor_context );

// Add some custom styles.
$editor_settings['styles'][] = array(
'css' => '
body { padding: 20px; }
',
);

$init_script = <<<JS
( function() {
window._wpLoadBlockEditor = new Promise( function( resolve ) {
wp.domReady( function() {
resolve( themer.initializeEditor( 'themer-root', %s ) );
} );
} );
} )();
JS;

$script = sprintf(
$init_script,
wp_json_encode( $editor_settings ),
);

wp_add_inline_script( 'themer', $script );

wp_enqueue_style( 'wp-edit-post' );

do_action( 'enqueue_block_editor_assets' );

require_once ABSPATH . 'wp-admin/admin-header.php';
?>

<div class="block-editor">
<h1 class="screen-reader-text hide-if-no-js"><?php echo esc_html( $title ); ?></h1>
<div id="themer-root"></div>
</div>

<?php
require_once ABSPATH . 'wp-admin/admin-footer.php';
1 change: 0 additions & 1 deletion inc/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
* @return void
*/
function setup() : void {
new Loader();
new Admin();
new Rest_API();
}
Loading