Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dave Jesch- WordCamp San Diego 2015- Object Oriented Programming and WordPress

Dave Jesch- WordCamp San Diego 2015- Object Oriented Programming and WordPress

Scalability and stability is a MUST in today's plugin development world. A solid architecture promotes healthy expandable code. Objected Oriented Programming can take your code into new realms of possibilities.

SpectrOMTech

March 28, 2015
Tweet

More Decks by SpectrOMTech

Other Decks in Programming

Transcript

  1. by Dave Jesch San Diego WordCamp - March 28, 2015

    OOP made Easy! Objected Oriented Programming and WordPress
  2. © 2015 SpectrOMtech.com. All Rights Reserved. Party Time?

  3. © 2015 SpectrOMtech.com. All Rights Reserved. Code Organization is EVERYTHING

  4. © 2015 SpectrOMtech.com. All Rights Reserved. WARNING: There will be

    code!
  5. © 2015 SpectrOMtech.com. All Rights Reserved. Follow Slides at: http://SpectrOMtech.com/presentations/wcsd2015/

  6. © 2015 SpectrOMtech.com. All Rights Reserved. Dave Jesch, Lifetime Geek

    & CTO @ • Custom Plugins • Systems Integration • Everything eCommerce .com OOP Scalability
  7. © 2015 SpectrOMtech.com. All Rights Reserved. Procedural vs. Object Oriented

  8. © 2015 SpectrOMtech.com. All Rights Reserved. Why use Object Oriented

    Code?
  9. © 2015 SpectrOMtech.com. All Rights Reserved. OOP is like baking

    a cake
  10. © 2015 SpectrOMtech.com. All Rights Reserved. What exactly is a

    Class?
  11. © 2015 SpectrOMtech.com. All Rights Reserved. Class Design Single Responsibility

    Principle
  12. © 2015 SpectrOMtech.com. All Rights Reserved. Sample Class class ChocolateCake

    { public $ingredients = array( 'flour' => 1, 'egg' => 2, 'milk' => 2.5, 'cocoa' => 3 ); public $temperature = 350; public function __construct() // creates instance of object { $this->preheat_oven(); } ... }
  13. © 2015 SpectrOMtech.com. All Rights Reserved. Constructors • Initializes the

    instance of the class • Can call any function public function __construct() { // initialize your object parent::__construct(); }
  14. © 2015 SpectrOMtech.com. All Rights Reserved. Instantiation • Creating a

    new instance of a class $cake = new ChocolateCake(); $cake->add_ingredients('flour', 'spices') $cake->add_ingredients('milk', 'egg'); $cake->bake(); $cake->add_frosting(); $cake->eat();
  15. © 2015 SpectrOMtech.com. All Rights Reserved. class MyPlugin { public

    function __construct() { add_action('init', array(&$this, 'init')); if (is_admin()) add_action('admin_init', array(&$this, 'admin_init')); else add_action('wp_enqueue_scripts', array(&$this, 'enqueue_scripts')); add_filter('the_content', array(&$this, 'content_filter'), 10, 1); register_activation_hook(__FILE__, array(&$this, 'activation_hook'); } public function init() { ... } public function admin_init() { ... } public function enqueue_scripts() { ... } public function content_filter($content) { ...; return ($content); } public function activation_hook() { ... } } new MyPlugin(); Sample Object Oriented Plugin
  16. © 2015 SpectrOMtech.com. All Rights Reserved. Quickest way to OOP

  17. © 2015 SpectrOMtech.com. All Rights Reserved. Action Items: 1. Organize.

    Organize. Organize. 2. Practice. Spend more time on Foundation. 3. Advance. Continue working on bigger projects
  18. © 2015 SpectrOMtech.com. All Rights Reserved. Bring Me Cake!

  19. © 2015 SpectrOMtech.com. All Rights Reserved. Q&A or Examples?

  20. © 2015 SpectrOMtech.com. All Rights Reserved. Organized Foundation is Key!

  21. © 2015 SpectrOMtech.com. All Rights Reserved. MetaBox- Procedural Example add_action(

    'admin_init', 'boj_mbe_image_create'); function boj_mbe_image_create() { add_meta_box( 'boj-image-meta', 'Set Image', 'boj_mbe_image_function' 'post', 'normal', 'high'); } function boj_mbe_image_function($post) { // output metabox contents } add_action( 'admin_print_scripts-post.php ', 'boj_mbe_image_admin_scripts'); function boj_mbe_image_admin_scripts() { ... } // add scripts - styles too add_action( 'save_post', 'boj_mbe_image_save_meta'); function boj_mbe_save_meta($post_id) { ... }
  22. © 2015 SpectrOMtech.com. All Rights Reserved. MetaBox- Object Oriented Example

    class OOMetaBoxPlugin { private function __construct() { add_action('init', array(&$this, 'init')); } // Create the metabox, setting the CSS class, title, etc. public function init() { // create the metabox instance new DemoMetaBox(); } } new OOMetaBoxPluin();
  23. © 2015 SpectrOMtech.com. All Rights Reserved. MetaBox- Object Oriented Example

    class DemoMetaBox extends SpectrOMMetaBox { // Create the metabox, setting the CSS class, title, etc. public function __construct() { parent::__construct( 'oo-image-meta', // CSS class for metabox __('Set Image', 'oometaboxdemo'), // metabox title array(&$this, 'output_metabox') // metabox callback // remaining parameters are defaults ); } // output metabox contents public function output_metabox($post) { ... } }
  24. © 2015 SpectrOMtech.com. All Rights Reserved. Using WP_Query $args =

    array( 'post_type' => 'testimonials', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => 5, // 5 per page 'paged' => 2, // show page 2 ); $query = new WP_Query($args);
  25. © 2015 SpectrOMtech.com. All Rights Reserved. Reusable WP_Query Base Class

    class SpectrOMQueryTestimonials extends SpectrOMQueryModel { public function __construct() { parent::__construct('testimonials'); // optionally customize this model $this->set_posts_per_page(5) ->no_found_rows(); } }
  26. © 2015 SpectrOMtech.com. All Rights Reserved. Example: Using SpectrOM Query

    Model $testimonials = new SpectrOMQueryTestimonials(); $testimonials->set_posts_per_page(3) // optionally change # per page ->add_pagination() // optionally add paged for archive // ->set_page(2) ->order_by('title', 'ASC'); $query = $testimonials->get_query(); // now do a standard "Loop" while ($query->have_posts()) { $query->the_post(); // continue as normal }
  27. © 2015 SpectrOMtech.com. All Rights Reserved. Example: Settings API- Procedural

    register_setting('group', 'option', 'validation_callback'); add_settings_section('section_id', 'title', 'section_callback', 'page'); add_settings_field('field_id1', 'field_title', 'field_callback1', 'page', 'section_id', $args); add_settings_field('field_id2', 'field_title', 'field_callback2', 'page', 'section_id', $args); add_settings_field('field_id3', 'field_title', 'field_callback3', 'page', 'section_id', $args); function field_callback($args) { ... } function validation_callback($input) { ... }
  28. © 2015 SpectrOMtech.com. All Rights Reserved. Settings API- Object Oriented

    $args = array( 'page' => 'slug_page', 'group' => 'slug_group', 'option' => 'slug_options', 'sections' => array( 'slug_settings' => array( // settings section name 'title' => __('My Plugin Settings', 'slug'), 'description' => __('Settings Description', 'slug'), 'fields' => array( 'text_setting' => array( // field name 'id' => 'text_setting', // required 'title' => __('Enter Text Here', 'slug'), 'type' => 'text', 'value' => $default))))); $settings = new SpectrOMSettings($args);
  29. © 2015 SpectrOMtech.com. All Rights Reserved. Settings API- Object Oriented

    $settings = new SpectrOMSettings($args); add_options_page('My Settings', 'My Settings', // page title / menu title 'manage_options', // capability $settings->get_page(), // menu slug 'settings_page_callback')); // callback function function settings_page_callback() { global $settings; echo '<div class="wrap">'; echo '<h2>', $settings->get_header(), '</h2>'; echo '<form action="options.php" method="post">'; // use the settings object to output sections and fields $settings->settings_fields(); $settings->settings_sections(); submit_button(); echo '</form>'; echo '</div>'; }
  30. © 2015 SpectrOMtech.com. All Rights Reserved. Code Samples Slides: Object

    Oriented Programming and WordPress http://SpectrOMtech.com/presentations/wcsd2015/ Object Oriented Plugin Boilerplate (includes Settings API example): https://github.com/davejesch/ooplugin Object Oriented WordPress Library: https://github.com/davejesch/oowp Object Oriented MetaBox Example: https://github.com/davejesch/oometabox @davejesch