1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
<?php
use MatthiasWeb\RealMediaLibrary\attachment;
use MatthiasWeb\RealMediaLibrary\general;
use MatthiasWeb\RealMediaLibrary\metadata;
use MatthiasWeb\RealMediaLibrary\order;
use MatthiasWeb\RealMediaLibrary\folder;
use MatthiasWeb\RealMediaLibrary\api;
if (!function_exists('is_rml_folder')) {
/**
* Checks, if a given variable is an implementation of the
* IFolder interface.
*
* @param int|mixed $obj Object or int (ID)
* @returns boolean
*/
function is_rml_folder($obj) {
return is_int($obj) ? is_rml_folder(wp_rml_get_object_by_id($obj)) : $obj instanceof api\IFolder;
}
}
if (!function_exists('wp_rml_get_parent_id')) {
/**
* Get the parent ID of a given folder id.
*
* @param int $id The id of the folder, collection, ...
* @returns int or null
*/
function wp_rml_get_parent_id($id) {
$folder = wp_rml_get_object_by_id($id);
return is_rml_folder($folder) ? $folder->getParent() : null;
}
}
if (!function_exists('wp_rml_objects')) {
/**
* Get all available folders, collections, galleries, ...
*
* @returns IFolder[]
*/
function wp_rml_objects() {
return attachment\Structure::getInstance()->getParsed();
}
}
if (!function_exists('wp_rml_root_childs')) {
/**
* Gets the first level childs of the media library.
*
* @returns IFolder[]
*/
function wp_rml_root_childs() {
return attachment\Structure::getInstance()->getTree();
}
}
if (!function_exists('wp_rml_create')) {
/**
* Creates a folder. At first it checks if a folder in parent already exists.
* Then it checks if the given type is allowed in the parent.
*
* It is highly recommenend, to use {@link wp_rml_structure_reset} after you created your folders.
*
* @param string $name String Name of the folder
* @param int $parent int ID of the parent (_wp_rml_root() for root)
* @param int $type integer 0|1|2 @see Folder.class.inc
* @param string[] $restrictions Restrictions for this folder
* @param boolean $supress_validation Supress the permission validation
* @param boolean $return_existing_id If true and the folder already exists, then return the ID of the existing folder
* @returns int|string[] int (ID) when successfully array with error strings
*/
function wp_rml_create($name, $parent, $type, $restrictions = array(), $supress_validation = false, $return_existing_id = false) {
return folder\CRUD::getInstance()->create($name, $parent, $type, $restrictions, $supress_validation, $return_existing_id);
}
}
if (!function_exists('wp_rml_create_or_return_existing_id')) {
/**
* Wrapper function for {@link wp_rml_create}.
*/
function wp_rml_create_or_return_existing_id($name, $parent, $type, $restrictions = array(), $supress_validation = false) {
return wp_rml_create($name, $parent, $type, $restrictions, $supress_validation, true);
}
}
if (!function_exists('wp_rml_rename')) {
/**
* Renames a folder and then checks, if there is no duplicate folder in the
* parent folder.
*
* @param string $name String New name of the folder
* @param int $id The ID of the folder
* @param boolean $supress_validation Supress the permission validation
* @return boolean|string[] true or array with error strings
*/
function wp_rml_rename($name, $id, $supress_validation = false) {
return folder\CRUD::getInstance()->update($name, $id, $supress_validation);
}
}
if (!function_exists('wp_rml_delete')) {
/**
* Deletes a folder by ID.
*
* @param int $id The ID of the folder
* @param boolean $supress_validation Supress the permission validation
* @returns boolean|string[] True or array with error string
*/
function wp_rml_delete($id, $supress_validation = false) {
return folder\CRUD::getInstance()->remove($id, $supress_validation);
}
}
if (!function_exists('wp_rml_update_count')) {
/**
* Handle the count cache for the folders. This should avoid
* a lack SQL subquery which loads data from the postmeta table.
*
* @param int[] $folders Array of folders ID, if null then all folders with cnt NULL are updated
* @param int[] $attachments Array of attachments ID, is merged with $folders if given
*/
function wp_rml_update_count($folders = null, $attachments = null) {
attachment\CountCache::getInstance()->updateCountCache($folders, $attachments);
}
}
if (!function_exists('wp_rml_dropdown')) {
/**
* This functions returns a HTML formatted string which contains
* options-tag elements with all folders, collections and galleries.
*
* @param mixed $selected The selected item, "" => "All Files", _wp_rml_root() => "Root", int => Folder ID. Can also be an array for multiple select (since 3.1.2)
* @param int[] $disabled Which folder types are disabled. Default disabled is RML_TYPE_COLLECTION
* @param boolean $useAll boolean Defines, if "All Files" should be showed
* @returns string
*/
function wp_rml_dropdown($selected, $disabled, $useAll = true) {
return attachment\Structure::getInstance()->getView()->dropdown($selected, $disabled, $useAll);
}
}
if (!function_exists('wp_rml_dropdown_collection')) {
/**
* This functions returns a HTML formatted string which contains
* <code><options></code> elements with all folders, collections and galleries.
* Note: Only COLLECTIONS are SELECTABLE!
*
* @param mixed $selected The selected item, "" => "All Files", _wp_rml_root() => "Root", int => Folder ID. Can also be an array for multiple select (since 3.1.2)
* @returns string
*/
function wp_rml_dropdown_collection($selected) {
return wp_rml_dropdown($selected, array(0,2,3,4));
}
}
if (!function_exists('wp_rml_dropdown_gallery')) {
/**
* This functions returns a HTML formatted string which contains
* option-tag elements with all folders, collections and galleries.
* Note: Only GALLERIES are SELECTABLE!
*
* @param mixed $selected The selected item, "" => "All Files", _wp_rml_root() => "Root", int => Folder ID. Can also be an array for multiple select (since 3.1.2)
* @returns string
*/
function wp_rml_dropdown_gallery($selected) {
return wp_rml_dropdown($selected, array(0,1,3,4));
}
}
if (!function_exists('wp_rml_dropdown_gallery_or_collection')) {
/**
* This functions returns a HTML formatted string which contains
* option-tag elements with all folders, collections and galleries.
* Note: Only GALLERIES AND COLLECTIONS are SELECTABLE!
*
* @param mixed $selected The selected item, "" => "All Files", _wp_rml_root() => "Root", int => Folder ID. Can also be an array for multiple select (since 3.1.2)
* @returns string
*/
function wp_rml_dropdown_gallery_or_collection($selected) {
return wp_rml_dropdown($selected, array(0,3,4));
}
}
if (!function_exists('wp_rml_is_type')) {
/**
* Determines if a Folder is a special folder type.
*
* @param IFolder|int $folder The folder object
* @param int[] $allowed Which folder types are allowed
* @returns boolean
*/
function wp_rml_is_type($folder, $allowed) {
if (!is_rml_folder($folder)) {
$folder = wp_rml_get_by_id($folder, null, true);
if (!is_rml_folder($folder)) {
return false;
}
}
return in_array($folder->getType(), $allowed);
}
}
if (!function_exists('wp_rml_get_object_by_id')) {
/**
* A shortcut function for the {@link wp_rml_get_by_id) function that ensures, that
* a IFolder object is returned. For -1 the root instance is returned.
*
* @returns IFolder Null if not found
*/
function wp_rml_get_object_by_id($id, $allowed = null) {
return wp_rml_get_by_id($id, $allowed, true, false);
}
}
if (!function_exists('wp_rml_get_by_id')) {
/**
* This functions checks if a specific folder exists by ID and is
* a given allowed RML Folder Type. If the given folder is {@link _wp_rml_root} you will
* get the first level folders.
*
* @param int $id Folder ID
* @param int[] $allowed Which folder types are allowed. If null all folder types are allowed.
* @param boolean $mustBeFolderObject Defines if the function may return the wp_rml_root_childs result
* @param boolean $nullForRoot If set to false and $id == -1 then the Root instance is returned
* @returns IFolder Null if not found
*/
function wp_rml_get_by_id($id, $allowed = null, $mustBeFolderObject = false, $nullForRoot = true) {
if (!is_numeric($id)) {
return null;
}
if ($mustBeFolderObject == false && $id == _wp_rml_root()) {
return wp_rml_root_childs();
}
$folder = attachment\Structure::getInstance()->byId($id, $nullForRoot);
if (is_array($allowed)) {
if (!wp_rml_is_type($folder, $allowed)) {
return null;
}
}
return $folder;
}
}
if (!function_exists('wp_rml_get_by_absolute_path')) {
/**
* This functions checks if a specific folder exists by absolute path and is
* a given allowed RML Folder Type.
*
* @param string $path Folder Absolute Path
* @param int[] $allowed Which folder types are allowed. If null all folder types are allowed.
* @returns IFolder Null if not found
*/
function wp_rml_get_by_absolute_path($path, $allowed = null) {
$folder = attachment\Structure::getInstance()->byAbsolutePath($path);
if (is_array($allowed)) {
if (!wp_rml_is_type($folder, $allowed)) {
return null;
}
}
return $folder;
}
}
if (!function_exists('wp_rml_register_creatable')) {
/**
* Register a new folder type for RML. It does not check if the creatable type
* is already registered.
*
* @param string $qualified The qualified name of the class representing the creatable
* @param int $type The type of the creatable. It must be the same as in yourClass::getType is returned
* @param boolean $onRegister Calls the yourClass::onRegister function
*/
function wp_rml_register_creatable($qualified, $type, $onRegister = false) {
folder\CRUD::getInstance()->registerCreatable($qualified, $type, $onRegister);
}
}
if (!function_exists('_wp_rml_root')) {
/**
* Get the parent root folder for a given blog id.
*
* @returns int Folder id
*/
function _wp_rml_root() {
/**
* Get the root folder id which is showed in the folder tree.
*
* @param {int} $folderId=-1 -1 is "/ Unorganized"
* @param {int} $blogId The current blog id
* @example <caption>Get the root folder</caption>
* $root = _wp_rml_root();
* @returns {int} The root folder id
* @hook RML/ParentRoot
*/
$result = apply_filters("RML/ParentRoot", -1, get_current_blog_id());
return (int) $result;
}
}
if (!function_exists('_wp_rml_active')) {
/**
* Checks if RML is active for the current user.
*
* @returns boolean
* @since 3.2
*/
function _wp_rml_active() {
/**
* Checks if RML is active for the current user. Do not use this filter
* yourself, instead use _wp_rml_active() function!
*
* @param {boolean} True for activated and false for deactivated
* @returns {boolean}
* @hook RML/Active
* @since 3.2
*/
$result = apply_filters("RML/Active", current_user_can("upload_files"));
return $result;
}
}
if (!function_exists('_wp_rml_sanitize')) {
/**
* Sanitize to a valid slug name for a given folder name. If the
* passed folder name contains only unvalide characters, then it falls
* back to the base64 encode.
*
* @param string $name The name of the folder
* @param boolean $database If true the name is generated unique from the database slugs
* @returns string
*/
function _wp_rml_sanitize($name, $database = false, $exclude = -1) {
$slug = sanitize_title(sanitize_file_name($name));
$slug = empty($slug) ? base64_encode($name) : $slug;
if ($database && !empty($name)) {
// Get unique slug
global $wpdb;
$core = general\Core::getInstance();
$table_name = $core->getTableName();
$i = 0;
while (true) {
$sql = $wpdb->prepare("SELECT COUNT(*) FROM $table_name WHERE slug = %s AND id <> %d", $i === 0 ? $slug : $slug . "-" . $i, $exclude);
$var = $wpdb->get_var($sql);
if ($var > 0) {
$i++;
}else{
break;
}
}
$slugOld = $slug;
$slug = $i === 0 ? $slugOld : $slugOld . "-" . $i;
$core->debug("Creating a new slug... check for unique slug '$slugOld', use '$slug'", __FUNCTION__);
}
return $slug;
}
}
if (!function_exists('_wp_rml_sanitize_filename')) {
function _wp_rml_sanitize_filename($name) {
$_name = sanitize_file_name($name);
return empty($_name) ? sanitize_file_name(_wp_rml_sanitize($name)) : $_name;
}
}
if (!function_exists('wp_rml_structure_reset')) {
/**
* Resets the structure. This function must be called when you create a new folder for example
* and to register it to the structure.
*
* ATTENTION: This function will be declared as deprecated soon, because it is
* planned to automatically reset the structure data / reset folder data (lazy loading
* of Folder objects).
*
* @param int $root The root folder to read the structure
* @param boolean $fetchData Determine if the data should be refetched
* @param int $returnId If set this folder is returned
* @returns IFolder If $returnId is set
*/
function wp_rml_structure_reset($root = null, $fetchData = true, $returnId = false) {
attachment\Structure::getInstance()->resetData($root, $fetchData);
if ($returnId !== false) {
return wp_rml_get_object_by_id($returnId);
}
}
}
if (!function_exists('wp_rml_structure')) {
/**
* Get the main working structure.
*
* @returns IStructure The structure
* @since 3.3.1
*/
function wp_rml_structure() {
return attachment\Structure::getInstance();
}
}
if (!function_exists('wp_rml_create_all_parents_sql')) {
/**
* Returns a SQL query to get all parents for a folder id.
* The first result for this SQL statement is the first parent and so on...
* Use rmltmp.lvl field for the depth number upwards. To avoid performance lacks
* you should figure out if there is already an action available to search for a meta_key
* in the action RML/$action/AnyParentHasMeta.
*
* <strong>$options</strong> parameters:
* <pre>"fields" => (string) SELECT fields (default: "rmldata.*, rmltmp.lvl AS lvlup")
* "join" => (string) JOIN statement (default: "")
* "where" => (string) Replace WHERE statement, it is preferred to use afterWhere (default: "rmltmp.lvl > " . ($includeSelf === true ? "-1" : "0"))
* "afterWhere"=> (string) Additional WHERE statement to the above WHERE (default: "")
* "orderby" => (string) ORDER BY statement (default: "rmltmp.lvl ASC")
* "limit" => (string) LIMIT statement (default: "")</pre>
*
* @param IFolder|int $folder The folder object or folder id
* @param boolean $includeSelf Set true to include self (passed $folder)
* @param int $until Until this folder id
* @param array $options Additional options for the SQL query, see above
* @returns string|boolean SQL query or false if something went wrong
*/
function wp_rml_create_all_parents_sql($folder, $includeSelf = false, $until = null, $options = null) {
return general\Util::getInstance()->createSQLForAllParents($folder, $includeSelf, $until, $options);
}
}
if (!function_exists('wp_rml_create_all_children_sql')) {
/**
* Returns a SQL query to get all children for a folder id.
* The first result for this SQL statement is the first children and so on...
* Use rmldata.lvldown field for the depth number downwards
*
* <strong>$options</strong> parameters:
* <pre>"fields" => (string) SELECT fields (default: "rmldata.*"),
* "join" => (string) JOIN statement (default: ""),
* "where" => (string) Replace WHERE statement, it is preferred to use afterWhere (default: $wpdb->prepare("rmldata._parent = %d", $folderId)
* . ($includeSelf === true ? "" : $wpdb->prepare(" AND rmldata.id != %d", $folderId))),
* "afterWhere" => (string) Additional WHERE statement to the above WHERE (default: ""),
* "orderby" => (string) ORDER BY statement (default: "rmldata.parent, rmldata.ord"),
* "limit" => (string) LIMIT statement (default: "")</pre>
*
* @param IFolder|int $folder The folder object or folder id
* @param boolean $includeSelf Set true to include self (passed $folder)
* @param array $options Additional options for the SQL query, see above
* @returns string|boolean SQL query or false if something went wrong
*/
function wp_rml_create_all_children_sql($folder, $includeSelf = false, $options = null) {
return general\Util::getInstance()->createSQLForAllChildren($folder, $includeSelf, $options);
}
}