Skip to content

Instantly share code, notes, and snippets.

@kadamwhite
Last active October 17, 2018 22:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kadamwhite/4bccec8d6bd1df8d57ba89396d425d0c to your computer and use it in GitHub Desktop.
Save kadamwhite/4bccec8d6bd1df8d57ba89396d425d0c to your computer and use it in GitHub Desktop.
Helpers I use for local WordPress development.
diff --git wp-config.php wp-config.php
index 298dad4..03d126f 100644
--- wp-config.php
+++ wp-config.php
@@ -142,4 +142,6 @@ if ( ! file_exists( ABSPATH . 'wp-settings.php' ) ) {
echo '<h1>WordPress is missing.</h1>';
die(1);
}
-require_once( ABSPATH . 'wp-settings.php' );
+if ( ! defined( 'WP_CLI' ) ) {
+ require_once( ABSPATH . 'wp-settings.php' );
+}
paths:
# Use the Chassis box normally...
base: .
content: content
# But use my development copy of WordPress for the WP source
#wp: ../svn/build
wp: ../git/build
# Also use your development copy of WordPress for the unit test
# framework, and for the unit tests themselves
synced_folders:
#../svn: /vagrant/extensions/tester/wpdevel
../git: /vagrant/extensions/tester/wpdevel
# Set the host to `core.local` to distinguish from other chassis boxes
hosts:
- core.local
# Explicitly set database configuration to avoid warning with Tester
database:
name: wordpress
user: wordpress
password: vagrantpassword
prefix: wp_
extensions:
- chassis/tester
- chassis/xdebug
# Run in multisite mode (totally optional)
multisite: false
# XDebug IDE key
ide: VSCODE

Local WordPress Core Development Helpers

This is a collection of scripts, bash functions, and config files that help me do WordPress core development.

Folder Structure

  • /wp: parent folder
  • /wp/svn: SVN checkout
  • /wp/git: Git checkout
  • /wp/{ git | svn }/wp-test-config.php: see attached file
  • /wp/{ git | svn }/src/wp-config.php: see attached file
  • /wp/core-vm: Chassis install set up to use one or the other of those checkouts per these instructions
  • /wp/core-vm/config.local.yaml: see attached file

A small change must be made to Chassis' wp-config.php; see attached file.

Functions & Scripts

switch-vcs-provider

Helper script to update the Chassis configuration to point to either the "git" or "svn" checkout, then reload the VM. Usage: switch-vcs-provider git or switch-vcs-provider svn.

wp-test

Run some or all of the WP unit tests inside the Chassis VM. It passes all arguments through to PHPUnit within the VM, so you can use arguments like --filter e.g. wp-test --filter Test_Class_Name

wp-lint

Run PHPCS. Note that currently this just lints the git checkout, and doesn't intelligently determine which one we're on (or run the command inside the VM, obviating the question).

Other Notes

grunt must be run to ensure the build directory is generated, or the VM won't start. grunt should be run before running unit tests as well.

#!/bin/sh
':' //; exec "$(command -v nodejs || command -v node)" "$0" "$@"
// ^^^ Lovely polyglot script to permit usage via node _or_ via bash: see
// http://unix.stackexchange.com/questions/65235/universal-node-js-shebang
const { execSync } = require( 'child_process' );
const { readFileSync, writeFileSync } = require( 'fs' );
const { join } = require( 'path' );
console.log( '' );
// Ensure we have a valid target checkout type.
// ============================================================================
const target = ( process.argv[2] || '' ).toLowerCase().trim();
if ( ! target || ! [ 'svn', 'git' ].includes( target ) ) {
console.log( 'Missing checkout type argument! You must specify one of ' );
console.log( ' switch-wp-vm-checkout git' );
console.log( ' switch-wp-vm-checkout svn' );
console.log( '\nAborting...' );
process.exit( 1 );
}
// Read in the VM configuration file.
// ============================================================================
console.log( `Switching WordPress Core VM to ${ target } checkout...` );
const vmPath = '/wp/core-vm';
const vmConfigPath = join( vmPath, 'config.local.yaml' );
process.chdir( vmPath );
const config = readFileSync( vmConfigPath ).toString();
// Comment out the lines in the config referencing the undesired checkout type,
// and un-comment those referencing the target checkout type.
// ============================================================================
const wpPathTemplate = ( checkoutType, commented ) =>
`${ commented ? '#' : '' }wp: ../${ checkoutType }/build`;
const testerTemplate = ( checkoutType, commented ) =>
`${ commented ? '#' : '' }../${ checkoutType }: /vagrant/extensions/tester/wpdevel`;
const addComment = ( templateFn, target ) => ( {
from: templateFn( target, false ),
to: templateFn( target, true ),
} );
const unComment = ( templateFn, target ) => ( {
from: templateFn( target, true ),
to: templateFn( target, false ),
} );
const transformations = target => {
const origin = target === 'git' ? 'svn' : 'git';
return [
// Swap the WP path
unComment( wpPathTemplate, target ),
addComment( wpPathTemplate, origin ),
// Swap the test directory path
unComment( testerTemplate, target ),
addComment( testerTemplate, origin ),
];
};
const updatedConfig = transformations( target )
.reduce(
( result, transform ) => result.replace( transform.from, transform.to ),
config
)
// Defend against replacements being made multiple times by mistake, which could
// yield multiple comment `#`s and prevent a successful switch back to the other
// VCS provider on a subsequent run.
.replace( /(\n\s*)#+/g, '$1#' );
writeFileSync( vmConfigPath, updatedConfig );
console.log( `Virtual Machine configuration file updated to use ${ target } checkout.` );
// Reload the virtual machine.
// ============================================================================
console.log( '\nReloading the Virtual Machine...' );
// execSync( 'vagrant reload' );
console.log( 'Done!' );
// Done!
// ============================================================================
console.log( '\n\n!! Remember to run `grunt` when you apply a patch !!' );
console.log( ' Happy coding <3\n' );
<?php
// Fool WP-CLI into recognising this as a valid config file
if ( false ) {
require ABSPATH . './wp-settings.php';
}
require '/vagrant/wp-config.php';
<?php
// ===================================================
// Load database info and local development parameters
// ===================================================
$chassisdir = '/vagrant';
if ( file_exists( $chassisdir . '/local-config-tester.php' ) ) {
define( 'WP_LOCAL_DEV', true );
include( $chassisdir . '/local-config-tester.php' );
} elseif ( file_exists( $chassisdir . '/local-config-db.php' ) ) {
define( 'WP_LOCAL_DEV', true );
include( $chassisdir . '/local-config-db.php' );
}
if ( file_exists( $chassisdir . '/local-config.php' ) ) {
defined( 'WP_LOCAL_DEV' ) or define( 'WP_LOCAL_DEV', true );
include( $chassisdir . '/local-config.php' );
}
// =======================
// Load Chassis extensions
// =======================
if ( file_exists( $chassisdir . '/local-config-extensions.php' ) ) {
include( $chassisdir . '/local-config-extensions.php' );
}
// ======================================
// Fake HTTP Host (for CLI compatibility)
// ======================================
if ( empty( $_SERVER['HTTP_HOST'] ) ) {
if ( defined( 'DOMAIN_CURRENT_SITE' ) ) {
$_SERVER['HTTP_HOST'] = DOMAIN_CURRENT_SITE;
} else {
$_SERVER['HTTP_HOST'] = 'vagrant.local';
}
}
// ========================
// Custom Content Directory
// ========================
defined( 'WP_CONTENT_DIR' ) or define( 'WP_CONTENT_DIR', $chassisdir . '/content' );
defined( 'WP_CONTENT_URL' ) or define( 'WP_CONTENT_URL', 'http://' . $_SERVER['HTTP_HOST'] . '/content' );
// =====================
// URL hacks for Vagrant
// =====================
if ( WP_LOCAL_DEV && ! defined( 'WP_SITEURL' ) ) {
define( 'WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/wp' );
if ( ! defined( 'WP_HOME' ) ) {
define( 'WP_HOME', 'http://' . $_SERVER['HTTP_HOST'] );
}
}
// ================================================
// You almost certainly do not want to change these
// ================================================
define( 'DB_CHARSET', 'utf8' );
define( 'DB_COLLATE', '' );
// ==============================================================
// Table prefix
// Change this if you have multiple installs in the same database
// ==============================================================
$table_prefix = 'test_';
if ( defined( 'MULTISITE' ) && ! defined( 'WP_TESTS_MULTISITE' ) ) {
define( 'WP_TESTS_MULTISITE', (bool) MULTISITE );
}
// =====================================
// Errors
// Show/hide errors for local/production
// =====================================
defined( 'WP_DEBUG' ) or define( 'WP_DEBUG', true );
define( 'WP_TESTS_DOMAIN', $_SERVER['HTTP_HOST'] );
define( 'WP_TESTS_EMAIL', 'admin@' . $_SERVER['HTTP_HOST'] );
define( 'WP_TESTS_TITLE', 'Test Blog' );
define( 'WP_PHP_BINARY', 'php' );
// ===================
// Bootstrap WordPress
// ===================
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', $chassisdir . '/wp/' );
}
wp-test() {
EXTRA_ARGS=$@
cd /wp/core-vm
vagrant ssh -c "cd /vagrant/extensions/tester/wpdevel && phpunit $EXTRA_ARGS"
cd -
}
wp-lint() {
cd /wp/git
echo "Running PHPCS in /wp/git"
composer run-script lint
cd -
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment