| 1 |
<?php |
| 2 |
/** |
| 3 |
* Submit URLs to index ability. |
| 4 |
* |
| 5 |
* @package ThinkRank\Abilities\Indexing |
| 6 |
*/ |
| 7 |
|
| 8 |
declare(strict_types=1); |
| 9 |
|
| 10 |
namespace ThinkRank\Abilities\Indexing; |
| 11 |
|
| 12 |
use ThinkRank\Abilities\Ability_Base; |
| 13 |
use ThinkRank\SEO\Instant_Indexing_Manager; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Submits URLs to search engines via the IndexNow protocol. |
| 21 |
* |
| 22 |
* Requires instant indexing to be configured (an API key must exist); otherwise |
| 23 |
* the manager responds with "API Key missing". This calls the external IndexNow |
| 24 |
* API. |
| 25 |
*/ |
| 26 |
class Submit_Urls_To_Index extends Ability_Base { |
| 27 |
/** |
| 28 |
* Constructor. |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
$this->id = 'thinkrank/submit-urls-to-index'; |
| 32 |
$this->label = __( 'Submit URLs to Instant Indexing', 'thinkrank' ); |
| 33 |
$this->description = __( 'Submit one or more URLs to search engines via the IndexNow protocol. Requires instant indexing to be configured (an API key must exist) or it returns "API Key missing". Calls the external IndexNow API. get-instant-indexing-settings reports whether a key is configured, and get-instant-indexing-history lists past submissions.', 'thinkrank' ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* {@inheritDoc} |
| 38 |
* |
| 39 |
* @return array<string, bool|float|string> |
| 40 |
*/ |
| 41 |
public function get_annotations() { |
| 42 |
return [ |
| 43 |
'readonly' => false, |
| 44 |
'destructive' => true, |
| 45 |
'idempotent' => false, |
| 46 |
'priority' => 2.0, |
| 47 |
'openWorldHint' => true, |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* {@inheritDoc} |
| 53 |
* |
| 54 |
* @return array<string, mixed> |
| 55 |
*/ |
| 56 |
public function get_input_schema() { |
| 57 |
return [ |
| 58 |
'type' => 'object', |
| 59 |
'additionalProperties' => false, |
| 60 |
'properties' => [ |
| 61 |
'urls' => [ |
| 62 |
'type' => 'array', |
| 63 |
'description' => __( 'The URLs to submit for indexing (same-site host only).', 'thinkrank' ), |
| 64 |
'minItems' => 1, |
| 65 |
'maxItems' => \ThinkRank\SEO\Instant_Indexing_Manager::MAX_URLS_PER_SUBMISSION, |
| 66 |
'items' => [ |
| 67 |
'type' => 'string', |
| 68 |
'format' => 'uri', |
| 69 |
], |
| 70 |
], |
| 71 |
], |
| 72 |
'required' => [ 'urls' ], |
| 73 |
]; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* {@inheritDoc} |
| 78 |
* |
| 79 |
* @return array<string, mixed> |
| 80 |
*/ |
| 81 |
public function get_output_schema() { |
| 82 |
return [ |
| 83 |
'type' => 'object', |
| 84 |
'properties' => [ |
| 85 |
'success' => [ 'type' => 'boolean' ], |
| 86 |
'code' => [ 'type' => 'integer' ], |
| 87 |
'message' => [ 'type' => 'string' ], |
| 88 |
'submitted_count' => [ 'type' => 'integer' ], |
| 89 |
], |
| 90 |
]; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Execute ability. |
| 95 |
* |
| 96 |
* @param array<string, mixed> $input Ability input payload. |
| 97 |
* @return array<string, mixed>|\WP_Error |
| 98 |
*/ |
| 99 |
public function execute( $input ) { |
| 100 |
$urls = isset( $input['urls'] ) && is_array( $input['urls'] ) ? $input['urls'] : []; |
| 101 |
$urls = array_values( array_filter( array_map( 'esc_url_raw', $urls ) ) ); |
| 102 |
|
| 103 |
if ( empty( $urls ) ) { |
| 104 |
return new \WP_Error( |
| 105 |
'thinkrank_missing_index_urls', |
| 106 |
__( 'At least one valid URL is required.', 'thinkrank' ), |
| 107 |
[ 'status' => 400 ] |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
$manager = new Instant_Indexing_Manager(); |
| 112 |
$result = $manager->submit_urls( $urls ); |
| 113 |
|
| 114 |
return [ |
| 115 |
'success' => (bool) ( $result['success'] ?? false ), |
| 116 |
'code' => (int) ( $result['code'] ?? 0 ), |
| 117 |
'message' => (string) ( $result['message'] ?? '' ), |
| 118 |
// The count the manager actually submitted after same-host filtering |
| 119 |
// and its cap, not the raw input size. |
| 120 |
'submitted_count' => (int) ( $result['submitted_count'] ?? 0 ), |
| 121 |
]; |
| 122 |
} |
| 123 |
} |
| 124 |
|