Skip to content

Commit

Permalink
Handle encoded urls on jsonLD exposition endpoint (#1757)
Browse files Browse the repository at this point in the history
* fix: incorrectly merged composer.json

* fix: jsonLD endpoint handles encoded urls

* add mock for wordpress-configuration request

* do not send include/exclude configurations if the key isn't set

---------

Co-authored-by: David Riccitelli <david@wordlift.io>
  • Loading branch information
rpanfili and ziodave authored Jan 15, 2025
1 parent 86717a8 commit 3e806ae
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 646 deletions.
12 changes: 11 additions & 1 deletion src/classes/jsonld/class-jsonld-endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function jsonld_using_meta( $request ) {

$meta_key = $request['meta_key'];
$params = $request->get_query_params();
$meta_value = urldecode( $params['meta_value'] );
$meta_value = $params['meta_value'];
$meta_values = array( $meta_value );
// Merchant Sync stores spaces as plus, so we need to restore them.
if ( strpos( $meta_value, ' ' ) > 0 ) {
Expand All @@ -238,6 +238,16 @@ public function jsonld_using_meta( $request ) {
$meta_values[] = str_replace( '+', ' ', $meta_value );
}

$contains_whitespace = strpos( $meta_value, ' ' ) !== false;
$contains_plus = strpos( $meta_value, '+' ) !== false;

if ( $contains_whitespace ) {
$meta_values[] = str_replace( ' ', '+', $meta_value );
}
if ( $contains_plus ) {
$meta_values[] = str_replace( '+', ' ', $meta_value );
}

$sql = "
SELECT pm.post_id AS id, %s AS type
FROM {$wpdb->postmeta} pm
Expand Down
5 changes: 5 additions & 0 deletions src/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,10 @@
},
"require": {
"deliciousbrains/wp-background-processing": "^1.0"
},
"config": {
"allow-plugins": {
"cweagans/composer-patches": false
}
}
}
68 changes: 63 additions & 5 deletions src/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Wordlift\Modules\Include_Exclude_Push_Config;

use Wordlift\Api\Api_Service;
use Wordlift\Api\Response;
use Wordlift\Modules\Include_Exclude\Configuration;
use Wordlift_Configuration_Service;

Expand Down Expand Up @@ -37,7 +38,7 @@ public function handle_init() {
// Send the default configuration only if it hasn't been sent already.
if ( ! get_option( '_wl_include_exclude_default_sent', false ) ) {
$response = $this->send();
if ( $response->is_success() ) {
if ( is_a( $response, Response::class ) && $response->is_success() ) {
update_option( '_wl_include_exclude_default_sent', true, true );
}
}
Expand All @@ -49,18 +50,30 @@ public function handle_update_option_wl_exclude_include_urls_settings() {
$this->handle_init();
}

public function send() {
/**
* Send the include/exclude default configuration.
*
* @return null|Response
*/
public function send(): ?Response {
// I prefer to instantiate the `Wordlift_Configuration_Service` and `Configuration` here
// in order to avoid preemptive unused instantiation.
$key = rawurlencode( Wordlift_Configuration_Service::get_instance()->get_key() );
$key = Wordlift_Configuration_Service::get_instance()->get_key();

// That makes no sense for us to send such a request if the key isn't set.
if ( empty( $key ) ) {
return null;
}

$encoded_key = rawurlencode( $key );
$wp_admin = rawurlencode( untrailingslashit( get_admin_url() ) );
$wp_json = rawurlencode( untrailingslashit( get_rest_url() ) );
$wp_include_exclude_default = rawurlencode( Configuration::get_instance()->get_default() );

return $this->api_service->request(
'PUT',
'/accounts/wordpress-configuration?'
. "key=$key"
. "key=$encoded_key"
. "&wpAdmin=$wp_admin"
. "&wpJson=$wp_json"
. "&wp_include_exclude_default=$wp_include_exclude_default"
Expand Down
Loading

0 comments on commit 3e806ae

Please sign in to comment.