• This repository has been archived on 10/Nov/2022
  • Stars
    star
    282
  • Rank 146,108 (Top 3 %)
  • Language
    Shell
  • License
    MIT License
  • Created about 11 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

DEPRECATED. Common code used during development of WordPress plugins and themes

wp-dev-lib

⚠️ This project is no longer maintained or supported!

This toolset was created before there were reliable ways of pulling in developer tools using Composer and npm. We now recommend adding linting and testing support by adding each tool as a developer dependency.


Common tools to facilitate the development and testing of WordPress themes and plugins.

Great for adding coding standards, linting and automated testing even to legacy projects since checks are applied to new code only by default.

Build Status

Install

Using Composer

composer require --dev xwp/wp-dev-lib

which will place it under vendor/xwp/wp-dev-lib.

Using npm

npm install --save-dev xwp/wp-dev-lib

which will place it under node_modules/xwp/wp-dev-lib. Lock it to a specific release version number by appending it to the package name xwp/wp-dev-lib#1.2.3 where 1.2.3 is the version number.

As Git Submodule

git submodule add -b master https://github.com/xwp/wp-dev-lib.git dev-lib

To update the library with the latest changes:

git submodule update --remote dev-lib
git add dev-lib
git commit -m "Update dev-lib"

Configure

This tool comes with sample configuration files for the following linters:

Copy the files you need to the root directory of your project.

It is a best practice to install the various tools as dependencies in the project itself, pegging them at specific versions as required. This will ensure that the the tools will be repeatably installed across environments. When a tool is installed locally, it will be used instead of any globally-installed version.

Suggested Composer Packages

Add these as development dependencies to your project:

composer require --dev package/name

Automate

Locally with Git Hooks

This tool comes with a pre-commit hook which runs all linters, tests and checks before every commit to your project.

To add the hook with Composer we suggest to use brainmaestro/composer-git-hooks:

composer require --dev brainmaestro/composer-git-hooks

with the following configuration added to composer.json:

{
  "extra": {
    "hooks": {
      "pre-commit": "./vendor/xwp/wp-dev-lib/scripts/pre-commit"
    }
  }
}

and two additional scripts that automatically setup the hooks during composer install:

{
  "scripts": {
    "post-install-cmd": [
      "vendor/bin/cghooks add --no-lock"
    ],
    "post-update-cmd": [
      "vendor/bin/cghooks update"
    ],
  }
}

With npm we suggest to use husky:

npm install husky --save-dev

with the following script added to your package.json:

{
  "scripts": {
    "precommit": "./node_modules/wp-dev-lib/scripts/pre-commit"
  }
}

Alternatively, create a symlink at .git/hooks/pre-commit pointing to pre-commit using the bundled script:

./vendor/xwp/wp-dev-lib/scripts/install-pre-commit-hook.sh

To ensure that everyone on your team has the pre-commit hook added automatically, we recommend using the Composer or npm scripts as described above as the package managers will set up the pre-commit hook during the install phase.

Travis CI

Copy the sample-config/.travis.yml file into the root of your repo:

cp ./vendor/xwp/wp-dev-lib/sample-config/.travis.yml .

Note that the bulk of the logic in this config file is located in travis.install.sh, travis.script.sh, and travis.after_script.sh.

Edit the .travis.yml to change the target PHP version(s) and WordPress version(s) you need to test for and also whether you need to test on multisite or not:

php:
  - 5.3
  - 7.0

env:
  - WP_VERSION=latest WP_MULTISITE=0
  - WP_VERSION=latest WP_MULTISITE=1
  - WP_VERSION=trunk WP_MULTISITE=0
  - WP_VERSION=trunk WP_MULTISITE=1

Having more variations here is good for open source plugins, which are free for Travis CI. However, if you are using Travis CI with a private repo you probably want to limit the jobs necessary to complete a build. So if your production environment is running PHP 5.5, is on the latest stable version of WordPress, and is not multisite, then your .travis.yml could just be:

php:
  - 5.5

env:
  - WP_VERSION=4.0 WP_MULTISITE=0

This will greatly speed up the time build time, giving you quicker feedback on your pull request status, and prevent your Travis build queue from getting too backlogged.

Pre-commit Tips

The default behaviour for the linters is to only report errors on lines that are within actual staged changes being committed. So remember to selectively stage the files (via git add ...) or better the patches (via git add -p ...).

Skipping Checks

If you do need to disable the pre-commit hook for an extenuating circumstance (e.g. to commit a work in progress to share), you can use the --no-verify argument:

git commit --no-verify -m "WIP"

Alternatively, you can also selectively disable certain aspects of the pre-commit hook from being run via the DEV_LIB_SKIP environment variable. For example, when there is a change to a PHP file and there are PHPUnit tests included in a repo, but you've just changed a PHP comment or something that certainly won't cause tests to fail, you can make a commit and run all checks except for PHPUnit via:

DEV_LIB_SKIP=phpunit git commit

You can string along multiple checks to skip via commas:

DEV_LIB_SKIP=composer,phpunit,phpcs,yuicompressor,jscs,jshint,codeception,executebit git commit

Naturally you'd want to create a Git alias for whatever you use most often, for example:

git config --global alias.commit-without-phpunit '!DEV_LIB_SKIP="$DEV_LIB_SKIP,phpunit" git commit'

Which would allow you to then do the following (with Bash tab completion even):

git commit-without-phpunit

Aside, you can skip Travis CI builds by including [ci skip] in the commit message.

Running Specific Checks

If you would like to run a specific check and ignore all other checks, then you can use DEV_LIB_ONLY environment variable. For example, you may want to only run PHPUnit before a commit:

DEV_LIB_ONLY=phpunit git commit

Manually Invoking Pre-commit

Sometimes you may want to run the pre-commit checks manually to compare changes (patches) between branches much in the same way that Travis CI runs its checks. To compare the current staged changes against master, do:

DIFF_BASE=master ./vendor/xwp/wp-dev-lib/scripts/pre-commit

To compare the committed changes between master and the current branch:

DIFF_BASE=master DIFF_HEAD=HEAD ./vendor/xwp/wp-dev-lib/scripts/pre-commit

Limiting Scope of Checks

A barrier of entry for adding automated code quality checks to an existing project is that there may be a lot of issues in your codebase that get reported initially. So to get passing builds you would then have a major effort to clean up your codebase to make it conforming to PHP_CodeSniffer, JSHint, and other tools. This is not ideal and can be problematic in projects with a lot of activity since these changes will add lots of conflicts with others' pull requests.

To get around this issue, there is now an environment variable available for configuration: CHECK_SCOPE. By default its value is patches which means that when a pre-commit runs or Travis runs a build on a pull request or commit, the checks will be restricted in their scope to only report on issues occurring in the changed lines (patches). Checking patches is the most useful, but CHECK_SCOPE=changed-files can be added in the project config so that the checks will be limited to the entirety of any file that has been modified.

Also important to note that when the the pre-commit check runs, it will run the linters (PHPCS, JSHint, JSCS, etc) on the staged changes, not the files as they exist in the working tree. This means that you can use git add -p to interactively select changes to stage (which is a good general best practice in contrast to git commit -a), and any code excluded from being staged will be ignored by the linter. This is very helpful when you have some debug statements which you weren't intending to commit anyway (e.g. print_r() or console.log()).

With CHECK_SCOPE=patches and CHECK_SCOPE=changed-files available, it is much easier to integrate automated checks on existing projects that may have a lot of nonconforming legacy code. You can fix up a codebase incrementally line-by-line or file-by-file in the normal course of fixing bugs and adding new features.

If you want to disable the scope-limiting behavior, you can define CHECK_SCOPE=all.

Environment Variables

You may customize the behavior of the .travis.yml and pre-commit hook by specifying a .dev-lib (formerly .ci-env.sh) Bash script in the root of the repo, for example:

DEFAULT_BASE_BRANCH=develop
PHPCS_GITHUB_SRC=xwp/PHP_CodeSniffer
PHPCS_GIT_TREE=phpcs-patch
PHPCS_IGNORE='tests/*,includes/vendor/*' # See also PATH_INCLUDES below
WPCS_GIT_TREE=develop
WPCS_STANDARD=WordPress-Extra
DISALLOW_EXECUTE_BIT=1
YUI_COMPRESSOR_CHECK=1
PATH_INCLUDES="docroot/wp-content/plugins/acme-* docroot/wp-content/themes/acme-*"
CHECK_SCOPE=patches

Set DEFAULT_BASE_BRANCH to be whatever your default branch is in GitHub; this is use when doing diff-checks on changes in a branch build on Travis CI. The PATH_INCLUDES is especially useful when the dev-lib is used in the context of an entire site, so you can target just the themes and plugins that you're responsible for. For excludes, you can specify a PHPCS_IGNORE var and override the .jshintignore; there is a PATH_EXCLUDES_PATTERN as well.

PHPUnit Code Coverage

The plugin-tailored phpunit.xml has a filter in place to restrict PHPUnit's code coverage reporting to only look at the plugin's own PHP code, omitting the PHP from WordPress Core and other places that shouldn't be included. The filter greatly speeds up PHPUnit's execution. To get the code coverage report written out to a code-coverage-report directory:

phpunit --coverage-html code-coverage-report/

Then you can open up the index.html in that directory to learn about your plugin's code coverage.

Codeception

Bootstrap Codeception by:

wget -O /tmp/codecept.phar http://codeception.com/codecept.phar
php /tmp/codecept.phar bootstrap

Then update Acceptance tests configuration to reflect your own environment settings:

vim tests/acceptance.suite.yml

You can generate your first test, saved to tests/acceptance/WelcomeCept.php by:

php /tmp/codecept.phar generate:cept acceptance Welcome

Gitter

Create an empty .gitter file in the root of your repo and a Gitter chat badge will be added to your project's README.

Plugin Helpers

The library includes a WordPress README parser and converter to Markdown, so you don't have to manually keep your readme.txt on WordPress.org in sync with the readme.md you have on GitHub. The converter will also automatically recognize the presence of projects with Travis CI and include the status image in the markdown. Screenshots and banner images for WordPress.org are also automatically incorporated into the readme.md.

What is also included in this repo is an svn-push to push commits from a GitHub repo to the WordPress.org SVN repo for the plugin. The /assets/ directory in the root of the project will get automatically moved one directory above in the SVN repo (alongside trunk, branches, and tags). To use, include an svn-url file in the root of your repo and let this file contains he full root URL to the WordPress.org repo for plugin (don't include trunk).

Contribute

See the contributor documentation.

Credits

See all contributors.

The utilities in this project were first developed to facilitate development of XWP's plugins.

More Repositories

1

stream

🗄️ Stream plugin for WordPress
PHP
408
star
2

wp-cli-ssh

[OBSOLETE] Seamlessly run WP-CLI commands on a remote server via SSH
PHP
157
star
3

wp-customize-posts

Edit posts and postmeta in the WordPress Customizer
PHP
144
star
4

site-performance-tracker

Allows you to detect and track site performance metrics
PHP
92
star
5

wp-plugin-dependencies

WordPress plugin dependency management.
PHP
88
star
6

wp-foo-bar

WordPress plugin template for extending Gutenberg
PHP
75
star
7

vassh

vassh: Vagrant Host-Guest SSH Command Wrapper/Proxy/Forwarder
Shell
64
star
8

wp-widget-customizer

[OBSOLETE] Widget Customizer plugin for WordPress (now merged into Core)
JavaScript
54
star
9

wp-customize-snapshots

Customize Snapshots WordPress Plugin
PHP
53
star
10

wp-dependency-minification

Dependency Minification plugin for WordPress
PHP
53
star
11

wp-jquery-ui-datepicker-skins

jQuery UI datepicker skins for WordPress Admin color schemes
CSS
51
star
12

engineering-best-practices

XWP Engineering Best Practices
49
star
13

wp-docker

A docker environment for WordPress site development.
PHP
42
star
14

wp-js-widgets

JS Widgets plugin for WordPress
PHP
40
star
15

wp-mentionable

PHP
37
star
16

wp-core-media-widgets

Feature plugin for development of new core media widgets for image, video, and audio (and others)
PHP
35
star
17

travel

WordPress AMP Adventures Themes (Native AMP)
PHP
34
star
18

vip-site-template

Project template for WordPress VIP hosted websites with local development environment
PHP
34
star
19

wp-customize-image-gallery-control

PHP
31
star
20

ampnews

WordPress AMP News Themes (Native AMP)
PHP
27
star
21

wp-settings-revisions

Settings Revisions plugin for WordPress
PHP
26
star
22

config-driven-wp

Config-Driven WordPress
PHP
25
star
23

wp-customize-rest-resources

Customize REST Resources plugin for WordPress
PHP
22
star
24

wp-force-featured-image

PHP
20
star
25

wp-customizer-dev-tools

Customizer Dev Tools
JavaScript
19
star
26

wp-vip-twig

VIP Twig WordPress Plugin
PHP
19
star
27

wp-customizer-browser-history

Customizer Browser History
JavaScript
19
star
28

material-design-wp-theme

Material Design theme for WordPress
PHP
18
star
29

wp-customize-widgets-plus

WordPress plugin for lab features and a testbed for improvements to Widgets and the Customizer.
PHP
18
star
30

block-scaffolding-wp

WordPress plugin template for extending Gutenberg
PHP
18
star
31

wp-customize-pane-resizer

Feature plugin for WordPress Trac #32296: Allow the customizer be made wider
PHP
17
star
32

wp-customize-inline-editing

Customize Inline Editing WordPress Plugin
JavaScript
17
star
33

wp-customize-object-selector

Customize Object Selector plugin for WordPress
PHP
17
star
34

wp-customizer-blank-slate

PHP
17
star
35

wp-customize-react-street-address-control

Customize React Street Address Control
HTML
15
star
36

wp-customize-setting-validation

[OBSOLETE] Customize Setting Validation plugin for WordPress. See https://core.trac.wordpress.org/changeset/37476
PHP
15
star
37

wp-shortcode-ui-richtext

Shortcake (Shortcode UI) Richtext
PHP
14
star
38

material-design-wp-plugin

Material Design plugin for WordPress
JavaScript
13
star
39

multisite-search

Creates a search and index engine for WordPress Multisite installations.
PHP
13
star
40

wp-customize-partial-refresh

[OBSOLETE: Now part of Core] Customize Partial Refresh plugin for WordPress
PHP
12
star
41

wp-media-placeholders

Media Placeholders Plugin for WordPress
PHP
11
star
42

git-scripts

Misc scripts for working with Git
Python
11
star
43

undercurrent

Common tools to facilitate frontend development and testing.
JavaScript
9
star
44

wp-phpstorm

Shell
9
star
45

wp-customizer-everywhere

Customizer Everywhere plugin for WordPress
PHP
8
star
46

unsplash-wp

PHP
8
star
47

wp-option-usage-checker

Option Usage Checker plugin for WordPress
PHP
8
star
48

wp-customize-featured-content-demo

PHP
7
star
49

wp-next-recent-posts-widget

PHP
6
star
50

wp-tide-test-plugin

Plugin for testing wp-tide scoring
PHP
6
star
51

wp-github-pull-request-travis-ci-trac-sync

Sync from GitHub Pull Requests to WordPress Trac via Travis CI (see Trac #34694)
Shell
6
star
52

code-review-tools

Shell
5
star
53

stream-roadmap

The Stream community collaborative roadmap.
5
star
54

standalone-customizer-controls

Standalone Customizer Controls
PHP
5
star
55

fe-dev-lib

Common tools to facilitate the frontend development and testing of WordPress themes and plugins
JavaScript
5
star
56

wp-custom-scss-demo

Proof of concept for adding SCSS precompiler for Custom CSS in core. Depends on 4.7 and core patch.
PHP
5
star
57

gutenblox

The other interfacefor building with Blocks.
4
star
58

wp-plugin-safe-mode

Enables a debugging safe mode by preventing any plugins from loading.
4
star
59

wp-customize-widget-sidebar-meta

PHP
4
star
60

wp-customize-nav-menu-item-custom-fields

JavaScript
4
star
61

xwpwa

XWPWA
HTML
4
star
62

wp-customize-content-widgets

Extension to the Customize Posts and Shortcake plugins to add post/page-specific widget areas in content (an implementation of content blocks).
4
star
63

wp-customize-concurrency

PHP
4
star
64

wp-a11y-speech-synthesis

Accessibility (a11y) Speech Synthesis WordPress Plugin
JavaScript
4
star
65

wp-admin-screen-search

PHP
3
star
66

wp-customize-posts-css

Customize Posts CSS plugin
PHP
3
star
67

wp-customize-preview-fetch-api

Trac #42163: Customize: Add support for previewing requests made via the Fetch API
JavaScript
3
star
68

syntaxhighlighter-amplified

Adding AMP support to the SyntaxHighlighter Evolved plugin for WordPress
PHP
3
star
69

wp-customize-input-validity-constraints

Customize Input Validity Constraints plugin
PHP
3
star
70

wordpress-tests-installer

Handles installing the WordPress PHPUnit test suite with composer
PHP
2
star
71

wp-better-find-posts

Better Find Posts WordPress plugin
PHP
2
star
72

wp-stream.com

Source of the Stream plugin website.
JavaScript
2
star
73

wp-post-custom-content

Post Custom Content WordPress plugin
PHP
2
star
74

io-php-wp-autoload

[READ ONLY]
PHP
2
star
75

wp-customizer-responsive-device-preview

WordPress Plugin: Customizer Responsive Server-Side Components Device Preview
PHP
2
star
76

wp-post-inspector

WordPress Plugin that adds a metabox displaying Post information
PHP
2
star
77

formation

PHP
1
star
78

wp-media-selector-field

PHP
1
star
79

site-monorepo-example

PHP
1
star
80

delivery-handbook

1
star
81

wp-customizer-device-preview

[OBSOLETE] WordPress.com Customizer Device Preview (Polyfill)
PHP
1
star
82

minnow-amp

WordPress child theme of Minnow that adds AMP support
PHP
1
star
83

wp-trac-38624

Feature plugin for Loading Starter Content on non-fresh sites
PHP
1
star
84

xwp-wpengine-site-template

Template for WpEngine
1
star
85

wp-qunit-print-script-dependencies

QUnit Print Script Dependencies WP-CLI Command
PHP
1
star
86

wp-styles-control-background-position

Styles Control: background-position
PHP
1
star
87

Lottie-block

JavaScript
1
star
88

artwork

Internal artwork for XWP projects.
1
star
89

amp-app-shell

Support for app shell navigation via AMP Shadow DOM.
PHP
1
star
90

vip-wpcom-plugins

Composer Repository for VIP WPCOM Plugins
PHP
1
star
91

wp-safe-input

A PHP library for working with form inputs in WordPress
PHP
1
star
92

wp-customize-loopconf-workshop-advanced-examples

JavaScript
1
star
93

wp-customize-tests

Config and code coverage for PHPUnit tests for the WordPress Customizer
Shell
1
star
94

wp-customize-users

1
star
95

wp-multisite-subdirectory-nginx

Proof of concept of a WP subdirectory multisite in a subdirectory.
PHP
1
star
96

wp-flint

Create, discover, and join projects.
PHP
1
star
97

wp-search-tweets-widget

PHP
1
star
98

stream-sumo-driver

Send and Retrieve your Stream records with Sumo Logic.
PHP
1
star
99

wp-pluglets

Tiny reusable plugins for code that might normally be dumped into a theme.
1
star