PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.3.0
Gallery : FooGallery v3.3.0
3.3.0 3.1.31 3.1.32 3.1.34 3.1.36 3.2.0 3.2.6 trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / includes / admin / class-attachment-fields.php
foogallery / includes / admin Last commit date
class-admin-notice-custom-css.php 1 week ago class-admin-notices.php 1 week ago class-admin.php 1 week ago class-attachment-fields.php 1 week ago class-columns.php 1 week ago class-demo-content.php 1 week ago class-extensions.php 1 week ago class-gallery-attachment-modal.php 1 week ago class-gallery-datasources.php 1 week ago class-gallery-editor.php 1 week ago class-gallery-metabox-fields.php 1 week ago class-gallery-metabox-items.php 1 week ago class-gallery-metabox-settings-helper.php 1 week ago class-gallery-metabox-settings.php 1 week ago class-gallery-metabox-template.php 1 week ago class-gallery-metaboxes.php 1 week ago class-menu.php 1 week ago class-pro-promotion.php 1 week ago class-settings.php 1 week ago class-silent-installer-skin.php 1 week ago class-trial-mode.php 1 week ago demo-content-galleries.php 1 week ago demo-content-images.php 1 week ago index.php 1 week ago pro-features.php 1 week ago view-features.php 1 week ago view-help-demos.php 1 week ago view-help-getting-started.php 1 week ago view-help-pro.php 1 week ago view-help.php 1 week ago view-system-info.php 1 week ago
class-attachment-fields.php
260 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 /**
8 * class FooGallery_Attachment_Fields
9 *
10 * Add custom fields to media attachments
11 */
12 if (!class_exists('FooGallery_Attachment_Fields')) {
13
14 class FooGallery_Attachment_Fields {
15
16 function __construct() {
17 add_filter( 'attachment_fields_to_edit', array( $this, 'add_fields' ), 9, 2 );
18 add_filter( 'attachment_fields_to_save', array( $this, 'save_fields' ), 11, 2 );
19 }
20
21 public function get_custom_fields( $post = null ) {
22
23 $target_options = foogallery_get_target_options();
24
25 $fields = array(
26 'foogallery_custom_url' => array(
27 'label' => __( 'Custom URL', 'foogallery' ),
28 'input' => 'text', //other types are 'textarea', 'checkbox', 'radio', 'select',
29 'helps' => __( 'Point your attachment to a custom URL', 'foogallery' ),
30 'exclusions' => array( 'audio', 'video' ),
31 ),
32
33 'foogallery_custom_target' => array(
34 'label' => __( 'Custom Target', 'foogallery' ),
35 'input' => 'select',
36 'helps' => __( 'Set a custom target for your attachment', 'foogallery' ),
37 'exclusions' => array( 'audio', 'video' ),
38 'options' => $target_options
39 ),
40
41 'foogallery_custom_rel' => array(
42 'label' => __( 'Custom Rel', 'foogallery' ),
43 'input' => 'text',
44 'helps' => __( 'Set rel values for custom URL links (for example: nofollow sponsored).', 'foogallery' ),
45 'exclusions' => array( 'audio', 'video' ),
46 )
47 );
48
49 //original filter without $post
50 $fields = apply_filters( 'foogallery_attachment_custom_fields', $fields );
51
52 //newer filter including the $post
53 $fields = apply_filters( 'foogallery_attachment_custom_fields_with_post', $fields, $post );
54
55 return $fields;
56 }
57
58 /**
59 * @param $form_fields
60 * @param WP_Post $post
61 *
62 * @return mixed
63 */
64 public function add_fields( $form_fields, $post = null ) {
65 $custom_fields = $this->get_custom_fields();
66
67 // If $post is null, set it to the global post object
68 if ( is_null( $post ) ) {
69 $post = get_post();
70 }
71
72 if ( ! is_null( $post ) ) {
73 // If our fields array is not empty
74 if ( ! empty( $custom_fields ) ) {
75 // We browse our set of options
76 foreach ( $custom_fields as $field => $values ) {
77 //remove any help, as it just looks untidy
78 if ( isset( $values['helps'] ) ) {
79 unset( $values['helps'] );
80 }
81
82 if ( empty( $values['exclusions'] ) ) {
83 $values['exclusions'] = array();
84 }
85
86 // If the field matches the current attachment mime type
87 // and is not one of the exclusions
88 if ( ! in_array( $post->post_mime_type, $values['exclusions'] ) ) {
89 // We get the already saved field meta value
90 // Check if $post->ID is set before accessing its properties
91 $meta = apply_filters( 'foogallery_attachment_custom_field_value', get_post_meta( isset( $post->ID ) ? $post->ID : 0, '_' . $field, true ), isset( $post->ID ) ? $post->ID : 0, $field, $values );
92
93 switch ( $values['input'] ) {
94 default:
95 case 'text':
96 $values['input'] = 'text';
97 break;
98
99 case 'textarea':
100 $values['input'] = 'textarea';
101 break;
102
103 case 'select':
104
105 // Select type doesn't exist, so we will create the html manually
106 // For this, we have to set the input type to 'html'
107 $values['input'] = 'html';
108
109 // Create the select element with the right name (matches the one that wordpress creates for custom fields)
110 $html = '<select name="attachments[' . $post->ID . '][' . $field . ']">';
111
112 // If options array is passed
113 if ( isset( $values['options'] ) ) {
114 // Browse and add the options
115 foreach ( $values['options'] as $k => $v ) {
116 // Set the option selected or not
117 if ( $meta == $k )
118 $selected = ' selected="selected"';
119 else
120 $selected = '';
121
122 $html .= '<option' . $selected . ' value="' . $k . '">' . $v . '</option>';
123 }
124 }
125
126 $html .= '</select>';
127
128 // Set the html content
129 $values['html'] = $html;
130
131 break;
132
133 case 'checkbox':
134
135 // Checkbox type doesn't exist either
136 $values['input'] = 'html';
137
138 // Set the checkbox checked or not
139 if ( $meta == 'on' )
140 $checked = ' checked="checked"';
141 else
142 $checked = '';
143
144 $html = '<input' . $checked . ' type="checkbox" name="attachments[' . $post->ID . '][' . $field . ']" id="attachments-' . $post->ID . '-' . $field . '" />';
145
146 $values['html'] = $html;
147
148 break;
149
150 case 'radio':
151
152 // radio type doesn't exist either
153 $values['input'] = 'html';
154
155 $html = '';
156
157 if ( ! empty( $values['options'] ) ) {
158 $i = 0;
159
160 foreach ( $values['options'] as $k => $v ) {
161 if ( $meta == $k )
162 $checked = ' checked="checked"';
163 else
164 $checked = '';
165
166 $html .= '<input' . $checked . ' value="' . $k . '" type="radio" name="attachments[' . $post->ID . '][' . $field . ']" id="' . sanitize_key( $field . '_' . $post->ID . '_' . $i ) . '" /> <label for="' . sanitize_key( $field . '_' . $post->ID . '_' . $i ) . '">' . $v . '</label><br />';
167 $i++;
168 }
169 }
170
171 $values['html'] = $html;
172
173 break;
174
175 case 'html':
176 $values['input'] = 'html';
177 $values['html'] = '';
178 break;
179 }
180
181 // And set it to the field before building it
182 $values['value'] = $meta;
183
184 // We add our field into the $form_fields array
185 $filtered_field = apply_filters( 'foogallery_attachment_field_' . $field, $values, $post->ID );
186 $filtered_field = apply_filters( 'foogallery_attachment_field_' . $field . '_with_post', $values, $field, $post );
187 $form_fields[$field] = $filtered_field;
188 }
189 }
190 }
191 }
192
193 //allow it to change
194 $form_fields = apply_filters( 'foogallery_attachment_add_fields', $form_fields );
195
196 // We return the completed $form_fields array
197 return $form_fields;
198 }
199
200 function save_fields( $post, $attachment ) {
201 $custom_fields = $this->get_custom_fields();
202
203 // If our fields array is not empty
204 if ( ! empty( $custom_fields ) ) {
205
206 $post_id = absint( $post['ID'] );
207
208 // get out early if we don't have a post id or we don't have permission to edit it.
209 if ( $post_id === 0 || !current_user_can( 'edit_post', $post_id ) ) {
210 return $post;
211 }
212
213 // We browse our set of options
214 foreach ( $custom_fields as $field => $values ) {
215 switch ( $values['input'] ) {
216 case 'text':
217 case 'textarea':
218 case 'select':
219 case 'radio':
220 case 'checkbox':
221 // If this field has been submitted (is present in the $attachment variable)
222 if ( isset( $attachment[$field] ) ) {
223 $value = wp_unslash( $attachment[ $field ] );
224
225 if ( 'foogallery_custom_url' === $field ) {
226 $value = foogallery_sanitize_attachment_custom_url( $value );
227 } elseif ( 'foogallery_custom_target' === $field ) {
228 $value = foogallery_sanitize_attachment_custom_target( $value );
229 } elseif ( 'foogallery_custom_rel' === $field ) {
230 $value = foogallery_sanitize_attachment_custom_rel( $value );
231 } else {
232 $value = sanitize_text_field( $value );
233 }
234
235 // If submitted field is empty
236 // We add errors to the post object with the "error_text" parameter if set in the options
237 if ( strlen( trim( $value ) ) == 0 && isset( $values['error_text'] ) ) {
238 $post['errors'][ $field ]['errors'][] = __( $values['error_text'] );
239 // Otherwise we update the custom field
240 } else {
241 update_post_meta( $post_id, '_' . $field, $value );
242 }
243 }
244 // Otherwise, we delete it if it already existed
245 else {
246 delete_post_meta( $post_id, '_' . $field );
247 }
248 break;
249
250 default:
251 do_action( 'foogallery_attachment_save_field_' . $values['input'], $field, $values, $post, $attachment);
252 }
253 }
254 }
255
256 return $post;
257 }
258 }
259 }
260