Update expired properties.

This commit is contained in:
Hoang Huu 2019-11-04 08:28:41 +07:00
parent 09e5f5adfc
commit 943d5e20e4
5 changed files with 252 additions and 108 deletions

View File

@ -393,6 +393,33 @@ function opalestate_cleanup() {
}
}
wp_reset_postdata();
// Change status expired properties.
$expired_query = new WP_Query( [
'post_type' => 'opalestate_property',
'post_status' => [ 'pending', 'publish' ],
'meta_query' => [
[
'key' => OPALESTATE_PROPERTY_PREFIX . 'expired_time',
'value' => time(),
'compare' => '>=',
'type' => 'NUMERIC',
],
],
] );
if ( $expired_query->have_posts() ) {
while ( $expired_query->have_posts() ) {
$expired_query->the_post();
wp_update_post( [
'ID' => get_the_ID(),
'post_status' => 'expired',
] );
}
}
wp_reset_postdata();
}
/**

View File

@ -72,7 +72,7 @@ class Opalestate_Cache {
self::$instance->is_cache = ( defined( 'GIVE_CACHE' ) ? GIVE_CACHE : opalestate_is_setting_enabled( opalestate_get_option( 'cache', 'enabled' ) ) ) && is_admin();
// weekly delete all expired cache.
Give_Cron::add_weekly_event( array( $this, 'delete_all_expired' ) );
// Give_Cron::add_weekly_event( array( $this, 'delete_all_expired' ) );
add_action( 'save_post_opalestate_forms', array( $this, 'delete_form_related_cache' ) );
add_action( 'save_post_opalestate_payment', array( $this, 'delete_payment_related_cache' ) );

View File

@ -27,6 +27,7 @@ class Opalestate_PostType_Property {
*/
public function __construct() {
add_action( 'init', [ __CLASS__, 'definition' ] );
add_action( 'init', [ __CLASS__, 'register_post_status' ] );
}
/**
@ -74,6 +75,21 @@ class Opalestate_PostType_Property {
] )
);
}
/**
* Register post status.
*/
public static function register_post_status() {
register_post_status( 'expired', [
'label' => _x( 'Expired', 'Expired status', 'opalestate-pro' ),
'public' => false,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
/* translators: %s: number of orders */
'label_count' => _n_noop( 'Expired <span class="count">(%s)</span>', 'Expired <span class="count">(%s)</span>', 'opalestate-pro' ),
] );
}
}
new Opalestate_PostType_Property();

View File

@ -13,124 +13,125 @@
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
exit; // Exit if accessed directly
}
/**
* Count Number of listing following user
*/
function opalesate_get_user_current_listings( $user_id ){
$args = array(
'post_type' => 'opalestate_property',
'post_status' => array( 'pending', 'publish' ),
'author' => $user_id,
function opalesate_get_user_current_listings( $user_id ) {
$args = [
'post_type' => 'opalestate_property',
'post_status' => [ 'pending', 'publish' ],
'author' => $user_id,
);
$posts = new WP_Query( $args );
return $posts->found_posts;
wp_reset_postdata();
];
$posts = new WP_Query( $args );
return $posts->found_posts;
wp_reset_postdata();
}
/**
* Count Number of featured listing following user
*/
function opalesate_get_user_current_featured_listings( $user_id ){
function opalesate_get_user_current_featured_listings( $user_id ) {
$args = array(
'post_type' => 'opalestate_property',
'post_status' => array( 'pending', 'publish' ),
'author' => $user_id,
'meta_query' => array(
array(
'key' => OPALESTATE_PROPERTY_PREFIX.'featured',
'value' => 1,
'meta_compare '=>'='
)
)
);
$posts = new WP_Query( $args );
return $posts->found_posts;
wp_reset_postdata();
$args = [
'post_type' => 'opalestate_property',
'post_status' => [ 'pending', 'publish' ],
'author' => $user_id,
'meta_query' => [
[
'key' => OPALESTATE_PROPERTY_PREFIX . 'featured',
'value' => 1,
'meta_compare ' => '=',
],
],
];
$posts = new WP_Query( $args );
return $posts->found_posts;
wp_reset_postdata();
}
/**
* Check current package is downgrade package or not via current number of featured, listing lesser
*/
function opalesate_check_package_downgrade_status( $user_id, $package_id ){
function opalesate_check_package_downgrade_status( $user_id, $package_id ) {
$pack_listings = get_post_meta( $package_id, OPALMEMBERSHIP_PACKAGES_PREFIX.'package_listings', true );
$pack_featured_listings = get_post_meta( $package_id, OPALMEMBERSHIP_PACKAGES_PREFIX.'package_featured_listings', true );
$is_unlimited = get_post_meta( $package_id, OPALMEMBERSHIP_PACKAGES_PREFIX.'unlimited_listings', true );
$pack_unlimited_listings = $is_unlimited == 'on' ? 0 : 1;
$pack_listings = get_post_meta( $package_id, OPALMEMBERSHIP_PACKAGES_PREFIX . 'package_listings', true );
$pack_featured_listings = get_post_meta( $package_id, OPALMEMBERSHIP_PACKAGES_PREFIX . 'package_featured_listings', true );
$is_unlimited = get_post_meta( $package_id, OPALMEMBERSHIP_PACKAGES_PREFIX . 'unlimited_listings', true );
$pack_unlimited_listings = $is_unlimited == 'on' ? 0 : 1;
$user_current_listings = opalesate_get_user_current_listings( $user_id );
$user_current_featured_listings = opalesate_get_user_current_featured_listings( $user_id );
$user_current_listings = opalesate_get_user_current_listings( $user_id );
$user_current_featured_listings = opalesate_get_user_current_featured_listings( $user_id );
$current_listings = get_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_.'package_listings',true);
$current_listings = get_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'package_listings', true );
if( $pack_unlimited_listings == 1 ) {
return false;
}
if ( $pack_unlimited_listings == 1 ) {
return false;
}
// if is unlimited and go to non unlimited pack
if ( $current_listings == -1 && $pack_unlimited_listings != 1 ) {
return true;
}
// if is unlimited and go to non unlimited pack
if ( $current_listings == -1 && $pack_unlimited_listings != 1 ) {
return true;
}
return ( $user_current_listings > $pack_listings ) || ( $user_current_featured_listings > $pack_featured_listings ) ;
return ( $user_current_listings > $pack_listings ) || ( $user_current_featured_listings > $pack_featured_listings );
}
/**
* Check Current User having permission to add new property or not?
*/
function opalesate_check_has_add_listing( $user_id, $package_id=null ){
function opalesate_check_has_add_listing( $user_id, $package_id = null ) {
if( !$package_id ){
$package_id = (int)get_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_.'package_id', true );
}
if ( ! $package_id ) {
$package_id = (int) get_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'package_id', true );
}
$package_listings = (int) get_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_.'package_listings', true );
$package_listings = (int) get_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'package_listings', true );
$unlimited_listings = get_post_meta( $package_id, OPALMEMBERSHIP_PACKAGES_PREFIX.'unlimited_listings', true );
$unlimited_listings = !empty( $unlimited_listings ) && $unlimited_listings == 'on' ? 0 : 1;
$unlimited_listings = get_post_meta( $package_id, OPALMEMBERSHIP_PACKAGES_PREFIX . 'unlimited_listings', true );
if( $package_id > 0 && $unlimited_listings ){
return true;
}
$unlimited_listings = ! empty( $unlimited_listings ) && $unlimited_listings == 'on' ? 0 : 1;
if( $package_listings > 0 ){
return true;
}
return false;
if ( $package_id > 0 && $unlimited_listings ) {
return true;
}
if ( $package_listings > 0 ) {
return true;
}
return false;
}
/**
* Check current package is downgrade package or not via current number of featured, listing lesser
*/
function opalesate_get_user_featured_remaining_listing( $user_id ){
function opalesate_get_user_featured_remaining_listing( $user_id ) {
$count = get_the_author_meta( OPALMEMBERSHIP_USER_PREFIX_.'package_featured_listings' , $user_id );
$count = get_the_author_meta( OPALMEMBERSHIP_USER_PREFIX_ . 'package_featured_listings', $user_id );
return $count;
return $count;
}
/**
*
*/
function opalestate_reset_user_free_package( $user_id ){
$duration = opalestate_options('free_expired_month', 12);
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_.'package_id', -1 );
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_.'package_listings', opalestate_options('free_number_listing', 3) );
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_.'package_featured_listings', opalestate_options('free_number_featured', 3) );
function opalestate_reset_user_free_package( $user_id ) {
$duration = opalestate_options( 'free_expired_month', 12 );
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'package_id', -1 );
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'package_listings', opalestate_options( 'free_number_listing', 3 ) );
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'package_featured_listings', opalestate_options( 'free_number_featured', 3 ) );
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'package_activation', time() );
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'package_expired', time() + ( $duration * 60 * 60 * 24 * 30 ) );
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_.'package_activation', time() );
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_.'package_expired', time() + ($duration*60*60*24*30) );
return true;
return true;
}
/**
@ -138,13 +139,13 @@ function opalestate_reset_user_free_package( $user_id ){
*/
function opalesate_update_package_number_featured_listings( $user_id ) {
$current = get_the_author_meta( OPALMEMBERSHIP_USER_PREFIX_.'package_featured_listings' , $user_id );
$current = get_the_author_meta( OPALMEMBERSHIP_USER_PREFIX_ . 'package_featured_listings', $user_id );
if( $current-1 >= 0 ) {
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_.'package_featured_listings', $current-1 ) ;
} else {
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_.'package_featured_listings', 0 );
}
if ( $current - 1 >= 0 ) {
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'package_featured_listings', $current - 1 );
} else {
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'package_featured_listings', 0 );
}
}
/**
@ -152,50 +153,47 @@ function opalesate_update_package_number_featured_listings( $user_id ) {
*/
function opalesate_update_package_number_listings( $user_id ) {
$current = get_the_author_meta( OPALMEMBERSHIP_USER_PREFIX_.'package_listings' , $user_id );
$current = get_the_author_meta( OPALMEMBERSHIP_USER_PREFIX_ . 'package_listings', $user_id );
if( $current-1 >= 0 ) {
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_.'package_listings', $current-1 ) ;
} else {
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_.'package_listings', 0 );
}
if ( $current - 1 >= 0 ) {
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'package_listings', $current - 1 );
} else {
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'package_listings', 0 );
}
}
/**
* Check
*/
function opalesate_is_membership_valid( $user_id = null ){
// $package_id = (int)get_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_.'package_id', true );
return Opalmembership_User::is_membership_valid( $user_id );
function opalesate_is_membership_valid( $user_id = null ) {
// $package_id = (int)get_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_.'package_id', true );
return Opalmembership_User::is_membership_valid( $user_id );
}
/**
*
*/
function opalesate_listing_set_to_expire($post_id){
function opalesate_listing_set_to_expire( $post_id ) {
$prop = [
'ID' => $post_id,
'post_type' => 'opalestate_property',
'post_status' => 'expired',
];
$prop = array(
wp_update_post( $prop );
'ID' => $post_id,
'post_type' => 'opalestate_property',
'post_status' => 'expired'
);
$post = get_post( $post_id );
$user_id = $post->post_author;
wp_update_post($prop );
$user = get_user_by( 'id', $user_id );
$user_email = $user->user_email;
$post = get_post( $post_id );
$user_id = $post->post_author;
$args = [
'expired_listing_url' => get_permalink( $post_id ),
'expired_listing_name' => get_the_title( $post_id ),
];
$user = get_user_by('id', $user_id);
$user_email = $user->user_email;
$args = array(
'expired_listing_url' => get_permalink($post_id),
'expired_listing_name' => get_the_title($post_id)
);
opalesate_email_type( $user_email, 'listing_expired', $args );
}
opalesate_email_type( $user_email, 'listing_expired', $args );
}

View File

@ -98,6 +98,8 @@ class OpalEstate_Membership {
add_action( 'profile_update', [ __CLASS__, 'on_update_user' ], 10, 1 );
add_action( 'save_post', [ __CLASS__, 'update_property_expired_time' ], 10, 2 );
}
/**
@ -244,6 +246,34 @@ class OpalEstate_Membership {
'description' => esc_html__( 'Number of properties can make featured with this package.', 'opalestate-pro' ),
];
$fields[] = [
'name' => esc_html__( 'Enable Expired for properties ', 'opalestate-pro' ),
'id' => $prefix . 'enable_property_expired',
'type' => 'checkbox',
'description' => esc_html__( 'Do you want enable expired date for properties?', 'opalestate-pro' ),
];
$fields[] = [
'name' => esc_html__( 'Expired Property In', 'opalestate-pro' ),
'id' => $prefix . 'property_duration',
'type' => 'text',
'attributes' => [
'type' => 'number',
'pattern' => '\d*',
'min' => 0,
],
'std' => '1',
'description' => esc_html__( 'Expired a property in. Enter expired number. Example 1, 2, 3', 'opalestate-pro' ),
];
$fields[] = [
'name' => esc_html__( 'Expired property Date Type', 'opalestate-pro' ),
'id' => $prefix . 'property_duration_unit',
'type' => 'select',
'options' => opalmembership_package_expiry_labels(),
'description' => esc_html__( 'Enter expired date type. Example Day(s), Week(s), Month(s), Year(s)', 'opalestate-pro' ),
];
return $fields;
}
@ -358,6 +388,7 @@ class OpalEstate_Membership {
$is_unlimited_listings = get_post_meta( $package_id, OPALMEMBERSHIP_PACKAGES_PREFIX . 'unlimited_listings', true );
$pack_unlimited_listings = $is_unlimited_listings == 'on' ? 0 : 1;
/**
* Get package information with user logined
*/
@ -394,6 +425,14 @@ class OpalEstate_Membership {
*/
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'package_listings', $new_listings );
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'package_featured_listings', $new_featured_listings );
$enable_property_expired = get_post_meta( $package_id, OPALMEMBERSHIP_PACKAGES_PREFIX . 'enable_property_expired', true );
$property_duration = get_post_meta( $package_id, OPALMEMBERSHIP_PACKAGES_PREFIX . 'property_duration', true );
$property_duration_unit = get_post_meta( $package_id, OPALMEMBERSHIP_PACKAGES_PREFIX . 'property_duration_unit', true );
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'enable_property_expired', $enable_property_expired );
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'property_duration', $property_duration );
update_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'property_duration_unit', $property_duration_unit );
}
/**
@ -645,6 +684,70 @@ class OpalEstate_Membership {
}
}
}
/**
* @param $user_id
* @param $post_id
*/
public static function update_property_expired_time( $post_id ) {
$post = get_post( $post_id );
$user_id = $post->post_author;
$activated = get_post_meta( $post_id, OPALESTATE_PROPERTY_PREFIX . 'expired_activated', true );
if ( ! $activated ) {
static::handle_property_expired_time( $user_id, $post_id );
}
}
public static function handle_property_expired_time( $user_id, $post_id ) {
$is_valid = Opalmembership_User::is_membership_valid( $user_id );
if ( ! $is_valid ) {
return;
}
$enable_property_expired = get_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'enable_property_expired', true );
$post_status = get_post_status( $post_id );
if ( $enable_property_expired && 'publish' == $post_status ) {
$property_duration = get_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'property_duration', true );
$property_duration_unit = get_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'property_duration_unit', true );
$expired_time = time() + static::get_expiration_unit_time( $property_duration, $property_duration_unit );
update_post_meta( $post_id, OPALESTATE_PROPERTY_PREFIX . 'expired_time', $expired_time );
update_post_meta( $post_id, OPALESTATE_PROPERTY_PREFIX . 'expired_activated', 1 );
}
}
/**
* @param $duration
* @param $unit
* @return float|int
*/
public static function get_expiration_unit_time( $duration, $unit ) {
if ( ! ( $duration = absint( $duration ) ) ) {
$duration = 1;
}
switch ( $unit ) {
case 'day':
$seconds = 60 * 60 * 24;
break;
case 'week':
$seconds = 60 * 60 * 24 * 7;
break;
case 'month':
$seconds = 60 * 60 * 24 * 30;
break;
case 'year':
$seconds = 60 * 60 * 24 * 365;
break;
}
return $seconds * $duration;
}
}
OpalEstate_Membership::init();