PodsAPI::save_object PHP Method

save_object() public method

$params['id'] int The Object ID $params['name'] string The Object name $params['type'] string The Object type $params['options'] Associative array of Object options
Since: 2.0
public save_object ( array | object $params, boolean $sanitized = false ) : integer
$params array | object An associative array of parameters
$sanitized boolean (optional) Decides whether the params have been sanitized before being passed, will sanitize them if false.
return integer The Object ID
    public function save_object($params, $sanitized = false)
    {
        $params = (object) $params;
        if (false === $sanitized) {
            $params = pods_sanitize($params);
        }
        if (!isset($params->name) || empty($params->name)) {
            return pods_error(__('Name must be given to save an Object', 'pods'), $this);
        }
        if (!isset($params->type) || empty($params->type)) {
            return pods_error(__('Type must be given to save an Object', 'pods'), $this);
        }
        $object = array('id' => 0, 'name' => $params->name, 'type' => $params->type, 'code' => '', 'options' => array());
        // Setup options
        $options = get_object_vars($params);
        if (isset($options['method'])) {
            unset($options['method']);
        }
        $exclude = array('id', 'name', 'helper_type', 'code', 'options', 'status');
        foreach ($exclude as $k => $exclude_field) {
            $aliases = array($exclude_field);
            if (is_array($exclude_field)) {
                $aliases = array_merge(array($k), $exclude_field);
                $exclude_field = $k;
            }
            foreach ($aliases as $alias) {
                if (isset($options[$alias])) {
                    $object[$exclude_field] = pods_trim($options[$alias]);
                    unset($options[$alias]);
                }
            }
        }
        if ('helper' == $object['type']) {
            $object['options']['helper_type'] = $object['helper_type'];
        }
        if (isset($object['options']['code'])) {
            unset($object['options']['code']);
        }
        $object['options'] = array_merge($object['options'], $options);
        $post_data = array('post_name' => pods_clean_name($object['name'], true), 'post_title' => $object['name'], 'post_content' => $object['code'], 'post_type' => '_pods_' . $object['type'], 'post_status' => 'publish');
        if (!empty($object['id'])) {
            $post_data['ID'] = $object['id'];
        }
        if (null !== pods_var('status', $object, null, null, true)) {
            $post_data['post_status'] = pods_var('status', $object, null, null, true);
        }
        remove_filter('content_save_pre', 'balanceTags', 50);
        $post_data = pods_sanitize($post_data);
        $params->id = $this->save_post($post_data, $object['options'], true, true);
        pods_transient_clear('pods_objects_' . $params->type);
        pods_transient_clear('pods_objects_' . $params->type . '_get');
        return $params->id;
    }