1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* joltw - To be named. jetpack#19212 |
4
|
|
|
* Jetpack interface for calling atomic-auth-proxy on wpcom. |
5
|
|
|
* |
6
|
|
|
* @package automattic/jetpack |
7
|
|
|
* @since ??? |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
use Automattic\Jetpack\Connection\Client; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* joltw - Call wpcom atomic-auth-proxy from jetpack. |
14
|
|
|
* |
15
|
|
|
* @since ??? |
16
|
|
|
*/ |
17
|
|
|
class WPCOM_REST_API_V2_Endpoint_Joltw extends WP_REST_Controller { |
18
|
|
|
/** |
19
|
|
|
* Constructor. |
20
|
|
|
*/ |
21
|
|
|
public function __construct() { |
22
|
|
|
$this->namespace = 'wpcom/v2'; |
23
|
|
|
$this->rest_base = 'joltw'; |
24
|
|
|
add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Register the route. |
29
|
|
|
*/ |
30
|
|
View Code Duplication |
public function register_routes() { |
31
|
|
|
register_rest_route($this->namespace, '/' . $this->rest_base . '/file(?P<path>/.+)?', [ |
32
|
|
|
[ |
33
|
|
|
'show_in_index' => false, |
34
|
|
|
'methods' => WP_REST_Server::READABLE, |
35
|
|
|
'callback' => [ $this, 'fetch_remote_media_file' ], |
36
|
|
|
'permission_callback' => [ $this, 'permission_check' ], |
37
|
|
|
'args' => [], |
38
|
|
|
], |
39
|
|
|
] ); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function permission_check( WP_REST_Request $request ) { |
43
|
|
|
return true; |
44
|
|
|
/* |
45
|
|
|
return ( |
46
|
|
|
$this->is_requested_by_trusted_client() && |
47
|
|
|
$this->is_site_accessible_by_current_user() && |
48
|
|
|
$this->is_atomic_site() && |
49
|
|
|
$this->is_active_site() |
50
|
|
|
); |
51
|
|
|
*/ |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function fetch_remote_media_file( WP_REST_Request $request ) { |
55
|
|
|
$query_parameters = $request->get_query_params(); |
56
|
|
|
if ( isset( $query_parameters['path'] ) ) { |
57
|
|
|
$path = $query_parameters['path']; |
|
|
|
|
58
|
|
|
unset( $query_parameters['path'] ); |
59
|
|
|
} else { |
60
|
|
|
$path = $request->get_param( 'path' ); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// 'https://public-api.wordpress.com/wpcom/v2/sites/testsitemmrtag.wordpress.com/atomic-auth-proxy/file/wp-content/uploads/2021/03/drone-4.jpg?resize=214%2C214' |
64
|
|
|
$site_id = 190440205; // testsitemmrtag.wordpress.com |
65
|
|
|
$file_path = 'wp-content/uploads/2021/03/drone-4.jpg?resize=214%2C214'; |
66
|
|
|
$endpoint = sprintf( '/sites/%d/atomic-auth-proxy/file/', $site_id ) . $file_path; |
67
|
|
|
/* return ['this' => 'works']; */ |
68
|
|
|
|
69
|
|
|
//$abc = Client::wpcom_json_api_request_as_blog( |
70
|
|
|
$abc = Client::wpcom_json_api_request_as_user( |
71
|
|
|
$endpoint, |
72
|
|
|
'2', |
73
|
|
|
array(), //$args, |
74
|
|
|
null, |
75
|
|
|
'wpcom' |
76
|
|
|
); |
77
|
|
|
return ['this' => 'works2', 'abc' => $abc]; |
78
|
|
|
|
79
|
|
|
/* |
80
|
|
|
$remote_file_url = $this->build_remote_file_url( |
81
|
|
|
$path, |
82
|
|
|
$query_parameters |
83
|
|
|
); |
84
|
|
|
if ( is_wp_error( $remote_file_url ) ) { |
85
|
|
|
return $remote_file_url; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$cookies = $this->cached_fetch_read_access_cookies(); |
89
|
|
|
if ( is_wp_error( $cookies ) ) { |
90
|
|
|
return $cookies; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$result = $this->stream_remote_file_to_browser( $request->get_method(), $remote_file_url, $cookies ); |
94
|
|
|
if ( is_wp_error( $result ) ) { |
95
|
|
|
return $result; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
die(); |
99
|
|
|
*/ |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Joltw' ); |
103
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.