Overview
  • Namespace
  • Class

Namespaces

  • MatthiasWeb
    • RealMediaLibrary
      • api
      • attachment
      • base
      • comp
        • complexquery
      • folder
      • general
      • metadata
      • order
      • rest
      • usersettings
  • None

Classes

  • MatthiasWeb\RealMediaLibrary\attachment\CountCache
  • MatthiasWeb\RealMediaLibrary\attachment\CustomField
  • MatthiasWeb\RealMediaLibrary\attachment\Filter
  • MatthiasWeb\RealMediaLibrary\attachment\Permissions
  • MatthiasWeb\RealMediaLibrary\attachment\Shortcut
  • MatthiasWeb\RealMediaLibrary\attachment\Structure
  • MatthiasWeb\RealMediaLibrary\attachment\Upload
  • MatthiasWeb\RealMediaLibrary\base\Assets
  • MatthiasWeb\RealMediaLibrary\base\Base
  • MatthiasWeb\RealMediaLibrary\base\Core
  • MatthiasWeb\RealMediaLibrary\comp\complexquery\ComplexQuery
  • MatthiasWeb\RealMediaLibrary\comp\complexquery\ResetNames
  • MatthiasWeb\RealMediaLibrary\comp\ExImport
  • MatthiasWeb\RealMediaLibrary\comp\PageBuilders
  • MatthiasWeb\RealMediaLibrary\comp\PolyLang
  • MatthiasWeb\RealMediaLibrary\comp\WPML
  • MatthiasWeb\RealMediaLibrary\folder\BaseFolder
  • MatthiasWeb\RealMediaLibrary\folder\Collection
  • MatthiasWeb\RealMediaLibrary\folder\Creatable
  • MatthiasWeb\RealMediaLibrary\folder\CRUD
  • MatthiasWeb\RealMediaLibrary\folder\Folder
  • MatthiasWeb\RealMediaLibrary\folder\Gallery
  • MatthiasWeb\RealMediaLibrary\folder\Root
  • MatthiasWeb\RealMediaLibrary\general\Activator
  • MatthiasWeb\RealMediaLibrary\general\Assets
  • MatthiasWeb\RealMediaLibrary\general\Core
  • MatthiasWeb\RealMediaLibrary\general\FolderShortcode
  • MatthiasWeb\RealMediaLibrary\general\Lang
  • MatthiasWeb\RealMediaLibrary\general\Migration
  • MatthiasWeb\RealMediaLibrary\general\Options
  • MatthiasWeb\RealMediaLibrary\general\QueryCount
  • MatthiasWeb\RealMediaLibrary\general\Util
  • MatthiasWeb\RealMediaLibrary\general\View
  • MatthiasWeb\RealMediaLibrary\metadata\CoverImage
  • MatthiasWeb\RealMediaLibrary\metadata\Description
  • MatthiasWeb\RealMediaLibrary\metadata\Meta
  • MatthiasWeb\RealMediaLibrary\order\GalleryOrder
  • MatthiasWeb\RealMediaLibrary\order\Sortable
  • MatthiasWeb\RealMediaLibrary\rest\Attachment
  • MatthiasWeb\RealMediaLibrary\rest\Folder
  • MatthiasWeb\RealMediaLibrary\rest\Reset
  • MatthiasWeb\RealMediaLibrary\rest\Service
  • MatthiasWeb\RealMediaLibrary\usersettings\Demo

Interfaces

  • MatthiasWeb\RealMediaLibrary\api\IFolder
  • MatthiasWeb\RealMediaLibrary\api\IFolderActions
  • MatthiasWeb\RealMediaLibrary\api\IFolderContent
  • MatthiasWeb\RealMediaLibrary\api\IMetadata
  • MatthiasWeb\RealMediaLibrary\api\IStructure
  • MatthiasWeb\RealMediaLibrary\api\IUserSettings

Exceptions

  • MatthiasWeb\RealMediaLibrary\general\FolderAlreadyExistsException

Functions

  • _wp_rml_active
  • _wp_rml_root
  • _wp_rml_sanitize
  • _wp_rml_sanitize_filename
  • add_media_folder_meta
  • add_rml_meta_box
  • add_rml_user_settings_box
  • delete_media_folder_meta
  • delete_media_folder_meta_by_key
  • get_media_folder_meta
  • is_rml_folder
  • rml_skip_php_admin_notice
  • rml_skip_rest_admin_notice
  • rml_skip_wp_admin_notice
  • truncate_media_folder_meta
  • update_media_folder_meta
  • wp_attachment_ensure_source_file
  • wp_attachment_folder
  • wp_attachment_get_shortcuts
  • wp_attachment_has_shortcuts
  • wp_attachment_is_shortcut
  • wp_attachment_order_update
  • wp_rml_create
  • wp_rml_create_all_children_sql
  • wp_rml_create_all_parents_sql
  • wp_rml_create_or_return_existing_id
  • wp_rml_create_shortcuts
  • wp_rml_created_shortcuts_last_ids
  • wp_rml_delete
  • wp_rml_dropdown
  • wp_rml_dropdown_collection
  • wp_rml_dropdown_gallery
  • wp_rml_dropdown_gallery_or_collection
  • wp_rml_get_attachments
  • wp_rml_get_by_absolute_path
  • wp_rml_get_by_id
  • wp_rml_get_object_by_id
  • wp_rml_get_parent_id
  • wp_rml_is_type
  • wp_rml_move
  • wp_rml_objects
  • wp_rml_register_creatable
  • wp_rml_rename
  • wp_rml_root_childs
  • wp_rml_structure
  • wp_rml_structure_reset
  • wp_rml_update_count
  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 
<?php
namespace MatthiasWeb\RealMediaLibrary\attachment;
use MatthiasWeb\RealMediaLibrary\general;
use MatthiasWeb\RealMediaLibrary\order;
use MatthiasWeb\RealMediaLibrary\base;

defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

/**
 * This class handles all hooks for the general filters.
 */
class Filter extends base\Base {
    private static $me = null;
    
    /**
     * @see wp_attachment_folder()
     */
    public function getAttachmentFolder($attachmentId, $default = null) {
        $isArray = is_array($attachmentId);
        $attachmentId = $isArray ? $attachmentId : array($attachmentId);
        
        if (count($attachmentId) > 0) {
            global $wpdb;
            $attachments_in = implode(",", $attachmentId);
            $table_name = general\Core::getInstance()->getTableName("posts");
            $folders = $wpdb->get_col("SELECT DISTINCT(rmlposts.fid) FROM $table_name AS rmlposts WHERE rmlposts.attachment IN ($attachments_in)");
            if ($isArray) {
                return $folders;
            }else{
                return isset($folders[0]) ? $folders[0] : (($default === null) ? _wp_rml_root() : $default);
            }
        }
        return $default;
    }
    
    /**
     * Changes the SQL query like this way to JOIN the realmedialibrary_posts
     * table and search for the given folder.
     */
    public function posts_clauses($clauses, $query) {
        global $wpdb;
        $table_name = general\Core::getInstance()->getTableName("posts");
        $saveInCookie = (is_admin() && defined('DOING_AJAX') && DOING_AJAX || $this->getCore()->getAssets()->isScreenBase("upload")) && !headers_sent();
        
        // Shortcut destinations
        $fields = trim($clauses["fields"], ",");
        
        if (!empty($query->query_vars['parsed_rml_folder'])) {
            $folderId = $query->query_vars['parsed_rml_folder'];
            $root = _wp_rml_root();
            $cookieValue = $root; // Save the last queried cookie for "New media" dropdown
            
            /**
             * Do a filter to restrict the RML posts clauses and apply an own clauses modifier.
             * If you want to use your own implementation of posts_clauses you can add this code
             * to to restrict the RML standard posts_clauses: <code>$clauses["_restrictRML"] = true;</code>
             * 
             * @param {array} $clauses The list of clauses for the query
             * @param {WP_Query} $query The WP_Query instance
             * @param {int} $folderId The folder ID to query
             * @param {int} $root The root folder ID, see also {@link RML/RootParent}
             * @returns {array} $clauses
             * @hook RML/Filter/PostsClauses
             * @see https://developer.wordpress.org/reference/hooks/posts_clauses/
             */
            $clauses = apply_filters("RML/Filter/PostsClauses", $clauses, $query, $folderId, $root);
            $builtIn = !isset($clauses["_restrictRML"]);
            if (!$builtIn) {
                unset($clauses["_restrictRML"]);
            }
            
            // Folder relevant data
            if ($builtIn === true && ($folderId > 0 || $folderId == $root)) {
                // Change fields
                $fields = trim($clauses["fields"], ",");
                $clauses["fields"] = $fields . ", rmlposts.fid, rmlposts.isShortcut ";
                
                // Change join regarding the folder id
                $clauses["join"] .= " LEFT JOIN $table_name AS rmlposts ON rmlposts.attachment = ".$wpdb->posts.".ID ";
                
                if ($folderId > 0) {
                    $clauses["join"] .= $wpdb->prepare(" AND rmlposts.fid = %d ", $folderId);
                    $clauses["where"] .= " AND rmlposts.fid IS NOT NULL ";
                }else{
                    $clauses["where"] .= $wpdb->prepare(" AND (rmlposts.fid IS NULL OR rmlposts.fid = %d) ", $root);
                }
                
                $cookieValue = $folderId;
            }
            
            // Save cookie value
            if ($saveInCookie && $builtIn) {
                $this->lastQueriedFolder($cookieValue);
            }
        }else if ($query->get("post_type") === "attachment") {
            // Reset last queried folder to unorganized
            if ($saveInCookie) {
                $this->lastQueriedFolder(_wp_rml_root());
            }
        }
        
        return $clauses;
    }
    
    /**
     * Set or get the last queried folder.
     * 
     * @param int $folder The folder id
     * @returns int
     */
    public function lastQueriedFolder($folder = null) {
        $key = "rml_" . get_current_blog_id() . "_lastquery";
        if ($folder !== null) {
            setcookie( $key, $folder, strtotime( '+365 days' ), '/' );
        }
        return isset($_COOKIE[$key]) ? $_COOKIE[$key] : _wp_rml_root();
    }
    
    /**
     * Define a new query option for \WP_Query.
     */
    public function pre_get_posts($query) {
        $folder = $this->getFolder($query, $this->getCore()->getAssets()->isScreenBase("upload"));
        
        if ($folder !== null) {
            $query->set('parsed_rml_folder', $folder);
        }
    }
    
    /**
     * Get folder from different sources (WP_Query, GET Query).
     * 
     * @returns int
     */
    public function getFolder($query, $fromRequest = false) {
        $folder = null;
        
        if ($query !== null && 
            ($queryFolder = $query->get('rml_folder')) &&
            isset($queryFolder)) {
                
            // Query rml folder from query itself
            $folder = $queryFolder;
        }else if(_wp_rml_active()) {
            if ($fromRequest) {
                if (isset($_REQUEST["rml_folder"])) {
                    // Query rml folder from list mode
                    $folder = $_REQUEST["rml_folder"];
                }else if (isset($_POST["query"]["rml_folder"])) {
                    // Query rml folder from grid mode
                    $folder = $_POST["query"]["rml_folder"];
                }else{
                    return;
                }
            }
        }else{
            return null;
        }
        return is_numeric($folder) ? $folder : null;
    }
    
    /**
     * Modify AJAX request for query-attachments request.
     */
    public function ajax_query_attachments_args($query) {
        $fid = $this->getFolder(null, true);
        if ($fid !== null) {
            $query["rml_folder"] = $fid;
        }
        return $query;
    }
    
    /**
     * Add the attachment ID to the count update when deleting it.
     */
    public function delete_attachment($postID) {
        //wp_rml_move(_wp_rml_root(), array($postID)); // Simulate an move to unorganized @deprecated
        
        // Reset folder count
        //CountCache::getInstance()->addNewAttachment($postID);
        CountCache::getInstance()->resetCountCacheOnWpDie(wp_attachment_folder($postID));
        
        // Delete row in posts table
        global $wpdb;
        $table_name = general\Core::getInstance()->getTableName("posts");
        $sql = $wpdb->query($wpdb->prepare("DELETE FROM $table_name WHERE attachment = %d", $postID));
        
        // Reindex folder
        $folder = wp_rml_get_object_by_id(wp_attachment_folder($postID));
        if (is_rml_folder($folder)) {
            $folder->contentReindex();
        }
    }
    
    /**
     * Add a attribute to the ajax output. The attribute represents
     * the folder order number if it is a gallery.
     */
    public function wp_prepare_attachment_for_js($response, $attachment, $meta) {
        // append attribute
        $folderId = $this->getFolder(null, true);
        $response['rmlFolderId'] = !empty($folderId) ? $folderId : _wp_rml_root();
        $response['rmlGalleryOrder'] = -1;
        $response['rmlIsShortcut'] = wp_attachment_is_shortcut($attachment->ID, true);
        
        if (isset($_POST["query"]) &&
                is_array($_POST["query"]) &&
                isset($_POST["query"]["orderby"]) &&
                $_POST["query"]["orderby"] == "rml") {
            $folder = wp_rml_get_object_by_id($folderId);
            if (is_rml_folder($folder)) {
                $orders = $folder->getContentOrderNumbers();
                if (is_array($orders) && isset($orders[$attachment->ID])) {
                    $response['rmlGalleryOrder'] = $orders[$attachment->ID];
                }
            }
        }
        
        // return
        return $response;
    }
    
    /**
     * Create a select option in list table of attachments.
     */
    public function restrict_manage_posts() {
        $screen = get_current_screen();
        if ($screen->id == "upload") {
            echo '<select name="rml_folder" id="filter-by-rml-folder" class="attachment-filters attachment-filters-rml">
                ' . Structure::getInstance()->getView()->dropdown(
                            isset($_REQUEST['rml_folder']) ? $_REQUEST['rml_folder'] : "",
                            array()
                        ) . '
            </select>&nbsp;';
        }
    }
    
    public static function getInstance() {
        if (self::$me == null) {
            self::$me = new Filter();
        }
        return self::$me;
    }
}
API documentation generated by ApiGen