Opal-Estate-Pro/inc/class-opalestate-email.php

582 lines
19 KiB
PHP
Raw Normal View History

2019-09-10 06:27:33 +02:00
<?php
/**
* $Desc$
*
* @version $Id$
* @package $package$
* @author Opal Team <info@wpopal.com >
2019-09-30 11:28:04 +02:00
* @copyright Copyright (C) 2019 wpopal.com. All Rights Reserved.
2019-09-10 06:27:33 +02:00
* @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
*/
// Exit if accessed directly
2019-09-20 12:36:01 +02:00
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
2019-09-10 06:27:33 +02:00
/**
2019-09-20 12:36:01 +02:00
* @class OpalMembership_Checkout
2019-09-10 06:27:33 +02:00
*
* @version 1.0
*/
class Opalestate_Emails {
/**
* init action to automatic send email when user edit or submit a new submission and init setting form in plugin setting of admin
*/
public static function init() {
self::load();
2019-09-20 12:36:01 +02:00
add_action( 'opalestate_processed_new_submission', [ __CLASS__, 'new_submission_email' ], 10, 2 );
2019-10-21 08:24:14 +02:00
add_action( 'opalestate_processed_new_submission', [ __CLASS__, 'admin_new_submission_email' ], 15, 2 );
2019-09-20 12:36:01 +02:00
//add_action( 'opalestate_processed_edit_submission' , array( __CLASS__ , 'new_submission_email'), 10, 2 );
if ( is_admin() ) {
add_filter( 'opalestate_settings_tabs', [ __CLASS__, 'setting_email_tab' ], 1 );
add_filter( 'opalestate_registered_emails_settings', [ __CLASS__, 'setting_email_fields' ], 10, 1 );
}
$enable_approve_property_email = opalestate_get_option( 'enable_approve_property_email' );
if ( $enable_approve_property_email == 'on' ) {
add_action( 'transition_post_status', [ __CLASS__, 'send_email_when_publish_property' ], 10, 3 );
add_action( 'opalestate_processed_approve_publish_property', [ __CLASS__, 'approve_publish_property_email' ], 10, 1 );
}
/**
* Send email when User contact via Enquiry Form and Contact Form
*/
add_action( 'opalestate_send_email_notifycation', [ __CLASS__, 'send_notifycation' ] );
add_action( 'opalestate_send_email_submitted', [ __CLASS__, 'new_submission_email' ] );
2019-10-21 08:24:14 +02:00
add_action( 'opalestate_send_email_submitted', [ __CLASS__, 'admin_new_submission_email' ] );
2019-09-20 12:36:01 +02:00
add_action( 'opalestate_send_email_request_reviewing', [ __CLASS__, 'send_email_request_reviewing' ] );
2019-09-10 06:27:33 +02:00
}
/**
2019-10-21 08:24:14 +02:00
* Load.
2019-09-10 06:27:33 +02:00
*/
public static function load() {
require_once OPALESTATE_PLUGIN_DIR . 'inc/email/class-opalestate-abs-email-template.php';
require_once OPALESTATE_PLUGIN_DIR . 'inc/email/class-opalestate-email-notifycation.php';
require_once OPALESTATE_PLUGIN_DIR . 'inc/email/class-opalestate-request-viewing.php';
require_once OPALESTATE_PLUGIN_DIR . 'inc/email/class-opalestate-new-submitted.php';
2019-10-21 08:24:14 +02:00
require_once OPALESTATE_PLUGIN_DIR . 'inc/email/class-opalestate-admin-new-submitted.php';
2019-10-02 03:33:42 +02:00
require_once OPALESTATE_PLUGIN_DIR . 'inc/email/class-opalestate-approve.php';
2019-09-10 06:27:33 +02:00
}
/**
* Send Email Notifycation with two types: Enquiry or Contact
*/
2019-09-20 12:36:01 +02:00
public static function send_notifycation( $content ) {
$mail = new OpalEstate_Send_Email_Notification();
$mail->set_args( $content );
$return = self::send_mail_now( $mail );
if ( isset( $content['data'] ) ) {
2019-09-10 06:27:33 +02:00
$return['data'] = $content['data'];
}
2019-09-20 12:36:01 +02:00
echo json_encode( $return );
die();
}
2019-09-10 06:27:33 +02:00
/**
* send email if agent submit a new property
*/
2019-09-20 12:36:01 +02:00
public static function new_submission_email( $user_id, $post_id ) {
$mail = new OpalEstate_Send_Email_New_Submitted();
2019-09-10 06:27:33 +02:00
$mail->set_pros( $post_id, $user_id );
2019-09-20 12:36:01 +02:00
$return = self::send_mail_now( $mail );
2019-10-21 08:24:14 +02:00
}
2019-09-10 06:27:33 +02:00
2019-10-21 08:24:14 +02:00
/**
* send email if agent submit a new property
*/
public static function admin_new_submission_email( $user_id, $post_id ) {
$mail = new OpalEstate_Send_Email_Admin_New_Submitted();
$mail->set_pros( $post_id, $user_id );
$return = self::send_mail_now( $mail );
2019-09-10 06:27:33 +02:00
}
/**
* Send email to requet viewing a property
*/
2019-09-20 12:36:01 +02:00
public static function send_email_request_reviewing( $content ) {
2019-09-10 06:27:33 +02:00
$mail = new OpalEstate_Send_Email_Request_Reviewing();
$mail->set_args( $content );
2019-09-20 12:36:01 +02:00
$return = self::send_mail_now( $mail );
2019-09-10 06:27:33 +02:00
2019-09-20 12:36:01 +02:00
echo json_encode( $return );
die();
2019-09-10 06:27:33 +02:00
}
/**
*
*/
2019-09-20 12:36:01 +02:00
public static function send_mail_now( $mail ) {
$from_name = $mail->from_name();
$from_email = $mail->from_email();
$headers = sprintf( "From: %s <%s>\r\n Content-type: text/html", $from_name, $from_email );
2019-09-10 06:27:33 +02:00
2019-09-20 12:36:01 +02:00
$subject = $mail->get_subject();
$message = $mail->get_body();
2019-09-10 06:27:33 +02:00
2019-09-20 12:36:01 +02:00
if ( $mail->to_email() ) {
2019-09-10 06:27:33 +02:00
2019-09-20 12:36:01 +02:00
if ( $mail->get_cc() ) {
2019-09-10 06:27:33 +02:00
$status = @wp_mail( $mail->get_cc(), $subject, $message, $headers );
2019-09-20 12:36:01 +02:00
}
2019-09-10 06:27:33 +02:00
2019-09-20 12:36:01 +02:00
$status = @wp_mail( $mail->to_email(), $subject, $message, $headers );
2019-09-10 06:27:33 +02:00
2019-09-20 12:36:01 +02:00
if ( $status ) {
return [ 'status' => true, 'msg' => esc_html__( 'Message has been successfully sent.', 'opalestate-pro' ) ];
} else {
return [ 'status' => true, 'msg' => esc_html__( 'Unable to send a message.', 'opalestate-pro' ) ];
}
2019-09-10 06:27:33 +02:00
}
2019-09-20 12:36:01 +02:00
return [ 'status' => true, 'msg' => esc_html__( 'Missing some information!', 'opalestate-pro' ) ];
2019-09-10 06:27:33 +02:00
}
/**
*
*/
2019-09-20 12:36:01 +02:00
public static function send_email_when_publish_property( $new_status, $old_status, $post ) {
if ( is_object( $post ) ) {
if ( $post->post_type == 'opalestate_property' ) {
if ( $new_status != $old_status ) {
if ( $new_status == 'publish' ) {
if ( $old_status == 'draft' || $old_status == 'pending' ) {
// Send email
$post_id = $post->ID;
do_action( "opalestate_processed_approve_publish_property", $post_id );
}
}
}
}
}
}
2019-09-10 06:27:33 +02:00
/**
* add new tab Email in opalestate -> setting
*/
2019-09-20 12:36:01 +02:00
public static function setting_email_tab( $tabs ) {
2019-09-10 06:27:33 +02:00
$tabs['emails'] = esc_html__( 'Email', 'opalestate-pro' );
2019-09-20 12:36:01 +02:00
2019-09-10 06:27:33 +02:00
return $tabs;
2019-09-20 12:36:01 +02:00
}
public static function newproperty_email_body() {
}
public static function approve_email_body() {
2019-09-10 06:27:33 +02:00
}
/**
* render setting email fields with default values
*/
2019-09-20 12:36:01 +02:00
public static function setting_email_fields( $fields ) {
2019-09-10 06:27:33 +02:00
$contact_list_tags = '<div>
<p class="tags-description">Use the following tags to automatically add property information to the emails. Tags labeled with an asterisk (*) can be used in the email subject as well.</p>
<div class="opalestate-template-tags-box">
<strong>{receive_name}</strong> Name of the agent who made the property
</div>
<div class="opalestate-template-tags-box">
2019-10-21 08:24:14 +02:00
<strong>{property_link}</strong> Link of the property
2019-09-10 06:27:33 +02:00
</div>
<div class="opalestate-template-tags-box">
<strong>{name}</strong> Name of the user who contact via email form
</div>
<div class="opalestate-template-tags-box">
<strong>{email}</strong> Email of the user who contact via email form
</div>
2019-10-21 08:24:14 +02:00
<div class="opalestate-template-tags-box">
<strong>{message}</strong> * Message content of who sent via form
</div>
<div class="opalestate-template-tags-box">
<strong>{site_link}</strong> A link to this website
</div>
<div class="opalestate-template-tags-box">
<strong>{current_time}</strong> Current date and time
</div>
</div> ';
$review_list_tags = '<div>
<p class="tags-description">Use the following tags to automatically add property information to the emails. Tags labeled with an asterisk (*) can be used in the email subject as well.</p>
<div class="opalestate-template-tags-box">
<strong>{receive_name}</strong> Name of the agent who made the property
</div>
<div class="opalestate-template-tags-box">
<strong>{property_link}</strong> Link of the property
</div>
<div class="opalestate-template-tags-box">
<strong>{name}</strong> Name of the user who contact via email form
</div>
2019-09-10 06:27:33 +02:00
<div class="opalestate-template-tags-box">
2019-10-21 08:24:14 +02:00
<strong>{email}</strong> Email of the user who contact via email form
</div>
<div class="opalestate-template-tags-box">
<strong>{schedule_time}</strong> Schedule time
</div>
<div class="opalestate-template-tags-box">
<strong>{schedule_date}</strong> Schedule date
2019-09-10 06:27:33 +02:00
</div>
<div class="opalestate-template-tags-box">
<strong>{message}</strong> * Message content of who sent via form
</div>
2019-10-21 08:24:14 +02:00
<div class="opalestate-template-tags-box">
<strong>{site_link}</strong> A link to this website
</div>
<div class="opalestate-template-tags-box">
<strong>{current_time}</strong> Current date and time
</div>
2019-09-10 06:27:33 +02:00
</div> ';
$list_tags = '<div>
<p class="tags-description">Use the following tags to automatically add property information to the emails. Tags labeled with an asterisk (*) can be used in the email subject as well.</p>
<div class="opalestate-template-tags-box">
<strong>{property_name}</strong> Email of the user who made the property
</div>
<div class="opalestate-template-tags-box">
2019-10-21 08:24:14 +02:00
<strong>{property_link}</strong> Link of the property
</div>
<div class="opalestate-template-tags-box">
<strong>{property_edit_link}</strong> Link for editing of the property (admin)
2019-09-10 06:27:33 +02:00
</div>
<div class="opalestate-template-tags-box">
<strong>{user_email}</strong> Email of the user who made the property
</div>
<div class="opalestate-template-tags-box">
<strong>{submitted_date}</strong> Email of the user who made the property
</div>
<div class="opalestate-template-tags-box">
<strong>{user_name}</strong> * Name of the user who made the property
</div>
<div class="opalestate-template-tags-box">
<strong>{date}</strong> * Date and time of the property
</div>
<div class="opalestate-template-tags-box">
<strong>{site_name}</strong> The name of this website
</div>
<div class="opalestate-template-tags-box">
<strong>{site_link}</strong> A link to this website
</div>
<div class="opalestate-template-tags-box">
<strong>{current_time}</strong> Current date and time
</div></div>';
$list_tags = apply_filters( 'opalestate_email_tags', $list_tags );
2019-09-20 12:36:01 +02:00
$fields = [
'id' => 'options_page',
'title' => esc_html__( 'Email Settings', 'opalestate-pro' ),
'show_on' => [ 'key' => 'options-page', 'value' => [ 'opalestate_settings' ], ],
'fields' => apply_filters( 'opalestate_settings_emails', [
[
2019-09-10 06:27:33 +02:00
'name' => esc_html__( 'Email Settings', 'opalestate-pro' ),
'desc' => '<hr>',
'id' => 'opalestate_title_email_settings_1',
2019-09-20 12:36:01 +02:00
'type' => 'title',
],
[
2019-09-10 06:27:33 +02:00
'id' => 'from_name',
'name' => esc_html__( 'From Name', 'opalestate-pro' ),
'desc' => esc_html__( 'The name donation receipts are said to come from. This should probably be your site or shop name.', 'opalestate-pro' ),
'default' => get_bloginfo( 'name' ),
2019-09-20 12:36:01 +02:00
'type' => 'text',
],
[
2019-09-10 06:27:33 +02:00
'id' => 'from_email',
'name' => esc_html__( 'From Email', 'opalestate-pro' ),
'desc' => esc_html__( 'Email to send donation receipts from. This will act as the "from" and "reply-to" address.', 'opalestate-pro' ),
'default' => get_bloginfo( 'admin_email' ),
2019-09-20 12:36:01 +02:00
'type' => 'text',
],
2019-09-10 06:27:33 +02:00
2019-09-20 12:36:01 +02:00
[
2019-09-10 06:27:33 +02:00
'name' => esc_html__( 'Email Submission Templates (Template Tags)', 'opalestate-pro' ),
2019-09-20 12:36:01 +02:00
'desc' => $list_tags . '<br><hr>',
2019-09-10 06:27:33 +02:00
'id' => 'opalestate_title_email_settings_2',
2019-09-20 12:36:01 +02:00
'type' => 'title',
],
2019-09-10 06:27:33 +02:00
2019-09-30 11:28:04 +02:00
//------------------------------------------
2019-09-20 12:36:01 +02:00
[
2019-10-21 08:24:14 +02:00
'name' => esc_html__( 'Notification For New Property Submission (Customer)', 'opalestate-pro' ),
2019-09-10 06:27:33 +02:00
'desc' => '<hr>',
'id' => 'opalestate_title_email_settings_3',
2019-09-20 12:36:01 +02:00
'type' => 'title',
],
[
'id' => 'newproperty_email_subject',
'name' => esc_html__( 'Email Subject', 'opalestate-pro' ),
'type' => 'text',
'desc' => esc_html__( 'The email subject for admin notifications.', 'opalestate-pro' ),
'attributes' => [
2019-10-21 08:24:14 +02:00
'rows' => 3,
2019-09-20 12:36:01 +02:00
],
'default' => esc_html__( 'New Property Listing Submitted: {property_name}', 'opalestate-pro' ),
],
[
2019-09-10 06:27:33 +02:00
'id' => 'newproperty_email_body',
'name' => esc_html__( 'Email Body', 'opalestate-pro' ),
'type' => 'wysiwyg',
2019-09-20 12:36:01 +02:00
'desc' => esc_html__( 'Enter the email an admin should receive when an initial payment request is made.', 'opalestate-pro' ),
2019-09-10 06:27:33 +02:00
'default' => OpalEstate_Send_Email_New_Submitted::get_default_template(),
2019-09-20 12:36:01 +02:00
],
2019-09-10 06:27:33 +02:00
//------------------------------------------
2019-10-21 08:24:14 +02:00
[
'name' => esc_html__( 'Notification For New Property Submission (Admin)', 'opalestate-pro' ),
'desc' => '<hr>',
'id' => 'opalestate_title_email_settings_admin',
'type' => 'title',
],
[
'id' => 'admin_newproperty_email_subject',
'name' => esc_html__( 'Email Subject', 'opalestate-pro' ),
'type' => 'text',
'desc' => esc_html__( 'The email subject for admin notifications.', 'opalestate-pro' ),
'attributes' => [
'rows' => 3,
],
'default' => esc_html__( 'You received a new submission: {property_name} from {user_mail}', 'opalestate-pro' ),
],
[
'id' => 'admin_newproperty_email_body',
'name' => esc_html__( 'Email Body', 'opalestate-pro' ),
'type' => 'wysiwyg',
'desc' => esc_html__( 'Enter the email an admin should receive when an initial payment request is made.', 'opalestate-pro' ),
'default' => OpalEstate_Send_Email_Admin_New_Submitted::get_default_template(),
],
//------------------------------------------
2019-09-20 12:36:01 +02:00
[
2019-09-10 06:27:33 +02:00
'name' => esc_html__( 'Approve property for publish', 'opalestate-pro' ),
'desc' => '<hr>',
'id' => 'opalestate_title_email_settings_4',
2019-09-20 12:36:01 +02:00
'type' => 'title',
],
[
'name' => esc_html__( 'Enable approve property email', 'opalestate-pro' ),
'desc' => esc_html__( 'Enable approve property email.', 'opalestate-pro' ),
'id' => 'enable_approve_property_email',
'type' => 'switch',
'options' => [
'on' => esc_html__( 'Enable', 'opalestate-pro' ),
'off' => esc_html__( 'Disable', 'opalestate-pro' ),
],
'default' => 'off',
],
[
'id' => 'approve_email_subject',
'name' => esc_html__( 'Email Subject', 'opalestate-pro' ),
'type' => 'text',
'desc' => esc_html__( 'The email subject a user should receive when they make an initial property request.', 'opalestate-pro' ),
'attributes' => [
'placeholder' => 'Your property at I Love WordPress is pending',
get_bloginfo( 'name' ),
'rows' => 3,
],
'default' => esc_html__( 'New Property Listing Submitted: {property_name}', 'opalestate-pro' ),
],
[
'id' => 'approve_email_body',
'name' => esc_html__( 'Email Body', 'opalestate-pro' ),
'type' => 'wysiwyg',
'desc' => esc_html__( 'Enter the email a user should receive when they make an initial payment request.', 'opalestate-pro' ),
'default' => OpalEstate_Send_Email_Approve::get_default_template(),
],
2019-09-30 11:28:04 +02:00
/// enquire contact template ////
[
'name' => esc_html__( 'Email Enquiry Contact Templates (Template Tags)', 'opalestate-pro' ),
'desc' => $contact_list_tags . '<br><hr>',
'id' => 'opalestate_title_email_settings_6_1',
'type' => 'title',
],
[
'id' => 'enquiry_email_subject',
'name' => esc_html__( 'Email Subject', 'opalestate-pro' ),
'type' => 'text',
'desc' => esc_html__( 'The email subject a user should receive when they make an initial property request.', 'opalestate-pro' ),
'attributes' => [
'placeholder' => 'Your property at I Love WordPress is pending',
get_bloginfo( 'name' ),
'rows' => 3,
],
'default' => esc_html__( 'You got a message', 'opalestate-pro' ),
],
2019-09-10 06:27:33 +02:00
2019-09-30 11:28:04 +02:00
[
2019-10-21 08:24:14 +02:00
'id' => 'enquiry_email_body',
'name' => esc_html__( 'Email Body', 'opalestate-pro' ),
'type' => 'wysiwyg',
'default' => OpalEstate_Send_Email_Notification::get_default_template( 'enquiry' ),
2019-09-30 11:28:04 +02:00
],
2019-09-10 06:27:33 +02:00
/// email contact template ////
2019-09-20 12:36:01 +02:00
[
2019-09-10 06:27:33 +02:00
'name' => esc_html__( 'Email Contact Templates (Template Tags)', 'opalestate-pro' ),
2019-09-20 12:36:01 +02:00
'desc' => $contact_list_tags . '<br><hr>',
2019-09-10 06:27:33 +02:00
'id' => 'opalestate_title_email_settings_6',
2019-09-20 12:36:01 +02:00
'type' => 'title',
],
[
'id' => 'contact_email_subject',
'name' => esc_html__( 'Email Subject', 'opalestate-pro' ),
'type' => 'text',
'desc' => esc_html__( 'The email subject a user should receive when they make an initial property request.', 'opalestate-pro' ),
'attributes' => [
'placeholder' => 'Your property at I Love WordPress is pending',
get_bloginfo( 'name' ),
'rows' => 3,
],
'default' => esc_html__( 'You got a message', 'opalestate-pro' ),
],
[
2019-10-21 08:24:14 +02:00
'id' => 'contact_email_body',
'name' => esc_html__( 'Email Body', 'opalestate-pro' ),
'type' => 'wysiwyg',
'default' => OpalEstate_Send_Email_Notification::get_default_template(),
2019-09-30 11:28:04 +02:00
],
/// Email Request Review ///
[
'name' => esc_html__( 'Email Request Review Templates (Template Tags)', 'opalestate-pro' ),
2019-10-21 08:24:14 +02:00
'desc' => $review_list_tags . '<br><hr>',
2019-09-30 11:28:04 +02:00
'id' => 'opalestate_title_email_settings_7',
'type' => 'title',
],
[
'id' => 'request_review_email_subject',
'name' => esc_html__( 'Email Subject', 'opalestate-pro' ),
'type' => 'text',
'desc' => esc_html__( 'The email subject a user should receive when they make an initial property request.', 'opalestate-pro' ),
'attributes' => [
'placeholder' => 'Your property at I Love WordPress is pending',
get_bloginfo( 'name' ),
'rows' => 3,
],
2019-10-21 08:24:14 +02:00
'default' => esc_html__( 'You have a message request reviewing', 'opalestate-pro' ),
2019-09-30 11:28:04 +02:00
],
[
2019-10-21 08:24:14 +02:00
'id' => 'request_review_email_body',
'name' => esc_html__( 'Email Body', 'opalestate-pro' ),
'type' => 'wysiwyg',
'default' => OpalEstate_Send_Email_Request_Reviewing::get_default_template(),
2019-09-20 12:36:01 +02:00
],
]
),
];
2019-09-10 06:27:33 +02:00
return $fields;
}
/**
* get data of newrequest email
*
* @return text: message
2019-09-20 12:36:01 +02:00
* @var $args array: property_id , $body
2019-09-10 06:27:33 +02:00
*/
public static function replace_shortcode( $args, $body ) {
2019-09-20 12:36:01 +02:00
$tags = [
'user_name' => "",
'user_mail' => "",
2019-09-10 06:27:33 +02:00
'submitted_date' => "",
2019-09-20 12:36:01 +02:00
'property_name' => "",
'site_name' => '',
'site_link' => '',
'property_link' => '',
];
2019-09-10 06:27:33 +02:00
$tags = array_merge( $tags, $args );
extract( $tags );
2019-09-20 12:36:01 +02:00
$tags = [
"{user_mail}",
"{user_name}",
"{submitted_date}",
"{site_name}",
"{site_link}",
"{current_time}",
'{property_name}',
'{property_link}',
];
$values = [
$user_mail,
$user_name,
$submitted_date,
get_bloginfo( 'name' ),
get_home_url(),
date( "F j, Y, g:i a" ),
$property_name,
$property_link,
];
$message = str_replace( $tags, $values, $body );
2019-09-10 06:27:33 +02:00
return $message;
}
2019-09-20 12:36:01 +02:00
2019-09-10 06:27:33 +02:00
public static function approve_publish_property_email( $post_id ) {
2019-09-20 12:36:01 +02:00
$mail = new OpalEstate_Send_Email_Approve();
2019-09-10 06:27:33 +02:00
$mail->set_pros( $post_id );
2019-09-20 12:36:01 +02:00
$return = self::send_mail_now( $mail );
2019-09-10 06:27:33 +02:00
2019-09-20 12:36:01 +02:00
echo json_encode( $return );
die();
}
2019-09-10 06:27:33 +02:00
}
2019-09-20 12:36:01 +02:00
Opalestate_Emails::init();