| 1 |
<?php |
| 2 |
|
| 3 |
namespace FileBird\Controller\Import\Methods; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || exit; |
| 6 |
|
| 7 |
class TermFolderImport extends ImportMethod { |
| 8 |
public function get_counters( $data ) { |
| 9 |
global $wpdb; |
| 10 |
return intval( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(term_taxonomy_id) FROM $wpdb->term_taxonomy WHERE taxonomy = %s", $data->taxonomy ) ) ); |
| 11 |
} |
| 12 |
|
| 13 |
public function get_folders( $data ) { |
| 14 |
$folders = $this->get_term_folders( $data->taxonomy ); |
| 15 |
|
| 16 |
update_option( self::TMP_OPTION_FOLDER . $data->prefix, $folders, 'no' ); |
| 17 |
|
| 18 |
return new \WP_REST_Response( |
| 19 |
array( |
| 20 |
'result' => true, |
| 21 |
) |
| 22 |
); |
| 23 |
} |
| 24 |
|
| 25 |
public function get_term_folders( $taxonomy = '', $parent = 0 ) { |
| 26 |
global $wpdb; |
| 27 |
|
| 28 |
$query = $wpdb->prepare( |
| 29 |
"SELECT term_taxonomy.term_id, terms.name, term_taxonomy.term_taxonomy_id FROM $wpdb->term_taxonomy as `term_taxonomy` |
| 30 |
JOIN $wpdb->terms as `terms` |
| 31 |
ON term_taxonomy.term_taxonomy_id = terms.term_id |
| 32 |
WHERE taxonomy = %s and parent = %d", |
| 33 |
$taxonomy, |
| 34 |
$parent |
| 35 |
); |
| 36 |
|
| 37 |
$folders = $wpdb->get_results( $query, ARRAY_A ); |
| 38 |
|
| 39 |
foreach ( $folders as $key => $folder ) { |
| 40 |
$folders[ $key ]['children'] = $this->get_term_folders( $taxonomy, $folder['term_id'] ); |
| 41 |
} |
| 42 |
|
| 43 |
return $folders; |
| 44 |
} |
| 45 |
|
| 46 |
public function get_attachments( $data ) { |
| 47 |
$folders = get_option( self::TMP_OPTION_FOLDER . $data->prefix, array() ); |
| 48 |
|
| 49 |
$attachments = $this->get_term_attachments( $data->taxonomy, $folders ); |
| 50 |
|
| 51 |
update_option( self::TMP_OPTION_ATTACHMENT . $data->prefix, $attachments, 'no' ); |
| 52 |
|
| 53 |
return new \WP_REST_Response( |
| 54 |
array( |
| 55 |
'result' => true, |
| 56 |
) |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
public function get_term_attachments( $taxonomy, $folders ) { |
| 61 |
global $wpdb; |
| 62 |
|
| 63 |
$attachments = array(); |
| 64 |
|
| 65 |
$query = "SELECT term_relationships.object_id |
| 66 |
FROM $wpdb->term_relationships as `term_relationships` |
| 67 |
JOIN $wpdb->term_taxonomy as `term_taxonomy` |
| 68 |
ON term_relationships.term_taxonomy_id = term_taxonomy.term_taxonomy_id |
| 69 |
WHERE taxonomy = %s and term_id = %d"; |
| 70 |
|
| 71 |
foreach ( $folders as $folder ) { |
| 72 |
$attachments[ $folder['term_id'] ] = $wpdb->get_col( $wpdb->prepare( $query, $taxonomy, $folder['term_id'] ) ); |
| 73 |
|
| 74 |
if ( count( $folder['children'] ) > 0 ) { |
| 75 |
$attachments = $attachments + $this->get_term_attachments( $taxonomy, $folder['children'] ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return $attachments; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|