Exemple #1
0
/**
 * Map a multideminsional array
 *
 * @param	string	$func		the function to map
 * @param	array	$meta		a multidimensional array
 * @param	array	$sanitizer	a matching multidimensional array of sanitizers
 *
 * @return	array				new array, fully mapped with the provided arrays
 */
function bon_array_map_r($func, $meta, $sanitizer)
{
    $newMeta = array();
    $meta = array_values($meta);
    foreach ($meta as $key => $array) {
        if ($array == '') {
            continue;
        }
        /**
         * some values are stored as array, we only want multidimensional ones
         */
        if (!is_array($array)) {
            return array_map($func, $meta, (array) $sanitizer);
            break;
        }
        /**
         * the sanitizer will have all of the fields, but the item may only 
         * have valeus for a few, remove the ones we don't have from the santizer
         */
        $keys = array_keys($array);
        $newSanitizer = $sanitizer;
        if (is_array($sanitizer)) {
            foreach ($newSanitizer as $sanitizerKey => $value) {
                if (!in_array($sanitizerKey, $keys)) {
                    unset($newSanitizer[$sanitizerKey]);
                }
            }
        }
        /**
         * run the function as deep as the array goes
         */
        foreach ($array as $arrayKey => $arrayValue) {
            if (is_array($arrayValue)) {
                $array[$arrayKey] = bon_array_map_r($func, $arrayValue, $newSanitizer[$arrayKey]);
            }
        }
        $array = array_map($func, $array, $newSanitizer);
        $newMeta[$key] = array_combine($keys, array_values($array));
    }
    return $newMeta;
}
 /**
  * saves the captured data
  */
 function save_box($post_id)
 {
     $post_type = get_post_type();
     // verify nonce
     if (!isset($_POST['bon_meta_box_nonce_field'])) {
         return $post_id;
     }
     if (!(in_array($post_type, $this->page) || wp_verify_nonce($_POST['bon_meta_box_nonce_field'], 'bon_meta_box_nonce_action'))) {
         return $post_id;
     }
     // check autosave
     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
         return $post_id;
     }
     // check permissions
     if (!current_user_can('edit_page', $post_id)) {
         return $post_id;
     }
     // loop through fields and save the data
     foreach ($this->fields as $field) {
         if ($field['type'] == 'section') {
             $sanitizer = null;
             continue;
         } else {
             // save the rest
             $old = get_post_meta($post_id, $field['id'], true);
             if (isset($_POST[$field['id']])) {
                 $new = $_POST[$field['id']];
             }
             // Set checkbox to false if it wasn't sent in the $_POST
             if ('checkbox' == $field['type'] && !isset($_POST[$field['id']])) {
                 $new = '0';
             }
             // Set each item in the multicheck to false if it wasn't sent in the $_POST
             if ('multicheck' == $field['type'] && !isset($_POST[$field['id']])) {
                 foreach ($field['options'] as $key => $value) {
                     $new[$key] = '0';
                 }
             }
             if (isset($new) && '' == $new && $old) {
                 delete_post_meta($post_id, $field['id'], $old);
             } else {
                 if (isset($new) && $new != $old) {
                     $sanitizer = isset($field['sanitizer']) ? $field['sanitizer'] : 'sanitize_text_field';
                     if (is_array($new) && isset($field['repeatable_fields'])) {
                         $new = bon_array_map_r('bon_sanitize', $new, $field['repeatable_fields']);
                     } else {
                         //$new = bon_sanitize( $new, $sanitizer );
                         $new = apply_filters('bon_sanitize_' . $field['type'], $new, $field);
                     }
                     update_post_meta($post_id, $field['id'], $new);
                 }
             }
         }
     }
     // end foreach
 }
 /**
  * Format Configuration Array.
  *
  * Get an array of all default values as set in
  * options.php. The 'id','std' and 'type' keys need
  * to be defined in the configuration array. In the
  * event that these keys are not present the option
  * will not be included in this function's output.
  *
  * @return    array     Rey-keyed options configuration array.
  *
  * @access    private
  */
 private function _get_default_values($opt_group = '')
 {
     foreach ($this->option_pages as $pages) {
         if ($pages['option_key'] == $opt_group) {
             $config = $pages['option_set'];
             break;
         }
     }
     foreach ((array) $config as $option) {
         if (!isset($option['id'])) {
             continue;
         }
         if (!isset($option['std'])) {
             continue;
         }
         if (!isset($option['type'])) {
             continue;
         }
         if ('repeatable' != $option['type']) {
             if (has_filter('bon_sanitize_' . $option['type'])) {
                 $output[$option['id']] = apply_filters('bon_sanitize_' . $option['type'], $option['std'], $option);
             }
         } else {
             $sanitizer = isset($option['sanitizer']) ? $option['sanitizer'] : 'sanitize_text_field';
             $output[$option['id']] = bon_array_map_r('bon_sanitize', $output[$option['id']], $option);
         }
     }
     return $output;
 }