Function Reference: geodir_payment_invoice_callback_add_listing

Summary

This function has not been documented yet.

Source Code

function geodir_payment_invoice_callback_add_listing( $invoice_id, $new_status, $old_status = 'pending', $subscription = false ) {
	$invoice_info = geodir_get_invoice( $invoice_id );

	if ( empty( $invoice_info ) ) {
		return false;
	}

    if (  $new_status == $old_status && !$subscription) {
        return false;
    }

	$invoice_package_id = $invoice_info->package_id;
	$invoice_alive_days = $invoice_info->alive_days;
	$invoice_type		= $invoice_info->invoice_type;
	
	$current_date = date_i18n( 'Y-m-d', current_time( 'timestamp' ) );
	
	$post_id = $invoice_info->post_id;
	$gd_post_info = geodir_get_post_info( $post_id );
	
	if ( empty( $gd_post_info ) ) {
		return false;
	}

	$package_info = geodir_get_package_info( $gd_post_info->package_id );
	
	$post_status = get_post_status( $post_id );
	$post_package_id = $gd_post_info->package_id;
	$post_expire_date = $gd_post_info->expire_date;
	
	$sub_num_trial_days = 0;
	$sub_units = 'D';
	$sub_units_num = 1;
	
	if ( !empty( $package_info ) ) {
		$sub_num_trial_days = (int)$package_info->sub_num_trial_days;
		$sub_units = $package_info->sub_units;
		$sub_units_num = $package_info->sub_units_num;
	}
			
	if ( $new_status == 'confirmed' ) {
		$post_default_status = geodir_new_post_default_status();
		$post_default_status = $post_default_status != '' ? $post_default_status : 'publish';
												
		$update_post = array();
		
		if ( $subscription ) {
			$payment_date = date_i18n( 'Y-m-d', strtotime( $invoice_info->date ) );
			
			$date_diff = round( abs( strtotime( $payment_date ) - strtotime( $current_date ) ) / 86400 );
			
			$multiply = 1;
			
			if ( $sub_units == 'W' ) {
				$multiply = 7;
			}
			
			if ( $sub_units == 'M' ) {
				$multiply = 30;
			}
			
			if ( $sub_units == 'Y' ) {
				$multiply = 365;
			}
			
			$pay_days = $sub_units_num * $multiply;
            //$alive_days = $pay_days - $date_diff;
            $alive_days = $pay_days;

			// Update post status
			if ( $sub_num_trial_days > 0 || $alive_days > 0 ) {
				geodir_set_post_status( $post_id, $post_default_status );
				
				if ( $post_status != 'publish' ) {
					$update_post['ID'] = $post_id;
					$update_post['post_date'] = current_time( 'mysql' );
					$update_post['post_date_gmt'] = current_time( 'mysql', 1 );
				}
			}

			$expire_date = date_i18n( 'Y-m-d', strtotime( $current_date . '+' . $alive_days . ' days' ) );

			geodir_save_post_meta( $post_id, 'expire_date', $expire_date );
		} else {
			// Update post status
			geodir_set_post_status( $post_id, $post_default_status );
		
			if ( !empty( $invoice_package_id ) && $invoice_alive_days > 0 && $invoice_package_id == $post_package_id && geodir_strtolower( $post_expire_date ) != 'never' && strtotime( $post_expire_date ) >= strtotime( $current_date ) && $post_status == 'publish' ) {
				$alive_days = (int)($gd_post_info->alive_days + $invoice_alive_days);
				$expire_date = date_i18n( 'Y-m-d', strtotime( $post_expire_date . ' + ' . $invoice_alive_days . ' days' ) );
			} else {
				if ( $post_status != 'publish' ) {
					$update_post['ID'] = $post_id;
					$update_post['post_date'] = current_time( 'mysql' );
					$update_post['post_date_gmt'] = current_time( 'mysql', 1 );
				}
				
				$alive_days = (int)$gd_post_info->alive_days;
				
				if ( geodir_strtolower( $post_expire_date ) != 'never' && strtotime( $post_expire_date ) < strtotime( $current_date ) ) {
					$alive_days = $invoice_alive_days;
				}
				
				$expire_date = $alive_days > 0 ? date_i18n( 'Y-m-d', strtotime( $current_date . ' + ' . $alive_days . ' days' ) ) : 'Never';
			}
		}
		
		geodir_save_post_meta( $post_id, 'alive_days', $alive_days);
		geodir_save_post_meta( $post_id, 'expire_date', $expire_date);
				
		// Update the post into the database
		if ( !empty( $update_post ) ) {
			wp_update_post( $update_post );
		}
		
		$auther_id = !empty($gd_post_info->post_author) ? $gd_post_info->post_author : $invoice_info->user_id;
		$author_data = get_userdata($auther_id);
		
		$post_status = get_post_status( $post_id );
		
		if ($post_status == 'publish' && !empty($author_data)) {
			if ($invoice_type == 'upgrade_listing') {
				geodir_payment_clientEmail($post_id, $auther_id, 'payment_upgrade');
				geodir_payment_adminEmail($post_id, $auther_id, 'payment_upgrade');
			} else if ($invoice_type == 'renew_listing') {
				geodir_payment_clientEmail($post_id, $auther_id, 'payment_renew');
				geodir_payment_adminEmail($post_id, $auther_id, 'payment_renew');
			}
		}
	} else if ( $new_status == 'pending' ) {
		geodir_set_post_status( $post_id, 'draft' );
	} else if ( $new_status == 'cancelled' ) {
		geodir_set_post_status( $post_id, 'draft' );
	} else if ( $new_status == 'failed' ) {
		geodir_set_post_status( $post_id, 'draft' );
	} else if ( $new_status == 'onhold' ) {
		geodir_set_post_status( $post_id, 'draft' );
	}
	
	return true;
}