-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-document-folder.php
More file actions
627 lines (547 loc) · 16.6 KB
/
custom-document-folder.php
File metadata and controls
627 lines (547 loc) · 16.6 KB
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
<?php
/**
* Plugin Name: Custom Document Folder
* Plugin URI: https://github.com/soderlind/custom-document-folder
* Description: Redirects specific document types to custom folders based on file extensions. Configure document types in Settings > Document Folder.
* Version: 1.1.1
* Author: Per Soderlind
* Author URI: https://soderlind.no
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: custom-document-folder
*/
declare(strict_types=1);
namespace Soderlind\DocumentFolder;
if ( ! defined( 'ABSPATH' ) ) {
wp_die();
}
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/class-github-plugin-updater.php';
use Soderlind\DocumentFolder\GitHub_Plugin_Updater;
GitHub_Plugin_Updater::create_with_assets(
'https://github.com/soderlind/custom-document-folder',
__FILE__,
'custom-document-folder',
'/custom-document-folder\.zip/',
'main'
);
/**
* Class Upload_Document
*
* @package Soderlind\DocumentFolder
*/
class Upload_Document {
/**
* Selected file extensions.
*
* @var array
*/
private $selected_extensions = [];
/**
* File extension.
*
* @var string
*/
private $ext = '';
/**
* Constructor.
*/
public function __construct() {
$this->selected_extensions = get_option( 'cdf_selected_extensions', [ 'pdf' ] );
if ( ! is_array( $this->selected_extensions ) ) {
$this->selected_extensions = [ 'pdf' ];
}
add_action( 'admin_menu', [ $this, 'add_admin_menu' ] );
add_action( 'admin_init', [ $this, 'settings_init' ] );
add_filter( 'upload_dir', [ $this, 'upload_dir' ], 20 );
add_filter( 'wp_handle_upload_prefilter', [ $this, 'wp_handle_upload_prefilter' ] );
add_filter( 'wp_check_filetype_and_ext', [ $this, 'wp_check_filetype_and_ext' ], 10, 4 );
add_filter( 'wp_handle_upload_overrides', [ $this, 'non_unique_filename' ] );
add_filter( 'sanitize_file_name', [ $this, 'filename_filter' ], 10, 1 );
}
/**
* Add settings page.
*/
public function add_admin_menu() {
add_options_page(
'Custom Document Folder',
'Document Folder',
'manage_options',
'custom-document-folder',
[ $this, 'settings_page_html' ]
);
}
/**
* Register settings.
*/
public function settings_init() {
register_setting(
'cdf_plugin',
'cdf_selected_extensions',
[
'type' => 'array',
'sanitize_callback' => [ $this, 'sanitize_extensions' ],
'default' => [ 'pdf' ],
]
);
add_settings_section(
'cdf_section',
__( 'File Extension Settings', 'custom-document-folder' ),
[ $this, 'section_callback' ],
'cdf_plugin'
);
add_settings_field(
'cdf_selected_extensions',
__( 'Select Extensions', 'custom-document-folder' ),
[ $this, 'extensions_render' ],
'cdf_plugin',
'cdf_section'
);
}
/**
* Sanitize extensions.
*
* @param array $input Input array.
* @return array
*/
public function sanitize_extensions( $input ) {
if ( ! is_array( $input ) ) {
return [ 'pdf' ];
}
return array_map( 'sanitize_text_field', $input );
}
/**
* Section callback.
*/
public function section_callback() {
echo '<p>' . esc_html__( 'Select the file extensions that should be uploaded to custom folders. Each selected extension will be uploaded to its own folder (e.g., /uploads/pdf/).', 'custom-document-folder' ) . '</p>';
}
/**
* Render settings field.
*/
public function extensions_render() {
$allowed_mimes = get_allowed_mime_types();
$extensions = [];
$grouped = [];
// Build extensions array and group by mime type.
foreach ( $allowed_mimes as $ext_str => $mime ) {
$exts = explode( '|', $ext_str );
foreach ( $exts as $e ) {
$extensions[ $e ] = $mime;
$mime_category = $this->get_mime_category( $mime );
$grouped[ $mime_category ][] = [ 'ext' => $e, 'mime' => $mime ];
}
}
// Sort categories and extensions.
ksort( $grouped );
foreach ( $grouped as $category => $items ) {
usort( $grouped[ $category ], function ( $a, $b ) {
return strcmp( $a[ 'ext' ], $b[ 'ext' ] );
} );
}
$selected_count = count( $this->selected_extensions );
$hidden_categories = [ 'audio', 'images', 'video', 'other' ];
?>
<div class="cdf-extensions-wrapper">
<div class="cdf-controls">
<input type="text" id="cdf-search" class="regular-text"
placeholder="<?php esc_attr_e( 'Search extensions...', 'custom-document-folder' ); ?>">
<button type="button"
class="button cdf-select-all"><?php esc_html_e( 'Select All', 'custom-document-folder' ); ?></button>
<button type="button"
class="button cdf-deselect-all"><?php esc_html_e( 'Deselect All', 'custom-document-folder' ); ?></button>
<button type="button" class="button cdf-toggle-hidden" id="cdf-toggle-hidden">
<span class="dashicons dashicons-arrow-down-alt2"></span>
<?php esc_html_e( 'Show More Categories', 'custom-document-folder' ); ?>
</button>
<span
class="cdf-count"><?php printf( esc_html__( 'Selected: %d', 'custom-document-folder' ), $selected_count ); ?></span>
</div>
<div class="cdf-extensions-grid">
<?php foreach ( $grouped as $category => $items ) : ?>
<?php
$is_hidden = in_array( $category, $hidden_categories, true );
$category_class = $is_hidden ? 'cdf-category cdf-category-collapsible' : 'cdf-category';
?>
<div class="<?php echo esc_attr( $category_class ); ?>" data-category="<?php echo esc_attr( $category ); ?>">
<h4 class="cdf-category-title"><?php echo esc_html( ucfirst( $category ) ); ?></h4>
<div class="cdf-extensions-list">
<?php foreach ( $items as $item ) : ?>
<?php
$ext = $item[ 'ext' ];
$mime = $item[ 'mime' ];
$checked = in_array( $ext, $this->selected_extensions, true ) ? 'checked' : '';
?>
<label class="cdf-extension-item" data-ext="<?php echo esc_attr( $ext ); ?>">
<input type="checkbox" name="cdf_selected_extensions[]" value="<?php echo esc_attr( $ext ); ?>"
<?php echo $checked; ?>>
<span class="cdf-ext-name"><?php echo esc_html( $ext ); ?></span>
<span class="cdf-mime-type"><?php echo esc_html( $mime ); ?></span>
</label>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
<style>
.cdf-extensions-wrapper {
max-width: 1200px;
}
.cdf-controls {
margin-bottom: 20px;
padding: 15px;
background: #f9f9f9;
border: 1px solid #ddd;
border-radius: 4px;
display: flex;
gap: 10px;
align-items: center;
flex-wrap: wrap;
}
#cdf-search {
flex: 1;
min-width: 200px;
}
.cdf-toggle-hidden {
display: flex;
align-items: center;
gap: 5px;
}
.cdf-toggle-hidden .dashicons {
transition: transform 0.3s;
}
.cdf-toggle-hidden.expanded .dashicons {
transform: rotate(180deg);
}
.cdf-count {
margin-left: auto;
font-weight: 600;
color: #2271b1;
}
.cdf-extensions-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 20px;
}
.cdf-category {
border: 1px solid #ddd;
border-radius: 4px;
background: #fff;
overflow: hidden;
}
.cdf-category.hidden {
display: none;
}
.cdf-category-collapsible {
display: none;
}
.cdf-category-collapsible.show {
display: block;
}
.cdf-category-title {
margin: 0;
padding: 12px 15px;
background: #f0f0f1;
border-bottom: 1px solid #ddd;
font-size: 14px;
font-weight: 600;
color: #1d2327;
}
.cdf-extensions-list {
padding: 10px;
max-height: 400px;
overflow-y: auto;
}
.cdf-extension-item {
display: flex;
align-items: center;
padding: 8px 10px;
margin: 0;
border-radius: 3px;
transition: background 0.2s;
cursor: pointer;
gap: 8px;
}
.cdf-extension-item:hover {
background: #f6f7f7;
}
.cdf-extension-item.hidden {
display: none;
}
.cdf-extension-item input[type="checkbox"] {
margin: 0;
}
.cdf-ext-name {
font-weight: 600;
color: #2271b1;
min-width: 50px;
}
.cdf-mime-type {
font-size: 12px;
color: #666;
flex: 1;
}
</style>
<script>
jQuery(document).ready(function ($) {
const $search = $('#cdf-search');
const $count = $('.cdf-count');
const $checkboxes = $('input[name="cdf_selected_extensions[]"]');
const $toggleBtn = $('#cdf-toggle-hidden');
let hiddenExpanded = false;
// Update count
function updateCount() {
const count = $checkboxes.filter(':checked').length;
$count.text('<?php esc_html_e( 'Selected: ', 'custom-document-folder' ); ?>' + count);
}
// Toggle hidden categories
$toggleBtn.on('click', function () {
hiddenExpanded = !hiddenExpanded;
$('.cdf-category-collapsible').toggleClass('show', hiddenExpanded);
if (hiddenExpanded) {
$(this).addClass('expanded');
$(this).find('span:not(.dashicons)').text('<?php esc_html_e( 'Show Fewer Categories', 'custom-document-folder' ); ?>');
} else {
$(this).removeClass('expanded');
$(this).find('span:not(.dashicons)').text('<?php esc_html_e( 'Show More Categories', 'custom-document-folder' ); ?>');
}
});
// Search functionality
$search.on('input', function () {
const query = $(this).val().toLowerCase();
let visibleCategories = [];
if (query) {
// When searching, show all categories including hidden ones
$('.cdf-category-collapsible').addClass('show');
}
$('.cdf-extension-item').each(function () {
const ext = $(this).data('ext').toLowerCase();
const mime = $(this).find('.cdf-mime-type').text().toLowerCase();
const matches = ext.includes(query) || mime.includes(query);
$(this).toggleClass('hidden', !matches);
if (matches) {
const category = $(this).closest('.cdf-category').data('category');
if (!visibleCategories.includes(category)) {
visibleCategories.push(category);
}
}
});
$('.cdf-category').each(function () {
const category = $(this).data('category');
const hasVisibleItems = visibleCategories.includes(category);
if (!query) {
// When not searching, respect the collapsed state
if ($(this).hasClass('cdf-category-collapsible')) {
$(this).toggleClass('show', hiddenExpanded && hasVisibleItems);
} else {
$(this).toggleClass('hidden', !hasVisibleItems);
}
} else {
// When searching, show/hide based on matches
$(this).toggleClass('hidden', !hasVisibleItems);
}
});
});
// Select/Deselect all
$('.cdf-select-all').on('click', function () {
$checkboxes.filter(':visible').prop('checked', true);
updateCount();
});
$('.cdf-deselect-all').on('click', function () {
$checkboxes.filter(':visible').prop('checked', false);
updateCount();
});
// Update count on change
$checkboxes.on('change', updateCount);
});
</script>
<?php
}
/**
* Get mime category.
*
* @param string $mime Mime type.
* @return string
*/
private function get_mime_category( $mime ) {
if ( strpos( $mime, 'image/' ) === 0 ) {
return 'images';
}
if ( strpos( $mime, 'video/' ) === 0 ) {
return 'video';
}
if ( strpos( $mime, 'audio/' ) === 0 ) {
return 'audio';
}
if ( strpos( $mime, 'text/' ) === 0 ) {
return 'text';
}
if ( in_array( $mime, [ 'application/pdf', 'application/msword', 'application/vnd.ms-excel', 'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument' ], true ) || strpos( $mime, 'officedocument' ) !== false ) {
return 'documents';
}
if ( in_array( $mime, [ 'application/zip', 'application/x-rar-compressed', 'application/x-tar', 'application/x-7z-compressed' ], true ) ) {
return 'archives';
}
if ( strpos( $mime, 'font/' ) === 0 || in_array( $mime, [ 'application/font-woff', 'application/font-woff2', 'application/x-font-ttf' ], true ) ) {
return 'fonts';
}
return 'other';
}
/**
* Render settings page.
*/
public function settings_page_html() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<p class="description">
<?php esc_html_e( 'Configure which file types should be uploaded to custom folders. Each selected extension will be uploaded to /uploads/[extension]/ (e.g., /uploads/pdf/).', 'custom-document-folder' ); ?>
</p>
<form action="options.php" method="post">
<?php
settings_fields( 'cdf_plugin' );
do_settings_sections( 'cdf_plugin' );
submit_button( __( 'Save Settings', 'custom-document-folder' ) );
?>
</form>
</div>
<?php
}
/**
* Set custom upload folder.
*
* @param array $param An array of upload directory data with keys of 'path', 'url', 'subdir', 'basedir', and 'error'.
*
* @return array
*/
public function upload_dir( array $param ): array {
if ( ! in_array( $this->ext, $this->selected_extensions, true ) ) {
return $param;
}
$folder = '/' . $this->ext;
wp_mkdir_p( $param[ 'basedir' ] . $folder );
$param[ 'subdir' ] = $folder;
$param[ 'path' ] = $param[ 'basedir' ] . $param[ 'subdir' ];
$param[ 'url' ] = $param[ 'baseurl' ] . $param[ 'subdir' ];
return $param;
}
/**
* Handle upload prefilter.
*
* @param array $file An array of data for a single file.
*
* @return array
*/
public function wp_handle_upload_prefilter( array $file ): array {
$ext = pathinfo( $file[ 'name' ], \PATHINFO_EXTENSION );
if ( ! in_array( $ext, $this->selected_extensions, true ) ) {
return $file;
}
$this->ext = $ext;
return $file;
}
/**
* Check filetype and ext.
*
* @param array $data An array of data returned from the wp_check_filetype() function.
* @param string $file Full path to the file.
* @param string $filename The name of the file (may differ from $file due to $file being in a tmp directory).
* @param array|null $mimes Key is the file extension with value as the mime type.
*
* @return array
*/
public function wp_check_filetype_and_ext( array $data, string $file, string $filename, $mimes ): array { // phpcs:ignore
$ext = pathinfo( $filename, \PATHINFO_EXTENSION );
if ( ! in_array( $ext, $this->selected_extensions, true ) ) {
return $data;
}
$data[ 'ext' ] = $ext;
if ( empty( $data[ 'type' ] ) ) {
$allowed = get_allowed_mime_types();
foreach ( $allowed as $key => $mime ) {
if ( in_array( $ext, explode( '|', $key ), true ) ) {
$data[ 'type' ] = $mime;
break;
}
}
}
return $data;
}
/**
* Set the callback for handling non-unique file names.
*
* @param array $overrides An associative array of file upload overrides.
*
* @return array
*/
public function non_unique_filename( array $overrides ): array {
$overrides[ 'test_form' ] = false;
$overrides[ 'unique_filename_callback' ] = [ $this, 'non_unique_filename_callback' ];
return $overrides;
}
/**
* If the file extension is selected, remove old attachment.
*
* @param string $directory Directory path for file.
* @param string $filename File name.
* @param string $extension File extension.
*
* @return string
*/
public function non_unique_filename_callback( $directory, $filename, $extension ) {
if ( ! in_array( strtolower( $extension ), $this->selected_extensions, true ) ) {
return $filename;
}
$this->remove_old_attach( $filename );
return $filename;
}
/**
* Delete old attachment.
*
* @param string $filename File name.
*
* @return void
*/
public function remove_old_attach( $filename ) {
$arguments = [
'numberposts' => -1,
'meta_key' => '_wp_attached_file', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => $filename, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
'post_type' => 'attachment',
];
$attachments_to_remove = \get_posts( $arguments );
foreach ( $attachments_to_remove as $a ) {
\wp_delete_attachment( $a->ID, true );
}
}
/**
* Remove the old attachment.
*
* @param string $name File name.
*
* @return string
*/
public function filename_filter( string $name ): string {
$args = [
'numberposts' => -1,
'post_type' => 'attachment',
'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
[
'key' => '_wp_attached_file',
'value' => $name,
'compare' => 'LIKE',
],
],
];
$attachments_to_remove = \get_posts( $args );
foreach ( $attachments_to_remove as $attach ) {
\wp_delete_attachment( $attach->ID, true );
}
return $name;
}
}
new Upload_Document();