Writing Code That Scales
by Dave Jesch
LA WordCamp - September 6, 2014
© 2014 SpectrOMtech.com. All Rights Reserved.
Write a PluginPopularity Unrecognizable Code
© 2014 SpectrOMtech.com. All Rights Reserved.
Scalability: Think Big, Focus Small.
© 2014 SpectrOMtech.com. All Rights Reserved.
There will be code.
Warning:
© 2014 SpectrOMtech.com. All Rights Reserved.
http://SpectrOMTech.com/presentations/wcla2014/
Follow Slides:
© 2014 SpectrOMtech.com. All Rights Reserved.
➔ Web Architecture
OOP
➔ Everything eCommerce
➔ Custom Plugins
Dave Jesch, Lifetime Geek & CTO @ .com
Scalability
© 2014 SpectrOMtech.com. All Rights Reserved.
Procedural Object Oriented (est. 1990’s)(est. 1800’s)
© 2014 SpectrOMtech.com. All Rights Reserved.
Big Take-Away: Only call in the code you need.
© 2014 SpectrOMtech.com. All Rights Reserved.
1. Best Practices 2. Code Organization3. Code Techniques
Today’s Goals:
© 2014 SpectrOMtech.com. All Rights Reserved.
1. Best Practices
© 2014 SpectrOMtech.com. All Rights Reserved.
Coding Standards
● WordPress Coding Standardshttp://codex.wordpress.org/WordPress_Coding_Standards
o Indentation
o Placement of Braces
o Class / Function / Variable Naming
© 2014 SpectrOMtech.com. All Rights Reserved.
Data Handling
● Validate All Inputhttp://codex.wordpress.org/Data_Validation
● Escape Output● Escape Data in SQL Queries
http://codex.wordpress.org/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks
© 2014 SpectrOMtech.com. All Rights Reserved.
2. Code Organization
© 2014 SpectrOMtech.com. All Rights Reserved.
1. Admin Only
2. Front-end Only
3. Common
Separate Code into Logical Areas
© 2014 SpectrOMtech.com. All Rights Reserved.
Sample Organization● Main Plugin File plugin_directory/my_plugin.php
● Admin Codeplugin_directory/include/admin.php
● Front-end code plugin_directory/include/frontend.php
● Common Codeplugin_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
© 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
© 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 Sequencehttp://codex.wordpress.org/Plugin_API/Action_Reference
© 2014 SpectrOMtech.com. All Rights Reserved.
3. Code Techniques
© 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');
© 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');}
© 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');
elserequire_once(PLUGIN_INCLUDE . 'frontend.php');
© 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');
© 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');
© 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');
}
© 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');}
}
© 2014 SpectrOMtech.com. All Rights Reserved.
Sample Techniques (8 of 8)Use Transients When Appropriate:
// get data from transientif (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;
© 2014 SpectrOMtech.com. All Rights Reserved.
Only call in the code you need.
© 2014 SpectrOMtech.com. All Rights Reserved.
Scalability Achieved!
1. Organize Your Code2. Load Only What’s Needed3. Use Your Actions4. Always Learn More
© 2014 SpectrOMtech.com. All Rights Reserved.
Connect with us at [email protected]
Live as if you were to die tomorrow. Learn as if you were to live forever. ~ Mahatma Gandhi
Learning more about...
OOP in
© 2014 SpectrOMtech.com. All Rights Reserved.
More ReferencesPHP 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