/components/import/import-stream.php

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