diff --git a/frontend/ad-functions.php b/frontend/ad-functions.php
index 06d18ff99..384357687 100644
--- a/frontend/ad-functions.php
+++ b/frontend/ad-functions.php
@@ -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
', '$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.
@@ -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();
diff --git a/frontend/placeholders.php b/frontend/placeholders.php
index e1ebb64d5..cf4417815 100644
--- a/frontend/placeholders.php
+++ b/frontend/placeholders.php
@@ -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',
@@ -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 ) ) {
diff --git a/includes/helpers/class-listing-renderer.php b/includes/helpers/class-listing-renderer.php
index c89b2d2c8..be5f312a9 100644
--- a/includes/helpers/class-listing-renderer.php
+++ b/includes/helpers/class-listing-renderer.php
@@ -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
*/
diff --git a/includes/settings/class-display-settings.php b/includes/settings/class-display-settings.php
index 86d8b58d6..961ed18b1 100644
--- a/includes/settings/class-display-settings.php
+++ b/includes/settings/class-display-settings.php
@@ -99,7 +99,7 @@ private function register_layout_and_presentation_settings( $settings_manager )