Skip to content

Commit

Permalink
Merge pull request #2214 from 10up/refactor/pr-1958
Browse files Browse the repository at this point in the history
Updated prepare_date_terms to only call date_i18n once
  • Loading branch information
felipeelia authored May 27, 2021
2 parents 0940eec + 20e5fcf commit c6a260e
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 18 deletions.
47 changes: 29 additions & 18 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,25 +336,36 @@ public function prepare_document( $post_id ) {
/**
* Prepare date terms to send to ES.
*
* @param string $post_date_gmt Post date
* @param string $date_to_prepare Post date
* @since 0.1.4
* @return array
*/
private function prepare_date_terms( $post_date_gmt ) {
$timestamp = strtotime( $post_date_gmt );
$date_terms = array(
'year' => (int) date_i18n( 'Y', $timestamp ),
'month' => (int) date_i18n( 'm', $timestamp ),
'week' => (int) date_i18n( 'W', $timestamp ),
'dayofyear' => (int) date_i18n( 'z', $timestamp ),
'day' => (int) date_i18n( 'd', $timestamp ),
'dayofweek' => (int) date_i18n( 'w', $timestamp ),
'dayofweek_iso' => (int) date_i18n( 'N', $timestamp ),
'hour' => (int) date_i18n( 'H', $timestamp ),
'minute' => (int) date_i18n( 'i', $timestamp ),
'second' => (int) date_i18n( 's', $timestamp ),
'm' => (int) ( date_i18n( 'Y', $timestamp ) . date_i18n( 'm', $timestamp ) ), // yearmonth
);
public function prepare_date_terms( $date_to_prepare ) {
$terms_to_prepare = [
'year' => 'Y',
'month' => 'm',
'week' => 'W',
'dayofyear' => 'z',
'day' => 'd',
'dayofweek' => 'w',
'dayofweek_iso' => 'N',
'hour' => 'H',
'minute' => 'i',
'second' => 's',
'm' => 'Ym', // yearmonth
];

// Combine all the date term formats and perform one single call to date_i18n() for performance.
$date_format = implode( '||', array_values( $terms_to_prepare ) );
$combined_dates = explode( '||', date_i18n( $date_format, strtotime( $date_to_prepare ) ) );

// Then split up the results for individual indexing.
$date_terms = [];
foreach ( $terms_to_prepare as $term_name => $date_format ) {
$index_in_combined_format = array_search( $term_name, array_keys( $terms_to_prepare ), true );
$date_terms[ $term_name ] = (int) $combined_dates[ $index_in_combined_format ];
}

return $date_terms;
}

Expand Down Expand Up @@ -430,7 +441,7 @@ private function prepare_terms( $post ) {
'term_order' => (int) $this->get_term_order( $term->term_taxonomy_id, $post->ID ),
);

$terms_dic[ $term->term_id ]['facet'] = json_encode( $terms_dic[ $term->term_id ] );
$terms_dic[ $term->term_id ]['facet'] = wp_json_encode( $terms_dic[ $term->term_id ] );

if ( $allow_hierarchy ) {
$terms_dic = $this->get_parent_terms( $terms_dic, $term, $taxonomy->name, $post->ID );
Expand Down Expand Up @@ -468,7 +479,7 @@ private function get_parent_terms( $terms, $term, $tax_name, $object_id ) {
'term_order' => $this->get_term_order( $parent_term->term_taxonomy_id, $object_id ),
);

$terms[ $parent_term->term_id ]['facet'] = json_encode( $terms[ $parent_term->term_id ] );
$terms[ $parent_term->term_id ]['facet'] = wp_json_encode( $terms[ $parent_term->term_id ] );

}
return $this->get_parent_terms( $terms, $parent_term, $tax_name, $object_id );
Expand Down
63 changes: 63 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -5916,4 +5916,67 @@ public function testPostSyncQueuePermissions() {

$this->assertEmpty( $document );
}

/**
* Test prepare_date_terms function
*
* @return void
* @group post
*/
public function testPostPrepareDateTerms() {
$date = new \DateTime('2021-04-11 23:58:12');

$return_prepare_date_terms = ElasticPress\Indexables::factory()->get( 'post' )->prepare_date_terms( $date->format( 'Y-m-d H:i:s' ) );

$this->assertIsArray( $return_prepare_date_terms );

$this->assertArrayHasKey( 'year', $return_prepare_date_terms );
$this->assertEquals( $date->format('Y'), $return_prepare_date_terms['year'] );

$this->assertArrayHasKey( 'month', $return_prepare_date_terms );
$this->assertEquals( $date->format('m'), $return_prepare_date_terms['month'] );

$this->assertArrayHasKey( 'week', $return_prepare_date_terms );
$this->assertEquals( $date->format('W'), $return_prepare_date_terms['week'] );

$this->assertArrayHasKey( 'dayofyear', $return_prepare_date_terms );
$this->assertEquals( $date->format('z'), $return_prepare_date_terms['dayofyear'] );

$this->assertArrayHasKey( 'day', $return_prepare_date_terms );
$this->assertEquals( $date->format('d'), $return_prepare_date_terms['day'] );

$this->assertArrayHasKey( 'dayofweek', $return_prepare_date_terms );
$this->assertEquals( $date->format('w'), $return_prepare_date_terms['dayofweek'] );

$this->assertArrayHasKey( 'dayofweek_iso', $return_prepare_date_terms );
$this->assertEquals( $date->format('N'), $return_prepare_date_terms['dayofweek_iso'] );

$this->assertArrayHasKey( 'hour', $return_prepare_date_terms );
$this->assertEquals( $date->format('H'), $return_prepare_date_terms['hour'] );

$this->assertArrayHasKey( 'minute', $return_prepare_date_terms );
$this->assertEquals( $date->format('i'), $return_prepare_date_terms['minute'] );

$this->assertArrayHasKey( 'second', $return_prepare_date_terms );
$this->assertEquals( $date->format('s'), $return_prepare_date_terms['second'] );

$this->assertArrayHasKey( 'm', $return_prepare_date_terms );
$this->assertEquals( $date->format('Ym'), $return_prepare_date_terms['m'] );

$return_prepare_date_terms = ElasticPress\Indexables::factory()->get( 'post' )->prepare_date_terms('');

$this->assertIsArray($return_prepare_date_terms );

$this->assertArrayHasKey( 'year', $return_prepare_date_terms );
$this->assertArrayHasKey( 'month', $return_prepare_date_terms );
$this->assertArrayHasKey( 'week', $return_prepare_date_terms );
$this->assertArrayHasKey( 'dayofyear', $return_prepare_date_terms );
$this->assertArrayHasKey( 'day', $return_prepare_date_terms );
$this->assertArrayHasKey( 'dayofweek', $return_prepare_date_terms );
$this->assertArrayHasKey( 'dayofweek_iso', $return_prepare_date_terms );
$this->assertArrayHasKey( 'hour', $return_prepare_date_terms );
$this->assertArrayHasKey( 'minute', $return_prepare_date_terms );
$this->assertArrayHasKey( 'second', $return_prepare_date_terms );
$this->assertArrayHasKey( 'm', $return_prepare_date_terms );
}
}

0 comments on commit c6a260e

Please sign in to comment.