/components/import/facebook.php

  1. <?php 
  2. /** 
  3. * Facebook Fanpage Import Component. 
  4. * 
  5. * This class initializes the component. 
  6. * 
  7. * @author mahype, awesome.ug <very@awesome.ug> 
  8. * @package Facebook Fanpage Import 
  9. * @version 1.0.0 
  10. * @since 1.0.0 
  11. * @license GPL 2 
  12.   
  13. Copyright 2015 Awesome UG (very@awesome.ug) 
  14.   
  15. This program is free software; you can redistribute it and/or modify 
  16. it under the terms of the GNU General Public License, version 2, as  
  17. published by the Free Software Foundation. 
  18.   
  19. This program is distributed in the hope that it will be useful,  
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of 
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  22. GNU General Public License for more details. 
  23.   
  24. You should have received a copy of the GNU General Public License 
  25. along with this program; if not, write to the Free Software 
  26. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 
  27.   
  28. */ 
  29.  
  30. if ( !defined( 'ABSPATH' ) ) exit; 
  31.  
  32. class FacebookFanpageConnect{ 
  33. var $access_token; 
  34. var $page_id; 
  35.  
  36. /** 
  37. * Initializes the Component. 
  38. * @since 1.0.0 
  39. */ 
  40. function __construct( $page_id, $access_token = '' ) { 
  41. $this->access_token = '1412978082344911|a7f5722a2b02f24aad0cda61ae5c4fe9'; 
  42.  
  43. if( '' != $access_token ) 
  44. $this->access_token = $access_token; 
  45.  
  46. $this->page_id = $page_id; 
  47.  
  48. function create_access_token( $app_id, $app_secret ) { 
  49. $access_token = $app_id . '|' . $app_secret; 
  50. return $data; 
  51.  
  52. function get_page() { 
  53. $url = 'https://graph.facebook.com/v2.1/'; 
  54. $url.= $this->page_id; 
  55. $url.= '?access_token=' . $this->access_token; 
  56.  
  57. $data = $this->fetch_data( $url ); 
  58. $data = json_decode( $data ); 
  59.  
  60. return $data; 
  61.  
  62. function get_posts( $limit = 10 ) { 
  63. $url = 'https://graph.facebook.com/v2.1/'; 
  64. $url.= $this->page_id . '/'; 
  65. $url.= 'posts/'; 
  66. $url.= '?access_token=' . $this->access_token; 
  67. $url.= '&limit=' . $limit; 
  68.  
  69. $data = $this->fetch_data( $url ); 
  70.  
  71. $data = json_decode( $data ); 
  72.  
  73. return $data->data; 
  74.  
  75. function get_post_picture( $post_id ) { 
  76. $url = 'https://graph.facebook.com/v2.1/'; 
  77. $url.= $post_id; 
  78. $url.= '?access_token=' . $this->access_token; 
  79. $url.= '&fields=full_picture'; 
  80.  
  81. $data = $this->fetch_data( $url ); 
  82. $data = json_decode( $data ); 
  83.  
  84. return $data; 
  85.  
  86. function get_photo_by_object( $object_id ) { 
  87. $url = 'https://graph.facebook.com/v2.1/'; 
  88. $url.= $object_id; 
  89. $url.= '?access_token=' . $this->access_token; 
  90.  
  91. $data = $this->fetch_data( $url ); 
  92. $data = json_decode( $data ); 
  93.  
  94. $data = $data->images[ 0 ]->source; 
  95.  
  96. return $data; 
  97.  
  98. private function fetch_data( $url ) { 
  99. if( is_callable( 'curl_init' ) ): 
  100. $con = curl_init(); 
  101.  
  102. curl_setopt( $con, CURLOPT_URL, $url ); 
  103. curl_setopt( $con, CURLOPT_RETURNTRANSFER, 1 ); 
  104. curl_setopt( $con, CURLOPT_TIMEOUT, 20 ); 
  105. curl_setopt( $con, CURLOPT_SSL_VERIFYPEER, false ); 
  106.  
  107. $data = curl_exec( $con ); 
  108.  
  109. curl_close( $con ); 
  110.  
  111. elseif ( ini_get( 'allow_url_fopen' ) === TRUE || ini_get( 'allow_url_fopen' ) == 1 ): 
  112. $data = @file_get_contents($url); 
  113.  
  114. else: 
  115. if( !class_exists( 'WP_Http' ) ) include_once( ABSPATH . WPINC. '/class-http.php' ); 
  116. $request = new WP_Http; 
  117. $result = $request->request($url); 
  118. $data = $result['body']; 
  119. endif; 
  120.  
  121. return $data; 
  122.  
.