PodsAPI::save_wp_object PHP Method

save_wp_object() public method

Save a WP object and its meta
Since: 2.0
public save_wp_object ( string $object_type, array $data, array $meta = [], boolean $strict = false, boolean $sanitized = false, array $fields = [] ) : boolean | mixed
$object_type string Object type: post|taxonomy|user|comment|setting
$data array All post data to be saved
$meta array (optional) Associative array of meta keys and values
$strict boolean (optional) Decides whether the previous saved meta should be deleted or not
$sanitized boolean (optional) Will unsanitize the data, should be passed if the data is sanitized before sending.
$fields array (optional) The array of fields and their options, for further processing with
return boolean | mixed
    public function save_wp_object($object_type, $data, $meta = array(), $strict = false, $sanitized = false, $fields = array())
    {
        if (in_array($object_type, array('post_type', 'media'))) {
            $object_type = 'post';
        }
        if ('taxonomy' == $object_type) {
            $object_type = 'term';
        }
        if ($sanitized) {
            $data = pods_unsanitize($data);
            $meta = pods_unsanitize($meta);
        }
        if (in_array($object_type, array('post', 'term', 'user', 'comment'))) {
            return call_user_func(array($this, 'save_' . $object_type), $data, $meta, $strict, false, $fields);
        } elseif ('settings' == $object_type) {
            // Nothing to save
            if (empty($meta)) {
                return true;
            }
            return $this->save_setting(pods_var('option_id', $data), $meta, false);
        }
        return false;
    }

Usage Example

Example #1
0
 /**
  * Save the value to the DB
  *
  * @param mixed $value
  * @param int $id
  * @param string $name
  * @param array $options
  * @param array $fields
  * @param array $pod
  * @param object $params
  *
  * @since 2.3
  */
 public function save($value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null)
 {
     if (empty(self::$api)) {
         self::$api = pods_api();
     }
     // File title / field handling
     foreach ($value as $id) {
         $title = false;
         if (is_array($id)) {
             if (isset($id['title']) && 0 < strlen(trim($id['title']))) {
                 $title = trim($id['title']);
             }
             if (isset($id['id'])) {
                 $id = (int) $id['id'];
             } else {
                 $id = 0;
             }
         }
         if (empty($id)) {
             continue;
         }
         // Update the title if set
         if (false !== $title && 1 == pods_var(self::$type . '_edit_title', $options, 0)) {
             $attachment_data = array('ID' => $id, 'post_title' => $title);
             self::$api->save_wp_object('media', $attachment_data);
         }
     }
 }