Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finishing the test coverage for everything that can reasonably be tested #7

Merged
merged 22 commits into from
Sep 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Changelog
* Adds helper functions
* Adds support for ES 1.0+ *(breaking change)*
* Refactors search *(breaking change)*
* Removes SP_Config::unserialize_meta()


### 0.1
Expand Down
38 changes: 20 additions & 18 deletions lib/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,46 +53,48 @@ public function admin_menu() {


public function sync() {
if ( !current_user_can( 'manage_options' ) ) wp_die( __( 'You do not have sufficient permissions to access this page.', 'searchpress' ) );
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.', 'searchpress' ) );
}
$sync = SP_Sync_Meta();
?>
<div class="wrap">
<h2>SearchPress</h2>
<h2><?php esc_html_e( 'SearchPress', 'searchpress' ); ?></h2>

<?php if ( isset( $_GET['error'] ) ) : ?>
<div class="error updated"><p><?php printf( __( 'An error has occurred: %s', 'searchpress' ), esc_html( $this->get_error( $_GET['error'] ) ) ) ?></p></div>
<div class="error updated"><p><?php esc_html( sprintf( __( 'An error has occurred: %s', 'searchpress' ), $this->get_error( $_GET['error'] ) ) ) ?></p></div>
<?php endif ?>

<?php if ( isset( $_GET['complete'] ) ) : ?>
<div class="updated success"><p><?php _e( 'Sync complete!', 'searchpress' ); ?></p></div>
<div class="updated success"><p><?php esc_html_e( 'Sync complete!', 'searchpress' ); ?></p></div>
<?php endif ?>

<h3>Settings</h3>
<h3><?php esc_html_e( 'Settings', 'searchpress' ); ?></h3>
<form method="post" action="<?php echo admin_url( 'admin-post.php' ) ?>">
<input type="hidden" name="action" value="sp_settings" />
<?php wp_nonce_field( 'sp_settings', 'sp_settings_nonce' ); ?>
<p>
<input type="text" name="sp_host" value="<?php echo esc_url( SP_Config()->get_setting( 'host' ) ) ?>" style="width:100%;max-width:500px" />
</p>
<p>
<label for="sp_reindex"><input type="checkbox" name="sp_reindex" id="sp_reindex" value="1" /> <?php _e( 'Immediately initiate a full sync', 'searchpress' ); ?>
<label for="sp_reindex"><input type="checkbox" name="sp_reindex" id="sp_reindex" value="1" /> <?php esc_html_e( 'Immediately initiate a full sync', 'searchpress' ); ?>
</p>
<?php submit_button( 'Save Settings', 'primary' ) ?>
<?php submit_button( __( 'Save Settings', 'searchpress' ), 'primary' ) ?>
</form>

<hr />

<?php if ( $sync->running ) : ?>

<h3><?php _e( 'Sync in progress', 'searchpress' ); ?></h3>
<p><?php _e( 'You do not need to stay on this page while the sync runs.', 'searchpress' ); ?></p>
<h3><?php esc_html_e( 'Sync in progress', 'searchpress' ); ?></h3>
<p><?php esc_html_e( 'You do not need to stay on this page while the sync runs.', 'searchpress' ); ?></p>
<div class="progress">
<div class="progress-text"><span id="sync-processed"><?php echo number_format( intval( $sync->processed ) ) ?></span> / <span id="sync-total"><?php echo number_format( intval( $sync->total ) ) ?></span></div>
<div class="progress-bar" data-processed="<?php echo intval( $sync->processed ) ?>" data-total="<?php echo intval( $sync->total ) ?>" style="width:<?php echo intval( round( 100 * $sync->processed / $sync->total ) ) ?>%;"></div>
</div>
<script type="text/javascript">
var progress_total, progress_processed;
var sp_url = '<?php echo admin_url( 'tools.php?page=searchpress_sync&complete=1' ) ?>';
var sp_url = '<?php echo esc_url( admin_url( "tools.php?page=searchpress_sync&complete=1" ) ) ?>';
jQuery( function( $ ) {
progress_total = $( '.progress-bar' ).data( 'total' ) - 0;;
progress_processed = $( '.progress-bar' ).data( 'processed' ) - 0;
Expand All @@ -116,26 +118,26 @@ public function sync() {
<form method="post" action="<?php echo admin_url( 'admin-post.php' ) ?>">
<input type="hidden" name="action" value="sp_cancel_sync" />
<?php wp_nonce_field( 'sp_sync', 'sp_sync_nonce' ); ?>
<?php submit_button( 'Cancel Sync', 'delete' ) ?>
<?php submit_button( __( 'Cancel Sync', 'searchpress' ), 'delete' ) ?>
</form>

<?php else : ?>

<h3><?php _e( 'Full Sync', 'searchpress' ); ?></h3>
<p><?php _e( 'Running a full sync will wipe the current index if there is one and rebuild it from scratch.', 'searchpress' ); ?></p>
<h3><?php esc_html_e( 'Full Sync', 'searchpress' ); ?></h3>
<p><?php esc_html_e( 'Running a full sync will wipe the current index if there is one and rebuild it from scratch.', 'searchpress' ); ?></p>
<p>
<?php printf( _n( 'Your site has %s post to index.', 'Your site has %s posts to index.', intval( SP_Sync_Manager()->count_posts() ), 'searchpress' ), number_format( intval( SP_Sync_Manager()->count_posts() ) ) ) ?>
<?php echo esc_html( sprintf( _n( 'Your site has %s post to index.', 'Your site has %s posts to index.', intval( SP_Sync_Manager()->count_posts() ), 'searchpress' ), number_format( intval( SP_Sync_Manager()->count_posts() ) ) ) ) ?>
<?php if ( SP_Sync_Manager()->count_posts() > 25000 ) : ?>
<?php _e( 'As a result of there being so many posts, this may take a long time to index.', 'searchpress' ); ?>
<?php esc_html_e( 'As a result of there being so many posts, this may take a long time to index.', 'searchpress' ); ?>
<?php endif ?>
<?php _e( "Exactly how long this will take will vary on a number of factors, like your server's CPU and memory, connection speed, current traffic, average post length, and associated terms and post meta.", 'searchpress' ); ?>
<?php esc_html_e( "Exactly how long this will take will vary on a number of factors, like your server's CPU and memory, connection speed, current traffic, average post length, and associated terms and post meta.", 'searchpress' ); ?>
</p>
<p><?php _e( 'Your site will not use SearchPress until the indexing is complete.', 'searchpress' ); ?></p>
<p><?php esc_html_e( 'Your site will not use SearchPress until the indexing is complete.', 'searchpress' ); ?></p>

<form method="post" action="<?php echo admin_url( 'admin-post.php' ) ?>">
<input type="hidden" name="action" value="sp_full_sync" />
<?php wp_nonce_field( 'sp_sync', 'sp_sync_nonce' ); ?>
<?php submit_button( 'Run Full Sync', 'delete' ) ?>
<?php submit_button( __( 'Run Full Sync', 'searchpress' ), 'delete' ) ?>
</form>

<?php endif ?>
Expand Down
11 changes: 0 additions & 11 deletions lib/class-sp-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,6 @@ public function flush() {
}


/**
* Should we attempt to unserialize post meta?
*
* @return bool
* @todo implement this as a site option
*/
public function unserialize_meta() {
return apply_filters( 'sp_config_unserialize_meta', false );
}


public function get_settings() {
$settings = get_option( 'sp_settings' );
$this->settings = wp_parse_args( $settings, array(
Expand Down
7 changes: 5 additions & 2 deletions lib/class-sp-cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ public function setup() {
public function reindex() {
do_action( 'sp_debug', '[SP Cron] Reindexing' );
SP_Sync_Manager()->do_index_loop();
if ( SP_Sync_Meta()->running )
if ( SP_Sync_Meta()->running ) {
$this->schedule_reindex();
}
}

public function schedule_reindex() {
Expand All @@ -61,7 +62,9 @@ public function cancel_reindex() {
function SP_Cron() {
return SP_Cron::instance();
}
if ( defined( 'DOING_CRON' ) && DOING_CRON )

if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
SP_Cron();
}

endif;
13 changes: 5 additions & 8 deletions lib/class-sp-integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,12 @@ public function filter__posts_request( $sql, &$query ) {

// Convert the WP-style args into ES args
$this->search_obj = new SP_WP_Search( $es_wp_query_args );
$results = $this->search_obj->get_results();
$results = $this->search_obj->get_results( 'hits' );

// Total number of results for paging purposes
$this->found_posts = $this->search_obj->get_results( 'total' );

if ( empty( $results ) ) {
$this->found_posts = 0;
return "SELECT * FROM {$wpdb->posts} WHERE 1=0 /* SearchPress search results */";
}

Expand All @@ -189,9 +191,6 @@ public function filter__posts_request( $sql, &$query ) {
$post_ids = array_map( 'absint', $post_ids );
$post_ids = array_filter( $post_ids );

// Total number of results for paging purposes
$this->found_posts = $this->search_obj->get_results( 'total' );

// Replace the search SQL with one that fetches the exact posts we want in the order we want
$post_ids_string = implode( ',', $post_ids );
return "SELECT * FROM {$wpdb->posts} WHERE {$wpdb->posts}.ID IN( {$post_ids_string} ) ORDER BY FIELD( {$wpdb->posts}.ID, {$post_ids_string} ) /* SearchPress search results */";
Expand Down Expand Up @@ -277,9 +276,7 @@ protected function build_es_request( $query ) {
}

if ( ! empty( $es_wp_query_args['date_range'] ) && empty( $es_wp_query_args['date_range']['field'] ) ) {
$es_wp_query_args['date_range']['field'] = 'post_date.date';
} elseif ( ! empty( $es_wp_query_args['date_range']['field'] ) ) {
$es_wp_query_args['date_range']['field'] .= '.date';
$es_wp_query_args['date_range']['field'] = 'post_date';
}


Expand Down
28 changes: 14 additions & 14 deletions lib/class-sp-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ class SP_Post {
* @return void
*/
function __construct( $post ) {
if ( is_numeric( $post ) && 0 != intval( $post ) )
if ( is_numeric( $post ) && 0 != intval( $post ) ) {
$post = get_post( intval( $post ) );
if ( ! is_object( $post ) )
}
if ( ! is_object( $post ) ) {
return;
}

$this->fill( $post );
}
Expand Down Expand Up @@ -48,8 +50,9 @@ public function __set( $property, $value ) {
*/
public function __get( $property ) {
# let the post ID be accessed either way
if ( 'ID' == $property )
if ( 'ID' == $property ) {
$property = 'post_id';
}

return isset( $this->data[ $property ] ) ? $this->data[ $property ] : null;
}
Expand Down Expand Up @@ -125,14 +128,6 @@ public static function get_meta( $post_id ) {
$values = array_map( array( 'SP_Post', 'cast_meta_types' ), $values );
}

if ( SP_Config()->unserialize_meta() ) {
# If post meta is serialized, unserialize it
foreach ( $meta as $key => &$values ) {
$values = array_map( 'maybe_unserialize', $values );
}
$values = apply_filters( 'sp_unserialize_meta', $values, $key );
}

do_action( 'sp_debug', '[SP_Post] Compiled Meta', $meta );
return $meta;
}
Expand Down Expand Up @@ -182,8 +177,9 @@ public static function cast_meta_types( $value ) {
* @return array
*/
public static function get_terms( $post ) {
if ( defined( 'WP_CLI' ) && WP_CLI )
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return self::get_terms_efficiently( $post );
}

$object_terms = array();
$taxonomies = get_object_taxonomies( $post->post_type );
Expand Down Expand Up @@ -216,6 +212,8 @@ public static function get_terms( $post ) {
/**
* Does the same thing as get_terms but in 1 query instead of <num of taxonomies> + 1
*
* @codeCoverageIgnore
*
* @param object $post
* @return object
*/
Expand All @@ -228,8 +226,9 @@ private static function get_terms_efficiently( $post ) {
$query = "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id = $post_id ORDER BY t.term_id";

$object_terms = $wpdb->get_results( $query );
if ( !$object_terms || is_wp_error( $object_terms ) )
if ( !$object_terms || is_wp_error( $object_terms ) ) {
return array();
}

$terms = array();
foreach ( (array) $object_terms as $term ) {
Expand All @@ -253,8 +252,9 @@ private static function get_terms_efficiently( $post ) {
* @return array
*/
public function get_user( $user_id ) {
if ( ! empty( SP_Sync_Manager()->users[ $user_id ] ) )
if ( ! empty( SP_Sync_Manager()->users[ $user_id ] ) ) {
return SP_Sync_Manager()->users[ $user_id ];
}

$user = get_userdata( $user_id );
if ( $user instanceof WP_User ) {
Expand Down
22 changes: 0 additions & 22 deletions lib/class-sp-sync-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,6 @@ public function delete_post( $post_id ) {
}


/**
* Run the sync process
*
* @param int $start
* @param int $limit
* @return void
*/
public function sync( $start, $limit ) {
if ( false !== ( $previous_sync = get_transient( 'sp_sync' ) ) ) {
# Sync is running, or died. Do something about it.
return $previous_sync;
}

set_transient( 'sp_sync', array( 'start' => $start, 'limit' => $limit ), HOUR_IN_SECONDS );

$data = $this->get_range( $start, $limit );
# Do something with $data

delete_transient( 'sp_sync' );
}


/**
* Get all the posts in a given range
*
Expand Down
15 changes: 10 additions & 5 deletions lib/class-sp-sync-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ public static function instance() {
public function setup() {
$this->init();

if ( defined( 'WP_CLI' ) && WP_CLI )
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}

if ( false != ( $sync_meta = get_option( 'sp_sync_meta' ) ) ) {
foreach ( $sync_meta as $key => $value ) {
Expand Down Expand Up @@ -86,8 +87,9 @@ public function start() {


public function save( $force = false ) {
if ( ! 'force' == $force && defined( 'WP_CLI' ) && WP_CLI )
if ( 'force' != $force && defined( 'WP_CLI' ) && WP_CLI ) {
return;
}

$this->delete();
add_option( 'sp_sync_meta', array(
Expand All @@ -105,18 +107,21 @@ public function save( $force = false ) {


public function delete( $do_init = false, $force = false ) {
if ( ! 'force' == $force && defined( 'WP_CLI' ) && WP_CLI )
if ( ! 'force' == $force && defined( 'WP_CLI' ) && WP_CLI ) {
return;
}

delete_option( 'sp_sync_meta' );
if ( 'init' == $do_init )
if ( 'init' == $do_init ) {
$this->init();
}
}


public function reload() {
if ( defined( 'WP_CLI' ) && WP_CLI )
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}

wp_cache_delete( 'sp_sync_meta', 'options' );
$this->setup();
Expand Down
20 changes: 6 additions & 14 deletions searchpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,12 @@
*/


/**
* The list
*
* @todo Implement a heartbeat and only use ES if the heartbeat is active
* @todo If search url changes, need to prompt to reindex
* @todo faceting widget
* @todo related posts widget
*/


if ( !defined( 'SP_PLUGIN_URL' ) )
define( 'SP_PLUGIN_URL', plugins_url( '/', __FILE__ ) );
if ( !defined( 'SP_PLUGIN_DIR' ) )
define( 'SP_PLUGIN_DIR', __DIR__ );
if ( !defined( 'SP_PLUGIN_URL' ) ) {
define( 'SP_PLUGIN_URL', plugins_url( '/', __FILE__ ) );
}
if ( !defined( 'SP_PLUGIN_DIR' ) ) {
define( 'SP_PLUGIN_DIR', __DIR__ );
}

# Constants, etc.
require_once SP_PLUGIN_DIR . '/lib/globals.php';
Expand Down
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function sp_manually_load_plugin() {
'last_beat' => false
) );
require dirname( __FILE__ ) . '/../searchpress.php';

SP_API()->index = 'searchpress-tests';

// Make sure ES is running and responding
Expand Down
Loading