PluginProbe
Smart Custom Fields / trunk
Smart Custom Fields vtrunk
1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.7.0 2.0.0 2.0.1 2.1.0 2.1.1 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 All 68 releases
smart-custom-fields / classes / controller / class.controller-base.php

class.controller-base.php in Smart Custom Fields trunk, at classes/controller/class.controller-base.php

326 lines 10.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package smart-custom-fields
4 * @author inc2734
5 * @license GPL-2.0+
6 */
7
8 /**
9 * Smart_Custom_Fields_Controller_Base class.
10 */
11 class Smart_Custom_Fields_Controller_Base {
12
13 /**
14 * Array of the form field objects
15 *
16 * @var array
17 */
18 protected $fields = array();
19
20 /**
21 * __construct
22 */
23 public function __construct() {
24 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
25 }
26
27 /**
28 * Loading resources for edit page.
29 *
30 * @param string $hook The current admin page.
31 */
32 public function admin_enqueue_scripts( $hook ) {
33 do_action( SCF_Config::PREFIX . 'before-editor-enqueue-scripts', $hook );
34 wp_enqueue_style(
35 SCF_Config::PREFIX . 'editor',
36 SMART_CUSTOM_FIELDS_URL . '/css/editor.css',
37 array(),
38 filemtime( SMART_CUSTOM_FIELDS_PATH . '/css/editor.css' )
39 );
40 wp_enqueue_media();
41 wp_enqueue_script(
42 SCF_Config::PREFIX . 'editor',
43 SMART_CUSTOM_FIELDS_URL . '/js/editor.js',
44 array( 'jquery' ),
45 filemtime( SMART_CUSTOM_FIELDS_PATH . '/js/editor.js' ),
46 true
47 );
48 wp_localize_script(
49 SCF_Config::PREFIX . 'editor',
50 'smart_cf_uploader',
51 array(
52 'image_uploader_title' => esc_html__( 'Image setting', 'smart-custom-fields' ),
53 'file_uploader_title' => esc_html__( 'File setting', 'smart-custom-fields' ),
54 )
55 );
56 do_action( SCF_Config::PREFIX . 'after-editor-enqueue-scripts', $hook );
57
58 if ( ! user_can_richedit() ) {
59 wp_enqueue_script(
60 'tinymce',
61 SMART_CUSTOM_FIELDS_URL . '/js/tinymce/tinymce.min.js',
62 array(),
63 filemtime( SMART_CUSTOM_FIELDS_PATH . '/js/tinymce/tinymce.min.js' ),
64 true
65 );
66 }
67 }
68
69 /**
70 * Display custom fields in edit page.
71 *
72 * @param WP_Post|WP_User|WP_Term|stdClass $wp_object Object meta object.
73 * @param array $callback_args Custom field setting information.
74 */
75 public function display_meta_box( $wp_object, $callback_args ) {
76 $groups = $callback_args['args'];
77 $tables = $this->get_tables( $wp_object, $groups );
78
79 printf( '<div class="%s">', esc_attr( SCF_Config::PREFIX . 'meta-box' ) );
80 $index = 0;
81 foreach ( $tables as $group_key => $group ) {
82 $is_repeatable = $group->is_repeatable();
83 if ( $is_repeatable && 0 === $index ) {
84 printf(
85 '<div class="%s">',
86 esc_attr( SCF_Config::PREFIX . 'meta-box-repeat-tables' )
87 );
88 $this->display_tr( $wp_object, $is_repeatable, $group->get_fields() );
89 }
90 $this->display_tr( $wp_object, $is_repeatable, $group->get_fields(), $index );
91
92 // If in the loop, count up the index.
93 // If exit the loop, reset the count.
94 if (
95 $is_repeatable
96 && isset( $tables[ $group_key + 1 ] )
97 && $group->get_name() === $tables[ $group_key + 1 ]->get_name()
98 ) {
99 ++$index;
100 } else {
101 $index = 0;
102 }
103 if ( $is_repeatable && 0 === $index ) {
104 printf( '</div>' );
105 }
106 }
107 printf( '</div>' );
108 wp_nonce_field( SCF_Config::NAME . '-fields', SCF_Config::PREFIX . 'fields-nonce' );
109 }
110
111 /**
112 * Saving posted data.
113 *
114 * @param array $data Data.
115 * @param WP_Post|WP_User|WP_Term|stdClass $wp_object Object meta object.
116 */
117 protected function save( $data, $wp_object ) {
118 check_admin_referer(
119 SCF_Config::NAME . '-fields',
120 SCF_Config::PREFIX . 'fields-nonce'
121 );
122
123 $meta = new Smart_Custom_Fields_Meta( $wp_object );
124 $meta->save( $data );
125 }
126
127 /**
128 * Generating array for displaying the custom fields.
129 *
130 * @param WP_Post|WP_User|WP_Term|stdClass $wp_object Object meta object.
131 * @param array $groups Settings from custom field settings page.
132 * @return array Array for displaying a table for custom fields.
133 */
134 protected function get_tables( $wp_object, $groups ) {
135 $meta = new Smart_Custom_Fields_Meta( $wp_object );
136
137 $repeat_multiple_data = SCF::get_repeat_multiple_data( $wp_object );
138 $tables = array();
139 foreach ( $groups as $group ) {
140 // If in the loop, Added groupgs by the amount of the loop.
141 // Added only one if setting is repetition but not loop (Ex, new registration)
142 if ( true === $group->is_repeatable() ) {
143 $loop_count = 1;
144 $fields = $group->get_fields();
145 foreach ( $fields as $field ) {
146 $field_name = $field->get( 'name' );
147 $meta_value = $meta->get( $field_name );
148 if ( is_array( $meta_value ) ) {
149 $meta_count = count( $meta_value );
150 // When the same name of the custom field is a multiple (checbox or loop)
151 if ( $meta_count > 1 ) {
152 // checkbox
153 if ( $field->get_attribute( 'allow-multiple-data' ) ) {
154 if ( is_array( $repeat_multiple_data ) && isset( $repeat_multiple_data[ $field_name ] ) ) {
155 $repeat_multiple_data_count = count( $repeat_multiple_data[ $field_name ] );
156 if ( $loop_count < $repeat_multiple_data_count ) {
157 $loop_count = $repeat_multiple_data_count;
158 }
159 }
160 } elseif ( $loop_count < $meta_count ) {
161 // other than checkbox
162 $loop_count = $meta_count;
163 }
164 }
165 }
166 }
167 if ( $loop_count >= 1 ) {
168 for ( $i = $loop_count; $i > 0; $i-- ) {
169 $tables[] = $group;
170 }
171 continue;
172 }
173 }
174 $tables[] = $group;
175 }
176 return $tables;
177 }
178
179 /**
180 * Getting the multi-value field meta data.
181 *
182 * @param WP_Post|WP_User|WP_Term|stdClass $wp_object Object meta object.
183 * @param Smart_Custom_Fields_Field_Base $field Field object.
184 * @param int $index Index of value.
185 * @return array
186 */
187 public function get_multiple_data_field_value( $wp_object, $field, $index ) {
188 $meta = new Smart_Custom_Fields_Meta( $wp_object );
189 $field_name = $field->get( 'name' );
190
191 if ( is_null( $index ) ) {
192 return SCF::get_default_value( $field );
193 }
194
195 if ( ! $meta->is_saved_the_key( $field_name ) ) {
196 return SCF::get_default_value( $field );
197 }
198
199 $value = $meta->get( $field_name );
200
201 // in the loop
202 $repeat_multiple_data = SCF::get_repeat_multiple_data( $wp_object );
203 if ( is_array( $repeat_multiple_data ) && isset( $repeat_multiple_data[ $field_name ] ) ) {
204 $now_num = 0;
205 if ( is_array( $repeat_multiple_data[ $field_name ] ) && isset( $repeat_multiple_data[ $field_name ][ $index ] ) ) {
206 $now_num = $repeat_multiple_data[ $field_name ][ $index ];
207 }
208
209 // The index is starting point to refer to the total of the previous number than me ($index)
210 $_temp = array_slice( $repeat_multiple_data[ $field_name ], 0, $index );
211 $sum = array_sum( $_temp );
212 $start = $sum;
213
214 if ( $now_num ) {
215 $value = array_slice( $value, $start, $now_num );
216 } else {
217 $value = array();
218 }
219 }
220 return $value;
221 }
222
223 /**
224 * Getting the non multi-value field meta data.
225 *
226 * @param WP_Post|WP_User|WP_Term|stdClass $wp_object Object meta object.
227 * @param Smart_Custom_Fields_Field_Base $field Field object.
228 * @param int $index Index of value.
229 * @return string
230 */
231 public function get_single_data_field_value( $wp_object, $field, $index ) {
232 $meta = new Smart_Custom_Fields_Meta( $wp_object );
233 $field_name = $field->get( 'name' );
234
235 if ( is_null( $index ) ) {
236 return SCF::get_default_value( $field, true );
237 }
238
239 if ( $meta->is_saved_the_key( $field_name ) ) {
240 $value = $meta->get( $field_name );
241 if ( isset( $value[ $index ] ) ) {
242 return $value[ $index ];
243 }
244 return '';
245 }
246 return SCF::get_default_value( $field, true );
247 }
248
249 /**
250 * Displaying tr element for table of custom fields.
251 *
252 * @param WP_Post|WP_User|WP_Term|stdClass $wp_object Object meta object.
253 * @param bool $is_repeat If repeat, return true.
254 * @param array $fields Fields.
255 * @param int|null $index Field index.
256 */
257 protected function display_tr( $wp_object, $is_repeat, $fields, $index = null ) {
258 $btn_repeat = '';
259 if ( $is_repeat ) {
260 $btn_repeat = sprintf(
261 '<span class="%s"></span>',
262 esc_attr( SCF_Config::PREFIX . 'icon-handle dashicons dashicons-menu' )
263 );
264 $btn_repeat .= '<span class="btn-add-repeat-group dashicons dashicons-plus-alt ' . SCF_Config::PREFIX . 'repeat-btn"></span>';
265 $btn_repeat .= ' <span class="btn-remove-repeat-group dashicons dashicons-dismiss ' . SCF_Config::PREFIX . 'repeat-btn"></span>';
266 }
267
268 printf(
269 '<div class="%s" %s>%s',
270 esc_attr( SCF_Config::PREFIX . 'meta-box-table' ),
271 is_null( $index ) ? 'style="display: none;"' : '',
272 $btn_repeat // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
273 );
274
275 foreach ( $fields as $field ) {
276 $field_type = $field->get_attribute( 'type' ); // gets the field type for use in aditional CSS classes
277 $layout = $field->get_attribute( 'layout' ); // get layout type
278 $field_name = $field->get( 'name' );
279 $field_label = $field->get( 'label' );
280 if ( ! $field_label ) {
281 $field_label = $field_name;
282 }
283
284 if ( $field->get_attribute( 'allow-multiple-data' ) ) {
285 // When multi-value field
286 $value = $this->get_multiple_data_field_value( $wp_object, $field, $index );
287 } else {
288 // When non multi-value field
289 $value = $this->get_single_data_field_value( $wp_object, $field, $index );
290 }
291
292 $instruction = $field->get( 'instruction' );
293 if ( ! empty( $instruction ) ) {
294 if ( apply_filters( SCF_Config::PREFIX . 'instruction-apply-html', false ) === true ) {
295 $instruction_html = $instruction;
296 } else {
297 $instruction_html = esc_html( $instruction );
298 }
299 $instruction = sprintf(
300 '<div class="instruction">%s</div>',
301 $instruction_html
302 );
303 }
304
305 printf(
306 '<table class="%1$sfield-type-%6$s %1$slayout-type-%7$s"><tr>
307 %2$s
308 <td>
309 %3$s
310 %4$s
311 %5$s
312 </td>
313 </tr></table>',
314 esc_attr( SCF_Config::PREFIX ),
315 'full-width' !== $layout ? '<th>' . esc_html( $field_label ) . '</th>' : '',
316 $instruction, // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
317 $field->get_field( $index, $value ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
318 $field->get( 'notes' ) ? '<p class="description">' . esc_html( $field->get( 'notes' ) ) . '</p>' : '',
319 esc_attr( $field_type ),
320 esc_attr( $layout )
321 );
322 }
323 echo '</div>';
324 }
325 }
326