Beispiel #1
0
 /**
  * @param $post_id
  * @param $post
  *
  * @return mixed
  */
 public function save_post($post_id, $post)
 {
     $is_new_item = false;
     if ('new' == self::$old_post_status) {
         $is_new_item = true;
     }
     // Reset to avoid manual new post issues
     self::$old_post_status = '';
     $blacklisted_types = array('revision', '_pods_pod', '_pods_field');
     $blacklisted_types = apply_filters('pods_meta_save_post_blacklist_types', $blacklisted_types, $post_id, $post);
     if (empty($_POST) || 1 != pods_var('pods_metasave', 'post')) {
         return $post_id;
     }
     // @todo Figure out how to hook into autosave for saving meta
     // Block Autosave and Revisions
     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || in_array($post->post_type, $blacklisted_types)) {
         return $post_id;
     }
     // Block Quick Edits / Bulk Edits
     if ('edit.php' == pods_var('pagenow', 'global') && ('inline-save' == pods_var('action', 'post') || null != pods_var('bulk_edit', 'get') || is_array(pods_var('post', 'get')))) {
         return $post_id;
     }
     // Block Trash
     if (in_array(pods_var('action', 'get'), array('untrash', 'trash'))) {
         return $post_id;
     }
     // Block Auto-drafting and Trash (not via Admin action)
     $blacklisted_status = array('auto-draft', 'trash');
     $blacklisted_status = apply_filters('pods_meta_save_post_blacklist_status', $blacklisted_status, $post_id, $post);
     if (in_array($post->post_status, $blacklisted_status)) {
         return $post_id;
     }
     $groups = $this->groups_get('post_type', $post->post_type);
     if (empty($groups)) {
         return $post_id;
     }
     $data = array();
     $id = $post_id;
     $pod = null;
     foreach ($groups as $group) {
         if (empty($group['fields'])) {
             continue;
         }
         if (null === $pod) {
             $pod = pods($group['pod']['name'], $id, true);
         }
         foreach ($group['fields'] as $field) {
             if (false === PodsForm::permission($field['type'], $field['name'], $field, $group['fields'], $pod, $id)) {
                 if (!pods_var('hidden', $field['options'], false, null, true)) {
                     continue;
                 }
             }
             $data[$field['name']] = '';
             if (isset($_POST['pods_meta_' . $field['name']])) {
                 $data[$field['name']] = $_POST['pods_meta_' . $field['name']];
             }
         }
     }
     if ($is_new_item) {
         do_action('pods_meta_create_pre_post', $data, $pod, $id, $groups, $post, $post->post_type);
         do_action("pods_meta_create_pre_post_{$post->post_type}", $data, $pod, $id, $groups, $post);
     }
     do_action('pods_meta_save_pre_post', $data, $pod, $id, $groups, $post, $post->post_type);
     do_action("pods_meta_save_pre_post_{$post->post_type}", $data, $pod, $id, $groups, $post);
     pods_no_conflict_on('post');
     if (!empty($pod)) {
         // Fix for Pods doing it's own sanitization
         $data = stripslashes_deep($data);
         $pod->save($data, null, null, array('is_new_item' => $is_new_item));
     } elseif (!empty($id)) {
         foreach ($data as $field => $value) {
             update_post_meta($id, $field, $value);
         }
     }
     pods_no_conflict_off('post');
     if ($is_new_item) {
         do_action('pods_meta_create_post', $data, $pod, $id, $groups, $post, $post->post_type);
         do_action("pods_meta_create_post_{$post->post_type}", $data, $pod, $id, $groups, $post);
     }
     do_action('pods_meta_save_post', $data, $pod, $id, $groups, $post, $post->post_type);
     do_action("pods_meta_save_post_{$post->post_type}", $data, $pod, $id, $groups, $post);
     return $post_id;
 }