Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
Merge branch 'release/v0.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosescri committed Oct 23, 2018
2 parents 161cbd5 + 2b68c7f commit c5f3be5
Show file tree
Hide file tree
Showing 18 changed files with 656 additions and 211 deletions.
4 changes: 2 additions & 2 deletions doofinder/doofinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Doofinder
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Version: 0.1.1
* Version: 0.2.0
* Author: Doofinder
* Description: Integrate Doofinder Search in your WordPress website.
*
Expand All @@ -30,7 +30,7 @@ class Doofinder_For_WordPress {
*
* @var string
*/
public static $version = '0.1.1';
public static $version = '0.2.0';

/**
* The only instance of Doofinder_For_WordPress
Expand Down
8 changes: 3 additions & 5 deletions doofinder/includes/api/class-api-wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ public function remove_item( $item_type, $id );
public function send_batch( $items_type, array $items );

/**
* Remove one type from the index.
* Remove all post types from the API.
*
* @param string $type
*
* @return string Status of the operation, from Api_Status
* @return string Status of the operation.
*/
public function remove_type( $type );
public function remove_types();
}
28 changes: 11 additions & 17 deletions doofinder/includes/api/class-doofinder-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,29 +207,23 @@ public function send_batch( $items_type, array $items ) {
/**
* @inheritdoc
*/
public function remove_type( $type ) {
public function remove_types() {
if ( ! $this->search_engine ) {
$this->log->log( 'Invalid search engine.' );

return Api_Status::$invalid_search_engine;
}

// Doofinder API will throw an exception in case of invalid token
// or something like that.
try {
if ( ! $this->search_engine ) {
$this->log->log( 'Invalid search engine.' );

return Api_Status::$invalid_search_engine;
}

$this->search_engine->deleteType( $type );

return Api_Status::$success;
} catch ( NotFound $exception ) {
// We cannot remove the type because it doesn't exist.
// That's not really an error, that's expected if we're trying
// to add a new type.
$this->search_engine->deleteType( $this->search_engine->getTypes() );

return Api_Status::$success;
} catch ( IndexingInProgress $exception ) {
// We requested deletion of the post type and the server
// is still processing that request.
// We should retry the request in a moment.
// This exception is thrown after we've sent request to delete a type
// and the server is not finished processing it yet.
// We'll have to retry the request in a moment.

$this->log->log( $exception );

Expand Down
4 changes: 2 additions & 2 deletions doofinder/includes/api/class-local-dump.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,13 @@ public function send_batch( $items_type, array $items ) {
/**
* @inheritdoc
*/
public function remove_type( $type ) {
public function remove_types() {
// Fail the API call if we want to test something.
if ( $this->should_fail ) {
return Api_Status::$unknown_error;
}

$this->log->log( "Removing $type" );
$this->log->log('- Removing all post types -');

return Api_Status::$success;
}
Expand Down
50 changes: 23 additions & 27 deletions doofinder/includes/class-data-index.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function ajax_handler() {
* @return bool True if the indexing has finished.
*/
public function index_posts() {
$this->indexing_data->set( 'status', 'processing' );
$this->maybe_remove_posts();

// Load the data that we'll use to fetch posts.
$this->load_post_types();
Expand Down Expand Up @@ -200,6 +200,27 @@ public function index_posts() {
return false;
}

/**
* Remove all post types, but only once, at the beginning of indexing process.
*/
private function maybe_remove_posts() {
if ( $this->indexing_data->get( 'status' ) === 'processing' ) {
return;
}

$types_removed = $this->api->remove_types();
$this->indexing_data->set( 'status', 'processing' );

if ( $types_removed !== Api_Status::$success ) {
$this->ajax_response_error( array(
'status' => $types_removed,
'message' => __( 'Deleting objects...', 'doofinder_for_wp' ),
) );
}

$this->indexing_data->set( 'post_types_removed', Settings::get_post_types_to_index() );
}

/**
* Load post types from DB, and set current post type if is not defined.
*
Expand Down Expand Up @@ -234,38 +255,13 @@ private function load_posts_ids() {

$this->post_count = count( $this->posts_ids );

$this->maybe_remove_current_post_type();

if ( $this->post_count === 0 ) {
if ( $this->check_next_post_type() ) {
$this->load_posts_ids();
}
}
}

/**
* We should remove posts from a given type when we begin indexing that type.
*
* This function checks if the post type currently being indexed was removed,
* and removes it it was not.
*/
private function maybe_remove_current_post_type() {
$post_type = $this->indexing_data->get( 'post_type' );
if ( $this->indexing_data->has( 'post_types_removed', $post_type ) ) {
return;
}

$removing_post_type = $this->api->remove_type( $post_type );
if ( $removing_post_type !== Api_Status::$success ) {
$this->ajax_response_error( array(
'status' => $removing_post_type,
'message' => __( "Deleting \"$post_type\" type...", 'doofinder_for_wp' ),
) );
}

$this->indexing_data->set( 'post_types_removed', $post_type );
}

/**
* Load posts from DB.
*
Expand Down Expand Up @@ -401,7 +397,7 @@ private function check_next_post_type() {
* @since 1.0.0
*
* @param bool $completed Status of indexing posts.
* @param string $message Additional message to pass to front.
* @param string $message Additional message to pass to front.
*/
private function ajax_response( $completed, $message = '' ) {
// We're about to call "die", so we need to make sure our data
Expand Down
7 changes: 7 additions & 0 deletions doofinder/includes/class-indexing-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@ public function set( $option_name, $value ) {
return;
}

// If both the option and value are arrays - merge.
if ( is_array( $this->data[ $option_name ] ) && is_array( $value ) ) {
$this->data[ $option_name ] = array_merge( $this->data[ $option_name ], $value );

return;
}

// If the option is an array - add the value.
if ( is_array( $this->data[ $option_name ] ) ) {
$this->data[ $option_name ][] = $value;
Expand Down
12 changes: 11 additions & 1 deletion doofinder/includes/class-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public function format_for_api() {
'id' => $this->post->ID,
'title' => $this->post->post_title,
'link' => get_the_permalink( $this->post ),
'post_date' => $this->post->post_date
'post_date' => $this->get_post_date()
);

// Post content.
Expand Down Expand Up @@ -351,6 +351,16 @@ public function get_meta_fields() {
return $meta;
}

/**
* Return the date of the post in the following format:
* 2018-09-18T15:27:00Z
*
* @return string
*/
public function get_post_date() {
return date( 'Y-m-d\TH:i:s\Z', strtotime( $this->post->post_date ) );
}

/**
* Prepare post content for the Doofinder API.
*
Expand Down
Loading

0 comments on commit c5f3be5

Please sign in to comment.