PluginProbe
Parse.ly / 3.16.2
Parse.ly v3.16.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 3.20.2 3.20.3 All 105 releases
wp-parsely / src / RemoteAPI / content-suggestions / class-suggest-linked-reference-api.php

class-suggest-linked-reference-api.php in Parse.ly 3.16.2, at src/RemoteAPI/content-suggestions/class-suggest-linked-reference-api.php

89 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Remote API: Content Suggestions Suggest Linked Reference (Smart Links) API
4 *
5 * @package Parsely
6 * @since 3.14.0
7 */
8
9 declare(strict_types=1);
10
11 namespace Parsely\RemoteAPI\ContentSuggestions;
12
13 use Parsely\Models\Smart_Link;
14 use Parsely\Parsely;
15 use WP_Error;
16
17 /**
18 * Class for Content Suggestions Suggest Linked Reference (Smart Links) API.
19 *
20 * @since 3.14.0
21 *
22 * @phpstan-import-type WP_HTTP_Request_Args from Parsely
23 */
24 class Suggest_Linked_Reference_API extends Content_Suggestions_Base_API {
25 protected const ENDPOINT = '/suggest-linked-reference';
26 protected const QUERY_FILTER = 'wp_parsely_suggest_linked_reference_endpoint_args';
27
28 /**
29 * Gets suggested smart links for the given content.
30 *
31 * @since 3.14.0
32 *
33 * @param string $content The content to generate links for.
34 * @param int $max_link_words The maximum number of words in links.
35 * @param int $max_links The maximum number of links to return.
36 * @param string[] $url_exclusion_list A list of URLs to exclude from the suggestions.
37 *
38 * @return Smart_Link[]|WP_Error The response from the remote API, or a WP_Error
39 * object if the response is an error.
40 */
41 public function get_links(
42 string $content,
43 int $max_link_words = 4,
44 int $max_links = 10,
45 array $url_exclusion_list = array()
46 ) {
47 $body = array(
48 'output_config' => array(
49 'max_link_words' => $max_link_words,
50 'max_items' => $max_links,
51 ),
52 'text' => wp_strip_all_tags( $content ),
53 );
54
55 if ( count( $url_exclusion_list ) > 0 ) {
56 $body['url_exclusion_list'] = $url_exclusion_list;
57 }
58
59 $decoded = $this->post_request( array(), $body );
60
61 if ( is_wp_error( $decoded ) ) {
62 return $decoded;
63 }
64
65 if ( ! property_exists( $decoded, 'result' ) ||
66 ! is_array( $decoded->result ) ) {
67 return new WP_Error(
68 400,
69 __( 'Unable to parse suggested links from upstream API', 'wp-parsely' )
70 );
71 }
72
73 // Convert the links to Smart_Link objects.
74 $links = array();
75 foreach ( $decoded->result as $link ) {
76 $link = apply_filters( 'wp_parsely_suggest_linked_reference_link', $link );
77 $link_obj = new Smart_Link(
78 esc_url( $link->canonical_url ),
79 esc_attr( $link->title ),
80 wp_kses_post( $link->text ),
81 $link->offset
82 );
83 $links[] = $link_obj;
84 }
85
86 return $links;
87 }
88 }
89