問題描述
介紹。
在上面的螢幕截圖中,您可以看到使用高階外掛 Real Media Library 構建的資料夾結構。現在我想建立一個可以將資料夾結構組織成物理資料夾結構的擴充套件外掛–RML 只是視覺結構。
更新#2(2017-01-27):檢視答案!
看看 Physical organization of wordpress media library (Real Media Library plugin)我已經建立了一個免費的擴充套件外掛。
更新#1(2016-12-14):第一個成功:自定義縮圖上傳資料夾
現在,我建立了另一個外掛 Real Thumbnail Generator,它允許您建立自定義縮圖上傳資料夾。只要看看這個截圖:
為什麼自定義縮圖資料夾?自定義縮圖資料夾更容易維護,因為在這裡,我們不需要維護資料庫更新 URL,因為縮圖仍然位於相同的位置 (RML 副檔名仍未更改) 。
如果您想了解有關自定義縮圖生成器的更多資訊,您可以看看這個執行緒,在那裡我已經解釋了一個技術方法 Each custom image size in custom upload directory? 。
請繼續這個執行緒,因為 2017 年初我將繼續開發 RML 擴充套件,允許 RML 和伺服器上傳資料夾之間的同步。擴充套件也相容 Real Thumbnail Generator 外掛,所以應該有資料庫更新。
原帖
我的擴充套件目標
目前我在 「/無組織」 資料夾中,這就是資料夾/wp-content /uploads /。當我將檔案 (在螢幕截圖中可以看到) 移動到資料夾 PDFs /SubDir 時,該檔案在可視資料夾中。現在我的擴充套件檢測到不同的物理資料夾,並顯示一個”button”,它允許使用者物理地移動它:
使用者現在點選按鈕 「Physix it!」 該檔案應移動到/wp-content/uploads/pdfs/subdir/Another-Doc.pdf 。我已經建立了移動過程:我讀出了這個附件的所有媒體檔案 (包含影像的縮圖),並使用 php 函式 rename($old_file, $new_file)和 WP 函式 wp_mkdir_p()。 wp_posts 表中的 GUID 和 wp_postmeta 中的後設資料也被更改。當所有檔案被移動時,我呼叫該操作:
<?php
do_action('RML/Physix/Moved', $meta, $id);
// $meta = Infos about the move process, see above screenshot
// $id = The attachment ID
?>
$ meta 是一個陣列:
關鍵”rename” 包含所有重新命名過程 (例如,這裡可以是影像的縮圖檔案) 。
問題:保證外掛的相容性。
WordPress 媒體庫的主要問題 (如果是),許多外掛會使用完整的 URL 而不是附件 ID 儲存對影像的引用。這意味著,有一些 MySQL 表的列包含給定檔案的 URL 。我如何保證所有的引用都是 up-to-date 與物理資料夾?我認為這是不可能的
一種可能的方法
我鉤住了這個動作,並在 SQL 中使用遞迴的 REPLACE-statement 來更新諸如 wp_post-> post_content,… 的標準表。
<?php
/**
* When a attachment is moved.
*
* @hooked RML/Physix/Moved
*/
function physix_moved($meta, $id) {
$rename = $meta["rename"];
// Prepare array for recursive REPLACE
$arr = array();
foreach ($rename as $value) {
$arr[] = array($value["old_url"], $value["new_url"]);
}
$rec = $this->recReplace($arr, "post_content"); // function is already finished
}
?>
$ rec 變數現在是一個 REPLACE-Statement:
REPLACE(post_content, 'https://example.io/wp-content/uploads/Another-Doc.pdf', 'https://example.io/wp-content/uploads/pdfs/subdir/Another-Doc.pdf')
順便說一句:對於具有所有縮圖檔案的影像 (testimage.jpg),它可以如下所示:
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(post_content, 'https://example.io/wp-content/uploads/testimage-750x350.jpg', 'https://example.io/wp-content/uploads/pdfs/subdir/testimage-750x350.jpg'), 'https://example.io/wp-content/uploads/testimage-1170x855.jpg', 'https://example.io/wp-content/uploads/pdfs/subdir/testimage-1170x855.jpg'), 'https://example.io/wp-content/uploads/testimage-256x187.jpg', 'https://example.io/wp-content/uploads/pdfs/subdir/testimage-256x187.jpg'), 'https://example.io/wp-content/uploads/testimage-1024x748.jpg', 'https://example.io/wp-content/uploads/pdfs/subdir/testimage-1024x748.jpg'), 'https://example.io/wp-content/uploads/testimage-300x219.jpg', 'https://example.io/wp-content/uploads/pdfs/subdir/testimage-300x219.jpg'), 'https://example.io/wp-content/uploads/testimage-150x150.jpg', 'https://example.io/wp-content/uploads/pdfs/subdir/testimage-150x150.jpg'), 'https://example.io/wp-content/uploads/testimage.jpg', 'https://example.io/wp-content/uploads/pdfs/subdir/testimage.jpg')
但是,如果它是資料庫表中的序列化字串 (JSON),會發生什麼?所以看起來像 { "image": "http://example.io/wp-content/uploads/Another-Doc.pdf" }。我必須新增到 REPLACE-Statement?
現在可以透過包含影像 URL 的所有 MySQL 表使用 REPLACE-Statement 。我想到建立一個過濾器陣列,其中外掛可以新增他們的表,我的擴充套件做其餘的:
<?php
$tables = apply_filters("RML/Physix/Moved/Tables", array( // TODO: use $wpdb->prefix
"wp_posts" => array("post_excerpt", "post_content"),
"wp_postmeta" => array("meta_value")
//...
));
?>
“move” 日誌
我想建立一個”log”,使用者可以在其中撤消移動。如果使用者看到,影像被破壞 (例如在 Slider Revolution 外掛中),他可以撤消移動到原始資料夾。
你對這個想法有什麼看法?有更好的解決方案嗎?我希望我有一個很好的解釋方式!
參考文獻
注:本文內容整合自 google/baidu/bing 翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:gxnotes#qq.com(#替換為 @) 。



