studiopress / agentpress-listings

AgentPress Listings
http://wordpress.org/plugins/agentpress-listings/
GNU General Public License v2.0
9 stars 11 forks source link

_listing_price_sortable key removed on save in 1.3.2 #42

Closed nickcernis closed 5 years ago

nickcernis commented 5 years ago

The _listing_price_sortable key is being removed when saving a listing:

https://wordpress.org/support/topic/version-1-3-2-removing-meta_key-_listing_price_sortable/

Impact

New listings and edited listings may fail to appear in search results, archives, or other lists if _listing_price_sortable has been used in the query to select or order them.

The paid plugin Extended AgentPress Listings Widget apparently allows sorting by _listing_price_sortable, for example.

To reproduce

  1. git checkout 1.3.1
  2. Create a listing with a price.
  3. Confirm the saved listing has a _listing_price and _listing_price_sortable:
❯ wp post meta list 7488
+---------+-------------------------+--------------+
| post_id | meta_key                | meta_value   |
+---------+-------------------------+--------------+
| 7488    | _edit_last              | 1            |
| 7488    | _edit_lock              | 1562778936:1 |
| 7488    | _listing_price          | 12345        |
| 7488    | _listing_price_sortable | 12345        |
+---------+-------------------------+--------------+
  1. git checkout 1.3.2
  2. Edit the listing title and save the listing.

The _listing_price_sortable row will now be missing:

❯ wp post meta list 7488
+---------+----------------+--------------+
| post_id | meta_key       | meta_value   |
+---------+----------------+--------------+
| 7488    | _edit_last     | 1            |
| 7488    | _edit_lock     | 1562779081:1 |
| 7488    | _listing_price | 12345        |
+---------+----------------+--------------+

To fix

Fixed for me by changing these lines in agentpress-listings/includes/class-agentpress-listings.php:

-       if ( isset( $property_details['_listing_price'] ) && ! empty( $property_details['_listing_price'] ) ) {
+       if ( isset( $property_details[0]['_listing_price'] ) && ! empty( $property_details[0]['_listing_price'] ) ) {

-           $price_sortable = preg_replace( '/[^0-9\.]/', '', $property_details['_listing_price'] );
+           $price_sortable = preg_replace( '/[^0-9\.]/', '', $property_details[0]['_listing_price'] );

            update_post_meta( $post_id, '_listing_price_sortable', floatval( $price_sortable ) );
        } else {
            delete_post_meta( $post_id, '_listing_price_sortable' );
        }

New posts and posts that were edited would then have to be re-saved after this fix in order to restore the _listing_price_sortable.

Cause

The $property_details data structure changed here in 1.3.2:

https://github.com/studiopress/agentpress-listings/blob/cd47ad2c84fe71bd7abc600567bf7bfca6270b1e/includes/class-agentpress-listings.php#L219

And we missed that $property_details needed to become $property_details[0] at all points below that in the code, rather than just the for loop.