PluginProbe
FileBird – WordPress Media Library Folders & File Manager / 6.3
FileBird – WordPress Media Library Folders & File Manager v6.3
6.5.8 6.5.7 6.5.5 6.5.4 trunk 4.3.1 5.1.6 5.3 5.3.2 5.4.3 5.4.4 5.4.5 5.5 5.5.3 5.5.5 5.5.8 5.5.8.1 5.6 5.6.1 5.6.2 5.6.3 5.6.4 6.2.3 6.2.5 6.3 All 36 releases
filebird / includes / Controller / SyncController.php

SyncController.php in FileBird – WordPress Media Library Folders & File Manager 6.3, at includes/Controller/SyncController.php

158 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FileBird\Controller;
4
5 defined( 'ABSPATH' ) || exit;
6
7 use FileBird\Model\Folder as FolderModel;
8
9 class SyncController {
10 public function exportCSV( \WP_REST_Request $request ) {
11 $id = $request->get_param( 'id' );
12 if( !empty( $id ) ) {
13 $folders = get_option( 'filebird_backup_' . $id, array() );
14 } else {
15 $folders = FolderModel::exportAll();
16 }
17 return new \WP_REST_Response( array( 'folders' => $folders ) );
18 }
19
20 public function readCSV( \WP_REST_Request $request ) {
21 $params = $request->get_file_params();
22 $handle = \fopen( $params['file']['tmp_name'], 'r' );
23 $data = array();
24 $columns = array();
25 if ( false !== $handle ) {
26 $count = 1;
27 while ( 1 ) {
28 $row = fgetcsv( $handle, 0 );
29 if ( 1 === $count ) {
30 $columns = $row;
31 $count++;
32 continue;
33 }
34 if ( false === $row ) {
35 break;
36 }
37 foreach ( $columns as $key => $col ) {
38 $tmp[ $col ] = $row[ $key ];
39 }
40 $data[] = $tmp;
41 }
42 }
43 \fclose( $handle );
44
45 $check = array_diff(
46 $columns,
47 array(
48 'id',
49 'name',
50 'parent',
51 'type',
52 'ord',
53 'created_by',
54 'attachment_ids',
55 )
56 );
57
58 if ( count( $check ) > 0 ) {
59 return new \WP_REST_Response(
60 array(
61 'success' => false,
62 'message' => __( 'The uploaded file was not generated by FileBird. Please check again.', 'filebird' ),
63 )
64 );
65 }
66
67 return $data;
68 }
69
70 public function restoreFolderStructure( $folders ) {
71 $tree = $this->buildTree( $folders );
72 $folders_created = $this->run_import_folders( $tree );
73
74 $mess = sprintf( __( 'Congratulations! We imported successfully %d folders into <strong>FileBird.</strong>', 'filebird' ), count( $folders_created ) );
75
76 return new \WP_REST_Response( array( 'mess' => $mess ) );
77 }
78
79 public function buildTree( array &$data, $parentId = 0 ) {
80 $tree = array();
81 foreach ( $data as &$node ) {
82 if ( $node['parent'] == $parentId ) {
83 $children = $this->buildTree( $data, $node['id'] );
84 if ( $children ) {
85 $node['children'] = $children;
86 }
87 $tree[] = $node;
88 unset( $node );
89 }
90 }
91 return $tree;
92 }
93
94 public function run_import_folders( $folders, $parent = 0 ) {
95 $folders_created = array();
96
97 usort(
98 $folders,
99 function( $folder1, $folder2 ) {
100 return intval( $folder1['ord'] ) > intval( $folder2['ord'] ) ? 1 : -1;
101 }
102 );
103
104 foreach ( $folders as $folder ) {
105 $new_folder = FolderModel::newOrGet( $folder['name'], $parent );
106 $attachment_ids = ! empty( $folder['attachment_ids'] ) ? explode( '|', $folder['attachment_ids'] ) : false;
107 array_push( $folders_created, $folder['id'] );
108
109 if ( $attachment_ids && false !== $new_folder ) {
110 FolderModel::setFoldersForPosts( $attachment_ids, $new_folder['id'] );
111 }
112
113 if ( isset( $folder['children'] ) && count( $folder['children'] ) > 0 ) {
114 $new_child_folders = $this->run_import_folders( $folder['children'], $new_folder['id'] );
115 $folders_created = array_merge( $folders_created, $new_child_folders );
116 }
117 }
118
119 return $folders_created;
120 }
121
122 public function importCSV( \WP_REST_Request $request ) {
123 $data = $this->readCSV( $request );
124 $createdBy = intval( $request->get_param( 'userId' ) );
125
126 if ( $createdBy != '-1' ) {
127 $data = array_filter(
128 $data,
129 function( $item ) use ( $createdBy ) {
130 return $item['created_by'] == $createdBy;
131 }
132 );
133 }
134
135 $result = $this->restoreFolderStructure( $data );
136
137 return new \WP_REST_Response( array( 'success' => $result ) );
138 }
139
140 public function getImportCSVDetail( \WP_REST_Request $request ) {
141 $data = $this->readCSV( $request );
142
143 $users = \get_users(
144 array(
145 'include' => array_unique( array_column( $data, 'created_by' ) ),
146 )
147 );
148
149 $usersReturn = array();
150
151 foreach ( $users as $user ) {
152 $usersReturn[ $user->ID ] = $user->data->display_name . ' ' . __( 'folders', 'filebird' );
153 }
154
155 return new \WP_REST_Response( $usersReturn );
156 }
157 }
158