| 1 | <?php |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | add_filter( 'default_option_gutenberg-experiments', 'wpcomsh_filter_gutenberg_experiments' ); |
| 16 | add_filter( 'option_gutenberg-experiments', 'wpcomsh_filter_gutenberg_experiments' ); |
| 17 | |
| 18 | |
| 19 | add_action( 'muplugins_loaded', 'wpcomsh_register_ignore_outdated_gutenberg_plugin' ); |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | const WPCOMSH_CORE_BUNDLED_GUTENBERG_VERSIONS = array( |
| 27 | '7.1' => '23.6.0', |
| 28 | ); |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | function wpcomsh_register_ignore_outdated_gutenberg_plugin() { |
| 39 | if ( ! wpcomsh_is_wp_beta_version() ) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | add_filter( 'option_active_plugins', 'wpcomsh_ignore_outdated_gutenberg_plugin' ); |
| 44 | add_action( |
| 45 | 'plugins_loaded', |
| 46 | static function () { |
| 47 | remove_filter( 'option_active_plugins', 'wpcomsh_ignore_outdated_gutenberg_plugin' ); |
| 48 | }, |
| 49 | 0 |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | function wpcomsh_is_wp_beta_version() { |
| 59 | global $wp_version; |
| 60 | |
| 61 | return is_string( $wp_version ) && (bool) preg_match( '/-(alpha|beta|RC)/i', $wp_version ); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | function wpcomsh_get_core_bundled_gutenberg_version() { |
| 70 | global $wp_version; |
| 71 | |
| 72 | if ( ! is_string( $wp_version ) || ! preg_match( '/^(\d+\.\d+)/', $wp_version, $matches ) ) { |
| 73 | return null; |
| 74 | } |
| 75 | |
| 76 | return WPCOMSH_CORE_BUNDLED_GUTENBERG_VERSIONS[ $matches[1] ] ?? null; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | function wpcomsh_get_installed_gutenberg_plugin_version() { |
| 87 | $plugin_file = WP_PLUGIN_DIR . '/gutenberg/gutenberg.php'; |
| 88 | |
| 89 | if ( ! is_readable( $plugin_file ) ) { |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | $data = get_file_data( $plugin_file, array( 'Version' => 'Version' ) ); |
| 94 | |
| 95 | return empty( $data['Version'] ) ? null : $data['Version']; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | function wpcomsh_should_ignore_gutenberg_plugin( $plugin_version, $min_bundled_version ) { |
| 107 | if ( ! wpcomsh_is_wp_beta_version() || null === $plugin_version || null === $min_bundled_version ) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | return version_compare( $plugin_version, $min_bundled_version, '<' ); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | function wpcomsh_is_gutenberg_plugin_ignored() { |
| 120 | return wpcomsh_should_ignore_gutenberg_plugin( |
| 121 | wpcomsh_get_installed_gutenberg_plugin_version(), |
| 122 | wpcomsh_get_core_bundled_gutenberg_version() |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | function wpcomsh_ignore_outdated_gutenberg_plugin( $plugins ) { |
| 135 | if ( ! is_array( $plugins ) ) { |
| 136 | return $plugins; |
| 137 | } |
| 138 | |
| 139 | $key = array_search( 'gutenberg/gutenberg.php', $plugins, true ); |
| 140 | if ( false === $key || ! wpcomsh_is_gutenberg_plugin_ignored() ) { |
| 141 | return $plugins; |
| 142 | } |
| 143 | |
| 144 | unset( $plugins[ $key ] ); |
| 145 | |
| 146 | return array_values( $plugins ); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | function wpcomsh_remove_gutenberg_experiments() { |
| 155 | $jetpack_options = get_option( 'jetpack_options' ); |
| 156 | if ( is_array( $jetpack_options ) && isset( $jetpack_options['id'] ) ) { |
| 157 | $blog_id = (int) $jetpack_options['id']; |
| 158 | } else { |
| 159 | $blog_id = get_current_blog_id(); |
| 160 | } |
| 161 | |
| 162 | $allowed_blogs = array( |
| 163 | 211453162, |
| 164 | ); |
| 165 | |
| 166 | if ( in_array( $blog_id, $allowed_blogs, true ) ) { |
| 167 | |
| 168 | remove_filter( 'default_option_gutenberg-experiments', 'wpcomsh_filter_gutenberg_experiments' ); |
| 169 | remove_filter( 'option_gutenberg-experiments', 'wpcomsh_filter_gutenberg_experiments' ); |
| 170 | return; |
| 171 | } |
| 172 | add_action( 'admin_menu', 'wpcomsh_remove_gutenberg_experimental_menu' ); |
| 173 | } |
| 174 | add_action( 'init', 'wpcomsh_remove_gutenberg_experiments' ); |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | function wpcomsh_filter_gutenberg_experiments() { |
| 184 | return array( |
| 185 | 'gutenberg-guidelines' => true, |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | function wpcomsh_remove_gutenberg_experimental_menu() { |
| 193 | remove_submenu_page( 'options-general.php', 'experiments-wp-admin' ); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | function wpcomsh_enable_error_reporting_for_react_19( $is_enabled ) { |
| 207 | if ( $is_enabled ) { |
| 208 | return true; |
| 209 | } |
| 210 | |
| 211 | if ( ! function_exists( 'is_user_connected' ) || ! is_user_connected( get_current_user_id() ) ) { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | if ( function_exists( 'wpcomsh_is_site_sticker_active' ) && wpcomsh_is_site_sticker_active( 'gutenberg-react-19' ) ) { |
| 216 | return true; |
| 217 | } |
| 218 | |
| 219 | $site_id = wpcomsh_get_atomic_site_id(); |
| 220 | if ( ! $site_id ) { |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | $current_segment = 70; |
| 225 | $site_segment = $site_id % 100; |
| 226 | |
| 227 | |
| 228 | return $site_segment < $current_segment; |
| 229 | } |
| 230 | add_filter( 'a8c_enable_error_reporting', 'wpcomsh_enable_error_reporting_for_react_19' ); |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | function wpcomsh_remove_site_editor_reset_styles() { |
| 240 | $current_screen = get_current_screen(); |
| 241 | |
| 242 | if ( ! $current_screen || $current_screen->base !== 'toplevel_page_gutenberg-edit-site' ) { |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | if ( isset( wp_styles()->registered['wp-edit-blocks'] ) ) { |
| 251 | $wp_edit_blocks_dependencies = array_diff( wp_styles()->registered['wp-edit-blocks']->deps, array( 'wp-reset-editor-styles' ) ); |
| 252 | wp_styles()->registered['wp-edit-blocks']->deps = $wp_edit_blocks_dependencies; |
| 253 | } |
| 254 | } |
| 255 | add_action( 'admin_enqueue_scripts', 'wpcomsh_remove_site_editor_reset_styles' ); |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | function gutenberg_enqueue_block_styles_assets() { |
| 268 | $block_styles = WP_Block_Styles_Registry::get_instance()->get_all_registered(); |
| 269 | |
| 270 | foreach ( $block_styles as $block_name => $styles ) { |
| 271 | foreach ( $styles as $style_properties ) { |
| 272 | if ( isset( $style_properties['style_handle'] ) ) { |
| 273 | |
| 274 | |
| 275 | if ( wp_should_load_separate_core_block_assets() ) { |
| 276 | add_filter( |
| 277 | 'render_block', |
| 278 | function ( $html ) use ( $style_properties ) { |
| 279 | wp_enqueue_style( $style_properties['style_handle'] ); |
| 280 | return $html; |
| 281 | } |
| 282 | ); |
| 283 | } else { |
| 284 | wp_enqueue_style( $style_properties['style_handle'] ); |
| 285 | } |
| 286 | } |
| 287 | if ( isset( $style_properties['inline_style'] ) ) { |
| 288 | |
| 289 | |
| 290 | $handle = 'wp-block-library'; |
| 291 | |
| 292 | |
| 293 | if ( wp_should_load_separate_core_block_assets() ) { |
| 294 | $block_stylesheet_handle = generate_block_asset_handle( $block_name, 'style' ); |
| 295 | global $wp_styles; |
| 296 | if ( isset( $wp_styles->registered[ $block_stylesheet_handle ] ) ) { |
| 297 | $handle = $block_stylesheet_handle; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | |
| 302 | wp_add_inline_style( $handle, $style_properties['inline_style'] ); |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | add_filter( 'gutenberg_use_widgets_block_editor', '__return_false', 100 ); |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | function wpcom_safecss_filter_attr_allow_css_rgba( $allow_css, $css_test_string ) { |
| 328 | if ( false === $allow_css ) { |
| 329 | |
| 330 | $css_test_string = preg_replace( |
| 331 | '/\b(?:rgb[a]?)\((?:[\.]?\d+[\.\s\,\%\/]*\d*){0,4}\)/', |
| 332 | '', |
| 333 | $css_test_string |
| 334 | ); |
| 335 | |
| 336 | $allow_css = ! preg_match( '%[\\\(&=}]|/\*%', $css_test_string ); |
| 337 | } |
| 338 | |
| 339 | return $allow_css; |
| 340 | } |
| 341 | add_filter( 'safecss_filter_attr_allow_css', 'wpcom_safecss_filter_attr_allow_css_rgba', 10, 2 ); |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | function wpcom_safecss_allow_additional_css_properties( $css_properties ) { |
| 354 | $css_properties[] = 'display'; |
| 355 | $css_properties[] = 'transform'; |
| 356 | $css_properties[] = 'position'; |
| 357 | |
| 358 | return $css_properties; |
| 359 | } |
| 360 | add_filter( 'safe_style_css', 'wpcom_safecss_allow_additional_css_properties', 10, 1 ); |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | function wpcomsh_add_guideline_sync_meta_whitelist( $whitelist ) { |
| 369 | $guideline_meta_keys = array( |
| 370 | '_guideline_copy', |
| 371 | '_guideline_images', |
| 372 | '_guideline_site', |
| 373 | '_guideline_additional', |
| 374 | '_guideline_block_core_paragraph', |
| 375 | '_guideline_block_core_image', |
| 376 | '_guideline_block_core_heading', |
| 377 | '_guideline_block_core_list', |
| 378 | '_guideline_block_core_list_item', |
| 379 | '_guideline_block_core_quote', |
| 380 | '_guideline_block_core_code', |
| 381 | '_guideline_block_core_table', |
| 382 | '_guideline_block_core_video', |
| 383 | '_guideline_block_core_audio', |
| 384 | '_guideline_block_core_gallery', |
| 385 | '_guideline_block_core_cover', |
| 386 | '_guideline_block_core_pullquote', |
| 387 | '_guideline_block_core_preformatted', |
| 388 | '_guideline_block_core_verse', |
| 389 | '_guideline_block_core_button', |
| 390 | '_guideline_block_core_media_text', |
| 391 | '_guideline_block_core_freeform', |
| 392 | '_guideline_block_core_html', |
| 393 | '_guideline_block_core_embed', |
| 394 | ); |
| 395 | return array_merge( $whitelist, $guideline_meta_keys ); |
| 396 | } |
| 397 | add_filter( 'jetpack_sync_post_meta_whitelist', 'wpcomsh_add_guideline_sync_meta_whitelist' ); |