| 1 |
<?php |
| 2 |
|
| 3 |
namespace FileBird\Controller\Import\Methods; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
class WPMediaLibraryFolders extends ImportMethod { |
| 8 |
const TABLE = 'mgmlp_folders'; |
| 9 |
|
| 10 |
private $wpmlf_table; |
| 11 |
|
| 12 |
public function __construct() { |
| 13 |
global $wpdb; |
| 14 |
|
| 15 |
$this->wpmlf_table = $wpdb->prefix . self::TABLE; |
| 16 |
} |
| 17 |
|
| 18 |
public function get_counters( $data ) { |
| 19 |
global $wpdb; |
| 20 |
return intval( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_type = %s", 'mgmlp_media_folder' ) ) ); |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
public function get_folders( $data ) { |
| 25 |
$folders = $this->get_wpmlf_folders(); |
| 26 |
|
| 27 |
update_option( self::TMP_OPTION_FOLDER . $data->prefix, $folders, 'no' ); |
| 28 |
|
| 29 |
return new \WP_REST_Response( |
| 30 |
array( |
| 31 |
'result' => true, |
| 32 |
) |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
public function get_wpmlf_folders( $parent = 0 ) { |
| 37 |
global $wpdb; |
| 38 |
|
| 39 |
$table_exist = $wpdb->get_var( $wpdb->prepare( 'show tables like %s', $this->wpmlf_table ) ) == $this->wpmlf_table; |
| 40 |
|
| 41 |
if ( ! $table_exist ) { |
| 42 |
return array(); |
| 43 |
} |
| 44 |
|
| 45 |
$query = $wpdb->prepare( |
| 46 |
"SELECT ID as term_id, post_title as name |
| 47 |
from {$wpdb->prefix}posts |
| 48 |
LEFT JOIN {$this->wpmlf_table} ON( {$wpdb->prefix}posts.ID = {$this->wpmlf_table}.post_id ) |
| 49 |
where post_type = %s and {$this->wpmlf_table}.folder_id = %d |
| 50 |
order by folder_id", |
| 51 |
'mgmlp_media_folder', |
| 52 |
$parent |
| 53 |
); |
| 54 |
|
| 55 |
$folders = $wpdb->get_results( $query, ARRAY_A ); |
| 56 |
|
| 57 |
foreach ( $folders as $key => $folder ) { |
| 58 |
$folders[ $key ]['children'] = $this->get_wpmlf_folders( $folder['term_id'] ); |
| 59 |
} |
| 60 |
|
| 61 |
return $folders; |
| 62 |
} |
| 63 |
|
| 64 |
public function get_attachments( $data ) { |
| 65 |
$folders = get_option( self::TMP_OPTION_FOLDER . $data->prefix, array() ); |
| 66 |
|
| 67 |
$attachments = $this->get_wpmlf_attachments( $folders ); |
| 68 |
|
| 69 |
update_option( self::TMP_OPTION_ATTACHMENT . $data->prefix, $attachments, 'no' ); |
| 70 |
|
| 71 |
return new \WP_REST_Response( |
| 72 |
array( |
| 73 |
'result' => true, |
| 74 |
) |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
public function get_wpmlf_attachments( $folders ) { |
| 79 |
global $wpdb; |
| 80 |
|
| 81 |
$query = "SELECT {$wpdb->prefix}posts.ID FROM {$wpdb->prefix}posts |
| 82 |
LEFT JOIN {$wpdb->prefix}postmeta as pm ON pm.post_id = {$wpdb->prefix}posts.ID |
| 83 |
LEFT JOIN {$wpdb->prefix}mgmlp_folders ON( {$wpdb->prefix}posts.ID = {$wpdb->prefix}mgmlp_folders.post_id ) |
| 84 |
WHERE post_type = 'attachment' |
| 85 |
and pm.meta_key = '_wp_attached_file' |
| 86 |
and folder_id = %d"; |
| 87 |
|
| 88 |
foreach ( $folders as $folder ) { |
| 89 |
$attachments[ $folder['term_id'] ] = $wpdb->get_col( $wpdb->prepare( $query, $folder['term_id'] ) ); |
| 90 |
if ( count( $folder['children'] ) > 0 ) { |
| 91 |
$attachments = $attachments + $this->get_wpmlf_attachments( $folder['children'] ); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
return $attachments; |
| 96 |
} |
| 97 |
} |