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