2019-09-10 06:27:33 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* $Desc$
|
|
|
|
*
|
|
|
|
* @version $Id$
|
|
|
|
* @package opalestate
|
|
|
|
* @author Opal Team <info@wpopal.com >
|
|
|
|
* @copyright Copyright (C) 2019 wpopal.com. All Rights Reserved.
|
|
|
|
* @license GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
|
|
|
|
*
|
|
|
|
* @website http://www.wpopal.com
|
|
|
|
* @support http://www.wpopal.com/support/forum.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2019-11-04 02:28:41 +01:00
|
|
|
exit; // Exit if accessed directly
|
2019-09-10 06:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Count Number of listing following user
|
|
|
|
*/
|
2019-11-04 02:28:41 +01:00
|
|
|
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();
|
2019-09-10 06:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Count Number of featured listing following user
|
|
|
|
*/
|
2019-11-04 02:28:41 +01:00
|
|
|
function opalesate_get_user_current_featured_listings( $user_id ) {
|
|
|
|
|
|
|
|
$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();
|
2019-09-10 06:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check current package is downgrade package or not via current number of featured, listing lesser
|
|
|
|
*/
|
2019-11-04 02:28:41 +01:00
|
|
|
function opalesate_check_package_downgrade_status( $user_id, $package_id ) {
|
2019-09-10 06:27:33 +02:00
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
$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;
|
2019-09-10 06:27:33 +02:00
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
$user_current_listings = opalesate_get_user_current_listings( $user_id );
|
|
|
|
$user_current_featured_listings = opalesate_get_user_current_featured_listings( $user_id );
|
2019-09-10 06:27:33 +02:00
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
$current_listings = get_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'package_listings', true );
|
2019-09-10 06:27:33 +02:00
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
if ( $pack_unlimited_listings == 1 ) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-09-10 06:27:33 +02:00
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
// if is unlimited and go to non unlimited pack
|
|
|
|
if ( $current_listings == -1 && $pack_unlimited_listings != 1 ) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-10 06:27:33 +02:00
|
|
|
|
2019-12-06 07:30:11 +01:00
|
|
|
if ( $user_current_listings > $pack_listings ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2019-09-10 06:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check Current User having permission to add new property or not?
|
|
|
|
*/
|
2019-11-04 02:28:41 +01:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
$package_listings = (int) get_user_meta( $user_id, OPALMEMBERSHIP_USER_PREFIX_ . 'package_listings', true );
|
2019-09-10 06:27:33 +02:00
|
|
|
|
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
$unlimited_listings = get_post_meta( $package_id, OPALMEMBERSHIP_PACKAGES_PREFIX . 'unlimited_listings', true );
|
2019-09-10 06:27:33 +02:00
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
$unlimited_listings = ! empty( $unlimited_listings ) && $unlimited_listings == 'on' ? 0 : 1;
|
2019-09-10 06:27:33 +02:00
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
if ( $package_id > 0 && $unlimited_listings ) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-10 06:27:33 +02:00
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
if ( $package_listings > 0 ) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-10 06:27:33 +02:00
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
return false;
|
2019-09-10 06:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check current package is downgrade package or not via current number of featured, listing lesser
|
|
|
|
*/
|
2019-11-04 02:28:41 +01:00
|
|
|
function opalesate_get_user_featured_remaining_listing( $user_id ) {
|
2019-09-10 06:27:33 +02:00
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
$count = get_the_author_meta( OPALMEMBERSHIP_USER_PREFIX_ . 'package_featured_listings', $user_id );
|
2019-09-10 06:27:33 +02:00
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
return $count;
|
2019-09-10 06:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2019-11-04 02:28:41 +01:00
|
|
|
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 ) );
|
|
|
|
|
|
|
|
return true;
|
2019-09-10 06:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update remaining featured listings
|
|
|
|
*/
|
|
|
|
function opalesate_update_package_number_featured_listings( $user_id ) {
|
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
$current = get_the_author_meta( OPALMEMBERSHIP_USER_PREFIX_ . 'package_featured_listings', $user_id );
|
2019-09-10 06:27:33 +02:00
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
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 );
|
|
|
|
}
|
2019-09-10 06:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update remaining featured listings
|
|
|
|
*/
|
|
|
|
function opalesate_update_package_number_listings( $user_id ) {
|
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
$current = get_the_author_meta( OPALMEMBERSHIP_USER_PREFIX_ . 'package_listings', $user_id );
|
2019-09-10 06:27:33 +02:00
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
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 );
|
|
|
|
}
|
2019-09-10 06:27:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check
|
|
|
|
*/
|
2019-11-04 02:28:41 +01:00
|
|
|
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 );
|
2019-09-10 06:27:33 +02:00
|
|
|
}
|
2019-11-04 02:28:41 +01:00
|
|
|
|
|
|
|
|
2019-09-10 06:27:33 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2019-11-04 02:28:41 +01:00
|
|
|
function opalesate_listing_set_to_expire( $post_id ) {
|
|
|
|
$prop = [
|
|
|
|
'ID' => $post_id,
|
|
|
|
'post_type' => 'opalestate_property',
|
|
|
|
'post_status' => 'expired',
|
|
|
|
];
|
2019-09-10 06:27:33 +02:00
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
wp_update_post( $prop );
|
2019-09-10 06:27:33 +02:00
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
$post = get_post( $post_id );
|
|
|
|
$user_id = $post->post_author;
|
2019-09-10 06:27:33 +02:00
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
$user = get_user_by( 'id', $user_id );
|
|
|
|
$user_email = $user->user_email;
|
2019-09-10 06:27:33 +02:00
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
$args = [
|
|
|
|
'expired_listing_url' => get_permalink( $post_id ),
|
|
|
|
'expired_listing_name' => get_the_title( $post_id ),
|
|
|
|
];
|
2019-09-10 06:27:33 +02:00
|
|
|
|
2019-11-04 02:28:41 +01:00
|
|
|
opalesate_email_type( $user_email, 'listing_expired', $args );
|
|
|
|
}
|