PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / llms-txt / application / file / commands / populate-file-command-handler.php

populate-file-command-handler.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at src/llms-txt/application/file/commands/populate-file-command-handler.php

112 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // phpcs:disable Yoast.NamingConventions.NamespaceName.MaxExceeded
4 // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong
5 namespace Yoast\WP\SEO\Llms_Txt\Application\File\Commands;
6
7 use Yoast\WP\SEO\Helpers\Options_Helper;
8 use Yoast\WP\SEO\Llms_Txt\Application\Markdown_Builders\Markdown_Builder;
9 use Yoast\WP\SEO\Llms_Txt\Infrastructure\File\WordPress_File_System_Adapter;
10 use Yoast\WP\SEO\Llms_Txt\Infrastructure\File\WordPress_Llms_Txt_Permission_Gate;
11
12 /**
13 * Handles the population of the llms.txt.
14 */
15 class Populate_File_Command_Handler {
16
17 public const CONTENT_HASH_OPTION = 'wpseo_llms_txt_content_hash';
18 public const GENERATION_FAILURE_OPTION = 'wpseo_llms_txt_file_failure';
19
20 /**
21 * The permission gate.
22 *
23 * @var WordPress_Llms_Txt_Permission_Gate $permission_gate
24 */
25 private $permission_gate;
26
27 /**
28 * The options helper.
29 *
30 * @var Options_Helper
31 */
32 private $options_helper;
33
34 /**
35 * The file system adapter.
36 *
37 * @var WordPress_File_System_Adapter
38 */
39 private $file_system_adapter;
40
41 /**
42 * The markdown builder.
43 *
44 * @var Markdown_Builder
45 */
46 private $markdown_builder;
47
48 /**
49 * Constructor.
50 *
51 * @param Options_Helper $options_helper The options helper.
52 * @param WordPress_File_System_Adapter $file_system_adapter The file system adapter.
53 * @param Markdown_Builder $markdown_builder The markdown builder.
54 * @param WordPress_Llms_Txt_Permission_Gate $permission_gate The editing permission checker.
55 */
56 public function __construct(
57 Options_Helper $options_helper,
58 WordPress_File_System_Adapter $file_system_adapter,
59 Markdown_Builder $markdown_builder,
60 WordPress_Llms_Txt_Permission_Gate $permission_gate
61 ) {
62 $this->options_helper = $options_helper;
63 $this->file_system_adapter = $file_system_adapter;
64 $this->markdown_builder = $markdown_builder;
65 $this->permission_gate = $permission_gate;
66 }
67
68 /**
69 * Runs the command.
70 *
71 * @return void
72 */
73 public function handle() {
74 if ( $this->permission_gate->is_managed_by_yoast_seo() ) {
75 $content = $this->markdown_builder->render();
76 $content = $this->encode_content( $content );
77 $file_written = $this->file_system_adapter->set_file_content( $content );
78
79 if ( $file_written ) {
80 // Maybe move this to a class if we need to handle this option more often.
81 \update_option( self::CONTENT_HASH_OPTION, \md5( $content ) );
82 \delete_option( self::GENERATION_FAILURE_OPTION );
83 return;
84 }
85
86 \update_option( self::GENERATION_FAILURE_OPTION, 'filesystem_permissions' );
87 return;
88 }
89
90 \update_option( self::GENERATION_FAILURE_OPTION, 'not_managed_by_yoast_seo' );
91 }
92
93 /**
94 * Encodes the content by prepending it with the Byte Order Mark (BOM) for UTF-8.
95 *
96 * @param string $content The content to encode.
97 *
98 * @return string
99 */
100 private function encode_content( string $content ): string {
101
102 /**
103 * Filter: 'wpseo_llmstxt_encoding_prefix' - Allows editing the Byte Order Mark (BOM) for UTF-8 we prepend to the llmst.txt file.
104 *
105 * @param string $encoding_prefix The Byte Order Mark (BOM) for UTF-8 we prepend to the llmst.txt file.
106 */
107 $encoding_prefix = \apply_filters( 'wpseo_llmstxt_encoding_prefix', "\xEF\xBB\xBF" );
108
109 return $encoding_prefix . $content;
110 }
111 }
112