-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
see #607: Introduce WordLift_Autocomplete_Service & Wordlift_Autocomp…
…lete_Adapter
- Loading branch information
Showing
4 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
/** | ||
* Wordlift_Autocomplete_Adapter class. | ||
* | ||
* The {@link Wordlift_Autocomplete_Adapter} class create requests to external API's. | ||
* | ||
* @link https://wordlift.io | ||
* | ||
* @package Wordlift | ||
* @since 3.15.0 | ||
*/ | ||
|
||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; | ||
} | ||
|
||
/** | ||
* Create autocomplete request to external API's and return the result if there is such. | ||
* | ||
* @since 3.15.0 | ||
*/ | ||
class Wordlift_Autocomplete_Adapter { | ||
|
||
/** | ||
* @var Wordlift_Ajax_Service | ||
*/ | ||
private $autocomplete_service; | ||
|
||
|
||
/** | ||
* Wordlift_Batch_Analysis_Adapter constructor. | ||
* | ||
* @since 3.14.2 | ||
* | ||
* @param \Wordlift_Autocomplete_Service $autocomplete_service | ||
*/ | ||
public function __construct( $autocomplete_service ) { | ||
$this->autocomplete_service = $autocomplete_service; | ||
} | ||
|
||
/** | ||
* Handle the autocomplete ajax request. | ||
* | ||
* @since 3.15.0 | ||
* | ||
* @return string The entity base path. | ||
*/ | ||
public function wl_autocomplete() { | ||
// if ( ! wp_verify_nonce( $_REQUEST['nonce'], 'wordlift_autocomplete' ) ) { | ||
// wp_send_json_error( array( 'message' => 'Nonce field doens\'t match' ) ); | ||
// } | ||
|
||
// Return | ||
if ( empty( $_REQUEST['query'] ) ) { | ||
wp_send_json_error( array( 'message' => 'The query param is empty!' ) ); | ||
} | ||
|
||
// Make request. | ||
$response = $this->autocomplete_service->make_request( $_REQUEST['query'] ); | ||
// Decode response body. | ||
$suggestions = json_decode( wp_remote_retrieve_body( $response ), true ); | ||
|
||
// Clear any buffer. | ||
ob_clean(); | ||
|
||
// If the response is valid, then send the suggestions. | ||
if ( ! is_wp_error( $response ) && 200 === (int) $response['response']['code'] ) { | ||
// Echo the response. | ||
wp_send_json_success( array( 'suggestions' => $suggestions ) ); | ||
} else { | ||
// There is an error, so send error message. | ||
wp_send_json_error( array( 'message' => __( 'Something went wrong.' ) ) ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
/** | ||
* Wordlift_Autocomplete_Service class. | ||
* | ||
* The {@link Wordlift_Autocomplete_Service} class handle and process all autocomplete requests. | ||
* | ||
* @link https://wordlift.io | ||
* | ||
* @package Wordlift | ||
* @since 3.15.0 | ||
*/ | ||
|
||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; | ||
} | ||
|
||
/** | ||
* Process WordLift's autocomplete requests. | ||
* | ||
* @since 3.15.0 | ||
*/ | ||
class Wordlift_Autocomplete_Service { | ||
/** | ||
* The {@link Wordlift_Configuration_Service} instance. | ||
* | ||
* @since 3.15.0 | ||
* @access private | ||
* @var \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. | ||
*/ | ||
private $configuration_service; | ||
|
||
/** | ||
* A {@link Wordlift_Log_Service} instance. | ||
* | ||
* @since 3.15.0 | ||
* @access private | ||
* @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. | ||
*/ | ||
private $log; | ||
|
||
/** | ||
* The {@link Class_Wordlift_Autocomplete_Service} instance. | ||
* | ||
* @since 3.15.0 | ||
* | ||
* @param \Wordlift_Configuration_Service $configuration_service The {@link Wordlift_Configuration_Service} instance. | ||
*/ | ||
public function __construct ( $configuration_service ) { | ||
$this->configuration_service = $configuration_service; | ||
$this->log = Wordlift_Log_Service::get_logger( 'Wordlift_Autocomplete_Service' ); | ||
} | ||
|
||
/** | ||
* Make request to external API and return the | ||
* | ||
* @since 3.15.0 | ||
* | ||
* @param string $query The search string | ||
* | ||
* @return array The number of founded suggestions. | ||
*/ | ||
public function make_request( $query ) { | ||
$url = $this->build_request_url( $query ); | ||
|
||
// Make request. | ||
$response = wp_remote_get( $url ); | ||
|
||
// return the response. | ||
return $response; | ||
} | ||
|
||
/** | ||
* Build the autocomplete url. | ||
* | ||
* @since 3.15.0 | ||
* | ||
* @param string $query The search string | ||
* | ||
* @return string Builded url. | ||
*/ | ||
public function build_request_url( $query ) { | ||
$args = array( | ||
'key' => $this->configuration_service->get_key(), | ||
'language' => $this->configuration_service->get_language_code(), | ||
'query' => $query, | ||
'limit' => 50, | ||
); | ||
|
||
// Add args to URL. | ||
$request_url = add_query_arg( | ||
$args, | ||
$this->configuration_service->get_autocomplete_url() | ||
); | ||
|
||
// return the builded url. | ||
return $request_url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters