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