|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WordPressdotorg\Theme\Parent_2021; |
| 4 | + |
| 5 | +use function WordPressdotorg\MU_Plugins\Global_Fonts\get_font_stylesheet_url; |
| 6 | + |
| 7 | +defined( 'WPINC' ) || die(); |
| 8 | + |
| 9 | +require_once __DIR__ . '/inc/gutenberg-tweaks.php'; |
| 10 | +require_once __DIR__ . '/inc/block-styles.php'; |
| 11 | +require_once __DIR__ . '/inc/rosetta-styles.php'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Actions and filters. |
| 15 | + */ |
| 16 | +add_action( 'after_setup_theme', __NAMESPACE__ . '\theme_support', 9 ); |
| 17 | +add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' ); |
| 18 | +add_filter( 'author_link', __NAMESPACE__ . '\use_wporg_profile_for_author_link', 10, 3 ); |
| 19 | +add_filter( 'render_block_core/pattern', __NAMESPACE__ . '\add_aria_hidden_to_arrows', 19 ); |
| 20 | +add_filter( 'the_content', __NAMESPACE__ . '\add_aria_hidden_to_arrows', 19 ); |
| 21 | +add_filter( 'wp_theme_json_data_theme', __NAMESPACE__ . '\merge_parent_child_theme_json' ); |
| 22 | + |
| 23 | +// Enable embeds in patterns. |
| 24 | +// See https://github.com/WordPress/gutenberg/issues/46556. |
| 25 | +global $wp_embed; |
| 26 | +add_filter( 'render_block_core/pattern', array( $wp_embed, 'autoembed' ) ); |
| 27 | + |
| 28 | +// Render shortcodes in patterns. |
| 29 | +add_filter( 'render_block_core/pattern', 'do_shortcode' ); |
| 30 | + |
| 31 | +/** |
| 32 | + * Register theme support. |
| 33 | + */ |
| 34 | +function theme_support() { |
| 35 | + // Alignwide and alignfull classes in the block editor. |
| 36 | + add_theme_support( 'align-wide' ); |
| 37 | + |
| 38 | + // Add support for responsive embedded content. |
| 39 | + // https://github.com/WordPress/gutenberg/issues/26901 |
| 40 | + add_theme_support( 'responsive-embeds' ); |
| 41 | + |
| 42 | + // Add support for editor styles. |
| 43 | + $suffix = is_rtl() ? '-rtl' : ''; |
| 44 | + add_theme_support( 'editor-style' ); |
| 45 | + // add_editor_style( get_font_stylesheet_url() ); |
| 46 | + add_editor_style( "/build/editor{$suffix}.css" ); |
| 47 | + |
| 48 | + // Add support for post thumbnails. |
| 49 | + add_theme_support( 'post-thumbnails' ); |
| 50 | + |
| 51 | + // Declare that there are no <title> tags and allow WordPress to provide them |
| 52 | + add_theme_support( 'title-tag' ); |
| 53 | + |
| 54 | + // Experimental support for adding blocks inside nav menus |
| 55 | + add_theme_support( 'block-nav-menus' ); |
| 56 | + |
| 57 | + // Remove the default margin-top added when the admin bar is used, this is |
| 58 | + // handled by the theme, in `_site-header.scss`. |
| 59 | + add_theme_support( 'admin-bar', array( 'callback' => '__return_false' ) ); |
| 60 | + |
| 61 | + register_block_pattern_category( |
| 62 | + 'wporg', |
| 63 | + array( |
| 64 | + 'label' => __( 'WordPress.org', 'wporg' ), |
| 65 | + ) |
| 66 | + ); |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Enqueue scripts and styles. |
| 71 | + */ |
| 72 | +function enqueue_assets() { |
| 73 | + wp_enqueue_style( |
| 74 | + 'wporg-parent-2021-style', |
| 75 | + get_template_directory_uri() . '/build/style.css', |
| 76 | + array( 'wporg-global-fonts', 'wporg-parent-block-styles' ), |
| 77 | + filemtime( __DIR__ . '/build/style.css' ) |
| 78 | + ); |
| 79 | + wp_style_add_data( 'wporg-parent-2021-style', 'rtl', 'replace' ); |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Swap out the normal author archive link for the author's wp.org profile link. |
| 84 | + * |
| 85 | + * @param string $link Overwritten. |
| 86 | + * @param int $author_id Unused. |
| 87 | + * @param string $author_nicename Used as the slug in the profiles URL. |
| 88 | + * |
| 89 | + * @return string |
| 90 | + */ |
| 91 | +function use_wporg_profile_for_author_link( $link, $author_id, $author_nicename ) { |
| 92 | + return sprintf( |
| 93 | + 'https://profiles.wordpress.org/%s/', |
| 94 | + $author_nicename |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Wrap the arrow emoji in an aria-hidden span tag, to prevent screen readers |
| 100 | + * from trying to read them. |
| 101 | + * |
| 102 | + * This runs just before `prevent_arrow_emoji`, so that the variation-selector |
| 103 | + * can be added inside the span. |
| 104 | + * |
| 105 | + * @param string $content Content of the current post. |
| 106 | + * @return string The updated content. |
| 107 | + */ |
| 108 | +function add_aria_hidden_to_arrows( $content ) { |
| 109 | + return preg_replace( '/([←↑→↓↔↕↖↗↘↙])/u', '<span aria-hidden="true" class="wp-exclude-emoji">\1</span>', $content ); |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Merge the child theme's theme.json into the parent theme.json. |
| 114 | + * |
| 115 | + * Pull the parent theme's values for array settings out and merge them by slug |
| 116 | + * with the values in the child theme. This prevents values in the child theme |
| 117 | + * from blowing away the parent theme's settings. |
| 118 | + * |
| 119 | + * Additional settings are merged correctly, since they're objects (and merged |
| 120 | + * by key). |
| 121 | + * |
| 122 | + * See https://github.com/WordPress/gutenberg/issues/40557. |
| 123 | + * |
| 124 | + * @param WP_Theme_JSON_Data $theme_json Parsed child theme.json. |
| 125 | + * |
| 126 | + * @return WP_Theme_JSON_Data The updated theme.json settings. |
| 127 | + */ |
| 128 | +function merge_parent_child_theme_json( $theme_json ) { |
| 129 | + // Nothing to merge if this is not a child theme. |
| 130 | + if ( get_template_directory() === get_stylesheet_directory() ) { |
| 131 | + return $theme_json; |
| 132 | + } |
| 133 | + |
| 134 | + // Build a new theme.json object. |
| 135 | + $new_data = array( |
| 136 | + 'version' => 2, |
| 137 | + ); |
| 138 | + |
| 139 | + $child_theme = $theme_json->get_data(); |
| 140 | + |
| 141 | + if ( ! empty( $child_theme['settings'] ) ) { |
| 142 | + $parent_theme_json_file = get_template_directory() . '/theme.json'; |
| 143 | + $parent_theme_json_data = wp_json_file_decode( $parent_theme_json_file, array( 'associative' => true ) ); |
| 144 | + if ( class_exists( 'WP_Theme_JSON_Gutenberg' ) ) { |
| 145 | + $parent_theme = new \WP_Theme_JSON_Gutenberg( $parent_theme_json_data ); |
| 146 | + } else { |
| 147 | + $parent_theme = new \WP_Theme_JSON( $parent_theme_json_data ); |
| 148 | + } |
| 149 | + |
| 150 | + // Get base theme.json settings. |
| 151 | + $parent_settings = $parent_theme->get_settings(); |
| 152 | + $child_settings = $child_theme['settings']; |
| 153 | + |
| 154 | + // Define the array values here, so they can be updated if the theme.json schema changes. |
| 155 | + $color_keys = [ 'duotone', 'gradient', 'palette' ]; |
| 156 | + $typog_keys = [ 'fontFamilies', 'fontSizes' ]; |
| 157 | + $space_keys = [ 'spacingSizes' ]; |
| 158 | + |
| 159 | + foreach ( $color_keys as $key ) { |
| 160 | + if ( ! empty( $child_settings['color'][ $key ]['theme'] ) ) { |
| 161 | + $child_settings['color'][ $key ]['theme'] = _merge_by_slug( |
| 162 | + $parent_settings['color'][ $key ]['theme'], |
| 163 | + $child_settings['color'][ $key ]['theme'] |
| 164 | + ); |
| 165 | + |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + foreach ( $typog_keys as $key ) { |
| 170 | + if ( ! empty( $child_settings['typography'][ $key ]['theme'] ) ) { |
| 171 | + $child_settings['typography'][ $key ]['theme'] = _merge_by_slug( |
| 172 | + $parent_settings['typography'][ $key ]['theme'], |
| 173 | + $child_settings['typography'][ $key ]['theme'] |
| 174 | + ); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + foreach ( $space_keys as $key ) { |
| 179 | + if ( ! empty( $child_settings['spacing'][ $key ]['theme'] ) ) { |
| 180 | + $child_settings['spacing'][ $key ]['theme'] = _merge_by_slug( |
| 181 | + $parent_settings['spacing'][ $key ]['theme'], |
| 182 | + $child_settings['spacing'][ $key ]['theme'] |
| 183 | + ); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + $new_data['settings'] = $child_settings; |
| 188 | + } |
| 189 | + |
| 190 | + return $theme_json->update_with( $new_data ); |
| 191 | +} |
| 192 | + |
| 193 | +/** |
| 194 | + * Merge two (or more) arrays, de-duplicating by the `slug` key. |
| 195 | + * |
| 196 | + * If any values in later arrays have slugs matching earlier items, the earlier |
| 197 | + * items are overwritten with the later value. |
| 198 | + * |
| 199 | + * @param array ...$arrays A list of arrays of associative arrays, each item |
| 200 | + * must have a `slug` key. |
| 201 | + * |
| 202 | + * @return array The combined array, unique by `slug`. Empty if any item is |
| 203 | + * missing a slug. |
| 204 | + */ |
| 205 | +function _merge_by_slug( ...$arrays ) { |
| 206 | + $combined = array_merge( ...$arrays ); |
| 207 | + $result = []; |
| 208 | + |
| 209 | + foreach ( $combined as $value ) { |
| 210 | + if ( ! isset( $value['slug'] ) ) { |
| 211 | + return []; |
| 212 | + } |
| 213 | + |
| 214 | + $found = array_search( $value['slug'], wp_list_pluck( $result, 'slug' ), true ); |
| 215 | + if ( false !== $found ) { |
| 216 | + $result[ $found ] = $value; |
| 217 | + } else { |
| 218 | + $result[] = $value; |
| 219 | + } |
| 220 | + } |
| 221 | + return $result; |
| 222 | +} |
| 223 | + |
| 224 | +/** |
| 225 | + * Increase the "Big image" threshold so our full-size high-DPI-ready images are |
| 226 | + * not scaled down. |
| 227 | + */ |
| 228 | +add_filter( |
| 229 | + 'big_image_size_threshold', |
| 230 | + function() { |
| 231 | + // 3200 = 2 × 1600px for full-width images on wide screens. |
| 232 | + return 3200; |
| 233 | + } |
| 234 | +); |
| 235 | + |
| 236 | +/** |
| 237 | + * Make posts and pages available for export from the staging site, so the import script can |
| 238 | + * fetch them to a local dev environment. |
| 239 | + */ |
| 240 | +add_filter( |
| 241 | + 'wporg_export_context_post_types', |
| 242 | + function( $types ) { |
| 243 | + return array_merge( |
| 244 | + $types, |
| 245 | + [ |
| 246 | + 'post', |
| 247 | + 'page', |
| 248 | + ] |
| 249 | + ); |
| 250 | + } |
| 251 | +); |
0 commit comments