• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 481faf637ba3a7c1e5f2bfef59b582ac39523de6

21 May 2025 09:11AM UTC coverage: 46.174%. First build
481faf637ba3a7c1e5f2bfef59b582ac39523de6

Pull #22291

github

web-flow
Merge pull request #22278 from Yoast/577-sanitize-and-deal-with-special-markdown-characters-before-writing-to-file

577 sanitize and deal with special markdown characters before writing to file
Pull Request #22291: Introduce the llms.txt feature

52 of 403 new or added lines in 30 files covered. (12.9%)

15586 of 33755 relevant lines covered (46.17%)

3.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/llms-txt/infrastructure/file/wordpress-file-system-adapter.php
1
<?php
2
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong
3
namespace Yoast\WP\SEO\Llms_Txt\Infrastructure\File;
4

5
use Yoast\WP\SEO\Llms_Txt\Domain\File\Llms_File_System_Interface;
6

7
/**
8
 * Adapter class for handling file system operations in a WordPress environment.
9
 */
10
class WordPress_File_System_Adapter implements Llms_File_System_Interface {
11

12
        /**
13
         * Creates a file and writes the specified content to it.
14
         *
15
         * @param string $content The content to write into the file.
16
         *
17
         * @return bool True on success, false on failure.
18
         */
NEW
19
        public function set_file_content( string $content ): bool {
×
NEW
20
                if ( $this->is_file_system_available() ) {
×
NEW
21
                        global $wp_filesystem;
×
NEW
22
                        $result = $wp_filesystem->put_contents(
×
NEW
23
                                $this->get_llms_file_path(),
×
NEW
24
                                $content,
×
NEW
25
                                \FS_CHMOD_FILE
×
NEW
26
                        );
×
27

NEW
28
                        return $result;
×
29
                }
30

NEW
31
                return false;
×
32
        }
33

34
        /**
35
         * Removes the llms.txt from the filesystem.
36
         *
37
         * @return bool True on success, false on failure.
38
         */
NEW
39
        public function remove_file(): bool {
×
NEW
40
                if ( $this->is_file_system_available() ) {
×
NEW
41
                        global $wp_filesystem;
×
NEW
42
                        $result = $wp_filesystem->delete( $this->get_llms_file_path() );
×
43

NEW
44
                        return $result;
×
45
                }
46

NEW
47
                return false;
×
48
        }
49

50
        /**
51
         * Gets the contents of the current llms.txt file.
52
         *
53
         * @return string The content of the file.
54
         */
NEW
55
        public function get_file_contents(): string {
×
NEW
56
                if ( $this->is_file_system_available() ) {
×
NEW
57
                        global $wp_filesystem;
×
58

NEW
59
                        return $wp_filesystem->get_contents( $this->get_llms_file_path() );
×
60
                }
61

NEW
62
                return '';
×
63
        }
64

65
        /**
66
         * Checks if the llms.txt file exists.
67
         *
68
         * @return bool Whether the llms.txt file exists.
69
         */
NEW
70
        public function file_exists(): bool {
×
NEW
71
                if ( $this->is_file_system_available() ) {
×
NEW
72
                        global $wp_filesystem;
×
73

NEW
74
                        return $wp_filesystem->exists( $this->get_llms_file_path() );
×
75
                }
76

NEW
77
                return false;
×
78
        }
79

80
        /**
81
         * Checks if the file system is available.
82
         *
83
         * @return bool If the file system is available.
84
         */
NEW
85
        private function is_file_system_available(): ?bool {
×
NEW
86
                if ( ! \function_exists( 'WP_Filesystem' ) ) {
×
NEW
87
                        require_once \ABSPATH . 'wp-admin/includes/file.php';
×
88
                }
89

NEW
90
                return \WP_Filesystem();
×
91
        }
92

93
        /**
94
         * Creates the path to the llms.txt file.
95
         *
96
         * @return string
97
         */
NEW
98
        private function get_llms_file_path(): string {
×
NEW
99
                return \trailingslashit( \get_home_path() ) . 'llms.txt';
×
100
        }
101
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc