WP_REST_Posts_Controller::can_access_password_content() WordPress Method

The WP_REST_Posts_Controller::can_access_password_content() method is used to determine whether the current user can access password-protected post content.

WP_REST_Posts_Controller::can_access_password_content( WP_Post $post, WP_REST_Request $request ) #

Checks if the user can access password-protected content.


Description

This method determines whether we need to override the regular password check in core with a filter.


Top ↑

Parameters

$post

(WP_Post)(Required)Post to check against.

$request

(WP_REST_Request)(Required)Request data to check.


Top ↑

Return

(bool) True if the user can access password-protected content, otherwise false.


Top ↑

Source

File: wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

	public function can_access_password_content( $post, $request ) {
		if ( empty( $post->post_password ) ) {
			// No filter required.
			return false;
		}

		/*
		 * Users always gets access to password protected content in the edit
		 * context if they have the `edit_post` meta capability.
		 */
		if (
			'edit' === $request['context'] &&
			current_user_can( 'edit_post', $post->ID )
		) {
			return true;
		}

		// No password, no auth.
		if ( empty( $request['password'] ) ) {
			return false;
		}

		// Double-check the request password.
		return hash_equals( $post->post_password, $request['password'] );
	}


Top ↑

Changelog

Changelog
VersionDescription
4.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.