Skip to content
This repository has been archived by the owner on May 9, 2019. It is now read-only.

Commit

Permalink
Merge pull request #613 from algolia/feat/push-settings-button
Browse files Browse the repository at this point in the history
feat(admin): add buttons to re-push the settings
  • Loading branch information
rayrutjes authored Jun 8, 2017
2 parents b7e1fe5 + 32862c3 commit 29dd578
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 6 deletions.
24 changes: 24 additions & 0 deletions includes/admin/class-algolia-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __construct( Algolia_Plugin $plugin ) {
new Algolia_Admin_Page_Native_Search( $plugin );

add_action( 'wp_ajax_algolia_re_index', array( $this, 're_index' ) );
add_action( 'wp_ajax_algolia_push_settings', array( $this, 'push_settings' ) );

if ( isset( $_GET['page'] ) && substr( (string) $_GET['page'], 0, 7) === 'algolia' ) {
add_action( 'admin_notices', array( $this, 'display_reindexing_notices' ) );
Expand All @@ -43,6 +44,7 @@ public function enqueue_styles() {
public function enqueue_scripts() {
wp_enqueue_script( 'algolia-admin', plugin_dir_url( __FILE__ ) . 'js/algolia-admin.js', array( 'jquery', 'jquery-ui-sortable' ), ALGOLIA_VERSION );
wp_enqueue_script( 'algolia-admin-reindex-button', plugin_dir_url( __FILE__ ) . 'js/reindex-button.js', array( 'jquery' ), ALGOLIA_VERSION );
wp_enqueue_script( 'algolia-admin-push-settings-button', plugin_dir_url( __FILE__ ) . 'js/push-settings-button.js', array( 'jquery' ), ALGOLIA_VERSION );
}

/**
Expand Down Expand Up @@ -138,4 +140,26 @@ public function re_index() {
throw $exception;
}
}

public function push_settings() {
try {
if ( ! isset( $_POST['index_id'] ) ) {
throw new RuntimeException('index_id should be provided.');
}
$index_id = (string) $_POST['index_id'];

$index = $this->plugin->get_index($index_id);
if (null === $index) {
throw new RuntimeException(sprintf('Index named %s does not exist.', $index_id));
}

$index->push_settings();

$response = array( 'success' => true );
wp_send_json($response);
} catch (\Exception $exception) {
echo $exception->getMessage();
throw $exception;
}
}
}
53 changes: 53 additions & 0 deletions includes/admin/js/push-settings-button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
(function($) {

$(function() {
var $buttons = $('.algolia-push-settings-button');
$buttons.on('click', handleButtonClick);
});


function handleButtonClick(e) {
$clickedButton = $(e.currentTarget);
var index = $clickedButton.data('index');
if (!index) {
throw new Error('Clicked button has no "data-index" set.');
}

disableButton($clickedButton);

pushSettings($clickedButton, index);
}

function disableButton($button) {
$button.prop('disabled', true);
}

function enableButton($button) {
$button.prop('disabled', false);
}

function pushSettings($clickedButton, index) {

var data = {
'action': 'algolia_push_settings',
'index_id': index
};

$.post(ajaxurl, data, function(response) {
if(typeof response.success === 'undefined') {
alert('An error occurred');
enableButton($clickedButton);
return;
}

alert('Settings correctly pushed for index: ' + index);
enableButton($clickedButton);
}).fail(function(response) {
alert('An error occurred: ' + response.responseText);
enableButton($clickedButton);
});
}

})(jQuery);


9 changes: 5 additions & 4 deletions includes/admin/partials/page-autocomplete-config.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<table class="widefat table-autocomplete">
<thead>
<tr>
<th><?php _e( 'Re-order', 'algolia' ); ?></th>
<th><?php _e( 'Enable', 'algolia' ); ?></th>
<th style="width: 20px;"></th>
<th style="width: 75px;"><?php _e( 'Enable', 'algolia' ); ?></th>
<th><?php _e( 'Index', 'algolia' ); ?></th>
<th><?php _e( 'Label', 'algolia' ); ?></th>
<th><?php _e( 'Max. Suggestions', 'algolia' ); ?></th>
<th style="width: 75px;"><?php _e( 'Max. Suggestions', 'algolia' ); ?></th>
<th><?php _e( 'Actions', 'algolia' ); ?></th>
</tr>
</thead>
Expand All @@ -27,10 +27,11 @@
<input type="text" name="algolia_autocomplete_config[<?php echo esc_attr( $index['index_id'] ); ?>][label]" value="<?php echo esc_attr( $index['label'] ); ?>" />
</td>
<td>
<input type="number" name="algolia_autocomplete_config[<?php echo esc_attr( $index['index_id'] ); ?>][max_suggestions]" value="<?php echo (int) $index['max_suggestions']; ?>" />
<input style="width: 40px; text-align: center;" type="number" name="algolia_autocomplete_config[<?php echo esc_attr( $index['index_id'] ); ?>][max_suggestions]" value="<?php echo (int) $index['max_suggestions']; ?>" />
</td>
<td>
<button class="algolia-reindex-button button button-primary" data-index="<?php echo esc_attr( $index['index_id'] ); ?>"><?php _e('Re-index', 'algolia'); ?></button>
<button class="algolia-push-settings-button button" data-index="<?php echo esc_attr( $index['index_id'] ); ?>"><?php _e('Push Settings', 'algolia'); ?></button>
</td>
</tr>
<?php endforeach; ?>
Expand Down
3 changes: 2 additions & 1 deletion includes/admin/partials/page-search.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<div class="wrap">
<h1>
<?php echo esc_html(get_admin_page_title()); ?>
<button class="algolia-reindex-button button button-primary" data-index="searchable_posts">Re-index search page records.</button>
<button class="algolia-reindex-button button button-primary" data-index="searchable_posts"><?php _e('Re-index search page records', 'algolia'); ?></button>
<button class="algolia-push-settings-button button" data-index="searchable_posts"><?php _e('Push Settings', 'algolia'); ?></button>
</h1>
<form method="post" action="options.php">
<?php
Expand Down
9 changes: 8 additions & 1 deletion includes/indices/class-algolia-index.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,15 @@ public function create_index_if_not_existing( $clear_if_existing = true )
}
}

$this->push_settings();
}

public function push_settings() {
$index = $this->get_index();

// This will create the index if it does not exist.
$settings = $this->get_settings();
$index->setSettings( $settings ); // This will create the index.
$index->setSettings( $settings );

// Push synonyms.
$synonyms = $this->get_synonyms();
Expand Down

0 comments on commit 29dd578

Please sign in to comment.