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