• Stars
    star
    112
  • Rank 312,240 (Top 7 %)
  • Language
    Shell
  • License
    MIT License
  • Created about 9 years ago
  • Updated over 4 years ago

Reviews

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

Repository Details

Git hooks to encourage well-written WordPress.

WP Enforcer

GitHub release GitHub license

When writing for a platform as big as WordPress – especially in a team environment – a consistent set of coding standards is vital. Having multiple developers committing code using a mixture of spaces and tabs, inconsistent formatting, or otherwise varying coding styles can lead to headache-inducing merge conflicts.

There are a number of great tools available to check against established coding standards (namely PHP_CodeSniffer), but setting them up on a project can be a pain. WP Enforcer is designed to make this setup a breeze.

How does it work?

WP Enforcer uses Git Hooks to automatically run PHP_CodeSniffer every time a developer tries to make a commit; if the code isn't up-to-snuff, the commit will be rejected and the developer will get a message explaining what needs to be fixed.

Note: If your project already has messy standards you may not want to make WP Enforcer mandatory until after you've cleaned up the codebase or suddenly commits will be quite painful! See Adding to an Existing Project for an easy work-around.

Installation

The easiest way to install WP Enforcer is via Composer:

$ composer require --dev stevegrunwell/wp-enforcer

This will add WP Enforcer to your composer.json file and install the WP Enforcer package.

Next, you'll need to run the WP Enforcer installation script to copy the Git hooks into your local repository:

$ ./vendor/bin/wp-enforcer

If you'd like to require WP Enforcer for all developers on your project, you can add the following scripts to your composer.json file to have WP Enforcer automatically set up Git hooks on composer install and composer update (for more information, please see Composer Scripts):

{
	"scripts": {
		"post-install-cmd": [
			"wp-enforcer"
		],
		"post-update-cmd": [
			"wp-enforcer"
		]
	}
}

Writing for WordPress.com VIP

WP Enforcer includes built-in support for the WordPress.com VIP coding standards, simply add --vip when you run the wp-enforcer command (including in the post-install and post-update commands).

If you need to switch an existing project to the WordPress.com VIP coding standards, you can add the following to your project's phpcs.xml file (replacing "WordPress-Extra", if it's set):

<rule ref="WordPress-VIP" />

Using a custom ruleset

If you want to use a custom XML configuration file with PHP_CodeSniffer, add --ruleset=[FILE] when you run the wp-enforcer command.

Configuration

PHP_CodeSniffer allows you to define a custom, default ruleset by creating a phpcs.xml file in your project root. WP Enforcer will automatically create a phpcs.xml file in your project if one doesn't already exist, but you can edit this to exclude certain files/directories, ignore specific rules, or load additional rulesets.

For a complete list of configuration options, see the PHP_CodeSniffer ruleset.xml standard.

Adding to an Existing Project

Adding coding standards to an existing project can be a painful process. WP Enforcer will only run the Git hook against changed files, but if the current codebase is a mess, making even standards cleanup commits becomes a giant hassle.

If you're adding WP Enforcer to a current project, it's recommended to follow the installation instructions above in a new clean-up branch, then remove the pre-commit hook:

$ rm .git/hooks/pre-commit

Reminder: Use the rm command with care; double-check everything you're passing to it before you execute, as we'd hate to see you lose valuable repository information (or worse: your operating system).

This leaves you with a copy of PHP_CodeSniffer, a phpcs.xml file, and the WordPress Coding Standards but removes the "don't commit improperly formatted code" restrictions. While still in your clean-up branch, work through the codebase and clean up coding standards, running PHP_CodeSniffer manually as needed:

$ ./vendor/bin/phpcs

You may also run PHP Code Beautifier and Fixer, which can fix many common indentation and formatting issues automatically, using the PHP Code Beautifier and Fixer (phpcbf) command:

$ ./vendor/bin/phpcbf

Once CodeSniffer comes back clean, re-run the WP Enforcer installation script to restore the Git hook, forcing all future commits to be clean:

$ ./vendor/bin/wp-enforcer

Finally, merge your clean-up branch into master, kicking your project's coding standards compliance into high gear.

Bypassing WP Enforcer

If it's necessary to bypass WP Enforcer's sniffs for a particular commit (for instance, you're committing small chunks of a file that has older, non-compliant code) you may do so by using git commit --no-verify.

It's recommended that WP Enforcer is only bypassed when making changes in legacy files that simply haven't been cleaned up yet. For more on integrating WP Enforcer with legacy projects, please see Adding to an Existing Project.

More Repositories

1

asimov

Automatically exclude development dependencies from Apple Time Machine backups
PHP
1,598
star
2

wp-cache-remember

Helper for the WordPress object cache and transients.
PHP
144
star
3

one-time-callbacks

Enable WordPress actions and filter callbacks to be called exactly once.
PHP
65
star
4

schemify

Automatically generate Schema.org JSON-LD markup for WordPress content.
PHP
57
star
5

lost-in-translation

Uncover missing translations and localization strings in Laravel applications.
PHP
35
star
6

time-constants

A series of constants designed to make it easier to express time in PHP applications
PHP
25
star
7

wp404

Get in-depth information about the 404 errors that occur on your WordPress site.
PHP
21
star
8

mcavoy

Discover what visitors are searching for on your WordPress site.
PHP
20
star
9

revision-strike

Periodically purge old post revisions via WP Cron.
PHP
17
star
10

wordpress-git

Reveal.js version of my "Keeping WordPress Under [Version] Control with Git" blog post
HTML
17
star
11

advanced-post-excerpt

Replace the default Post Excerpt meta box with a superior editing experience.
PHP
15
star
12

reveal-ga

Add Google Universal Analytics tracking to your reveal.js presentation.
JavaScript
15
star
13

wp-admin-tabbed-settings-pages

A polyfill for Trac #51086, bringing tabbed settings pages into WP-Admin.
PHP
13
star
14

phpunit-markup-assertions

Assertions for PHPUnit to verify the presence or state of elements within markup
PHP
12
star
15

building-for-php-cli

Slides for "Building for the PHP Command Line Interface"
CSS
12
star
16

wp-client-reference

A WordPress plugin designed for agencies to embed client documentation directly into a WordPress site
PHP
10
star
17

php-cli-examples

Examples to accompany "Building for the PHP Command Line Interface"
8
star
18

intro-to-ci-cd

Slides for "Build and Release Confidently with Continuous Integration and Delivery"
CSS
8
star
19

writing-wp-cli-scripts-that-work

Slides for "Writing WP-CLI Commands That Work!"
CSS
8
star
20

wp-password-generator

The WP Password Generator plugin takes the hassle out of creating new WordPress users by generating random, secure passwords with one click.
PHP
7
star
21

wordpress-starter-theme

My WordPress starter theme so I don't have to repeat myself at the beginning of every project.
PHP
5
star
22

intro-to-testing

Slides for "Testing Like You've Never Tested Before (Because You Haven't)"
CSS
5
star
23

wp-encrypted-options

An API for saving encrypted data into the wp_options table. **NOT YET READY FOR PRODUCTION USE!**
PHP
5
star
24

platform-plugin

Slides for "Considerations & Concerns for Platform Plugin Development"
CSS
5
star
25

technical-debt

Slides for "Up to my Eyeballs in Technical Debt!"
CSS
5
star
26

mailto-link-formatter

Composer package for easily generating RFC 6068-compliant mailto: links in your markup.
PHP
5
star
27

vvv-redis

Custom VVV utility to automatically install and configure Redis
Shell
4
star
28

simple-twitter-timeline

A PHP class for reading public Twitter timelines. Designed with "Latest Tweets"-style widgets in mind.
PHP
4
star
29

semver-parser

Library for parsing and manipulating Semantic Versioning ("SemVer") relases
PHP
4
star
30

pupper

A Twitter clone for use in training courses
PHP
3
star
31

stevegrunwell-com-legacy

Source for http://stevegrunwell.com running the Grunwell 2012 theme.
PHP
3
star
32

wp-analysis

A custom PHP_CodeSniffer ruleset and report format to generate user-friendly details about what high-level issues exist and *why* they're important.
PHP
3
star
33

runkit7-installer

Composer package to automate the installation of Runkit7 in development + CI environments
Shell
3
star
34

protecting-yourself-online-irl

Slides for "Protecting Yourself Online & IRL"
CSS
3
star
35

professional-development-professional-developers

Slides for "Professional Development for Professional Developers"
CSS
3
star
36

code-review-for-me-and-you

Slides for "Code Review: For Me & You"
CSS
3
star
37

php-namespaces

Slides for "A Crash-course in PHP Namespaces for WordPress Developers"
CSS
3
star
38

parse-video-metadata

Extract meta data from videos uploaded to the WordPress media gallery.
PHP
2
star
39

stevegrunwell-com

Source for SteveGrunwell.com in 2018 (not yet launched!)
PHP
2
star
40

son-of-clippy

Add "fun" animated characters to your WordPress edit screens!
JavaScript
2
star
41

write-some-docs

Slides for "I'd Like to Write the World Some Docs"
CSS
2
star
42

smee

ABANDONED: Please use https://github.com/BrainMaestro/composer-git-hooks instead! Library for automatically copying Git hooks into a project.
PHP
2
star
43

taking-the-web-offline

Slides for "Taking the Web Offline"
CSS
2
star
44

wordpress-starter-plugin

A quick template to start a new WordPress plugin. Meant to complement https://github.com/stevegrunwell/wordpress-starter-theme
PHP
2
star
45

laravel-authentication-made-easy

Slides for "Laravel Authentication and Permissions Made Easy"
CSS
2
star
46

chronology

PHP
1
star
47

up-and-running-with-wp-cli

Slides for "Up and Running with WP-CLI"
CSS
1
star
48

wp-test-assertions

Additional PHPUnit assertions for testing WordPress plugins and themes.
Shell
1
star
49

mustache-auction

Auction your facial hair style this Movember in the name of men's health!
PHP
1
star
50

grunwell-reveal-theme

CSS overrides for Reveal.js presentations
CSS
1
star
51

wordpress-security-basics

Presentation on WordPress Security Basics based on http://stevegrunwell.com/blog/wordpress-security-basics
CSS
1
star
52

structured-data

Slides for "Computers <3 Structured Data"
CSS
1
star
53

custom-orders-table

Slides for "Custom Tables & the Checkout Bottleneck"
HTML
1
star
54

githookd

Abandoned: Please see https://github.com/BrainMaestro/composer-git-hooks instead. PHP-CLI framework for installing Git Hooks automatically.
PHP
1
star
55

volunteer-line

*Work in Progress* Connecting volunteers of grass-roots campaigns with the community
PHP
1
star
56

laravel-in-a-nutshell

A basic overview of Laravel 5.0 principles for developers new to the framework.
HTML
1
star
57

php-compatibility

Test a package's compatibility with different versions of PHP.
Shell
1
star
58

Barker

A jQuery-powered, Growl-like notification system for in-page news, tweets, and more
JavaScript
1
star