Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 33 additions & 13 deletions includes/classes/class-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function __construct() {
Offline Payment Made*/
// add_action('atbdp_offline_payment_created', array($this, 'notify_owner_offline_payment_created'), 10, 2);
/*Fire up email for Completed Orders*/
add_action( 'atbdp_order_completed', [ $this, 'notify_owner_order_completed' ], 10, 2 );
add_action( 'atbdp_order_completed', [ $this, 'notify_admin_order_completed' ], 10, 2 );
add_action( 'atbdp_order_completed', [ $this, 'notify_owner_order_completed' ], 10, 3 );
add_action( 'atbdp_order_completed', [ $this, 'notify_admin_order_completed' ], 10, 3 );
/*Fire up email for renewal notification*/
add_action( 'atbdp_status_updated_to_renewal', [ $this, 'notify_owner_listing_to_expire' ] );
/*Fire up email for expired listings*/
Expand Down Expand Up @@ -92,12 +92,13 @@ public function notify_owner_offline_payment_created( $order_id, $listing_id ) {
* @param int $order_id [optional] Order ID
* @param int $listing_id [optional] Listing ID
* @param WP_User $user [optional] User Object
* @param stdClass $order [optional] New table order object
* @see strtr() is better than str_replace() in our case : https://stackoverflow.com/questions/8177296/when-to-use-strtr-vs-str-replace
* @return string It returns the content after replacing the placeholder with proper data.
*/
public function replace_in_content( $content, $order_id = 0, $listing_id = 0, $user = null, $renewal = null, $pin = 0 ) {
public function replace_in_content( $content, $order_id = 0, $listing_id = 0, $user = null, $renewal = null, $pin = 0, $order = null ) {
if ( empty( $listing_id ) ) {
$listing_id = (int) get_post_meta( $order_id, '_listing_id', true );
$listing_id = ! empty( $order->listing_id ) ? (int) $order->listing_id : (int) get_post_meta( $order_id, '_listing_id', true );
}
if ( empty( $user ) ) {
$post_author_id = get_post_field( 'post_author', $listing_id ? $listing_id : $order_id );
Expand Down Expand Up @@ -184,7 +185,26 @@ public function replace_in_content( $content, $order_id = 0, $listing_id = 0, $u

$c = nl2br( strtr( $content, $find_replace ) );
// we do not want to use br for line break in the order details markup. so we removed that from bulk replacement.
return str_replace( '==ORDER_DETAILS==', ATBDP_Order::get_order_details( $order_id ), $c );
if ( false === strpos( $c, '==ORDER_DETAILS==' ) ) {
return $c;
}

return str_replace( '==ORDER_DETAILS==', ATBDP_Order::get_order_details( $order_id, $order ), $c );
}

/**
* Get the admin order details URL for legacy and new table orders.
*
* @param int $order_id The order ID.
* @param stdClass $order Optional new table order object.
* @return string
*/
private function get_admin_order_receipt_url( $order_id, $order = null ) {
if ( ! empty( $order ) ) {
return admin_url( 'edit.php?post_type=at_biz_dir&page=directorist-orders#/edit/' . absint( $order_id ) );
}

return admin_url( 'edit.php?post_type=atbdp_orders' );
}

/**
Expand Down Expand Up @@ -571,7 +591,7 @@ public function notify_owner_order_created( $order_id, $listing_id, $offline = f
* @param int $listing_id The Listing ID
* @return bool Whether the message was sent successfully or not.
*/
public function notify_owner_order_completed( $order_id, $listing_id ) {
public function notify_owner_order_completed( $order_id, $listing_id, $order = null ) {
if ( get_directorist_option( 'disable_email_notification' ) ) {
return false;
}
Expand All @@ -581,8 +601,8 @@ public function notify_owner_order_completed( $order_id, $listing_id ) {
}

$user = $this->get_owner( $listing_id ? $listing_id : $order_id );
$subject = $this->replace_in_content( get_directorist_option( 'email_sub_completed_order' ), $order_id, $listing_id, $user );
$body = $this->replace_in_content( get_directorist_option( 'email_tmpl_completed_order' ), $order_id, $listing_id, $user );
$subject = $this->replace_in_content( get_directorist_option( 'email_sub_completed_order' ), $order_id, $listing_id, $user, null, 0, $order );
$body = $this->replace_in_content( get_directorist_option( 'email_tmpl_completed_order' ), $order_id, $listing_id, $user, null, 0, $order );
$message = atbdp_email_html( $subject, $body );
$to = $user->user_email;
$headers = $this->get_email_headers();
Expand Down Expand Up @@ -1094,7 +1114,7 @@ public function notify_admin_order_created( $order_id, $listing_id ) {
* @param int $listing_id the listing id
* @return bool Whether the email was sent correctly or not
*/
public function notify_admin_order_completed( $order_id, $listing_id ) {
public function notify_admin_order_completed( $order_id, $listing_id, $order = null ) {

if ( get_directorist_option( 'disable_email_notification' ) ) {
return false;
Expand All @@ -1105,11 +1125,11 @@ public function notify_admin_order_completed( $order_id, $listing_id ) {
}

$s = __( '[==SITE_NAME==] Payment Notification : Order #==ORDER_ID== Completed', 'directorist' );
$subject = $this->replace_in_content( $s, $order_id );
$subject = $this->replace_in_content( $s, $order_id, $listing_id, null, null, 0, $order );

$t = $this->get_order_completed_admin_tmpl(); // get the email template & replace order_receipt placeholder in it
$body = str_replace( '==ORDER_RECEIPT_URL==', admin_url( 'edit.php?post_type=atbdp_orders' ), $t );
$body = $this->replace_in_content( $body, $order_id, $listing_id );
$body = str_replace( '==ORDER_RECEIPT_URL==', $this->get_admin_order_receipt_url( $order_id, $order ), $t );
$body = $this->replace_in_content( $body, $order_id, $listing_id, null, null, 0, $order );
$message = atbdp_email_html( $subject, $body );
$to = $this->get_admin_email_list();
$headers = $this->get_email_headers();
Expand Down Expand Up @@ -1324,4 +1344,4 @@ public function send_user_confirmation_email( Wp_User $user ) {
return $this->send_mail( $user->user_email, $subject, $body, $this->get_email_headers() );
}
} // ends class
endif;
endif;
12 changes: 9 additions & 3 deletions includes/classes/class-featured-listing-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
class FeaturedListingCheckout {
const CHECKOUT_TYPE = 'featured_listing';

public function __construct() {
public function __construct() {
add_filter( 'directorist_checkout_types', [$this, 'add_checkout_type'] );
add_filter( 'directorist_order_data', [ $this, 'handle_order_data' ] );
add_action( 'directorist_after_order_update', [$this, 'handle_after_order_update'] );
add_action( 'directorist_after_order_update', [$this, 'handle_after_order_update'], 10, 2 );
add_filter( 'directorist_payment_receipt_order_items', [$this, 'handle_payment_receipt_order_items'], 10, 2 );
add_filter( 'directorist_checkout_validation', [$this, 'validate_checkout'], 10, 2 );
add_action( 'directorist_checkout_table', [$this, 'handle_checkout_table'], 10, 4 );
Expand Down Expand Up @@ -142,7 +142,7 @@ public function handle_before_order_update( OrderDTO $dto ) {
}
}

public function handle_after_order_update( OrderDTO $dto ) {
public function handle_after_order_update( OrderDTO $dto, $old_order = null ) {
if ( ! $this->is_featured_order( $dto ) ) {
return;
}
Expand All @@ -160,6 +160,12 @@ public function handle_after_order_update( OrderDTO $dto ) {
if ( $listing && 'publish' !== $listing->post_status ) {
directorist_set_listing_status( $dto->get_listing_id(), 'publish' );
}

if ( ! isset( $old_order->status ) || Status::PAID !== $old_order->status ) {
$order = directorist_order_repository()->single( $dto->get_id() );

do_action( 'atbdp_order_completed', $dto->get_id(), $dto->get_listing_id(), $order );
}
} else {
directorist_set_listing_featured( $dto->get_listing_id(), false );
}
Expand Down
97 changes: 91 additions & 6 deletions includes/payments/class-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,20 @@ public function sync_order_author_on_listing_update( $listing_id, $post, $update

/**
* It returns order details in html format
* @param int $order_id The order ID
* @param int $order_id The order ID
* @param stdClass $table_order Optional new table order object.
* @return string
*/
public static function get_order_details( $order_id ) {
public static function get_order_details( $order_id, $table_order = null ) {
if ( ! empty( $table_order ) ) {
return self::get_table_order_details( $table_order );
}

if ( empty( $order_id ) ) return __( 'No Order ID Provided', 'directorist' );
$c_position = get_directorist_option( 'payment_currency_position' );
$currency = atbdp_get_payment_currency();
$symbol = atbdp_currency_symbol( $currency );
$c_position = get_directorist_option( 'payment_currency_position' );
$currency = atbdp_get_payment_currency();
$symbol = atbdp_currency_symbol( $currency );

$order_items = apply_filters( 'atbdp_order_items_data', [], $order_id ); // this is the hook that an extension can hook to, to add new items on checkout page.eg. plan

$featured = get_post_meta( $order_id, '_featured', true );
Expand Down Expand Up @@ -166,6 +172,85 @@ public static function get_order_details( $order_id ) {
return ob_get_clean();
}

/**
* It returns new table order details in html format.
*
* @param stdClass $table_order New table order object.
* @return string
*/
public static function get_table_order_details( $table_order ) {
if ( empty( $table_order ) ) return __( 'No Order ID Provided', 'directorist' );

$table_order_id = ! empty( $table_order->id ) ? absint( $table_order->id ) : 0;

if ( $table_order_id && function_exists( 'directorist_order_repository' ) && ! isset( $table_order->listing_title ) ) {
$order = directorist_order_repository()->single( $table_order_id );

if ( ! empty( $order ) ) {
$table_order = $order;
}
}

$c_position = get_directorist_option( 'payment_currency_position' );
$order_currency = ! empty( $table_order->currency ) ? $table_order->currency : atbdp_get_payment_currency();
$symbol = html_entity_decode( atbdp_currency_symbol( $order_currency ), ENT_QUOTES, get_bloginfo( 'charset' ) );
$listing_id = ! empty( $table_order->listing_id ) ? absint( $table_order->listing_id ) : 0;
$order_dto = function_exists( 'directorist_order_repository' ) ? directorist_order_repository()->to_dto( $table_order ) : null;
$order_items = $order_dto ? apply_filters( 'directorist_payment_receipt_order_items', [], $order_dto ) : [];

if ( empty( $order_items ) ) {
$order_items[] = [
'title' => ! empty( $table_order->order_type ) ? $table_order->order_type : __( 'Order', 'directorist' ),
'desc' => ! empty( $table_order->listing_title ) ? $table_order->listing_title : get_the_title( $listing_id ),
'price' => $table_order->sub_total ?? $table_order->amount ?? 0,
];
}

$before = '';
$after = '';

if ( 'after' == $c_position ) {
$after = $symbol;
} else {
$before = $symbol;
}

ob_start();
?>
<table border="0" cellspacing="0" cellpadding="7" style="border:1px solid #CCC;">
<tr style="background-color:#F0F0F0;">
<th style="border-right:1px solid #CCC; border-bottom:1px solid #CCC; text-align:left;"><?php esc_html_e( 'Item(s)', 'directorist' ); ?></th>
<th style="border-bottom:1px solid #CCC;"><?php printf( esc_html__( 'Price [%s]', 'directorist' ), esc_html( $order_currency ) ); ?></th>
</tr>
<?php foreach ( $order_items as $order ) : ?>
<tr>
<td style="border-right:1px solid #CCC; border-bottom:1px solid #CCC;">
<h3><?php echo ! empty( $order['label'] ) ? esc_html( $order['label'] ) : esc_html( $order['title'] ?? '' ); ?></h3>
<?php echo ! empty( $order['desc'] ) ? esc_html( $order['desc'] ) : ''; ?>
</td>
<td style="border-bottom:1px solid #CCC;">
<?php echo esc_html( $before . ( $order['price'] ?? 0 ) . $after ); ?>
</td>
</tr>
<?php endforeach; ?>
<tr>
<td style="border-right:1px solid #CCC; text-align:right; vertical-align:middle;">
<?php printf( esc_html__( 'Total amount [%s]', 'directorist' ), esc_html( $order_currency ) ); ?>
</td>
<td>
<?php
$total_amount = apply_filters( 'directorist_email_receipt_order_amount', directorist_order_total_amount( $table_order ), $table_order_id );

echo esc_html( $before . $total_amount . $after );
do_action( 'atbdp_email_receipt_after_total_price', $listing_id );
?>
</td>
</tr>
</table>
<?php
return ob_get_clean();
}

/**
* Register a custom post type "atbdp_orders".
*
Expand Down Expand Up @@ -633,4 +718,4 @@ public function set_payment_receipt_link( $actions, WP_Post $post ) {
unset( $actions['inline hide-if-no-js'] );
return $actions;
}
}
}
Loading