| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong |
| 4 |
namespace Yoast\WP\SEO\Llms_Txt\Infrastructure\File; |
| 5 |
|
| 6 |
use Yoast\WP\SEO\Helpers\Options_Helper; |
| 7 |
use Yoast\WP\SEO\Llms_Txt\Application\File\Commands\Populate_File_Command_Handler; |
| 8 |
use Yoast\WP\SEO\Llms_Txt\Domain\File\Llms_Txt_Permission_Gate_Interface; |
| 9 |
|
| 10 |
/** |
| 11 |
* Handles checks to see if we manage the llms.txt file. |
| 12 |
*/ |
| 13 |
class WordPress_Llms_Txt_Permission_Gate implements Llms_Txt_Permission_Gate_Interface { |
| 14 |
|
| 15 |
/** |
| 16 |
* The file system adapter. |
| 17 |
* |
| 18 |
* @var WordPress_File_System_Adapter |
| 19 |
*/ |
| 20 |
private $file_system_adapter; |
| 21 |
|
| 22 |
/** |
| 23 |
* The options helper. |
| 24 |
* |
| 25 |
* @var Options_Helper |
| 26 |
*/ |
| 27 |
private $options_helper; |
| 28 |
|
| 29 |
/** |
| 30 |
* Constructor. |
| 31 |
* |
| 32 |
* @param WordPress_File_System_Adapter $file_system_adapter The file system adapter. |
| 33 |
* @param Options_Helper $options_helper The options helper. |
| 34 |
*/ |
| 35 |
public function __construct( |
| 36 |
WordPress_File_System_Adapter $file_system_adapter, |
| 37 |
Options_Helper $options_helper |
| 38 |
) { |
| 39 |
$this->file_system_adapter = $file_system_adapter; |
| 40 |
$this->options_helper = $options_helper; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Checks if Yoast SEO manages the llms.txt. |
| 45 |
* |
| 46 |
* @return bool Checks if Yoast SEO manages the llms.txt. |
| 47 |
*/ |
| 48 |
public function is_managed_by_yoast_seo(): bool { |
| 49 |
$stored_hash = \get_option( Populate_File_Command_Handler::CONTENT_HASH_OPTION, '' ); |
| 50 |
|
| 51 |
// If the file does not exist yet, we always regenerate/create it. |
| 52 |
if ( ! $this->file_system_adapter->file_exists() ) { |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
// This means the file is already there (maybe hand made or another plugin created it). And since we don't have a hash it's not ours. |
| 57 |
if ( $stored_hash === '' ) { |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
$current_content = $this->file_system_adapter->get_file_contents(); |
| 62 |
|
| 63 |
// If you have a hash, we want to make sure it's the same. This check makes sure the file is not edited by the user. |
| 64 |
return \md5( $current_content ) === $stored_hash; |
| 65 |
} |
| 66 |
} |
| 67 |
|