Skip to content
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
34 changes: 29 additions & 5 deletions frontend/ad-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,34 @@ function awpcp_ad_posted_user_email( $ad, $transaction = null, $message='' ) {
return $email;
}

/**
* Returns the layout used to render each listing in lists of listings.
*
* A saved layout identical to the default shipped before the
* $most_recent_start_date placeholder was added is treated as the default,
* so listings show the same date the date-based orders use.
*
* @since x.x
*
* @return string
*/
function awpcp_get_listings_layout() {
$layout = get_awpcp_option( 'displayadlayoutcode' );
$default_layout = (string) awpcp()->settings->get_option_default_value( 'displayadlayoutcode' );

if ( empty( $layout ) ) {
return $default_layout;
}

$previous_default_layout = str_replace( '$most_recent_start_date<br/>', '$awpcpadpostdate', $default_layout );

if ( preg_replace( '/\s+/', '', $layout ) === preg_replace( '/\s+/', '', $previous_default_layout ) ) {
return $default_layout;
}

return $layout;
}

/**
* Renders each listing using the layout configured in the plugin
* settings.
Expand All @@ -206,11 +234,7 @@ function awpcp_ad_posted_user_email( $ad, $transaction = null, $message='' ) {
*/
function awpcp_render_listings_items( $listings, $context, $options = array() ) {
$parity = array( 'displayaditemseven', 'displayaditemsodd' );
$layout = get_awpcp_option('displayadlayoutcode');

if ( empty( $layout ) ) {
$layout = awpcp()->settings->get_option_default_value( 'displayadlayoutcode' );
}
$layout = awpcp_get_listings_layout();

$listing_renderer = awpcp_listing_renderer();

Expand Down
5 changes: 5 additions & 0 deletions frontend/placeholders.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ function awpcp_content_placeholders() {
'renewed_date' => array(
'callback' => 'awpcp_do_placeholder_dates',
),
'most_recent_start_date' => array(
'callback' => 'awpcp_do_placeholder_dates',
),

'featured_image' => array(
'callback' => 'awpcp_do_placeholder_images',
Expand Down Expand Up @@ -543,6 +546,8 @@ function awpcp_do_placeholder_dates( $ad, $placeholder ) {
$replacements['posted_date_time'] = $listing_renderer->get_posted_date_and_time_formatted( $ad );
$replacements['last_updated_date'] = $listing_renderer->get_last_updated_date_formatted( $ad );

$replacements['most_recent_start_date'] = $listing_renderer->get_most_recent_start_date_formatted( $ad );

$verification_date = $listing_renderer->get_verification_date( $ad );

if ( ! empty( $verification_date ) ) {
Expand Down
25 changes: 25 additions & 0 deletions includes/helpers/class-listing-renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,31 @@ public function get_posted_date_formatted( $listing ) {
return $this->get_formatted_date( $listing->post_date );
}

/**
* Returns the date used by the date-based listing orders: the renewed
* date if the listing was renewed after it started, or its start date.
*
* @since x.x
*/
public function get_most_recent_start_date_formatted( $listing ) {
$date = $this->wordpress->get_post_meta( $listing->ID, '_awpcp_most_recent_start_date', true );

if ( empty( $date ) ) {
$date = $this->get_plain_start_date( $listing );
$renewed_date = $this->get_renewed_date( $listing );

if ( $renewed_date && strtotime( $renewed_date ) > strtotime( (string) $date ) ) {
$date = $renewed_date;
}
}

if ( empty( $date ) ) {
$date = $listing->post_date;
}

return $this->get_formatted_date( $date );
}

/**
* @since 4.0.0
*/
Expand Down
2 changes: 1 addition & 1 deletion includes/settings/class-display-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private function register_layout_and_presentation_settings( $settings_manager )
<div class="awpcp-listing-excerpt-content">$excerpt</div>
</div>
<div class="awpcp-listing-excerpt-extra">
$awpcpadpostdate
$most_recent_start_date<br/>
$awpcp_city_display
$awpcp_state_display
$awpcp_display_adviews
Expand Down
28 changes: 28 additions & 0 deletions tests/suite/frontend/test-placeholders.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,34 @@ public function test_renewed_date_placeholder_returns_posted_date_if_listing_has
$this->assertEquals( $expected_value, awpcp_do_placeholders( $ad, '$renewed_date', 'listings' ) );
}

public function test_most_recent_start_date_placeholder() {
awpcp()->settings->set_or_update_option( 'date-format', 'F d, Y' );

$ad = awpcp_tests_create_listing();
$ad->post_date = '2013-10-24 12:34:56';

update_post_meta( $ad->ID, '_awpcp_start_date', '2013-10-24 12:34:56' );
update_post_meta( $ad->ID, '_awpcp_most_recent_start_date', '2013-11-05 08:00:00' );

$this->assertEquals( 'November 05, 2013', awpcp_do_placeholders( $ad, '$most_recent_start_date', 'listings' ) );
}

public function test_most_recent_start_date_placeholder_falls_back_to_start_date_and_posted_date() {
awpcp()->settings->set_or_update_option( 'date-format', 'F d, Y' );

$ad = awpcp_tests_create_listing();
$ad->post_date = '2013-10-24 12:34:56';

delete_post_meta( $ad->ID, '_awpcp_most_recent_start_date' );
update_post_meta( $ad->ID, '_awpcp_start_date', '2013-10-25 12:34:56' );

$this->assertEquals( 'October 25, 2013', awpcp_do_placeholders( $ad, '$most_recent_start_date', 'listings' ) );

delete_post_meta( $ad->ID, '_awpcp_start_date' );

$this->assertEquals( 'October 24, 2013', awpcp_do_placeholders( $ad, '$most_recent_start_date', 'listings' ) );
}

public function test_price_placeholder() {
awpcp()->settings->set_or_update_option( 'displaypricefield', true );
awpcp()->settings->set_or_update_option( 'price-field-is-restricted', false );
Expand Down
Loading