PageRenderTime 3ms CodeModel.GetById 2ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 1ms

/wp-content/plugins/types/embedded/includes/fields/checkboxes.php

https://github.com/ngocphuuit/wordpress-tax
PHP | 359 lines | 238 code | 18 blank | 103 comment | 80 complexity | 0c0fa5746a885e6fd71b09ae2d6a8d85 MD5 | raw file
  1<?php
  2add_filter( 'wpcf_relationship_meta_form',
  3        'wpcf_filds_checkboxes_relationship_form_filter' );
  4
  5/**
  6 * Register data (called automatically).
  7 * 
  8 * @return type 
  9 */
 10function wpcf_fields_checkboxes() {
 11    return array(
 12        'id' => 'wpcf-checkboxes',
 13        'title' => __( 'Checkboxes', 'wpcf' ),
 14        'description' => __( 'Checkboxes', 'wpcf' ),
 15//        'validate' => array('required'),
 16        'meta_key_type' => 'BINARY',
 17    );
 18}
 19
 20// Add filter when using wpv_condition()
 21add_filter( 'wpv_condition', 'wpcf_fields_checkboxes_wpv_conditional_trigger' );
 22add_filter( 'wpv_condition_end',
 23        'wpcf_fields_checkboxes_wpv_conditional_trigger_end' );
 24
 25/**
 26 * Form data for post edit page.
 27 * 
 28 * @param type $field 
 29 */
 30function wpcf_fields_checkboxes_meta_box_form( $field, $field_object ) {
 31    $options = array();
 32    if ( !empty( $field['data']['options'] ) ) {
 33        global $pagenow;
 34        foreach ( $field['data']['options'] as $option_key => $option ) {
 35            // Set value
 36            $options[$option_key] = array(
 37                '#value' => $option['set_value'],
 38                '#title' => wpcf_translate( 'field ' . $field['id'] . ' option '
 39                        . $option_key . ' title', $option['title'] ),
 40                '#default_value' => (!empty( $field['value'][$option_key] )// Also check new post
 41                || ($pagenow == 'post-new.php' && !empty( $option['checked'] ))) ? 1 : 0,
 42                '#name' => 'wpcf[' . $field['id'] . '][' . $option_key . ']',
 43                '#id' => $option_key . '_'
 44                . wpcf_unique_id( serialize( $field ) ),
 45                '#inline' => true,
 46                '#after' => '<br />',
 47            );
 48        }
 49    }
 50    return array(
 51        '#type' => 'checkboxes',
 52        '#options' => $options,
 53    );
 54}
 55
 56/**
 57 * Editor callback form.
 58 */
 59function wpcf_fields_checkboxes_editor_callback( $field, $settings ) {
 60    $data = array();
 61    if ( !empty( $field['data']['options'] ) ) {
 62        $index = 0;
 63        foreach ( $field['data']['options'] as $option_key => $option ) {
 64            $data['checkboxes'][$option_key] = array(
 65                'id' => $option_key,
 66                'title' => $option['title'],
 67                'selected' => isset( $settings['options'][$index]['selected'] ) ? $settings['options'][$index]['selected'] : htmlspecialchars( stripslashes( strval( ( $option['display_value_selected'] )))),
 68                'not_selected' => isset( $settings['options'][$index]['not_selected'] ) ? $settings['options'][$index]['not_selected'] : htmlspecialchars(stripslashes( strval(  $option['display_value_not_selected'] ))),
 69            );
 70            $index++;
 71        }
 72    }
 73    return array(
 74        'supports' => array('style'),
 75        'tabs' => array(
 76            'display' => array(
 77                'menu_title' => __( 'Display', 'wpcf' ),
 78                'title' => __( 'Display', 'wpcf' ),
 79                'content' => WPCF_Loader::template( 'editor-modal-checkboxes',
 80                        $data ),
 81            )
 82        )
 83    );
 84}
 85
 86/**
 87 * Editor callback form submit.
 88 */
 89function wpcf_fields_checkboxes_editor_submit( $data, $field, $context ) {
 90    $add = '';
 91    $types_attr = $context == 'usermeta' ? 'usermeta' : 'field';
 92    $shortcode = '';
 93    if ( $context == 'usermeta' ) {
 94        $add .= wpcf_get_usermeta_form_addon_submit();
 95    }
 96    if ( !empty( $data['options'] ) ) {
 97        if ( $data['display'] == 'display_all' ) {
 98            $separator = !empty( $data['cbs_separator'] ) ? $data['cbs_separator'] : '';
 99            $_add = $add . ' separator="' . $separator . '"';
100            if ( $context == 'usermeta' ) {
101                $shortcode .= wpcf_usermeta_get_shortcode( $field, $_add );
102            } else {
103                $shortcode .= wpcf_fields_get_shortcode( $field, $_add );
104            }
105        } else {
106            $i = 0;
107            foreach ( $data['options'] as $option ) {
108                if ( $data['display'] == 'value' ) {
109                    $checked_add = $add . ' option="' . $i . '" state="checked"';
110                    $unchecked_add = $add . ' option="' . $i . '" state="unchecked"';
111
112                    if ( $context == 'usermeta' ) {
113                        $shortcode_checked = wpcf_usermeta_get_shortcode( $field,
114                                $checked_add, $option['selected'] );
115                        $shortcode_unchecked = wpcf_usermeta_get_shortcode( $field,
116                                $unchecked_add, $option['not_selected'] );
117                    } else {
118                        $shortcode_checked = wpcf_fields_get_shortcode( $field,
119                                $checked_add, $option['selected'] );
120                        $shortcode_unchecked = wpcf_fields_get_shortcode( $field,
121                                $unchecked_add, $option['not_selected'] );
122                    }
123                    $shortcode .= $shortcode_checked . $shortcode_unchecked;
124                } else {
125                    $add = ' option="' . $i . '"';
126                    if ( $context == 'usermeta' ) {
127                        $add .= wpcf_get_usermeta_form_addon_submit();
128                    }
129                    if ( $types_attr == 'usermeta' ) {
130                        $shortcode .= wpcf_usermeta_get_shortcode( $field, $add );
131                    } else {
132                        $shortcode .= wpcf_fields_get_shortcode( $field, $add );
133                    }
134                }
135                $i++;
136            }
137        }
138    } else {
139        if ( $types_attr == 'usermeta' ) {
140            $shortcode .= wpcf_usermeta_get_shortcode( $field, $add );
141        } else {
142            $shortcode .= wpcf_fields_get_shortcode( $field, $add );
143        }
144    }
145    return $shortcode;
146}
147
148/**
149 * View function.
150 * 
151 * @param type $params 
152 */
153function wpcf_fields_checkboxes_view( $params ) {
154    $option = array();
155    // Basic checks
156    if ( empty( $params['field']['data']['options'] )
157            || !is_array( $params['field_value'] ) ) {
158        return '__wpcf_skip_empty';
159    }
160
161    /*
162     * 
163     * NO OPTION specified
164     * loop over all options and display all of them
165     */
166    if ( !isset( $params['option'] ) ) {
167        $separator = isset( $params['separator'] ) ? html_entity_decode( $params['separator'] ) : ', ';
168        foreach ( $params['field_value'] as $name => &$value ) {
169            /*
170             * 
171             * Set option
172             */
173            if ( isset( $params['field']['data']['options'][$name] ) ) {
174                $option = $params['field']['data']['options'][$name];
175            } else {
176                // Unset if not valid
177                unset( $params['field_value'][$name] );
178                continue;
179            }
180            /*
181             * 
182             * Set output according to settings.
183             * 'db' or 'value'
184             */
185            if ( $option['display'] == 'db'
186                    && !empty( $option['set_value'] ) && !empty( $value ) ) {
187                $value = $option['set_value'];
188                $value = wpcf_translate( 'field ' . $params['field']['id'] . ' option ' . $name . ' value',
189                        $value );
190            } else if ( $option['display'] == 'value' ) {
191                if ( isset( $option['display_value_selected'] ) && !empty( $value ) ) {
192                    $value = $option['display_value_selected'];
193                    $value = wpcf_translate( 'field ' . $params['field']['id'] . ' option ' . $name . ' display value selected',
194                            $value );
195                } else {
196                    $value = $option['display_value_not_selected'];
197                    $value = wpcf_translate( 'field ' . $params['field']['id'] . ' option ' . $name . ' display value not selected',
198                            $value );
199                }
200            } else {
201                unset( $params['field_value'][$name] );
202            }
203        }
204        $output = implode( array_values( $params['field_value'] ), $separator );
205        return empty( $output ) ? '__wpcf_skip_empty' : $output;
206    }
207
208    /*
209     * 
210     * 
211     * OPTION specified - set required option.
212     */
213    $i = 0;
214    foreach ( $params['field']['data']['options'] as $option_key => $option_value ) {
215        if ( intval( $params['option'] ) == $i ) {
216            $option['key'] = $option_key;
217            $option['data'] = $option_value;
218            $option['value'] = !empty( $params['field_value'][$option_key] ) ? $params['field_value'][$option_key] : '__wpcf_unchecked';
219            break;
220        }
221        $i++;
222    }
223
224    $output = '';
225
226    /*
227     * STATE set - use #content is as render value.
228     * If setings are faulty - return '__wpcf_skip_empty'.
229     */
230    if ( isset( $params['state'] ) ) {
231        $content = !empty( $params['#content'] ) ? htmlspecialchars_decode( $params['#content'] ) : '__wpcf_skip_empty';
232        if ( $params['state'] == 'checked'
233                && $option['value'] != '__wpcf_unchecked' ) {
234            return $content;
235        } else if ( $params['state'] == 'unchecked'
236                && $option['value'] == '__wpcf_unchecked' ) {
237            return $content;
238        } else if ( isset( $params['state'] ) ) {
239            return '__wpcf_skip_empty';
240        }
241    }
242
243    /*
244     * 
245     * MAIN settings
246     * 'db'      - Use 'set_value' as render value
247     * 'value'   - Use values set in Group form data 'display_value_selected'
248     *                  or 'display_value_not_selected'
249     * 
250     * Only set if it matches settings.
251     * Otherwise leave empty and '__wpcf_skip_empty' will be returned.
252     */
253    if ( $option['data']['display'] == 'db' ) {
254        /*
255         * 
256         * Only if NOT unchecked!
257         */
258        if ( !empty( $option['data']['set_value'] )
259                && $option['value'] != '__wpcf_unchecked' ) {
260            $output = $option['data']['set_value'];
261            $output = wpcf_translate( 'field ' . $params['field']['id']
262                    . ' option ' . $option['key'] . ' value', $output );
263        }
264    } else if ( $option['data']['display'] == 'value' ) {
265        /*
266         * 
267         * Checked
268         */
269        if ( $option['value'] != '__wpcf_unchecked' ) {
270            if ( isset( $option['data']['display_value_selected'] ) ) {
271                $output = $option['data']['display_value_selected'];
272                $output = wpcf_translate( 'field ' . $params['field']['id'] . ' option ' . $option['key'] . ' display value selected',
273                        $output );
274            }
275            /*
276             * 
277             * 
278             * Un-checked
279             */
280        } else if ( isset( $option['data']['display_value_not_selected'] ) ) {
281            $output = $option['data']['display_value_not_selected'];
282            $output = wpcf_translate( 'field ' . $params['field']['id'] . ' option ' . $option['key'] . ' display value not selected',
283                    $output );
284        }
285    }
286
287    if ( empty( $output ) ) {
288        return '__wpcf_skip_empty';
289    }
290
291    return $output;
292}
293
294/**
295 * This marks child posts checkboxes.
296 * 
297 * Because if all unchecked, on submit there won't be any data.
298 * 
299 * @param string $form
300 * @param type $cf
301 * @return string
302 */
303function wpcf_filds_checkboxes_relationship_form_filter( $form, $cf ) {
304    if ( $cf->cf['type'] == 'checkboxes' ) {
305        $form[wpcf_unique_id( serialize( $cf ) . 'rel_child' )] = array(
306            '#type' => 'hidden',
307            '#name' => '_wpcf_check_checkboxes[' . $cf->post->ID . ']['
308            . $cf->slug . ']',
309            '#value' => '1'
310        );
311    }
312    return $form;
313}
314
315/**
316 * Triggers post_meta filter.
317 * 
318 * @param type $post
319 * @return type
320 */
321function wpcf_fields_checkboxes_wpv_conditional_trigger( $post ) {
322    add_filter( 'get_post_metadata',
323            'wpcf_fields_checkboxes_conditional_filter_post_meta', 10, 4 );
324}
325
326/**
327 * Returns string.
328 * 
329 * @global type $wpcf
330 * @param type $null
331 * @param type $object_id
332 * @param type $meta_key
333 * @param type $single
334 * @return type
335 */
336function wpcf_fields_checkboxes_conditional_filter_post_meta( $null, $object_id,
337        $meta_key, $single ) {
338    global $wpcf;
339    $field = wpcf_admin_fields_get_field( $wpcf->field->__get_slug_no_prefix( $meta_key ) );
340    if ( !empty( $field ) && $field['type'] == 'checkboxes' ) {
341        $_meta = maybe_unserialize( wpcf_get_post_meta( $object_id, $meta_key,
342                        $single ) );
343        if ( is_array( $_meta ) ) {
344            $null = empty( $_meta ) ? '1' : '';
345        }
346    }
347    return $null;
348}
349
350/**
351 * Triggers post_meta filter.
352 * 
353 * @param type $post
354 * @return type
355 */
356function wpcf_fields_checkboxes_wpv_conditional_trigger_end( $post ) {
357    remove_filter( 'get_post_metadata',
358            'wpcf_fields_checkboxes_conditional_filter_post_meta', 10, 4 );
359}