| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
class Meow_MGL_RML { |
| 5 |
|
| 6 |
|
| 7 |
public static function is_available() { |
| 8 |
return function_exists( 'wp_rml_get_by_absolute_path' ) && function_exists( 'wp_rml_get_attachments' ); |
| 9 |
} |
| 10 |
|
| 11 |
|
| 12 |
/** |
| 13 |
* Returns the whole Real Media Library folder structure as a flat, path-sorted |
| 14 |
* list, so the admin can offer it as a selectable dropdown. |
| 15 |
* |
| 16 |
* @return array List of ['path' => string, 'name' => string, 'type' => int]. |
| 17 |
* Type: 0 = Folder, 1 = Collection, 2 = Gallery. |
| 18 |
*/ |
| 19 |
public static function get_all_folders() { |
| 20 |
if ( ! function_exists( 'wp_rml_objects' ) ) { |
| 21 |
return []; |
| 22 |
} |
| 23 |
|
| 24 |
$objects = wp_rml_objects(); |
| 25 |
if ( ! is_array( $objects ) ) { |
| 26 |
return []; |
| 27 |
} |
| 28 |
|
| 29 |
$folders = []; |
| 30 |
foreach ( $objects as $folder ) { |
| 31 |
if ( ! self::is_folder( $folder ) ) { |
| 32 |
continue; |
| 33 |
} |
| 34 |
|
| 35 |
// Skip the root / "Unorganized" pseudo-folder (negative id, empty path). |
| 36 |
if ( (int) $folder->getId() < 0 ) { |
| 37 |
continue; |
| 38 |
} |
| 39 |
|
| 40 |
$path = $folder->getAbsolutePath(); |
| 41 |
if ( $path === '' ) { |
| 42 |
continue; |
| 43 |
} |
| 44 |
|
| 45 |
$folders[] = [ |
| 46 |
'path' => $path, |
| 47 |
'name' => $folder->getName(), |
| 48 |
'type' => (int) $folder->getType(), |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
usort( $folders, function ( $a, $b ) { |
| 53 |
return strcmp( $a['path'], $b['path'] ); |
| 54 |
} ); |
| 55 |
|
| 56 |
return $folders; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
public static function get_image_ids( $path ) { |
| 61 |
$folder = self::resolve_folder( $path ); |
| 62 |
if ( is_wp_error( $folder ) ) { |
| 63 |
return $folder; |
| 64 |
} |
| 65 |
|
| 66 |
// Collect the folder's own media plus, if it is a Collection, the media of |
| 67 |
// all its sub-galleries (recursively) so they are returned as one gallery. |
| 68 |
$image_ids = self::collect_image_ids( $folder ); |
| 69 |
if ( empty( $image_ids ) ) { |
| 70 |
return new WP_Error( |
| 71 |
'rml_folder_empty', |
| 72 |
sprintf( __( 'The Real Media Library folder "%s" is empty.', MGL_DOMAIN ), $folder->getAbsolutePath() ) |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
return array_values( array_unique( array_map( 'intval', $image_ids ) ) ); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public static function get_collection_galleries( $path ) { |
| 82 |
$folder = self::resolve_folder( $path ); |
| 83 |
if ( is_wp_error( $folder ) ) { |
| 84 |
return $folder; |
| 85 |
} |
| 86 |
|
| 87 |
// Only Folders and Collections can be turned into a collection. |
| 88 |
if ( self::is_gallery( $folder ) ) { |
| 89 |
return new WP_Error( |
| 90 |
'rml_not_a_collection', |
| 91 |
sprintf( __( 'The Real Media Library path "%s" is a gallery. Only folders and collections are allowed here.', MGL_DOMAIN ), $folder->getAbsolutePath() ) |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
$children = method_exists( $folder, 'getChildren' ) ? $folder->getChildren() : []; |
| 96 |
$children = is_array( $children ) ? $children : []; |
| 97 |
|
| 98 |
$galleries = []; |
| 99 |
foreach ( $children as $child ) { |
| 100 |
if ( ! self::is_folder( $child ) ) { |
| 101 |
continue; |
| 102 |
} |
| 103 |
|
| 104 |
// Gather all media of this sub-gallery (recursively) so we can pick a |
| 105 |
// lead image and skip empty ones. |
| 106 |
$media_ids = self::collect_image_ids( $child ); |
| 107 |
if ( empty( $media_ids ) ) { |
| 108 |
continue; |
| 109 |
} |
| 110 |
$media_ids = array_values( array_unique( array_map( 'intval', $media_ids ) ) ); |
| 111 |
|
| 112 |
$galleries[] = [ |
| 113 |
'path' => $child->getAbsolutePath(), |
| 114 |
'name' => $child->getName(), |
| 115 |
'lead_image_id' => $media_ids[0], |
| 116 |
]; |
| 117 |
} |
| 118 |
|
| 119 |
if ( empty( $galleries ) ) { |
| 120 |
return new WP_Error( |
| 121 |
'rml_collection_empty', |
| 122 |
sprintf( __( 'The Real Media Library folder "%s" has no galleries.', MGL_DOMAIN ), $folder->getAbsolutePath() ) |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
return $galleries; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
private static function resolve_folder( $path ) { |
| 131 |
if ( ! self::is_available() ) { |
| 132 |
return new WP_Error( 'rml_not_enabled', __( 'Real Media Library is not enabled.', MGL_DOMAIN ) ); |
| 133 |
} |
| 134 |
|
| 135 |
// RML uses "/" as the folder separator for absolute paths. |
| 136 |
$path = trim( str_replace( '\\', '/', (string) $path ) ); |
| 137 |
$path = trim( $path, '/' ); |
| 138 |
|
| 139 |
if ( $path === '' ) { |
| 140 |
return new WP_Error( 'rml_empty_path', __( 'The Real Media Library folder path is empty.', MGL_DOMAIN ) ); |
| 141 |
} |
| 142 |
|
| 143 |
$folder = wp_rml_get_by_absolute_path( $path ); |
| 144 |
if ( ! self::is_folder( $folder ) ) { |
| 145 |
return new WP_Error( |
| 146 |
'rml_folder_not_found', |
| 147 |
sprintf( __( 'The Real Media Library folder "%s" was not found.', MGL_DOMAIN ), $path ) |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
return $folder; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
/** |
| 156 |
* Whether the folder is a Gallery Data Folder (holds media directly). |
| 157 |
* RML folder types: 0 = Folder, 1 = Collection, 2 = Gallery. |
| 158 |
*/ |
| 159 |
private static function is_gallery( $folder ) { |
| 160 |
if ( ! is_object( $folder ) || ! method_exists( $folder, 'getType' ) ) { |
| 161 |
return false; |
| 162 |
} |
| 163 |
if ( defined( 'RML_TYPE_GALLERY' ) ) { |
| 164 |
return (int) $folder->getType() === RML_TYPE_GALLERY; |
| 165 |
} |
| 166 |
return (int) $folder->getType() === 2; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
private static function collect_image_ids( $folder ) { |
| 171 |
$ids = wp_rml_get_attachments( $folder->getId() ); |
| 172 |
$ids = is_array( $ids ) ? $ids : []; |
| 173 |
|
| 174 |
if ( method_exists( $folder, 'getChildren' ) ) { |
| 175 |
$children = $folder->getChildren(); |
| 176 |
if ( is_array( $children ) ) { |
| 177 |
foreach ( $children as $child ) { |
| 178 |
if ( self::is_folder( $child ) ) { |
| 179 |
$ids = array_merge( $ids, self::collect_image_ids( $child ) ); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
return $ids; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
private static function is_folder( $folder ) { |
| 190 |
if ( function_exists( 'is_rml_folder' ) ) { |
| 191 |
return is_rml_folder( $folder ); |
| 192 |
} |
| 193 |
return is_object( $folder ) && method_exists( $folder, 'getId' ); |
| 194 |
} |
| 195 |
} |
| 196 |
|