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
<?php
use MatthiasWeb\RealMediaLibrary\attachment;
use MatthiasWeb\RealMediaLibrary\general;
use MatthiasWeb\RealMediaLibrary\metadata;
use MatthiasWeb\RealMediaLibrary\order;
use MatthiasWeb\RealMediaLibrary\folder;
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/*
* In this file you will find attachment relevant functions.
*
* DEFINED POST TYPES
*
* define('RML_TYPE_FOLDER', 0);
* define('RML_TYPE_COLLECTION', 1);
* define('RML_TYPE_GALLERY', 2);
*
* ==========================================
*
* Example Szenario #1:
* 1. User navigates to http://example.com/rml/collection1
* 2. Use wp_rml_get_by_absolute_path("/collection1") to get the api\IFolder Object
* 3. (Additional check) $folder->is(RML_TYPE_COLLECTION) to check, if it is a collection.
* 4. Iterate the childrens with foreach ($folder->getChildren() as $value) { }
* 5. In collection can only be other collections or galleries.
*
* 6. (Additional check) $value->is(RML_TYPE_GALLERY) to check, if it is a gallery.
* 7. Fetch the IDs with $value->read();
*
* ==========================================
*
* If you want to use more functions look into the attachment\Structure Class.
* You easily get it with attachment\Structure::getInstance() (Singleton).
*
* Meaning: Root = Unorganized Pictures
*
* ==========================================
*
* ORDER QUERY
*
* Using the custom order of galleries: In your get_posts()
* query args use the option "orderby" => "rml" to get the
* images ordered by custom user order.
*
* ==========================================
*
* CUSTOM FIELDS FOR FOLDERS, COLLECTIONS, GALLERIES, ....
*
* You want create your own custom fields for a rml object?
* Have a look at themetadata\Metaclass.
*
* @see inc/metadata/Meta.class.php
*/
if (!function_exists('wp_rml_get_attachments')) {
/**
* Reads content of a folder.
*
* @param int $fid The folder id
* @param string $order The order statement
* @param string $orderby The order by statement
* @returns null|int[] Null if folder not exists or array of post ids
*/
function wp_rml_get_attachments($fid, $order = null, $orderby = null) {
$folder = wp_rml_get_object_by_id($fid);
return is_rml_folder($folder) ? $folder->read($order, $orderby) : null;
}
}
if (!function_exists('wp_attachment_folder')) {
/**
* Returns the folder id of an given attachment or more than one attachment (array). If you pass an array
* as attachment ids, then the default value does not work, only for single queries. When you pass a
* shortcut attachment id, the folder id for the shortcut is returned.
*
* @param int $attachmentId The attachment ID, if you pass an array you get an array of folder IDs
* @param mixed $default If no folder was found for this, this value is returned for the attachment
* @returns int|mixed Folder ID or $default or Array
*/
function wp_attachment_folder($attachmentId, $default = null) {
return attachment\Filter::getInstance()->getAttachmentFolder($attachmentId, $default);
}
}
if (!function_exists('wp_attachment_order_update')) {
/**
* Moves an attachment before another given attachment in the order table.
*
* @param int $folderId The folder id where the attachment exists
* @param int $attachmentId The attachment which should be moved
* @param int $nextId The attachment next to the currentId, if it is false the currentId should be moved to the end of table.
* @param int $lastIdInView If you have pagination, you can pass the last id from this view
* @returns boolean True or array with error strings
*/
function wp_attachment_order_update($folderId, $attachmentId, $nextId, $lastIdInView = false) {
// Get folder
$folder = wp_rml_get_object_by_id($folderId);
if (is_rml_folder($folder)) {
// Try to insert
try {
$folder->contentOrder($attachmentId, $nextId, $lastIdInView);
return true;
}catch (Exception $e) {
general\Core::getInstance()->debug($e->getMessage(), __FUNCTION__);
return array($e->getMessage());
}
}else{
general\Core::getInstance()->debug("Could not find the folder with id $folderId", __FUNCTION__);
return array(__("The given folder was not found.", RML_TD));
}
}
}
if (!function_exists('wp_rml_move')) {
/**
* Move or create shortcuts of a set of attachments to a specific folder.
*
* If you copy attachments, the action called is also "RML/Item/Move"... but
* there is a paramter $isShortcut.
*
* @param int $to Folder ID
* @param int[] $ids Array of attachment ids
* @param boolean $supress_validation Supress the permission validation
* @param boolean $isShortcut Determines, if the ID's are copies
* @return boolean|string[] True or Array with errors
*/
function wp_rml_move($to, $ids, $supress_validation = false, $isShortcut = false) {
if ($to === false || !is_numeric($to)) { // No movement
return array(__("The given folder was not found.", RML_TD));
}
// Get folder
$folder = wp_rml_get_object_by_id($to);
if (is_rml_folder($folder)) {
// Try to insert
try {
$folder->insert($ids, $supress_validation, $isShortcut);
return true;
}catch (Exception $e) {
general\Core::getInstance()->debug($e->getMessage(), __FUNCTION__);
return array($e->getMessage());
}
}else{
general\Core::getInstance()->debug("Could not find the folder with id $to", __FUNCTION__);
return array(__("The given folder was not found.", RML_TD));
}
}
}
/*
* Shortcut relevant API.
*/
if (!function_exists('wp_rml_create_shortcuts')) {
/**
* Link/Copy a set of attachments to a specific folder. When the folder
* has already a given shortcut, the movement for the given attachment will be skipped.
*
* If you want to receive the last created shortcut ID's you can use the
* wp_rml_created_shortcuts_last_ids() function.
*
* @param int $to Folder ID, if folder not exists then root will be
* @param int[] $ids Array of attachment ids
* @param boolean $supress_validation Supress the permission validation
* @returns boolean|string[] True or Array with errors
*/
function wp_rml_create_shortcuts($to, $ids, $supress_validation = false) {
return wp_rml_move($to, $ids, $supress_validation, true);
}
}
if (!function_exists('wp_rml_created_shortcuts_last_ids')) {
/**
* If you create shortcuts you can get the ids for those shortcuts with this function.
*
* @returns int[]
*/
function wp_rml_created_shortcuts_last_ids() {
return attachment\Shortcut::getInstance()->getLastIds();
}
}
if (!function_exists('wp_attachment_ensure_source_file')) {
/**
* Checks if a given attachment has already a shortcut in a given folder id
* or has generelly shortcuts.
*
* @param int|WP_Post $post The attachment id or a WP_Post object
* @returns int|WP_Post
*/
function wp_attachment_ensure_source_file($post) {
$isShortcut = wp_attachment_is_shortcut($post, true);
if ($isShortcut > 0) {
return $post instanceof \WP_Post ? get_post($isShortcut) : $isShortcut;
}
return $post;
}
}
if (!function_exists('wp_attachment_has_shortcuts')) {
/**
* Checks if a given attachment has already a shortcut in a given folder id
* or has generelly shortcuts.
*
* @param int $postId The attachment id
* @param int $fid The folder id, if false, it checks if there generelly exists shortcuts
* @returns boolean
*/
function wp_attachment_has_shortcuts($postId, $fid = false) {
global $wpdb;
$table_name = general\Core::getInstance()->getTableName("posts");
if ($fid !== false) {
$sql = $wpdb->prepare("SELECT COUNT(*) FROM $table_name WHERE isShortcut=%d AND fid=%d",
$postId, $fid);
}else{
$sql = $wpdb->prepare("SELECT COUNT(*) FROM $table_name WHERE isShortcut=%d",
$postId);
}
return $wpdb->get_var($sql) > 0;
}
}
if (!function_exists('wp_attachment_get_shortcuts')) {
/**
* Checks if a given attachment ID has shortcut and returns the shortcut IDs as array.
*
* @param int $postId The attachment id
* @param int $fid The folder id, if false, it checks if there generelly exists shortcuts
* @param boolean $extended If true the result is an array with all informations about the associated folder
* @returns mixed
*/
function wp_attachment_get_shortcuts($postId, $fid = false, $extended = false) {
global $wpdb;
$table_name = general\Core::getInstance()->getTableName("posts");
$table_name_rml = general\Core::getInstance()->getTableName();
$join = $extended ? "LEFT JOIN $table_name_rml AS rml ON rml.id = p.fid" : "";
$select = $extended ? ", rml.*" : "";
$orderby = $extended ? "ORDER BY name" : "";
if ($fid !== false) {
$sql = $wpdb->prepare("SELECT p.attachment, p.fid AS folderId, $select FROM $table_name AS p $join WHERE p.isShortcut=%d AND p.fid=%d $orderby",
$postId, $fid);
}else{
$sql = $wpdb->prepare("SELECT p.attachment, p.fid AS folderId $select FROM $table_name AS p $join WHERE p.isShortcut=%d $orderby",
$postId);
}
return $extended ? $wpdb->get_results($sql, ARRAY_A) : $wpdb->get_col($sql);
}
}
if (!function_exists('wp_attachment_is_shortcut')) {
/**
* Checks if a given attachment is a shortcut, use the $returnSourceId
* parameter to get the source attachment id.
*
* @param int|WP_Post $post The attachment id or a WP_Post object
* @param boolean $returnSourceId If true, the return will be the source attachment id or 0 if it is no shortcut
* @return boolean Boolean or int or 0
*/
function wp_attachment_is_shortcut($post, $returnSourceId = false) {
$guid = get_the_guid($post);
preg_match('/\?sc=([0-9]+)$/', $guid, $matches);
if (isset($matches) && is_array($matches) && isset($matches[1])) {
return $returnSourceId ? (int) $matches[1] : true;
}else{
return $returnSourceId ? 0 : false;
}
}
}
if (!function_exists('_wp_rml_synchronize_attachment')) {
/**
* Synchronizes a result with the realmedialibrary_posts table so on this
* base there can be made the folder content. It also creates shortcuts, if the
* given $isShortcut parameter is true.
*
* Do not use this directly, instead use the {@link wp_rml_move} function.
*
* @internal
* @param int $postId The post ID
* @param int $fid The folder ID
* @param boolean $isShortcut true = Is shortcut in the given folder, false = Is no shortcut, mainly in this folder
* @returns boolean
*/
function _wp_rml_synchronize_attachment($postId, $fid, $isShortcut = false) {
return attachment\Shortcut::getInstance()->create($postId, $fid, $isShortcut);
}
}
?>