PluginProbe
FileBird – WordPress Media Library Folders & File Manager / 4.3.1
FileBird – WordPress Media Library Folders & File Manager v4.3.1
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 / Convert.php

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

133 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace FileBird\Controller;
3
4 use FileBird\Config\Fbv;
5
6 defined('ABSPATH') || exit;
7
8 /**
9 * Helps to convert old-data (from filebird old version) to new data
10 */
11
12 class Convert {
13
14 private static $folder_table = 'fbv';
15 private static $relation_table = 'fbv_attachment_folder';
16
17 public function __construct() {
18 }
19
20 public static function insertToNewTable($folders = null) {
21 global $wpdb;
22 if(is_null($folders)) $folders = Convert::getOldFolers();
23 foreach($folders as $k => $folder) {
24 if(is_array($folder)) {
25 $folder = json_decode(json_encode($folder));
26 }
27
28 $parent = $folder->parent;
29 if($parent > 0) {
30 $parent = get_term_meta($parent, 'new_fbv_id', true);
31 }
32
33 $check = self::detail($folder->name, $parent);
34 $insert_id = 0;
35 if(is_null($check)) {
36 $wpdb->insert(self::getTable(self::$folder_table), array(
37 'name' => $folder->name,
38 'parent' => $parent,
39 'type' => 0
40 ));
41 $insert_id = (int)$wpdb->insert_id;
42 } else {
43 $insert_id = (int)$check->id;
44 }
45 //attachments
46 if(isset($folder->attachments)) {
47 foreach($folder->attachments as $k2 => $attachment_id) {
48 $post = get_post($attachment_id);
49 if(is_object($post) && $post->post_type == 'attachment') {
50 self::setFolder($attachment_id, $insert_id, false);
51 }
52 }
53 }
54 //update new_fbv_id for this term
55 update_term_meta($folder->id, 'new_fbv_id', $insert_id);
56
57 }
58 }
59 private static function setFolder($ids, $folder, $delete_first = false) {
60 global $wpdb;
61 if(is_numeric($ids)) {
62 $ids = array($ids);
63 }
64 foreach ($ids as $k => $v) {
65 if($delete_first) {
66 $wpdb->delete(self::getTable(self::$relation_table), array('attachment_id' => $v), array('%d'));
67 }
68 if((int)$folder > 0) {
69 $wpdb->insert(self::getTable(self::$relation_table), array('attachment_id' => (int)$v, 'folder_id' => (int)$folder), array('%d', '%d'));
70 }
71 }
72 }
73 private static function detail($name, $parent) {
74 global $wpdb;
75
76 $query = $wpdb->prepare('SELECT id FROM %1$s WHERE `name` = %2$s AND `parent` = %3$d', self::getTable(self::$folder_table), $name, $parent);
77
78 $user_has_own_folder = get_option('njt_fbv_folder_per_user', '0') === '1';
79 if($user_has_own_folder) {
80 $query .= " AND created_by = " . get_current_user_id();
81 } else {
82 $query .= " AND created_by = 0";
83 }
84
85 $check = $wpdb->get_results($query);
86
87 if($check != null && count($check) > 0) {
88 return $check[0];
89 } else {
90 return null;
91 }
92 }
93 private static function getTable($table) {
94 global $wpdb;
95 return $wpdb->prefix . $table;
96 }
97
98 public static function getOldFolers() {
99 $folders = self::_getOldFolers(0);
100 return $folders;
101 }
102
103 private static function _getOldFolers($parent) {
104 global $wpdb;
105 $folders = array();
106 $query = "SELECT t.term_id as id, t.name FROM $wpdb->terms as t LEFT JOIN $wpdb->term_taxonomy as tt ON (t.term_id = tt.term_id) WHERE parent = $parent and taxonomy = 'nt_wmc_folder'";
107 $folders = $wpdb->get_results($query);
108 foreach($folders as $k => $v) {
109 $folders[$k]->parent = $parent;
110 $folders[$k]->attachments = self::_getAttachments($v->id);
111 }
112 foreach($folders as $k => $v) {
113 $children = self::_getOldFolers($v->id);
114 foreach($children as $k2 => $v2) {
115 $folders[] = $v2;
116 }
117 }
118 return $folders;
119 }
120 private static function _getAttachments($term_id) {
121 global $wpdb;
122 $term_taxonomy_id = $wpdb->get_var("SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = $term_id");
123 $query = "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = " . $term_taxonomy_id;
124
125 $_data = $wpdb->get_results($query);
126 $ids = array();
127 foreach($_data as $k => $v) {
128 $ids[] = $v->object_id;
129 }
130 return $ids;
131 }
132 }
133