Skip to content

Commit

Permalink
Merge pull request #67 from alleyinteractive/feature/mapping-version
Browse files Browse the repository at this point in the history
Store/check mapping version
  • Loading branch information
mboynes authored Dec 12, 2016
2 parents 9c523f6 + 61fd7b4 commit 67b9319
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
25 changes: 25 additions & 0 deletions lib/class-sp-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,31 @@ public function admin_notices() {
);
}
echo '<div class="updated error">' . wpautop( $message_escaped ) . '</div>'; // WPCS: XSS ok.
} else {
$this->check_mapping_version();
}
}

/**
* If the mapping needs to be updated, alert the user about it.
*/
protected function check_mapping_version() {
if ( SP_Config()->get_setting( 'map_version' ) < apply_filters( 'sp_map_version', SP_MAP_VERSION ) ) {
if ( ! $this->is_settings_page() ) {
$link_escaped = sprintf(
' <a href="%s">%s</a>',
esc_url( admin_url( 'tools.php?page=searchpress#sp-sync' ) ),
esc_html__( 'Go to SearchPress Settings', 'searchpress' )
);
} else {
$link_escaped = '';
}

printf(
'<div class="updated error"><p>%1$s%2$s</p></div>', // WPCS: XSS ok.
esc_html__( 'SearchPress was updated and you need to reindex your content.', 'searchpress' ),
$link_escaped
);
}
}

Expand Down
39 changes: 35 additions & 4 deletions lib/class-sp-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,15 @@ public function sync_post_types() {
return $this->post_types;
}


/**
* Set the mappings.
*
* The map version is stored in sp_settings. When the map is updated, the
* version is bumped. When updates are made to the map, the site should be
* reindexed.
*
* @return mixed {@see SP_API::put()}.
*/
public function create_mapping() {
$mapping = array(
'settings' => array(
Expand Down Expand Up @@ -266,7 +274,29 @@ public function create_mapping() {
),
),
);

/**
* Filter the mappings. Plugins and themes can customize the mappings
* however they need by manipulating this array.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html
* @param array $mapping SearchPress' mapping array.
*/
$mapping = apply_filters( 'sp_config_mapping', $mapping );

/**
* Filter the map version. Plugins and themes can tweak this value if
* they update the mapping, and SearchPress will flag that the site
* needs to be reindexed.
*
* @param int $map_version Map version. Should be of the format
* `{year}{month}{day}{version}` where version
* is a two-digit sequential number.
*/
$this->update_settings( array(
'map_version' => apply_filters( 'sp_map_version', SP_MAP_VERSION ),
) );

return SP_API()->put( '', wp_json_encode( $mapping ) );
}

Expand All @@ -279,9 +309,10 @@ public function flush() {
public function get_settings() {
$settings = get_option( 'sp_settings' );
$this->settings = wp_parse_args( $settings, array(
'host' => 'http://localhost:9200',
'must_init' => true,
'active' => false,
'host' => 'http://localhost:9200',
'must_init' => true,
'active' => false,
'map_version' => 0,
) );
return $this->settings;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/globals.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* Miscellaneous Constants
*/


// The map version. Represents the last time the mappings were changed.
define( 'SP_MAP_VERSION', 2016112401 );

/**
* Error Codes
Expand Down
7 changes: 7 additions & 0 deletions tests/test-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,13 @@ public function test_admin_notices_heartbeat_shutdown() {
SP_Admin()->admin_notices();
}

public function test_admin_notices_map_version() {
$this->expectOutputRegex( '/SearchPress was updated and you need to reindex your content/' );
wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
SP_Config()->update_settings( array( 'map_version' => 0 ) );
SP_Admin()->admin_notices();
}

public function test_errors() {
$this->assertSame( 'SearchPress could not flush the old data', SP_Admin()->get_error( SP_ERROR_FLUSH_FAIL ) );
$this->assertSame( 'SearchPress cannot reach the Elasticsearch server', SP_Admin()->get_error( SP_ERROR_NO_BEAT ) );
Expand Down

0 comments on commit 67b9319

Please sign in to comment.