Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/hostinge.../includes/Admin/Jobs
File: LlmsTxtInjectContentJob.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Hostinger\Admin\Jobs;
[2] Fix | Delete
[3] Fix | Delete
use Hostinger\Admin\PluginSettings;
[4] Fix | Delete
use Hostinger\LlmsTxtGenerator\LlmsTxtFileHelper;
[5] Fix | Delete
use Hostinger\LlmsTxtGenerator\LlmsTxtGenerator;
[6] Fix | Delete
use Hostinger\LlmsTxtGenerator\LlmsTxtParser;
[7] Fix | Delete
[8] Fix | Delete
class LlmsTxtInjectContentJob extends AbstractBatchedJob {
[9] Fix | Delete
[10] Fix | Delete
public const JOB_NAME = 'generate_llmstxt';
[11] Fix | Delete
[12] Fix | Delete
protected LlmsTxtParser $llms_txt_parser;
[13] Fix | Delete
protected LlmsTxtFileHelper $llms_txt_file_helper;
[14] Fix | Delete
protected PluginSettings $plugin_settings;
[15] Fix | Delete
[16] Fix | Delete
public function __construct( ActionScheduler $action_scheduler, LlmsTxtParser $llms_txt_parser, LlmsTxtFileHelper $llms_txt_file_helper, PluginSettings $plugin_settings ) {
[17] Fix | Delete
parent::__construct( $action_scheduler );
[18] Fix | Delete
$this->llms_txt_parser = $llms_txt_parser;
[19] Fix | Delete
$this->llms_txt_file_helper = $llms_txt_file_helper;
[20] Fix | Delete
$this->plugin_settings = $plugin_settings;
[21] Fix | Delete
}
[22] Fix | Delete
[23] Fix | Delete
protected function get_batch( int $batch_number, $args ): array {
[24] Fix | Delete
if ( ! isset( $args['post_type'] ) ) {
[25] Fix | Delete
return array();
[26] Fix | Delete
}
[27] Fix | Delete
[28] Fix | Delete
$offset = $this->get_query_offset( $batch_number );
[29] Fix | Delete
$limit = $this->get_batch_size();
[30] Fix | Delete
[31] Fix | Delete
return $this->llms_txt_parser->get_by_post_type( $args['post_type'], $limit, $offset );
[32] Fix | Delete
}
[33] Fix | Delete
[34] Fix | Delete
public function get_name(): string {
[35] Fix | Delete
return self::JOB_NAME;
[36] Fix | Delete
}
[37] Fix | Delete
[38] Fix | Delete
protected function process_items( array $args = array() ): void {
[39] Fix | Delete
if ( ! $this->is_llms_txt_enabled() ) {
[40] Fix | Delete
return;
[41] Fix | Delete
}
[42] Fix | Delete
[43] Fix | Delete
$items = $args['items'] ?? array();
[44] Fix | Delete
$job_args = $args['args'] ?? array();
[45] Fix | Delete
[46] Fix | Delete
if ( ! isset( $job_args['post_type'] ) || empty( $items ) ) {
[47] Fix | Delete
return;
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
$content = $this->llms_txt_parser->get_items( $items );
[51] Fix | Delete
$this->inject_content( $job_args['post_type'], $content );
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
public function schedule( array $args = array() ): void {
[55] Fix | Delete
// Initiate as 2, as the first batch will be created when the user toggles on the option.
[56] Fix | Delete
$this->schedule_create_batch_action( 2, $args );
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
public function can_schedule( $args = array() ): bool {
[60] Fix | Delete
return parent::can_schedule( $args ) && $this->is_llms_txt_enabled();
[61] Fix | Delete
}
[62] Fix | Delete
[63] Fix | Delete
public function is_llms_txt_enabled(): bool {
[64] Fix | Delete
$settings = $this->plugin_settings->get_plugin_settings();
[65] Fix | Delete
return $settings->get_enable_llms_txt();
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
public function inject_content( $post_type, $new_content ): void {
[69] Fix | Delete
$content = $this->llms_txt_file_helper->get_content();
[70] Fix | Delete
$section = LlmsTxtGenerator::HOSTINGER_LLMSTXT_SUPPORTED_POST_TYPES[ $post_type ];
[71] Fix | Delete
$header = "## $section\n\n";
[72] Fix | Delete
$header_position = strpos( $content, $header );
[73] Fix | Delete
$header_length = strlen( $header );
[74] Fix | Delete
if ( $header_position === false ) {
[75] Fix | Delete
return;
[76] Fix | Delete
}
[77] Fix | Delete
[78] Fix | Delete
$before_injection_slot = substr( $content, 0, $header_position + $header_length );
[79] Fix | Delete
$after_injection_slot = substr( $content, $header_position + $header_length );
[80] Fix | Delete
[81] Fix | Delete
$final_content = $before_injection_slot . $new_content . $after_injection_slot;
[82] Fix | Delete
$this->llms_txt_file_helper->create( $final_content );
[83] Fix | Delete
}
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function