the struggle is real: developing wordpress plugins · the struggle is real: developing wordpress...

60
The Struggle is Real: Developing WordPress Plugins Daniel Simanek <[email protected]> Research and Graduate Education

Upload: others

Post on 12-Jul-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

The Struggle is Real: Developing WordPress PluginsDaniel Simanek <[email protected]>Research and Graduate Education

Page 2: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Classes

Throwing Things

Page 3: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

No Magic

Filter all the Things

Page 4: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Break the(WordPress) Glass

Code like everyoneIs Stupid

Page 5: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Draft Pending Published

Vanilla WordPress Workflow

Page 6: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Draft Pending Published

Hot Swap Workflow

Draft Pending

Parent

Child

Page 7: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Object Classes

Page 8: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

lunchbox->num_grapes = 4;

Page 9: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

lunchbox->num_grapes = 4;

The programming 101 idea of objects ...

Page 10: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

black_box->get_child();

Page 11: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education
Page 12: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education
Page 13: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education
Page 14: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

??

Page 15: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education
Page 16: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education
Page 17: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education
Page 18: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education
Page 19: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education
Page 20: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Fewer function parameters

Less to remember

Page 21: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Function scoping

plugin_function_prefix_function_name()

plugin_function_prefix_foo()

plugin_function_prefix_bar()plugin_function_prefix_foo_bar()

plugin_function_prefix_example()

plugin_function_prefix_test()plugin_function_prefix_do_something()plugin_function_prefix_my_amazing_function()

Page 22: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Function scoping

$this->function_name()

OR

Class_Name::function_name()

Page 23: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

OrderAnd

Structure

Page 24: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

ReadabilityAnd

Maintainability

Page 25: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education
Page 26: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

PHP Kinda Sucks ...

Page 27: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

PHP Kinda Sucks ...

http://phpsadness.com/

Page 28: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education
Page 29: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Return Value?

Page 30: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

What is FALSE as an INT?

Page 31: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education
Page 32: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Don’t be like PHP ...

Page 33: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

and pass errors in return values ...

Page 34: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Throw Exceptions!

and pass errors in return values ...

Page 35: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

What does PHP do if I don’t check for errors?

Page 36: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

PHP keeps going, no matter what ...

Page 37: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Death by exception

Page 38: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Use Constants

Page 39: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

… and not Magic Numbers

Page 40: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

What are Magic Numbers?

Page 41: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

What are Magic Numbers?

if( $x == 32 ) for( range( 1, 52 ) as $card )

if( $x == ‘supercalifragilisticexpialidocious’ )

Page 42: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Why are Magic Numbers bad?

if( $x == 32 ) for( range( 1, 52 ) as $card )

if( $x == ‘supercalifragilisticexpialidocious’ )

Page 43: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Why are Magic Numbers bad?

if( $x == $freezing_point ) for( range( 1, $deck_size ) as $card )

if( $x == $song_name )

Page 44: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Magic Numbers, WordPress Style

$cat_id = 7;

$user_id = 64;

$post_id = 527;

Page 45: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Two versions of Constants

Global

Class

Page 46: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

BYO Filters

Page 47: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education
Page 48: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

apply_filters( ‘name_of_my_filter’, $value_to_be_filtered );

e.g.

apply_filters( ‘hot_swap/use_cloning’, $cloning_enabled );

Using apply_filters()

Page 49: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

hot_swap/use_cloning

Naming Filters

my_awesome_plugin/options/save

hot_pocket/remove_from_microwave

search_plugin/results

hot_swap/excluded_post_types

Page 50: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Only call apply_filters() once per filter

Page 51: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Break out of WordPress

Page 52: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education
Page 53: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education
Page 54: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education
Page 55: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Code like everyone is stupid

Page 56: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Code like everyone is stupid

Objects/classes

Page 57: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Code like everyone is stupid

Objects/classes

Throwing exceptions

Page 58: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Code like everyone is stupid

Objects/classes

Throwing exceptions

Constants

Page 59: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

Questions?

Page 60: The Struggle is Real: Developing WordPress Plugins · The Struggle is Real: Developing WordPress Plugins Daniel Simanek < daniel.simanek@wisc.edu > Research and Graduate Education

The Struggle is Real: Developing WordPress PluginsDaniel Simanek <[email protected]>Research and Graduate Education