prepare_item_for_database example



    /** * Prepares a single term for create or update. * * @since 5.9.0 * * @param WP_REST_Request $request Request object. * @return object Prepared term data. */
    public function prepare_item_for_database( $request ) {
        $prepared_term = parent::prepare_item_for_database( $request );

        $schema = $this->get_item_schema();

        if ( isset( $request['name'] ) && ! empty( $schema['properties']['name'] ) ) {
            $prepared_term->{'menu-name'} = $request['name'];
        }

        return $prepared_term;
    }

    /** * Creates a single term in a taxonomy. * * @since 5.9.0 * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */

    public function create_item( $request ) {
        if ( ! empty( $request['id'] ) ) {
            return new WP_Error(
                'rest_post_exists',
                __( 'Cannot create existing post.' ),
                array( 'status' => 400 )
            );
        }

        $prepared_post = $this->prepare_item_for_database( $request );

        if ( is_wp_error( $prepared_post ) ) {
            return $prepared_post;
        }

        $prepared_post->post_type = $this->post_type;

        if ( ! empty( $prepared_post->post_name )
            && ! empty( $prepared_post->post_status )
            && in_array( $prepared_post->post_status, array( 'draft', 'pending' ), true )
        ) {
            
$schema = $this->get_item_schema();

        if ( ! empty( $request['roles'] ) && ! empty( $schema['properties']['roles'] ) ) {
            $check_permission = $this->check_role_update( $request['id']$request['roles'] );

            if ( is_wp_error( $check_permission ) ) {
                return $check_permission;
            }
        }

        $user = $this->prepare_item_for_database( $request );

        if ( is_multisite() ) {
            $ret = wpmu_validate_user_signup( $user->user_login, $user->user_email );

            if ( is_wp_error( $ret['errors'] ) && $ret['errors']->has_errors() ) {
                $error = new WP_Error(
                    'rest_invalid_param',
                    __( 'Invalid user parameter(s).' ),
                    array( 'status' => 400 )
                );

                
$parent = get_term( (int) $request['parent']$this->taxonomy );

            if ( ! $parent ) {
                return new WP_Error(
                    'rest_term_invalid',
                    __( 'Parent term does not exist.' ),
                    array( 'status' => 400 )
                );
            }
        }

        $prepared_term = $this->prepare_item_for_database( $request );

        $term = wp_insert_term( wp_slash( $prepared_term->name )$this->taxonomy, wp_slash( (array) $prepared_term ) );
        if ( is_wp_error( $term ) ) {
            /* * If we're going to inform the client that the term already exists, * give them the identifier for future use. */
            $term_id = $term->get_error_data( 'term_exists' );
            if ( $term_id ) {
                $existing_term = get_term( $term_id$this->taxonomy );
                $term->add_data( $existing_term->term_id, 'term_exists' );
                
if ( isset( $request['source'] ) && 'theme' === $request['source'] ) {
            wp_delete_post( $template->wp_id, true );
            $request->set_param( 'context', 'edit' );

            $template = get_block_template( $request['id']$this->post_type );
            $response = $this->prepare_item_for_response( $template$request );

            return rest_ensure_response( $response );
        }

        $changes = $this->prepare_item_for_database( $request );

        if ( is_wp_error( $changes ) ) {
            return $changes;
        }

        if ( 'custom' === $template->source ) {
            $update = true;
            $result = wp_update_post( wp_slash( (array) $changes ), false );
        } else {
            $update      = false;
            $post_before = null;
            

    public function update_item( $request ) {
        $post_before = $this->get_post( $request['id'] );
        if ( is_wp_error( $post_before ) ) {
            return $post_before;
        }

        $changes = $this->prepare_item_for_database( $request );
        if ( is_wp_error( $changes ) ) {
            return $changes;
        }

        $result = wp_update_post( wp_slash( (array) $changes ), true, false );
        if ( is_wp_error( $result ) ) {
            return $result;
        }

        $post          = get_post( $request['id'] );
        $fields_update = $this->update_additional_fields_for_object( $post$request );
        

    public function create_item( $request ) {
        $user = $this->get_user( $request );

        if ( is_wp_error( $user ) ) {
            return $user;
        }

        $prepared = $this->prepare_item_for_database( $request );

        if ( is_wp_error( $prepared ) ) {
            return $prepared;
        }

        $created = WP_Application_Passwords::create_new_application_password( $user->ID, wp_slash( (array) $prepared ) );

        if ( is_wp_error( $created ) ) {
            return $created;
        }

        
if ( ! empty( $image_meta ) ) {
            if ( empty( $request['title'] ) && trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
                $request['title'] = $image_meta['title'];
            }

            if ( empty( $request['caption'] ) && trim( $image_meta['caption'] ) ) {
                $request['caption'] = $image_meta['caption'];
            }
        }

        $attachment = $this->prepare_item_for_database( $request );

        $attachment->post_mime_type = $type;
        $attachment->guid           = $url;

        if ( empty( $attachment->post_title ) ) {
            $attachment->post_title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) );
        }

        // $post_parent is inherited from $attachment['post_parent'].         $id = wp_insert_attachment( wp_slash( (array) $attachment )$file, 0, true, false );

        


        // Do not allow comments to be created with a non-default type.         if ( ! empty( $request['type'] ) && 'comment' !== $request['type'] ) {
            return new WP_Error(
                'rest_invalid_comment_type',
                __( 'Cannot create a comment with that type.' ),
                array( 'status' => 400 )
            );
        }

        $prepared_comment = $this->prepare_item_for_database( $request );
        if ( is_wp_error( $prepared_comment ) ) {
            return $prepared_comment;
        }

        $prepared_comment['comment_type'] = 'comment';

        if ( ! isset( $prepared_comment['comment_content'] ) ) {
            $prepared_comment['comment_content'] = '';
        }

        if ( ! $this->check_is_comment_content_allowed( $prepared_comment ) ) {
            
if ( ! defined( 'DOING_AUTOSAVE' ) ) {
            define( 'DOING_AUTOSAVE', true );
        }

        $post = get_post( $request['id'] );

        if ( is_wp_error( $post ) ) {
            return $post;
        }

        $prepared_post     = $this->parent_controller->prepare_item_for_database( $request );
        $prepared_post->ID = $post->ID;
        $user_id           = get_current_user_id();

        // We need to check post lock to ensure the original author didn't leave their browser tab open.         if ( ! function_exists( 'wp_check_post_lock' ) ) {
            require_once ABSPATH . 'wp-admin/includes/post.php';
        }

        $post_lock = wp_check_post_lock( $post->ID );
        $is_draft  = 'draft' === $post->post_status || 'auto-draft' === $post->post_status;

        

    public function create_item( $request ) {
        if ( ! empty( $request['id'] ) ) {
            return new WP_Error( 'rest_post_exists', __( 'Cannot create existing post.' ), array( 'status' => 400 ) );
        }

        $prepared_nav_item = $this->prepare_item_for_database( $request );

        if ( is_wp_error( $prepared_nav_item ) ) {
            return $prepared_nav_item;
        }
        $prepared_nav_item = (array) $prepared_nav_item;

        $nav_menu_item_id = wp_update_nav_menu_item( $prepared_nav_item['menu-id']$prepared_nav_item['menu-item-db-id']wp_slash( $prepared_nav_item ), false );
        if ( is_wp_error( $nav_menu_item_id ) ) {
            if ( 'db_insert_error' === $nav_menu_item_id->get_error_code() ) {
                $nav_menu_item_id->add_data( array( 'status' => 500 ) );
            } else {
                
Home | Imprint | This part of the site doesn't use cookies.