| 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\Llms_Txt\Domain\File\Llms_File_System_Interface; |
| 7 |
|
| 8 |
/** |
| 9 |
* Adapter class for handling file system operations in a WordPress environment. |
| 10 |
*/ |
| 11 |
class WordPress_File_System_Adapter implements Llms_File_System_Interface { |
| 12 |
|
| 13 |
/** |
| 14 |
* Creates a file and writes the specified content to it. |
| 15 |
* |
| 16 |
* @param string $content The content to write into the file. |
| 17 |
* |
| 18 |
* @return bool True on success, false on failure. |
| 19 |
*/ |
| 20 |
public function set_file_content( string $content ): bool { |
| 21 |
if ( $this->is_file_system_available() ) { |
| 22 |
global $wp_filesystem; |
| 23 |
$result = $wp_filesystem->put_contents( |
| 24 |
$this->get_llms_file_path(), |
| 25 |
$content, |
| 26 |
\FS_CHMOD_FILE, |
| 27 |
); |
| 28 |
|
| 29 |
return $result; |
| 30 |
} |
| 31 |
|
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Removes the llms.txt from the filesystem. |
| 37 |
* |
| 38 |
* @return bool True on success, false on failure. |
| 39 |
*/ |
| 40 |
public function remove_file(): bool { |
| 41 |
if ( $this->is_file_system_available() ) { |
| 42 |
global $wp_filesystem; |
| 43 |
$result = $wp_filesystem->delete( $this->get_llms_file_path() ); |
| 44 |
|
| 45 |
return $result; |
| 46 |
} |
| 47 |
|
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Gets the contents of the current llms.txt file. |
| 53 |
* |
| 54 |
* @return string The content of the file. |
| 55 |
*/ |
| 56 |
public function get_file_contents(): string { |
| 57 |
if ( $this->is_file_system_available() ) { |
| 58 |
global $wp_filesystem; |
| 59 |
|
| 60 |
return $wp_filesystem->get_contents( $this->get_llms_file_path() ); |
| 61 |
} |
| 62 |
|
| 63 |
return ''; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Checks if the llms.txt file exists. |
| 68 |
* |
| 69 |
* @return bool Whether the llms.txt file exists. |
| 70 |
*/ |
| 71 |
public function file_exists(): bool { |
| 72 |
if ( $this->is_file_system_available() ) { |
| 73 |
global $wp_filesystem; |
| 74 |
|
| 75 |
return $wp_filesystem->exists( $this->get_llms_file_path() ); |
| 76 |
} |
| 77 |
|
| 78 |
return false; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Checks if the file system is available. |
| 83 |
* |
| 84 |
* @return bool If the file system is available. |
| 85 |
*/ |
| 86 |
private function is_file_system_available(): ?bool { |
| 87 |
if ( ! \function_exists( 'WP_Filesystem' ) ) { |
| 88 |
require_once \ABSPATH . 'wp-admin/includes/file.php'; |
| 89 |
} |
| 90 |
|
| 91 |
return \WP_Filesystem(); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Creates the path to the llms.txt file. |
| 96 |
* |
| 97 |
* @return string |
| 98 |
*/ |
| 99 |
private function get_llms_file_path(): string { |
| 100 |
|
| 101 |
$llms_filesystem_path = \get_home_path(); |
| 102 |
|
| 103 |
// phpcs:disable WordPress.Security.ValidatedSanitizedInput -- Reason: This is how we used this for the robots.txt file as well. |
| 104 |
if ( ! \is_writable( $llms_filesystem_path ) && ! empty( $_SERVER['DOCUMENT_ROOT'] ) ) { |
| 105 |
$llms_filesystem_path = $_SERVER['DOCUMENT_ROOT']; |
| 106 |
} |
| 107 |
// phpcs:enable WordPress.Security.ValidatedSanitizedInput |
| 108 |
|
| 109 |
/** |
| 110 |
* Filter: 'wpseo_llmstxt_filesystem_path' - Allows editing the filesystem path of the llmst.txt file to account for server restrictions to the filesystem. |
| 111 |
* |
| 112 |
* @param string $llms_filesystem_path The filesystem path of the llmst.txt file that defaults to get_home_path() or the $_SERVER['DOCUMENT_ROOT'] if the home path is not writeable. |
| 113 |
*/ |
| 114 |
$llms_filesystem_path = \apply_filters( 'wpseo_llmstxt_filesystem_path', $llms_filesystem_path ); |
| 115 |
|
| 116 |
return \trailingslashit( $llms_filesystem_path ) . 'llms.txt'; |
| 117 |
} |
| 118 |
} |
| 119 |
|