| 1 |
<?php |
| 2 |
/** |
| 3 |
* Publish llms.txt ability. |
| 4 |
* |
| 5 |
* @package ThinkRank\Abilities\Analysis |
| 6 |
*/ |
| 7 |
|
| 8 |
declare(strict_types=1); |
| 9 |
|
| 10 |
namespace ThinkRank\Abilities\Analysis; |
| 11 |
|
| 12 |
use ThinkRank\Abilities\Ability_Base; |
| 13 |
use ThinkRank\SEO\LLMs_Txt_Manager; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Generates the llms.txt content and writes it to the site root. |
| 21 |
* |
| 22 |
* Unlike generate-llms-txt (which is preview-only and never touches disk), this |
| 23 |
* ability persists the generated content to <site root>/llms.txt so it is |
| 24 |
* served publicly. Pass an empty user_input object to publish using the |
| 25 |
* currently saved settings. |
| 26 |
*/ |
| 27 |
class Publish_Llms_Txt extends Ability_Base { |
| 28 |
/** |
| 29 |
* Constructor. |
| 30 |
*/ |
| 31 |
public function __construct() { |
| 32 |
$this->id = 'thinkrank/publish-llms-txt'; |
| 33 |
$this->label = __( 'Publish ThinkRank llms.txt', 'thinkrank' ); |
| 34 |
$this->description = __( 'Generate the llms.txt content and publish it so it is served publicly at /llms.txt. Pass an empty user_input object to publish using the currently saved settings. Depending on the delivery_mode setting this writes a physical file to the site root or stores the document for WordPress to serve; unlike generate-llms-txt it persists either way, and afterwards get-llms-txt-status reports published: true.', 'thinkrank' ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* {@inheritDoc} |
| 39 |
* |
| 40 |
* @return array<string, bool|float|string> |
| 41 |
*/ |
| 42 |
public function get_annotations() { |
| 43 |
return [ |
| 44 |
'readonly' => false, |
| 45 |
'destructive' => true, |
| 46 |
'idempotent' => true, |
| 47 |
'priority' => 2.0, |
| 48 |
'openWorldHint' => false, |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* {@inheritDoc} |
| 54 |
* |
| 55 |
* @return array<string, mixed> |
| 56 |
*/ |
| 57 |
public function get_input_schema() { |
| 58 |
return [ |
| 59 |
'type' => 'object', |
| 60 |
'additionalProperties' => false, |
| 61 |
'properties' => [ |
| 62 |
'user_input' => [ |
| 63 |
'type' => 'object', |
| 64 |
'description' => __( 'Optional override values. When empty, saved settings are used.', 'thinkrank' ), |
| 65 |
], |
| 66 |
], |
| 67 |
]; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* {@inheritDoc} |
| 72 |
* |
| 73 |
* @return array<string, mixed> |
| 74 |
*/ |
| 75 |
public function get_output_schema() { |
| 76 |
return [ |
| 77 |
'type' => 'object', |
| 78 |
'properties' => [ |
| 79 |
'success' => [ 'type' => 'boolean' ], |
| 80 |
'message' => [ 'type' => 'string' ], |
| 81 |
'content' => [ 'type' => 'string' ], |
| 82 |
'validation' => [ 'type' => [ 'array', 'object' ] ], |
| 83 |
'file_status' => [ 'type' => [ 'array', 'object' ] ], |
| 84 |
], |
| 85 |
]; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Execute ability. |
| 90 |
* |
| 91 |
* @param array<string, mixed> $input Ability input payload. |
| 92 |
* @return array<string, mixed>|\WP_Error |
| 93 |
*/ |
| 94 |
public function execute( $input ) { |
| 95 |
$user_input = isset( $input['user_input'] ) && is_array( $input['user_input'] ) ? $input['user_input'] : []; |
| 96 |
|
| 97 |
$manager = new LLMs_Txt_Manager(); |
| 98 |
|
| 99 |
$generated = $manager->generate_llms_txt( $user_input, [] ); |
| 100 |
$validation = isset( $generated['validation'] ) && is_array( $generated['validation'] ) ? $generated['validation'] : []; |
| 101 |
|
| 102 |
// Do not write an incomplete/invalid file to disk. |
| 103 |
if ( empty( $validation['valid'] ) ) { |
| 104 |
return [ |
| 105 |
'success' => false, |
| 106 |
'message' => __( 'llms.txt was not published because the settings are incomplete. Save the required fields (Website Description, Key Features, Target Audience) first.', 'thinkrank' ), |
| 107 |
'content' => '', |
| 108 |
'validation' => $validation, |
| 109 |
'file_status' => $manager->get_llms_txt_status( true ), |
| 110 |
]; |
| 111 |
} |
| 112 |
|
| 113 |
$content = isset( $generated['content'] ) ? (string) $generated['content'] : ''; |
| 114 |
$write = $manager->write_llms_txt_to_file( $content ); |
| 115 |
|
| 116 |
return [ |
| 117 |
'success' => ! empty( $write['success'] ), |
| 118 |
'message' => isset( $write['message'] ) ? (string) $write['message'] : '', |
| 119 |
'content' => $content, |
| 120 |
'validation' => $validation, |
| 121 |
'file_status' => $manager->get_llms_txt_status( true ), |
| 122 |
]; |
| 123 |
} |
| 124 |
} |
| 125 |
|