PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.7
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.7
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 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 All 129 releases
wordpress-seo / admin / services / class-file-size.php

class-file-size.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.7, at admin/services/class-file-size.php

107 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }
32 catch ( WPSEO_File_Size_Exception $exception ) {
33 return new WP_REST_Response(
34 [
35 'type' => 'failure',
36 'response' => $exception->getMessage(),
37 ],
38 404
39 );
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 ) {
52 $file_url = rawurldecode( $request->get_param( 'url' ) );
53
54 if ( ! $this->is_externally_hosted( $file_url ) ) {
55 return $file_url;
56 }
57
58 throw WPSEO_File_Size_Exception::externally_hosted( $file_url );
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(
101 [
102 'path' => $file_url,
103 ]
104 );
105 }
106 }
107