Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panoslyrakis/df0ea026e643c718df5df21348ac282b to your computer and use it in GitHub Desktop.
Save panoslyrakis/df0ea026e643c718df5df21348ac282b to your computer and use it in GitHub Desktop.
Send notification to members when dripped content available
<?php
/**
* Plugin Name: Dripped content notifications
* Version: 1.0
* Description: Send notification to members when dripped content available
* Author: Panos Lyrakis (WPMU DEV)
* Author URI: https://premium.wpmudev.org/profile/panoskatws
* Plugin URI: https://premium.wpmudev.org/project/membership/
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
if( ! class_exists( 'WPMUDEV_MS_Dripped_Content_Notification') ){
class WPMUDEV_MS_Dripped_Content_Notification{
private static $_instance = null;
//Number of days
private static $_closest_range;
public static function get_instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new WPMUDEV_MS_Dripped_Content_Notification();
}
return self::$_instance;
}
private function __construct() {
if( ! class_exists( 'MS_Model_Membership' ) ){
return;
}
self::$_closest_range = apply_filters( 'WPMUDEV_MS_Dripped_Content_Notification/closest_range', 0 );
add_action('init', array( $this, 'shcedule_notifications' ) );
add_action( 'ms_set_dripped_content_items', array( $this, 'set_unsent_dripped_notifications' ), 10 );
add_action( 'ms_send_dripped_content_notification', array( $this, 'send_notifications' ), 10 );
}
public function send_notifications(){
$emails_limit = apply_filters( 'WPMUDEV_MS_Dripped_Content_Notification/emails_limit', 50 );
$emails_count = 0;
$unsent_items = $this->_load_unsent_items();
foreach ( $unsent_items as $user_key => $item ) {
if( $this->send_notification( $item ) ){
unset( $unsent_items[ $user_key ] );
}
$emails_count++;
if( $emails_count == $emails_limit ){
break;
}
}
$this->_update_unsent_notifications( $unsent_items );
return;
}
public function send_notification( $item ){
$user = $item[ 'user' ];
$pages = $item[ 'pages' ];
$subject = apply_filters( 'WPMUDEV_MS_Dripped_Content_Notification/notification_subject', __( 'Brand new stuff to checkout' ) );
$message = $this->get_notification_content( $pages, $user );
$headers = array();
$content_type = apply_filters(
'ms_model_communication_set_html_content_type',
'text/html'
);
$headers[] = sprintf(
'Content-Type: %s; charset="UTF-8"',
$content_type
);
$admin_emails = MS_Model_Member::get_admin_user_emails();
if ( ! empty( $admin_emails[0] ) ) {
$headers[] = sprintf(
'From: %s <%s> ',
get_option( 'blogname' ),
$admin_emails[0]
);
}
$headers = apply_filters(
'ms_model_communication_send_message_headers',
$headers
);
$sent = wp_mail( $user->user_email, $subject, $message, $headers );
if( $sent ){
$user_key = 'dripped_pages_list_sent';
$sent_pages_list = get_user_meta($user->ID, $user_key, true );
$new_sent_pages_list = array_keys( $pages );
if( empty( $sent_pages_list ) ){
$sent_pages_list = $new_sent_pages_list;
}
else{
$sent_pages_list = array_merge( $sent_pages_list, $new_sent_pages_list );
}
update_user_meta($user->ID, $user_key, $sent_pages_list );
}
return $sent;
}
public function get_notification_content( $pages, $user ){
$content = __( 'Hi there ' ) . $user->display_name . ',<br />';
$content .= __( 'For your eyes only' ) . '<br />';
foreach( $pages as $page ){
$content .= '<p>';
$content .= '<h4>' . $page[ 'page_title' ] . '</h4>';
$content .= $page[ 'page_excerpt' ] . '<a href="' . $page[ 'page_link' ] . '">' . __( 'More' ) . '</a>';
$content .= '</p>';
}
return apply_filters( 'WPMUDEV_MS_Dripped_Content_Notification/notification_content', $content, $pages, $user );
}
public function set_unsent_dripped_notifications(){
//delete_option( 'ms_unsent_dripped_items_notifications' );
$unsent_items = $this->_load_unsent_items();
$dripped_items = $this->_get_unsent_notifications();
if( is_array( $dripped_items ) && ! empty( $dripped_items ) ){
$dripped_items = array_merge( $dripped_items, $unsent_items );
}
else{
$dripped_items = $unsent_items;
}
$this->_update_unsent_notifications( $dripped_items );
}
private function _update_unsent_notifications( $dripped_items ){
update_option( 'ms_unsent_dripped_items_notifications', '' );
//wp_cache_set( 'ms_unsent_dripped_items_notifications', $dripped_items );
}
private function _get_unsent_notifications(){
//$dripped_items = wp_cache_get( 'ms_unsent_dripped_items_notifications' );
//if( ! $dripped_items ){
$dripped_items = get_option( 'ms_unsent_dripped_items_notifications' );
//wp_cache_set( 'ms_unsent_dripped_items_notifications', $dripped_items );
//}
return $dripped_items;
}
private function _load_unsent_items(){
$memberships = MS_Model_Membership::get_memberships();
if( empty( $memberships ) ){
return;
}
$unsent_items = array();
foreach( $memberships as $membership ){
if( $membership->type == 'dripped' ){
$members = $membership->get_members();
if( empty( $members ) ){
continue;
}
$rules = $membership->_rules;
$dripped_pages = $rules['page'];
foreach( $dripped_pages->dripped as $page_id => $rule ){
foreach ( $members as $member ) {
if( $this->_ok_to_add_rule_to_member_list( $rule, $member, $membership->id ) && $this->_ok_to_send_page( $page_id, $member->id ) ){
$member_id = $member->id;
$user = $member->wp_user;
if( ! isset( $unsent_items[ 'user-' . $member_id ][ 'user' ] ) ){
$unsent_items[ 'user-' . $member_id ][ 'user' ] = $user->data;
}
$unsent_items[ 'user-' . $member_id ][ 'pages' ][ $page_id ] = array(
'page_title' => get_the_title( $page_id ),
'page_excerpt' => $this->get_page_description( $page_id ),
'page_link' => get_permalink( $page_id )
);
}
}
}
}
}
return $unsent_items;
}
public function get_page_description( $page_id ){
$excerpt = get_the_excerpt( $page_id );
if( $excerpt != '' ){
return $excerpt;
}
$page = get_post( $page_id );
$description_words_count = apply_filters( 'WPMUDEV_MS_Dripped_Content_Notification/page_description_words_count', 50 );
return wp_trim_words( strip_tags( $page->post_content ), $description_words_count );
//return apply_filters( 'the_content', wp_trim_words( strip_tags( $page->post_content ), $description_words_count ) );
}
private function _ok_to_add_rule_to_member_list( $rule, $member, $membership_id ){
$today_start = new DateTime( date('Y-m-d', time() ) . '00:00:00' );
switch( $rule['type'] ){
case 'specific_date':
$page_start = new DateTime( $rule['date'] . '00:00:00' );
$days_diff = $page_start->diff( $today_start );
if( $days_diff->days <= self::$_closest_range ){
return true;
}
break;
case 'from_registration':
$subscription = $member->get_subscription( $membership_id );
$subscription_start = new DateTime( $subscription->start_date . '00:00:00' );
$days_diff = $subscription_start->diff( $today_start );
if( $days_diff->days <= self::$_closest_range ){
return true;
}
break;
case 'instantly':
return true;
break;
}
return false;
}
private function _ok_to_send_page( $page_id, $user_id ){
//$page_sent = get_post_meta( $page_id, 'ms_dripped_item_notified', true );
$user_key = 'dripped_pages_list_sent';
$sent_pages_list = get_user_meta( $user_id, $user_key, true );
if( is_array( $sent_pages_list ) && in_array( $page_id, $sent_pages_list ) ){
return false;
}
return true;
}
public function shcedule_notifications(){
$setter_interval = apply_filters( 'WPMUDEV_MS_Dripped_Content_Notification/setter_interval', 'daily' );
$sender_interval = apply_filters( 'WPMUDEV_MS_Dripped_Content_Notification/sender_interval', 'hourly' );
if( !wp_next_scheduled( 'ms_set_dripped_content_items' ) ) {
wp_schedule_event( time(), $setter_interval, 'ms_set_dripped_content_items' );
}
if( !wp_next_scheduled( 'ms_send_dripped_content_notification' ) ) {
wp_schedule_event( time(), $sender_interval, 'ms_send_dripped_content_notification' );
}
}
}
add_action( 'plugins_loaded', function(){
$GLOBALS['WPMUDEV_MS_Dripped_Content_Notification'] = WPMUDEV_MS_Dripped_Content_Notification::get_instance();
} );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment