-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathclass-wp-service-workers.php
202 lines (169 loc) · 4.9 KB
/
class-wp-service-workers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
* Dependencies API: WP_Service_Workers class
*
* @since ?
*
* @package PWA
*/
/**
* Class used to register service workers.
*
* @since ?
*
* @see WP_Dependencies
*/
class WP_Service_Workers extends WP_Scripts {
/**
* Param for service workers.
*
* @var string
*/
public $query_var = 'wp_service_worker';
/**
* Output for service worker scope script.
*
* @var string
*/
public $output = '';
/**
* WP_Service_Workers constructor.
*/
public function __construct() {
parent::__construct();
global $wp_filesystem;
if ( ! class_exists( 'WP_Filesystem' ) ) {
require_once ABSPATH . '/wp-admin/includes/file.php';
}
if ( null === $wp_filesystem ) {
WP_Filesystem();
}
}
/**
* Initialize the class.
*/
public function init() {
/**
* Fires when the WP_Service_Workers instance is initialized.
*
* @param WP_Service_Workers $this WP_Service_Workers instance (passed by reference).
*/
do_action_ref_array( 'wp_default_service_workers', array( &$this ) );
}
/**
* Register service worker.
*
* Registers service worker if no item of that name already exists.
*
* @param string $handle Name of the item. Should be unique.
* @param string $path Path of the item relative to the WordPress root directory.
* @param array $deps Optional. An array of registered item handles this item depends on. Default empty array.
* @param array $scopes Optional. Scopes of the service worker. Default relative path.
* @return bool Whether the item has been registered. True on success, false on failure.
*/
public function register( $handle, $path, $deps = array(), $scopes = array() ) {
// Set default scope if missing.
if ( empty( $scopes ) ) {
$scopes = array( site_url( '/', 'relative' ) );
}
if ( false === parent::add( $handle, $path, $deps, false, compact( 'scopes' ) ) ) {
return false;
}
return true;
}
/**
* Get service worker logic for scope.
*
* @param string $scope Scope of the Service Worker.
*/
public function serve_request( $scope ) {
header( 'Content-Type: text/javascript; charset=utf-8' );
nocache_headers();
if ( ! in_array( $scope, $this->get_scopes(), true ) ) {
status_header( 404 );
return;
}
$scope_items = array();
// Get handles from the relevant scope only.
foreach ( $this->registered as $handle => $item ) {
if ( in_array( $scope, $item->args['scopes'], true ) ) {
$scope_items[] = $handle;
}
}
$this->output = '';
$this->do_items( $scope_items );
$file_hash = md5( $this->output );
header( "Etag: $file_hash" );
$etag_header = isset( $_SERVER['HTTP_IF_NONE_MATCH'] ) ? trim( $_SERVER['HTTP_IF_NONE_MATCH'] ) : false;
if ( $file_hash === $etag_header ) {
status_header( 304 );
return;
}
// @codingStandardsIgnoreLine
echo $this->output;
}
/**
* Get all scopes.
*
* @return array Array of scopes.
*/
public function get_scopes() {
$scopes = array();
foreach ( $this->registered as $handle => $item ) {
$scopes = array_merge( $scopes, $item->args['scopes'] );
}
return $scopes;
}
/**
* Process one registered script.
*
* @param string $handle Handle.
* @param bool $group Group.
* @return void
*/
public function do_item( $handle, $group = false ) {
global $wp_filesystem;
$obj = $this->registered[ $handle ];
$this->output .= $wp_filesystem->get_contents( $this->get_validated_file_path( $obj->src ) ) . "\n";
}
/**
* Remove URL scheme.
*
* @param string $schemed_url URL.
* @return string URL.
*/
public function remove_url_scheme( $schemed_url ) {
return preg_replace( '#^\w+:(?=//)#', '', $schemed_url );
}
/**
* Get validated path to file.
*
* @param string $url Relative path.
* @return null|string|WP_Error
*/
public function get_validated_file_path( $url ) {
$needs_base_url = ! preg_match( '|^(https?:)?//|', $url );
$base_url = site_url();
if ( $needs_base_url ) {
$url = $base_url . $url;
}
// Strip URL scheme, query, and fragment.
$url = $this->remove_url_scheme( preg_replace( ':[\?#].*$:', '', $url ) );
$content_url = $this->remove_url_scheme( content_url( '/' ) );
$allowed_host = wp_parse_url( $content_url, PHP_URL_HOST );
$url_host = wp_parse_url( $url, PHP_URL_HOST );
if ( $allowed_host !== $url_host ) {
/* translators: %s is file URL */
return new WP_Error( 'external_file_url', sprintf( __( 'URL is located on an external domain: %s.', 'pwa' ), $url_host ) );
}
$file_path = null;
if ( 0 === strpos( $url, $content_url ) ) {
$file_path = WP_CONTENT_DIR . substr( $url, strlen( $content_url ) - 1 );
}
if ( ! $file_path || false !== strpos( '../', $file_path ) || 0 !== validate_file( $file_path ) || ! file_exists( $file_path ) ) {
/* translators: %s is file URL */
return new WP_Error( 'file_path_not_found', sprintf( __( 'Unable to locate filesystem path for %s.', 'pwa' ), $url ) );
}
return $file_path;
}
}