Permalink
Browse files

Initial commit

  • Loading branch information...
robertstaddon committed Oct 12, 2018
0 parents commit 280fb5f390247d704277bc15e59d99ea604cd2a3
Showing with 256 additions and 0 deletions.
  1. +169 −0 buddypress-welcome-email.php
  2. +51 −0 includes/admin/admin.php
  3. +36 −0 includes/sender.php
@@ -0,0 +1,169 @@
<?php
/**
* Plugin Name: BuddyPress Welcome Email
* Description: Add a welcome email to the BuddyPress emails that sends after the user is successfully activated
* Version: 1.0
* Author: Abundant Designs
* Author URI: https://www.abundantdesigns.com
* License: GPLv2 or later
* Text Domain: buddypress-welcome-email
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
// Check if class name already exists
if ( ! class_exists( 'BP_Welcome_Email' ) ) :
/**
* Main class
*/
class BP_Welcome_Email {
/**
* The BP_Welcome_Email instance
*
* @access private
* @var object $instance
*/
private static $instance;
/**
* The BP_Welcome_Email_Admin instance
*
* @access private
* @var object $instance_admin
*/
private static $instance_admin;
/**
* Instantiate the main class
*
* This function instantiates the class, initialize all functions and return the object.
*
* @return object The LearnDash_UpdateNotify instance.
*/
public static function instance() {
if ( ! isset( self::$instance ) && ( ! self::$instance instanceof BP_Welcome_Email ) ) {
self::$instance = new BP_Welcome_Email();
self::$instance->setup_constants();
add_action( 'plugins_loaded', array( self::$instance, 'load_textdomain' ) );
add_action( 'admin_init', array( self::$instance, 'require_dependencies' ) );
self::$instance->includes();
register_activation_hook( BP_WELCOME_EMAIL_FILE, array( self::$instance, 'activate' ) );
}
return self::$instance;
}
/**
* Function for setting up constants
*
* This function is used to set up constants used throughout the plugin.
*/
public function setup_constants() {
// Plugin version
if ( ! defined( 'BP_WELCOME_EMAIL_VERSION' ) ) {
define( 'BP_WELCOME_EMAIL_VERSION', '1.0' );
}
// Plugin file
if ( ! defined( 'BP_WELCOME_EMAIL_FILE' ) ) {
define( 'BP_WELCOME_EMAIL_FILE', __FILE__ );
}
// Plugin folder path
if ( ! defined( 'BP_WELCOME_EMAIL_PLUGIN_PATH' ) ) {
define( 'BP_WELCOME_EMAIL_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
}
// Plugin folder URL
if ( ! defined( 'BP_WELCOME_EMAIL_PLUGIN_URL' ) ) {
define( 'BP_WELCOME_EMAIL_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
}
/**
* Load text domain used for translation
*
* This function loads mo and po files used to translate text strings used throughout the
* plugin.
*/
public function load_textdomain() {
// Set filter for plugin language directory
$lang_dir = dirname( plugin_basename( BP_WELCOME_EMAIL_FILE ) ) . '/languages/';
$lang_dir = apply_filters( 'buddypress_welcome_email_languages_directory', $lang_dir );
// Load plugin translation file
load_plugin_textdomain( 'buddypress-welcome-email', false, $lang_dir );
}
/**
* Require dependencies
*/
public function require_dependencies() {
if ( is_admin() && current_user_can( 'activate_plugins' ) ) {
if( !is_plugin_active( 'buddypress/bp-loader.php' ) ) {
add_action( 'admin_notices', array( $this, 'require_dependency_notice' ) );
deactivate_plugins( plugin_basename( BP_WELCOME_EMAIL_FILE ) );
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
}
}
}
public function require_dependency_notice() {
echo '<div class="error"><p>';
echo __('Sorry, but <strong>BuddyPress Welcome Email</strong> requires <a href="https://buddypress.org/">BuddyPress</a> to be installed and activated.', 'buddypress-welcome-email');
echo '</p></div>';
}
/**
* Includes all necessary PHP files
*
* This function is responsible for including all necessary PHP files.
*/
public function includes() {
if ( is_admin() ) {
include BP_WELCOME_EMAIL_PLUGIN_PATH . '/includes/admin/admin.php';
self::$instance_admin = new BP_Welcome_Email_Admin();
}
include BP_WELCOME_EMAIL_PLUGIN_PATH . '/includes/sender.php';
new BP_Welcome_Email_Sender();
}
/**
* Functions to run on activation
*
*/
public function activate() {
if ( is_object( self::$instance_admin ) )
self::$instance_admin->add_welcome_email();
}
}
endif; // End if class exists check
/**
* The main function for returning instance
*/
function launch_buddypress_welcome_email() {
return BP_Welcome_Email::instance();
}
// Run plugin
launch_buddypress_welcome_email();
@@ -0,0 +1,51 @@
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit();
/**
* BP_Welcome_Email_Admin class
*
*/
class BP_Welcome_Email_Admin {
/**
* Class __construct function
*/
public function __construct() {
add_action( 'bp_core_install_emails', array( $this, 'add_welcome_email' ) );
}
public function add_welcome_email() {
// Do not create if it already exists and is not in the trash
$post_exists = post_exists( '[{{{site.name}}}] Welcome!' );
if ( $post_exists != 0 && get_post_status( $post_exists ) == 'publish' )
return;
// Create post object
$my_post = array(
'post_title' => __( '[{{{site.name}}}] Welcome!', 'buddypress-welcome-email' ),
'post_content' => __( 'Welcome to [{{{site.name}}}]!', 'buddypress-welcome-email' ), // HTML email content.
'post_excerpt' => __( 'Welcome to [{{{site.name}}}]!', 'buddypress-welcome-email' ), // Plain text email content.
'post_status' => 'publish',
'post_type' => bp_get_email_post_type() // this is the post type for emails
);
// Insert the email post into the database
$post_id = wp_insert_post( $my_post );
if ( $post_id ) {
// add our email to the taxonomy term 'activation_completed'
// Email is a custom post type, therefore use wp_set_object_terms
$tt_ids = wp_set_object_terms( $post_id, 'activation_completed', bp_get_email_tax_type() );
foreach ( $tt_ids as $tt_id ) {
$term = get_term_by( 'term_taxonomy_id', (int) $tt_id, bp_get_email_tax_type() );
wp_update_term( (int) $term->term_id, bp_get_email_tax_type(), array(
'description' => 'Recipient has successfully activated an account.',
) );
}
}
}
}
@@ -0,0 +1,36 @@
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit();
/**
* BP_Welcome_Email_Admin class
*
*/
class BP_Welcome_Email_Sender {
/**
* Class __construct function
*/
public function __construct() {
add_action( 'bp_core_activated_user', array( $this, 'send_welcome_email' ) );
}
public function send_welcome_email( $user_id ) {
// add tokens to parse in email
$args = array(
'tokens' => array(
),
);
// add each xprofile field value of the user as an available token (e.g. {{xprofile.1}} for the field_id of "1")
$xprofile_field_ids = bp_xprofile_get_fields_by_visibility_levels( $user_id, array( 'public', 'friends', 'loggedin', 'adminsonly' ) );
foreach( $xprofile_field_ids as $field_id ) {
$token_key = 'xprofile.' . $field_id;
$args['tokens'][$token_key] = xprofile_get_field_data( $field_id, $user_id );
}
// send args and user ID to receive email
bp_send_email( 'activation_completed', $user_id, $args );
}
}

0 comments on commit 280fb5f

Please sign in to comment.