LoopConf: Notes from Day 2

Writing WP-CLI Commands That Work

by Steve Grunwell

Benefits

  • Easier to perform bulk ops
  • Better vis into WP internals
    • Cron, cache, transients, etc
  • Not subject to timeouts like browser
  • Better filesystem access
  • Ability to script (automation)

Good places to use it

  • Deploying updates
  • Troubleshooting issues with cron, caching, transients
  • Scheduled maintenance routines
  • Data migrations and transforms
  • YOLO scripts – scripts that can be run once and should never be available in the admin
  • Serialized search and replace

What makes a good command?

  1. Don’t assume anything
    1. The command should be as portable as possible
    2. Don’t assume themes/plugins are present
  2. Composability
    1. Idea: command can be chained (e.g. Gulp)
  3. Readability trumps # of keystrokes
    1. Make commands/flags overtly clear
  4. Stay focused
    1. WP-CLI commands should leverage existing WP functionality

Basic Structure

  • Basic OOP – class extends base class with private & public methods
  • Docblock
    • Description
    • Options – args accepted by command
    • Examples
    • Tags
      • @synopsis – used to validate args
      • @subcommand – set canonical name for command (defaults to method name)
      • @alias – additional handle

I/O

Input

  • Args and assoc_args
  • Confirm
  • Prompt
  • Menus

Output

  • log
  • warn
  • error
  • success
  • tables
  • FORMAT_ITEMS()
  • progress indicator

More good ideas

  • --dry-run – good for trialing destructive runs
  • --verbose
  • --quiet
  • great addition to plugins
  • use wp_parse_args

Performance tips

  • Try to cache whenever possible, but be careful of what persists
  • SAVEQUERIES
  • STOP_THE_INSANITY

Considerations

  • Bash completion not included by default, available via .org
  • Beware in multisite which site you’re working on

Useful globals

  • --user – set user
  • --debug – show all PHP errors
  • --prompt – prompt user to enter values

Gotchas

  • die() and exit() kill the entire process – use return instead
  • you can call other CLI commands from within your commandWP_CLI::sub_command( ... )

Build Your Dreams With The WordPress REST API & Javascript

by K.Adam White

Recent reqs fulfilled prior to merging in WP 4.7

  • Meta handling
  • Password-protected posts
  • Site settings endpoint
  • Improved consistency/examples

Conditional merge with success metrics monitoring:

  1. Plugin utilization
  2. Distributed theme utilization
  3. Custom development implementations
  4. Core dev and feature projects

Plusses

  • Common data
  • Common models

Stronger Than Fear: Mental Health In The Developer Community

by Ed Finkler

Empathetic Communication

by Sharon Steed

Creating The Community You Crave

by Andrew Norcross

Unix Philosophies for WordPress Development

by JJJ

WordPress is the Windows of the web.

– Justin Rohrer

The lesson to be learned from this is that it is often undesirable to go for the right thing first. It is better to get half of the right thing available so that it spreads like a virus. Once people are hooked on it, take the time to improve it to 90% of the right thing.

– Richard P. Gabriel

Resources & Reading

1 thought on “LoopConf: Notes from Day 2”

Leave a Reply

Your email address will not be published. Required fields are marked *