| 1 |
<?php |
| 2 |
/** |
| 3 |
* Get robots.txt ability. |
| 4 |
* |
| 5 |
* @package ThinkRank\Abilities\Settings |
| 6 |
*/ |
| 7 |
|
| 8 |
declare(strict_types=1); |
| 9 |
|
| 10 |
namespace ThinkRank\Abilities\Settings; |
| 11 |
|
| 12 |
use ThinkRank\Abilities\Ability_Base; |
| 13 |
use ThinkRank\SEO\Site_Identity_Manager; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Retrieves the custom robots.txt content managed by ThinkRank. |
| 21 |
*/ |
| 22 |
class Get_Robots_Txt extends Ability_Base { |
| 23 |
/** |
| 24 |
* Constructor. |
| 25 |
*/ |
| 26 |
public function __construct() { |
| 27 |
$this->id = 'thinkrank/get-robots-txt'; |
| 28 |
$this->label = __( 'Get ThinkRank Robots.txt', 'thinkrank' ); |
| 29 |
$this->description = __( 'Retrieve the ThinkRank robots.txt: both the stored custom override (may be empty) and the "rendered" content actually served to crawlers, which includes the auto-generated default rules when no override is set. Use update-robots-txt to replace it.', 'thinkrank' ); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* {@inheritDoc} |
| 34 |
* |
| 35 |
* @return array<string, bool|float|string> |
| 36 |
*/ |
| 37 |
public function get_annotations() { |
| 38 |
return [ |
| 39 |
'readonly' => true, |
| 40 |
'destructive' => false, |
| 41 |
'idempotent' => true, |
| 42 |
'priority' => 1.0, |
| 43 |
'openWorldHint' => false, |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* {@inheritDoc} |
| 49 |
* |
| 50 |
* @return array<string, mixed> |
| 51 |
*/ |
| 52 |
public function get_input_schema() { |
| 53 |
return [ |
| 54 |
'type' => 'object', |
| 55 |
'additionalProperties' => false, |
| 56 |
'properties' => self::empty_properties(), |
| 57 |
]; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* {@inheritDoc} |
| 62 |
* |
| 63 |
* @return array<string, mixed> |
| 64 |
*/ |
| 65 |
public function get_output_schema() { |
| 66 |
return [ |
| 67 |
'type' => 'object', |
| 68 |
'properties' => [ |
| 69 |
'content' => [ |
| 70 |
'type' => 'string', |
| 71 |
'description' => __( 'The stored custom override. Empty when ThinkRank is auto-generating robots.txt.', 'thinkrank' ), |
| 72 |
], |
| 73 |
'enabled' => [ 'type' => 'boolean' ], |
| 74 |
'rendered' => [ |
| 75 |
'type' => 'string', |
| 76 |
'description' => __( 'The effective robots.txt actually served to crawlers (custom override, or the generated defaults when none is set).', 'thinkrank' ), |
| 77 |
], |
| 78 |
'is_default' => [ |
| 79 |
'type' => 'boolean', |
| 80 |
'description' => __( 'True when the served content is ThinkRank\'s auto-generated defaults rather than a custom override.', 'thinkrank' ), |
| 81 |
], |
| 82 |
'source' => [ |
| 83 |
'type' => 'string', |
| 84 |
'description' => __( 'Origin of the served content: "file" (physical robots.txt in the web root), "custom" (stored override), "generated" (auto-generated defaults), or "wordpress" (management disabled).', 'thinkrank' ), |
| 85 |
], |
| 86 |
], |
| 87 |
]; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Execute ability. |
| 92 |
* |
| 93 |
* @param array<string, mixed> $input Ability input payload. |
| 94 |
* @return array<string, mixed> |
| 95 |
*/ |
| 96 |
public function execute( $input ) { |
| 97 |
$mgr = new Site_Identity_Manager(); |
| 98 |
$settings = $mgr->get_settings( 'site', null ); |
| 99 |
$effective = $mgr->get_effective_robots_txt(); |
| 100 |
|
| 101 |
return [ |
| 102 |
'content' => (string) ( $settings['robots_txt_content'] ?? '' ), |
| 103 |
'enabled' => (bool) ( $settings['robots_txt_enabled'] ?? true ), |
| 104 |
'rendered' => (string) $effective['content'], |
| 105 |
'is_default' => (bool) $effective['is_default'], |
| 106 |
'source' => (string) $effective['source'], |
| 107 |
]; |
| 108 |
} |
| 109 |
} |
| 110 |
|