HTML
1 year ago
Jobs
2 days ago
views
2 days ago
ActivityReport.php
2 days ago
Apply.php
6 months ago
Cron.php
1 year ago
CronJob.php
2 days ago
CronJobs.php
2 days ago
Crypt.php
2 months ago
DownloadStats.php
5 months ago
Email.php
2 days ago
EmailCron.php
1 year ago
FileSystem.php
1 year ago
Installer.php
2 days ago
Messages.php
1 year ago
Query.php
5 months ago
Session.php
1 week ago
Settings.php
5 years ago
SimpleMath.php
5 years ago
TempStorage.php
1 week ago
Template.php
5 months ago
UI.php
7 months ago
Updater.php
4 years ago
UserAgent.php
2 years ago
__.php
2 months ago
__MailUI.php
3 years ago
Email.php
583 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Email Handler Class for WordPress Download Manager Pro |
| 4 | * Since: v4.6.0 |
| 5 | * Author: Shahjada |
| 6 | * Version: 2.0.2 |
| 7 | */ |
| 8 | namespace WPDM\__; |
| 9 | |
| 10 | class Email { |
| 11 | |
| 12 | public $_to; |
| 13 | public $cc; |
| 14 | public $bcc; |
| 15 | public $_from; |
| 16 | public $fromName; |
| 17 | public $_subject; |
| 18 | public $_message; |
| 19 | public $_template; |
| 20 | public $_attachments; |
| 21 | public $_params = []; |
| 22 | |
| 23 | var $email_hooks = [ |
| 24 | 'wpdm_before_email_download_link' => [ |
| 25 | 'title' => 'Before email download link', |
| 26 | 'params' => 2 |
| 27 | ], |
| 28 | 'wpdm_onstart_download' => [ |
| 29 | 'title' => 'Just before download starts', |
| 30 | 'params' => 1 |
| 31 | ], |
| 32 | 'create_package_frontend' => [ |
| 33 | 'title' => 'Create new package from front-end', |
| 34 | 'params' => 2 |
| 35 | ], |
| 36 | 'edit_package_frontend' => [ |
| 37 | 'title' => 'Update a package from frontend', |
| 38 | 'params' => 2 |
| 39 | ], |
| 40 | 'wpdm_after_checkout' => [ |
| 41 | 'title' => 'After a successful checkout', |
| 42 | 'params' => 2 |
| 43 | ], |
| 44 | ]; |
| 45 | |
| 46 | public $templateDir; |
| 47 | |
| 48 | function __construct() { |
| 49 | $this->templateDir = __DIR__.'/views/email-templates/'; |
| 50 | } |
| 51 | |
| 52 | function to($to, $cc = '', $bcc = '') |
| 53 | { |
| 54 | $this->_to = $to; |
| 55 | $this->_params['to_email'] = $to; |
| 56 | $this->cc = $cc; |
| 57 | $this->_params['cc'] = $cc; |
| 58 | $this->bcc = $bcc; |
| 59 | $this->_params['bcc'] = $bcc; |
| 60 | return $this; |
| 61 | } |
| 62 | |
| 63 | function from($email, $name = '') |
| 64 | { |
| 65 | $this->_from = $email; |
| 66 | $this->_params['from_email'] = $email; |
| 67 | $this->fromName = $name; |
| 68 | $this->_params['from_name'] = $name; |
| 69 | return $this; |
| 70 | } |
| 71 | |
| 72 | function subject($subject) |
| 73 | { |
| 74 | $this->_subject = $subject; |
| 75 | $this->_params['subject'] = $subject; |
| 76 | return $this; |
| 77 | } |
| 78 | function message($message) |
| 79 | { |
| 80 | $this->_message = $message; |
| 81 | $this->_params['message'] = $message; |
| 82 | return $this; |
| 83 | } |
| 84 | function attachments($attachments) |
| 85 | { |
| 86 | $this->_attachments = $attachments; |
| 87 | return $this; |
| 88 | } |
| 89 | |
| 90 | function template($template) |
| 91 | { |
| 92 | $this->_template = $template; |
| 93 | return $this; |
| 94 | } |
| 95 | |
| 96 | function params($params) |
| 97 | { |
| 98 | foreach ($params as $key => $val) { |
| 99 | $this->_params[$key] = $val; |
| 100 | } |
| 101 | return $this; |
| 102 | } |
| 103 | |
| 104 | function getStatus($id = '') { |
| 105 | $status = get_option("_fm_email_template_status"); |
| 106 | if(!is_array($status)) { |
| 107 | $templates = self::templates(); |
| 108 | $templates = array_keys($templates); |
| 109 | $status = array_combine($templates, array_pad([], count($templates), 1)); |
| 110 | } |
| 111 | |
| 112 | return $id ? ( isset($status[$id]) ? (int)$status[$id] : 1 ) : $status; |
| 113 | } |
| 114 | |
| 115 | public static function templates() { |
| 116 | $admin_email = get_option( 'admin_email' ); |
| 117 | $sitename = get_option( "blogname" ); |
| 118 | $templates = array( |
| 119 | 'default' => array( |
| 120 | 'label' => __( "General Email Template" , "download-manager" ), |
| 121 | 'for' => 'varies', |
| 122 | 'default' => array( 'subject' => '{{subject}}', |
| 123 | 'from_name' => get_option('blogname'), |
| 124 | 'from_email' => $admin_email, |
| 125 | 'message' => '{{message}}</b><br/><br/>Best Regards,<br/>Support Team<br/><b><a href="{{homeurl}}">{{sitename}}</a></b>' |
| 126 | ) |
| 127 | ), |
| 128 | 'user-signup' => array( |
| 129 | 'label' => __( "User Signup Notification" , "download-manager" ), |
| 130 | 'for' => 'customer', |
| 131 | 'default' => array( |
| 132 | 'subject' => sprintf( __( "Welcome to %s" , "download-manager" ), $sitename ), |
| 133 | 'from_name' => get_option( 'blogname' ), |
| 134 | 'from_email' => $admin_email, |
| 135 | 'message' => '<h3>Welcome to {{sitename}}</h3>Hello {{first_name}},<br/>Thanks for registering to {{sitename}}. For the record, here is your login info again:<br/>Username: {{username}}<br/>Password: {{password}}<br/><b>Login URL: <a href="{{loginurl}}">{{loginurl}}</a></b><br/><br/>Best Regards,<br/>Support Team<br/><b><a href="{{homeurl}}">{{sitename}}</a></b>' |
| 136 | ) |
| 137 | ), |
| 138 | 'user-signup-admin' => array( |
| 139 | 'label' => __( "User Signup Notification" , "download-manager" ), |
| 140 | 'for' => 'admin', |
| 141 | 'default' => array( |
| 142 | 'subject' => sprintf( __( "[ %s ] New User Registration" , "download-manager" ), $sitename ), |
| 143 | 'from_name' => get_option( 'blogname' ), |
| 144 | 'from_email' => $admin_email, |
| 145 | 'to_email' => $admin_email, |
| 146 | 'message' => __( "New user registration on your site WordPress Download Manager:" , "download-manager" ).'<hr/>Username: {{username}}<br/>Email: {{email}}<br/>IP: {{user_ip}}<hr/>{{edit_user_btn}}<br/><br/>Best Regards,<br/>Support Team<br/><b><a href="{{homeurl}}">{{sitename}}</a></b>' |
| 147 | ) |
| 148 | ), |
| 149 | 'user-signup-approved' => array( |
| 150 | 'label' => __( "User Signup Approved" , "download-manager" ), |
| 151 | 'for' => 'customer', |
| 152 | 'default' => array( |
| 153 | 'subject' => __( "Congratulation! Your signup request is approved" , "download-manager" ), |
| 154 | 'from_name' => get_option( 'blogname' ), |
| 155 | 'from_email' => $admin_email, |
| 156 | 'message' => '<h3>Welcome to {{sitename}}</h3>Hello {{first_name}},<br/>Congratulation!! Your signup request is approved! <br/>Login URL: <a href="{{loginurl}}">{{loginurl}}</a></b><br/><br/>Best Regards,<br/>Support Team<br/><b><a href="{{homeurl}}">{{sitename}}</a></b>' |
| 157 | ) |
| 158 | ), |
| 159 | 'user-signup-declined' => array( |
| 160 | 'label' => __( "User Signup Declined" , "download-manager" ), |
| 161 | 'for' => 'customer', |
| 162 | 'default' => array( |
| 163 | 'subject' => __( "Your signup request is declined" , "download-manager" ), |
| 164 | 'from_name' => get_option( 'blogname' ), |
| 165 | 'from_email' => $admin_email, |
| 166 | 'message' => '<h3>Signup Request Declined</h3>Hello {{first_name}},<br/>Unfortunately we are unable to approve your signup for the following reason:<hr/>{{reason}}<hr/><br/><br/>Best Regards,<br/>Support Team<br/><b><a href="{{homeurl}}">{{sitename}}</a></b>' |
| 167 | ) |
| 168 | ), |
| 169 | 'user-signup-suspended' => array( |
| 170 | 'label' => __( "User Account Suspended" , "download-manager" ), |
| 171 | 'for' => 'customer', |
| 172 | 'default' => array( |
| 173 | 'subject' => __( "Your account has been suspended" , "download-manager" ), |
| 174 | 'from_name' => get_option( 'blogname' ), |
| 175 | 'from_email' => $admin_email, |
| 176 | 'message' => '<h3>Account Suspended</h3>Hello {{first_name}},<br/>Unfortunately your account has been suspended for the following reason:<hr/>{{reason}}<hr/><br/><br/>Best Regards,<br/>Support Team<br/><b><a href="{{homeurl}}">{{sitename}}</a></b>' |
| 177 | ) |
| 178 | ), |
| 179 | 'password-reset' => array( |
| 180 | 'label' => __( "Password Reset Notification" , "download-manager" ), |
| 181 | 'for' => 'customer', |
| 182 | 'default' => array( |
| 183 | 'subject' => sprintf( __( "Request to reset your %s password" , "download-manager" ), $sitename ), |
| 184 | 'from_name' => get_option( 'blogname' ), |
| 185 | 'from_email' => $admin_email, |
| 186 | 'message' => 'You have requested for your password to be reset.<br/>Please confirm by clicking the button below: <a href="{{reset_password}}">{{reset_password}}</a><br/>No action required if you did not request it.</b><br/><br/>Best Regards,<br/>Support Team<br/><b><a href="{{homeurl}}">{{sitename}}</a></b>' |
| 187 | ) |
| 188 | ), |
| 189 | 'email-lock' => array( |
| 190 | 'label' => __( "Email Lock Notification" , "download-manager" ), |
| 191 | 'for' => 'customer', |
| 192 | 'default' => array( |
| 193 | 'subject' => __( "Download {{package_name}}" , "download-manager" ), |
| 194 | 'from_name' => get_option( 'blogname' ), |
| 195 | 'from_email' => $admin_email, |
| 196 | 'message' => 'Thanks for Subscribing to {{sitename}}<br/>Please click on following link to start download:<br/><b><a style="display: block;text-align: center" class="button" href="{{download_url}}">Download</a></b><br/><br/><br/>Best Regards,<br/>Support Team<br/><b>{{sitename}}</b>' |
| 197 | ) |
| 198 | ), |
| 199 | 'new-package-frontend' => array( |
| 200 | 'label' => __( "New Package Notification" , "download-manager" ), |
| 201 | 'for' => 'admin', |
| 202 | 'default' => array( |
| 203 | 'subject' => __( "New Package is Added By {{name}}" , "download-manager" ), |
| 204 | 'from_name' => get_option( 'blogname' ), |
| 205 | 'from_email' => $admin_email, |
| 206 | 'to_email' => $admin_email, |
| 207 | 'message' => 'A new package is added<br/><br/><table style="width: 100%" cellpadding="10px"><tr><td width="120px">Package Name:</td><td>{{package_name}}</td></tr><tr><td width="120px">Added By:</td><td>{{author}}</td></tr><tr><td width="120px"></td><td><div style="padding-top: 10px;"><a class="btn" href="{{edit_url}}">Review The Package</a></div></td></tr></table>' |
| 208 | ) |
| 209 | ), |
| 210 | 'package-approved' => array( |
| 211 | 'label' => __( "Package Approval Notification" , "download-manager" ), |
| 212 | 'for' => 'seller', |
| 213 | 'default' => array( |
| 214 | 'subject' => __( "Congratulation! {{package_name}} has been approved" , "download-manager" ), |
| 215 | 'from_name' => get_option( 'blogname' ), |
| 216 | 'from_email' => $admin_email, |
| 217 | 'message' => 'Your package has been approved<br/><br/><table style="width: 100%" cellpadding="10px"><tr><td width="120px">Package Name:</td><td>{{package_name}}</td></tr><tr><td width="120px"></td><td><div style="padding-top: 10px;"><a class="btn" href="{{package_url}}">View Package</a></div></td></tr></table>' |
| 218 | ) |
| 219 | ), |
| 220 | 'activity-report' => array( |
| 221 | 'label' => __( "Activity Report" , "download-manager" ), |
| 222 | 'for' => 'admin', |
| 223 | 'default' => array( |
| 224 | 'subject' => sprintf( __( "[%s] {{report_period}} Activity Report" , "download-manager" ), $sitename ), |
| 225 | 'from_name' => get_option( 'blogname' ), |
| 226 | 'from_email' => $admin_email, |
| 227 | 'to_email' => $admin_email, |
| 228 | 'message' => '{{report_content}}' |
| 229 | ) |
| 230 | ), |
| 231 | ); |
| 232 | |
| 233 | $templates = apply_filters( 'wpdm_email_templates', $templates ); |
| 234 | |
| 235 | return $templates; |
| 236 | |
| 237 | } |
| 238 | |
| 239 | public static function info( $id ) { |
| 240 | $templates = self::templates(); |
| 241 | return isset($templates[ $id ]) ? $templates[ $id ] : null; |
| 242 | } |
| 243 | |
| 244 | public static function tags() { |
| 245 | $tags = array( |
| 246 | "{{SERVER_...key...}}" => ['value' => '', 'desc' => 'Server variables, replace <code>...key...</code> with proper key, for example, to show referer, use <code>{{SERVER_HTTP_REFERER}}</code>'], |
| 247 | "{{REQUEST_...key...}}" => ['value' => '', 'desc' => 'Request variables, replace <code>...key...</code> with proper key'], |
| 248 | "{{support_email}}" => array( 'value' => get_option( 'admin_email' ), 'desc' => 'Support Email' ), |
| 249 | "{{img_logo}}" => array( 'value' => '', 'desc' => 'Site Logo' ), |
| 250 | "{{banner}}" => array( 'value' => '', 'desc' => 'Banner/Background Image URL' ), |
| 251 | "{{site_url}}" => array( 'value' => home_url( '/' ), 'desc' => 'Home URL of your website' ), |
| 252 | "{{homeurl}}" => array( 'value' => home_url( '/' ), 'desc' => 'Home URL of your website' ), |
| 253 | "{{sitename}}" => array( |
| 254 | 'value' => get_option( 'blogname' ), |
| 255 | 'desc' => 'The name/title of your website' |
| 256 | ), |
| 257 | "{{site_tagline}}" => array( |
| 258 | 'value' => get_bloginfo( 'description' ), |
| 259 | 'desc' => 'The name/title of your website' |
| 260 | ), |
| 261 | "{{loginurl}}" => array( 'value' => wp_login_url(), 'desc' => 'Login page URL' ), |
| 262 | "{{name}}" => array( 'value' => '', 'desc' => 'Members First Name' ), |
| 263 | "{{username}}" => array( 'value' => '', 'desc' => 'Username' ), |
| 264 | "{{password}}" => array( 'value' => '', 'desc' => 'Members account password' ), |
| 265 | "{{date}}" => array( |
| 266 | 'value' => date_i18n( get_option( 'date_format' ), time() + wpdm_tzoffset() ), |
| 267 | 'desc' => 'Current Date' |
| 268 | ), |
| 269 | "{{package_name}}" => array( 'value' => '', 'desc' => 'Package Name' ), |
| 270 | "{{author}}" => array( 'value' => '', 'desc' => 'Package author profile' ), |
| 271 | "{{package_url}}" => array( 'value' => '', 'desc' => 'Package URL' ), |
| 272 | "{{edit_url}}" => array( 'value' => '', 'desc' => 'Package Edit URL' ) |
| 273 | ); |
| 274 | |
| 275 | $tags["{{client_ip}}"] = ['value' => wpdm_get_client_ip(), 'desc' => 'User IP']; |
| 276 | |
| 277 | if(is_user_logged_in()) { |
| 278 | global $current_user; |
| 279 | $tags["{{user_login}}"] = ['value' => $current_user->user_login, 'desc' => 'User login']; |
| 280 | $tags["{{user_email}}"] = ['value' => $current_user->user_email, 'desc' => 'User email']; |
| 281 | $tags["{{user_first_name}}"] = ['value' => $current_user->user_firstname, 'desc' => 'User first name']; |
| 282 | $tags["{{user_last_name}}"] = ['value' => $current_user->user_lastname, 'desc' => 'User last name']; |
| 283 | $tags["{{user_display_name}}"] = ['value' => $current_user->display_name, 'desc' => 'User display name']; |
| 284 | $tags["{{user_description}}"] = ['value' => get_user_meta($current_user->ID, 'description', true), 'desc' => 'User display name']; |
| 285 | $tags["{{um_...metakey...}}"] = ['value' => '', 'desc' => 'User meta data, replace <code>...metakey...</code> with user meta key']; |
| 286 | } |
| 287 | return apply_filters( "wpdm_email_template_tags", $tags ); |
| 288 | } |
| 289 | |
| 290 | public static function defaultTemplate( $id ) { |
| 291 | $templates = self::templates(); |
| 292 | |
| 293 | return isset($templates[ $id ], $templates[ $id ]['default']) ? $templates[ $id ]['default'] : null; |
| 294 | } |
| 295 | |
| 296 | public static function getTemplate( $id ) { |
| 297 | $template = maybe_unserialize( get_option( "__wpdm_etpl_" . $id, false ) ); |
| 298 | //print_r($template);die(); |
| 299 | $default = self::defaultTemplate( $id ); |
| 300 | if ( ! $template ) { |
| 301 | $template = $default; |
| 302 | } |
| 303 | $template['message'] = ! isset( $template['message'] ) || trim( strip_tags( $template['message'] ) ) == '' ? $default['message'] : $template['message']; |
| 304 | |
| 305 | return $template; |
| 306 | } |
| 307 | |
| 308 | public static function prepare( $id, $params ) { |
| 309 | $template = self::getTemplate( $id ); |
| 310 | |
| 311 | $params = apply_filters( "wpdm_email_params_" . $id, $params ); |
| 312 | $template = apply_filters( "wpdm_email_template_" . $id, $template ); |
| 313 | if(!is_array($params)) $params = []; |
| 314 | $__wpdm_email_setting = maybe_unserialize( get_option( '__wpdm_email_setting', array() ) ); |
| 315 | if(!is_array($__wpdm_email_setting)) $__wpdm_email_setting = []; |
| 316 | $params = $params + $__wpdm_email_setting; |
| 317 | $logo = isset($params['logo']) ? esc_url($params['logo']) : ''; |
| 318 | $banner = isset($params['banner']) ? esc_url($params['banner']) : ''; |
| 319 | $logo_wh = isset( $params['logo_w'] ) ? "width:{$params['logo_w']};" : ""; |
| 320 | $logo_wh .= isset($params['logo_h']) ? "height:{$params['logo_h']};" : ""; |
| 321 | $params['img_logo'] = isset( $params['logo'] ) && $params['logo'] != '' ? "<img style='max-width: 70%;{$logo_wh}' src='{$logo}' alt='".esc_attr(get_option('blogname'))."' />" : get_bloginfo('name'); |
| 322 | $params['banner'] = isset( $params['banner'] ) && $params['banner'] != '' ? esc_url($params['banner']) : ""; |
| 323 | $params['banner_img'] = isset( $params['banner'] ) && $params['banner'] != '' ? "<img style='max-width: 100%;' src='{$banner}' alt='Banner Image' />" : ""; |
| 324 | $params['images_dir'] = WPDM_BASE_URL.'src/__/views/email-templates/images/'; |
| 325 | $template_file = get_option( "__wpdm_email_template", "default.html" ); |
| 326 | $emltpl = null; |
| 327 | if ( isset( $params['template_file'] ) ) { |
| 328 | $template_file = $params['template_file']; |
| 329 | $emltpl = Template::locate( "email-templates/".sanitize_file_name($template_file), __DIR__ . '/views' ); |
| 330 | } |
| 331 | if(!$emltpl) |
| 332 | $emltpl = Template::locate( "email-templates/".sanitize_file_name($template_file), __DIR__ . '/views' ); |
| 333 | if($emltpl) |
| 334 | $emltpl = realpath($emltpl); |
| 335 | |
| 336 | if($template_file === '' || !$emltpl) |
| 337 | $emltpl = Template::locate( "email-templates/default.html", __DIR__ . '/views' ); |
| 338 | |
| 339 | if(file_exists($emltpl)) |
| 340 | $template_data = file_get_contents( $emltpl ); |
| 341 | |
| 342 | $template['message'] = str_replace( "{{message}}", stripslashes( wpautop( $template['message'] ) ), $template_data ); |
| 343 | $tags = self::tags(); |
| 344 | |
| 345 | // Drop any link whose href placeholder resolves to a blank value (e.g. a |
| 346 | // social profile URL the admin left empty) so the email never renders a |
| 347 | // dead/blank icon. Done before token replacement, keyed on the |
| 348 | // placeholder name, so it is immune to how an empty value renders. |
| 349 | $resolved_hrefs = $params; |
| 350 | foreach ( $tags as $tag_name => $tag_info ) { |
| 351 | $bare_tag = trim( $tag_name, '{}' ); |
| 352 | if ( ! isset( $resolved_hrefs[ $bare_tag ] ) || $resolved_hrefs[ $bare_tag ] === '' ) { |
| 353 | $resolved_hrefs[ $bare_tag ] = $tag_info['value']; |
| 354 | } |
| 355 | } |
| 356 | if ( preg_match_all( '/href\s*=\s*([\'"])\{\{([a-z0-9_]+)\}\}\1/i', $template['message'], $href_matches ) ) { |
| 357 | foreach ( array_unique( $href_matches[2] ) as $href_key ) { |
| 358 | $href_value = isset( $resolved_hrefs[ $href_key ] ) && is_scalar( $resolved_hrefs[ $href_key ] ) |
| 359 | ? trim( (string) $resolved_hrefs[ $href_key ] ) |
| 360 | : ''; |
| 361 | if ( $href_value === '' ) { |
| 362 | $template['message'] = self::removeEmptyLink( $template['message'], $href_key ); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | $new_pasrams = array(); |
| 368 | foreach ( $params as $key => $val ) { |
| 369 | $new_pasrams["{{{$key}}}"] = stripslashes($val); |
| 370 | $new_pasrams["[#{$key}#]"] = stripslashes($val); |
| 371 | } |
| 372 | $params = $new_pasrams; |
| 373 | foreach ( $tags as $key => $info ) { |
| 374 | if ( ! isset( $params[$key] )) { |
| 375 | $params[$key] = $info['value']; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | $template['subject'] = isset($params['subject']) ? $params['subject'] : str_replace( array_keys( $params ), array_values( $params ), $template['subject'] ); |
| 380 | if(isset($template['to_email'])) |
| 381 | $template['to_email'] = str_replace( array_keys( $params ), array_values( $params ), $template['to_email'] ); |
| 382 | $template['message'] = str_replace( array_keys( $params ), array_values( $params ), $template['message'] ); |
| 383 | $template['message'] = self::compile($template['message']); |
| 384 | $template['message'] = self::compile($template['message'], "/\{\{([^\}]+)\}\}/"); |
| 385 | return $template; |
| 386 | } |
| 387 | |
| 388 | public static function send( $id, $params ) { |
| 389 | |
| 390 | if(!$id || !WPDM()->email->getStatus($id)) return false; |
| 391 | |
| 392 | $email = self::info( $id ); |
| 393 | $template = self::prepare( $id, $params ); |
| 394 | $headers[] = "From: " . $template['from_name'] . " <" . $template['from_email'] . ">"; |
| 395 | $headers[] = "Content-type: text/html"; |
| 396 | if(!isset($template['to_email'])) { |
| 397 | $template['to_email'] = get_option('admin_email'); |
| 398 | } |
| 399 | //$to = $email['for'] !== 'admin' && !isset($params['to_seller']) && isset($params['to_email']) ? $params['to_email'] : $template['to_email']; |
| 400 | if(!isset($template['to_email'])) $template['to_email'] = get_option('admin_email'); |
| 401 | $to = isset($params['to_email']) ? $params['to_email'] : $template['to_email']; |
| 402 | $headers = apply_filters( "wpdm_email_headers_" . str_replace("-", "_", $id), $headers ); |
| 403 | if(isset($params['cc'])){ |
| 404 | $headers[] = "CC: {$params['cc']}"; |
| 405 | unset($params['cc']); |
| 406 | } |
| 407 | if(isset($params['bcc'])){ |
| 408 | $headers[] = "Bcc: {$params['bcc']}"; |
| 409 | unset($params['bcc']); |
| 410 | } |
| 411 | |
| 412 | $attachments = apply_filters( "wpdm_email_attachments_" . str_replace("-", "_", $id), array(), $params ); |
| 413 | |
| 414 | return wp_mail( $to, esc_attr($template['subject']), $template['message'], $headers, $attachments ); |
| 415 | } |
| 416 | |
| 417 | function sendMail() |
| 418 | { |
| 419 | $params = $this->_params; |
| 420 | |
| 421 | $email = self::info( $this->_template ); |
| 422 | $template = self::prepare( $this->_template, $this->_params ); |
| 423 | $headers[] = "From: " . $template['from_name'] . " <" . $template['from_email'] . ">"; |
| 424 | $headers[] = "Content-type: text/html"; |
| 425 | if(!isset($template['to_email'])) { |
| 426 | $template['to_email'] = get_option('admin_email'); |
| 427 | } |
| 428 | //$to = $email['for'] !== 'admin' && !isset($params['to_seller']) && isset($params['to_email']) ? $params['to_email'] : $template['to_email']; |
| 429 | if(!isset($template['to_email'])) $template['to_email'] = get_option('admin_email'); |
| 430 | $to = isset($params['to_email']) ? $params['to_email'] : $template['to_email']; |
| 431 | $headers = apply_filters( "wpdm_email_headers_" . str_replace("-", "_", $this->_template), $headers ); |
| 432 | if(isset($params['cc'])){ |
| 433 | $headers[] = "CC: {$params['cc']}"; |
| 434 | unset($params['cc']); |
| 435 | } |
| 436 | if(isset($params['bcc'])){ |
| 437 | $headers[] = "Bcc: {$params['bcc']}"; |
| 438 | unset($params['bcc']); |
| 439 | } |
| 440 | |
| 441 | $attachments = apply_filters( "wpdm_email_attachments_" . str_replace("-", "_", $this->_template), array(), $params ); |
| 442 | |
| 443 | return wp_mail( $to, esc_attr($template['subject']), $template['message'], $headers, $attachments ); |
| 444 | } |
| 445 | |
| 446 | |
| 447 | public function preview() { |
| 448 | global $current_user; |
| 449 | |
| 450 | |
| 451 | if ( ! isset( $_REQUEST['action'] ) || $_REQUEST['action'] != 'email_template_preview' ) { |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | __::isAuthentic("__empnonce", WPDM_PRI_NONCE, WPDM_MENU_ACCESS_CAP, false); |
| 456 | |
| 457 | |
| 458 | $id = wpdm_query_var('id'); |
| 459 | $email = self::info( $id ); |
| 460 | $params = array( |
| 461 | "name" => $current_user->display_name, |
| 462 | "username" => $current_user->user_login, |
| 463 | "password" => "**************", |
| 464 | "package_name" => __( "Sample Package Name" , "download-manager" ), |
| 465 | "author" => $current_user->display_name, |
| 466 | "package_url" => "#", |
| 467 | "edit_url" => "#" |
| 468 | ); |
| 469 | |
| 470 | if ( isset( $_REQUEST['etmpl'] ) ) { |
| 471 | $params['template_file'] = wpdm_query_var('etmpl'); |
| 472 | } |
| 473 | $template = self::prepare( $id, $params ); |
| 474 | echo $template['message']; |
| 475 | die(); |
| 476 | |
| 477 | } |
| 478 | |
| 479 | static public function fetch($template, $message) { |
| 480 | global $current_user; |
| 481 | if ( ! current_user_can( WPDM_MENU_ACCESS_CAP ) ) { |
| 482 | die( 'Error' ); |
| 483 | } |
| 484 | |
| 485 | $params['template_file'] = $template; |
| 486 | |
| 487 | $template = self::prepare( 'default', $params ); |
| 488 | return $template['message']; |
| 489 | } |
| 490 | |
| 491 | static function compile($template, $rule = "/\[\#([^\#]+)\#\]/") |
| 492 | { |
| 493 | $compiled = preg_replace_callback($rule, [new self, '_var'], $template); |
| 494 | return $compiled; |
| 495 | } |
| 496 | |
| 497 | static function _var($matched) |
| 498 | { |
| 499 | if(substr_count($matched[1], "acfx_user_meta_") && file_exists('get_field')){ |
| 500 | $meta_name = str_replace("acfx_user_meta_", "", $matched[1]); |
| 501 | $meta_value = get_field($meta_name, 'user_'.get_current_user_id()); |
| 502 | return $meta_value; |
| 503 | } |
| 504 | if(substr_count($matched[1], "acf_user_meta_")){ |
| 505 | $meta_name = str_replace("acf_user_meta_", "", $matched[1]); |
| 506 | $data = maybe_unserialize(get_user_meta(get_current_user_id(), 'wpdm_cregf', true)); |
| 507 | $value = wpdm_valueof($data, $meta_name); |
| 508 | if(is_array($value)) $value = implode(", ", $value); |
| 509 | return $value; |
| 510 | } |
| 511 | if(substr_count($matched[1], "user_meta_")){ |
| 512 | $meta_name = str_replace("user_meta_", "", $matched[1]); |
| 513 | if(substr_count($meta_name, '/')){ |
| 514 | $meta_name = explode("/", $meta_name); |
| 515 | $meta_value = get_user_meta(get_current_user_id(), $meta_name[0], true); |
| 516 | array_shift($meta_name); |
| 517 | $meta_value = wpdm_valueof($meta_value, implode("/", $meta_name)); |
| 518 | return $meta_value; |
| 519 | } |
| 520 | return get_user_meta(get_current_user_id(), $meta_name, true); |
| 521 | } |
| 522 | if(substr_count($matched[1], "um_")){ |
| 523 | $meta_name = str_replace("um_", "", $matched[1]); |
| 524 | if(substr_count($meta_name, '/')){ |
| 525 | $meta_name = explode("/", $meta_name); |
| 526 | $meta_value = get_user_meta(get_current_user_id(), $meta_name[0], true); |
| 527 | array_shift($meta_name); |
| 528 | $meta_value = wpdm_valueof($meta_value, implode("/", $meta_name)); |
| 529 | return $meta_value; |
| 530 | } |
| 531 | return get_user_meta(get_current_user_id(), $meta_name, true); |
| 532 | } |
| 533 | if(substr_count($matched[1], "SERVER_")){ |
| 534 | $meta_name = str_replace("SERVER_", "", $matched[1]); |
| 535 | $meta_value = wpdm_valueof($_SERVER, $meta_name); |
| 536 | return $meta_value; |
| 537 | } |
| 538 | if(substr_count($matched[1], "REQUEST_")){ |
| 539 | $meta_name = str_replace("REQUEST_", "", $matched[1]); |
| 540 | $meta_value = wpdm_valueof($_REQUEST, $meta_name); |
| 541 | if(is_array($meta_value)) $meta_value = implode(", ", $meta_value); |
| 542 | return $meta_value; |
| 543 | } |
| 544 | return $matched[1]; |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * Remove an anchor whose href is the given (empty) placeholder, together with |
| 549 | * its sole-content table cell and any adjacent separator cell, so an unset |
| 550 | * social/link URL leaves no blank icon or stray divider in the email. |
| 551 | * |
| 552 | * @param string $html The email markup, still containing {{tokens}}. |
| 553 | * @param string $key The placeholder name, e.g. "facebook". |
| 554 | * @return string |
| 555 | */ |
| 556 | protected static function removeEmptyLink( $html, $key ) { |
| 557 | if ( ! is_string( $html ) || $html === '' ) return $html; |
| 558 | |
| 559 | $ph = '\{\{' . preg_quote( $key, '~' ) . '\}\}'; |
| 560 | $separator = '(?:<td\b[^>]*>(?:\s|&[a-z0-9#]+;|\|)*</td>\s*)?'; |
| 561 | |
| 562 | // 1) Anchor alone in its own <td> (icon tiles or text links), plus a |
| 563 | // trailing separator cell when present, so no empty gap or stray |
| 564 | // divider is left behind. |
| 565 | $html = preg_replace( |
| 566 | '~<td\b[^>]*>\s*<a\b[^>]*\bhref\s*=\s*([\'"])' . $ph . '\1[^>]*>.*?</a>\s*</td>\s*' . $separator . '~is', |
| 567 | '', |
| 568 | $html |
| 569 | ); |
| 570 | |
| 571 | // 2) Fallback: any remaining bare anchor pointing at the empty placeholder. |
| 572 | $html = preg_replace( |
| 573 | '~<a\b[^>]*\bhref\s*=\s*([\'"])' . $ph . '\1[^>]*>.*?</a>~is', |
| 574 | '', |
| 575 | $html |
| 576 | ); |
| 577 | |
| 578 | return $html; |
| 579 | } |
| 580 | |
| 581 | |
| 582 | } |
| 583 |