PluginProbe
Parse.ly / 3.24.3
Parse.ly v3.24.3
3.24.3 3.24.2 3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 All 107 releases
wp-parsely / src / services / suggestions-api / endpoints / class-endpoint-suggest-inbound-links.php

class-endpoint-suggest-inbound-links.php in Parse.ly 3.24.3, at src/services/suggestions-api/endpoints/class-endpoint-suggest-inbound-links.php

152 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Parse.ly Suggestions API Endpoint: Suggest Inbound Links
4 *
5 * @package Parsely
6 * @since 3.19.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\Services\Suggestions_API\Endpoints;
12
13 use Parsely\Parsely;
14 use Parsely\Models\Inbound_Smart_Link;
15 use WP_Error;
16 /**
17 * The endpoint for the Suggest Inbound Links API request.
18 *
19 * @since 3.19.0
20 *
21 * @phpstan-type Endpoint_Suggest_Inbound_Links_Options = array{
22 * max_items?: int,
23 * url_exclusion_list?: array<string>,
24 * }
25 */
26 class Endpoint_Suggest_Inbound_Links extends Suggestions_API_Base_Endpoint {
27 /**
28 * Returns the endpoint for the API request.
29 *
30 * @since 3.19.0
31 *
32 * @return string The endpoint for the API request.
33 */
34 public function get_endpoint(): string {
35 return '/suggest-inbound-links';
36 }
37
38 /**
39 * Gets suggested inbound links for the given URL using the Parse.ly
40 * Content Suggestion API.
41 *
42 * @since 3.19.0
43 *
44 * @param \WP_Post $post The post to get inbound link suggestions for.
45 * @param Endpoint_Suggest_Inbound_Links_Options $options The options to pass to the API request.
46 * @return array<Inbound_Smart_Link>|WP_Error The response from the remote API, or a WP_Error
47 * object if the response is an error.
48 */
49 public function get_inbound_links(
50 \WP_Post $post,
51 $options = array()
52 ) {
53 /**
54 * The Parse.ly canonical URL for the post.
55 *
56 * @var string $post_url
57 */
58 $post_url = Parsely::get_canonical_url_from_post( $post );
59
60 $request_body = array(
61 'canonical_url' => $post_url,
62 'output_config' => array(
63 'max_items' => $options['max_items'] ?? 10,
64 ),
65 'title' => $post->post_title,
66 'text' => wp_strip_all_tags( $post->post_content ),
67 );
68
69 if ( isset( $options['url_exclusion_list'] ) && count( $options['url_exclusion_list'] ) > 0 ) {
70 $request_body['url_exclusion_list'] = $options['url_exclusion_list'];
71 }
72
73 $request_body = apply_filters( 'wp_parsely_suggest_inbound_links_request_body', $request_body, $post, $options );
74 $response = $this->request( 'POST', array(), $request_body );
75
76 if ( is_wp_error( $response ) ) {
77 return $response;
78 }
79
80 // Convert the links to Inbound_Smart_Link objects.
81 $links = array();
82 foreach ( $response as $link ) {
83 $link = apply_filters( 'wp_parsely_suggest_inbound_links_link', $link );
84
85 $anchor_text_suggestions = $link['anchor_texts'];
86
87 foreach ( $anchor_text_suggestions as $anchor_text_suggestion ) {
88 $link_obj = new Inbound_Smart_Link(
89 esc_url( $link['source_url'] ),
90 esc_attr( $link['title'] ),
91 wp_kses_post( $anchor_text_suggestion['text'] ),
92 $anchor_text_suggestion['offset']
93 );
94
95 // Set the destination to be the current post.
96 $link_obj->set_destination_post( $post );
97
98 // Set the source post from the URL.
99 $did_set_source = $link_obj->set_source_from_url( $link['source_url'] );
100
101 // If no source post was found or the source post is the same as
102 // the destination post, skip to the next link suggestion.
103 if ( ! $did_set_source || $link_obj->source_post_id === $post->ID ) {
104 break;
105 }
106
107 // If the link doesn't have a valid placement, skip to the next
108 // anchor text suggestion.
109 $valid_placement = $link_obj->has_valid_placement();
110 if ( is_wp_error( $valid_placement ) || false === $valid_placement ) {
111 continue;
112 }
113
114 // Update the UID of the smart link.
115 $link_obj->update_uid();
116
117 $links[] = $link_obj;
118
119 // Break after the first valid link.
120 break;
121 }
122 }
123
124 return $links;
125 }
126
127 /**
128 * Executes the API request.
129 *
130 * @since 3.19.0
131 *
132 * @param array<mixed> $args The arguments to pass to the API request.
133 * @return WP_Error|array<mixed> The response from the API.
134 */
135 public function call( array $args = array() ) {
136 /** @var \WP_Post|null $post */
137 $post = $args['post'] ?? null;
138 /** @var Endpoint_Suggest_Inbound_Links_Options $options */
139 $options = $args['options'] ?? array();
140
141 if ( ! ( $post instanceof \WP_Post ) ) {
142 return new \WP_Error(
143 'parsely_invalid_post',
144 __( 'Invalid post.', 'wp-parsely' ),
145 array( 'status' => 400 )
146 );
147 }
148
149 return $this->get_inbound_links( $post, $options );
150 }
151 }
152