Plugin and Theme Refactoring for WordPress with Michael Bastos [LIVEBOG]

Next up we are with Michael Bastos here at WordCamp San Diego.

Jonathan Dingman March 23, 20139:57 am

Quick disclaimer…this talk is very technical, geared towards advanced users.

Testing, refactoring, and more.

Jonathan Dingman March 23, 20139:57 am

Why refactor PHP code? do it well.

Jonathan Dingman March 23, 201310:00 am

* Never “easily” throw away working code
– It’s still existing code that works, don’t “easily” throw it away
* existing production code = MONEY
– If people are using a plugin, it’s time and money that was spent
* opportunity cost is basic economics
* time and good developers are scarce
* working code saves you time and resources

Jonathan Dingman March 23, 201310:01 am

I forgot to mention, you can follow along the slides here: http://bit.ly/WB7tag

Jonathan Dingman March 23, 201310:01 am

* WooCommerce is a refactor of Jigishop
* WPMU is a refactor of WordPress
* WordPress itself is a refactor of b2
* John O’Nolan wants to fork WP into Ghost

Jonathan Dingman March 23, 201310:02 am

“Why is my code so bad?”

* in general, the php community avoided proper methodologies
* bad habits were due to easy learning
* older version of php lacked real support for Object Oriented (OO) development patterns and designs
* don’t dismiss old themes and plugins, refactor them by first learning what is bad code

Jonathan Dingman March 23, 201310:04 am

Bad code is …

* when you can’t follow the logic
* when you’re afraid to touch it
* A ton of code within a disorganized mess
* functional and procedural

you know it’s time to refactor.

Jonathan Dingman March 23, 201310:05 am

Person Best Practices

* Objectify your theme code, move it out of functions.php into separate php class files
* keep methods at a 5 to 10 line minimum for easy reading and understanding
* if possible, write tests before you start coding

Jonathan Dingman March 23, 201310:06 am

unit testing is the default for WordPress, write tests. there are a number of good examples to build tests for your code.

“Don’t just code for coding sake”

Jonathan Dingman March 23, 201310:07 am

Installing WordPress Tests

* create a test directory inside a new theme directory
* git clone the WP test from github directly, start building out the tests and connected with WP

Reference: https://github.com/nb/wordpress-tests

Jonathan Dingman March 23, 201310:08 am

Setup Composer

http://getcomposer.org

Jonathan Dingman March 23, 201310:11 am

[Michael is digging deep into actual WordPress unit testing]

Jonathan Dingman March 23, 201310:13 am

If you’re not already writing OO code, you need to start. very important for caching and performance.

Jonathan Dingman March 23, 201310:15 am

[Michael diving deep into OO programming, “Refactoring Inline Methods”]

Jonathan Dingman March 23, 201310:18 am

Breaking down your code so it’s easy to read and simple calculations

Jonathan Dingman March 23, 201310:20 am

“Other things to look for…”

* Remove unnecessary duplication
* dealing with long methods
* handling large classes
* wrangling a long list of parameters

Jonathan Dingman March 23, 201310:27 am

Michael is a huge fan of Child Roots Theme

Reference: https://github.com/rydaly/roots-child-theme-template

Jonathan Dingman March 23, 201310:28 am

Michael loves working with WP MVC

Reference: https://github.com/bastosmichael/wp-mvc

This is his own fork of the original.

Jonathan Dingman March 23, 201310:30 am

Michael is working on a plugin – http://wpbundled.com/

Jonathan Dingman March 23, 201310:31 am

that’s it folks! Stay tuned for another session or two later today.