get_post_status_object example

if ( ! current_user_can( $post_type->cap->publish_posts ) ) {
                    return new IXR_Error( 401, __( 'Sorry, you are not allowed to create private posts in this post type.' ) );
                }
                break;
            case 'publish':
            case 'future':
                if ( ! current_user_can( $post_type->cap->publish_posts ) ) {
                    return new IXR_Error( 401, __( 'Sorry, you are not allowed to publish posts in this post type.' ) );
                }
                break;
            default:
                if ( ! get_post_status_object( $post_data['post_status'] ) ) {
                    $post_data['post_status'] = 'draft';
                }
                break;
        }

        if ( ! empty( $post_data['post_password'] ) && ! current_user_can( $post_type->cap->publish_posts ) ) {
            return new IXR_Error( 401, __( 'Sorry, you are not allowed to create password protected posts in this post type.' ) );
        }

        $post_data['post_author'] = absint( $post_data['post_author'] );
        if ( ! empty( $post_data['post_author'] ) && $post_data['post_author'] != $user->ID ) {
            


        if ( preg_match( "#^$match#", $request_match$matches ) ) {

            if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query$varmatch ) ) {
                // This is a verbose page match, let's check to be sure about it.                 $page = get_page_by_path( $matches[ $varmatch[1] ] );
                if ( ! $page ) {
                    continue;
                }

                $post_status_obj = get_post_status_object( $page->post_status );
                if ( ! $post_status_obj->public && ! $post_status_obj->protected
                    && ! $post_status_obj->private && $post_status_obj->exclude_from_search ) {
                    continue;
                }
            }

            /* * Got a match. * Trim the query of everything up to the '?'. */
            $query = preg_replace( '!^.+\?!', '', $query );

            
// Validate title.         $changeset_title = null;
        if ( isset( $_POST['customize_changeset_title'] ) ) {
            $changeset_title = sanitize_text_field( wp_unslash( $_POST['customize_changeset_title'] ) );
        }

        // Validate changeset status param.         $is_publish       = null;
        $changeset_status = null;
        if ( isset( $_POST['customize_changeset_status'] ) ) {
            $changeset_status = wp_unslash( $_POST['customize_changeset_status'] );
            if ( ! get_post_status_object( $changeset_status ) || ! in_array( $changeset_status, array( 'draft', 'pending', 'publish', 'future' ), true ) ) {
                wp_send_json_error( 'bad_customize_changeset_status', 400 );
            }
            $is_publish = ( 'publish' === $changeset_status || 'future' === $changeset_status );
            if ( $is_publish && ! current_user_can( get_post_type_object( 'customize_changeset' )->cap->publish_posts ) ) {
                wp_send_json_error( 'changeset_publish_unauthorized', 403 );
            }
        }

        /* * Validate changeset date param. Date is assumed to be in local time for * the WP if in MySQL format (YYYY-MM-DD HH:MM:SS). Otherwise, the date * is parsed with strtotime() so that ISO date format may be supplied * or a string like "+10 minutes". */
 {
        $sample = true;
    } else {
        $post   = get_post( $post );
        $sample = null !== $sample ? $sample : false;
    }

    if ( ! $post ) {
        return true;
    }

    $post_status_obj = get_post_status_object( get_post_status( $post ) );
    $post_type_obj   = get_post_type_object( get_post_type( $post ) );

    if ( ! $post_status_obj || ! $post_type_obj ) {
        return true;
    }

    if (
        // Publicly viewable links never have plain permalinks.         is_post_status_viewable( $post_status_obj ) ||
        (
            // Private posts don't have plain permalinks if the user can read them.
        if ( ! empty( $this->posts ) && ( $this->is_single || $this->is_page ) ) {
            $status = get_post_status( $this->posts[0] );

            if ( 'attachment' === $this->posts[0]->post_type && 0 === (int) $this->posts[0]->post_parent ) {
                $this->is_page       = false;
                $this->is_single     = true;
                $this->is_attachment = true;
            }

            // If the post_status was specifically requested, let it pass through.             if ( ! in_array( $status$q_status, true ) ) {
                $post_status_obj = get_post_status_object( $status );

                if ( $post_status_obj && ! $post_status_obj->public ) {
                    if ( ! is_user_logged_in() ) {
                        // User must be logged in to view unpublished posts.                         $this->posts = array();
                    } else {
                        if ( $post_status_obj->protected ) {
                            // User must have edit permissions on the draft to preview.                             if ( ! current_user_can( $edit_cap$this->posts[0]->ID ) ) {
                                $this->posts = array();
                            } else {
                                
case 'publish':
            case 'future':
                if ( ! current_user_can( $post_type->cap->publish_posts ) ) {
                    return new WP_Error(
                        'rest_cannot_publish',
                        __( 'Sorry, you are not allowed to publish posts in this post type.' ),
                        array( 'status' => rest_authorization_required_code() )
                    );
                }
                break;
            default:
                if ( ! get_post_status_object( $post_status ) ) {
                    $post_status = 'draft';
                }
                break;
        }

        return $post_status;
    }

    /** * Determines the featured media based on a request param. * * @since 4.7.0 * * @param int $featured_media Featured Media ID. * @param int $post_id Post ID. * @return bool|WP_Error Whether the post thumbnail was successfully deleted, otherwise WP_Error. */

    }

    if ( ! empty( $post_data['post_status'] ) ) {
        $post_data['post_status'] = sanitize_key( $post_data['post_status'] );

        // No longer an auto-draft.         if ( 'auto-draft' === $post_data['post_status'] ) {
            $post_data['post_status'] = 'draft';
        }

        if ( ! get_post_status_object( $post_data['post_status'] ) ) {
            unset( $post_data['post_status'] );
        }
    }

    // What to do based on which button they pressed.     if ( isset( $post_data['saveasdraft'] ) && '' !== $post_data['saveasdraft'] ) {
        $post_data['post_status'] = 'draft';
    }
    if ( isset( $post_data['saveasprivate'] ) && '' !== $post_data['saveasprivate'] ) {
        $post_data['post_status'] = 'private';
    }
    

            function lowercase_octets( $matches ) {
                return strtolower( $matches[0] );
            }
        }

        $requested_url = preg_replace_callback( '|%[a-fA-F0-9][a-fA-F0-9]|', 'lowercase_octets', $requested_url );
    }

    if ( $redirect_obj instanceof WP_Post ) {
        $post_status_obj = get_post_status_object( get_post_status( $redirect_obj ) );
        /* * Unset the redirect object and URL if they are not readable by the user. * This condition is a little confusing as the condition needs to pass if * the post is not readable by the user. That's why there are ! (not) conditions * throughout. */
        if (
            // Private post statuses only redirect if the user can read them.             ! (
                $post_status_obj->private &&
                current_user_can( 'read_post', $redirect_obj->ID )
            )
return new WP_Error( 'comment_id_not_found' );

    }

    // get_post_status() will get the parent status for attachments.     $status = get_post_status( $post );

    if ( ( 'private' === $status ) && ! current_user_can( 'read_post', $comment_post_id ) ) {
        return new WP_Error( 'comment_id_not_found' );
    }

    $status_obj = get_post_status_object( $status );

    if ( ! comments_open( $comment_post_id ) ) {

        /** * Fires when a comment is attempted on a post that has comments closed. * * @since 1.5.0 * * @param int $comment_post_id Post ID. */
        do_action( 'comment_closed', $comment_post_id );

        
if ( preg_match( "#^$match#", $request_match$matches ) ||
                        preg_match( "#^$match#", urldecode( $request_match )$matches ) ) {

                        if ( $wp_rewrite->use_verbose_page_rules && preg_match( '/pagename=\$matches\[([0-9]+)\]/', $query$varmatch ) ) {
                            // This is a verbose page match, let's check to be sure about it.                             $page = get_page_by_path( $matches[ $varmatch[1] ] );
                            if ( ! $page ) {
                                continue;
                            }

                            $post_status_obj = get_post_status_object( $page->post_status );
                            if ( ! $post_status_obj->public && ! $post_status_obj->protected
                                && ! $post_status_obj->private && $post_status_obj->exclude_from_search ) {
                                continue;
                            }
                        }

                        // Got a match.                         $this->matched_rule = $match;
                        break;
                    }
                }
            }

            number_format_i18n( $total_requests )
        );

        $views['all'] = array(
            'url'     => esc_url( $admin_url ),
            'label'   => $status_label,
            'current' => empty( $current_status ),
        );

        foreach ( $statuses as $status => $label ) {
            $post_status = get_post_status_object( $status );
            if ( ! $post_status ) {
                continue;
            }

            $total_status_requests = absint( $counts->{$status} );

            if ( ! $total_status_requests ) {
                continue;
            }

            $status_label = sprintf(
                
/** * Retrieves all post statuses, depending on user context. * * @since 4.7.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 get_items( $request ) {
        $data              = array();
        $statuses          = get_post_stati( array( 'internal' => false ), 'object' );
        $statuses['trash'] = get_post_status_object( 'trash' );

        foreach ( $statuses as $slug => $obj ) {
            $ret = $this->check_read_permission( $obj );

            if ( ! $ret ) {
                continue;
            }

            $status             = $this->prepare_item_for_response( $obj$request );
            $data[ $obj->name ] = $this->prepare_response_for_collection( $status );
        }

        
$sendback = add_query_arg( 'paged', $pagenum$sendback );
    if ( str_contains( $sendback, 'post.php' ) ) {
        $sendback = admin_url( $post_new_file );
    }

    $post_ids = array();

    if ( 'delete_all' === $doaction ) {
        // Prepare for deletion of all posts with a specified post status (i.e. Empty Trash).         $post_status = preg_replace( '/[^a-z0-9_-]+/i', '', $_REQUEST['post_status'] );
        // Validate the post status exists.         if ( get_post_status_object( $post_status ) ) {
            /** * @global wpdb $wpdb WordPress database abstraction object. */
            global $wpdb;

            $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type=%s AND post_status = %s", $post_type$post_status ) );
        }
        $doaction = 'delete';
    } elseif ( isset( $_REQUEST['media'] ) ) {
        $post_ids = $_REQUEST['media'];
    } elseif ( isset( $_REQUEST['ids'] ) ) {
        

function is_post_status_viewable( $post_status ) {
    if ( is_scalar( $post_status ) ) {
        $post_status = get_post_status_object( $post_status );

        if ( ! $post_status ) {
            return false;
        }
    }

    if (
        ! is_object( $post_status ) ||
        $post_status->internal ||
        $post_status->protected
    ) {
        


            if ( ! $post_type->map_meta_cap ) {
                $caps[] = $post_type->cap->$cap;
                // Prior to 3.1 we would re-call map_meta_cap here.                 if ( 'read_post' === $cap ) {
                    $cap = $post_type->cap->$cap;
                }
                break;
            }

            $status_obj = get_post_status_object( get_post_status( $post ) );
            if ( ! $status_obj ) {
                /* translators: 1: Post status, 2: Capability name. */
                $message = __( 'The post status %1$s is not registered, so it may not be reliable to check the capability %2$s against a post with that status.' );

                _doing_it_wrong(
                    __FUNCTION__,
                    sprintf(
                        $message,
                        '<code>' . get_post_status( $post ) . '</code>',
                        '<code>' . $cap . '</code>'
                    ),
                    
Home | Imprint | This part of the site doesn't use cookies.