Merged in feature/MAW-855-import-code-into-aws (pull request #2)
code import from pantheon * code import from pantheon
This commit is contained in:
@@ -0,0 +1,401 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Premium\Actions;
|
||||
|
||||
use RuntimeException;
|
||||
use WP_User;
|
||||
use WPSEO_Addon_Manager;
|
||||
use Yoast\WP\SEO\Helpers\Options_Helper;
|
||||
use Yoast\WP\SEO\Helpers\User_Helper;
|
||||
use Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Forbidden_Exception;
|
||||
use Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Unauthorized_Exception;
|
||||
use Yoast\WP\SEO\Premium\Helpers\AI_Generator_Helper;
|
||||
|
||||
/**
|
||||
* Handles the actual requests to our API endpoints.
|
||||
*/
|
||||
class AI_Generator_Action {
|
||||
|
||||
/**
|
||||
* The AI_Generator helper.
|
||||
*
|
||||
* @var AI_Generator_Helper
|
||||
*/
|
||||
protected $ai_generator_helper;
|
||||
|
||||
/**
|
||||
* The Options helper.
|
||||
*
|
||||
* @var Options_Helper
|
||||
*/
|
||||
protected $options_helper;
|
||||
|
||||
/**
|
||||
* The User helper.
|
||||
*
|
||||
* @var User_Helper
|
||||
*/
|
||||
protected $user_helper;
|
||||
|
||||
/**
|
||||
* The add-on manager.
|
||||
*
|
||||
* @var WPSEO_Addon_Manager
|
||||
*/
|
||||
private $addon_manager;
|
||||
|
||||
/**
|
||||
* AI_Generator_Action constructor.
|
||||
*
|
||||
* @param AI_Generator_Helper $ai_generator_helper The AI_Generator helper.
|
||||
* @param Options_Helper $options_helper The Options helper.
|
||||
* @param User_Helper $user_helper The User helper.
|
||||
* @param WPSEO_Addon_Manager $addon_manager The add-on manager.
|
||||
*/
|
||||
public function __construct(
|
||||
AI_Generator_Helper $ai_generator_helper,
|
||||
Options_Helper $options_helper,
|
||||
User_Helper $user_helper,
|
||||
WPSEO_Addon_Manager $addon_manager
|
||||
) {
|
||||
$this->ai_generator_helper = $ai_generator_helper;
|
||||
$this->options_helper = $options_helper;
|
||||
$this->user_helper = $user_helper;
|
||||
$this->addon_manager = $addon_manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests a new set of JWT tokens.
|
||||
*
|
||||
* Requests a new JWT access and refresh token for a user from the Yoast AI Service and stores it in the database
|
||||
* under usermeta. The storing of the token happens in a HTTP callback that is triggered by this request.
|
||||
*
|
||||
* @param \WP_User $user The WP user.
|
||||
*
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Bad_Request_Exception Bad_Request_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Forbidden_Exception Forbidden_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Internal_Server_Error_Exception Internal_Server_Error_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Not_Found_Exception Not_Found_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Payment_Required_Exception Payment_Required_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Request_Timeout_Exception Request_Timeout_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Service_Unavailable_Exception Service_Unavailable_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Too_Many_Requests_Exception Too_Many_Requests_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Unauthorized_Exception Unauthorized_Exception.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function token_request( WP_User $user ): void {
|
||||
// Ensure the user has given consent.
|
||||
if ( $this->user_helper->get_meta( $user->ID, '_yoast_wpseo_ai_consent', true ) !== '1' ) {
|
||||
throw $this->handle_consent_revoked( $user->ID );
|
||||
}
|
||||
|
||||
// Generate a verification code and store it in the database.
|
||||
$code_verifier = $this->ai_generator_helper->generate_code_verifier( $user );
|
||||
$this->ai_generator_helper->set_code_verifier( $user->ID, $code_verifier );
|
||||
|
||||
$request_body = [
|
||||
'service' => 'openai',
|
||||
'code_challenge' => \hash( 'sha256', $code_verifier ),
|
||||
'license_site_url' => $this->ai_generator_helper->get_license_url(),
|
||||
'user_id' => (string) $user->ID,
|
||||
'callback_url' => $this->ai_generator_helper->get_callback_url(),
|
||||
'refresh_callback_url' => $this->ai_generator_helper->get_refresh_callback_url(),
|
||||
];
|
||||
|
||||
$this->ai_generator_helper->request( '/token/request', $request_body );
|
||||
|
||||
// The callback saves the metadata. Because that is in another session, we need to delete the current cache here. Or we may get the old token.
|
||||
\wp_cache_delete( $user->ID, 'user_meta' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Refreshes the JWT access token.
|
||||
*
|
||||
* Refreshes a stored JWT access token for a user with the Yoast AI Service and stores it in the database under
|
||||
* usermeta. The storing of the token happens in a HTTP callback that is triggered by this request.
|
||||
*
|
||||
* @param \WP_User $user The WP user.
|
||||
*
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Bad_Request_Exception Bad_Request_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Forbidden_Exception Forbidden_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Internal_Server_Error_Exception Internal_Server_Error_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Not_Found_Exception Not_Found_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Payment_Required_Exception Payment_Required_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Request_Timeout_Exception Request_Timeout_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Service_Unavailable_Exception Service_Unavailable_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Too_Many_Requests_Exception Too_Many_Requests_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Unauthorized_Exception Unauthorized_Exception.
|
||||
* @throws \RuntimeException Unable to retrieve the refresh token.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function token_refresh( WP_User $user ): void {
|
||||
$refresh_jwt = $this->ai_generator_helper->get_refresh_token( $user->ID );
|
||||
|
||||
// Generate a verification code and store it in the database.
|
||||
$code_verifier = $this->ai_generator_helper->generate_code_verifier( $user );
|
||||
$this->ai_generator_helper->set_code_verifier( $user->ID, $code_verifier );
|
||||
|
||||
$request_body = [
|
||||
'code_challenge' => \hash( 'sha256', $code_verifier ),
|
||||
];
|
||||
$request_headers = [
|
||||
'Authorization' => "Bearer $refresh_jwt",
|
||||
];
|
||||
|
||||
$this->ai_generator_helper->request( '/token/refresh', $request_body, $request_headers );
|
||||
|
||||
// The callback saves the metadata. Because that is in another session, we need to delete the current cache here. Or we may get the old token.
|
||||
\wp_cache_delete( $user->ID, 'user_meta' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback function that will be invoked by our API.
|
||||
*
|
||||
* @param string $access_jwt The access JWT.
|
||||
* @param string $refresh_jwt The refresh JWT.
|
||||
* @param string $code_challenge The verification code.
|
||||
* @param int $user_id The user ID.
|
||||
*
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Unauthorized_Exception Unauthorized_Exception.
|
||||
*
|
||||
* @return string The code verifier.
|
||||
*/
|
||||
public function callback(
|
||||
string $access_jwt,
|
||||
string $refresh_jwt,
|
||||
string $code_challenge,
|
||||
int $user_id
|
||||
): string {
|
||||
try {
|
||||
$code_verifier = $this->ai_generator_helper->get_code_verifier( $user_id );
|
||||
} catch ( RuntimeException $exception ) {
|
||||
throw new Unauthorized_Exception( 'Unauthorized' );
|
||||
}
|
||||
|
||||
if ( $code_challenge !== \hash( 'sha256', $code_verifier ) ) {
|
||||
throw new Unauthorized_Exception( 'Unauthorized' );
|
||||
}
|
||||
$this->user_helper->update_meta( $user_id, '_yoast_wpseo_ai_generator_access_jwt', $access_jwt );
|
||||
$this->user_helper->update_meta( $user_id, '_yoast_wpseo_ai_generator_refresh_jwt', $refresh_jwt );
|
||||
$this->ai_generator_helper->delete_code_verifier( $user_id );
|
||||
|
||||
return $code_verifier;
|
||||
}
|
||||
|
||||
// phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber -- PHPCS doesn't take into account exceptions thrown in called methods.
|
||||
|
||||
/**
|
||||
* Action used to generate suggestions through AI.
|
||||
*
|
||||
* @param \WP_User $user The WP user.
|
||||
* @param string $suggestion_type The type of the requested suggestion.
|
||||
* @param string $prompt_content The excerpt taken from the post.
|
||||
* @param string $focus_keyphrase The focus keyphrase associated to the post.
|
||||
* @param string $language The language of the post.
|
||||
* @param string $platform The platform the post is intended for.
|
||||
* @param bool $retry_on_unauthorized Whether to retry when unauthorized (mechanism to retry once).
|
||||
*
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Bad_Request_Exception Bad_Request_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Forbidden_Exception Forbidden_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Internal_Server_Error_Exception Internal_Server_Error_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Not_Found_Exception Not_Found_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Payment_Required_Exception Payment_Required_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Request_Timeout_Exception Request_Timeout_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Service_Unavailable_Exception Service_Unavailable_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Too_Many_Requests_Exception Too_Many_Requests_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Unauthorized_Exception Unauthorized_Exception.
|
||||
* @throws \RuntimeException Unable to retrieve the access token.
|
||||
*
|
||||
* @return array The suggestions.
|
||||
*/
|
||||
public function get_suggestions(
|
||||
WP_User $user,
|
||||
string $suggestion_type,
|
||||
string $prompt_content,
|
||||
string $focus_keyphrase,
|
||||
string $language,
|
||||
string $platform,
|
||||
bool $retry_on_unauthorized = true
|
||||
): array {
|
||||
$token = $this->get_or_request_access_token( $user );
|
||||
|
||||
$request_body = [
|
||||
'service' => 'openai',
|
||||
'user_id' => (string) $user->ID,
|
||||
'subject' => [
|
||||
'content' => $prompt_content,
|
||||
'focus_keyphrase' => $focus_keyphrase,
|
||||
'language' => $language,
|
||||
'platform' => $platform,
|
||||
],
|
||||
];
|
||||
$request_headers = [
|
||||
'Authorization' => "Bearer $token",
|
||||
];
|
||||
|
||||
try {
|
||||
$response = $this->ai_generator_helper->request( "/openai/suggestions/$suggestion_type", $request_body, $request_headers );
|
||||
} catch ( Unauthorized_Exception $exception ) {
|
||||
// Delete the stored JWT tokens, as they appear to be no longer valid.
|
||||
$this->user_helper->delete_meta( $user->ID, '_yoast_wpseo_ai_generator_access_jwt' );
|
||||
$this->user_helper->delete_meta( $user->ID, '_yoast_wpseo_ai_generator_refresh_jwt' );
|
||||
|
||||
if ( ! $retry_on_unauthorized ) {
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
// Try again once more by fetching a new set of tokens and trying the suggestions endpoint again.
|
||||
return $this->get_suggestions( $user, $suggestion_type, $prompt_content, $focus_keyphrase, $language, $platform, false );
|
||||
} catch ( Forbidden_Exception $exception ) {
|
||||
// Follow the API in the consent being revoked (Use case: user sent an e-mail to revoke?).
|
||||
throw $this->handle_consent_revoked( $user->ID, $exception->getCode() );
|
||||
}
|
||||
|
||||
return $this->ai_generator_helper->build_suggestions_array( $response );
|
||||
}
|
||||
|
||||
// phpcs:enable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
|
||||
|
||||
/**
|
||||
* Stores the consent given or revoked by the user.
|
||||
*
|
||||
* @param int $user_id The user ID.
|
||||
* @param bool $consent Whether the consent has been given.
|
||||
*
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Bad_Request_Exception Bad_Request_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Internal_Server_Error_Exception Internal_Server_Error_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Not_Found_Exception Not_Found_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Payment_Required_Exception Payment_Required_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Request_Timeout_Exception Request_Timeout_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Service_Unavailable_Exception Service_Unavailable_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Too_Many_Requests_Exception Too_Many_Requests_Exception.
|
||||
* @throws \RuntimeException Unable to retrieve the access token.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function consent( int $user_id, bool $consent ): void {
|
||||
if ( $consent ) {
|
||||
// Store the consent at user level.
|
||||
$this->user_helper->update_meta( $user_id, '_yoast_wpseo_ai_consent', true );
|
||||
}
|
||||
else {
|
||||
$this->token_invalidate( $user_id );
|
||||
|
||||
// Delete the consent at user level.
|
||||
$this->user_helper->delete_meta( $user_id, '_yoast_wpseo_ai_consent' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Busts the subscription cache.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function bust_subscription_cache(): void {
|
||||
$this->addon_manager->remove_site_information_transients();
|
||||
}
|
||||
|
||||
// phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber -- PHPCS doesn't take into account exceptions thrown in called methods.
|
||||
|
||||
/**
|
||||
* Retrieves the access token.
|
||||
*
|
||||
* @param WP_User $user The WP user.
|
||||
*
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Bad_Request_Exception Bad_Request_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Forbidden_Exception Forbidden_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Internal_Server_Error_Exception Internal_Server_Error_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Not_Found_Exception Not_Found_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Payment_Required_Exception Payment_Required_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Request_Timeout_Exception Request_Timeout_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Service_Unavailable_Exception Service_Unavailable_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Too_Many_Requests_Exception Too_Many_Requests_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Unauthorized_Exception Unauthorized_Exception.
|
||||
* @throws \RuntimeException Unable to retrieve the access or refresh token.
|
||||
*
|
||||
* @return string The access token.
|
||||
*/
|
||||
private function get_or_request_access_token( WP_User $user ): string {
|
||||
$access_jwt = $this->user_helper->get_meta( $user->ID, '_yoast_wpseo_ai_generator_access_jwt', true );
|
||||
if ( ! \is_string( $access_jwt ) || $access_jwt === '' ) {
|
||||
$this->token_request( $user );
|
||||
$access_jwt = $this->ai_generator_helper->get_access_token( $user->ID );
|
||||
}
|
||||
elseif ( $this->ai_generator_helper->has_token_expired( $access_jwt ) ) {
|
||||
try {
|
||||
$this->token_refresh( $user );
|
||||
} catch ( Unauthorized_Exception $exception ) {
|
||||
$this->token_request( $user );
|
||||
} catch ( Forbidden_Exception $exception ) {
|
||||
// Follow the API in the consent being revoked (Use case: user sent an e-mail to revoke?).
|
||||
throw $this->handle_consent_revoked( $user->ID, $exception->getCode() );
|
||||
}
|
||||
$access_jwt = $this->ai_generator_helper->get_access_token( $user->ID );
|
||||
}
|
||||
|
||||
return $access_jwt;
|
||||
}
|
||||
|
||||
// phpcs:enable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
|
||||
|
||||
/**
|
||||
* Invalidates the access token.
|
||||
*
|
||||
* @param string $user_id The user ID.
|
||||
*
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Bad_Request_Exception Bad_Request_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Internal_Server_Error_Exception Internal_Server_Error_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Not_Found_Exception Not_Found_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Payment_Required_Exception Payment_Required_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Request_Timeout_Exception Request_Timeout_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Service_Unavailable_Exception Service_Unavailable_Exception.
|
||||
* @throws \Yoast\WP\SEO\Premium\Exceptions\Remote_Request\Too_Many_Requests_Exception Too_Many_Requests_Exception.
|
||||
* @throws \RuntimeException Unable to retrieve the access token.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function token_invalidate( string $user_id ): void {
|
||||
try {
|
||||
$access_jwt = $this->ai_generator_helper->get_access_token( $user_id );
|
||||
} catch ( RuntimeException $e ) {
|
||||
$access_jwt = '';
|
||||
}
|
||||
|
||||
$request_body = [
|
||||
'user_id' => (string) $user_id,
|
||||
];
|
||||
$request_headers = [
|
||||
'Authorization' => "Bearer $access_jwt",
|
||||
];
|
||||
|
||||
try {
|
||||
$this->ai_generator_helper->request( '/token/invalidate', $request_body, $request_headers );
|
||||
} catch ( Unauthorized_Exception | Forbidden_Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch -- Reason: Ignored on purpose.
|
||||
// We do nothing in this case, we trust nonce verification and try to remove the user data anyway.
|
||||
// I.e. we fallthrough to the same logic as if we got a 200 OK.
|
||||
}
|
||||
|
||||
// Delete the stored JWT tokens.
|
||||
$this->user_helper->delete_meta( $user_id, '_yoast_wpseo_ai_generator_access_jwt' );
|
||||
$this->user_helper->delete_meta( $user_id, '_yoast_wpseo_ai_generator_refresh_jwt' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles consent revoked.
|
||||
*
|
||||
* By deleting the consent user metadata from the database.
|
||||
* And then throwing a Forbidden_Exception.
|
||||
*
|
||||
* @param int $user_id The user ID.
|
||||
* @param int $status_code The status code. Defaults to 403.
|
||||
*
|
||||
* @return Forbidden_Exception The Forbidden_Exception.
|
||||
*/
|
||||
private function handle_consent_revoked( int $user_id, int $status_code = 403 ): Forbidden_Exception {
|
||||
$this->user_helper->delete_meta( $user_id, '_yoast_wpseo_ai_consent' );
|
||||
|
||||
return new Forbidden_Exception( 'CONSENT_REVOKED', $status_code );
|
||||
}
|
||||
}
|
||||
@@ -4,16 +4,23 @@ namespace Yoast\WP\SEO\Premium\Actions;
|
||||
|
||||
use WP_Query;
|
||||
use WPSEO_Premium_Prominent_Words_Support;
|
||||
use Yoast\WP\SEO\Helpers\Prominent_Words_Helper;
|
||||
use Yoast\WP\SEO\Models\Indexable;
|
||||
use Yoast\WP\SEO\Premium\Helpers\Prominent_Words_Helper;
|
||||
use Yoast\WP\SEO\Premium\Repositories\Prominent_Words_Repository;
|
||||
use Yoast\WP\SEO\Repositories\Indexable_Repository;
|
||||
use Yoast\WP\SEO\Repositories\Prominent_Words_Repository;
|
||||
use Yoast\WP\SEO\Repositories\SEO_Links_Repository;
|
||||
|
||||
/**
|
||||
* Handles the actual requests to the prominent words endpoints.
|
||||
*/
|
||||
class Link_Suggestions_Action {
|
||||
|
||||
/**
|
||||
* The amount of indexables to retrieve in one go
|
||||
* when generating internal linking suggestions.
|
||||
*/
|
||||
const BATCH_SIZE = 1000;
|
||||
|
||||
/**
|
||||
* The repository to retrieve prominent words from.
|
||||
*
|
||||
@@ -28,6 +35,13 @@ class Link_Suggestions_Action {
|
||||
*/
|
||||
protected $indexable_repository;
|
||||
|
||||
/**
|
||||
* The repository to retrieve links from.
|
||||
*
|
||||
* @var SEO_Links_Repository
|
||||
*/
|
||||
protected $links_repository;
|
||||
|
||||
/**
|
||||
* Contains helper functions for calculating with and comparing prominent words.
|
||||
*
|
||||
@@ -49,30 +63,36 @@ class Link_Suggestions_Action {
|
||||
* @param Indexable_Repository $indexable_repository The repository to retrieve indexables from.
|
||||
* @param Prominent_Words_Helper $prominent_words_helper Class with helper methods for prominent words.
|
||||
* @param WPSEO_Premium_Prominent_Words_Support $prominent_words_support The prominent words support class.
|
||||
* @param SEO_Links_Repository $links_repository The repository to retrieve links from.
|
||||
*/
|
||||
public function __construct(
|
||||
Prominent_Words_Repository $prominent_words_repository,
|
||||
Indexable_Repository $indexable_repository,
|
||||
Prominent_Words_Helper $prominent_words_helper,
|
||||
WPSEO_Premium_Prominent_Words_Support $prominent_words_support
|
||||
WPSEO_Premium_Prominent_Words_Support $prominent_words_support,
|
||||
SEO_Links_Repository $links_repository
|
||||
) {
|
||||
$this->prominent_words_repository = $prominent_words_repository;
|
||||
$this->indexable_repository = $indexable_repository;
|
||||
$this->prominent_words_helper = $prominent_words_helper;
|
||||
$this->prominent_words_support = $prominent_words_support;
|
||||
$this->links_repository = $links_repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Suggests a list of links, based on the given array of prominent words.
|
||||
*
|
||||
* @param array $words_from_request The prominent words as an array mapping words to weights.
|
||||
* @param int $limit The maximum number of link suggestions to retrieve.
|
||||
* @param int $object_id The object id for the current indexable.
|
||||
* @param string $object_type The object type for the current indexable.
|
||||
* @param array $words_from_request The prominent words as an array mapping words to weights.
|
||||
* @param int $limit The maximum number of link suggestions to retrieve.
|
||||
* @param int $object_id The object id for the current indexable.
|
||||
* @param string $object_type The object type for the current indexable.
|
||||
* @param bool $include_existing_links Optional. Whether or not to include existing links, defaults to true.
|
||||
* @param array $post_type Optional. The list of post types where suggestions may come from.
|
||||
* @param bool $only_include_public Optional. Only include public indexables, defaults to false.
|
||||
*
|
||||
* @return array Links for the post that are suggested.
|
||||
*/
|
||||
public function get_suggestions( $words_from_request, $limit, $object_id, $object_type ) {
|
||||
public function get_suggestions( $words_from_request, $limit, $object_id, $object_type, $include_existing_links = true, $post_type = [], $only_include_public = false ) {
|
||||
$current_indexable_id = null;
|
||||
$current_indexable = $this->indexable_repository->find_by_id_and_type( $object_id, $object_type );
|
||||
if ( $current_indexable ) {
|
||||
@@ -81,9 +101,9 @@ class Link_Suggestions_Action {
|
||||
|
||||
/*
|
||||
* Gets best suggestions (returns a sorted array [$indexable_id => score]).
|
||||
* The indexables are processed in batches of 100 indexables each.
|
||||
* The indexables are processed in batches of 1000 indexables each.
|
||||
*/
|
||||
$suggestions_scores = $this->retrieve_suggested_indexable_ids( $words_from_request, $limit, 100, $current_indexable_id );
|
||||
$suggestions_scores = $this->retrieve_suggested_indexable_ids( $words_from_request, $limit, self::BATCH_SIZE, $current_indexable_id, $include_existing_links, $post_type, $only_include_public );
|
||||
|
||||
$indexable_ids = \array_keys( $suggestions_scores );
|
||||
|
||||
@@ -109,6 +129,41 @@ class Link_Suggestions_Action {
|
||||
return $this->create_suggestions( $suggestions_indexables, $suggestions_scores );
|
||||
}
|
||||
|
||||
/**
|
||||
* Suggests a list of links, based on the given array of prominent words.
|
||||
*
|
||||
* @param int $id The object id for the current indexable.
|
||||
* @param int $limit The maximum number of link suggestions to retrieve.
|
||||
* @param bool $include_existing_links Optional. Whether or not to include existing links, defaults to true.
|
||||
*
|
||||
* @return array Links for the post that are suggested.
|
||||
*/
|
||||
public function get_indexable_suggestions_for_indexable( $id, $limit, $include_existing_links = true ) {
|
||||
$weighted_words = [];
|
||||
$prominent_words = $this->prominent_words_repository->query()
|
||||
->where( 'indexable_id', $id )
|
||||
->find_array();
|
||||
foreach ( $prominent_words as $prominent_word ) {
|
||||
$weighted_words[ $prominent_word['stem'] ] = $prominent_word['weight'];
|
||||
}
|
||||
|
||||
/*
|
||||
* Gets best suggestions (returns a sorted array [$indexable_id => score]).
|
||||
* The indexables are processed in batches of 1000 indexables each.
|
||||
*/
|
||||
$suggestions_scores = $this->retrieve_suggested_indexable_ids( $weighted_words, $limit, self::BATCH_SIZE, $id, $include_existing_links );
|
||||
|
||||
$indexable_ids = \array_keys( $suggestions_scores );
|
||||
|
||||
// Return the empty list if no suggestions have been found.
|
||||
if ( empty( $indexable_ids ) ) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Retrieve indexables for suggestions.
|
||||
return $this->indexable_repository->query()->where_id_in( $indexable_ids )->find_array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the titles of the posts with the given IDs.
|
||||
*
|
||||
@@ -297,16 +352,20 @@ class Link_Suggestions_Action {
|
||||
* Request prominent words for indexables in the batch (including the iDF of all words) to calculate
|
||||
* their vector length later.
|
||||
*
|
||||
* @param array $stems The stems in the request.
|
||||
* @param int $batch_size How many indexables to request in one query.
|
||||
* @param int $page The start of the current batch (in pages).
|
||||
* @param array $stems The stems in the request.
|
||||
* @param int $batch_size How many indexables to request in one query.
|
||||
* @param int $page The start of the current batch (in pages).
|
||||
* @param int[] $excluded_ids The indexable IDs to exclude.
|
||||
* @param array $post_type The post types that will be searched.
|
||||
* @param bool $only_include_public If only public indexables are included.
|
||||
*
|
||||
* @return array An array of ProminentWords objects, containing their stem, weight, indexable id,
|
||||
* and document frequency.
|
||||
*/
|
||||
protected function get_candidate_words( $stems, $batch_size, $page ) {
|
||||
protected function get_candidate_words( $stems, $batch_size, $page, $excluded_ids = [], $post_type = [], $only_include_public = false ) {
|
||||
|
||||
return $this->prominent_words_repository->find_by_list_of_ids(
|
||||
$this->prominent_words_repository->find_ids_by_stems( $stems, $batch_size, $page )
|
||||
$this->prominent_words_repository->find_ids_by_stems( $stems, $batch_size, $page, $excluded_ids, $post_type, $only_include_public )
|
||||
);
|
||||
}
|
||||
|
||||
@@ -315,27 +374,42 @@ class Link_Suggestions_Action {
|
||||
* The candidate indexables are analyzed in batches.
|
||||
* After having computed scores for a batch the function saves the best candidates until now.
|
||||
*
|
||||
* @param array $request_words The words to match, as an array mapping words to weights.
|
||||
* @param int $limit The max number of suggestions that should be returned by the function.
|
||||
* @param int $batch_size The number of indexables that should be analyzed in every batch.
|
||||
* @param int|null $current_indexable_id The id for the current indexable.
|
||||
* @param array $request_words The words to match, as an array mapping words to weights.
|
||||
* @param int $limit The max number of suggestions that should be returned by the function.
|
||||
* @param int $batch_size The number of indexables that should be analyzed in every batch.
|
||||
* @param int|null $current_indexable_id The id for the current indexable.
|
||||
* @param bool $include_existing_links Optional. Whether or not to include existing links, defaults to true.
|
||||
* @param array $post_type Optional. The list of post types where suggestions may come from.
|
||||
* @param bool $only_include_public Optional. Only include public indexables, defaults to false.
|
||||
*
|
||||
* @return array An array mapping indexable IDs to scores. Higher scores mean better matches.
|
||||
*/
|
||||
protected function retrieve_suggested_indexable_ids( $request_words, $limit, $batch_size, $current_indexable_id ) {
|
||||
protected function retrieve_suggested_indexable_ids( $request_words, $limit, $batch_size, $current_indexable_id, $include_existing_links = true, $post_type = [], $only_include_public = false ) {
|
||||
// Combine stems, weights and DFs from request.
|
||||
$request_data = $this->compose_request_data( $request_words );
|
||||
|
||||
// Calculate vector length of the request set (needed for score normalization later).
|
||||
$request_vector_length = $this->prominent_words_helper->compute_vector_length( $request_data );
|
||||
|
||||
// Get all links the post already links to, those shouldn't be suggested.
|
||||
$excluded_indexable_ids = [ $current_indexable_id ];
|
||||
if ( ! $include_existing_links && $current_indexable_id ) {
|
||||
$links = $this->links_repository->query()
|
||||
->distinct()
|
||||
->select( 'indexable_id' )
|
||||
->where( 'target_indexable_id', $current_indexable_id )
|
||||
->find_many();
|
||||
$excluded_indexable_ids = \array_merge( $excluded_indexable_ids, \wp_list_pluck( $links, 'indexable_id' ) );
|
||||
}
|
||||
$excluded_indexable_ids = \array_filter( $excluded_indexable_ids );
|
||||
|
||||
$request_stems = \array_keys( $request_data );
|
||||
$scores = [];
|
||||
$page = 1;
|
||||
|
||||
do {
|
||||
// Retrieve the words of all indexables in this batch that share prominent word stems with request.
|
||||
$candidates_words = $this->get_candidate_words( $request_stems, $batch_size, $page );
|
||||
$candidates_words = $this->get_candidate_words( $request_stems, $batch_size, $page, $excluded_indexable_ids, $post_type, $only_include_public );
|
||||
|
||||
// Transform the prominent words table so that it is indexed by indexable_ids.
|
||||
$candidates_words_by_indexable_ids = $this->group_words_by_indexable_id( $candidates_words );
|
||||
@@ -347,10 +421,6 @@ class Link_Suggestions_Action {
|
||||
++$batch_scores_size;
|
||||
}
|
||||
|
||||
if ( $current_indexable_id && isset( $scores[ $current_indexable_id ] ) ) {
|
||||
unset( $scores[ $current_indexable_id ] );
|
||||
}
|
||||
|
||||
// Sort the list of scores and keep only the top $limit of the scores.
|
||||
$scores = $this->get_top_suggestions( $scores, $limit );
|
||||
|
||||
@@ -393,10 +463,11 @@ class Link_Suggestions_Action {
|
||||
// Sort the indexables by descending score.
|
||||
\uasort(
|
||||
$scores,
|
||||
static function( $score_1, $score_2 ) {
|
||||
static function ( $score_1, $score_2 ) {
|
||||
if ( $score_1 === $score_2 ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ( ( $score_1 < $score_2 ) ? 1 : -1 );
|
||||
}
|
||||
);
|
||||
@@ -506,7 +577,7 @@ class Link_Suggestions_Action {
|
||||
protected function sort_suggestions_by_field( array &$link_suggestions, $field ) {
|
||||
\usort(
|
||||
$link_suggestions,
|
||||
static function( $suggestion_1, $suggestion_2 ) use ( $field ) {
|
||||
static function ( $suggestion_1, $suggestion_2 ) use ( $field ) {
|
||||
if ( $suggestion_1[ $field ] === $suggestion_2[ $field ] ) {
|
||||
return 0;
|
||||
}
|
||||
@@ -527,7 +598,7 @@ class Link_Suggestions_Action {
|
||||
protected function filter_suggestions( $link_suggestions, $cornerstone ) {
|
||||
return \array_filter(
|
||||
$link_suggestions,
|
||||
static function( $suggestion ) use ( $cornerstone ) {
|
||||
static function ( $suggestion ) use ( $cornerstone ) {
|
||||
return (bool) $suggestion['isCornerstone'] === $cornerstone;
|
||||
}
|
||||
);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Yoast\WP\SEO\Premium\Actions\Prominent_Words;
|
||||
|
||||
use Yoast\WP\SEO\Helpers\Prominent_Words_Helper;
|
||||
use Yoast\WP\SEO\Premium\Helpers\Prominent_Words_Helper;
|
||||
|
||||
/**
|
||||
* Action for completing the prominent words indexing.
|
||||
|
||||
@@ -127,16 +127,14 @@ class Content_Action implements Indexation_Action_Interface {
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a batch of indexables, to be indexed for internal linking suggestions.
|
||||
* The total number of indexables without prominent words.
|
||||
*
|
||||
* @deprecated 15.1
|
||||
* @codeCoverageIgnore
|
||||
* @param int $limit Limit the number of unindexed objects that are counted.
|
||||
*
|
||||
* @return array The indexables data to use for generating prominent words.
|
||||
* @return int|false The total number of indexables without prominent words. False if the query fails.
|
||||
*/
|
||||
public function get() {
|
||||
\_deprecated_function( __METHOD__, '15.1', 'Content_Action::index' );
|
||||
return $this->index();
|
||||
public function get_limited_unindexed_count( $limit ) {
|
||||
return $this->get_total_unindexed();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -155,7 +153,9 @@ class Content_Action implements Indexation_Action_Interface {
|
||||
->limit( $this->get_limit() )
|
||||
->find_many();
|
||||
|
||||
\delete_transient( static::TRANSIENT_CACHE_KEY );
|
||||
if ( \count( $indexables ) > 0 ) {
|
||||
\delete_transient( static::TRANSIENT_CACHE_KEY );
|
||||
}
|
||||
|
||||
// If no indexables have been left unindexed, return the empty array.
|
||||
if ( ! $indexables ) {
|
||||
|
||||
@@ -4,15 +4,16 @@ namespace Yoast\WP\SEO\Premium\Actions\Prominent_Words;
|
||||
|
||||
use Exception;
|
||||
use WPSEO_Premium_Prominent_Words_Versioning;
|
||||
use Yoast\WP\SEO\Helpers\Prominent_Words_Helper;
|
||||
use Yoast\WP\SEO\Models\Prominent_Words;
|
||||
use Yoast\WP\SEO\Premium\Helpers\Prominent_Words_Helper;
|
||||
use Yoast\WP\SEO\Premium\Repositories\Prominent_Words_Repository;
|
||||
use Yoast\WP\SEO\Repositories\Indexable_Repository;
|
||||
use Yoast\WP\SEO\Repositories\Prominent_Words_Repository;
|
||||
|
||||
/**
|
||||
* Action for linking a list of prominent words to an indexable.
|
||||
* Action for updating the prominent words in the prominent words table,
|
||||
* and linking them to an indexable.
|
||||
*
|
||||
* @see \Yoast\WP\SEO\Routes\Prominent_Words_Route;
|
||||
* @see \Yoast\WP\SEO\Premium\Routes\Prominent_Words_Route;
|
||||
*/
|
||||
class Save_Action {
|
||||
|
||||
@@ -58,11 +59,11 @@ class Save_Action {
|
||||
}
|
||||
|
||||
/**
|
||||
* Links a list of prominent words to an indexable.
|
||||
* Passes to-be-linked prominent words to the link function, together with the object type and object id of the
|
||||
* indexable to which they will need to be linked.
|
||||
*
|
||||
* Deletes the prominent words that have been stored previously, but are not in the new list of prominent words.
|
||||
*
|
||||
* @param array $data The data to process.
|
||||
* @param array $data The data to process. This is an array consisting of associative arrays (1 per indexable) with the keys
|
||||
* 'object_id', 'object_type' and 'prominent_words' (an array with 'stem' => 'weight' mappings).
|
||||
*/
|
||||
public function save( $data ) {
|
||||
if ( $data ) {
|
||||
@@ -75,7 +76,7 @@ class Save_Action {
|
||||
}
|
||||
|
||||
/**
|
||||
* Links a list of prominent words to an indexable.
|
||||
* Inserts, updates and removes prominent words that are now, or are no longer, associated with an indexable.
|
||||
*
|
||||
* @param string $object_type The object type of the indexable (e.g. `post` or `term`).
|
||||
* @param int $object_id The object id of the indexable.
|
||||
@@ -84,41 +85,87 @@ class Save_Action {
|
||||
public function link( $object_type, $object_id, $words ) {
|
||||
$indexable = $this->indexable_repository->find_by_id_and_type( $object_id, $object_type );
|
||||
|
||||
$indexable->prominent_words_version = WPSEO_Premium_Prominent_Words_Versioning::get_version_number();
|
||||
if ( $indexable ) {
|
||||
// Set the prominent words version number on the indexable.
|
||||
$indexable->prominent_words_version = WPSEO_Premium_Prominent_Words_Versioning::get_version_number();
|
||||
|
||||
/*
|
||||
* It is correct to save here, because find_by_id_and_type will auto create an indexable object
|
||||
* with the correct data. So we are not saving an incomplete indexable.
|
||||
*/
|
||||
$indexable->save();
|
||||
/*
|
||||
* It is correct to save here, because if the indexable didn't exist yet,
|
||||
* find_by_id_and_type (in the above 'save' function) will have auto-created an indexable object
|
||||
* with the correct data. So we are not saving an incomplete indexable.
|
||||
*/
|
||||
$indexable->save();
|
||||
|
||||
$old_words = $this->prominent_words_repository->find_by_indexable_id( $indexable->id );
|
||||
// Find the prominent words that were already associated with this indexable.
|
||||
$old_words = $this->prominent_words_repository->find_by_indexable_id( $indexable->id );
|
||||
|
||||
// Handle these words.
|
||||
$words = $this->handle_old_words( $indexable->id, $old_words, $words );
|
||||
|
||||
// Create database entries for all new words that are not yet in the database.
|
||||
$this->create_words( $indexable->id, $words );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes outdated prominent words from the database, and otherwise considers
|
||||
* whether the old words need to have their weights updated.
|
||||
*
|
||||
* @param int $indexable_id The id of the indexable which needs to have its
|
||||
* old words updated.
|
||||
* @param Prominent_Words[] $old_words An array with prominent words that were already
|
||||
* present in the database for a given indexable.
|
||||
* @param array $words The new prominent words for a given indexable.
|
||||
*
|
||||
* @return array The words that need to be created.
|
||||
*/
|
||||
protected function handle_old_words( $indexable_id, $old_words, $words ) {
|
||||
// Return early if the indexable didn't already have any prominent words associated with it.
|
||||
if ( empty( $old_words ) ) {
|
||||
return $words;
|
||||
}
|
||||
|
||||
$outdated_stems = [];
|
||||
|
||||
foreach ( $old_words as $old_word ) {
|
||||
// Remove when old word isn't found.
|
||||
// If an old prominent word is no longer associated with an indexable,
|
||||
// add it to the array with outdated stems, so that at a later step
|
||||
// it can be deleted from the database.
|
||||
if ( ! \array_key_exists( $old_word->stem, $words ) ) {
|
||||
$old_word->delete();
|
||||
$outdated_stems[] = $old_word->stem;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// If the old word should still be associated with the indexable,
|
||||
// update its weight if that has changed.
|
||||
$this->update_weight_if_changed( $old_word, $words[ $old_word->stem ] );
|
||||
|
||||
// Remove the key from the array with the new prominent words.
|
||||
unset( $words[ $old_word->stem ] );
|
||||
}
|
||||
|
||||
// Create all new words that are not yet in the database.
|
||||
$this->create_words( $indexable->id, $words );
|
||||
// Delete all the outdated prominent words in one query.
|
||||
try {
|
||||
$this->prominent_words_repository->delete_by_indexable_id_and_stems( $indexable_id, $outdated_stems );
|
||||
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch -- There is nothing to do.
|
||||
} catch ( Exception $exception ) {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
return $words;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the weight of the given prominent word.
|
||||
* (Does not update when the weights are the same).
|
||||
* Updates the weight of the given prominent word, if the weight has changed significantly.
|
||||
*
|
||||
* @param Prominent_Words $word The prominent word of which to update the weight.
|
||||
* @param float $new_weight The new weight.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function update_weight_if_changed( $word, $new_weight ) {
|
||||
if ( $word->weight !== $new_weight ) {
|
||||
if ( \abs( $word->weight - $new_weight ) > 0.1 ) {
|
||||
$word->weight = $new_weight;
|
||||
$word->save();
|
||||
}
|
||||
@@ -128,24 +175,34 @@ class Save_Action {
|
||||
* Creates the given words in the database and links them to the indexable with the given id.
|
||||
*
|
||||
* @param int $indexable_id The ID of the indexable.
|
||||
* @param array $words The words to create, as a `'stem'` => weight` map.
|
||||
* @param array $words The prominent words to create, as a `'stem'` => weight` map.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function create_words( $indexable_id, $words ) {
|
||||
// Return early if there are no new words to add to the database.
|
||||
if ( empty( $words ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$new_models = [];
|
||||
|
||||
foreach ( $words as $stem => $weight ) {
|
||||
$new_word = $this->prominent_words_repository->query()->create(
|
||||
$new_model = $this->prominent_words_repository->query()->create(
|
||||
[
|
||||
'indexable_id' => $indexable_id,
|
||||
'stem' => $stem,
|
||||
'weight' => $weight,
|
||||
]
|
||||
);
|
||||
$new_models[] = $new_model;
|
||||
}
|
||||
|
||||
try {
|
||||
$new_word->save();
|
||||
try {
|
||||
$this->prominent_words_repository->query()->insert_many( $new_models );
|
||||
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch -- There is nothing to do.
|
||||
} catch ( Exception $exception ) {
|
||||
// Do nothing.
|
||||
}
|
||||
} catch ( Exception $exception ) {
|
||||
// Do nothing.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user