Last active
December 11, 2018 16:16
-
-
Save wpmudev-sls/6b3ab59769f105c9577d70e04e48aa38 to your computer and use it in GitHub Desktop.
[Membership] - Multiple Cancelation emails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Membership] - Multiple Cancelation emails | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Temporary fix for Multiple Cancelation emails | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_MS_Multiple_Cancelation_Emails' ) ) { | |
class WPMUDEV_MS_Multiple_Cancelation_Emails { | |
private static $_instance = null; | |
private static $subscription_id = null; | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_MS_Multiple_Cancelation_Emails(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
add_action( 'ms_model_relationship_check_membership_status_before', array( $this, 'set_subscription' ) ); | |
add_action( 'ms_model_communication_add_to_queue_after', array( $this, 'after_comm_queue_set' ) ); | |
add_action( 'ms_model_communication_after_send_message', array( $this, 'after_comm_sent' ), 20 ,7 ); | |
} | |
public function set_subscription( $subscription ) { | |
self::$subscription_id = $subscription->id; | |
// We need to keep our custom meta. M2 will delete it by default | |
add_filter( 'ms_model_clean_metadata', array( $this, 'keep_comm_meta' ), 20, 3 ); | |
} | |
public function after_comm_sent( $sent, $recipients, $subject, $message, $headers, $comm, $subscription ) { | |
if ( $sent ) { | |
$sent_comms = get_post_meta( $subscription->ID, '_ms_comms_sent', true ); | |
if ( ! $sent_comms || ! is_array( $sent_comms ) ) { | |
$sent_comms = array(); | |
} | |
$sent_comms[] = $comm->type; | |
update_post_meta( $subscription->id, '_ms_comms_sent', $sent_comms ); | |
} | |
} | |
public function after_comm_queue_set( $comm ) { | |
if ( is_null( self::$subscription_id ) ) { | |
return; | |
} | |
$sent_comms = get_post_meta( self::$subscription_id, '_ms_comms_sent', true ); | |
if ( ! $sent_comms || ! is_array( $sent_comms ) ) { | |
$sent_comms = array(); | |
} | |
if ( ! in_array( $comm->type, $sent_comms ) ) { | |
return; | |
} | |
$comm->remove_from_queue( self::$subscription_id ); | |
} | |
public function keep_comm_meta( $remove, $all_fields, $subscription_id ) { | |
// We need to keep the _ms_comms_sent meta, so lets remove it from the ... um ... remove | |
if ( | |
$subscription_id == self::$subscription_id && | |
( $key = array_search( '_ms_comms_sent', $remove ) ) !== false | |
) { | |
unset( $remove[$key] ); | |
} | |
return $remove; | |
} | |
} | |
if ( ! function_exists( 'wpmudev_ms_multiple_cancelation_emails' ) ) { | |
function wpmudev_ms_multiple_cancelation_emails(){ | |
return WPMUDEV_MS_Multiple_Cancelation_Emails::get_instance(); | |
}; | |
add_action( 'plugins_loaded', 'wpmudev_ms_multiple_cancelation_emails', 10 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment