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

WP REST API + React / AngularJSで はじめるWebアプリケーション

WP REST API + React / AngularJSで はじめるWebアプリケーション

WordBench京都&ng-kyoto Meetup 2016/10 スライド

Hidetaka Okamoto

October 09, 2016
Tweet

More Decks by Hidetaka Okamoto

Other Decks in Technology

Transcript

  1. W P R E S T A P I +

    R e a c t / A n g u l a r J S Ͱ ͸ ͡ Ί Δ We b Ξ ϓ Ϧ έ ʔ γ ϣ ϯ Wo rd B e n c h ژ ౎ & n g - k y o t o M e e t u p 2 0 1 6 / 1 0
  2. J a v a S c r i p t

    ֮ ͑Δ ͱ ɺ w e b ੍ ࡞ ͷ Ҿ ͖ ग़ ͠ ͕ ͬ͢ ͝ ͍ ૿ ͑Δ Α
  3. H i d e t a k a O k

    a m o t o • AMIMOTO ( Digitalcube ) • WordBenchژ౎ϞσϨʔλ • ೔ຊޠϑΥʔϥϜϦΤκϯ
  4. Ξ δΣ ϯ μ • WP REST APIͷ͓͞Β͍ • React

    ΍ AngularJS Ͱ͍Ζ͍Ζ΍ͬͯΈΑ͏ • JavaScript + WP REST APIͰ޿͕Δweb੍࡞
  5. Ξ δΣ ϯ μ • WP REST APIͷ͓͞Β͍ • React

    ΍ AngularJS Ͱ͍Ζ͍Ζ΍ͬͯΈΑ͏ • JavaScript + WP REST APIͰ޿͕Δweb੍࡞
  6. W P R E S T A P I (

    Ve r s i o n 2 ) • WordPressͷσʔλΛJSONͰऔಘͰ͖ΔAPI • WordPress4.4ͰҰ෦ػೳ͕ίΞ࣮૷ࡁΈ • ίΞ౷߹ʹ޲͚ͯ։ൃਐߦத • ϓϥάΠϯ΍ίʔυ௥ՃʹΑΔػೳ֦ு΋Մೳ
  7. $ c u r l h t t p :

    / / w p - k y o t o . n e t / w p - j s o n / w p / v 2 / p o s t s | j q .
  8. / w p - j s o n / w

    p / v 2 / : o b j e c t / : i d • ౤ߘͷҰཡΛऔಘ GET /wp-json/wp/v2/ posts • ID͕123ͷݻఆϖʔδΛऔಘ GET /wp-json/wp/v2/ pages/123
  9. G E T w p - j s o n

    / p o s t s ? f i l t e r [ s ] = w o rd p re s s ɹ ɹ ʮ Wo rd P re s s ʯ Λ ؚ Ή ౤ ߘ Λ औ ಘ P O S T w p - j s o n / c o m m e n t s / 1 2 3 - d “ c o n t e n t = c o m m e n t ” ɹ ɹ I D : 1 2 3 ͷ ه ࣄ ʹ ί ϝ ϯ τ Λ ౤ ߘ
  10. W P R E S T A P I υ

    Ω ϡ ϝ ϯ ς ʔ γ ϣ ϯ ʢ ೔ ຊ ޠ ൛ ʣ Λ P R ग़ ͯ͠ ϝ ϯ ς ͠ Α ͏ http://ja.wp-api.org/
  11. Ξ δΣ ϯ μ • WP REST APIͷ͓͞Β͍ • React

    ΍ AngularJS Ͱ͍Ζ͍Ζ΍ͬͯΈΑ͏ • JavaScript + WP REST APIͰ޿͕Δweb੍࡞
  12. W P - A n g u l a r

    J S h t t p s : / / g i t h u b . c o m / m i y a 0 0 0 1 / w p - a n g u l a r j s
  13. W P - A n g u l a r

    J S <have-posts api-root="http://example.com/wp-json/wp/v2" post-type="posts" per-page="5"> <the-post-thumbnail></the-post-thumbnail> <h1 class=“entry-title"> <the-title href=“#/posts/:id"></the-title> </h1> </have-posts>
  14. W P - A n g u l a r

    J S ͳ Β ɺ ς ϯ ϓ Ϩ ʔ τ λ ά ϥ ΠΫ ͳ Ϛʔ Ϋ Ξ οϓ ͕ Մ ೳ <?php if ( have_posts() ) { while ( have_posts() ) { the_post();?> <?php the_post_thumbnail(); ?> <h1 class=“entry-title”><?php the_title(); ?> </h1> <?php } // end while } // end if
  15. W P - A n g u l a r

    J S ͳ Β ɺ ς ϯ ϓ Ϩ ʔ τ λ ά ϥ ΠΫ ͳ Ϛʔ Ϋ Ξ οϓ ͕ Մ ೳ <have-posts api-root="http://example.com/wp-json/wp/v2" post-type="posts" per-page="5"> <the-post-thumbnail></the-post-thumbnail> <h1 class=“entry-title"> <the-title href=“#/posts/:id"></the-title> </h1> </have-posts>
  16. W P - A n g u l a r

    J S ͷ A P I <have-posts> <the-title> <the-content> <the-post-thumbnail> <the-id> <the-excerpt> <the-date>
  17. ϧ ʔϓ ͸ < h a v e - p

    o s t s > ύ ϥϝ ʔ λ σ ϑ Υϧ τ આ ໌ a p i - ro o t A P I ͷ U R L Λ ࢦ ఆ p o s t - t y p e ౤ ߘ λ Π ϓ Λ ࢦ ఆ p e r- p a g e 1 0 औ ಘ ͢ Δ ه ࣄ ݅ ਺ o ff s e t 0 εΩ οϓ ͢ Δ ه ࣄ ݅ ਺ p o s t - i d ه ࣄ I D Λ ࢦ ఆ f i l t e r W P _ Q u e r y ϥ ΠΫ ͳ Ϋ Τ Ϧ
  18. e x a m p l e . c o

    m ͷ p o s t ౤ ߘ λ Π ϓ Ͱɺ Χ ς ΰ Ϧ I D 1 2 3 ͷ ه ࣄ Λ ঢ ॱ Ͱ औ ಘ ͢ Δ <have-posts api-root=“http://example.com" post-type="posts" filter="{ order: 'ASC', cat: 123 }">
  19. e x a m p l e . c o

    m ͷ p o s t ౤ ߘ λ Π ϓ Ͱɺ ه ࣄ I D 1 2 3 ͷ ه ࣄ Λ औ ಘ ͢ Δ <have-posts api-root=“http://example.com" post-type="posts" post-id="123">
  20. W P - A N G U L A R

    J S Ͱ Ϟ όΠϧΞ ϓ Ϧ Λ ࡞ Δ
  21. Wo rd P re s s + W P -

    A P I + I o n i c F r a m e w o r k h t t p s : / / g i t h u b . c o m / m i y a 0 0 0 1 / i o n i c - s t a r t e r- w o rd p re s s
  22. Wo rd P re s s + W P -

    A P I + I o n i c F r a m e w o r k • WP AngularJS • Cordova • ionic
  23. Wo rd P re s s + W P -

    A P I + I o n i c F r a m e w o r k $ npm install -g ionic cordova ios-sim
  24. Wo rd P re s s + W P -

    A P I + I o n i c F r a m e w o r k $ ionic start myApp https://github.com/miya0001/ionic- starter-wordpress $ cd myApp $ ionic platform add ios $ ionic platform add android
  25. $ v i m m y A p p /

    w w w / j s / a p p . j s .constant( 'config', { api: 'http://example.com/wp-json/wp/v2' } )
  26. i o n i c + w p - a

    n g u l a r j s <ion-view title="͓஌Βͤ"><ion-content> <have-posts api-root="{{ apiRoot }}" post-type="posts" offset="0" per-page="5"> <the-post-thumbnail href="#/app/posts/:id" size=“post-thumbnail”> </the-post-thumbnail> <h2 class="entry-title"><the-title href="#/app/posts/:id"></the-title></h1> <the-date format="yyyy/MM/dd"></the-date> </have-posts> </ion-content></ion-view> https://github.com/ogijima-library/app/blob/master/www/templates/posts.html
  27. Wo rd P re s s + W P -

    A P I + I o n i c F r a m e w o r k $ ionic emulate ios or $ ionic serve
  28. R e a c t ν ϡ ʔ τ Ϧ

    Ξϧ Ͱ W P R E S T A P I https://facebook.github.io/react/docs/tutorial.html
  29. C o m m e n t B o x

    ͷ u r l Λ W P ɹ A P I ʹ ReactDOM.render( - <CommentBox url="/api/comments" pollInterval={2000} />, + <CommentBox url="http://api.wp-app.org/wp-json/ wp/v2/comments" pollInterval={2000} />, document.getElementById('content') );
  30. C o m m e n t ͷ M a

    r k d o w n ର Ԡ Λ Φ ϑ ʹ var Comment = React.createClass({ rawMarkup: function() { - var md = new Remarkable(); - var rawMarkup = md.render(this.props.children.toString()); - return { __html: rawMarkup }; + return { __html: this.props.children.toString()}; },
  31. C o m m e n t L i s

    t Λ W P A P I ର Ԡ render: function() { var commentNodes = this.props.data.map(function(comment) { return ( - <Comment author={comment.author} key={comment.id}> - {comment.text} + <Comment author={comment.author_name} key={comment.id}> + {comment.content.rendered} </Comment> ); });
  32. C o m m e n t ౤ ߘ ͸

    ͜ Μ ͳ ײ ͡ Ͱ P O S T ͢ Δ $.ajax({ url: this.props.url, dataType: 'json', type: 'POST', data: { id: POST_ID, author: "AUTHOR_NAME", content: "YOUR COMMENT" },
  33. Ξ δΣ ϯ μ • WP REST APIͷ͓͞Β͍ • React

    ΍ AngularJS Ͱ͍Ζ͍Ζ΍ͬͯΈΑ͏ • JavaScript + WP REST APIͰ޿͕Δweb੍࡞
  34. Wo rd P re s s ʹ न Θ Ε

    ͳ ͍ w e b ੍ ࡞
  35. h t t p s : / / a m

    i m o t o - a m i . c o m / a m i m o t o - n i n j a / Ի੠ೝࣝαʔϏεͱWordPressͷ࿈ܞ
  36. A M I M O T O ε ϙ ϯ

    α ʔϒʔε ແ ਓ Խ ܭ ը
  37. A u d i o C a p t u

    re A u d i o P l a y b a c k Yo u r C o d e W P R E S T A P I W h a t i s Wo rd p re s s ? Wo rd P re s s i s …
  38. Wo rd P re s s a s a M

    i c ro s e r v i c e
  39. ݕ ࡧ ͸ E l a s t i c

    s e a rc h Ϛʔ έ ςΟ ϯ ά ͸ M a u t i c +BWB4DSJQU )5.- "1* "1* "1*
  40. S PA ͷ ί ϯ ς ϯ π ෦ ෼

    Λ Wo rd P re s s Ͱ ؅ ཧ ͢ Δ +BWB4DSJQU )5.- Ϣʔβʔೝূ "1* ίϯςϯπ͸ 81"1*͔Β
  41. ࠓ · Ͱ ͸ ഔ ମ ຖ ʹ ί ϯ

    ς ϯ π Λ ߋ ৽ ͯ͠ ͍ ͨ
  42. ί ϯ ς ϯ π ͸ Wo rd P re

    s s ʹ A P I ͔ Β ί ϯ ς ϯ π Λ ར ༻ ͢ Δ
  43. I n t e l l i g e n

    t C o n t e n t https://www.amazon.co.jp/dp/B01698YCF0
  44. ࠓ · Ͱ ௨ Γ ͷ ࡞ Γ ํ ͕

    ͳ ͘ ͳ Δ Θ ͚ Ͱ ͸ ͳ ͍
  45. Ҋ ݅ ʹ Ԡ ͡ ͯ W P A P

    I ͷ ى ༻ Λ ݕ ౼ ͢ Δ • ϞόΠϧΞϓϦ౳ɺΦϜχνϟωϧʹల։͢ΔϝσΟΞ • JavaScriptͰͷ։ൃ͕ಘҙͳΤϯδχΞ͕ଟ͍νʔϜͰͷ։ൃ • ςʔϚ΍ϓϥάΠϯͷઃఆ͚ͩͰย෇͘Ҋ݅ • WordPressςʔϚ࡞ऀͱ͔͕͍ΔνʔϜͰͷ։ൃ ै དྷ ͷ ߏ ங ํ ๏ ͕ ޮ ཰ త ͳ έ ʔε ͷ ྫ W P R E S T A P I ͕ ׆ ༂ ͢ Δ έ ʔε ͷ ྫ
  46. Ξ δΣ ϯ μ • WP REST APIͷ͓͞Β͍ • React

    ΍ AngularJS Ͱ͍Ζ͍Ζ΍ͬͯΈΑ͏ • JavaScript + WP REST APIͰ޿͕Δweb੍࡞ ੍࡞ɾஶ࡞ Ί૊ ऴ