Skip to content

Commit

Permalink
Patch parent theme
Browse files Browse the repository at this point in the history
  • Loading branch information
carstingaxion committed Aug 15, 2024
1 parent b52361e commit 8e4b7cd
Show file tree
Hide file tree
Showing 3 changed files with 278 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- [New developer.gatherpress.org · Issue #1 · carstingaxion/gatherpress-devhub](https://github.com/carstingaxion/gatherpress-devhub/issues/1)
- [WordPress devhub blueprint · Issue #44 · WordPress/blueprints](https://github.com/WordPress/blueprints/issues/44)

[<kbd> <br> Edit <code>blueprint.json</code> <br> </kbd>](https://playground.wordpress.net/builder/builder.html?blueprint-url=https://raw.githubusercontent.com/carstingaxion/gatherpress-devhub/main/blueprint.json)
[<kbd> <br> Edit <code>blueprint.json</code> <br> </kbd>](https://playground.wordpress.net/builder/builder.html?blueprint-url=https://raw.githubusercontent.com/carstingaxion/gatherpress-devhub/WIP/use-build-deps/blueprint.json)

---

Expand Down
27 changes: 26 additions & 1 deletion blueprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@
"activate": false
}
},
{
"step": "installPlugin",
"pluginZipFile": {
"resource": "wordpress.org/plugins",
"slug": "code-syntax-block"
},
"options": {
"activate": true
}
},
{
"step": "writeFile",
"path": "/wordpress/wporg-mu-plugins.zip",
Expand Down Expand Up @@ -68,7 +78,18 @@
"step": "installTheme",
"themeZipFile": {
"resource": "url",
"url": "https://github-proxy.com/proxy/?repo=wporg-parent-2021&branch=build"
"url": "https://github-proxy.com/proxy/?repo=WordPress/wporg-parent-2021&branch=build"
},
"options": {
"activate": false
}
},
{
"step": "writeFile",
"path": "/wordpress/wp-content/themes/wporg-parent-2021-build/functions.php",
"data": {
"resource": "url",
"url": "https://raw.githubusercontent.com/carstingaxion/gatherpress-devhub/WIP/use-build-deps/wp-content/themes/wporg-parent-2021-build/functions.php"
}
},
{
Expand Down Expand Up @@ -106,6 +127,10 @@
"step": "wp-cli",
"command": "wp plugin activate phpdoc-parser"
},
{
"step": "wp-cli",
"command": "wp rewrite flush"
},
{
"step": "wp-cli",
"command": "wp parser create '/wordpress/wp-content/plugins/gatherpress' --user=1"
Expand Down
251 changes: 251 additions & 0 deletions wp-content/themes/wporg-parent-2021-build/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
<?php

namespace WordPressdotorg\Theme\Parent_2021;

use function WordPressdotorg\MU_Plugins\Global_Fonts\get_font_stylesheet_url;

defined( 'WPINC' ) || die();

require_once __DIR__ . '/inc/gutenberg-tweaks.php';
require_once __DIR__ . '/inc/block-styles.php';
require_once __DIR__ . '/inc/rosetta-styles.php';

/**
* Actions and filters.
*/
add_action( 'after_setup_theme', __NAMESPACE__ . '\theme_support', 9 );
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_assets' );
add_filter( 'author_link', __NAMESPACE__ . '\use_wporg_profile_for_author_link', 10, 3 );
add_filter( 'render_block_core/pattern', __NAMESPACE__ . '\add_aria_hidden_to_arrows', 19 );
add_filter( 'the_content', __NAMESPACE__ . '\add_aria_hidden_to_arrows', 19 );
add_filter( 'wp_theme_json_data_theme', __NAMESPACE__ . '\merge_parent_child_theme_json' );

// Enable embeds in patterns.
// See https://github.com/WordPress/gutenberg/issues/46556.
global $wp_embed;
add_filter( 'render_block_core/pattern', array( $wp_embed, 'autoembed' ) );

// Render shortcodes in patterns.
add_filter( 'render_block_core/pattern', 'do_shortcode' );

/**
* Register theme support.
*/
function theme_support() {
// Alignwide and alignfull classes in the block editor.
add_theme_support( 'align-wide' );

// Add support for responsive embedded content.
// https://github.com/WordPress/gutenberg/issues/26901
add_theme_support( 'responsive-embeds' );

// Add support for editor styles.
$suffix = is_rtl() ? '-rtl' : '';
add_theme_support( 'editor-style' );
// add_editor_style( get_font_stylesheet_url() );
add_editor_style( "/build/editor{$suffix}.css" );

// Add support for post thumbnails.
add_theme_support( 'post-thumbnails' );

// Declare that there are no <title> tags and allow WordPress to provide them
add_theme_support( 'title-tag' );

// Experimental support for adding blocks inside nav menus
add_theme_support( 'block-nav-menus' );

// Remove the default margin-top added when the admin bar is used, this is
// handled by the theme, in `_site-header.scss`.
add_theme_support( 'admin-bar', array( 'callback' => '__return_false' ) );

register_block_pattern_category(
'wporg',
array(
'label' => __( 'WordPress.org', 'wporg' ),
)
);
}

/**
* Enqueue scripts and styles.
*/
function enqueue_assets() {
wp_enqueue_style(
'wporg-parent-2021-style',
get_template_directory_uri() . '/build/style.css',
array( 'wporg-global-fonts', 'wporg-parent-block-styles' ),
filemtime( __DIR__ . '/build/style.css' )
);
wp_style_add_data( 'wporg-parent-2021-style', 'rtl', 'replace' );
}

/**
* Swap out the normal author archive link for the author's wp.org profile link.
*
* @param string $link Overwritten.
* @param int $author_id Unused.
* @param string $author_nicename Used as the slug in the profiles URL.
*
* @return string
*/
function use_wporg_profile_for_author_link( $link, $author_id, $author_nicename ) {
return sprintf(
'https://profiles.wordpress.org/%s/',
$author_nicename
);
}

/**
* Wrap the arrow emoji in an aria-hidden span tag, to prevent screen readers
* from trying to read them.
*
* This runs just before `prevent_arrow_emoji`, so that the variation-selector
* can be added inside the span.
*
* @param string $content Content of the current post.
* @return string The updated content.
*/
function add_aria_hidden_to_arrows( $content ) {
return preg_replace( '/([←↑→↓↔↕↖↗↘↙])/u', '<span aria-hidden="true" class="wp-exclude-emoji">\1</span>', $content );
}

/**
* Merge the child theme's theme.json into the parent theme.json.
*
* Pull the parent theme's values for array settings out and merge them by slug
* with the values in the child theme. This prevents values in the child theme
* from blowing away the parent theme's settings.
*
* Additional settings are merged correctly, since they're objects (and merged
* by key).
*
* See https://github.com/WordPress/gutenberg/issues/40557.
*
* @param WP_Theme_JSON_Data $theme_json Parsed child theme.json.
*
* @return WP_Theme_JSON_Data The updated theme.json settings.
*/
function merge_parent_child_theme_json( $theme_json ) {
// Nothing to merge if this is not a child theme.
if ( get_template_directory() === get_stylesheet_directory() ) {
return $theme_json;
}

// Build a new theme.json object.
$new_data = array(
'version' => 2,
);

$child_theme = $theme_json->get_data();

if ( ! empty( $child_theme['settings'] ) ) {
$parent_theme_json_file = get_template_directory() . '/theme.json';
$parent_theme_json_data = wp_json_file_decode( $parent_theme_json_file, array( 'associative' => true ) );
if ( class_exists( 'WP_Theme_JSON_Gutenberg' ) ) {
$parent_theme = new \WP_Theme_JSON_Gutenberg( $parent_theme_json_data );
} else {
$parent_theme = new \WP_Theme_JSON( $parent_theme_json_data );
}

// Get base theme.json settings.
$parent_settings = $parent_theme->get_settings();
$child_settings = $child_theme['settings'];

// Define the array values here, so they can be updated if the theme.json schema changes.
$color_keys = [ 'duotone', 'gradient', 'palette' ];
$typog_keys = [ 'fontFamilies', 'fontSizes' ];
$space_keys = [ 'spacingSizes' ];

foreach ( $color_keys as $key ) {
if ( ! empty( $child_settings['color'][ $key ]['theme'] ) ) {
$child_settings['color'][ $key ]['theme'] = _merge_by_slug(
$parent_settings['color'][ $key ]['theme'],
$child_settings['color'][ $key ]['theme']
);

}
}

foreach ( $typog_keys as $key ) {
if ( ! empty( $child_settings['typography'][ $key ]['theme'] ) ) {
$child_settings['typography'][ $key ]['theme'] = _merge_by_slug(
$parent_settings['typography'][ $key ]['theme'],
$child_settings['typography'][ $key ]['theme']
);
}
}

foreach ( $space_keys as $key ) {
if ( ! empty( $child_settings['spacing'][ $key ]['theme'] ) ) {
$child_settings['spacing'][ $key ]['theme'] = _merge_by_slug(
$parent_settings['spacing'][ $key ]['theme'],
$child_settings['spacing'][ $key ]['theme']
);
}
}

$new_data['settings'] = $child_settings;
}

return $theme_json->update_with( $new_data );
}

/**
* Merge two (or more) arrays, de-duplicating by the `slug` key.
*
* If any values in later arrays have slugs matching earlier items, the earlier
* items are overwritten with the later value.
*
* @param array ...$arrays A list of arrays of associative arrays, each item
* must have a `slug` key.
*
* @return array The combined array, unique by `slug`. Empty if any item is
* missing a slug.
*/
function _merge_by_slug( ...$arrays ) {
$combined = array_merge( ...$arrays );
$result = [];

foreach ( $combined as $value ) {
if ( ! isset( $value['slug'] ) ) {
return [];
}

$found = array_search( $value['slug'], wp_list_pluck( $result, 'slug' ), true );
if ( false !== $found ) {
$result[ $found ] = $value;
} else {
$result[] = $value;
}
}
return $result;
}

/**
* Increase the "Big image" threshold so our full-size high-DPI-ready images are
* not scaled down.
*/
add_filter(
'big_image_size_threshold',
function() {
// 3200 = 2 × 1600px for full-width images on wide screens.
return 3200;
}
);

/**
* Make posts and pages available for export from the staging site, so the import script can
* fetch them to a local dev environment.
*/
add_filter(
'wporg_export_context_post_types',
function( $types ) {
return array_merge(
$types,
[
'post',
'page',
]
);
}
);

0 comments on commit 8e4b7cd

Please sign in to comment.