PluginProbe
FileBird – WordPress Media Library Folders & File Manager / 6.3.4
FileBird – WordPress Media Library Folders & File Manager v6.3.4
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 6.3.4, at includes/Controller/Convert.php

150 lines 4.5 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\Classes\Helpers;
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 private static $folder_table = 'fbv';
14 private static $relation_table = 'fbv_attachment_folder';
15 public function __construct() {
16 }
17
18 public static function insertToNewTable( $folders = null ) {
19 global $wpdb;
20 if ( is_null( $folders ) ) {
21 $folders = self::getOldFolders();
22 }
23 foreach ( $folders as $k => $folder ) {
24 if ( is_array( $folder ) ) {
25 $folder = json_decode( wp_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 $check = self::detail( $folder->name, $parent, $folder->created_by );
33 $insert_id = 0;
34 if ( is_null( $check ) ) {
35 $wpdb->insert(
36 self::getTable( self::$folder_table ),
37 array(
38 'name' => Helpers::sanitize_for_excel( $folder->name ),
39 'parent' => $parent,
40 'created_by' => $folder->created_by,
41 'type' => 0,
42 )
43 );
44 $insert_id = (int) $wpdb->insert_id;
45 } else {
46 $insert_id = (int) $check->id;
47 }
48 //attachments
49 if ( isset( $folder->attachments ) ) {
50 foreach ( $folder->attachments as $k2 => $attachment_id ) {
51 $post = get_post( $attachment_id );
52 if ( is_object( $post ) && $post->post_type == 'attachment' ) {
53 self::setFolder( $attachment_id, $insert_id, false );
54 }
55 }
56 }
57 //update new_fbv_id for this term
58 update_term_meta( $folder->id, 'new_fbv_id', $insert_id );
59 }
60 }
61
62 private static function setFolder( $ids, $folder, $delete_first = false ) {
63 global $wpdb;
64 if ( is_numeric( $ids ) ) {
65 $ids = array( $ids );
66 }
67 foreach ( $ids as $k => $v ) {
68 if ( $delete_first ) {
69 $wpdb->delete( self::getTable( self::$relation_table ), array( 'attachment_id' => $v ), array( '%d' ) );
70 }
71 if ( (int) $folder > 0 ) {
72 $wpdb->insert(
73 self::getTable( self::$relation_table ),
74 array(
75 'attachment_id' => (int) $v,
76 'folder_id' => (int) $folder,
77 ),
78 array( '%d', '%d' )
79 );
80 }
81 }
82 }
83 private static function detail( $name, $parent, $created_by = null ) {
84 global $wpdb;
85
86 if ( ! is_null( $created_by ) ) {
87 $created = (int) $created_by;
88 } else {
89 $user_has_own_folder = get_option( 'njt_fbv_folder_per_user', '0' ) === '1';
90 if ( $user_has_own_folder ) {
91 $created = get_current_user_id();
92 } else {
93 $created = 0;
94 }
95 }
96 $check = $wpdb->get_results( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}fbv WHERE `name` = %s AND `parent` = %d AND `created_by` = %d", $name, $parent, $created ) );
97
98 if ( $check != null && count( $check ) > 0 ) {
99 return $check[0];
100 } else {
101 return null;
102 }
103 }
104 private static function getTable( $table ) {
105 global $wpdb;
106 return $wpdb->prefix . $table;
107 }
108
109 public static function getOldFolders() {
110 $folders = self::_getOldFolders( 0 );
111 return $folders;
112 }
113
114 public static function countOldFolders() {
115 global $wpdb;
116
117 $oldFolders = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(t.term_id) FROM $wpdb->terms as t LEFT JOIN $wpdb->term_taxonomy as tt ON(t.term_id = tt.term_id) WHERE taxonomy = %s", 'nt_wmc_folder' ) );
118
119 return intval( $oldFolders );
120 }
121
122 private static function _getOldFolders( $parent ) {
123 global $wpdb;
124 $folders = array();
125 $folders = $wpdb->get_results( $wpdb->prepare( "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 = %d and taxonomy = 'nt_wmc_folder'", $parent ) );
126 foreach ( $folders as $k => $v ) {
127 $folders[ $k ]->parent = $parent;
128 $folders[ $k ]->created_by = (int) $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$wpdb->termmeta} WHERE meta_key = 'fb_created_by' AND term_id = %d", $v->id ) );
129 $folders[ $k ]->attachments = self::_getAttachments( $v->id );
130 }
131 foreach ( $folders as $k => $v ) {
132 $children = self::_getOldFolders( $v->id );
133 foreach ( $children as $k2 => $v2 ) {
134 $folders[] = $v2;
135 }
136 }
137 return $folders;
138 }
139 private static function _getAttachments( $term_id ) {
140 global $wpdb;
141 $term_taxonomy_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) );
142
143 $_data = $wpdb->get_results( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term_taxonomy_id ) );
144 $ids = array();
145 foreach ( $_data as $k => $v ) {
146 $ids[] = $v->object_id;
147 }
148 return $ids;
149 }
150 }