107
WordCamp Orlando, 2015 - Chad Windnagle How To Be A Good Developer Citizen

Good dev citizen

Embed Size (px)

Citation preview

Page 1: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

How To BeA Good Developer

Citizen

Page 2: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Quick Intro(disclaimer)

Page 3: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Not a Wordpress Guy

Page 4: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Actually a Joomla, Symfony, Laravel, and

PHP Guy.

Page 5: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

8+ Years Working with Joomla & PHP

Page 6: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Now I work with Laravel (and WordPress)

Page 7: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

PHP Frameworks

Page 8: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Good* Code

Page 9: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Object Orientation Programming

Page 10: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Documentation

Page 11: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Testing

Page 12: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

These are a few of my favorite things.

Page 13: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

I want to bring Modern PHP

techniques to WordPress Developers

Page 14: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 15: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 16: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

No! Definitely Not.

Page 17: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 18: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Let’s get started

Page 19: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Stop using themes for functionality.

Page 20: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Themes are for Presentation.

Page 21: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Only presentation.

Page 22: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Themes should never

• Touch $wp_query• Change the post content• Change the post title• Change the meta data• Change URL parameter• Change anything except CSS, javascript, and

markup

Page 23: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

If your site will not function the same with a different theme, you are

doing it wrong.

Page 24: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

“But I need functions.php!”

Page 25: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

No you don’t. You need a plugin.

Page 26: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Plugins are easy to build.

Page 27: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Plugins can do everything

functions.php can do.

Page 28: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

You can change themes without

affecting plugins, or needing functions.php

Page 29: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 30: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

My first plugin experience:

Page 31: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Documentation and tutorials are everywhere.

Page 32: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 33: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Google Results for Building WordPress

Plugins:

Page 34: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 35: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 36: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Good PHP coding standards not so

much.

Page 37: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Most information I found:

• Not object oriented• Bad function names• Required Vendor prefixed• Inconsistent Code Style

Page 38: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

How to Plugin The Right Way

Page 39: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Have some class

Page 40: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 41: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 42: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

This is an application class.

Page 43: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

A few things about this technique

Page 44: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

This is not object oriented (not

really).

Page 45: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

We keep the vendor prefix only on the

class name.

Page 46: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

We put most add_action and

add_filter calls into the constructor.

Page 47: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Now we can do things like this:

Page 48: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 49: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Major PHP Wins:• Object oriented code• Reusable Code• Entering into SOLID programming• DRY Methods. • Code that can be extended• Code that can be inherited• Flexible Coding FTW

Page 50: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Why have class?• Clean fast reusable code• Saves time & money• Happy developers & Happy users

Page 51: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 52: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Javascript Injection

Page 53: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 54: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Use WordPress’ Hook In your Plugin

Class:

Page 55: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 56: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Executing Javascript From Markup

Page 57: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 58: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Form Submissions

Page 59: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Handle Form Actions with a

Plugin

Page 60: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 61: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 62: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Error HandlingGracefully

Page 63: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Let’s play catch

Page 64: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 65: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 66: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Logging?

Page 67: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 68: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Code Comprehension

Page 69: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Method Names That Make Sense

Page 70: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Verb-Based Methods:

Page 71: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Good Method Name:get Leads();

Page 72: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Can we do better?

Page 73: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

leadsid | f_name | l_name1 | roy | rogers2 | robin | peters

recruitersid | f_name | l_name1 | hannah | mckay2 | carol | williams

leads_recruitersid | lead_id | recruiter_id1 | 1 | 12 | 2 | 1

Page 74: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Great Method Name:get Lead ById(1)

get Leads ByRecruiter(1)

get Recruiter ByLead(1)

Page 75: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Other ExamplesfindByRecruiter()

addRecruiterToLead()sortRecruitersByLead()

Page 76: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

If-Statements

Page 77: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

I (proudly) confess…

Page 78: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

I haven’t written anelse statement in 2+

years.

Page 79: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 80: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 81: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

My approach is:Validate FirstReturn EarlyProcess Last

Page 82: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 83: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

2 Levels ofIndentation*

Not counting classes, try & catch, & method body

Page 84: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 85: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 86: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

This will force you to:create more methods (DRY! don’t repeat

yourself)throw more exceptionsdo more error checking

think about code-scenarios less

Page 87: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Nitpicking.

Page 88: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Doc Blocks

Page 89: Good dev citizen
Page 90: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Code Style (WP-CS)

Page 91: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

php code sniffer

https://github.com/squizlabs/PHP_CodeSniffer

Page 92: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Install phpcsphp code-sniffer

Page 93: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

wordpress code sniffer

Page 94: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 95: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 96: Good dev citizen

Fixing PHPCS Errors

Page 97: Good dev citizen
Page 98: Good dev citizen
Page 99: Good dev citizen
Page 100: Good dev citizen
Page 101: Good dev citizen
Page 102: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Page 103: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Take Away Challenges

• No “else” keyword• 2 levels of indentation• No functionality in themes!

Page 104: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Resources

Page 105: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

“Object Oriented Calisthenics”

Page 106: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

PHP The Right Way

Page 107: Good dev citizen

WordCamp Orlando, 2015 - Chad Windnagle

Thank You!Chad Windnagle

Software EngineerAdvanced Medical

@drmmr763

Credits• “Your Code Sucks, Let’s Fix It” - @rdohms /

doh.ms• PHPCS - SquizLabs