Yoast\WP\SEO\Actions\Wincher

Wincher_Keyphrases_Action::track_keyphrases()publicYoast 1.0

Sends the tracking API request for one or more keyphrases.

Метод класса: Wincher_Keyphrases_Action{}

Хуков нет.

Возвращает

Object. The reponse object.

Использование

$Wincher_Keyphrases_Action = new Wincher_Keyphrases_Action();
$Wincher_Keyphrases_Action->track_keyphrases( $keyphrases, $limits );
$keyphrases(строка|массив) (обязательный)
One or more keyphrases that should be tracked.
$limits(Object) (обязательный)
The limits API call response data.

Код Wincher_Keyphrases_Action::track_keyphrases() Yoast 22.4

public function track_keyphrases( $keyphrases, $limits ) {
	try {
		$endpoint = \sprintf(
			self::KEYPHRASES_ADD_URL,
			$this->options_helper->get( 'wincher_website_id' )
		);

		// Enforce arrrays to ensure a consistent way of preparing the request.
		if ( ! \is_array( $keyphrases ) ) {
			$keyphrases = [ $keyphrases ];
		}

		// Calculate if the user would exceed their limit.
		// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- To ensure JS code style, this can be ignored.
		if ( ! $limits->canTrack || $this->would_exceed_limits( $keyphrases, $limits ) ) {
			$response = [
				'limit'  => $limits->limit,
				'error'  => 'Account limit exceeded',
				'status' => 400,
			];

			return $this->to_result_object( $response );
		}

		$formatted_keyphrases = \array_values(
			\array_map(
				static function ( $keyphrase ) {
					return [
						'keyword' => $keyphrase,
						'groups'  => [],
					];
				},
				$keyphrases
			)
		);

		$results = $this->client->post( $endpoint, WPSEO_Utils::format_json_encode( $formatted_keyphrases ) );

		if ( ! \array_key_exists( 'data', $results ) ) {
			return $this->to_result_object( $results );
		}

		// The endpoint returns a lot of stuff that we don't want/need.
		$results['data'] = \array_map(
			static function ( $keyphrase ) {
				return [
					'id'         => $keyphrase['id'],
					'keyword'    => $keyphrase['keyword'],
				];
			},
			$results['data']
		);

		$results['data'] = \array_combine(
			\array_column( $results['data'], 'keyword' ),
			\array_values( $results['data'] )
		);

		return $this->to_result_object( $results );
	} catch ( Exception $e ) {
		return (object) [
			'error'  => $e->getMessage(),
			'status' => $e->getCode(),
		];
	}
}