| 1 |
<?php |
| 2 |
|
| 3 |
namespace FileBird\Controller\Import\Methods; |
| 4 |
|
| 5 |
use FileBird\Model\Folder as FolderModel; |
| 6 |
|
| 7 |
defined( 'ABSPATH' ) || exit; |
| 8 |
|
| 9 |
abstract class ImportMethod { |
| 10 |
const TMP_OPTION_FOLDER = 'fb_tmp_folders_'; |
| 11 |
const TMP_OPTION_ATTACHMENT = 'fb_tmp_attachments_'; |
| 12 |
|
| 13 |
abstract public function get_counters( $data ); |
| 14 |
|
| 15 |
abstract public function get_folders( $data); |
| 16 |
|
| 17 |
abstract public function get_attachments( $data); |
| 18 |
|
| 19 |
public function run_import_folders( $folders, $attachments, $parent = 0 ) { |
| 20 |
$folders_created = array(); |
| 21 |
|
| 22 |
foreach ( $folders as $folder ) { |
| 23 |
$new_folder = FolderModel::newOrGet( $folder['name'], $parent ); |
| 24 |
|
| 25 |
array_push( $folders_created, $folder['term_id'] ); |
| 26 |
|
| 27 |
if ( isset( $attachments[ $folder['term_id'] ] ) |
| 28 |
&& count( $attachments[ $folder['term_id'] ] ) > 0 |
| 29 |
&& false !== $new_folder ) { |
| 30 |
FolderModel::setFoldersForPosts( $attachments[ $folder['term_id'] ], $new_folder['id'] ); |
| 31 |
} |
| 32 |
|
| 33 |
if ( count( $folder['children'] ) > 0 ) { |
| 34 |
$new_child_folders = $this->run_import_folders( $folder['children'], $attachments, $new_folder['id'] ); |
| 35 |
$folders_created = array_merge( $folders_created, $new_child_folders ); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
return $folders_created; |
| 40 |
} |
| 41 |
|
| 42 |
public function run( $data ) { |
| 43 |
$folders = get_option( self::TMP_OPTION_FOLDER . $data->prefix ); |
| 44 |
$attachments = get_option( self::TMP_OPTION_ATTACHMENT . $data->prefix ); |
| 45 |
|
| 46 |
$folders_created = $this->run_import_folders( $folders, $attachments ); |
| 47 |
|
| 48 |
delete_option( self::TMP_OPTION_FOLDER . $data->prefix ); |
| 49 |
delete_option( self::TMP_OPTION_ATTACHMENT . $data->prefix ); |
| 50 |
|
| 51 |
update_option( 'njt_fb_updated_from_' . $data->prefix, '1' ); |
| 52 |
|
| 53 |
$mess = sprintf( __( 'Congratulations! We imported successfully %d folders into <strong>FileBird.</strong>', 'filebird' ), count( $folders_created ) ); |
| 54 |
|
| 55 |
return new \WP_REST_Response( array( 'mess' => $mess ) ); |
| 56 |
} |
| 57 |
} |
| 58 |
|