FacebookFanpageImportFacebookStream

The Facebook Fanpage Import FacebookFanpageImportFacebookStream class.

Defined (1)

The class is defined in the following location(s).

/components/import/import-stream.php  
  1. class FacebookFanpageImportFacebookStream{ 
  2. var $name; 
  3. var $fb; 
  4. var $app_id; 
  5. var $app_secret; 
  6. var $page_id; 
  7. var $update_interval; 
  8. var $errors = array(); 
  9. var $notices = array(); 
  10.  
  11. /** 
  12. * Initializes the Component. 
  13. * @since 1.0.0 
  14. */ 
  15. function __construct() { 
  16. $this->name = get_class( $this ); 
  17.  
  18. $this->page_id = skip\value( 'fbfpi_settings', 'page_id' ); 
  19. $this->stream_language = skip\value( 'fbfpi_settings', 'stream_language' ); 
  20. $this->update_interval = skip\value( 'fbfpi_settings', 'update_interval' ); 
  21. $this->update_num = skip\value( 'fbfpi_settings', 'update_num' ); 
  22. $this->link_target = skip\value( 'fbfpi_settings', 'link_target' ); 
  23.  
  24. if( '' == $this->page_id ) 
  25. $this->errors[] = sprintf( __( '<a href="%s">Fanpage ID have to be provided.</a>', 'fbfpi' ), get_bloginfo( 'wpurl' ) . '/wp-admin/options-general.php?page=ComponentFacebookFanpageImportAdminSettings' ); 
  26.  
  27. if( '' == $this->stream_language ) 
  28. $this->stream_language = 'en_US'; 
  29.  
  30. if( '' == $this->update_interval ) 
  31. $this->update_interval = 'hourly'; 
  32.  
  33. if( '' == $this->update_num ) 
  34. $this->update_num = 10; 
  35.  
  36. // Scheduling import 
  37. if ( !wp_next_scheduled( 'fanpage_import' ) ) 
  38. wp_schedule_event( time(), $this->update_interval, 'fanpage_import' ); 
  39.  
  40. add_action( 'fanpage_import', array( $this, 'import' ) ); 
  41.  
  42. // Importing now! 
  43. if( array_key_exists( 'bfpi-now', $_POST ) && '' != $_POST['bfpi-now'] ) 
  44. add_action( 'init', array( $this, 'import' ), 12 ); // For testing of import 
  45.  
  46. // Adding notices  
  47. add_action( 'admin_notices', array( $this, 'admin_notices' ) ); 
  48.  
  49. } // end constructor 
  50.  
  51.  
  52. /** 
  53. * Functions of the Component 
  54. * @since 1.0.0 
  55. */ 
  56. public function import() { 
  57. global $wpdb; 
  58.  
  59. set_time_limit( 240 ); 
  60.  
  61. $ffbc = new FacebookFanpageConnect( $this->page_id ); 
  62. $page_details = $ffbc->get_page(); 
  63. $entries = $ffbc->get_posts( $this->update_num ); 
  64.  
  65. if( 'status' == skip\value( 'fbfpi_settings', 'insert_post_type' ) ) 
  66. $post_type = 'status-message'; 
  67. else 
  68. $post_type = 'post'; 
  69.  
  70. $post_status = skip\value( 'fbfpi_settings', 'insert_post_status' ); 
  71. if( '' == $post_status ) 
  72. $post_status = 'draft'; 
  73.  
  74. $author_id = skip\value( 'fbfpi_settings', 'insert_user_id' );  
  75.  
  76. $post_format = skip\value( 'fbfpi_settings', 'insert_post_format' ); 
  77. if( '' == $post_format ) 
  78. $post_format = 'none'; 
  79.  
  80. $i = 0; 
  81.  
  82. if( count( $entries ) > 0 ): 
  83. foreach( $entries AS $entry ): 
  84.  
  85. $sql = $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts AS p, $wpdb->postmeta AS m WHERE p.ID = m.post_id AND p.post_type='%s' AND p.post_status = 'publish' AND m.meta_key = 'entry_id' AND m.meta_value = '%s'", $post_type, $entry->id ); 
  86. $post_count = $wpdb->get_var( $sql ); 
  87.  
  88. if ( $post_count > 0 ) // If entry already exists 
  89. continue; 
  90.  
  91. if ( !property_exists( $entry, 'message' ) ) 
  92. continue; 
  93.  
  94. // Get post picture URL (Made here, because needed twice) 
  95. $post_picture = $ffbc->get_post_picture( $entry->id ); 
  96. if( property_exists( $post_picture, 'full_picture' ) ) 
  97. $picture_url = $post_picture->full_picture; 
  98.  
  99. // Post title 
  100. $post_title = ''; 
  101.  
  102. if( property_exists( $entry, 'message' ) && '' != $entry->message ) 
  103. $post_title = $entry->message; 
  104.  
  105. if( property_exists( $entry, 'description' ) &&'' != $entry->description && '' == $post_title ) 
  106. $post_title = $entry->description; 
  107.  
  108. $post_title = $this->set_title( $post_title ); 
  109.  
  110. // Inserting raw post without content 
  111. $post = array( 
  112. 'comment_status'=> 'closed', // 'closed' means no comments. 
  113. 'ping_status' => 'open', // 'closed' means pingbacks or trackbacks turned off 
  114. 'post_date' => date( 'Y-m-d H:i:s', strtotime( $entry->created_time ) ),  
  115. 'post_status' => $post_status, //Set the status of the new post. 
  116. 'post_title' => $post_title, //The title of your post. 
  117. 'post_type' => $post_type, //You may want to insert a regular post, page, link, a menu item or some custom post type 
  118. 'post_excerpt' => $entry->message,  
  119. 'post_author' => $author_id 
  120. ); 
  121. $post_id = wp_insert_post( $post ); 
  122. $post = get_post( $post_id ); 
  123. $attach_id = ''; 
  124.  
  125. // Relink URLs 
  126. $entry->message = $this->set_links( $entry->message ); 
  127.  
  128. // Getting Hashtags 
  129. preg_match_all("/(#\w+)/", $entry->message, $found_hash_tags ); 
  130. $found_hash_tags = $found_hash_tags[1]; 
  131.  
  132. $tags = array(); 
  133. foreach( $found_hash_tags AS $hash_tag ): 
  134. $tags[] = substr( $hash_tag, 1, strlen( $hash_tag ) ); 
  135. endforeach; 
  136.  
  137. if( count( $tags ) > 0 ) 
  138. wp_set_post_tags( $post_id, $tags ); 
  139.  
  140. // Post content 
  141. switch( $entry->type ) { 
  142.  
  143. case 'link': 
  144. if( !empty( $picture_url ) ) 
  145. $attach_id = $this->fetch_picture( $picture_url, $post_id ); 
  146.  
  147. $post->post_content = $this->get_link_content( $entry, $attach_id ); 
  148. break; 
  149.  
  150. case 'photo': 
  151. $picture_url = $ffbc->get_photo_by_object( $entry->object_id ); 
  152.  
  153. if( !empty( $picture_url ) ) 
  154. $attach_id = $this->fetch_picture( $picture_url, $post_id ); 
  155.  
  156. $post->post_content = $this->get_photo_content( $entry, $attach_id ); 
  157.  
  158. if( !empty( $attach_id ) ) 
  159. set_post_thumbnail( $post_id, $attach_id ); 
  160.  
  161. break; 
  162.  
  163. case 'video': 
  164. if( !empty( $entry->picture ) ) 
  165. $attach_id = $this->fetch_picture( $entry->picture, $post_id ); 
  166.  
  167. $post->post_content = $this->get_video_content( $entry, $attach_id ); 
  168.  
  169. if( !empty( $attach_id ) ) 
  170. set_post_thumbnail( $post_id, $attach_id ); 
  171.  
  172.  
  173. break; 
  174. case 'status': 
  175. $post->post_content = $this->set_links( $entry->message ); 
  176.  
  177. if( !empty( $attach_id ) ) 
  178. set_post_thumbnail( $post_id, $attach_id ); 
  179.  
  180.  
  181. break; 
  182. default: 
  183. // skip\p( $entry ); 
  184. break; 
  185. wp_update_post( $post ); 
  186.  
  187. // skip\p($entry); 
  188.  
  189. // Updating post meta 
  190. $ids = explode( '_', $entry->id ); 
  191. $pure_entry_id = $ids[ 1 ]; 
  192. $entry_url = $page_details->link . '/posts/' . $pure_entry_id; 
  193.  
  194. if( property_exists( $entry, 'id' ) ) update_post_meta( $post_id, 'entry_id', $entry->id ); 
  195. if( property_exists( $entry, 'message' ) ) update_post_meta( $post_id, 'message', $entry->message ); 
  196. if( property_exists( $entry, 'description' ) ) update_post_meta( $post_id, 'description', $entry->description ); 
  197. update_post_meta( $post_id, 'image_url', $post_picture ); 
  198. update_post_meta( $post_id, 'fanpage_id', $this->page_id ); 
  199. update_post_meta( $post_id, 'fanpage_name', $page_details->name ); 
  200. update_post_meta( $post_id, 'fanpage_link', $page_details->link ); 
  201. update_post_meta( $post_id, 'entry_url', $entry_url ); 
  202. update_post_meta( $post_id, 'type', $entry->type ); 
  203.  
  204. if( 'none' != $post_format ) 
  205. set_post_format( $post_id, $post_format ); 
  206.  
  207. $i++; 
  208. endforeach; 
  209.  
  210. $this->notices[] = sprintf( __( '%s entries have been imported.', 'fbfpi' ), $i ); 
  211.  
  212. endif; 
  213.  
  214. private function get_link_content( $entry, $attach_id ) { 
  215. $attach_url = wp_get_attachment_url( $attach_id ); 
  216.  
  217. $copyright = '© ' . $entry->caption . ' - ' . $entry->name; 
  218.  
  219. $content = $entry->message; 
  220. $content.= '<div class="fbfpi_link">'; 
  221. if( '' != $attach_url ): 
  222. $content.= '<div class="fbfpi_image">'; 
  223. $content.= '<a href="' . $entry->link . '" target="' . $this->link_target . '" title="' . $copyright . '"><img src="' . $attach_url . '" title="' . $copyright . '"></a>'; 
  224. $content.= '</div>'; 
  225. endif; 
  226. $content.= '<div class="fbfpi_text">'; 
  227. $content.= '<h4><a href="' . $entry->link . '" target="' . $this->link_target . '" title="' . $copyright . '">' . $entry->name . '</a></h4>'; 
  228. $content.= '<p><small>' . $entry->caption . '</small><br /></p>'; 
  229. if( property_exists( $entry, 'description' ) ) $content.= '<p>' . $entry->description . '</p>'; 
  230. $content.= '</div>'; 
  231. $content.= '</div>'; 
  232.  
  233. return $content; 
  234.  
  235. private function get_photo_content( $entry, $attach_id ) { 
  236. $attach_url = wp_get_attachment_url( $attach_id ); 
  237.  
  238. $content = $entry->message; 
  239. $content.= '<div class="fbfpi_photo">'; 
  240. $content.= '<img src="' . $attach_url . '">'; 
  241. $content.= '</div>'; 
  242. return $content; 
  243.  
  244. private function get_video_content( $entry ) { 
  245. $content = $entry->message; 
  246.  
  247. $content.= '<div class="fbfpi_video">'; 
  248. $content.= '[embed]' . $entry->link . '[/embed]'; 
  249. $content.= '<div class="fbfpi_text">'; 
  250. $content.= '<h4><a href="' . $entry->link . '" target="' . $this->link_target . '" title="' . $copyright . '">' . $entry->name . '</a></h4>'; 
  251. if( property_exists( $entry, 'description' ) ) $content.= '<p>' . $entry->description . '</p>'; 
  252. $content.= '</div>'; 
  253. $content.= '</div>'; 
  254.  
  255. return $content; 
  256.  
  257. private function fetch_picture( $picture_url, $post_id ) { 
  258. if ( ini_get( 'allow_url_fopen' ) === TRUE || ini_get( 'allow_url_fopen' ) == 1 ): 
  259. require_once ( ABSPATH . 'wp-admin/includes/image.php' ); 
  260.  
  261. $upload_dir = wp_upload_dir(); 
  262. $md5 = md5( time() ); 
  263. $new_filename = $upload_dir['path'] . '/fbfpi_' . $md5; 
  264. $new_fileurl = $upload_dir['url'] . '/fbfpi_' . $md5; 
  265.  
  266. if ( copy( $picture_url, $new_filename ) ): 
  267. $image_info = getImageSize( $new_filename ); 
  268.  
  269. switch( $image_info['mime'] ) { 
  270. case 'image/gif': 
  271. $extension = 'gif'; 
  272. break; 
  273. case 'image/jpeg': 
  274. $extension = 'jpg'; 
  275. break; 
  276. case 'image/png':  
  277. $extension = 'png'; 
  278. break; 
  279. default: 
  280. $extension = 'jpg'; 
  281. break; 
  282.  
  283. rename( $new_filename, $new_filename . '.' . $extension ); 
  284.  
  285. $new_filename = $new_filename . '.' . $extension; 
  286. $new_fileurl = $new_fileurl . '.' . $extension; 
  287.  
  288. $filetype = wp_check_filetype( basename( $new_filename ), null ); 
  289.  
  290. $attachment = array( 
  291. 'guid' => $new_fileurl,  
  292. 'post_mime_type' => $filetype['type'],  
  293. 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $new_filename ) ),  
  294. 'post_content' => '',  
  295. 'post_status' => 'inherit' 
  296. ); 
  297.  
  298. $attach_id = wp_insert_attachment( $attachment, $new_filename, $post_id ); 
  299. $attach_data = wp_generate_attachment_metadata( $attach_id, $new_filename ); 
  300.  
  301. wp_update_attachment_metadata( $attach_id, $attach_data ); 
  302.  
  303. return $attach_id; 
  304. endif; 
  305. endif;  
  306.  
  307. private function set_title( $string ) { 
  308. $title = explode( ':', $string ); 
  309. $title = $title[ 0 ]; 
  310.  
  311. $title = explode( '!', $title ); 
  312. $title = $title[ 0 ]; 
  313.  
  314. $title = explode( '.', $title ); 
  315. $title = $title[ 0 ]; 
  316.  
  317. $title = str_replace( '+', '', $title ); 
  318.  
  319. $title = trim( $title ); 
  320.  
  321. $desired_width = 50; 
  322.  
  323. if( strlen( $title ) > $desired_width ) 
  324. $title = wordwrap( $title, $desired_width ); 
  325. $i = strpos( $title , "\n"); 
  326. if ($i) { 
  327. $title = substr( $title, 0, $i); 
  328. $title = $title . ' ...';  
  329.  
  330. return $title; 
  331.  
  332. private function set_links( $content ) { 
  333. $content = preg_replace( '@(https?://([-\w.]+[-\w])+(:\d+)?(/([\w-.~:/?#\[\]\@!$&\'()*+, ;=%]*)?)?)@', '<a href="$1" target="_blank">$1</a>', $content ); 
  334. return $content; 
  335.  
  336. public function admin_notices() { 
  337. if( count( $this->errors ) > 0 ): 
  338. foreach( $this->errors AS $error ) 
  339. echo '<div class="updated"><p>' . __( 'Facebook Fanpage Import', 'fbfpi' ) . ': ' . $error . '</p></div>'; 
  340. endif; 
  341.  
  342. if( count( $this->notices ) > 0 ): 
  343. foreach( $this->notices AS $notice ) 
  344. echo '<div class="updated"><p>' . __( 'Facebook Fanpage Import', 'fbfpi' ) . ': ' . $notice . '</p></div>'; 
  345. endif; 
  346. }