Dave Jesch- WordCamp LA 2014- Writing Code that Scales

F401a8fa88c08811ff8cde6502e9224c?s=47 SpectrOMTech
September 04, 2014

Dave Jesch- WordCamp LA 2014- Writing Code that Scales

This presentation is for both theme and plugin developers. Dave Jesch at SpectrOMTech.com shares techniques on how to organize code so it can easily adapt to growth and reduce support after product release and/ or delivery. For other related presentations such as PHP Optimization, please visit http://SpectrOMTech.com/presentations/wcla2014/

F401a8fa88c08811ff8cde6502e9224c?s=128

SpectrOMTech

September 04, 2014
Tweet

Transcript

  1. Writing Code That Scales by Dave Jesch LA WordCamp -

    September 6, 2014
  2. © 2014 SpectrOMtech.com. All Rights Reserved. Write a Plugin Popularity

    Unrecognizable Code
  3. © 2014 SpectrOMtech.com. All Rights Reserved. Scalability: Think Big, Focus

    Small.
  4. © 2014 SpectrOMtech.com. All Rights Reserved. There will be code.

    Warning:
  5. © 2014 SpectrOMtech.com. All Rights Reserved. http://SpectrOMTech.com/presentations/wcla2014/ Follow Slides:

  6. © 2014 SpectrOMtech.com. All Rights Reserved. ➔ Web Architecture OOP

    ➔ Everything eCommerce ➔ Custom Plugins Dave Jesch, Lifetime Geek & CTO @ .com Scalability
  7. © 2014 SpectrOMtech.com. All Rights Reserved. Procedural Object Oriented (est.

    1990’s) (est. 1800’s)
  8. © 2014 SpectrOMtech.com. All Rights Reserved. Big Take-Away: Only call

    in the code you need.
  9. © 2014 SpectrOMtech.com. All Rights Reserved. 1. Best Practices 2.

    Code Organization 3. Code Techniques Today’s Goals:
  10. © 2014 SpectrOMtech.com. All Rights Reserved. 1. Best Practices

  11. © 2014 SpectrOMtech.com. All Rights Reserved. Coding Standards • WordPress

    Coding Standards http://codex.wordpress.org/WordPress_Coding_Standards ◦ Indentation ◦ Placement of Braces ◦ Class / Function / Variable Naming
  12. © 2014 SpectrOMtech.com. All Rights Reserved. Data Handling • Validate

    All Input http://codex.wordpress.org/Data_Validation • Escape Output • Escape Data in SQL Queries http://codex.wordpress. org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Atta cks
  13. © 2014 SpectrOMtech.com. All Rights Reserved. 2. Code Organization

  14. © 2014 SpectrOMtech.com. All Rights Reserved. 1. Admin Only 2.

    Front-end Only 3. Common Separate Code into Logical Areas
  15. © 2014 SpectrOMtech.com. All Rights Reserved. Sample Organization • Main

    Plugin File plugin_directory/my_plugin.php • Admin Code plugin_directory/include/admin.php • Front-end code plugin_directory/include/frontend.php • Common Code plugin_directory/include/some_code.php plugin_directory/include/more_code.php • Assets plugin_directory/assets/css/plugin.css plugin_directory/assets/js/plugin.js plugin_directory/assets/images/logo.png
  16. © 2014 SpectrOMtech.com. All Rights Reserved. Minimize Code Usage •

    Test for Front-end vs. Admin page request • Register scripts/styles; don’t enqueue until you need to • Separate out install/uninstall code • Separate out Cron code
  17. © 2014 SpectrOMtech.com. All Rights Reserved. Determining Page Request •

    is_admin() • if (defined('DOING_AJAX') && DOING_AJAX) • if (defined('DOING_CRON') && DOING_CRON) • Understand WordPress’ Initialization Sequence http://codex.wordpress.org/Plugin_API/Action_Reference
  18. © 2014 SpectrOMtech.com. All Rights Reserved. 3. Code Techniques

  19. © 2014 SpectrOMtech.com. All Rights Reserved. Sample Techniques (1 of

    8) In main plugin file: define('PLUGIN_FILE', __FILE__); define('PLUGIN_DIR', plugin_dir_path(PLUGIN_FILE)); define('PLUGIN_INCLUDE', PLUGIN_DIR . 'include/'); define('PLUGIN_ASSETS', PLUGIN_DIR . 'assets/'); define('PLUGIN_ASSETS_URL', plugin_dir_url(PLUGIN_FILE) . 'assets/' define('PLUGIN_VERSION', '1.0');
  20. © 2014 SpectrOMtech.com. All Rights Reserved. Sample Techniques (2 of

    8) Check for Plugin Activation: register_activation_hook(PLUGIN_FILE, 'plugin_activation'); function plugin_activation() { require_once(PLUGIN_INCLUDE . 'activate.php'); }
  21. © 2014 SpectrOMtech.com. All Rights Reserved. Sample Techniques (3 of

    8) Load code appropriate for page request: if (defined(DOING_CRON) && DOING_CRON) require_once(PLUGIN_INCLUDE . 'cron_code.php'); else if (is_admin()) require_once(PLUGIN_INCLUDE . 'admin.php'); else require_once(PLUGIN_INCLUDE . 'frontend.php');
  22. © 2014 SpectrOMtech.com. All Rights Reserved. Sample Techniques (4 of

    8) admin.php: add_action('admin_init', 'plugin_initialize'); function plugin_initialize() { add_menu_page('title', 'menu title', 'Administrator', 'plugin-slug', 'plugin_menu_page'); } function plugin_menu_page() { include(PLUGIN_INCLUDE . 'plugin_page_contents.php'); } include(PLUGIN_INCLUDE . 'some_code.php');
  23. © 2014 SpectrOMtech.com. All Rights Reserved. Sample Techniques (5 of

    8) frontend.php: add_action('wp_enqueue_scripts', 'plugin_register_script') function plugin_register_script() { wp_register_script('plugin_javascript', PLUGIN_ASSETS . 'js/javascript.js', array('jquery'), PLUGIN_VERSION, TRUE); } add_shortcode('sample_shortcode', 'plugin_shortcode') function plugin_shortcode($attr) { wp_enqueue_script('plugin_javascript'); return ('<img src="' . PLUGIN_ASSETS_URL . 'images/plugin_image.png" width="16" height="16" />'); } include(PLUGIN_INCLUDE . 'some_code.php');
  24. © 2014 SpectrOMtech.com. All Rights Reserved. Sample Techniques (6 of

    8) some_code.php: add_action('widget_init', 'plugin_register_widgets') function plugin_register_widgets() { include(PLUGIN_INCLUDE . 'widget.php'); register_widget('PluginWidget'); }
  25. © 2014 SpectrOMtech.com. All Rights Reserved. Sample Techniques (7 of

    8) widget.php: class PluginWidget extends WP_Widget { public function widget() { // display widget contents } public function form() { include(PLUGIN_INCLUDE . 'widget_form.php'); } public function update() { include(PLUGIN_INCLUDE . 'widget_update.php'); } }
  26. © 2014 SpectrOMtech.com. All Rights Reserved. Sample Techniques (8 of

    8) Use Transients When Appropriate: // get data from transient if (false === ($menu = get_transient('main_nav_menu'))) { // nothing in transient, generate menu $args = array(..., 'echo' => 0); $menu = wp_nav_menu($args); // save data from menu in transient set_transient('main_nav_menu', $menu, 86400); } echo $menu;
  27. © 2014 SpectrOMtech.com. All Rights Reserved. Only call in the

    code you need.
  28. © 2014 SpectrOMtech.com. All Rights Reserved. Scalability Achieved! 1. Organize

    Your Code 2. Load Only What’s Needed 3. Use Your Actions 4. Always Learn More
  29. © 2014 SpectrOMtech.com. All Rights Reserved. Connect with us at

    Hello@SpectrOMTech.com Live as if you were to die tomorrow. Learn as if you were to live forever. ~ Mahatma Gandhi Learning more about... OOP in
  30. © 2014 SpectrOMtech.com. All Rights Reserved. More References PHP Optimization

    Presentation OC WordCamp: http://wordpress.tv/2014/06/26/dave-jesch-php-optimization-getting-the-most- out-of-your-php-code/ http://www.slideshare.net/djesch/php-optimization-35545593