Skip to content

Commit

Permalink
see #607: Introduce WordLift_Autocomplete_Service & Wordlift_Autocomp…
Browse files Browse the repository at this point in the history
…lete_Adapter
  • Loading branch information
stoyan0v committed Sep 4, 2017
1 parent c483b24 commit 5d8d4f6
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/includes/class-wordlift-autocomplete-adapter.php
@@ -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.' ) ) );
}
}
}
98 changes: 98 additions & 0 deletions src/includes/class-wordlift-autocomplete-service.php
@@ -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;
}
}
13 changes: 13 additions & 0 deletions src/includes/class-wordlift-configuration-service.php
Expand Up @@ -430,4 +430,17 @@ public function get_batch_analysis_url() {

}

/**
* Get the URL to perform autocomplete request.
*
* @since 3.15.0
*
* @return string The URL to call to perform the batch analyzes.
*/
public function get_autocomplete_url() {

return WL_CONFIG_WORDLIFT_API_URL_DEFAULT_VALUE . '/autocomplete';

}

}
30 changes: 30 additions & 0 deletions src/includes/class-wordlift.php
Expand Up @@ -588,6 +588,24 @@ class Wordlift {
*/
protected $rendition_factory;

/**
* The {@link Wordlift_Autocomplete_Service} instance.
*
* @since 3.15.0
* @access private
* @var \Wordlift_Autocomplete_Service $autocomplete_service The {@link Wordlift_Autocomplete_Service} instance.
*/
private $autocomplete_service;

/**
* The {@link Wordlift_Autocomplete_Adapter} instance.
*
* @since 3.15.0
* @access private
* @var \Wordlift_Autocomplete_Adapter $autocomplete_adapter The {@link Wordlift_Autocomplete_Adapter} instance.
*/
private $autocomplete_adapter;

/**
* The {@link Wordlift_Relation_Service} instance.
*
Expand Down Expand Up @@ -849,6 +867,10 @@ private function load_dependencies() {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/wp-async-task/class-wordlift-batch-analysis-request-async-task.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/wp-async-task/class-wordlift-batch-analysis-complete-async-task.php';

/** Async Tasks. */
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wordlift-autocomplete-service.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wordlift-autocomplete-adapter.php';

/**
* The class responsible for defining all actions that occur in the admin area.
*/
Expand Down Expand Up @@ -1102,6 +1124,10 @@ private function load_dependencies() {
new Wordlift_Batch_Analysis_Request_Async_Task();
new Wordlift_Batch_Analysis_Complete_Async_Task();

/** WL Autocomplete. */
$this->autocomplete_service = new Wordlift_Autocomplete_Service( $this->configuration_service );
$this->autocomplete_adapter = new Wordlift_Autocomplete_Adapter( $this->autocomplete_service );

/** WordPress Admin UI. */

// UI elements.
Expand Down Expand Up @@ -1308,6 +1334,10 @@ private function define_admin_hooks() {
$this->loader->add_action( 'wp_ajax_wl_sample_data_create', $this->sample_data_ajax_adapter, 'create' );
$this->loader->add_action( 'wp_ajax_wl_sample_data_delete', $this->sample_data_ajax_adapter, 'delete' );

// Handle the autocomplete request.
add_action( 'wp_ajax_wl_autocomplete', array( $this->autocomplete_adapter, 'wl_autocomplete') );
add_action( 'wp_ajax_nopriv_wl_autocomplete', array( $this->autocomplete_adapter, 'wl_autocomplete') );

// Hooks to restrict multisite super admin from manipulating entity types.
if ( is_multisite() ) {
$this->loader->add_filter( 'map_meta_cap', $this->entity_type_admin_page, 'restrict_super_admin', 10, 4 );
Expand Down

0 comments on commit 5d8d4f6

Please sign in to comment.