class-media-library-organizer-upload.php
5.79 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
<?php
/**
* Upload class.
*
* @package Media_Library_Organizer
* @author WP Media Library
*/
/**
* Registers actions and filters for uploading to the Media Library.
*
* @since 1.0.0
*/
class Media_Library_Organizer_Upload {
/**
* Holds the base class object.
*
* @since 1.0.5
*
* @var object
*/
public $base;
/**
* Constructor
*
* @since 1.0.5
*
* @param object $base Base Plugin Class.
*/
public function __construct( $base ) {
// Store base class.
$this->base = $base;
// Prepend the UI.
add_action( 'pre-upload-ui', array( $this, 'output_upload_ui' ) );
// Define the pluploader's options.
add_filter( 'plupload_init', array( $this, 'plupload_options' ) );
// Run actions just before an attachment is added.
add_filter( 'wp_insert_attachment_data', array( $this, 'filter_attachment_data_before_save' ), 10, 2 );
// Run actions when an attachment is uploaded.
add_action( 'add_attachment', array( $this, 'add_attachment' ) );
}
/**
* Allows Addons to output below the HTML and JS uploaders at Media > Add New
*
* @since 1.0.5
*/
public function output_upload_ui() {
/**
* Allows Addons to output below the HTML and JS uploaders at Media > Add New
*
* @since 1.0.5
*/
do_action( 'media_library_organizer_upload_output_upload_ui' );
}
/**
* Define the pluploader's options.
*
* @since 1.0.5
*
* @param array $options Plupload Options.
* @return array Plupload Options
*/
public function plupload_options( $options ) {
// Iterate through Registered Taxonomies.
$fields = array();
foreach ( $this->base->get_class( 'taxonomies' )->get_taxonomies() as $taxonomy_name => $taxonomy ) {
// Define form fields to send with file uploads.
// The field values must be updated when the user updates the form, which is done at assets/js/upload.js.
$fields[ $taxonomy_name ] = $this->base->get_class( 'media' )->get_selected_terms_slugs( $taxonomy_name );
}
// Define a multipart_params array if not defined.
if ( ! is_array( $options['multipart_params'] ) ) {
$options['multipart_params'] = array();
}
// Assign fields to multipart_params.
$options['multipart_params']['media_library_organizer'] = $fields;
/**
* Define the pluploader's options.
*
* @since 1.0.5
*
* @param array $options Plupload Options.
*/
$options = apply_filters( 'media_library_organizer_upload_plupload_options', $options );
// Return.
return $options;
}
/**
* Filters attachment post data before it is saved to the WordPress Media Library, for new
* and existing Attachments.
*
* @since 1.1.9
*
* @param array $data Slashed, sanitized, and processed attachment post data.
* @param array $unprocessed_data Slashed and sanitized attachment post data, but not processed.
* @return array Attachment Post Data
*/
public function filter_attachment_data_before_save( $data, $unprocessed_data ) {
// Determine if we're saving Attachment data for a new upload or an existing file.
if ( isset( $data['ID'] ) && ! empty( $data['ID'] ) ) {
/**
* Filters attachment post data for an existing Attachment before it is saved to the WordPress Media Library.
*
* @since 1.1.9
*
* @param array $data Slashed, sanitized, and processed attachment post data.
* @param array $unprocessed_data Slashed and sanitized attachment post data, but not processed.
*/
$data = apply_filters( 'media_library_organizer_upload_filter_existing_attachment_data_before_save', $data, $unprocessed_data, $data['ID'] );
} else {
/**
* Filters attachment post data before a new Attachment is saved to the WordPress Media Library.
*
* @since 1.1.9
*
* @param array $data Slashed, sanitized, and processed attachment post data.
* @param array $unprocessed_data Slashed and sanitized attachment post data, but not processed.
*/
$data = apply_filters( 'media_library_organizer_upload_filter_new_attachment_data_before_save', $data, $unprocessed_data );
}
return $data;
}
/**
* Allows Addons to run actions on an attachment that has just been
* uploaded to the WordPress Media Library.
*
* @since 1.0.5
*
* @param int $attachment_id Attachment ID.
*/
public function add_attachment( $attachment_id ) {
// Get Attachment.
$attachment = new Media_Library_Organizer_Attachment( $attachment_id );
// Iterate through Registered Taxonomies.
foreach ( $this->base->get_class( 'taxonomies' )->get_taxonomies() as $taxonomy_name => $taxonomy ) {
// Conditionally set Media Categories, as they won't be included in the request if no checkboxes were selected.
if ( ! isset( $_REQUEST['media_library_organizer'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
continue;
}
// Fetch request.
$request = stripslashes_deep( $_REQUEST['media_library_organizer'] ); // phpcs:ignore WordPress.Security.NonceVerification
// Skip if the Taxonomy isn't specified.
if ( ! isset( $request[ $taxonomy_name ] ) ) {
continue;
}
if ( empty( $request[ $taxonomy_name ] ) ) {
continue;
}
// Set Terms.
$term = get_term_by( 'slug', sanitize_text_field( $request[ $taxonomy_name ] ), $taxonomy_name );
if ( ! $term ) {
continue;
}
$attachment->set_terms( $taxonomy_name, array( $term->term_id ) );
}
// Update the Attachment.
$attachment->update();
// Destroy the class.
unset( $attachment );
/**
* Allows Addons to run actions on an attachment that has just been
* uploaded to the WordPress Media Library
*
* @since 1.0.5
*
* @param int $attachment_id Attachment ID.
*/
do_action( 'media_library_organizer_upload_add_attachment', $attachment_id );
}
}