Skip to content

Commit 3882580

Browse files
committed
Add a filter on the api parameters
#16
1 parent 77a7a65 commit 3882580

File tree

1 file changed

+42
-5
lines changed

1 file changed

+42
-5
lines changed

src/Requests/API.php

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,51 @@ public function make_request( $api_params = array() ) {
8989
* @return array
9090
*/
9191
private function get_body( array $api_params ) {
92-
return wp_parse_args(
93-
$api_params,
94-
array(
95-
'url' => rawurlencode( home_url() ),
96-
)
92+
93+
/**
94+
* Filters the API parameters. The hook is specific to the API URL.
95+
* For example, if the API URL is https://example.com, the hook will be `edd_sl_sdk_api_params_example`.
96+
*
97+
* @since <next-version>
98+
* @param array $api_params The parameters for the specific request.
99+
* @param string $api_url The API URL.
100+
* @return array
101+
*/
102+
return apply_filters(
103+
$this->get_api_filter_hook(),
104+
wp_parse_args(
105+
$api_params,
106+
array(
107+
'url' => rawurlencode( home_url() ),
108+
'environment' => wp_get_environment_type(),
109+
)
110+
),
111+
$this->api_url
97112
);
98113
}
99114

115+
/**
116+
* Gets the API hook.
117+
*
118+
* @since <next-version>
119+
* @return string
120+
*/
121+
private function get_api_filter_hook() {
122+
$url = wp_parse_url( $this->api_url );
123+
if ( empty( $url['host'] ) ) {
124+
return 'edd_sl_sdk_api_params';
125+
}
126+
127+
$base = explode( '.', $url['host'] );
128+
129+
// If the base is a subdomain, use the main domain.
130+
if ( count( $base ) > 2 ) {
131+
$base = array_slice( $base, 1 );
132+
}
133+
134+
return 'edd_sl_sdk_api_params_' . reset( $base );
135+
}
136+
100137
/**
101138
* Determines if a request has recently failed.
102139
*

0 commit comments

Comments
 (0)