PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 2.2.44
Gallery : FooGallery v2.2.44
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-notices.php 3 years ago class-admin.php 3 years ago class-attachment-fields.php 3 years ago class-columns.php 3 years ago class-demo-content.php 3 years ago class-extensions.php 3 years ago class-gallery-attachment-modal.php 3 years ago class-gallery-datasources.php 3 years ago class-gallery-editor.php 3 years ago class-gallery-metabox-fields.php 3 years ago class-gallery-metabox-items.php 3 years ago class-gallery-metabox-settings-helper.php 3 years ago class-gallery-metabox-settings.php 3 years ago class-gallery-metaboxes.php 3 years ago class-menu.php 3 years ago class-pro-promotion.php 3 years ago class-settings.php 3 years ago class-silent-installer-skin.php 3 years ago demo-content-galleries.php 3 years ago demo-content-images.php 3 years ago index.php 3 years ago view-extensions.php 3 years ago view-help-demos.php 3 years ago view-help-getting-started.php 3 years ago view-help-pro.php 3 years ago view-help.php 3 years ago view-system-info.php 3 years ago
class-attachment-fields.php
224 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 = apply_filters( 'foogallery_attachment_field_custom_target_options', array(
19 'default' => __( 'Default', 'foogallery' ),
20 '_blank' => __( 'New tab (_blank)', 'foogallery' ),
21 '_self' => __( 'Same tab (_self)', 'foogallery' ),
22 'foobox' => __( 'FooBox', 'foogallery' )
23 ) );
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
42 //original filter without $post
43 $fields = apply_filters( 'foogallery_attachment_custom_fields', $fields );
44
45 //newer filter including the $post
46 $fields = apply_filters( 'foogallery_attachment_custom_fields_with_post', $fields, $post );
47
48 return $fields;
49 }
50
51 /**
52 * @param $form_fields
53 * @param WP_Post $post
54 *
55 * @return mixed
56 */
57 public function add_fields( $form_fields, $post = null ) {
58 $custom_fields = $this->get_custom_fields();
59
60 // If our fields array is not empty
61 if ( ! empty( $custom_fields ) ) {
62 // We browse our set of options
63 foreach ( $custom_fields as $field => $values ) {
64 //remove any help, as it just looks untidy
65 if ( isset( $values['helps'] ) ) {
66 unset( $values['helps'] );
67 }
68
69 if ( empty( $values['exclusions'] ) ) {
70 $values['exclusions'] = array();
71 }
72
73 // If the field matches the current attachment mime type
74 // and is not one of the exclusions
75 if ( !in_array( $post->post_mime_type, $values['exclusions'] ) ) {
76 // We get the already saved field meta value
77 $meta = apply_filters( 'foogallery_attachment_custom_field_value', get_post_meta( $post->ID, '_' . $field, true ), $post->ID, $field, $values );
78
79 switch ( $values['input'] ) {
80 default:
81 case 'text':
82 $values['input'] = 'text';
83 break;
84
85 case 'textarea':
86 $values['input'] = 'textarea';
87 break;
88
89 case 'select':
90
91 // Select type doesn't exist, so we will create the html manually
92 // For this, we have to set the input type to 'html'
93 $values['input'] = 'html';
94
95 // Create the select element with the right name (matches the one that wordpress creates for custom fields)
96 $html = '<select name="attachments[' . $post->ID . '][' . $field . ']">';
97
98 // If options array is passed
99 if ( isset( $values['options'] ) ) {
100 // Browse and add the options
101 foreach ( $values['options'] as $k => $v ) {
102 // Set the option selected or not
103 if ( $meta == $k )
104 $selected = ' selected="selected"';
105 else
106 $selected = '';
107
108 $html .= '<option' . $selected . ' value="' . $k . '">' . $v . '</option>';
109 }
110 }
111
112 $html .= '</select>';
113
114 // Set the html content
115 $values['html'] = $html;
116
117 break;
118
119 case 'checkbox':
120
121 // Checkbox type doesn't exist either
122 $values['input'] = 'html';
123
124 // Set the checkbox checked or not
125 if ( $meta == 'on' )
126 $checked = ' checked="checked"';
127 else
128 $checked = '';
129
130 $html = '<input' . $checked . ' type="checkbox" name="attachments[' . $post->ID . '][' . $field . ']" id="attachments-' . $post->ID . '-' . $field . '" />';
131
132 $values['html'] = $html;
133
134 break;
135
136 case 'radio':
137
138 // radio type doesn't exist either
139 $values['input'] = 'html';
140
141 $html = '';
142
143 if ( ! empty( $values['options'] ) ) {
144 $i = 0;
145
146 foreach ( $values['options'] as $k => $v ) {
147 if ( $meta == $k )
148 $checked = ' checked="checked"';
149 else
150 $checked = '';
151
152 $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 />';
153 $i++;
154 }
155 }
156
157 $values['html'] = $html;
158
159 break;
160
161 case 'html':
162 $values['input'] = 'html';
163 $values['html'] = '';
164 break;
165 }
166
167 // And set it to the field before building it
168 $values['value'] = $meta;
169
170 // We add our field into the $form_fields array
171 $filtered_field = apply_filters( 'foogallery_attachment_field_' . $field, $values, $post->ID );
172 $filtered_field = apply_filters( 'foogallery_attachment_field_' . $field . '_with_post', $values, $field, $post );
173 $form_fields[$field] = $filtered_field;
174 }
175 }
176 }
177
178 //allow it to change
179 $form_fields = apply_filters( 'foogallery_attachment_add_fields', $form_fields );
180
181 // We return the completed $form_fields array
182 return $form_fields;
183 }
184
185 function save_fields( $post, $attachment ) {
186 $custom_fields = $this->get_custom_fields();
187
188 // If our fields array is not empty
189 if ( ! empty( $custom_fields ) ) {
190 // We browse our set of options
191 foreach ( $custom_fields as $field => $values ) {
192 switch ( $values['input'] ) {
193 case 'text':
194 case 'textarea':
195 case 'select':
196 case 'radio':
197 case 'checkbox':
198 // If this field has been submitted (is present in the $attachment variable)
199 if ( isset( $attachment[$field] ) ) {
200 // If submitted field is empty
201 // We add errors to the post object with the "error_text" parameter if set in the options
202 if ( strlen( trim( $attachment[$field] ) ) == 0 && isset( $values['error_text'] ) ) {
203 $post['errors'][ $field ]['errors'][] = __( $values['error_text'] );
204 // Otherwise we update the custom field
205 } else {
206 update_post_meta( $post['ID'], '_' . $field, $attachment[ $field ] );
207 }
208 }
209 // Otherwise, we delete it if it already existed
210 else {
211 delete_post_meta( $post['ID'], $field );
212 }
213 break;
214
215 default:
216 do_action( 'foogallery_attachment_save_field_' . $values['input'], $field, $values, $post, $attachment);
217 }
218 }
219 }
220
221 return $post;
222 }
223 }
224 }