['id'], 'title' => ['name', 'title'], 'type' => ['type'], 'parent_id' => ['parent_id', 'parent'], 'color' => ['color'], 'total_files' => ['total_files', 'count'], 'created_by' => ['created_by', 'owner'], 'updated_by' => ['updated_by', 'modified_by'], 'created_at' => ['created_at', 'created'], 'updated_at' => ['updated_at', 'updated', 'modified'], ]; return $this->format_folders($folders, $fields); } /** * Get all folders of plugin using taxonomy * @param string $taxonomy * * @return array */ public function get_folders_by_taxonomy($taxonomy){ global $wpdb; // check plugin installed if( !$taxonomy || !is_string($taxonomy) ) return []; // phpcs:ignore WordPress.DB $sql = "SELECT t.*, tt.parent, tt.count FROM $wpdb->terms t INNER JOIN $wpdb->term_taxonomy tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s"; $folders = Helpers::query($sql, $taxonomy); if( !$folders ) return []; // map data of folders $fields = [ 'id' => ['term_id'], 'title' => ['name'], 'type' => ['taxonomy'], 'parent_id' => ['parent'], 'color' => ['color'], 'total_files' => ['total_files', 'count'], 'created_by' => ['created_by', 'owner'], 'updated_by' => ['updated_by', 'modified_by'], 'created_at' => ['created_at', 'created'], 'updated_at' => ['updated_at', 'updated', 'modified'], ]; return $this->format_folders($folders, $fields); } /** * Get all folders of plugin using post type * @param string $post_type * * @return array */ public function get_folders_by_post_type($post_type){ global $wpdb; // check plugin installed if( !$post_type || !is_string($post_type) ) return []; $folders = Helpers::query( "SELECT * FROM $wpdb->posts WHERE post_type = %s", $post_type ); if( !$folders ) return []; // map data of folders $fields = [ 'id' => ['ID'], 'title' => ['post_title'], 'type' => ['type'], 'parent_id' => ['post_parent'], 'color' => ['color'], 'total_files' => ['count'], 'created_by' => ['post_author'], 'updated_by' => ['post_author'], 'created_at' => ['post_date'], 'updated_at' => ['post_modified'], ]; $rows = $this->format_folders($folders, $fields); if( $post_type == 'mgmlp_media_folder'){ $rows = $this->get_mgmlp_folders($rows); } return $rows; } /** * Get all folders from plugin Media Library Folders (by Max Foundry) */ public function get_mgmlp_folders($folders){ if( !$folders || !is_array($folders) ) return []; // check table mgmlp_folders $table = Helpers::check_table_exists('mgmlp_folders'); if( !$table ) return []; $folder_ids = []; foreach( $folders as $folder ){ if( isset($folder['id']) && $folder['id'] ){ $folder_ids[] = $folder['id']; } } if( empty($folder_ids) ) return []; // get all post id from list folder ids $placeholders = implode(',', array_fill(0, count($folder_ids), '%d')); $sql = "SELECT post_id, folder_id FROM {$table} WHERE post_id IN ($placeholders)"; $rows = Helpers::query($sql, $folder_ids); if( !$rows ) return []; // update parent_id from rows to folders foreach( $folders as &$folder ){ foreach( $rows as $row ){ if( $row['post_id'] == $folder['id'] ){ $folder['parent_id'] = (int) $row['folder_id']; break; } } } return $folders; } /** * Format folder data with fields * * @param array $folders * @param array $fields * * @return array */ public function format_folders($folders, $fields){ $user_id = get_current_user_id(); $time = current_time('mysql'); $rows = []; $rows = []; foreach( $folders as $folder ){ $row = []; foreach( $fields as $name => $array ){ $row[$name] = ''; for( $i = 0; $i < count($array); $i++ ){ $key = $array[$i]; if( isset($folder[$key]) && $folder[$key] ){ $row[$name] = $folder[$key]; break; } } } if( !$row['id'] || !$row['title'] ) continue; if( !$row['parent_id'] || $row['parent_id'] < 0) $row['parent_id'] = 0; else $row['parent_id'] = (int) $row['parent_id']; if( !$row['type'] ) $row['type'] = 'attachment'; if( !$row['color'] ) $row['color'] = ''; if( !$row['total_files'] ) $row['total_files'] = 0; if( !$row['created_by'] ) $row['created_by'] = $user_id; if( !$row['updated_by'] ) $row['updated_by'] = $user_id; if( !$row['created_at'] ) $row['created_at'] = $time; if( !$row['updated_at'] ) $row['updated_at'] = $time; // add folder to rows array_push($rows, $row); } return $rows; } } ?>