['folder_id', 'fid'], 'post_id' => ['file_id', 'post_id', 'attachment_id', 'attachment'], '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_posts($posts, $fields); } /** * Get all posts of plugin using taxonomy * @param string $taxonomy * * @return array */ public function get_posts_by_taxonomy($taxonomy){ global $wpdb; $sql = "SELECT tr.*, object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = %s GROUP BY object_id"; $posts = Helpers::query($sql, $taxonomy); if( !$posts ) return []; // map data of posts $fields = [ 'folder_id' => ['term_taxonomy_id'], 'post_id' => ['object_id'], 'created_by' => [], 'updated_by' => [], 'created_at' => [], 'updated_at' => [], ]; return $this->format_posts($posts, $fields); } /** * Get all posts of plugin using post type * @param string $post_type * * @return array */ public function get_posts_by_post_type($post_type){ if( $post_type == 'mgmlp_media_folder'){ return $this->get_posts_by_mgmlp(); } return []; } /** * Get all posts from plugin Media Library Folders (by Max Foundry) */ public function get_posts_by_mgmlp(){ global $wpdb; $table = Helpers::check_table_exists('mgmlp_folders'); if( !$table ) return []; $sql = "SELECT p.ID as post_id, f.folder_id FROM {$table} AS f INNER JOIN {$wpdb->posts} AS p ON f.post_id = p.ID WHERE p.post_type = 'attachment'"; $posts = Helpers::query($sql); if( !$posts ) return []; // map data of attachments $fields = [ 'folder_id' => ['folder_id'], 'post_id' => ['post_id'], 'created_by' => [], 'updated_by' => [], 'created_at' => [], 'updated_at' => [], ]; return $this->format_posts($posts, $fields); } /** * Format data of posts with fields * * @param array $posts * @param array $fields * * @return array */ public function format_posts($posts, $fields){ $user_id = get_current_user_id(); $time = current_time('mysql'); $rows = []; foreach( $posts as $post ){ $row = []; foreach( $fields as $name => $array ){ $row[$name] = ''; foreach( $array as $key ){ if( isset($post[$key]) && $post[$key] ){ $row[$name] = $post[$key]; break; } } } if( !$row['post_id'] || !$row['post_id'] ) continue; 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 attachment to rows array_push($rows, $row); } return $rows; } }