PageRenderTime 68ms CodeModel.GetById 22ms app.highlight 27ms RepoModel.GetById 8ms app.codeStats 1ms

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

https://bitbucket.org/acipriani/madeinapulia.com
PHP | 367 lines | 241 code | 19 blank | 107 comment | 84 complexity | c60b24c36cf55cc596c17a1c98af671e 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' : stripslashes($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     */
254
255    if ( isset($option['data']) && $option['data']['display'] == 'db' ) {
256        /*
257         * 
258         * Only if NOT unchecked!
259         */
260        if ( !empty( $option['data']['set_value'] )
261                && $option['value'] != '__wpcf_unchecked' ) {
262            $output = $option['data']['set_value'];
263            $output = wpcf_translate( 'field ' . $params['field']['id']
264                    . ' option ' . $option['key'] . ' value', $output );
265        }
266    } else if ( isset($option['data']) && $option['data']['display'] == 'value' ) {
267        /*
268         * 
269         * Checked
270         */
271        if ( $option['value'] != '__wpcf_unchecked' ) {
272            if ( isset( $option['data']['display_value_selected'] ) ) {
273                $output = $option['data']['display_value_selected'];
274                $output = wpcf_translate( 'field ' . $params['field']['id'] . ' option ' . $option['key'] . ' display value selected',
275                        $output );
276            }
277            /*
278             * 
279             * 
280             * Un-checked
281             */
282        } else if ( isset( $option['data']['display_value_not_selected'] ) ) {
283            $output = $option['data']['display_value_not_selected'];
284            $output = wpcf_translate( 'field ' . $params['field']['id'] . ' option ' . $option['key'] . ' display value not selected',
285                    $output );
286        }
287    }
288
289    if ( empty( $output ) ) {
290        return '__wpcf_skip_empty';
291    }
292
293    return $output;
294}
295
296/**
297 * This marks child posts checkboxes.
298 * 
299 * Because if all unchecked, on submit there won't be any data.
300 * 
301 * @param string $form
302 * @param type $cf
303 * @return string
304 */
305function wpcf_filds_checkboxes_relationship_form_filter( $form, $cf ) {
306    if ( $cf->cf['type'] == 'checkboxes' ) {
307        $form[wpcf_unique_id( serialize( $cf ) . 'rel_child' )] = array(
308            '#type' => 'hidden',
309            '#name' => '_wpcf_check_checkboxes[' . $cf->post->ID . ']['
310            . $cf->slug . ']',
311            '#value' => '1'
312        );
313    }
314    return $form;
315}
316
317/**
318 * Triggers post_meta filter.
319 * 
320 * @param type $post
321 * @return type
322 */
323function wpcf_fields_checkboxes_wpv_conditional_trigger( $post ) {
324    add_filter( 'get_post_metadata',
325            'wpcf_fields_checkboxes_conditional_filter_post_meta', 10, 4 );
326}
327
328/**
329 * Returns string.
330 * 
331 * @global type $wpcf
332 * @param type $null
333 * @param type $object_id
334 * @param type $meta_key
335 * @param type $single
336 * @return type
337 */
338function wpcf_fields_checkboxes_conditional_filter_post_meta( $null, $object_id,
339        $meta_key, $single ) {
340    global $wpcf;
341    $field = wpcf_admin_fields_get_field( $wpcf->field->__get_slug_no_prefix( $meta_key ) );
342    if ( !empty( $field ) && $field['type'] == 'checkboxes' ) {
343        $_meta = maybe_unserialize( wpcf_get_post_meta( $object_id, $meta_key,
344                        $single ) );
345        if ( is_array( $_meta ) ) {
346            $null = empty( $_meta ) ? '1' : '';
347        }
348        /**
349         * be sure do not return string if array is expected!
350         */
351        if ( !$single && !is_array($null) ) {
352            return array($null);
353        }
354    }
355    return $null;
356}
357
358/**
359 * Triggers post_meta filter.
360 * 
361 * @param type $post
362 * @return type
363 */
364function wpcf_fields_checkboxes_wpv_conditional_trigger_end( $post ) {
365    remove_filter( 'get_post_metadata',
366            'wpcf_fields_checkboxes_conditional_filter_post_meta', 10, 4 );
367}