WordCamp Orlando, 2015 - Chad Windnagle
How To Be
A Good Developer
Citizen
WordCamp Orlando, 2015 - Chad Windnagle
Quick Intro
(disclaimer)
WordCamp Orlando, 2015 - Chad Windnagle
Not a Wordpress Guy
WordCamp Orlando, 2015 - Chad Windnagle
Actually a Joomla,
Symfony, Laravel, and
PHP Guy.
WordCamp Orlando, 2015 - Chad Windnagle
8+ Years Working
with Joomla & PHP
WordCamp Orlando, 2015 - Chad Windnagle
Now I work with
Laravel (and
WordPress)
WordCamp Orlando, 2015 - Chad Windnagle
PHP Frameworks
WordCamp Orlando, 2015 - Chad Windnagle
Good* Code
WordCamp Orlando, 2015 - Chad Windnagle
Object Orientation
Programming
WordCamp Orlando, 2015 - Chad Windnagle
Documentation
WordCamp Orlando, 2015 - Chad Windnagle
Testing
WordCamp Orlando, 2015 - Chad Windnagle
These are a few of my
favorite things.
WordCamp Orlando, 2015 - Chad Windnagle
I want to bring Modern
PHP techniques to
WordPress Developers
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
No! Definitely Not.
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
Let’s get started
WordCamp Orlando, 2015 - Chad Windnagle
Stop using themes for
functionality.
WordCamp Orlando, 2015 - Chad Windnagle
Themes are for
Presentation.
WordCamp Orlando, 2015 - Chad Windnagle
Only presentation.
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
WordCamp Orlando, 2015 - Chad Windnagle
If your site will not function
the same with a different
theme, you are doing it
wrong.
WordCamp Orlando, 2015 - Chad Windnagle
“But I need
functions.php!”
WordCamp Orlando, 2015 - Chad Windnagle
No you don’t. You
need a plugin.
WordCamp Orlando, 2015 - Chad Windnagle
Plugins are easy to
build.
WordCamp Orlando, 2015 - Chad Windnagle
Plugins can do
everything functions.php
can do.
WordCamp Orlando, 2015 - Chad Windnagle
You can change themes
without affecting plugins,
or needing functions.php
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
My first plugin
experience:
WordCamp Orlando, 2015 - Chad Windnagle
Documentation and
tutorials are
everywhere.
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
Google Results for
Building WordPress
Plugins:
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
Good PHP coding
standards not so
much.
WordCamp Orlando, 2015 - Chad Windnagle
Most information I
found:
• Not object oriented
• Bad function names
• Required Vendor prefixed
• Inconsistent Code Style
WordCamp Orlando, 2015 - Chad Windnagle
How to Plugin The
Right Way
WordCamp Orlando, 2015 - Chad Windnagle
Have some class
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
This is an application
class.
WordCamp Orlando, 2015 - Chad Windnagle
A few things about this
technique
WordCamp Orlando, 2015 - Chad Windnagle
This is not object
oriented (not really).
WordCamp Orlando, 2015 - Chad Windnagle
We keep the vendor
prefix only on the class
name.
WordCamp Orlando, 2015 - Chad Windnagle
We put most add_action
and add_filter calls into
the constructor.
WordCamp Orlando, 2015 - Chad Windnagle
Now we can do things
like this:
WordCamp Orlando, 2015 - Chad Windnagle
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
WordCamp Orlando, 2015 - Chad Windnagle
Why have class?
• Clean fast reusable code
• Saves time & money
• Happy developers & Happy users
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
Javascript Injection
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
Use WordPress’ Hook
In your Plugin Class:
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
Executing Javascript
From Markup
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
Form Submissions
WordCamp Orlando, 2015 - Chad Windnagle
Handle Form Actions
with a Plugin
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
Error Handling
Gracefully
WordCamp Orlando, 2015 - Chad Windnagle
Let’s play catch
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
Logging?
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
Code Comprehension
WordCamp Orlando, 2015 - Chad Windnagle
Method Names That
Make Sense
WordCamp Orlando, 2015 - Chad Windnagle
Verb-Based Methods:
WordCamp Orlando, 2015 - Chad Windnagle
Good Method Name:
get Leads();
WordCamp Orlando, 2015 - Chad Windnagle
Can we do better?
WordCamp Orlando, 2015 - Chad Windnagle
leads
id | f_name | l_name
1 | roy | rogers
2 | robin | peters
recruiters
id | f_name | l_name
1 | hannah | mckay
2 | carol | williams
leads_recruiters
id | lead_id | recruiter_id
1 | 1 | 1
2 | 2 | 1
WordCamp Orlando, 2015 - Chad Windnagle
Great Method Name:
get Lead ById(1)
get Leads ByRecruiter(1)
get Recruiter ByLead(1)
WordCamp Orlando, 2015 - Chad Windnagle
Other Examples
findByRecruiter()
addRecruiterToLead()
sortRecruitersByLead()
WordCamp Orlando, 2015 - Chad Windnagle
If-Statements
WordCamp Orlando, 2015 - Chad Windnagle
I (proudly) confess…
WordCamp Orlando, 2015 - Chad Windnagle
I haven’t written an
else statement in 2+
years.
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
My approach is:
Validate First
Return Early
Process Last
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
2 Levels of
Indentation*
Not counting classes, try & catch, & method body
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
This will force you to:
create more methods (DRY! don’t repeat
yourself)
throw more exceptions
do more error checking
think about code-scenarios less
WordCamp Orlando, 2015 - Chad Windnagle
Nitpicking.
WordCamp Orlando, 2015 - Chad Windnagle
Doc Blocks
WordCamp Orlando, 2015 - Chad Windnagle
Code Style (WP-CS)
WordCamp Orlando, 2015 - Chad Windnagle
php code sniffer
https://github.com/squizlabs/PHP_CodeSnif
fer
WordCamp Orlando, 2015 - Chad Windnagle
Install phpcs
php code-sniffer
WordCamp Orlando, 2015 - Chad Windnagle
wordpress code sniffer
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
Fixing PHPCS Errors
WordCamp Orlando, 2015 - Chad Windnagle
WordCamp Orlando, 2015 - Chad Windnagle
Take Away Challenges
• No “else” keyword
• 2 levels of indentation
• No functionality in themes!
WordCamp Orlando, 2015 - Chad Windnagle
Resources
WordCamp Orlando, 2015 - Chad Windnagle
“Object Oriented
Calisthenics”
WordCamp Orlando, 2015 - Chad Windnagle
PHP The Right Way
WordCamp Orlando, 2015 - Chad Windnagle
Thank You!
Chad Windnagle
Software Engineer
Advanced Medical
@drmmr763
Credits
• “Your Code Sucks, Let’s Fix It” - @rdohms / doh.ms
• PHPCS - SquizLabs

Good dev citizen

Editor's Notes

  • #2 Hi Everyone Thanks so much for coming. I know there are a lot of other great sessions on the schedule for this slot you could have gone too, so I really appreciate you taking the time to come to this one. I hope when you leave you feel like it was time well spent on your part!
  • #3 I want to give you a quick introduction about myself so you understand a bit where I’m coming from and why I have some of the opinions I do. Also you can consider this a bit of a disclaimer if you don’t like anything in my talk - I have a good excuse!
  • #4 I am not a wordpress guy. In fact I’ve only been working on wordpress sites for about 2 months in a full time, regular capacity.
  • #5 I’m really a joomla, symfony, lavaravel, and general all around php developer.
  • #6 I spent the last 8+ years doing consulting work specializing in Joomla and writing a lot of php.
  • #7 These days I work a lot on with Laravel, and a bit with wordpress. So, my experience has lead me to appreciate a few finer things in life:
  • #8 Symfony and Laravel, decent php frameworks for building custom applications.
  • #9 I love good code, and I realize good is an opinion or interpretation. We’ll get to defining that shortly.
  • #10 I *love* object oriented programming. Once you get away from procedural style coding and learn the power and simplicity of OOP, you’ll fall in love. I know I did!
  • #11 I love good documentation. This is an area that wordpress really excells. There’s tons of great documentation everywhere.
  • #12 I love to test code. Unit testing, code style testing, system tests. I want to be completely sure that my code works the way it is supposed too.
  • #13 These thigns, php frameworks, good quality code, object oriented style programming - these are a few of my most favorite things when it comes to working on code. They really have to be, because when you jump around from project to project, it’s extremely important to be able to quickly pick up the new set of code, and start being productive, or fixing bugs, or whatever else you have to do, right away. Don’t waste time learning having to learn crazy code. So with all those things that I love, and with all the different tools I’ve worked with - why in the world am I at a wordpress conference?
  • #14 I want to bring modern, object oriented style programming to the WordPress community. I want you to see the light! There are some great techniques of modern php practices that will save you time, money, effort, and a few brain cells. Let’s get started.
  • #15 So what does all of that back story mean? do I think I’m some sort of big deal?
  • #16 Do I think you should trust me because I’m a time lord and I’m smarter than all of you? No! Definitely not!
  • #17 I’m a newbie to wordpress. A lot of this is just best practices established by php community experts. So with that!
  • #18 Bro - trust me - it will be awesome!!
  • #19 Okay - Here we go. And I know, I already know, this first topic might be a little controversial to you! I pitched it to a few of my wordpress friends the other day and it took a while for it all to sink in. just relax, hold your breath and try to understand where I’m coming from! We need to stop using themes for functionality.
  • #20 Stop it! that’s not what themes are intended to be doing!
  • #21 Themes are for presentation - they are for styling, they are for javascript enhancements.
  • #24 Now you might say to me “but chad I need functions.php! how else will I do ___”
  • #25 You really don’t. I promise you don’t. and if you do, you might need to ask yourself if you should be doing what you’re trying to do!
  • #26 You really just need to make a plugin! Plugins are almost always the right answer.
  • #42 In this example we have a plugin that is written around the class-based design. we have a constructor executed when we instantiate we have the $this variable which references the instance of the class. This is an application class.
  • #46 Vendor prefix - we can easily change the prefix - renaming because of a collision is much much easier - function names are better.
  • #49 An abstract class could be like a telephone. The developer knows that the most basic features will be to send and receive phone calls. But a more advanced model might be things like voicemail, texting, call waiting, etc.. Rather than rewriting the send and receive calls, we can inherit those features, and add new ones without rewriting the same old features.
  • #50 This is object oriented programming. SOLID = Single responsibility, Open-closed, Liskov substitution, Interface segregation and Dependency inversion. DRY = do not repeat yourself
  • #51 This is object oriented programming.
  • #52 This is object oriented programming.
  • #62 A nonce is a number. used once. In wordpress its actually used more than once, but that’s another topic. The nonce helps wordpress determine if this is a legit form submission. it prevents users from double-entry, protects you from spam, etc… basically you should have this on all your forms.
  • #64 Any clues?
  • #65 change this example to get user and ! user exists throw exception. Types of exceptions before error logs Different types of severity different types of action (log error, throw to user, display, send email notification, etc..)
  • #70 method and variable names that make sense
  • #71 I’ve literally gone on to google and googled synonmons and adjectives and verbs to help me think of good variable and method names.
  • #75 you might need to change this