BPMediaImporter

Description of BPMediaImporter.

Defined (1)

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

/app/importers/BPMediaImporter.php  
  1. class BPMediaImporter { 
  2.  
  3. /** 
  4. */ 
  5. var $active; 
  6. var $import_steps; 
  7.  
  8. function __construct() { 
  9.  
  10.  
  11. static function table_exists( $table ) { 
  12. global $wpdb; 
  13.  
  14. if ( 1 === intval( $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) ) ) { // @codingStandardsIgnoreLine 
  15. return true; 
  16.  
  17. return false; 
  18.  
  19. static function _active( $path ) { 
  20. if ( ! function_exists( 'is_plugin_inactive' ) ) { 
  21. require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); 
  22. if ( is_plugin_active( $path ) ) { 
  23. return 1; 
  24. $plugins = get_plugins(); 
  25. if ( array_key_exists( $path, $plugins ) ) { 
  26. return 0; 
  27. } else { 
  28. return - 1; 
  29.  
  30. static function file_array( $filepath ) { 
  31.  
  32. $path_info = pathinfo( $filepath ); 
  33. $filetype = wp_check_filetype( $filepath ); 
  34. $file['error'] = ''; 
  35. $file['name'] = $path_info['basename']; 
  36. $file['type'] = $filetype['type']; 
  37. $file['tmp_name'] = $filepath; 
  38. $file['size'] = filesize( $filepath ); 
  39.  
  40. return $file; 
  41.  
  42. static function make_copy( $filepath ) { 
  43. $upload_dir = wp_upload_dir(); 
  44. $path_info = pathinfo( $filepath ); 
  45. $tmp_dir = trailingslashit( $upload_dir['basedir'] ) . 'bp-album-importer'; 
  46. $newpath = trailingslashit( $tmp_dir ) . $path_info['basename']; 
  47. if ( ! is_dir( $tmp_dir ) ) { 
  48. wp_mkdir_p( $tmp_dir ); 
  49. if ( file_exists( $filepath ) ) { 
  50. if ( copy( $filepath, $newpath ) ) { 
  51. return BPMediaImporter::file_array( $newpath ); 
  52.  
  53. return 0; 
  54.  
  55. function create_album( $album_name = '', $author_id = 1 ) { 
  56.  
  57. global $bp_media; 
  58.  
  59. if ( array_key_exists( 'bp_album_import_name', $bp_media->options ) ) { 
  60. if ( '' !== $bp_media->options['bp_album_import_name'] ) { 
  61. $album_name = $bp_media->options['bp_album_import_name']; 
  62. $found_album = BuddyPressMedia::get_wall_album(); 
  63.  
  64. if ( count( $found_album ) < 1 ) { 
  65. $album = new BPMediaAlbum(); 
  66. $album->add_album( $album_name, $author_id ); 
  67. $album_id = $album->get_id(); 
  68. } else { 
  69. $album_id = $found_album[0]->ID; 
  70.  
  71. return $album_id; 
  72.  
  73. static function add_media( $album_id, $title = '', $description = '', $filepath = '', $privacy = 0, $author_id = false, $album_name = false ) { 
  74.  
  75. $files = BPMediaImporter::make_copy( $filepath ); 
  76. if ( $files ) { 
  77. $bp_imported_media = new BPMediaHostWordpress(); 
  78. $imported_media_id = $bp_imported_media->insertmedia( $title, $description, $album_id, 0, false, false, $files, $author_id, $album_name ); 
  79.  
  80. wp_update_post( $args = array( 'ID' => $imported_media_id, 'post_author' => $author_id ) ); 
  81.  
  82. $bp_album_privacy = $privacy; 
  83. if ( 10 === intval( $bp_album_privacy ) ) { 
  84. $bp_album_privacy = 6; 
  85.  
  86. $privacy = new BPMediaPrivacy(); 
  87. $privacy->save( $bp_album_privacy, $imported_media_id ); 
  88.  
  89. return $imported_media_id; 
  90.  
  91. return 0; 
  92.  
  93. static function cleanup( $table, $directory ) { 
  94. global $wpdb; 
  95. $wpdb->query( "DROP TABLE IF EXISTS $table" ); // @codingStandardsIgnoreLine 
  96. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->base_prefix}bp_activity WHERE component = %s", 'album' ) ); 
  97. if ( is_dir( $directory ) ) { 
  98. BPMediaImporter::delete( $directory ); 
  99.  
  100. static function delete( $path ) { 
  101. if ( true === is_dir( $path ) ) { 
  102. $files = array_diff( scandir( $path ), array( '.', '..' ) ); 
  103.  
  104. foreach ( $files as $file ) { 
  105. BPMediaImporter::delete( realpath( $path ) . '/' . $file ); 
  106.  
  107. return rmdir( $path ); 
  108. } else { 
  109. if ( true === is_file( $path ) ) { 
  110. return unlink( $path ); 
  111.  
  112. return false;