示例#1
0
文件: core.php 项目: arobbins/sblog
 /**
  * Get registered meta boxes via a filter.
  * Advantages:
  * - prevents duplicated global variables.
  * - allows users to remove/hide registered meta boxes.
  */
 public static function get_meta_boxes()
 {
     if (null === self::$meta_boxes) {
         self::$meta_boxes = apply_filters('swpmb_meta_boxes', array());
         self::$meta_boxes = empty(self::$meta_boxes) || !is_array(self::$meta_boxes) ? array() : self::$meta_boxes;
     }
     return self::$meta_boxes;
 }
示例#2
0
文件: helper.php 项目: arobbins/sblog
 /**
  * Hash all fields into an indexed array for search
  * @param string $post_type Post type
  */
 public static function hash_fields($post_type)
 {
     self::$fields[$post_type] = array();
     $meta_boxes = SWPMB_Core::get_meta_boxes();
     foreach ($meta_boxes as $meta_box) {
         $meta_box = SWP_Meta_Box::normalize($meta_box);
         if (!in_array($post_type, $meta_box['post_types'])) {
             continue;
         }
         foreach ($meta_box['fields'] as $field) {
             if (!empty($field['id'])) {
                 self::$fields[$post_type][$field['id']] = $field;
             }
         }
     }
 }
示例#3
0
 /**
  * Save data from meta box
  *
  * @param int $post_id Post ID
  */
 function save_post($post_id)
 {
     // Check if this function is called to prevent duplicated calls like revisions, manual hook to wp_insert_post, etc.
     if (true === $this->saved) {
         return;
     }
     $this->saved = true;
     // Check whether form is submitted properly
     $nonce = (string) filter_input(INPUT_POST, "nonce_{$this->meta_box['id']}");
     if (!wp_verify_nonce($nonce, "swpmb-save-{$this->meta_box['id']}")) {
         return;
     }
     // Autosave
     if (defined('DOING_AUTOSAVE') && !$this->meta_box['autosave']) {
         return;
     }
     // Make sure meta is added to the post, not a revision
     if ($the_post = wp_is_post_revision($post_id)) {
         $post_id = $the_post;
     }
     // Before save action
     do_action('swpmb_before_save_post', $post_id);
     do_action("swpmb_{$this->meta_box['id']}_before_save_post", $post_id);
     foreach ($this->fields as $field) {
         $name = $field['id'];
         $single = $field['clone'] || !$field['multiple'];
         $old = get_post_meta($post_id, $name, $single);
         $new = isset($_POST[$name]) ? $_POST[$name] : ($single ? '' : array());
         // Allow field class change the value
         $new = call_user_func(array(self::get_class_name($field), 'value'), $new, $old, $post_id, $field);
         $new = SWPMB_Core::filter('value', $new, $field, $old);
         // Call defined method to save meta value, if there's no methods, call common one
         call_user_func(array(self::get_class_name($field), 'save'), $new, $old, $post_id, $field);
     }
     // After save action
     do_action('swpmb_after_save_post', $post_id);
     do_action("swpmb_{$this->meta_box['id']}_after_save_post", $post_id);
 }
示例#4
0
文件: field.php 项目: arobbins/sblog
 /**
  * Show field HTML
  * Filters are put inside this method, not inside methods such as "meta", "html", "begin_html", etc.
  * That ensures the returned value are always been applied filters
  * This method is not meant to be overwritten in specific fields
  *
  * @param array $field
  * @param bool  $saved
  *
  * @return string
  */
 static function show($field, $saved)
 {
     $post = get_post();
     $post_id = isset($post->ID) ? $post->ID : 0;
     $field_class = SWP_Meta_Box::get_class_name($field);
     $meta = call_user_func(array($field_class, 'meta'), $post_id, $saved, $field);
     $meta = SWPMB_Core::filter('field_meta', $meta, $field, $saved);
     $begin = call_user_func(array($field_class, 'begin_html'), $meta, $field);
     $begin = SWPMB_Core::filter('begin_html', $begin, $field, $meta);
     // Separate code for cloneable and non-cloneable fields to make easy to maintain
     // Cloneable fields
     if ($field['clone']) {
         $field_html = '';
         /**
          * Note: $meta must contain value so that the foreach loop runs!
          * @see meta()
          */
         foreach ($meta as $index => $sub_meta) {
             $sub_field = $field;
             $sub_field['field_name'] = $field['field_name'] . "[{$index}]";
             if ($index > 0) {
                 if (isset($sub_field['address_field'])) {
                     $sub_field['address_field'] = $field['address_field'] . "_{$index}";
                 }
                 $sub_field['id'] = $field['id'] . "_{$index}";
             }
             if ($field['multiple']) {
                 $sub_field['field_name'] .= '[]';
             }
             // Wrap field HTML in a div with class="swpmb-clone" if needed
             $class = "swpmb-clone swpmb-{$field['type']}-clone";
             $sort_icon = '';
             if ($field['sort_clone']) {
                 $class .= ' swpmb-sort-clone';
                 $sort_icon = "<a href='javascript:;' class='swpmb-clone-icon'></a>";
             }
             $input_html = "<div class='{$class}'>" . $sort_icon;
             // Call separated methods for displaying each type of field
             $input_html .= call_user_func(array($field_class, 'html'), $sub_meta, $sub_field);
             $input_html = SWPMB_Core::filter('html', $input_html, $sub_field, $sub_meta);
             // Remove clone button
             $input_html .= call_user_func(array($field_class, 'remove_clone_button'), $sub_field);
             $input_html .= '</div>';
             $field_html .= $input_html;
         }
     } else {
         // Call separated methods for displaying each type of field
         $field_html = call_user_func(array($field_class, 'html'), $meta, $field);
         $field_html = SWPMB_Core::filter('html', $field_html, $field, $meta);
     }
     $end = call_user_func(array($field_class, 'end_html'), $meta, $field);
     $end = SWPMB_Core::filter('end_html', $end, $field, $meta);
     $html = SWPMB_Core::filter('wrapper_html', "{$begin}{$field_html}{$end}", $field, $meta);
     // Display label and input in DIV and allow user-defined classes to be appended
     $classes = "swpmb-field swpmb-{$field['type']}-wrapper " . $field['class'];
     if ('hidden' === $field['type']) {
         $classes .= ' hidden';
     }
     if (!empty($field['required'])) {
         $classes .= ' required';
     }
     $outer_html = sprintf($field['before'] . '<div class="%s">%s</div>' . $field['after'], trim($classes), $html);
     $outer_html = SWPMB_Core::filter('outer_html', $outer_html, $field, $meta);
     echo $outer_html;
 }