Esempio n. 1
0
/**
 * Get a post meta value for multiple checkboxes and return a default if it has never been saved previously
 *
 * @since    1.0.0
 * @link     https://mintplugins.com/doc/mp_core_get_post_meta
 * @param    int $post_id The id of the post this meta value is attached to.
 * @param    string $meta_key The key for the value we want to get.
 * @param    mixed $default The default value for this if it has never been previously saved
 * @return   mixed $meta_value Either the meta value saved or the default passed-in.
 */
function mp_core_get_post_meta_multiple_checkboxes($post_id, $meta_key, $default = array())
{
    //Get the post meta field we are looking for
    $meta_value = mp_core_get_post_meta_or_never_been_saved($post_id, $meta_key);
    //If this checkbox has never been saved before, return the default value passed-in:
    if ($meta_value == 'never_been_saved_73698363746983746') {
        return $default;
    }
    //Othewise, if it has been saved before
    $meta_value = json_decode($meta_value, true);
    return $meta_value;
}
Esempio n. 2
0
        /**
         * This function prints the metabox content to the metabox
         *
         * @access   public
         * @since    1.0.0
         * @see      wp_nonce_field()
         * @see      has_filter()
         * @see      apply_filters()
         * @see      plugin_basename()
         * @see      get_post_meta()
         * @see      MP_CORE_Metabox::basictext()
         * @see      MP_CORE_Metabox::textbox()
         * @see      MP_CORE_Metabox::textarea()
         * @see      MP_CORE_Metabox::colorpicker()
         * @see      MP_CORE_Metabox::mediaupload()
         * @see      MP_CORE_Metabox::select()
         * @see      MP_CORE_Metabox::password()
         * @see      MP_CORE_Metabox::checkbox()
         * @see      MP_CORE_Metabox::url()
         * @see      MP_CORE_Metabox::date()
         * @see      MP_CORE_Metabox::time()
         * @see      MP_CORE_Metabox::number()		 
         * @return   void
         */
        public function mp_core_metabox_callback()
        {
            global $post;
            $this->_post_id = isset($post->ID) ? $post->ID : '';
            $prev_repeater = false;
            //Loop through the pre-set, passed-in array of fields
            foreach ($this->_metabox_items_array as $field) {
                //If this is a hook_anchor, it doesn not need to be processed so skip over it. It's just used for filters to hook new meta options to the right location.
                if ($field['field_type'] == 'hook_anchor') {
                    continue;
                }
                //Just realized I'm checking a nonce for EVERY field. Whoa there cowboy! Might be some overkill here....
                // Set up nonce for verification
                //wp_nonce_field( plugin_basename( __FILE__ ), $field['field_id'] . '_metabox_nonce' );
                // Filter for title of this field
                $field['field_title'] = has_filter('mp_' . $field['field_id'] . '_title') ? apply_filters('mp_' . $field['field_id'] . '_title', $field['field_title'], $this->_post_id) : $field['field_title'];
                // Filter for description of this field
                $field['field_description'] = has_filter('mp_' . $field['field_id'] . '_description') ? apply_filters('mp_' . $field['field_id'] . '_description', $field['field_description'], $this->_post_id) : $field['field_description'];
                //This is the first field in a set of repeater
                if (isset($field['field_repeater']) && $prev_repeater != $field['field_repeater']) {
                    //Just realized I'm checking a nonce for EVERY field. Whoa there cowboy! Might be some overkill here....
                    // Set up nonce for verification
                    //wp_nonce_field( plugin_basename( __FILE__ ), $field['field_repeater'] . '_metabox_nonce' );
                    //Make sure a post number has been set
                    if (isset($this->_post_id)) {
                        //Get the array of variables stored in the database for this repeater
                        $current_stored_repeater = get_post_meta($this->_post_id, $key = $field['field_repeater'], true);
                        //This is a brand new repeater
                        $repeat_counter = 0;
                        //Get the showhider
                        $showhider = !empty($field['field_showhider']) ? 'showhider="' . $field['field_showhider'] . '"' : NULL;
                        //Create ul container for this repeater
                        echo '<ul class="repeater_container" ' . $showhider . '>';
                        //If this repeater has had info saved to it previously
                        if ($current_stored_repeater != NULL) {
                            //Loop the same amount of times the user clicked 'repeat' (including the first one that was there before they clicked 'repeat')
                            foreach ($current_stored_repeater as $repeater_set) {
                                //Should we add the "closed" class (if there's more than 1 repeater)
                                $closed_class = count($current_stored_repeater) > 1 ? 'closed' : '';
                                //Create start of div for this repeat
                                echo '<li class="' . $field['field_repeater'] . '_repeater ' . $closed_class . '"> 
									<div class="mp_repeater_handlediv handlediv" title="Click to toggle">
										<br>
									</div>
									<h3 class="mp_drag hndle">
										<div class="mp-core-repeater-title">' . __('Enter Info:', 'mp_core') . '</div>
										<div class="mp-core-repeater-values-description"></div>
										<div class="mp-core-repeater-values-ellipses">...</div>
										<div class="mp-core-repeater-description">' . __(' Click to edit. Drag to re-order', 'mp_core') . '</div>
									</h3>';
                                foreach ($this->_metabox_items_array as $thefield) {
                                    if (isset($thefield['field_repeater']) && $thefield['field_repeater'] == $field['field_repeater']) {
                                        //formula to match all field in the rows they were saved to the rows they are displayed in  = $field_position_in_repeater*$number_of_repeats+$i
                                        //If a value has been saved for this specific field
                                        if (isset($repeater_set[$thefield['field_id']])) {
                                            //Use the saved value.
                                            $field_value = $repeater_set[$thefield['field_id']];
                                        } else {
                                            //Otherwise use the passed-in, default value.
                                            $field_value = isset($thefield['field_value']) ? $thefield['field_value'] : '';
                                        }
                                        //Set up the showhider attr based on whether it's parent showhidergroup repeats or if the entire repeater is in a parent showhider
                                        if (isset($thefield['field_showhider'])) {
                                            //If the showhider repeats with the repeater
                                            if (isset($thefield['field_showhider_repeats']) && $thefield['field_showhider_repeats']) {
                                                $showhider = 'showhider="' . $thefield['field_repeater'] . 'AAAAA' . $repeat_counter . 'BBBBBAAAAA' . $thefield['field_showhider'] . 'BBBBB' . '"';
                                            } else {
                                                $showhider = 'showhider="' . $thefield['field_showhider'] . '"';
                                            }
                                        } else {
                                            $showhider = NULL;
                                        }
                                        //Make array to pass to callback function
                                        $callback_args = array('field_id' => $thefield['field_repeater'] . '[' . $repeat_counter . '][' . $thefield['field_id'] . ']', 'field_title' => $thefield['field_title'], 'field_description' => $thefield['field_description'], 'field_value' => $field_value, 'field_input_class' => isset($thefield['field_input_class']) ? 'mp_repeater ' . $thefield['field_input_class'] : 'mp_repeater', 'field_container_class' => isset($thefield['field_container_class']) ? $thefield['field_container_class'] : NULL, 'field_select_values' => isset($thefield['field_select_values']) ? $thefield['field_select_values'] : NULL, 'field_default_attr' => NULL, 'field_required' => isset($thefield['field_required']) ? $thefield['field_required'] : false, 'field_showhider' => $showhider, 'field_placeholder' => isset($thefield['field_placeholder']) ? ' placeholder="' . $thefield['field_placeholder'] . '" ' : NULL, 'field_conditional_id' => isset($thefield['field_conditional_id']) ? $thefield['field_repeater'] . '[' . $repeat_counter . '][' . $thefield['field_conditional_id'] . ']' : NULL, 'field_conditional_values' => isset($thefield['field_conditional_values']) ? $thefield['field_conditional_values'] : NULL);
                                        //call function for field type (callback function name stored in $this->$field['field_type']
                                        $this->{$thefield}['field_type']($callback_args);
                                    }
                                }
                                //This is the last one in a set of repeatable fields
                                echo '<div class="mp_duplicate_buttons"><a class="mp_duplicate button">' . __('Add Another', 'mp_core') . '</a><a class="mp_duplicate_remove button">' . __('Remove', 'mp_core') . '</a></div>';
                                echo '</li>';
                                //bump the repeat_counter to the next number of the array
                                $repeat_counter = $repeat_counter + 1;
                            }
                        } else {
                            //Create start of div for this repeat
                            echo '<li class="' . $field['field_repeater'] . '_repeater"> <div class="handlediv" title="Click to toggle"><br></div><h3 class="mp_drag hndle">
										<div class="mp-core-repeater-title">' . __('Enter Info:', 'mp_core') . '</div>
										<div class="mp-core-repeater-values-description"></div>
										<div class="mp-core-repeater-values-ellipses">...</div>
										<div class="mp-core-repeater-description">' . __(' Click to edit. Drag to re-order', 'mp_core') . '</div>
									</h3>';
                            foreach ($this->_metabox_items_array as $thefield) {
                                if (isset($thefield['field_repeater']) && $thefield['field_repeater'] == $field['field_repeater']) {
                                    //formula to match all field in the rows they were saved to the rows they are displayed in  = $field_position_in_repeater*$number_of_repeats+$i
                                    //Set up the showhider attr based on whether it's parent showhidergroup repeats or if the entire repeater is in a parent showhider
                                    if (isset($thefield['field_showhider'])) {
                                        //If the showhider repeats with the repeater
                                        if (isset($thefield['field_showhider_repeats']) && $thefield['field_showhider_repeats']) {
                                            $showhider = 'showhider="' . $thefield['field_repeater'] . 'AAAAA' . $repeat_counter . 'BBBBBAAAAA' . $thefield['field_showhider'] . 'BBBBB' . '"';
                                        } else {
                                            $showhider = 'showhider="' . $thefield['field_showhider'] . '"';
                                        }
                                    } else {
                                        $showhider = NULL;
                                    }
                                    //Make array to pass to callback function
                                    $callback_args = array('field_id' => $thefield['field_repeater'] . '[' . $repeat_counter . '][' . $thefield['field_id'] . ']', 'field_title' => $thefield['field_title'], 'field_description' => $thefield['field_description'], 'field_value' => isset($thefield['field_value']) ? $thefield['field_value'] : '', 'field_input_class' => isset($thefield['field_input_class']) ? 'mp_repeater ' . $thefield['field_input_class'] : 'mp_repeater', 'field_container_class' => isset($thefield['field_container_class']) ? $thefield['field_container_class'] : NULL, 'field_select_values' => isset($thefield['field_select_values']) ? $thefield['field_select_values'] : NULL, 'field_default_attr' => NULL, 'field_required' => isset($thefield['field_required']) ? $thefield['field_required'] : false, 'field_showhider' => $showhider, 'field_placeholder' => isset($thefield['field_placeholder']) ? ' placeholder="' . $thefield['field_placeholder'] . '" ' : NULL, 'field_popup_help' => isset($thefield['field_popup_help']) ? $thefield['field_popup_help'] : NULL, 'field_conditional_id' => isset($thefield['field_conditional_id']) ? $thefield['field_repeater'] . '[' . $repeat_counter . '][' . $thefield['field_conditional_id'] . ']' : NULL, 'field_conditional_values' => isset($thefield['field_conditional_values']) ? $thefield['field_conditional_values'] : NULL);
                                    //call function for field type (callback function name stored in $this->$field['field_type']
                                    $this->{$thefield}['field_type']($callback_args);
                                }
                            }
                            //This is the last one in a set of repeatable fields
                            echo '<div class="mp_duplicate_buttons"><a class="mp_duplicate button">' . __('Add Another', 'mp_core') . '</a><a class="mp_duplicate_remove button">' . __('Remove', 'mp_core') . '</a>';
                            echo '</li>';
                        }
                        //close repeater container
                        echo '</ul>';
                        //Make a note that we have handled this repeater already so we don't do it again. We do this by storing the name of the current repeater
                        $prev_repeater = $field['field_repeater'];
                    }
                } else {
                    //And it's also not a repeater at all. It is a single field.
                    if (!isset($field['field_repeater'])) {
                        // Use mp_core_get_post_meta_or_never_been_saved to retrieve an existing value from the database or 'never_been_saved' if it was never previously saved.
                        $value = mp_core_get_post_meta_or_never_been_saved($this->_post_id, $key = $field['field_id']);
                        //If this field has never been saved before
                        if ($value == 'never_been_saved_73698363746983746') {
                            //Set the value to be the pre-set value for this field passed-in.
                            $value = isset($field['field_value']) ? $field['field_value'] : '';
                        }
                        //If field required hasn't been set, set it to be false
                        $field_required = isset($field['field_required']) ? $field['field_required'] : false;
                        //if $field_select_values hasn't been set, set it to be NULL
                        $field_select_values = isset($field['field_select_values']) ? $field['field_select_values'] : NULL;
                        //set the preset value to the passed in value
                        $preset_value = isset($field['field_value']) ? $field['field_value'] : '';
                        //Default Field Attribute String:
                        $field_default_attr = isset($field['field_value']) ? ' mp_default_value="' . $field['field_value'] . '" ' : ' mp_default_value="" ';
                        //set the showhider
                        $showhider_value = isset($field['field_showhider']) ? 'showhider="' . $field['field_showhider'] . '"' : '';
                        //set the field container class
                        $field_container_class = isset($field['field_container_class']) ? $field['field_container_class'] : '' . ' ' . $field['field_id'];
                        //set the field input class
                        $field_input_class = isset($field['field_input_class']) ? $field['field_input_class'] . ' ' . $field['field_id'] : $field['field_id'];
                        //set the placeholder
                        $placeholder_value = isset($field['field_placeholder']) ? 'placeholder="' . $field['field_placeholder'] . '"' : '';
                        //set the popup help
                        $popup_help = isset($field['field_popup_help']) ? $field['field_popup_help'] : NULL;
                        //Set the field that we want to check the condition of before showing this field
                        //These 2 options control a js function which hides fields dependant on the value of another field - conditional logic
                        $field_conditional_id = isset($field['field_conditional_id']) ? $field['field_conditional_id'] : NULL;
                        $field_conditional_values = isset($field['field_conditional_values']) ? $field['field_conditional_values'] : NULL;
                        //Make array to pass to callback function
                        $callback_args = array('field_id' => $field['field_id'], 'field_title' => $field['field_title'], 'field_description' => $field['field_description'], 'field_value' => $value, 'field_input_class' => $field_input_class, 'field_container_class' => $field_container_class, 'field_select_values' => $field_select_values, 'field_default_attr' => $field_default_attr, 'field_required' => $field_required, 'field_showhider' => $showhider_value, 'field_placeholder' => $placeholder_value, 'field_popup_help' => $popup_help, 'field_conditional_id' => $field_conditional_id, 'field_conditional_values' => $field_conditional_values);
                        //call function for field type (function name stored in $this->$field['field_type']
                        $this->{$field}['field_type']($callback_args);
                    }
                }
            }
        }