PluginProbe ʕ •ᴥ•ʔ
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.1.1
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.1.1
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 19.1 19.10 19.11 19.12 19.13 19.14 19.2 19.3 19.4 19.5 19.5.1 19.6 19.6.1 19.7 19.7.1 19.7.2 19.8 19.9 20.0 20.1 20.10 20.11 20.12 20.13 20.2 20.2.1 20.3 20.4 20.5 20.6 20.7 20.8 20.9 21.0 21.1 21.2 21.3 21.4 21.5 21.6 21.7 21.8 21.8.1 21.9 21.9.1 22.0 22.1 22.2 22.3 22.4 22.5 22.6 22.7 22.8 22.9 23.0 23.1 23.2 23.3 23.4 23.5 23.6 23.7 23.8 23.9 24.0 24.1 24.2 24.3 24.4 24.5 24.6 24.7 24.8 24.8.1 24.9 25.0 25.1 25.2 25.3 25.3.1 25.4 25.5 25.6 25.7 25.8 25.9 26.0 26.1 26.1.1 26.2 26.3 26.4 26.5 26.6 26.7 26.8 26.9 27.0 27.1 27.1.1 27.2 27.3 27.4
wordpress-seo / admin / services / class-file-size.php
wordpress-seo / admin / services Last commit date
class-file-size.php 3 months ago
class-file-size.php
106 lines
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 ) {
21 try {
22 $file_url = $this->get_file_url( $request );
23
24 return new WP_REST_Response(
25 [
26 'type' => 'success',
27 'size_in_bytes' => $this->get_file_size( $file_url ),
28 ],
29 200,
30 );
31 } catch ( WPSEO_File_Size_Exception $exception ) {
32 return new WP_REST_Response(
33 [
34 'type' => 'failure',
35 'response' => $exception->getMessage(),
36 ],
37 404,
38 );
39 }
40 }
41
42 /**
43 * Retrieves the file url.
44 *
45 * @param WP_REST_Request $request The request to retrieve file url from.
46 *
47 * @return string The file url.
48 * @throws WPSEO_File_Size_Exception The file is hosted externally.
49 */
50 protected function get_file_url( WP_REST_Request $request ) {
51 $file_url = rawurldecode( $request->get_param( 'url' ) );
52
53 if ( ! $this->is_externally_hosted( $file_url ) ) {
54 return $file_url;
55 }
56
57 throw WPSEO_File_Size_Exception::externally_hosted( $file_url );
58 }
59
60 /**
61 * Checks if the file is hosted externally.
62 *
63 * @param string $file_url The file url.
64 *
65 * @return bool True if it is hosted externally.
66 */
67 protected function is_externally_hosted( $file_url ) {
68 return wp_parse_url( home_url(), PHP_URL_HOST ) !== wp_parse_url( $file_url, PHP_URL_HOST );
69 }
70
71 /**
72 * Returns the file size.
73 *
74 * @param string $file_url The file url to get the size for.
75 *
76 * @return int The file size.
77 * @throws WPSEO_File_Size_Exception Retrieval of file size went wrong for unknown reasons.
78 */
79 protected function get_file_size( $file_url ) {
80 $file_config = wp_upload_dir();
81 $file_url = str_replace( $file_config['baseurl'], '', $file_url );
82 $file_size = $this->calculate_file_size( $file_url );
83
84 if ( ! $file_size ) {
85 throw WPSEO_File_Size_Exception::unknown_error( $file_url );
86 }
87
88 return $file_size;
89 }
90
91 /**
92 * Calculates the file size using the Utils class.
93 *
94 * @param string $file_url The file to retrieve the size for.
95 *
96 * @return int|bool The file size or False if it could not be retrieved.
97 */
98 protected function calculate_file_size( $file_url ) {
99 return WPSEO_Image_Utils::get_file_size(
100 [
101 'path' => $file_url,
102 ],
103 );
104 }
105 }
106