WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-mail.php
Go to the documentation of this file.
1 <?php
15 class WPDKMail extends WPDKPost {
16 
25  public $cc = '';
26 
35  public $bcc = '';
36 
44  private $from = '';
45 
57  public function __construct( $mail = null, $post_type = 'page' )
58  {
59  parent::__construct( $mail, $post_type );
60  }
61 
62  // -----------------------------------------------------------------------------------------------------------------
63  // Public methods
64  // -----------------------------------------------------------------------------------------------------------------
65 
80  public function send( $to, $subject = false, $from = '', $placeholders = array() ) {
81 
82  /* Use shared private property */
83  $this->from = $from;
84 
85  if ( is_numeric( $this->from ) ) {
86  $user = new WP_User( $from );
87  $this->from = sprintf( '%s <%s>', $user->data->display_name, $user->get( 'user_email' ) );
88  }
89 
90  /* $from is as 'NOME <email>', eg: 'wpXtreme <info@wpxtre.me>' */
91  if ( empty( $this->from ) ) {
92  /* Get the default WordPress email. */
93  $this->from = sprintf( '%s <%s>', get_option( 'blogname' ), get_option( 'admin_email' ) );
94  }
95 
96  /* User id for $to? */
97  $user = false;
98  if ( is_numeric( $to ) ) {
99  $user = new WP_User( $to );
100  $email = sanitize_email( $user->get( 'user_email' ) );
101 
102  /* If user has not email exit */
103  if ( empty( $email ) ) {
104  return;
105  }
106  $to = sprintf( ' %s <%s>', $user->data->display_name, $user->get( 'user_email' ) );
107  }
108 
109  if ( $subject === false ) {
110  $subject = apply_filters( 'the_title', $this->post_title );
111  }
112 
113  //$body = apply_filters( 'the_content', $post->post_content );
114  $body = $this->post_content;
115  $body = $this->replacePlaceholder( $body, $user, $placeholders );
116 
117  return wp_mail( $to, $subject, $body, $this->headers() );
118  }
119 
127  private function headers()
128  {
129  /* Build the header */
130  $headers = array(
131  'From: ' . $this->from,
132  'Content-Type: text/html'
133  );
134 
135  /* Added cc and bcc */
136  if ( !empty( $this->cc ) ) {
137  $this->cc = explode( ',', $this->cc );
138  foreach ( $this->cc as $email ) {
139  $headers[] = sprintf( 'Cc: %s', $email );
140  }
141  }
142 
143  if ( !empty( $this->bcc ) ) {
144  $this->bcc = explode( ',', $this->bcc );
145  foreach ( $this->bcc as $email ) {
146  $headers[] = sprintf( 'Bcc: %s', $email );
147  }
148  }
149 
150  $headers = apply_filters( 'wpdk_mail_headers', $headers );
151 
152  return implode( "\r\n", $headers );
153  }
154 
167  private function replacePlaceholder( $content, $id_user = false, $extra = array() ) {
168 
169  if ( false === $id_user ) {
170  $id_user = get_current_user_id();
171  $user = new WP_User( $id_user );
172  }
173  elseif ( is_object( $id_user ) && is_a( $id_user, 'WP_User' ) ) {
174  $user = $id_user;
175  }
176  elseif ( is_numeric( $id_user ) ) {
177  $user = new WP_User( $id_user );
178  }
179  else {
180  return $content;
181  }
182 
183  $str_replaces = array(
184  WPDKMailPlaceholder::USER_FIRST_NAME => $user->get( 'first_name' ),
185  WPDKMailPlaceholder::USER_LAST_NAME => $user->get( 'last_name' ),
186  WPDKMailPlaceholder::USER_DISPLAY_NAME => $user->data->display_name,
187  WPDKMailPlaceholder::USER_EMAIL => $user->data->user_email,
188  );
189 
190  if ( !empty( $extra ) ) {
191  $str_replaces = array_merge( $str_replaces, $extra );
192  }
193 
194  $str_replaces = apply_filters( 'wpdk_mail_replace_placeholders', $str_replaces, $id_user );
195 
196  $content = strtr( $content, $str_replaces );
197 
198  return $content;
199  }
200 
201 }
202 
213 
214  const USER_DISPLAY_NAME = '${USER_DISPLAY_NAME}';
215  const USER_EMAIL = '${USER_EMAIL}';
216  const USER_FIRST_NAME = '${USER_FIRST_NAME}';
217  const USER_LAST_NAME = '${USER_LAST_NAME}';
218 
227  public static function placeholders() {
228  $placeholders = array(
229  self::USER_FIRST_NAME => array(
230  __( 'User First name', WPDK_TEXTDOMAIN ),
231  'Core'
232  ),
233  self::USER_LAST_NAME => array(
234  __( 'User Last name', WPDK_TEXTDOMAIN ),
235  'Core'
236  ),
237  self::USER_DISPLAY_NAME => array(
238  __( 'User Display name', WPDK_TEXTDOMAIN ),
239  'Core'
240  ),
241  self::USER_EMAIL => array(
242  __( 'User email', WPDK_TEXTDOMAIN ),
243  'Core'
244  ),
245  );
246 
247  return apply_filters( 'wpdk_mail_placeholders', $placeholders );
248  }
249 
250 }