| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base class of LearnPress emails and helper functions. |
| 4 |
* |
| 5 |
* @author ThimPress |
| 6 |
* @package LearnPress/Classes |
| 7 |
* @version 3.0.1 |
| 8 |
* @editor tungnx |
| 9 |
* @modify 4.1.3 |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Prevent loading this file directly |
| 14 |
*/ |
| 15 |
defined( 'ABSPATH' ) || exit(); |
| 16 |
|
| 17 |
if ( ! class_exists( 'LP_Emails' ) ) { |
| 18 |
/** |
| 19 |
* Class LP_Emails |
| 20 |
*/ |
| 21 |
class LP_Emails { |
| 22 |
|
| 23 |
/** |
| 24 |
* List of all email actions. |
| 25 |
* |
| 26 |
* @var array |
| 27 |
*/ |
| 28 |
public $emails = array(); |
| 29 |
|
| 30 |
/** |
| 31 |
* The single instance of the class |
| 32 |
* |
| 33 |
* @var LP_Emails |
| 34 |
*/ |
| 35 |
protected static $_instance = null; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var LP_Email |
| 39 |
*/ |
| 40 |
protected $_current = null; |
| 41 |
|
| 42 |
/** |
| 43 |
* @var null |
| 44 |
*/ |
| 45 |
protected $_last_current = null; |
| 46 |
|
| 47 |
/** |
| 48 |
* LP_Emails constructor. |
| 49 |
*/ |
| 50 |
protected function __construct() { |
| 51 |
if ( did_action( 'learn-press/emails-init' ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
include_once LP_PLUGIN_PATH . 'inc/emails/class-lp-email.php'; |
| 55 |
|
| 56 |
$this->register_emails(); |
| 57 |
|
| 58 |
do_action( 'learn-press/emails-init', $this ); |
| 59 |
} |
| 60 |
|
| 61 |
public function register_emails() { |
| 62 |
include_once 'emails/types/class-lp-email-type-order.php'; |
| 63 |
include_once 'emails/types/class-lp-email-type-order-student.php'; |
| 64 |
include_once 'emails/types/class-lp-email-type-order-guest.php'; |
| 65 |
include_once 'emails/types/class-lp-email-type-order-admin.php'; |
| 66 |
include_once 'emails/types/class-lp-email-type-order-instructor.php'; |
| 67 |
include_once 'emails/types/class-lp-email-type-enrolled-course.php'; |
| 68 |
include_once 'emails/types/class-lp-email-type-finished-course.php'; |
| 69 |
include_once 'emails/types/class-lp-email-type-become-an-instructor.php'; |
| 70 |
|
| 71 |
// New order |
| 72 |
$this->emails['LP_Email_New_Order_Admin'] = include_once 'emails/admin/class-lp-email-new-order-admin.php'; |
| 73 |
$this->emails['LP_Email_New_Order_User'] = include_once 'emails/student/class-lp-email-new-order-user.php'; |
| 74 |
$this->emails['LP_Email_New_Order_Instructor'] = include_once 'emails/instructor/class-lp-email-new-order-instructor.php'; |
| 75 |
$this->emails['LP_Email_New_Order_Guest'] = include_once 'emails/guest/class-lp-email-new-order-guest.php'; |
| 76 |
|
| 77 |
// Processing order |
| 78 |
$this->emails['LP_Email_Processing_Order_User'] = include_once 'emails/student/class-lp-email-processing-order-user.php'; |
| 79 |
$this->emails['LP_Email_Processing_Order_Guest'] = include_once 'emails/guest/class-lp-email-processing-order-guest.php'; |
| 80 |
|
| 81 |
// Completed order |
| 82 |
$this->emails['LP_Email_Completed_Order_Admin'] = include_once 'emails/admin/class-lp-email-completed-order-admin.php'; |
| 83 |
$this->emails['LP_Email_Completed_Order_User'] = include_once 'emails/student/class-lp-email-completed-order-user.php'; |
| 84 |
$this->emails['LP_Email_Completed_Order_Guest'] = include_once 'emails/guest/class-lp-email-completed-order-guest.php'; |
| 85 |
|
| 86 |
// Cancelled order |
| 87 |
$this->emails['LP_Email_Cancelled_Order_Admin'] = include_once 'emails/admin/class-lp-email-cancelled-order-admin.php'; |
| 88 |
$this->emails['LP_Email_Cancelled_Order_Instructor'] = include_once 'emails/instructor/class-lp-email-cancelled-order-instructor.php'; |
| 89 |
$this->emails['LP_Email_Cancelled_Order_User'] = include_once 'emails/student/class-lp-email-cancelled-order-user.php'; |
| 90 |
$this->emails['LP_Email_Cancelled_Order_Guest'] = include_once 'emails/guest/class-lp-email-cancelled-order-guest.php'; |
| 91 |
|
| 92 |
// Enrolled course |
| 93 |
$this->emails['LP_Email_Enrolled_Course_Admin'] = include_once 'emails/admin/class-lp-email-enrolled-course-admin.php'; |
| 94 |
$this->emails['LP_Email_Enrolled_Course_Instructor'] = include_once 'emails/instructor/class-lp-email-enrolled-course-instructor.php'; |
| 95 |
$this->emails['LP_Email_Enrolled_Course_User'] = include_once 'emails/student/class-lp-email-enrolled-course-user.php'; |
| 96 |
|
| 97 |
// Finished course |
| 98 |
$this->emails['LP_Email_Finished_Course_Admin'] = include_once 'emails/admin/class-lp-email-finished-course-admin.php'; |
| 99 |
$this->emails['LP_Email_Finished_Course_Instructor'] = include_once 'emails/instructor/class-lp-email-finished-course-instructor.php'; |
| 100 |
$this->emails['LP_Email_Finished_Course_User'] = include_once 'emails/student/class-lp-email-finished-course-user.php'; |
| 101 |
|
| 102 |
// Become An Instructor |
| 103 |
$this->emails['LP_Email_Become_An_Instructor'] = include_once 'emails/admin/class-lp-email-become-an-instructor.php'; |
| 104 |
$this->emails['LP_Email_Instructor_Accepted'] = include_once 'emails/instructor/class-lp-email-instructor-accepted.php'; |
| 105 |
$this->emails['LP_Email_Instructor_Denied'] = include_once 'emails/instructor/class-lp-email-instructor-denied.php'; |
| 106 |
|
| 107 |
// Forgot Password |
| 108 |
$this->emails['LP_Email_Reset_Password'] = include_once 'emails/types/class-lp-email-reset-password.php'; |
| 109 |
|
| 110 |
do_action( 'learnpress/emails/register', $this->emails ); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
/** |
| 115 |
* @version 1.0 |
| 116 |
*/ |
| 117 |
public function __clone() { |
| 118 |
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'learnpress' ), '1.0' ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @version 1.0 |
| 123 |
*/ |
| 124 |
public function __wakeup() { |
| 125 |
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'learnpress' ), '1.0' ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Init email notification hooks. |
| 130 |
* |
| 131 |
* @since 3.0.0 |
| 132 |
* @editor tungnx |
| 133 |
* @reason comment - not use |
| 134 |
*/ |
| 135 |
/* |
| 136 |
public static function init_email_notifications() { |
| 137 |
self::instance(); |
| 138 |
}*/ |
| 139 |
|
| 140 |
/** |
| 141 |
* Push email notification into queue. |
| 142 |
* |
| 143 |
* @since 3.0.0 |
| 144 |
*/ |
| 145 |
/* |
| 146 |
public static function queue_email() { |
| 147 |
$data_queue = array( |
| 148 |
'filter' => current_filter(), |
| 149 |
'args' => func_get_args(), |
| 150 |
); |
| 151 |
LP()->background( 'emailer' )->push_to_queue( $data_queue ); |
| 152 |
}*/ |
| 153 |
|
| 154 |
/** |
| 155 |
* Send email notification. |
| 156 |
* |
| 157 |
* @editor tungnx |
| 158 |
* @reason not use |
| 159 |
* @deprecated 4.1.1 |
| 160 |
*/ |
| 161 |
/* |
| 162 |
public static function send_email() { |
| 163 |
try { |
| 164 |
$args = func_get_args(); |
| 165 |
self::instance(); |
| 166 |
do_action_ref_array( current_filter() . '/notification', $args ); |
| 167 |
} catch ( Exception $e ) { |
| 168 |
} |
| 169 |
}*/ |
| 170 |
|
| 171 |
/** |
| 172 |
* Email header. |
| 173 |
* |
| 174 |
* @param string $heading |
| 175 |
* @param bool $echo |
| 176 |
* |
| 177 |
* @return string |
| 178 |
* @editor tungnx |
| 179 |
* @modify 4.1.4 comment - not used |
| 180 |
*/ |
| 181 |
/*public function email_header( string $heading, bool $echo = true ): string { |
| 182 |
ob_start(); |
| 183 |
learn_press_get_template( 'emails/email-header.php', array( 'email_heading' => $heading ) ); |
| 184 |
$header = ob_get_clean(); |
| 185 |
if ( $echo ) { |
| 186 |
echo $header; |
| 187 |
} |
| 188 |
|
| 189 |
return $header; |
| 190 |
}*/ |
| 191 |
|
| 192 |
/** |
| 193 |
* Email footer. |
| 194 |
* |
| 195 |
* @param string $footer_text |
| 196 |
* @param bool $echo |
| 197 |
* |
| 198 |
* @return string |
| 199 |
* @editor tungnx |
| 200 |
* @modify 4.1.4 comment - not used |
| 201 |
*/ |
| 202 |
/*public function email_footer( string $footer_text, bool $echo = true ): string { |
| 203 |
ob_start(); |
| 204 |
learn_press_get_template( 'emails/email-footer.php', array( 'footer_text' => $footer_text ) ); |
| 205 |
$footer = ob_get_clean(); |
| 206 |
if ( $echo ) { |
| 207 |
echo $footer; |
| 208 |
} |
| 209 |
|
| 210 |
return $footer; |
| 211 |
}*/ |
| 212 |
|
| 213 |
public function set_current( $id ) { |
| 214 |
$this->_last_current = $this->_current; |
| 215 |
|
| 216 |
if ( $id instanceof LP_Email ) { |
| 217 |
$this->_current = $id->id; |
| 218 |
} else { |
| 219 |
$this->_current = $id; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* @return bool|LP_Email |
| 225 |
*/ |
| 226 |
public function get_current() { |
| 227 |
return self::get_email( $this->_current ); |
| 228 |
} |
| 229 |
|
| 230 |
public function reset_current() { |
| 231 |
$this->_current = $this->_last_current; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* @param string $id |
| 236 |
* |
| 237 |
* @return LP_Email|bool |
| 238 |
*/ |
| 239 |
public static function get_email( $id ) { |
| 240 |
static $emails = array(); |
| 241 |
|
| 242 |
if ( ! $emails || empty( $emails[ $id ] ) ) { |
| 243 |
foreach ( self::instance()->emails as $class => $email ) { |
| 244 |
$emails[ $email->id ] = $class; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
return ! empty( $emails[ $id ] ) ? self::instance()->emails[ $emails[ $id ] ] : false; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Get image header in general settings. |
| 253 |
* |
| 254 |
* @return string |
| 255 |
*/ |
| 256 |
public function get_image_header() { |
| 257 |
$image = LP_Settings::instance()->get( 'emails_general.header_image' ); |
| 258 |
|
| 259 |
if ( ! empty( $image ) ) { |
| 260 |
$image = wp_get_attachment_image_url( $image, 'full' ); |
| 261 |
} |
| 262 |
|
| 263 |
return $image; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Main LP_Mail Instance, ensures only one instance of LP_Mail is loaded or can be loaded. |
| 268 |
* |
| 269 |
* @since 3.0.0 |
| 270 |
* |
| 271 |
* @return LP_Emails |
| 272 |
*/ |
| 273 |
public static function instance() { |
| 274 |
|
| 275 |
if ( is_null( self::$_instance ) ) { |
| 276 |
self::$_instance = new self(); |
| 277 |
} |
| 278 |
|
| 279 |
return self::$_instance; |
| 280 |
} |
| 281 |
|
| 282 |
} |
| 283 |
|
| 284 |
} |
| 285 |
|