SlideShare ist ein Scribd-Unternehmen logo
1 von 50
Downloaden Sie, um offline zu lesen
TEAMING UP WP API & BACKBONE.JS
WHO KNOWS…
RFC 7230 - 7235
MAYBE THIS ONE…
RFC 2616
FAMILIAR WITH THIS?
HTTP
… AND THIS GUY?
-
RFC 7230 - 7235
=
RFC 2616
HTTPBIS
+
HTTPBIS
=
?
WHO THINKS HE/SHE KNOWS…?
REST
HTTP VERBS 101
CREATE POST SINGLE
RETRIEVE GET REPEATABLE
UPDATE PUT REPEATABLE
DELETE DELETE REPEATABLE
HTTPS://WORDPRESS.ORG/PLUGINS/JSON-REST-API/
WP API
WP-API.ORG
COMPOSER REQUIRE WPACKAGIST/JSON-REST-API
/WP-JSON
{
"name": “A WP Site",
"description": "",
"URL": “https://www.a-wp-site.nl”,
"routes": {
"/": { ... },
"/posts": { ... },
"/posts/<id>": { ... },
...
"/users": { ... },
"/users/<id>": { ... },
"/users/me": { ... },
...
"/media": { ... }
},
"authentication": []
}
/WP-JSON (DETAIL)
{
...
"/posts/<id>": {
"supports": [
"HEAD",
"GET",
"POST",
"PUT",
"PATCH",
"DELETE"
],
"accepts_json": true
},
...
}
/WP-JSON/POSTS
POST /wp-json/posts HTTP/1.1
Host: www.a-wp-site.nl
Authorization: Basic anZitsafake6NjU0MzIx
Content-Type: application/x-www-form-urlencoded
title=test&content_raw=test
/WP-JSON/POSTS
§ POSTS
§ POST META
§ PAGES
§ USERS
§ MEDIA
§ TAXONOMIES
§ COMMENTS
OUT OF THE BOX SUPPORT
QUESTIONS?
THAT’S IT!
PET PROJECT : KOOPHETLOKAAL
YOU KNOW HOW TO CREATE A PLUGIN?
EXTENDING THE API
MY PLUGIN.PHP SETUP
<?php
/*
Plugin Name: MY JSON API
Plugin URI: https://www.a-wp-site.nl/
Description: JSON REST API EXTENSION
Version: 1.0
Author: JRDK
Author URI: http://jrdk.nl
*/
require_once('src/Bootstrap.php');
new Bootstrap();
WHEN TO TRIGGER YOUR CODE
public function __construct()
{
add_action('plugins_loaded', [$this, 'initServer'], 100);
}
public function initServer()
{
if (!defined('JSON_API_VERSION')) {
// return early if WP API does not exist
add_action('all_admin_notices', [$this, 'showError']);
return;
}
}
ADD YOUR OWN CUSTOM POST TYPE
public function initServer()
{
//...
add_action('wp_json_server_before_serve', [$this, 'initEndpoints']);
}
public function initEndpoints($wp_json_server)
{
$ad = new Advertisement($wp_json_server);
}
class Advertisement extends WP_JSON_CustomPostType
{
protected $base = '/advertisements';
protected $type = 'advertisement';
}
/WP-JSON/VERSION/IOS
public function __construct()
{
add_filter('json_endpoints', [$this, 'registerRoutes']);
}
public function registerRoutes($routes)
{
$version_route = [
'/version/(?P<os>[a-z]{3,7})' => [
[
[$this, 'getVersion'],
WP_JSON_Server::READABLE
],
],
];
return array_merge($routes, $version_route);
}
FORGET IT ALL
WP API TEAM RUINED THIS TALK :)
IN A GOOD WAY
JSON != REST
IT COULD BE ANY OUTPUT
V2 REWRITE
CURRENTLY IN BETA
BACK TO THE PLUGIN SETUP
public function initServer()
{
if (!defined('JSON_API_VERSION') || !defined('REST_API_VERSION')) {
// return early if WP API versions do not exist
add_action('all_admin_notices', [$this, 'showError']);
return;
}
add_action('wp_json_server_before_serve', [$this, 'initEndpoints']);
add_filter('rest_url_prefix', [$this, 'changeApiBase']);
}
public function changeApiBase()
{
return 'api';
}
/API
{
"name": “A WP Site",
"description": "",
"URL": “https://www.a-wp-site.nl”,
"routes": {
"/": { ... },
“/wp/v2/posts”: { ... },
“/wp/v2/posts/<id>”: { ... },
...
“/wp/v2/users”: { ... },
“/wp/v2/users/<id>”: { ... },
“/wp/v2/users/me”: { ... },
...
“/wp/v2/media”: { ... }
},
"authentication": []
}
EXPOSE CUSTOM POST TYPE
public function initServer()
{
//...
add_action('init', [$this, 'updateExposure'], 12);
}
public function updateExposure()
{
global $wp_post_types;
$wp_post_types['advertisement']->show_in_rest = true;
$wp_post_types['advertisement']->rest_base = 'advertisement';
}
/API/KHL/VERSION/IOS
register_rest_route('khl', '/version/(?P<os>[a-z]{3,7})', [
'callback' => [$this, 'getVersion'],
'methods' => WP_REST_Server::READABLE,
'args' => [
'context' => [
'default' => 'view',
],
]]
);
APPCELERATOR TITANIUM
VERY GLOBAL OVERVIEW
xml + tss + js ALLOY
JAVASCRIPT
TITANIUM SDK + your own modules
ANDROID IOS WINDOWS
BACKBONE.JS
SAVE POST CREATE
FETCH GET RETRIEVE
SAVE PUT UPDATE
DESTROY DELETE DELETE
THAT’S IT… AGAIN
var post = Backbone.Model.extend({
urlRoot: '/api/wp/v2/posts'
});
var posts = Backbone.Collection.extend({
model: post,
url: '/api/wp/v2/posts'
});
var newPost = new post({
title: ‘test',
content_raw: ‘test'
});
newPost.save();
BACKBONE TITANIUM EXTENSION
exports.definition = {
config : {
// table schema and adapter information
},
extendModel: function(Model) {
_.extend(Model.prototype, {
// Extend, override or implement Backbone.Model
});
return Model;
},
extendCollection: function(Collection) {
_.extend(Collection.prototype, {
// Extend, override or implement Backbone.Collection
});
return Collection;
}
}
CREATE SQLITE STORAGE WITH REST
exports.definition = {
config: {
columns: {
id: 'INTEGER PRIMARY KEY',
title: 'VARCHAR(50)',
image: ‘TEXT',
lastmodified: 'TEXT'
},
URL: 'https://www.a-wp-site.nl/api/khl/advertisements',
adapter: {
type: 'sqlrest',
collection_name: 'advertisements',
idAttribute: 'id'
}
}
//...
};
PARSE WORDPRESS API DATA
exports.definition = {
config: {
//...
parentNode: function(data) {
if (_.isArray(data)) {
var entries = [];
_.each(data, function (_entry, index) {
entries.push({
'id': _entry.ID,
'title': _entry.title,
'image': _entry.featured_image
});
});
return entries;
}
}
}
//...
};
ALLOY VIEW EXAMPLE
<Alloy>
<Collection src="advertisement"/>
<Window class="container">
<ListView id="listing">
<Templates>
<ItemTemplate name="fullItem">
<ImageView bindId="adImage" class="li-image" />
<Label bindId="title" class=“li-title"/>
</ItemTemplate>
</Templates>
<ListSection dataCollection="advertisement">
<ListItem template="fullItem" itemId="{id}"
title:text="{title}" adImage:image="{image}" />
</ListSection>
</ListView>
</Window>
</Alloy>
QUERY DATA FROM WORDPRESS
var advertisements = Alloy.Collections.instance(‘advertisement');
advertisements.fetch({
urlparams: {
filter: {
posts_per_page: 3,
orderby: {'date': 'DESC', 'ID': 'DESC'}
},
page: 1
},
sql: {
orderBy: 'timestamp DESC, id DESC'
},
success: function (col) {
}
});
IF MODIFIED SINCE
GET_LASTPOSTMODIFIED
UPLOADING A FILE
var bgImage = Ti.Filesystem.getFile('upload.jpg');
var xhr = Ti.Network.createHTTPClient();
xhr.timeout = 60000;
xhr.open('POST', '/api/wp/v2/media');
xhr.setRequestHeader('Authorization', '...');
xhr.setRequestHeader('Content-Type', 'image/jpeg');
xhr.setRequestHeader(
'Content-Disposition', 'attachment; filename=upload.jpg'
);
xhr.send(bgImage.read());
MAGIC MINUS
NEGATIVE INTEGERS
THE END RESULT
QUESTIONS?
JEROEN VAN DIJK
@JRVANDIJK
JEROEN@ENRISE.COM
JOIND.IN/13583
Teaming up WordPress API with Backbone.js in Titanium

Weitere ähnliche Inhalte

Ähnlich wie Teaming up WordPress API with Backbone.js in Titanium

Teaming up WordPress API with Backbone.js in Titanium
Teaming up WordPress API with Backbone.js in TitaniumTeaming up WordPress API with Backbone.js in Titanium
Teaming up WordPress API with Backbone.js in TitaniumJeroen van Dijk
 
SDKs, the good the bad the ugly - Japan
SDKs, the good the bad the ugly - JapanSDKs, the good the bad the ugly - Japan
SDKs, the good the bad the ugly - Japantristansokol
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hackingJeroen van Dijk
 
Using WordPress as your application stack
Using WordPress as your application stackUsing WordPress as your application stack
Using WordPress as your application stackPaul Bearne
 
Connect Intergration Patterns: A Case Study - Patrick Streule
Connect Intergration Patterns: A Case Study - Patrick StreuleConnect Intergration Patterns: A Case Study - Patrick Streule
Connect Intergration Patterns: A Case Study - Patrick StreuleAtlassian
 
Specification-Driven Development of REST APIs by Alexander Zinchuk
Specification-Driven Development of REST APIs by Alexander Zinchuk   Specification-Driven Development of REST APIs by Alexander Zinchuk
Specification-Driven Development of REST APIs by Alexander Zinchuk OdessaJS Conf
 
Web::Machine - Simpl{e,y} HTTP
Web::Machine - Simpl{e,y} HTTPWeb::Machine - Simpl{e,y} HTTP
Web::Machine - Simpl{e,y} HTTPMichael Francis
 
The JSON REST API for WordPress
The JSON REST API for WordPressThe JSON REST API for WordPress
The JSON REST API for WordPressTaylor Lovett
 
Best Practices for creating WP REST API by Galkin Nikita
Best Practices for creating WP REST API by Galkin NikitaBest Practices for creating WP REST API by Galkin Nikita
Best Practices for creating WP REST API by Galkin NikitaWordCamp Kyiv
 
Introduction to WordPress REST API
Introduction to WordPress REST APIIntroduction to WordPress REST API
Introduction to WordPress REST APISimone D'Amico
 
WIRED and the WP REST API
WIRED and the WP REST APIWIRED and the WP REST API
WIRED and the WP REST APIkvignos
 
WordPress mit Composer und Git verwalten
WordPress mit Composer und Git verwaltenWordPress mit Composer und Git verwalten
WordPress mit Composer und Git verwaltenWalter Ebert
 
External Data Access with jQuery
External Data Access with jQueryExternal Data Access with jQuery
External Data Access with jQueryDoncho Minkov
 
Big Data Web applications for Interactive Hadoop by ENRICO BERTI at Big Data...
 Big Data Web applications for Interactive Hadoop by ENRICO BERTI at Big Data... Big Data Web applications for Interactive Hadoop by ENRICO BERTI at Big Data...
Big Data Web applications for Interactive Hadoop by ENRICO BERTI at Big Data...Big Data Spain
 
FamilySearch Reference Client
FamilySearch Reference ClientFamilySearch Reference Client
FamilySearch Reference ClientDallan Quass
 
JSON REST API for WordPress
JSON REST API for WordPressJSON REST API for WordPress
JSON REST API for WordPressTaylor Lovett
 

Ähnlich wie Teaming up WordPress API with Backbone.js in Titanium (20)

Teaming up WordPress API with Backbone.js in Titanium
Teaming up WordPress API with Backbone.js in TitaniumTeaming up WordPress API with Backbone.js in Titanium
Teaming up WordPress API with Backbone.js in Titanium
 
SDKs, the good the bad the ugly - Japan
SDKs, the good the bad the ugly - JapanSDKs, the good the bad the ugly - Japan
SDKs, the good the bad the ugly - Japan
 
JSON and the APInauts
JSON and the APInautsJSON and the APInauts
JSON and the APInauts
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hacking
 
Using WordPress as your application stack
Using WordPress as your application stackUsing WordPress as your application stack
Using WordPress as your application stack
 
Connect Intergration Patterns: A Case Study - Patrick Streule
Connect Intergration Patterns: A Case Study - Patrick StreuleConnect Intergration Patterns: A Case Study - Patrick Streule
Connect Intergration Patterns: A Case Study - Patrick Streule
 
Pyrax talk
Pyrax talkPyrax talk
Pyrax talk
 
Specification-Driven Development of REST APIs by Alexander Zinchuk
Specification-Driven Development of REST APIs by Alexander Zinchuk   Specification-Driven Development of REST APIs by Alexander Zinchuk
Specification-Driven Development of REST APIs by Alexander Zinchuk
 
Web::Machine - Simpl{e,y} HTTP
Web::Machine - Simpl{e,y} HTTPWeb::Machine - Simpl{e,y} HTTP
Web::Machine - Simpl{e,y} HTTP
 
The JSON REST API for WordPress
The JSON REST API for WordPressThe JSON REST API for WordPress
The JSON REST API for WordPress
 
Ws rest
Ws restWs rest
Ws rest
 
Best Practices for creating WP REST API by Galkin Nikita
Best Practices for creating WP REST API by Galkin NikitaBest Practices for creating WP REST API by Galkin Nikita
Best Practices for creating WP REST API by Galkin Nikita
 
Introduction to WordPress REST API
Introduction to WordPress REST APIIntroduction to WordPress REST API
Introduction to WordPress REST API
 
WIRED and the WP REST API
WIRED and the WP REST APIWIRED and the WP REST API
WIRED and the WP REST API
 
WordPress mit Composer und Git verwalten
WordPress mit Composer und Git verwaltenWordPress mit Composer und Git verwalten
WordPress mit Composer und Git verwalten
 
External Data Access with jQuery
External Data Access with jQueryExternal Data Access with jQuery
External Data Access with jQuery
 
Big Data Web applications for Interactive Hadoop by ENRICO BERTI at Big Data...
 Big Data Web applications for Interactive Hadoop by ENRICO BERTI at Big Data... Big Data Web applications for Interactive Hadoop by ENRICO BERTI at Big Data...
Big Data Web applications for Interactive Hadoop by ENRICO BERTI at Big Data...
 
FamilySearch Reference Client
FamilySearch Reference ClientFamilySearch Reference Client
FamilySearch Reference Client
 
Consumer-centric API Design
Consumer-centric API DesignConsumer-centric API Design
Consumer-centric API Design
 
JSON REST API for WordPress
JSON REST API for WordPressJSON REST API for WordPress
JSON REST API for WordPress
 

Mehr von Jeroen van Dijk

The Enterprise Wor/d/thy/Press
The Enterprise Wor/d/thy/PressThe Enterprise Wor/d/thy/Press
The Enterprise Wor/d/thy/PressJeroen van Dijk
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hackingJeroen van Dijk
 
The Enterprise Wor/d/thy/Press
The Enterprise Wor/d/thy/PressThe Enterprise Wor/d/thy/Press
The Enterprise Wor/d/thy/PressJeroen van Dijk
 
Beacons in Appcelerator Titanium
Beacons in Appcelerator TitaniumBeacons in Appcelerator Titanium
Beacons in Appcelerator TitaniumJeroen van Dijk
 
An app on the shoulders of giants
An app on the shoulders of giantsAn app on the shoulders of giants
An app on the shoulders of giantsJeroen van Dijk
 
Zend Server: Not just a PHP stack
Zend Server: Not just a PHP stackZend Server: Not just a PHP stack
Zend Server: Not just a PHP stackJeroen van Dijk
 
Refactoring using Codeception
Refactoring using CodeceptionRefactoring using Codeception
Refactoring using CodeceptionJeroen van Dijk
 
Liking Relevance - PHP North East 2014
Liking Relevance - PHP North East 2014Liking Relevance - PHP North East 2014
Liking Relevance - PHP North East 2014Jeroen van Dijk
 
To SQL or No(t)SQL - PHPNW12
To SQL or No(t)SQL - PHPNW12To SQL or No(t)SQL - PHPNW12
To SQL or No(t)SQL - PHPNW12Jeroen van Dijk
 
To SQL or No(t)SQL - PFCongres 2012
To SQL or No(t)SQL - PFCongres 2012To SQL or No(t)SQL - PFCongres 2012
To SQL or No(t)SQL - PFCongres 2012Jeroen van Dijk
 
Socializing a world of travel
Socializing a world of travelSocializing a world of travel
Socializing a world of travelJeroen van Dijk
 
Varnish, the high performance valhalla?
Varnish, the high performance valhalla?Varnish, the high performance valhalla?
Varnish, the high performance valhalla?Jeroen van Dijk
 
Varnish, the high performance valhalla?
Varnish, the high performance valhalla?Varnish, the high performance valhalla?
Varnish, the high performance valhalla?Jeroen van Dijk
 
Edge Side Includes in Zend Framework without Varnish
Edge Side Includes in Zend Framework without VarnishEdge Side Includes in Zend Framework without Varnish
Edge Side Includes in Zend Framework without VarnishJeroen van Dijk
 

Mehr von Jeroen van Dijk (14)

The Enterprise Wor/d/thy/Press
The Enterprise Wor/d/thy/PressThe Enterprise Wor/d/thy/Press
The Enterprise Wor/d/thy/Press
 
WordPress REST API hacking
WordPress REST API hackingWordPress REST API hacking
WordPress REST API hacking
 
The Enterprise Wor/d/thy/Press
The Enterprise Wor/d/thy/PressThe Enterprise Wor/d/thy/Press
The Enterprise Wor/d/thy/Press
 
Beacons in Appcelerator Titanium
Beacons in Appcelerator TitaniumBeacons in Appcelerator Titanium
Beacons in Appcelerator Titanium
 
An app on the shoulders of giants
An app on the shoulders of giantsAn app on the shoulders of giants
An app on the shoulders of giants
 
Zend Server: Not just a PHP stack
Zend Server: Not just a PHP stackZend Server: Not just a PHP stack
Zend Server: Not just a PHP stack
 
Refactoring using Codeception
Refactoring using CodeceptionRefactoring using Codeception
Refactoring using Codeception
 
Liking Relevance - PHP North East 2014
Liking Relevance - PHP North East 2014Liking Relevance - PHP North East 2014
Liking Relevance - PHP North East 2014
 
To SQL or No(t)SQL - PHPNW12
To SQL or No(t)SQL - PHPNW12To SQL or No(t)SQL - PHPNW12
To SQL or No(t)SQL - PHPNW12
 
To SQL or No(t)SQL - PFCongres 2012
To SQL or No(t)SQL - PFCongres 2012To SQL or No(t)SQL - PFCongres 2012
To SQL or No(t)SQL - PFCongres 2012
 
Socializing a world of travel
Socializing a world of travelSocializing a world of travel
Socializing a world of travel
 
Varnish, the high performance valhalla?
Varnish, the high performance valhalla?Varnish, the high performance valhalla?
Varnish, the high performance valhalla?
 
Varnish, the high performance valhalla?
Varnish, the high performance valhalla?Varnish, the high performance valhalla?
Varnish, the high performance valhalla?
 
Edge Side Includes in Zend Framework without Varnish
Edge Side Includes in Zend Framework without VarnishEdge Side Includes in Zend Framework without Varnish
Edge Side Includes in Zend Framework without Varnish
 

Kürzlich hochgeladen

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Kürzlich hochgeladen (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Teaming up WordPress API with Backbone.js in Titanium

  • 1. TEAMING UP WP API & BACKBONE.JS
  • 6. - RFC 7230 - 7235 = RFC 2616 HTTPBIS
  • 8. WHO THINKS HE/SHE KNOWS…? REST
  • 9. HTTP VERBS 101 CREATE POST SINGLE RETRIEVE GET REPEATABLE UPDATE PUT REPEATABLE DELETE DELETE REPEATABLE
  • 10.
  • 12. /WP-JSON { "name": “A WP Site", "description": "", "URL": “https://www.a-wp-site.nl”, "routes": { "/": { ... }, "/posts": { ... }, "/posts/<id>": { ... }, ... "/users": { ... }, "/users/<id>": { ... }, "/users/me": { ... }, ... "/media": { ... } }, "authentication": [] }
  • 13. /WP-JSON (DETAIL) { ... "/posts/<id>": { "supports": [ "HEAD", "GET", "POST", "PUT", "PATCH", "DELETE" ], "accepts_json": true }, ... }
  • 14. /WP-JSON/POSTS POST /wp-json/posts HTTP/1.1 Host: www.a-wp-site.nl Authorization: Basic anZitsafake6NjU0MzIx Content-Type: application/x-www-form-urlencoded title=test&content_raw=test
  • 16. § POSTS § POST META § PAGES § USERS § MEDIA § TAXONOMIES § COMMENTS OUT OF THE BOX SUPPORT
  • 18.
  • 19. PET PROJECT : KOOPHETLOKAAL
  • 20. YOU KNOW HOW TO CREATE A PLUGIN? EXTENDING THE API
  • 21. MY PLUGIN.PHP SETUP <?php /* Plugin Name: MY JSON API Plugin URI: https://www.a-wp-site.nl/ Description: JSON REST API EXTENSION Version: 1.0 Author: JRDK Author URI: http://jrdk.nl */ require_once('src/Bootstrap.php'); new Bootstrap();
  • 22. WHEN TO TRIGGER YOUR CODE public function __construct() { add_action('plugins_loaded', [$this, 'initServer'], 100); } public function initServer() { if (!defined('JSON_API_VERSION')) { // return early if WP API does not exist add_action('all_admin_notices', [$this, 'showError']); return; } }
  • 23. ADD YOUR OWN CUSTOM POST TYPE public function initServer() { //... add_action('wp_json_server_before_serve', [$this, 'initEndpoints']); } public function initEndpoints($wp_json_server) { $ad = new Advertisement($wp_json_server); } class Advertisement extends WP_JSON_CustomPostType { protected $base = '/advertisements'; protected $type = 'advertisement'; }
  • 24. /WP-JSON/VERSION/IOS public function __construct() { add_filter('json_endpoints', [$this, 'registerRoutes']); } public function registerRoutes($routes) { $version_route = [ '/version/(?P<os>[a-z]{3,7})' => [ [ [$this, 'getVersion'], WP_JSON_Server::READABLE ], ], ]; return array_merge($routes, $version_route); }
  • 26. WP API TEAM RUINED THIS TALK :)
  • 27. IN A GOOD WAY
  • 28. JSON != REST IT COULD BE ANY OUTPUT
  • 30. BACK TO THE PLUGIN SETUP public function initServer() { if (!defined('JSON_API_VERSION') || !defined('REST_API_VERSION')) { // return early if WP API versions do not exist add_action('all_admin_notices', [$this, 'showError']); return; } add_action('wp_json_server_before_serve', [$this, 'initEndpoints']); add_filter('rest_url_prefix', [$this, 'changeApiBase']); } public function changeApiBase() { return 'api'; }
  • 31. /API { "name": “A WP Site", "description": "", "URL": “https://www.a-wp-site.nl”, "routes": { "/": { ... }, “/wp/v2/posts”: { ... }, “/wp/v2/posts/<id>”: { ... }, ... “/wp/v2/users”: { ... }, “/wp/v2/users/<id>”: { ... }, “/wp/v2/users/me”: { ... }, ... “/wp/v2/media”: { ... } }, "authentication": [] }
  • 32. EXPOSE CUSTOM POST TYPE public function initServer() { //... add_action('init', [$this, 'updateExposure'], 12); } public function updateExposure() { global $wp_post_types; $wp_post_types['advertisement']->show_in_rest = true; $wp_post_types['advertisement']->rest_base = 'advertisement'; }
  • 33. /API/KHL/VERSION/IOS register_rest_route('khl', '/version/(?P<os>[a-z]{3,7})', [ 'callback' => [$this, 'getVersion'], 'methods' => WP_REST_Server::READABLE, 'args' => [ 'context' => [ 'default' => 'view', ], ]] );
  • 34.
  • 36. VERY GLOBAL OVERVIEW xml + tss + js ALLOY JAVASCRIPT TITANIUM SDK + your own modules ANDROID IOS WINDOWS
  • 38. SAVE POST CREATE FETCH GET RETRIEVE SAVE PUT UPDATE DESTROY DELETE DELETE
  • 39. THAT’S IT… AGAIN var post = Backbone.Model.extend({ urlRoot: '/api/wp/v2/posts' }); var posts = Backbone.Collection.extend({ model: post, url: '/api/wp/v2/posts' }); var newPost = new post({ title: ‘test', content_raw: ‘test' }); newPost.save();
  • 40. BACKBONE TITANIUM EXTENSION exports.definition = { config : { // table schema and adapter information }, extendModel: function(Model) { _.extend(Model.prototype, { // Extend, override or implement Backbone.Model }); return Model; }, extendCollection: function(Collection) { _.extend(Collection.prototype, { // Extend, override or implement Backbone.Collection }); return Collection; } }
  • 41. CREATE SQLITE STORAGE WITH REST exports.definition = { config: { columns: { id: 'INTEGER PRIMARY KEY', title: 'VARCHAR(50)', image: ‘TEXT', lastmodified: 'TEXT' }, URL: 'https://www.a-wp-site.nl/api/khl/advertisements', adapter: { type: 'sqlrest', collection_name: 'advertisements', idAttribute: 'id' } } //... };
  • 42. PARSE WORDPRESS API DATA exports.definition = { config: { //... parentNode: function(data) { if (_.isArray(data)) { var entries = []; _.each(data, function (_entry, index) { entries.push({ 'id': _entry.ID, 'title': _entry.title, 'image': _entry.featured_image }); }); return entries; } } } //... };
  • 43. ALLOY VIEW EXAMPLE <Alloy> <Collection src="advertisement"/> <Window class="container"> <ListView id="listing"> <Templates> <ItemTemplate name="fullItem"> <ImageView bindId="adImage" class="li-image" /> <Label bindId="title" class=“li-title"/> </ItemTemplate> </Templates> <ListSection dataCollection="advertisement"> <ListItem template="fullItem" itemId="{id}" title:text="{title}" adImage:image="{image}" /> </ListSection> </ListView> </Window> </Alloy>
  • 44. QUERY DATA FROM WORDPRESS var advertisements = Alloy.Collections.instance(‘advertisement'); advertisements.fetch({ urlparams: { filter: { posts_per_page: 3, orderby: {'date': 'DESC', 'ID': 'DESC'} }, page: 1 }, sql: { orderBy: 'timestamp DESC, id DESC' }, success: function (col) { } });
  • 46. UPLOADING A FILE var bgImage = Ti.Filesystem.getFile('upload.jpg'); var xhr = Ti.Network.createHTTPClient(); xhr.timeout = 60000; xhr.open('POST', '/api/wp/v2/media'); xhr.setRequestHeader('Authorization', '...'); xhr.setRequestHeader('Content-Type', 'image/jpeg'); xhr.setRequestHeader( 'Content-Disposition', 'attachment; filename=upload.jpg' ); xhr.send(bgImage.read());