Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CMSP-695] Filter WP font_dir #29

Merged
merged 61 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 49 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
19ae183
use the downloaded tmp directory to source the wp-latest.json
jazzsequence Feb 5, 2024
ae82717
add fonts namespace and font dir mods
jazzsequence Feb 5, 2024
79abd55
load our font dir mods
jazzsequence Feb 5, 2024
a8476b7
initial hello world tests
jazzsequence Feb 5, 2024
6e51dd8
test the pantheon_font_dir function works as expected
jazzsequence Feb 6, 2024
cd59bef
allow a nightly version of WP to be installed
jazzsequence Feb 6, 2024
4cad5c1
handle installed nightly releases for testing
jazzsequence Feb 6, 2024
f3fa10c
create wp-config for nightly wp if one doesn't exist
jazzsequence Feb 6, 2024
ae53e2e
install latest gutenberg if we're running tests on latest wp
jazzsequence Feb 6, 2024
435aa15
fix spacing
jazzsequence Feb 6, 2024
56f9c6d
Add font library retrieval function from Gutenberg
jazzsequence Feb 6, 2024
567a918
change how we identify whether to skip the test
jazzsequence Feb 6, 2024
cd021bd
Add test for our font directory filtering
jazzsequence Feb 6, 2024
6df74d6
use a function to get the modify fonts dir value
jazzsequence Feb 6, 2024
80ea6c9
use the new function rather than a constant
jazzsequence Feb 6, 2024
32def22
don't add our own filter
jazzsequence Feb 6, 2024
df631d7
lower the priority of our font_dir filter
jazzsequence Feb 6, 2024
979cdfd
test that lowering the priority and filtering font_dir overrides our …
jazzsequence Feb 6, 2024
3cc9501
test disabling our font dir modification
jazzsequence Feb 6, 2024
ecf626b
linting fixes
jazzsequence Feb 6, 2024
26f03b9
fix download function for shellcheck
jazzsequence Feb 6, 2024
6ea1258
check if the file exists first
jazzsequence Feb 6, 2024
976dc8a
install wp-cli for tests
jazzsequence Feb 6, 2024
439372c
move multisite tests to run before nightly tests
jazzsequence Feb 6, 2024
d4ecf9b
setup nightly tests
jazzsequence Feb 6, 2024
dc3bc68
use latest checkout
jazzsequence Feb 6, 2024
9a5b45c
remove reference to non-existant filter
jazzsequence Feb 6, 2024
93c6518
return after marking test skipped
jazzsequence Feb 6, 2024
fba7864
maybe we need to check against latest 6.4.x
jazzsequence Feb 6, 2024
8b2bfa4
explicitly skip tests if wp_get_font_dir does not exist after trying …
jazzsequence Feb 6, 2024
7e3e403
don't remove non-existant file
jazzsequence Feb 6, 2024
8585cfb
better prompt for local install
jazzsequence Feb 6, 2024
2021d13
turn on debugging for phpunit test
jazzsequence Feb 6, 2024
37c9525
remove the rm's which apparently aren't doing anything
jazzsequence Feb 6, 2024
ce3e0fe
don't define a path
jazzsequence Feb 6, 2024
00f78b7
try /tmp/wordpress
jazzsequence Feb 6, 2024
0cc668a
remove debug mode
jazzsequence Feb 6, 2024
3ba1885
add labels and dividers between each run
jazzsequence Feb 6, 2024
3fa3de7
add composer-diff workflow
jazzsequence Feb 6, 2024
3a3a647
fix prerelease check logic
jazzsequence Feb 6, 2024
3d1feeb
capital W and P, i guess, dangit
jazzsequence Feb 6, 2024
2f6624d
test on all PHPs
jazzsequence Feb 6, 2024
36e5008
break wp_get_upload_dir out to a global
jazzsequence Feb 8, 2024
c7e3fbc
use our global for the wp_upload_dir
jazzsequence Feb 8, 2024
e1fb384
switch the wp_ prefix for pantheon_
jazzsequence Feb 8, 2024
1ee5a46
ignore unused variable
jazzsequence Feb 8, 2024
bb57dfc
Mock our pantheon_upload_dir global so we can test against it
jazzsequence Feb 8, 2024
83218c2
don't prefix property with an underscore
jazzsequence Feb 8, 2024
2d80e9e
linting fixes
jazzsequence Feb 8, 2024
1fa5a8b
simplify font dir
jazzsequence Feb 8, 2024
8a28754
remove function and just use the filter
jazzsequence Feb 8, 2024
363f6ba
remove wp version checks and rely on wp_get_font_dir check
jazzsequence Feb 8, 2024
3359e15
remove function call
jazzsequence Feb 8, 2024
03bdddf
use wpunit-helpers instead of our own files
jazzsequence Feb 8, 2024
1c497b9
remove shellcheck action
jazzsequence Feb 8, 2024
ae0723c
allow the wpunit-helpers plugin
jazzsequence Feb 8, 2024
3cfe3d1
gitignore the scripts sourced by wpunit-helpers
jazzsequence Feb 9, 2024
a39677b
after copying the files, we need to chmod them, so hook to post-insta…
jazzsequence Feb 9, 2024
9746d52
update syntax and add a nightly script
jazzsequence Feb 9, 2024
f3dbd51
bump actions/cache
jazzsequence Feb 9, 2024
d300920
use the new release
jazzsequence Feb 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/composer-diff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Composer Diff
on:
pull_request:
paths:
- 'composer.lock'
permissions:
contents: write
pull-requests: write
jobs:
composer-diff:
name: Composer Diff
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Generate composer diff
id: composer_diff
uses: IonBazan/composer-diff-action@v1
- uses: marocchino/sticky-pull-request-comment@v2
if: ${{ steps.composer_diff.outputs.composer_diff_exit_code != 0 }}
with:
header: composer-diff
message: |
<summary>Composer Changes</summary>

${{ steps.composer_diff.outputs.composer_diff }}
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Lint
run: |
composer install
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/shellcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Shellcheck
run: find bin/ -name "*.sh" | grep -v "install-wp-tests.sh" | xargs shellcheck
22 changes: 17 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: PHP Compatibility
uses: pantheon-systems/phpcompatibility-action@dev
with:
Expand All @@ -26,12 +26,15 @@ jobs:
services:
mariadb:
image: mariadb:10.6
strategy:
matrix:
php_version: [7.4, 8.1, 8.3] # Versions represent newest 7.x, WP supported with exclusions, and beta support.
steps:
- uses: actions/checkout@v3
- name: Setup PHP 8.3
- uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.3
php-version: ${{ matrix.php_version }}
extensions: mysqli, zip, imagick
- name: Start MySQL
run: sudo systemctl start mysql
Expand All @@ -42,7 +45,16 @@ jobs:
key: test-phpunit-dependencies-${{ hashFiles('composer.json') }}
restore-keys: test-phpunit-dependencies-${{ hashFiles('composer.json') }}
- name: Install Composer dependencies
run: composer install
run: |
if [ ${{ matrix.php_version }} = "7.4" ]; then
composer update
fi
composer install
- name: Install WP-CLI
run: |
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
- name: Run PHPUnit
run: bash ./bin/phpunit-test.sh

31 changes: 30 additions & 1 deletion bin/install-local-tests.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#!/bin/bash
set -e

# Request version.
echo "Which version of WordPress would you like to test against? (latest, nightly, or a version number) Leave blank for latest."
read -r WP_VERSION

# Initialize variables with default values
TMPDIR="/tmp"
DB_NAME="wordpress_test"
DB_USER="root"
DB_PASS=""
DB_HOST="127.0.0.1"
WP_VERSION="latest"
WP_VERSION=${WP_VERSION:-latest}
SKIP_DB=""

# Display usage information
Expand All @@ -16,6 +20,14 @@ usage() {
echo "./install-local-tests.sh [--dbname=wordpress_test] [--dbuser=root] [--dbpass=''] [--dbhost=127.0.0.1] [--wpversion=latest] [--no-db]"
}

download() {
if [ "$(which curl)" ]; then
curl -s "$1" > "$2";
elif [ "$(which wget)" ]; then
wget -nv -O "$2" "$1"
fi
jazzsequence marked this conversation as resolved.
Show resolved Hide resolved
}

# Parse command-line arguments
for i in "$@"
do
Expand Down Expand Up @@ -54,8 +66,25 @@ done

# Run install-wp-tests.sh
echo "Installing local tests into ${TMPDIR}"
echo "Using WordPress version: ${WP_VERSION}"
bash "$(dirname "$0")/install-wp-tests.sh" "$DB_NAME" "$DB_USER" "$DB_PASS" "$DB_HOST" "$WP_VERSION" "$SKIP_DB"

# If WP nightly is chosen, the script doesn't download the wp-latest.json file, so download it manually.
if [ "${WP_VERSION}" == "nightly" ]; then
download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json

# If a wp-config file does not exist, create it.
if [ ! -f "${TMPDIR}/wordpress/wp-config.php" ]; then
echo "Creating wp-config.php"
wp config create --dbname="${DB_NAME}" --dbuser="${DB_USER}" --dbpass="${DB_PASS}" --dbhost="${DB_HOST}" --dbprefix=wptests_ --path="${TMPDIR}/wordpress/"
fi

wp core install --url=localhost --title=Test --admin_user=admin --admin_password=password [email protected] --path="${TMPDIR}/wordpress/"
# If nightly version of WP is installed, install latest Gutenberg plugin and activate it.
echo "Installing Gutenberg plugin"
wp plugin install gutenberg --activate --path="${TMPDIR}/wordpress/"
fi

# Run PHPUnit
echo "Running PHPUnit"
composer phpunit
41 changes: 33 additions & 8 deletions bin/phpunit-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,51 @@ set -e

DIRNAME=$(dirname "$0")

download() {
if [ "$(which curl)" ]; then
curl -s "$1" > "$2";
elif [ "$(which wget)" ]; then
wget -nv -O "$2" "$1"
fi
}
jazzsequence marked this conversation as resolved.
Show resolved Hide resolved

echo "🤔 Installing WP Unit tests..."
bash "${DIRNAME}/install-wp-tests.sh" wordpress_test root root 127.0.0.1 latest

echo "📄 Copying wp-latest.json..."
cp /tmp/wp-latest.json "${DIRNAME}/../tests/wp-latest.json"

echo "🏃‍♂️ Running PHPUnit on Single Site"
echo '------------------------------------------'
echo "🏃‍♂️ [Run 1]: Running PHPUnit on Single Site"
composer phpunit --ansi

echo "🧹 Removing files before testing nightly WP..."
rm -rf "$WP_TESTS_DIR" "$WP_CORE_DIR"
echo "🧹 Removing files before testing WPMS..."
rm "${DIRNAME}/../tests/wp-latest.json"

bash "${DIRNAME}/install-wp-tests.sh" wordpress_test root root 127.0.0.1 latest true
echo '------------------------------------------'
echo "🏃‍♂️ [Run 2]: Running PHPUnit on Multisite"
composer test:multisite --ansi

setup_wp_nightly() {
download http://api.wordpress.org/core/version-check/1.7/ /tmp/wp-latest.json
echo "Creating wp-config.php"
wp config create --dbname=wordpress_test --dbuser=root --dbpass=root --dbhost=127.0.0.1 --dbprefix=wptests_ --path="/tmp/wordpress"
wp core install --url=localhost --title=Test --admin_user=admin --admin_password=password [email protected] --path="/tmp/wordpress"
# If nightly version of WP is installed, install latest Gutenberg plugin and activate it.
echo "Installing Gutenberg plugin"
wp plugin install gutenberg --activate --path="/tmp/wordpress"
}
jazzsequence marked this conversation as resolved.
Show resolved Hide resolved

echo "🧹 Removing files before testing nightly WP..."

echo "🤔 Installing WP Unit tests with WP nightly version..."
bash "${DIRNAME}/install-wp-tests.sh" wordpress_test root root 127.0.0.1 nightly true
echo "📄 Copying wp-latest.json..."
cp /tmp/wp-latest.json "${DIRNAME}/../tests/wp-latest.json"
echo "🏃‍♂️ Running PHPUnit on Single Site (Nightly WordPress)"
composer phpunit --ansi

bash "${DIRNAME}/install-wp-tests.sh" wordpress_test root root 127.0.0.1 latest true
echo "🏃‍♂️ Running PHPUnit on Multisite"
composer test:multisite --ansi
setup_wp_nightly

echo '------------------------------------------'
echo "🏃‍♂️ [Run 3]: Running PHPUnit on Single Site (Nightly WordPress)"
composer phpunit --ansi
71 changes: 71 additions & 0 deletions inc/fonts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Pantheon mu-plugin customizations to the WP_Font_Library added in WordPress 6.5
*
* @package pantheon
*/

namespace Pantheon\Fonts;

/**
* Store the value of wp_get_upload_dir() in a global variable.
* This is to resolve an infinite loop when wp_get_upload_dir is used inside
* our filter of font_dir (because upload_dir is also being filtered).
*
* @var array $wp_upload_dir The value of wp_get_upload_dir().
* @see https://developer.wordpress.org/reference/functions/wp_get_upload_dir/
*/
$_pantheon_upload_dir = wp_get_upload_dir(); // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable

/**
* Kick off our customizations to the WP_Font_Library.
*/
function bootstrap() {
if ( pantheon_modify_fonts_dir() ) {
// Use the new font_dir filter added in WordPress 6.5. See https://github.com/WordPress/gutenberg/pull/57697.
add_filter( 'font_dir', __NAMESPACE__ . '\\pantheon_font_dir', 9 );
}
}
add_action( 'init', __NAMESPACE__ . '\\bootstrap' );

/**
* Get the value of the pantheon_modify_fonts_dir filter.
* By default, this should return true (we're filtering).
*
* @return bool Whether to modify the fonts directory.
*/
function pantheon_modify_fonts_dir() {
/**
* Modify the fonts directory.
*
* By default, this is set to true, so we can override the default fonts directory from wp-content/fonts to wp-content/uploads/fonts.
*
* Use the filter to set to false and use the default WordPress behavior (committing fonts to your repository and pushing from dev -> test -> live).
*
* @param bool $modify_fonts_dir Whether to modify the fonts directory.
*/
return apply_filters( 'pantheon_modify_fonts_dir', true );
}
jazzsequence marked this conversation as resolved.
Show resolved Hide resolved

/**
* Define a custom font directory for the WP Font Library.
* Default to {WP_CONTENT_DIR}/uploads/fonts.
*
* @param array $defaults The default settings for the font directory.
*/
function pantheon_font_dir( $defaults ) {
global $_pantheon_upload_dir;
$uploads_basedir = $_pantheon_upload_dir['basedir'];
$uploads_baseurl = $_pantheon_upload_dir['baseurl'];

// Set our font directory.
$font_dir = $uploads_basedir . '/fonts';
$font_url = $uploads_baseurl . '/fonts';

$defaults['path'] = $font_dir;
$defaults['url'] = $font_url;
$defaults['basedir'] = $font_dir;
$defaults['baseurl'] = $font_url;
jazzsequence marked this conversation as resolved.
Show resolved Hide resolved

return $defaults;
}
4 changes: 4 additions & 0 deletions pantheon.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
if ( ! defined( 'DISABLE_PANTHEON_UPDATE_NOTICES' ) || ! DISABLE_PANTHEON_UPDATE_NOTICES ) {
require_once 'inc/pantheon-updates.php';
}
// If the WP Font Library exists, we can add our font directory modifications. Use version_compare because the Font Library isn't actually loaded yet.
if ( version_compare( _pantheon_get_current_wordpress_version(), '6.5' ) ) {
require_once 'inc/fonts.php';
}
if ( ! defined( 'RETURN_TO_PANTHEON_BUTTON' ) || RETURN_TO_PANTHEON_BUTTON ) {
require_once 'inc/pantheon-login-form-mods.php';
}
Expand Down
Loading
Loading