| 1 |
<?php |
| 2 |
/** |
| 3 |
* Updates the script-loader.php file |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Updates the registered inline script `wp-date` required for moment.js localization. |
| 10 |
* Changes to the inline script output should be synced with Core in the file |
| 11 |
* src/wp-includes/script-loader.php in `wp_default_packages_inline_scripts()`. |
| 12 |
* |
| 13 |
* @since 6.4.0 |
| 14 |
* |
| 15 |
* @global WP_Locale $wp_locale WordPress date and time locale object. |
| 16 |
* |
| 17 |
* @param WP_Scripts $scripts WP_Scripts object. |
| 18 |
*/ |
| 19 |
function gutenberg_update_wp_date_settings( $scripts ) { |
| 20 |
if ( $scripts->query( 'wp-date', 'registered' ) ) { |
| 21 |
global $wp_locale; |
| 22 |
// Calculate the timezone abbr (EDT, PST) if possible. |
| 23 |
$timezone_string = get_option( 'timezone_string', 'UTC' ); |
| 24 |
$timezone_abbr = ''; |
| 25 |
|
| 26 |
if ( ! empty( $timezone_string ) ) { |
| 27 |
$timezone_date = new DateTime( 'now', new DateTimeZone( $timezone_string ) ); |
| 28 |
$timezone_abbr = $timezone_date->format( 'T' ); |
| 29 |
} |
| 30 |
$scripts->registered['wp-date']->extra['after'] = array( |
| 31 |
false, |
| 32 |
sprintf( |
| 33 |
'wp.date.setSettings( %s );', |
| 34 |
wp_json_encode( |
| 35 |
array( |
| 36 |
'l10n' => array( |
| 37 |
'locale' => get_user_locale(), |
| 38 |
'months' => array_values( $wp_locale->month ), |
| 39 |
'monthsShort' => array_values( $wp_locale->month_abbrev ), |
| 40 |
'weekdays' => array_values( $wp_locale->weekday ), |
| 41 |
'weekdaysShort' => array_values( $wp_locale->weekday_abbrev ), |
| 42 |
'meridiem' => (object) $wp_locale->meridiem, |
| 43 |
'relative' => array( |
| 44 |
/* translators: %s: Duration. */ |
| 45 |
'future' => __( '%s from now', 'gutenberg' ), |
| 46 |
/* translators: %s: Duration. */ |
| 47 |
'past' => __( '%s ago', 'gutenberg' ), |
| 48 |
/* translators: One second from or to a particular datetime, e.g., "a second ago" or "a second from now". */ |
| 49 |
's' => __( 'a second', 'gutenberg' ), |
| 50 |
/* translators: %s: Duration in seconds from or to a particular datetime, e.g., "4 seconds ago" or "4 seconds from now". */ |
| 51 |
'ss' => __( '%d seconds', 'gutenberg' ), |
| 52 |
/* translators: One minute from or to a particular datetime, e.g., "a minute ago" or "a minute from now". */ |
| 53 |
'm' => __( 'a minute', 'gutenberg' ), |
| 54 |
/* translators: %s: Duration in minutes from or to a particular datetime, e.g., "4 minutes ago" or "4 minutes from now". */ |
| 55 |
'mm' => __( '%d minutes', 'gutenberg' ), |
| 56 |
/* translators: %s: One hour from or to a particular datetime, e.g., "an hour ago" or "an hour from now". */ |
| 57 |
'h' => __( 'an hour', 'gutenberg' ), |
| 58 |
/* translators: %s: Duration in hours from or to a particular datetime, e.g., "4 hours ago" or "4 hours from now". */ |
| 59 |
'hh' => __( '%d hours', 'gutenberg' ), |
| 60 |
/* translators: %s: One day from or to a particular datetime, e.g., "a day ago" or "a day from now". */ |
| 61 |
'd' => __( 'a day', 'gutenberg' ), |
| 62 |
/* translators: %s: Duration in days from or to a particular datetime, e.g., "4 days ago" or "4 days from now". */ |
| 63 |
'dd' => __( '%d days', 'gutenberg' ), |
| 64 |
/* translators: %s: One month from or to a particular datetime, e.g., "a month ago" or "a month from now". */ |
| 65 |
'M' => __( 'a month', 'gutenberg' ), |
| 66 |
/* translators: %s: Duration in months from or to a particular datetime, e.g., "4 months ago" or "4 months from now". */ |
| 67 |
'MM' => __( '%d months', 'gutenberg' ), |
| 68 |
/* translators: %s: One year from or to a particular datetime, e.g., "a year ago" or "a year from now". */ |
| 69 |
'y' => __( 'a year', 'gutenberg' ), |
| 70 |
/* translators: %s: Duration in years from or to a particular datetime, e.g., "4 years ago" or "4 years from now". */ |
| 71 |
'yy' => __( '%d years', 'gutenberg' ), |
| 72 |
), |
| 73 |
'startOfWeek' => (int) get_option( 'start_of_week', 0 ), |
| 74 |
), |
| 75 |
'formats' => array( |
| 76 |
/* translators: Time format, see https://www.php.net/manual/datetime.format.php */ |
| 77 |
'time' => get_option( 'time_format', __( 'g:i a', 'default' ) ), |
| 78 |
/* translators: Date format, see https://www.php.net/manual/datetime.format.php */ |
| 79 |
'date' => get_option( 'date_format', __( 'F j, Y', 'default' ) ), |
| 80 |
/* translators: Date/Time format, see https://www.php.net/manual/datetime.format.php */ |
| 81 |
'datetime' => __( 'F j, Y g:i a', 'default' ), |
| 82 |
/* translators: Abbreviated date/time format, see https://www.php.net/manual/datetime.format.php */ |
| 83 |
'datetimeAbbreviated' => __( 'M j, Y g:i a', 'default' ), |
| 84 |
), |
| 85 |
'timezone' => array( |
| 86 |
'offset' => (float) get_option( 'gmt_offset', 0 ), |
| 87 |
'string' => $timezone_string, |
| 88 |
'abbr' => $timezone_abbr, |
| 89 |
), |
| 90 |
) |
| 91 |
) |
| 92 |
), |
| 93 |
); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
add_action( 'wp_default_scripts', 'gutenberg_update_wp_date_settings' ); |
| 98 |
|
| 99 |
/** |
| 100 |
* Collect the block editor assets that need to be loaded into the editor's iframe. |
| 101 |
* |
| 102 |
* @since 6.0.0 |
| 103 |
* @access private |
| 104 |
* |
| 105 |
* @return array { |
| 106 |
* The block editor assets. |
| 107 |
* |
| 108 |
* @type string|false $styles String containing the HTML for styles. |
| 109 |
* @type string|false $scripts String containing the HTML for scripts. |
| 110 |
* } |
| 111 |
*/ |
| 112 |
function _gutenberg_get_iframed_editor_assets_6_4() { |
| 113 |
global $wp_styles, $wp_scripts; |
| 114 |
|
| 115 |
// Keep track of the styles and scripts instance to restore later. |
| 116 |
$current_wp_styles = $wp_styles; |
| 117 |
$current_wp_scripts = $wp_scripts; |
| 118 |
|
| 119 |
// Create new instances to collect the assets. |
| 120 |
$wp_styles = new WP_Styles(); |
| 121 |
$wp_scripts = new WP_Scripts(); |
| 122 |
|
| 123 |
// Register all currently registered styles and scripts. The actions that |
| 124 |
// follow enqueue assets, but don't necessarily register them. |
| 125 |
$wp_styles->registered = $current_wp_styles->registered; |
| 126 |
$wp_scripts->registered = $current_wp_scripts->registered; |
| 127 |
|
| 128 |
// We generally do not need reset styles for the iframed editor. |
| 129 |
// However, if it's a classic theme, margins will be added to every block, |
| 130 |
// which is reset specifically for list items, so classic themes rely on |
| 131 |
// these reset styles. |
| 132 |
$wp_styles->done = |
| 133 |
wp_theme_has_theme_json() ? array( 'wp-reset-editor-styles' ) : array(); |
| 134 |
|
| 135 |
wp_enqueue_script( 'wp-polyfill' ); |
| 136 |
// Enqueue the `editorStyle` handles for all core block, and dependencies. |
| 137 |
wp_enqueue_style( 'wp-edit-blocks' ); |
| 138 |
|
| 139 |
if ( current_theme_supports( 'wp-block-styles' ) ) { |
| 140 |
wp_enqueue_style( 'wp-block-library-theme' ); |
| 141 |
} |
| 142 |
|
| 143 |
// We don't want to load EDITOR scripts in the iframe, only enqueue |
| 144 |
// front-end assets for the content. |
| 145 |
add_filter( 'should_load_block_editor_scripts_and_styles', '__return_false' ); |
| 146 |
do_action( 'enqueue_block_assets' ); |
| 147 |
remove_filter( 'should_load_block_editor_scripts_and_styles', '__return_false' ); |
| 148 |
|
| 149 |
$block_registry = WP_Block_Type_Registry::get_instance(); |
| 150 |
|
| 151 |
// Additionally, do enqueue `editorStyle` assets for all blocks, which |
| 152 |
// contains editor-only styling for blocks (editor content). |
| 153 |
foreach ( $block_registry->get_all_registered() as $block_type ) { |
| 154 |
if ( isset( $block_type->editor_style_handles ) && is_array( $block_type->editor_style_handles ) ) { |
| 155 |
foreach ( $block_type->editor_style_handles as $style_handle ) { |
| 156 |
wp_enqueue_style( $style_handle ); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Remove the deprecated `print_emoji_styles` handler. |
| 163 |
* It avoids breaking style generation with a deprecation message. |
| 164 |
*/ |
| 165 |
$has_emoji_styles = has_action( 'wp_print_styles', 'print_emoji_styles' ); |
| 166 |
if ( $has_emoji_styles ) { |
| 167 |
remove_action( 'wp_print_styles', 'print_emoji_styles' ); |
| 168 |
} |
| 169 |
|
| 170 |
ob_start(); |
| 171 |
wp_print_styles(); |
| 172 |
$styles = ob_get_clean(); |
| 173 |
|
| 174 |
if ( $has_emoji_styles ) { |
| 175 |
add_action( 'wp_print_styles', 'print_emoji_styles' ); |
| 176 |
} |
| 177 |
|
| 178 |
ob_start(); |
| 179 |
wp_print_head_scripts(); |
| 180 |
wp_print_footer_scripts(); |
| 181 |
$scripts = ob_get_clean(); |
| 182 |
|
| 183 |
// Restore the original instances. |
| 184 |
$wp_styles = $current_wp_styles; |
| 185 |
$wp_scripts = $current_wp_scripts; |
| 186 |
|
| 187 |
return array( |
| 188 |
'styles' => $styles, |
| 189 |
'scripts' => $scripts, |
| 190 |
); |
| 191 |
} |
| 192 |
|
| 193 |
add_filter( |
| 194 |
'block_editor_settings_all', |
| 195 |
static function ( $settings ) { |
| 196 |
// We must override what core is passing now. |
| 197 |
$settings['__unstableResolvedAssets'] = _gutenberg_get_iframed_editor_assets_6_4(); |
| 198 |
return $settings; |
| 199 |
} |
| 200 |
); |
| 201 |
|