Skip to content

Commit 5cc28b5

Browse files
authored
Merge pull request #653 from newfold-labs/add/PRESS8-257-Fix-Onboarding-Live-Preview
Fixing Live Previews
2 parents 3d0383c + 3cf2cc8 commit 5cc28b5

File tree

5 files changed

+1652
-323
lines changed

5 files changed

+1652
-323
lines changed

includes/WP_Admin.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,30 @@ public static function register_assets() {
147147
'wp.blocks.unstable__bootstrapServerSideBlockDefinitions(' . wp_json_encode( get_block_editor_server_block_settings() ) . ');'
148148
);
149149

150+
// Preload server-registered block bindings sources.
151+
$registered_sources = get_all_registered_block_bindings_sources();
152+
if ( ! empty( $registered_sources ) ) {
153+
$filtered_sources = array();
154+
foreach ( $registered_sources as $source ) {
155+
$filtered_sources[] = array(
156+
'name' => $source->name,
157+
'label' => $source->label,
158+
'usesContext' => $source->uses_context,
159+
);
160+
}
161+
$script = sprintf( 'for ( const source of %s ) { wp.blocks.registerBlockBindingsSource( source ); }', wp_json_encode( $filtered_sources ) );
162+
wp_add_inline_script(
163+
'wp-blocks',
164+
$script
165+
);
166+
}
167+
168+
wp_add_inline_script(
169+
'wp-blocks',
170+
sprintf( 'wp.blocks.setCategories( %s );', wp_json_encode( get_block_categories( $block_editor_context ) ) ),
171+
'after'
172+
);
173+
150174
\wp_enqueue_script( self::$slug );
151175
\wp_enqueue_style( self::$slug );
152176

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"@wordpress/style-engine": "^0.11.0",
4141
"bytes": "^3.1.2",
4242
"classnames": "^2.3.2",
43+
"colord": "^2.9.3",
4344
"react-router-dom": "^6.20.1"
4445
}
4546
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* Internal dependencies
3+
*/
4+
import { scopeSelector, getValueFromObjectPath } from './utils';
5+
6+
/**
7+
* Determine the CSS selector for the block type and target provided, returning
8+
* it if available.
9+
*
10+
* @param {Object} blockType The block's type.
11+
* @param {string|string[]} target The desired selector's target e.g. `root`, delimited string, or array path.
12+
* @param {Object} options Options object.
13+
* @param {boolean} options.fallback Whether or not to fallback to broader selector.
14+
*
15+
* @return {?string} The CSS selector or `null` if no selector available.
16+
*/
17+
export function getBlockCSSSelector(
18+
blockType,
19+
target = 'root',
20+
options = {}
21+
) {
22+
if ( ! target ) {
23+
return null;
24+
}
25+
26+
const { fallback = false } = options;
27+
const { name, selectors, supports } = blockType;
28+
29+
const hasSelectors = selectors && Object.keys( selectors ).length > 0;
30+
const path = Array.isArray( target ) ? target.join( '.' ) : target;
31+
32+
// Root selector.
33+
34+
// Calculated before returning as it can be used as a fallback for feature
35+
// selectors later on.
36+
let rootSelector = null;
37+
38+
if ( hasSelectors && selectors.root ) {
39+
// Use the selectors API if available.
40+
rootSelector = selectors?.root;
41+
} else if ( supports?.__experimentalSelector ) {
42+
// Use the old experimental selector supports property if set.
43+
rootSelector = supports.__experimentalSelector;
44+
} else {
45+
// If no root selector found, generate default block class selector.
46+
rootSelector =
47+
'.wp-block-' + name.replace( 'core/', '' ).replace( '/', '-' );
48+
}
49+
50+
// Return selector if it's the root target we are looking for.
51+
if ( path === 'root' ) {
52+
return rootSelector;
53+
}
54+
55+
// If target is not `root` or `duotone` we have a feature or subfeature
56+
// as the target. If the target is a string convert to an array.
57+
const pathArray = Array.isArray( target ) ? target : target.split( '.' );
58+
59+
// Feature selectors ( may fallback to root selector );
60+
if ( pathArray.length === 1 ) {
61+
const fallbackSelector = fallback ? rootSelector : null;
62+
63+
// Prefer the selectors API if available.
64+
if ( hasSelectors ) {
65+
// Get selector from either `feature.root` or shorthand path.
66+
const featureSelector =
67+
getValueFromObjectPath( selectors, `${ path }.root`, null ) ||
68+
getValueFromObjectPath( selectors, path, null );
69+
70+
// Return feature selector if found or any available fallback.
71+
return featureSelector || fallbackSelector;
72+
}
73+
74+
// Try getting old experimental supports selector value.
75+
const featureSelector = getValueFromObjectPath(
76+
supports,
77+
`${ path }.__experimentalSelector`,
78+
null
79+
);
80+
81+
// If nothing to work with, provide fallback selector if available.
82+
if ( ! featureSelector ) {
83+
return fallbackSelector;
84+
}
85+
86+
// Scope the feature selector by the block's root selector.
87+
return scopeSelector( rootSelector, featureSelector );
88+
}
89+
90+
// Subfeature selector.
91+
// This may fallback either to parent feature or root selector.
92+
let subfeatureSelector;
93+
94+
// Use selectors API if available.
95+
if ( hasSelectors ) {
96+
subfeatureSelector = getValueFromObjectPath( selectors, path, null );
97+
}
98+
99+
// Only return if we have a subfeature selector.
100+
if ( subfeatureSelector ) {
101+
return subfeatureSelector;
102+
}
103+
104+
// To this point we don't have a subfeature selector. If a fallback has been
105+
// requested, remove subfeature from target path and return results of a
106+
// call for the parent feature's selector.
107+
if ( fallback ) {
108+
return getBlockCSSSelector( blockType, pathArray[ 0 ], options );
109+
}
110+
111+
// We tried.
112+
return null;
113+
}

0 commit comments

Comments
 (0)