PageRenderTime 28ms CodeModel.GetById 9ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/acipriani/madeinapulia.com
PHP | 292 lines | 207 code | 30 blank | 55 comment | 91 complexity | 5dbc831f42b88f99d85eafca8f8b5bd6 MD5 | raw file
  1<?php
  2/**
  3 *
  4 * $HeadURL: https://www.onthegosystems.com/misc_svn/cck/tags/1.6.3/embedded/includes/fields/checkbox.php $
  5 * $LastChangedDate: 2014-08-28 09:37:36 +0000 (Thu, 28 Aug 2014) $
  6 * $LastChangedRevision: 26512 $
  7 * $LastChangedBy: bruce $
  8 *
  9 */
 10
 11add_action( 'save_post', 'wpcf_fields_checkbox_save_check', 15, 1 );
 12add_action( 'edit_attachment', 'wpcf_fields_checkbox_save_check', 15, 1 );
 13
 14/**
 15 * Register data (called automatically).
 16 *
 17 * @return type
 18 */
 19function wpcf_fields_checkbox() {
 20    return array(
 21        'id' => 'wpcf-checkbox',
 22        'title' => __( 'Checkbox', 'wpcf' ),
 23        'description' => __( 'Checkbox', 'wpcf' ),
 24        'validate' => array('required'),
 25        'meta_key_type' => 'BINARY',
 26    );
 27}
 28
 29/**
 30 * Form data for post edit page.
 31 *
 32 * @param type $field
 33 */
 34function wpcf_fields_checkbox_meta_box_form( $field, $field_object ) {
 35    global $wpcf;
 36    $checked = false;
 37    $field['data']['set_value'] = stripslashes( $field['data']['set_value'] );
 38    if ( $field['value'] == $field['data']['set_value'] ) {
 39        $checked = true;
 40    }
 41    // If post is new check if it's checked by default
 42    global $pagenow;
 43    if ( $pagenow == 'post-new.php' && !empty( $field['data']['checked'] ) ) {
 44        $checked = true;
 45    }
 46    // This means post is new
 47    if ( !isset( $field_object->post->ID ) ) {
 48        $field_object->post = (object) array('ID' => 0);
 49    }
 50    return array(
 51        '#type' => 'checkbox',
 52        '#value' => $field['data']['set_value'],
 53        '#default_value' => $checked,
 54        '#after' => '<input type="hidden" name="_wpcf_check_checkbox['
 55        . $field_object->post->ID . '][' . $field_object->slug
 56        . ']" value="1" />',
 57    );
 58}
 59
 60/**
 61 * Editor callback form.
 62 */
 63function wpcf_fields_checkbox_editor_callback( $field, $settings ) {
 64
 65    $value_not_selected = '';
 66    $value_selected = '';
 67
 68    if ( isset( $field['data']['display_value_not_selected'] ) ) {
 69        $value_not_selected = $field['data']['display_value_not_selected'];
 70    }
 71    if ( isset( $field['data']['display_value_selected'] ) ) {
 72        $value_selected = $field['data']['display_value_selected'];
 73    }
 74
 75    $data = array_merge( array(
 76        'selected' => WPCF_Editor::sanitizeParams( $value_selected ),
 77        'not_selected' => WPCF_Editor::sanitizeParams( $value_not_selected ),
 78            ), $settings );
 79
 80    return array(
 81        'supports' => array('style'),
 82        'tabs' => array(
 83            'display' => array(
 84                'menu_title' => __( 'Display', 'wpcf' ),
 85                'title' => __( 'Display', 'wpcf' ),
 86                'content' => WPCF_Loader::template( 'editor-modal-checkbox',
 87                        $data ),
 88            )
 89        ),
 90    );
 91}
 92
 93/**
 94 * Editor callback form submit.
 95 */
 96function wpcf_fields_checkbox_editor_submit( $data, $field, $context ) {
 97    $add = '';
 98    $types_attr = 'field';
 99    if ( $context == 'usermeta' ) {
100        $add .= wpcf_get_usermeta_form_addon_submit();
101        $types_attr = 'usermeta';
102    }
103
104    if ( isset($data['display']) && $data['display'] == 'value' ) {
105
106        $checked_add = $add . ' state="checked"';
107        $unchecked_add = $add . ' state="unchecked"';
108
109        if ( $context == 'usermeta' ) {
110            $shortcode_checked = wpcf_usermeta_get_shortcode( $field,
111                    $checked_add, $data['selected'] );
112            $shortcode_unchecked = wpcf_usermeta_get_shortcode( $field,
113                    $unchecked_add, $data['not_selected'] );
114        } else {
115            $shortcode_checked = wpcf_fields_get_shortcode( $field,
116                    $checked_add, $data['selected'] );
117            $shortcode_unchecked = wpcf_fields_get_shortcode( $field,
118                    $unchecked_add, $data['not_selected'] );
119        }
120        $shortcode = $shortcode_checked . $shortcode_unchecked;
121    } else {
122        if ( $context == 'usermeta' ) {
123            $shortcode = wpcf_usermeta_get_shortcode( $field, $add );
124        } else {
125            $shortcode = wpcf_fields_get_shortcode( $field, $add );
126        }
127    }
128
129    return $shortcode;
130
131}
132
133/**
134 * View function.
135 *
136 * @param type $params
137 */
138function wpcf_fields_checkbox_view( $params ) {
139    $output = '';
140    $option_name = 'wpcf-fields';
141    if ( isset( $params['usermeta'] ) && !empty( $params['usermeta'] ) ) {
142        $option_name = 'wpcf-usermeta';
143    }
144    if ( isset( $params['option_name'] ) ) {
145        $option_name = $params['option_name'];
146    }
147    if ( isset( $params['state'] )
148            && $params['state'] == 'unchecked'
149            && empty( $params['field_value'] ) ) {
150        if ( empty( $params['#content'] ) ) {
151            return '__wpcf_skip_empty';
152        }
153        return htmlspecialchars_decode( $params['#content'] );
154    } else if ( isset( $params['state'] ) && $params['state'] == 'unchecked' ) {
155        return '__wpcf_skip_empty';
156    }
157
158    if ( isset( $params['state'] ) && $params['state'] == 'checked' && !empty( $params['field_value'] ) ) {
159        if ( empty( $params['#content'] ) ) {
160            return '__wpcf_skip_empty';
161        }
162        return htmlspecialchars_decode( $params['#content'] );
163    } else if ( isset( $params['state'] ) && $params['state'] == 'checked' ) {
164        return '__wpcf_skip_empty';
165    }
166    if ( !empty( $params['#content'] )
167            && !empty( $params['field_value'] ) ) {
168        return htmlspecialchars_decode( $params['#content'] );
169    }
170
171    // Check if 'save_empty' is yes and if value is 0 - set value to empty string
172    if ( isset( $params['field']['data']['save_empty'] )
173            && $params['field']['data']['save_empty'] == 'yes'
174            && $params['field_value'] == '0' ) {
175        $params['field_value'] = '';
176    }
177
178    if ( $params['field']['data']['display'] == 'db' && $params['field_value'] != '' ) {
179        $field = wpcf_fields_get_field_by_slug( $params['field']['slug'],
180                $option_name );
181        $output = $field['data']['set_value'];
182
183        // Show the translated value if we have one.
184        $output = wpcf_translate( 'field ' . $field['id'] . ' checkbox value',
185                $output );
186    } else if ( $params['field']['data']['display'] == 'value'
187            && $params['field_value'] != '' ) {
188        if ( !empty( $params['field']['data']['display_value_selected'] ) ) {
189            $output = $params['field']['data']['display_value_selected'];
190            $output = wpcf_translate( 'field ' . $params['field']['id'] . ' checkbox value selected',
191                    $output );
192        }
193    } else if ( $params['field']['data']['display'] == 'value'
194        && !empty( $params['field']['data']['display_value_not_selected'] ) ) {
195        $output = $params['field']['data']['display_value_not_selected'];
196        $output = wpcf_translate( 'field ' . $params['field']['id'] . ' checkbox value not selected', $output );
197    } else {
198        return '__wpcf_skip_empty';
199    }
200
201    return $output;
202}
203
204/**
205 * Check if checkbox is submitted.
206 *
207 * Currently used on Relationship saving. May be expanded to general code.
208 *
209 * @param type $post_id
210 */
211function wpcf_fields_checkbox_save_check( $post_id ) {
212
213    $meta_to_unset = array();
214    $cf = new WPCF_Field();
215
216    /*
217     *
218     * We hve several calls on this:
219     * 1. Saving post with Update
220     * 2. Saving all children
221     * 3. Saving child
222     */
223
224    $mode = 'save_main';
225    if ( defined( 'DOING_AJAX' ) ) {
226        if ( isset( $_GET['wpcf_action'] )
227                && $_GET['wpcf_action'] == 'pr_save_all' ) {
228            $mode = 'save_all';
229        } else if ( isset( $_GET['wpcf_action'] )
230                && $_GET['wpcf_action'] == 'pr_save_child_post' ) {
231            $mode = 'save_child';
232        }
233    }
234
235    // See if any marked for checking
236    if ( isset( $_POST['_wpcf_check_checkbox'] ) ) {
237
238        // Loop and search in $_POST
239        foreach ( $_POST['_wpcf_check_checkbox'] as $child_id => $slugs ) {
240            foreach ( $slugs as $slug => $true ) {
241
242                $cf->set( $child_id, $cf->__get_slug_no_prefix( $slug ) );
243
244                // First check main post
245                if ( $mode == 'save_main'
246                        && intval( $child_id ) == wpcf_get_post_id() ) {
247                    if ( !isset( $_POST['wpcf'][$cf->cf['slug']] ) ) {
248                        $meta_to_unset[intval( $child_id )][$cf->slug] = true;
249                    }
250                    continue;
251                }
252
253                // If new post
254                if ( $mode == 'save_main' && $child_id == 0 ) {
255                    if ( !isset( $_POST['wpcf'][$cf->cf['slug']] ) ) {
256                        $meta_to_unset[$post_id][$cf->slug] = true;
257                    }
258                    continue;
259                }
260                /**
261                 * Relationship check
262                 */
263                if ( $mode == 'save_main' ) {
264                    if ( !isset( $_POST['wpcf'][$cf->cf['slug']] ) ) {
265                        $meta_to_unset[$post_id][$cf->slug] = true;
266                    }
267                    continue;
268                } else if ( !empty( $_POST['wpcf_post_relationship'] ) ) {
269                    foreach ( $_POST['wpcf_post_relationship'] as $_parent => $_children ) {
270                        foreach ( $_children as $_child_id => $_slugs ) {
271                            if ( !isset( $_slugs[$slug] ) ) {
272                                $meta_to_unset[$_child_id][$cf->slug] = true;
273                            }
274                        }
275                    }
276                }
277            }
278        }
279    }
280
281    // After collected - delete them
282    foreach ( $meta_to_unset as $child_id => $slugs ) {
283        foreach ( $slugs as $slug => $true ) {
284            $cf->set( $child_id, $cf->__get_slug_no_prefix( $slug ) );
285            if ( $cf->cf['data']['save_empty'] != 'no' ) {
286                update_post_meta( $child_id, $slug, 0 );
287            } else {
288                delete_post_meta( $child_id, $slug );
289            }
290        }
291    }
292}