← All changes
|
src/services/content-api/endpoints/class-endpoint-analytics-posts.php
+32
-29
3.17.0
→
3.24.2
View file →
| @@ -88,11 +88,10 @@ | ||
| 88 | 88 | |
| 89 | 89 | /** |
| 90 | 90 | * Returns the endpoint URL for the API request. |
| 91 | 91 | * |
| 92 | - * This method appends the author, tag, and section parameters to the | |
| 93 | - * endpoint URL, if they are set. Since the Parse.ly API needs a key for | |
| 94 | - * every value (e.g. tag=tag1&tag=tag2), we need to append them manually. | |
| 92 | + * This function supports repeating keys in the URL, which is a Parse.ly API | |
| 93 | + * requirement for specifying multiple values (e.g. tag=tag1&tag=tag2). | |
| 95 | 94 | * |
| 96 | 95 | * @since 3.17.0 |
| 97 | 96 | * |
| 98 | 97 | * @param array<mixed> $query_args The arguments to pass to the API request. |
| @@ -98,30 +97,25 @@ | ||
| 98 | 97 | * @param array<mixed> $query_args The arguments to pass to the API request. |
| 99 | 98 | * @return string The endpoint URL for the API request. |
| 100 | 99 | */ |
| 101 | 100 | public function get_endpoint_url( array $query_args = array() ): string { |
| 102 | - // Store the author, tag, and section parameters. | |
| 103 | - /** @var array<string> $authors */ | |
| 104 | - $authors = $query_args['author'] ?? array(); | |
| 105 | - | |
| 101 | + // Store the values of the parameters requiring repeating keys. | |
| 106 | 102 | /** @var array<string> $tags */ |
| 107 | 103 | $tags = $query_args['tag'] ?? array(); |
| 108 | 104 | |
| 109 | - /** @var array<string> $sections */ | |
| 110 | - $sections = $query_args['section'] ?? array(); | |
| 105 | + /** @var array<string> $urls */ | |
| 106 | + $urls = $query_args['urls'] ?? array(); | |
| 111 | 107 | |
| 112 | - // Remove the author, tag, and section parameters from the query args. | |
| 113 | - unset( $query_args['author'] ); | |
| 108 | + // Remove the parameters requiring repeating keys. | |
| 114 | 109 | unset( $query_args['tag'] ); |
| 115 | - unset( $query_args['section'] ); | |
| 110 | + unset( $query_args['urls'] ); | |
| 116 | 111 | |
| 117 | 112 | // Generate the endpoint URL. |
| 118 | 113 | $endpoint_url = parent::get_endpoint_url( $query_args ); |
| 119 | 114 | |
| 120 | - // Append the author, tag, and section parameters to the endpoint URL. | |
| 121 | - $endpoint_url = $this->append_multiple_params_to_url( $endpoint_url, $authors, 'author' ); | |
| 122 | - $endpoint_url = $this->append_multiple_params_to_url( $endpoint_url, $tags, 'tag' ); | |
| 123 | - $endpoint_url = $this->append_multiple_params_to_url( $endpoint_url, $sections, 'section' ); | |
| 115 | + // Append the parameters requiring repeating keys to the endpoint URL. | |
| 116 | + $endpoint_url = $this->append_same_key_params_to_url( $endpoint_url, $tags, 'tag' ); | |
| 117 | + $endpoint_url = $this->append_same_key_params_to_url( $endpoint_url, $urls, 'url' ); | |
| 124 | 118 | |
| 125 | 119 | return $endpoint_url; |
| 126 | 120 | } |
| 127 | 121 | |
| @@ -152,29 +146,38 @@ | ||
| 152 | 146 | |
| 153 | 147 | return $this->request( 'GET', $query_args ); |
| 154 | 148 | } |
| 155 | 149 | |
| 156 | - | |
| 157 | 150 | /** |
| 158 | - * Appends multiple parameters to the URL. | |
| 151 | + * Appends multiple parameters with the same key to the passed URL. | |
| 159 | 152 | * |
| 160 | - * This is required because the Parsely API requires the multiple values for the author, tag, | |
| 161 | - * and section parameters to share the same key. | |
| 162 | - * | |
| 163 | 153 | * @since 3.17.0 |
| 164 | 154 | * |
| 165 | 155 | * @param string $url The URL to append the parameters to. |
| 166 | - * @param array<string> $params The parameters to append. | |
| 167 | - * @param string $param_name The name of the parameter. | |
| 156 | + * @param array<string> $values The parameter values to append. | |
| 157 | + * @param string $key The common key to be used for the parameters. | |
| 168 | 158 | * @return string The URL with the appended parameters. |
| 169 | 159 | */ |
| 170 | - protected function append_multiple_params_to_url( string $url, array $params, string $param_name ): string { | |
| 171 | - foreach ( $params as $param ) { | |
| 172 | - $param = rawurlencode( $param ); | |
| 173 | - if ( strpos( $url, $param_name . '=' ) === false ) { | |
| 174 | - $url = add_query_arg( $param_name, $param, $url ); | |
| 160 | + protected function append_same_key_params_to_url( | |
| 161 | + string $url, | |
| 162 | + array $values, | |
| 163 | + string $key | |
| 164 | + ): string { | |
| 165 | + if ( '' === $key ) { | |
| 166 | + return $url; | |
| 167 | + } | |
| 168 | + | |
| 169 | + foreach ( $values as $value ) { | |
| 170 | + if ( '' === $value ) { | |
| 171 | + continue; | |
| 172 | + } | |
| 173 | + | |
| 174 | + $value = rawurlencode( $value ); | |
| 175 | + | |
| 176 | + if ( false === strpos( $url, '?' ) ) { | |
| 177 | + $url .= '?' . $key . '=' . $value; | |
| 175 | 178 | } else { |
| 176 | - $url .= '&' . $param_name . '=' . $param; | |
| 179 | + $url .= '&' . $key . '=' . $value; | |
| 177 | 180 | } |
| 178 | 181 | } |
| 179 | 182 | |
| 180 | 183 | return $url; |