| 1 |
<?php |
| 2 |
/** |
| 3 |
* Email Notification Util |
| 4 |
* |
| 5 |
* This class contains helper functions for email notification. |
| 6 |
* |
| 7 |
* @package Give |
| 8 |
* @subpackage Classes/Emails |
| 9 |
* @copyright Copyright (c) 2016, GiveWP |
| 10 |
* @license https://opensource.org/licenses/gpl-license GNU Public License |
| 11 |
* @since 2.0 |
| 12 |
*/ |
| 13 |
|
| 14 |
// Exit if access directly. |
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
class Give_Email_Notification_Util { |
| 21 |
/** |
| 22 |
* Instance. |
| 23 |
* |
| 24 |
* @since 2.0 |
| 25 |
* @access static |
| 26 |
* @var |
| 27 |
*/ |
| 28 |
private static $instance; |
| 29 |
|
| 30 |
/** |
| 31 |
* Singleton pattern. |
| 32 |
* |
| 33 |
* @since 2.0 |
| 34 |
* @access private |
| 35 |
* Give_Email_Notification_Util constructor. |
| 36 |
*/ |
| 37 |
private function __construct() { |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* Get instance. |
| 43 |
* |
| 44 |
* @since 2.0 |
| 45 |
* @access static |
| 46 |
* @return static |
| 47 |
*/ |
| 48 |
static function get_instance() { |
| 49 |
if ( null === static::$instance ) { |
| 50 |
self::$instance = new static(); |
| 51 |
} |
| 52 |
|
| 53 |
return self::$instance; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
/** |
| 58 |
* Check if notification has preview field or not. |
| 59 |
* |
| 60 |
* @since 2.0 |
| 61 |
* @access public |
| 62 |
* |
| 63 |
* @param Give_Email_Notification $email |
| 64 |
* |
| 65 |
* @return bool |
| 66 |
*/ |
| 67 |
public static function has_preview( Give_Email_Notification $email ) { |
| 68 |
return $email->config['has_preview']; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Check if notification has recipient field or not. |
| 73 |
* |
| 74 |
* @since 2.0 |
| 75 |
* @access public |
| 76 |
* |
| 77 |
* @param Give_Email_Notification $email |
| 78 |
* |
| 79 |
* @return bool |
| 80 |
*/ |
| 81 |
public static function has_recipient_field( Give_Email_Notification $email ) { |
| 82 |
return $email->config['has_recipient_field']; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Check if admin can edit notification status or not. |
| 87 |
* |
| 88 |
* @since 2.0 |
| 89 |
* @access public |
| 90 |
* |
| 91 |
* @param Give_Email_Notification $email |
| 92 |
* |
| 93 |
* @return bool |
| 94 |
*/ |
| 95 |
public static function is_notification_status_editable( Give_Email_Notification $email ) { |
| 96 |
$user_can_edit = $email->config['notification_status_editable']; |
| 97 |
|
| 98 |
return (bool) $user_can_edit; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Check if admin can edit notification status or not. |
| 103 |
* |
| 104 |
* @since 2.0 |
| 105 |
* @access public |
| 106 |
* |
| 107 |
* @param Give_Email_Notification $email |
| 108 |
* |
| 109 |
* @return bool |
| 110 |
*/ |
| 111 |
public static function is_content_type_editable( Give_Email_Notification $email ) { |
| 112 |
return $email->config['content_type_editable']; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Check email preview header active or not. |
| 117 |
* |
| 118 |
* @since 2.0 |
| 119 |
* @access public |
| 120 |
* |
| 121 |
* @param Give_Email_Notification $email |
| 122 |
* |
| 123 |
* @return bool |
| 124 |
*/ |
| 125 |
public static function is_email_preview_has_header( Give_Email_Notification $email ) { |
| 126 |
return $email->config['has_preview_header']; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Check email preview header active or not. |
| 131 |
* |
| 132 |
* @since 2.0 |
| 133 |
* @access public |
| 134 |
* |
| 135 |
* @param Give_Email_Notification $email |
| 136 |
* |
| 137 |
* @return bool |
| 138 |
*/ |
| 139 |
public static function is_email_preview( Give_Email_Notification $email ) { |
| 140 |
return $email->config['has_preview']; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Check if email notification setting appear on emails setting page or not. |
| 145 |
* |
| 146 |
* @since 2.0 |
| 147 |
* @access public |
| 148 |
* |
| 149 |
* @param Give_Email_Notification $email |
| 150 |
* |
| 151 |
* @return bool |
| 152 |
*/ |
| 153 |
public static function is_show_on_emails_setting_page( Give_Email_Notification $email ) { |
| 154 |
return $email->config['show_on_emails_setting_page']; |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Check if we can use form email options. |
| 159 |
* |
| 160 |
* @since 2.0 |
| 161 |
* @access public |
| 162 |
* |
| 163 |
* @param Give_Email_Notification $email |
| 164 |
* @param int $form_id |
| 165 |
* |
| 166 |
* @return bool |
| 167 |
*/ |
| 168 |
public static function can_use_form_email_options( Give_Email_Notification $email, $form_id = null ) { |
| 169 |
return give_is_setting_enabled( give_get_meta( $form_id, '_give_email_options', true ) ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Check email active or not. |
| 174 |
* |
| 175 |
* @since 2.0 |
| 176 |
* @access public |
| 177 |
* |
| 178 |
* @param Give_Email_Notification $email |
| 179 |
* @param int $form_id |
| 180 |
* |
| 181 |
* @return string |
| 182 |
*/ |
| 183 |
public static function is_email_notification_active( Give_Email_Notification $email, $form_id = null ) { |
| 184 |
$notification_status = $email->get_notification_status( $form_id ); |
| 185 |
|
| 186 |
$notification_status = empty( $form_id ) |
| 187 |
? give_is_setting_enabled( $notification_status ) |
| 188 |
: give_is_setting_enabled( give_get_option( "{$email->config['id']}_notification", $email->config['notification_status'] ) ) && give_is_setting_enabled( $notification_status, array( 'enabled', 'global' ) ); |
| 189 |
// To check if email notification is active or not on a per-form basis, email notification must be globally active—otherwise it will be considered disabled. |
| 190 |
|
| 191 |
/** |
| 192 |
* Filter to modify is email active notification |
| 193 |
* |
| 194 |
* @since 2.1.3 |
| 195 |
* |
| 196 |
* @param bool $notification_status True if notification is enable and false when disable |
| 197 |
* @param Give_Email_Notification $email Class instances Give_Email_Notification. |
| 198 |
* @param int $form_id Donation Form ID. |
| 199 |
* |
| 200 |
* @param bool $notification_status True if notification is enable and false when disable |
| 201 |
*/ |
| 202 |
return apply_filters( "give_{$email->config['id']}_is_email_notification_active", $notification_status, $email, $form_id ); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Check if admin preview email or not |
| 207 |
* |
| 208 |
* @since 2.0 |
| 209 |
* @access public |
| 210 |
* @return bool $is_preview |
| 211 |
*/ |
| 212 |
public static function can_preview_email() { |
| 213 |
$is_preview = false; |
| 214 |
|
| 215 |
if ( |
| 216 |
current_user_can( 'manage_give_settings' ) |
| 217 |
&& ! empty( $_GET['give_action'] ) |
| 218 |
&& 'preview_email' === $_GET['give_action'] |
| 219 |
) { |
| 220 |
$is_preview = true; |
| 221 |
} |
| 222 |
|
| 223 |
return $is_preview; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Check if admin preview email or not |
| 228 |
* |
| 229 |
* @since 2.0 |
| 230 |
* @access public |
| 231 |
* @return bool $is_preview |
| 232 |
*/ |
| 233 |
public static function can_send_preview_email() { |
| 234 |
$is_preview = false; |
| 235 |
|
| 236 |
if ( |
| 237 |
current_user_can( 'manage_give_settings' ) |
| 238 |
&& ! empty( $_GET['give_action'] ) |
| 239 |
&& 'send_preview_email' === $_GET['give_action'] |
| 240 |
) { |
| 241 |
$is_preview = true; |
| 242 |
} |
| 243 |
|
| 244 |
return $is_preview; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
/** |
| 249 |
* Get formatted text for email content type. |
| 250 |
* |
| 251 |
* @since 2.0 |
| 252 |
* @access public |
| 253 |
* |
| 254 |
* @param string $content_type |
| 255 |
* |
| 256 |
* @return string |
| 257 |
*/ |
| 258 |
public static function get_formatted_email_type( $content_type ) { |
| 259 |
$email_contents = array( |
| 260 |
'text/html' => __( 'HTML', 'give' ), |
| 261 |
'text/plain' => __( 'Plain', 'give' ), |
| 262 |
); |
| 263 |
|
| 264 |
return $email_contents[ $content_type ]; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
/** |
| 269 |
* Get email notification option value. |
| 270 |
* |
| 271 |
* @since 2.0 |
| 272 |
* @access public |
| 273 |
* |
| 274 |
* @param Give_Email_Notification $email |
| 275 |
* @param string $option_name |
| 276 |
* @param int $form_id |
| 277 |
* @param mixed $default |
| 278 |
* |
| 279 |
* @return mixed |
| 280 |
*/ |
| 281 |
public static function get_value( Give_Email_Notification $email, $option_name, $form_id = null, $default = false ) { |
| 282 |
// If form id set then option name can be contain _give_ prefix which is only used for meta key, |
| 283 |
// So make sure you are using correct option name. |
| 284 |
$global_option_name = ( 0 === strpos( $option_name, '_give_' ) |
| 285 |
? str_replace( '_give_', '', $option_name ) |
| 286 |
: $option_name ); |
| 287 |
$option_value = give_get_option( $global_option_name, $default ); |
| 288 |
|
| 289 |
// Ensure that emails sent to donors can be translated using WPML |
| 290 |
// Registering only the emails sent to the donors. |
| 291 |
if ( defined( 'ICL_SITEPRESS_VERSION' ) && ! is_array( $option_value ) ) { |
| 292 |
$option_value = apply_filters( 'wpml_translate_single_string', $option_value, 'admin_texts_give_settings', '[give_settings]' . $global_option_name ); |
| 293 |
} |
| 294 |
|
| 295 |
if ( |
| 296 |
! empty( $form_id ) |
| 297 |
&& give_is_setting_enabled( |
| 298 |
give_get_meta( |
| 299 |
$form_id, |
| 300 |
Give_Email_Setting_Field::get_prefix( $email, $form_id ) . 'notification', |
| 301 |
true, |
| 302 |
'global' |
| 303 |
) |
| 304 |
) |
| 305 |
) { |
| 306 |
$option_value = get_post_meta( $form_id, $option_name, true ); |
| 307 |
|
| 308 |
// Get only email field value from recipients setting. |
| 309 |
if ( Give_Email_Setting_Field::get_prefix( $email, $form_id ) . 'recipient' === $option_name ) { |
| 310 |
$option_value = wp_list_pluck( $option_value, 'email' ); |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
$option_value = empty( $option_value ) ? $default : $option_value; |
| 315 |
|
| 316 |
/** |
| 317 |
* Filter the setting value |
| 318 |
* |
| 319 |
* @since 2.0 |
| 320 |
*/ |
| 321 |
return apply_filters( 'give_email_setting_value', $option_value, $option_name, $email, $form_id, $default ); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
/** |
| 326 |
* Get email logo. |
| 327 |
* |
| 328 |
* @since 2.1.5 |
| 329 |
* |
| 330 |
* @access public |
| 331 |
* |
| 332 |
* @param integer $form_id FOrm ID. |
| 333 |
* |
| 334 |
* @return string |
| 335 |
*/ |
| 336 |
public static function get_email_logo( $form_id ) { |
| 337 |
|
| 338 |
// Email logo tag. |
| 339 |
$header_img = $form_id && give_is_setting_enabled( give_get_meta( $form_id, '_give_email_options', true ) ) |
| 340 |
? give_get_meta( $form_id, '_give_email_logo', true ) |
| 341 |
: give_get_option( 'email_logo', '' ); |
| 342 |
|
| 343 |
return $header_img; |
| 344 |
} |
| 345 |
} |
| 346 |
|