PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.6
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.6
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 27.6, at admin/services/class-file-size.php

106 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 } 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