wpsessions composer for wordpress plugin development

27
Using Composer For WordPress Plugin Development Josh Pollock -- @Josh412 -- JoshPress.net

Upload: josh-pollock

Post on 03-Aug-2015

211 views

Category:

Documents


2 download

TRANSCRIPT

Using Composer For WordPress Plugin

DevelopmentJosh Pollock -- @Josh412 -- JoshPress.net

Hi, I'm Josh, I make WordPress plugins...

These Are Photos Of My Cats

Hypothesis[0]

"reusable code" == "better code"

Hypothesis[1]

The "Do Not Repeat Yourself Principle" Is Correct/Important

Hypothesis[2]

The Single Responsibility Principle Is Correct/Important

Copypasta Is Uni-Directional

That's Bad

Why Composer?

It facilitates sharing your code & other's code.

● It's easy.● It uses VCS, just like you're used to.● It's a standard, works well with standards.● Has WordPress support backed-in.

Out Of Scope For Today

● Installing Composer○ Read https://getcomposer.org/doc/00-intro.md○ Make sure to install globally

● Composer Basics● Composer In Core● Composer For Theme Development● Composer For Full Stack Management

○ I probably can't resist this one:)

In Scope

● Discuss Structure For A Plugin With Composer

● Talk About The Autoloader● Make An Example Plugin● Worry About How To Ship It● Get Some Helpful Links● Take Questions

Plugin Structure

● How much is in the plugin vs separate packages?

● Are we using the autoloader?○ For the dependencies only or for the whole plugin?

● What 3rd party packages are we using?● What about tests?

Questions

Your Code:In The Plugin Or In A Package?

Autoloader

include_once( dirname( __FILE__ ) . '/vendor/autoload.php' );

Composer Autoloader: Fun Facts

● PSR-4, PSR-0, classmap, filemap● Can be used for:

○ Dependencies○ The package's classes

■ ^^ Means you can use it as the autoloader for your plugin.

Autoloaders Are Important!

● Less work/room for errors when adding classes.

● Less barriers to making a new class == smaller classes○ More maintainable○ More reusable○ More likely to abide by the single responsibility

principle

PSR-4 + filemap

https://github.com/postmatic/epoch/blob/master/composer.json

Example Time!

What About Shipping It?

● Dependencies need to get to WordPress.org SVN○ Can't I just commit my dependencies in the real

repo?○ Or automate it with grunt or bash?

● What about when someone checks out from Github?

Concerns

More Examples

● Caldera Metaplate○ Almost totally Composer Packages○ Grunt script for shipping to WordPress.org

● Rarst's Fragment Cache○ Uses Jaquith's tlc-transients package○ Only on Github

● WordPress SEO by Yoast○ Check out how they use scripts in composer.json

@Josh412https://CalderaWP.com

http://JoshPress.net

Thanks.Ya'll Are

Awesome!