Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. /**
  2. * Developer: My name
  3. * Date: 20/07/2016
  4. * Time: 11:08
  5. */
  6.  
  7. namespace AppHttpControllersApi;
  8.  
  9. use IlluminateHttpRequest;
  10. use AppHttpControllersController;
  11. use CarbonCarbon;
  12.  
  13.  
  14.  
  15. class WpBlogController extends Controller
  16. {
  17. /*
  18. * Using Wordpress Rest API
  19. * http://v2.wp-api.org/
  20. *
  21. * Plugins to Install in Wordpress:
  22. * 1. https://wordpress.org/plugins/rest-api/
  23. * 2. https://wordpress.org/plugins/better-rest-api-featured-images/
  24. * 3. https://wordpress.org/plugins/wp-rest-api-pure-taxonomies/
  25. */
  26.  
  27. const WP_USERNAME = "my_wp_username";
  28. const WP_PASSWORD = "my_wp_password";
  29. const WP_API_URL = "http://my.wp-blog.example/wp-json/wp/v2";
  30.  
  31.  
  32. public function getLatestThreePosts(){
  33.  
  34. $curl = $this->getCurl();
  35.  
  36. //set the right url to get only three posts
  37. curl_setopt($curl, CURLOPT_URL, self::WP_API_URL . "/posts?per_page=3&page=1");
  38.  
  39. $raw_posts = curl_exec($curl) or abort(502); //bad gateway error if request fails
  40.  
  41. //decode json and get the stdClass object
  42. $raw_posts = json_decode($raw_posts);
  43.  
  44.  
  45. $formatted_posts = [];
  46.  
  47. foreach($raw_posts as $post){
  48.  
  49. //some operations on the original html to get only the text
  50. $curpost = [
  51. "title" => trim(strip_tags($post->title->rendered)),
  52. "content" => trim(strip_tags($post->content->rendered)),
  53. "url" => $post->link,
  54. "date" => Carbon::parse($post->date)->formatLocalized('%d %B %Y')
  55. ];
  56.  
  57. //abrreviate the post content.
  58. $curpost["abbr"] = substr($curpost["content"],0,200).'...';
  59.  
  60. //get the featured image link if present
  61. if (!is_null($post->better_featured_image)){
  62. $curpost["thumb"] = $post->better_featured_image->media_details->sizes->medium->source_url;
  63. }
  64.  
  65. //get the post categories
  66. $cat_list = [];
  67. foreach($post->pure_taxonomies->categories as $tax_obj){
  68. $cat_list[] = $tax_obj->name;
  69. }
  70.  
  71. $curpost["categories"] = $cat_list;
  72. $formatted_posts[] = $curpost;
  73.  
  74. }
  75.  
  76. return response()->json($formatted_posts);
  77. }
  78.  
  79.  
  80.  
  81. //returns a curl instance with http auth header already set
  82. private function getCurl(){
  83.  
  84. // using http basic auth
  85. $auth_header = ["Authorization: Basic " . base64_encode(self::WP_USERNAME . ":" . self::WP_PASSWORD)];
  86.  
  87. $curl = curl_init();
  88. curl_setopt_array($curl, array(
  89. CURLOPT_RETURNTRANSFER => 1,
  90. CURLOPT_HTTPHEADER => $auth_header
  91. ));
  92.  
  93. return $curl;
  94. }
  95. }
  96.  
  97. //load wp posts
  98. function loadPosts(){
  99. $.ajax({
  100. url: '/api/blog/get-latest-three-posts',
  101. method: 'GET',
  102. dataType: "json",
  103. success: function (posts, txtStatus, xhr) {
  104. $.each(posts, function (index, post) {
  105. //console.log(post);
  106. var post_html = '<div class="post-block">';
  107.  
  108. if (post.hasOwnProperty('thumb')) {
  109. post_html += '<div class="post-thumb">';
  110. post_html += '<img src="' + post.thumb + '" alt="Post Thumb">';
  111. post_html += '</div>';
  112. }
  113.  
  114. post_html += '<div class="post-body">';
  115. post_html += '<div class="post-categories">';
  116. $.each(post.categories, function (index, category) {
  117. post_html += '<span class="subcategory-label">' + category + '</span>';
  118. });
  119. post_html += '</div>';
  120. post_html += '<h3><a href="' + post.url + '"target="_blank">' + post.title + '</a></h3>';
  121. post_html += '<p class="post-date"><small>' + post.date + '</small></p>'
  122. post_html += '<p>' + post.abbr + '</p>';
  123. post_html += '<div class="text-left">'
  124. post_html += '<a href="' + post.url + '" class="btn post-read-all" target="_blank">Leggi tutto</a>'
  125. post_html += '</div>';
  126. post_html += '</div>';
  127. post_html += '</div>';
  128.  
  129. $('#wp-posts').append(post_html);
  130. });
  131. },
  132. error: function (xhr) {
  133. $('#wp-posts').append('<p><strong>Impossibile caricare i post in questo momento</strong></p>');
  134. }
  135.  
  136. });
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement