| 1 |
<?php |
| 2 |
|
| 3 |
namespace FileBird\Controller\Import\Methods; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
class RealMediaFolderImport extends ImportMethod { |
| 8 |
const TABLE = 'realmedialibrary'; |
| 9 |
const TABLE_RELATIONSHIP = 'realmedialibrary_posts'; |
| 10 |
|
| 11 |
private $real_table; |
| 12 |
private $real_table_posts; |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
global $wpdb; |
| 16 |
|
| 17 |
$this->real_table = $wpdb->prefix . self::TABLE; |
| 18 |
$this->real_table_posts = $wpdb->prefix . self::TABLE_RELATIONSHIP; |
| 19 |
} |
| 20 |
|
| 21 |
public function get_counters( $data ) { |
| 22 |
global $wpdb; |
| 23 |
|
| 24 |
$table_exist = $wpdb->get_var( $wpdb->prepare( 'show tables like %s', $this->real_table ) ) == $this->real_table; |
| 25 |
|
| 26 |
if ( $table_exist ) { |
| 27 |
return intval( $wpdb->get_var( 'SELECT COUNT(id) FROM ' . $this->real_table ) ); |
| 28 |
} |
| 29 |
return 0; |
| 30 |
} |
| 31 |
|
| 32 |
public function get_folders( $data ) { |
| 33 |
$folders = $this->get_rml_folders(); |
| 34 |
|
| 35 |
update_option( self::TMP_OPTION_FOLDER . $data->prefix, $folders, 'no' ); |
| 36 |
|
| 37 |
return new \WP_REST_Response( |
| 38 |
array( |
| 39 |
'result' => true, |
| 40 |
) |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
public function get_rml_folders( $parent_id = -1 ) { |
| 45 |
global $wpdb; |
| 46 |
|
| 47 |
$table_exist = $wpdb->get_var( $wpdb->prepare( 'show tables like %s', $this->real_table ) ) == $this->real_table; |
| 48 |
|
| 49 |
if ( ! $table_exist ) { |
| 50 |
return array(); |
| 51 |
} |
| 52 |
|
| 53 |
$query = $wpdb->prepare( "SELECT id AS term_id, name FROM $this->real_table WHERE parent = %d", $parent_id ); |
| 54 |
|
| 55 |
$folders = $wpdb->get_results( $query, ARRAY_A ); |
| 56 |
|
| 57 |
foreach ( $folders as $key => $folder ) { |
| 58 |
$folders[ $key ]['children'] = $this->get_rml_folders( $folder['term_id'], $this->real_table, false ); |
| 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_rml_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_rml_attachments( $folders ) { |
| 79 |
global $wpdb; |
| 80 |
|
| 81 |
$table_exist = $wpdb->get_var( $wpdb->prepare( 'show tables like %s', $this->real_table_posts ) ) == $this->real_table_posts; |
| 82 |
|
| 83 |
if ( ! $table_exist ) { |
| 84 |
return array(); |
| 85 |
} |
| 86 |
|
| 87 |
$attachments = array(); |
| 88 |
|
| 89 |
$query = "SELECT attachment FROM $this->real_table_posts WHERE fid = %d"; |
| 90 |
|
| 91 |
foreach ( $folders as $folder ) { |
| 92 |
$attachments[ $folder['term_id'] ] = $wpdb->get_col( $wpdb->prepare( $query, $folder['term_id'] ) ); |
| 93 |
|
| 94 |
if ( count( $folder['children'] ) > 0 ) { |
| 95 |
$attachments = $attachments + $this->get_rml_attachments( $folder['children'], false ); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return $attachments; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|