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

Yoast / wordpress-seo / 99ff5821fbe8444b259463501ff00132570d3061

25 Mar 2025 09:23AM UTC coverage: 52.446% (+3.7%) from 48.71%
99ff5821fbe8444b259463501ff00132570d3061

Pull #21958

github

web-flow
Merge d493347a3 into facbdded4
Pull Request #21958: Improve function words list for Farsi

7990 of 14101 branches covered (56.66%)

Branch coverage included in aggregate %.

20 of 20 new or added lines in 1 file covered. (100.0%)

1567 existing lines in 41 files now uncovered.

29816 of 57984 relevant lines covered (51.42%)

41124.2 hits per line

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

59.46
/admin/services/class-file-size.php
1
<?php
2
/**
3
 * WPSEO plugin file.
4
 *
5
 * @package WPSEO\Admin\Services
6
 */
7

8
/**
9
 * Represents the file size service.
10
 */
11
class WPSEO_File_Size_Service {
12

13
        /**
14
         * Retrieves an indexable.
15
         *
16
         * @param WP_REST_Request $request The request object.
17
         *
18
         * @return WP_REST_Response The response.
19
         */
20
        public function get( WP_REST_Request $request ) {
10✔
21
                try {
22
                        $file_url = $this->get_file_url( $request );
10✔
23

24
                        return new WP_REST_Response(
6✔
25
                                [
6✔
26
                                        'type'          => 'success',
6✔
27
                                        'size_in_bytes' => $this->get_file_size( $file_url ),
6✔
28
                                ],
6✔
29
                                200
6✔
30
                        );
6✔
31
                }
32
                catch ( WPSEO_File_Size_Exception $exception ) {
6✔
33
                        return new WP_REST_Response(
6✔
34
                                [
6✔
35
                                        'type'     => 'failure',
6✔
36
                                        'response' => $exception->getMessage(),
6✔
37
                                ],
6✔
38
                                404
6✔
39
                        );
6✔
40
                }
41
        }
42

43
        /**
44
         * Retrieves the file url.
45
         *
46
         * @param WP_REST_Request $request The request to retrieve file url from.
47
         *
48
         * @return string The file url.
49
         * @throws WPSEO_File_Size_Exception The file is hosted externally.
50
         */
51
        protected function get_file_url( WP_REST_Request $request ) {
4✔
52
                $file_url = rawurldecode( $request->get_param( 'url' ) );
4✔
53

54
                if ( ! $this->is_externally_hosted( $file_url ) ) {
4✔
55
                        return $file_url;
2✔
56
                }
57

58
                throw WPSEO_File_Size_Exception::externally_hosted( $file_url );
2✔
59
        }
60

61
        /**
62
         * Checks if the file is hosted externally.
63
         *
64
         * @param string $file_url The file url.
65
         *
66
         * @return bool True if it is hosted externally.
67
         */
68
        protected function is_externally_hosted( $file_url ) {
×
69
                return wp_parse_url( home_url(), PHP_URL_HOST ) !== wp_parse_url( $file_url, PHP_URL_HOST );
×
70
        }
71

72
        /**
73
         * Returns the file size.
74
         *
75
         * @param string $file_url The file url to get the size for.
76
         *
77
         * @return int The file size.
78
         * @throws WPSEO_File_Size_Exception Retrieval of file size went wrong for unknown reasons.
79
         */
80
        protected function get_file_size( $file_url ) {
×
81
                $file_config = wp_upload_dir();
×
82
                $file_url    = str_replace( $file_config['baseurl'], '', $file_url );
×
83
                $file_size   = $this->calculate_file_size( $file_url );
×
84

85
                if ( ! $file_size ) {
×
86
                        throw WPSEO_File_Size_Exception::unknown_error( $file_url );
×
87
                }
88

89
                return $file_size;
×
90
        }
91

92
        /**
93
         * Calculates the file size using the Utils class.
94
         *
95
         * @param string $file_url The file to retrieve the size for.
96
         *
97
         * @return int|bool The file size or False if it could not be retrieved.
98
         */
99
        protected function calculate_file_size( $file_url ) {
×
100
                return WPSEO_Image_Utils::get_file_size(
×
UNCOV
101
                        [
×
102
                                'path' => $file_url,
×
UNCOV
103
                        ]
×
UNCOV
104
                );
×
105
        }
106
}
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