34
EXTENDING WORDPRESS AS A PRO Marko Heijnen WordSesh December 2014

Extending WordPress as a pro

Embed Size (px)

Citation preview

Page 1: Extending WordPress as a pro

EXTENDING WORDPRESS AS A PROMarko HeijnenWordSesh December 2014

Page 2: Extending WordPress as a pro

MARKO HEIJNEN

• Founder  of  CodeKitchen  

• Working  for  1&1  

• Lead  developer  of  GlotPress  

• Core  contributor  for  WordPress  

• Plugin  developer  

• Organizer  for  WordCamp  Belgradehttp://2015.belgrade.wordcamp.org/

WORDPRESS DEVELOPER

The Netherlands

Page 3: Extending WordPress as a pro

TODAY’STOPICS

• Current ways to extend WordPress• Expect the unexpectable• Demo wpcentral.io

• Build for the future• Look at new technologies• Demo opensesh.org

• Questions

Page 4: Extending WordPress as a pro

EXTENDING WORDPRESS

Page 5: Extending WordPress as a pro

HOOKS

• Hooks are a way for one piece of code to interact with and modify another piece of code. They make up the foundation for how plugins and themes interact with WordPress Core, but they’re also used extensively by Core itself

• Two types of hooks: Actions and Filters

Page 6: Extending WordPress as a pro

DROP-INS

Single site • advanced-cache.php• object-cache.php

• db.php• db-error.php

• install.php• maintenance.php

Multisite • sunrise.php• blog-deleted.php• blog-inactive.php• blog-suspended.php

Page 7: Extending WordPress as a pro

PLUGGABLE FUNCTIONS

• These functions let you override certain core functions via plugins

• Pluggable functions are no longer being added to WordPress core

• All new functions instead use filters on their output to allow for similar overriding of their functionality

• See http://codex.wordpress.org/Pluggable_Functions

Page 8: Extending WordPress as a pro

PLUGGABLE CLASSES

• Using a filter to replace the class that should be loaded like session_token_manager

• Be able to add your class to be loaded like wp_image_editors filter

Page 9: Extending WordPress as a pro

EXPECT THE UNEXPECTABLECheck out: http://wpcentral.io/

Page 10: Extending WordPress as a pro

WP CENTRALWHAT IS IT?

• Showing download history• Showing version usage history• List all locales and their current state• Showing contributors data (currently API only)

• Collects history of locale progress

Page 11: Extending WordPress as a pro

11

Page 12: Extending WordPress as a pro

12

Page 13: Extending WordPress as a pro

13

Page 14: Extending WordPress as a pro

14

Page 15: Extending WordPress as a pro

WP CENTRALHOW IT WORKS

• A lot of data handling by wp_remote_get• Scrapping profiles.WordPress.org to read data

• Bugs• Storing 0 values• Storing invalid data after failed wp_remote_get

requests• Not checking HTML output from

DOMDocument / DomXPath

Page 16: Extending WordPress as a pro

WP CENTRAL CACHING STRATEGY

• Expensive query result caching• Cron job to retrieve download counts and version

usage• 24 hour cache per locale on request with a max of

5 locales per time• Using WP CLI to run it command line• Creating contributor data on request of the API

Page 17: Extending WordPress as a pro

BUILD FOR THE FUTURE

Page 18: Extending WordPress as a pro

BUILD FOR THE FUTURE

• When writing code, keep the changes you want to make in mind

• Future proofing is making sure you can easily add code/features without breaking existing functionality

• Make the code robust against unexpected problem situations allowing for graceful recovery or fail-fast, but do not write code for possible future uses.

Page 19: Extending WordPress as a pro

LOOK AT NEW TECHNOLOGIEShttp://opensesh.org

Page 20: Extending WordPress as a pro

20

Page 21: Extending WordPress as a pro

NODE.JS NODEJS.ORG

• JavaScript platform• Uses an event-driven, non-blocking I/O model• Lightweight and efficient• Ideal for real time application• Lot’s of modules you can use• Manage with NPM - https://www.npmjs.org

Page 22: Extending WordPress as a pro

OPENSESH

Page 23: Extending WordPress as a pro

OPENSESH.ORG

• A platform for conferences like WordSesh• Community driven project for the open source community• Node.js lets you push new sessions• This with the help of Socket.IO• When the session is over, you can simply replace it• Be able to use YouTube files as wait music

• Possible other ideas:• Create a hangout through WordPress• Have a personal schedule

Page 24: Extending WordPress as a pro

THE SITE

Page 25: Extending WordPress as a pro

MANAGING CHANNELS

Page 26: Extending WordPress as a pro

BREAK TIME

Page 27: Extending WordPress as a pro

CONNECTING TO WORDPRESS

• Node.js checks cookie data to indicate if user is logged in• Validates over JSON API and retrieve more user data• Returns a WP_User object• Has basic capability check support

Page 28: Extending WordPress as a pro

BASIC CONFIGURATION

var config = {}, channels = [], clients = [];

config.port = 9000;config.application_host = ‘http://opensesh.com’;config.application_port = 80;

var app = require('http').createServer(handler), io = require('socket.io').listen(app), wordpress = require("wordpress-utils");

app.listen(config.port);wordpress.set_siteurl( config.application_host );

Page 29: Extending WordPress as a pro

SERVER HANDLER (PART 1)

function handler(req, res) {if (req.url == "/status") {

res.writeHead( 200 );res.end();return;

}

var wp_user = wordpress.connect( req.headers.cookie, req.headers['x-token'] );if ( wp_user ) { // More code to come }

res.end();}

Page 30: Extending WordPress as a pro

SOCKET CONNECTION

io.on('connection', function (socket) {clients[socket.id] = socket;socket.wp_user = wordpress.connect( socket.handshake.headers.cookie, socket.handshake.query.token );

socket.on('disconnect', function() {delete clients[socket.id]

});

var channel_id = parseInt(socket.handshake.query.channel);socket.emit('publish', { channel : channel_id, url :channels[ channel_id ].url });

});

Page 31: Extending WordPress as a pro

SERVER HANDLER (PART 2)

wp_user.on('wp_connected', function ( data ) {if ( ! wp_user.can('manage_options') ) {

res.writeHead(403); res.end(); return;}var fullBody = ‘';

req.on('data', function(chunk) {fullBody += chunk.toString();

});

req.on('end', function() { // Access to JSON data }}

Page 32: Extending WordPress as a pro

SERVER HANDLER (PART 3)

if (req.url == "/publish") {var json = JSON.parse(fullBody);if ( json.channel && json.url ) {

if ( ! channels[ json.channel ] ) {channels[ json.channel ] = {};

}channels[ json.channel ].url = json.url;io.sockets.emit( 'publish', { channel : json.channel, url : json.url } );

}else { res.writeHead(404); }

res.end();}

Page 33: Extending WordPress as a pro

SERVER HANDLER (PART 4)

else if (req.url == "/channels") {res.writeHead( 200, {'Content-Type': ‘application/json’} );res.end( JSON.stringify(channels) );

}

res.end();

Page 34: Extending WordPress as a pro

THANK YOU FOR LISTENING

QUESTIONS?

@markoheijnen - markoheijnen.com