PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/learnpress/inc/emails/class-lp-email.php

https://gitlab.com/gregtyka/lfmawordpress
PHP | 481 lines | 348 code | 22 blank | 111 comment | 1 complexity | 57556a5e8eed3f398cfc492b43fe24b8 MD5 | raw file
  1. <?php
  2. /**
  3. * Class LP_Email
  4. *
  5. * @author ThimPress
  6. * @package LearnPress/Classes
  7. * @version 1.0
  8. */
  9. defined( 'ABSPATH' ) || exit();
  10. class LP_Email {
  11. /**
  12. * Email method ID.
  13. *
  14. * @var String
  15. */
  16. public $id;
  17. /**
  18. * Email method title.
  19. *
  20. * @var string
  21. */
  22. public $title;
  23. /**
  24. * 'yes' if the method is enabled.
  25. *
  26. * @var string
  27. */
  28. public $enabled;
  29. /**
  30. * Description for the email.
  31. *
  32. * @var string
  33. */
  34. public $description;
  35. /**
  36. * Plain text template path.
  37. *
  38. * @var string
  39. */
  40. public $template_plain;
  41. /**
  42. * HTML template path.
  43. *
  44. * @var string
  45. */
  46. public $template_html;
  47. /**
  48. * Template path.
  49. *
  50. * @var string
  51. */
  52. public $template_base;
  53. /**
  54. * Recipients for the email.
  55. *
  56. * @var string
  57. */
  58. public $recipient;
  59. /**
  60. * Heading for the email content.
  61. *
  62. * @var string
  63. */
  64. public $heading;
  65. /**
  66. * Subject for the email.
  67. *
  68. * @var string
  69. */
  70. public $subject;
  71. /**
  72. * Default heading for the email content.
  73. *
  74. * @var string
  75. */
  76. public $default_heading;
  77. /**
  78. * Default subject for the email.
  79. *
  80. * @var string
  81. */
  82. public $default_subject;
  83. /**
  84. * Object this email is for, for example a customer, product, or email.
  85. *
  86. * @var object
  87. */
  88. public $object;
  89. /**
  90. * Strings to find in subjects/headings.
  91. *
  92. * @var array
  93. */
  94. public $find;
  95. /**
  96. * Strings to replace in subjects/headings.
  97. *
  98. * @var array
  99. */
  100. public $replace;
  101. /**
  102. * Mime boundary (for multipart emails).
  103. *
  104. * @var string
  105. */
  106. public $mime_boundary;
  107. /**
  108. * Mime boundary header (for multipart emails).
  109. *
  110. * @var string
  111. */
  112. public $mime_boundary_header;
  113. /**
  114. * True when email is being sent.
  115. *
  116. * @var bool
  117. */
  118. public $sending;
  119. /**
  120. * List of preg* regular expression patterns to search for,
  121. * used in conjunction with $replace.
  122. * https://raw.github.com/ushahidi/wp-silcc/master/class.html2text.inc
  123. *
  124. * @var array $search
  125. * @see $replace
  126. */
  127. public $plain_search = array(
  128. "/\r/", // Non-legal carriage return
  129. '/&(nbsp|#160);/i', // Non-breaking space
  130. '/&(quot|rdquo|ldquo|#8220|#8221|#147|#148);/i', // Double quotes
  131. '/&(apos|rsquo|lsquo|#8216|#8217);/i', // Single quotes
  132. '/&gt;/i', // Greater-than
  133. '/&lt;/i', // Less-than
  134. '/&#38;/i', // Ampersand
  135. '/&#038;/i', // Ampersand
  136. '/&amp;/i', // Ampersand
  137. '/&(copy|#169);/i', // Copyright
  138. '/&(trade|#8482|#153);/i', // Trademark
  139. '/&(reg|#174);/i', // Registered
  140. '/&(mdash|#151|#8212);/i', // mdash
  141. '/&(ndash|minus|#8211|#8722);/i', // ndash
  142. '/&(bull|#149|#8226);/i', // Bullet
  143. '/&(pound|#163);/i', // Pound sign
  144. '/&(euro|#8364);/i', // Euro sign
  145. '/&#36;/', // Dollar sign
  146. '/&[^&\s;]+;/i', // Unknown/unhandled entities
  147. '/[ ]{2,}/' // Runs of spaces, post-handling
  148. );
  149. /**
  150. * List of pattern replacements corresponding to patterns searched.
  151. *
  152. * @var array $replace
  153. * @see $search
  154. */
  155. public $plain_replace = array(
  156. '', // Non-legal carriage return
  157. ' ', // Non-breaking space
  158. '"', // Double quotes
  159. "'", // Single quotes
  160. '>', // Greater-than
  161. '<', // Less-than
  162. '&', // Ampersand
  163. '&', // Ampersand
  164. '&', // Ampersand
  165. '(c)', // Copyright
  166. '(tm)', // Trademark
  167. '(R)', // Registered
  168. '--', // mdash
  169. '-', // ndash
  170. '*', // Bullet
  171. '�', // Pound sign
  172. 'EUR', // Euro sign. � ?
  173. '$', // Dollar sign
  174. '', // Unknown/unhandled entities
  175. ' ' // Runs of spaces, post-handling
  176. );
  177. function __construct() {
  178. $this->id = str_replace( '-', '_', $this->id );
  179. if ( is_null( $this->template_base ) ) {
  180. $this->template_base = LP()->plugin_path( 'templates/' );
  181. }
  182. if ( $this->is_current() ) {
  183. add_filter( 'learn_press_update_option_value', array( $this, '_remove_email_content_from_option' ), 99, 2 );
  184. $this->template_actions();
  185. }
  186. $this->heading = LP()->settings->get( 'emails_' . $this->id . '.heading', $this->default_heading );
  187. $this->subject = LP()->settings->get( 'emails_' . $this->id . '.subject', $this->default_subject );
  188. $this->email_format = LP()->settings->get( 'emails_' . $this->id . '.email_format' );
  189. $this->enable = LP()->settings->get( 'emails_' . $this->id . '.enable' ) == 'yes';
  190. }
  191. function __get( $key ) {
  192. if ( !empty( $this->{$key} ) ) {
  193. return $this->{$key};
  194. } else {
  195. return LP()->settings->get( 'emails_' . $this->id . '.' . $key );
  196. }
  197. }
  198. private function is_current() {
  199. return !empty( $_REQUEST['section'] ) && $_REQUEST['section'] == $this->id;
  200. }
  201. function _remove_email_content_from_option( $options, $key ) {
  202. if ( !$this->is_current() ) {
  203. return;
  204. }
  205. if ( is_array( $options ) && ( array_key_exists( 'email_content_html', $options ) || array_key_exists( 'email_content_plain', $options ) ) ) {
  206. if ( array_key_exists( 'email_content_html', $options ) ) {
  207. $this->save_template( $options['email_content_html'], $this->template_html );
  208. unset( $options['email_content_html'] );
  209. }
  210. if ( array_key_exists( 'email_content_plain', $options ) ) {
  211. $this->save_template( $options['email_content_plain'], $this->template_plain );
  212. unset( $options['email_content_plain'] );
  213. }
  214. }
  215. return $options;
  216. }
  217. protected function template_actions() {
  218. if (
  219. ( !empty( $this->template_html ) || !empty( $this->template_plain ) )
  220. && ( !empty( $_GET['move_template'] ) || !empty( $_GET['delete_template'] ) )
  221. && 'GET' == $_SERVER['REQUEST_METHOD']
  222. ) {
  223. if ( empty( $_GET['_learn_press_email_nonce'] ) || !wp_verify_nonce( $_GET['_learn_press_email_nonce'], 'learn_press_email_template_nonce' ) ) {
  224. return;
  225. }
  226. if ( !current_user_can( 'edit_themes' ) ) {
  227. return;
  228. }
  229. if ( !empty( $_GET['move_template'] ) ) {
  230. $this->move_template( $_GET['move_template'] );
  231. }
  232. if ( !empty( $_GET['delete_template'] ) ) {
  233. $this->delete_template( $_GET['delete_template'] );
  234. }
  235. }
  236. }
  237. protected function move_template( $type ) {
  238. if ( $template = $this->get_template( 'template_' . $type ) ) {
  239. if ( !empty( $template ) ) {
  240. $theme_file = $this->get_theme_template_file( $template );
  241. if ( wp_mkdir_p( dirname( $theme_file ) ) && !file_exists( $theme_file ) ) {
  242. $template_file = $this->template_base . $template;
  243. // Copy template file
  244. copy( $template_file, $theme_file );
  245. echo '<div class="updated"><p>' . __( 'Template file copied to theme.', 'learnpress' ) . '</p></div>';
  246. }
  247. }
  248. }
  249. }
  250. protected function delete_template( $type ) {
  251. if ( $template = $this->get_template( 'template_' . $type ) ) {
  252. if ( !empty( $template ) ) {
  253. $theme_file = $this->get_theme_template_file( $template );
  254. if ( file_exists( $theme_file ) ) {
  255. unlink( $theme_file );
  256. echo '<div class="updated"><p>' . __( 'Template file deleted from theme.', 'learnpress' ) . '</p></div>';
  257. }
  258. }
  259. }
  260. }
  261. public function format_string( $string ) {
  262. return str_replace( apply_filters( 'learn_press_email_format_string_find', $this->find, $this ), apply_filters( 'learn_press_email_format_string_replace', $this->replace, $this ), $string );
  263. }
  264. public function get_recipient() {
  265. return apply_filters( 'learn_press_email_recipient_' . $this->id, $this->recipient, $this->object );
  266. }
  267. public function get_subject() {
  268. return apply_filters( 'learn_press_email_subject_' . $this->id, $this->format_string( $this->subject ), $this->object );
  269. }
  270. public function get_content() {
  271. if ( $this->get_email_format() == 'plain_text' ) {
  272. $email_content = preg_replace( $this->plain_search, $this->plain_replace, strip_tags( $this->get_content_plain() ) );
  273. } else {
  274. $email_content = $this->get_content_html();
  275. }
  276. return wordwrap( $email_content, 70 );
  277. }
  278. public function get_heading() {
  279. return apply_filters( 'learn_press_email_heading_' . $this->id, $this->format_string( $this->heading ), $this->object );
  280. }
  281. function get_footer_text() {
  282. return apply_filters( 'learn_press_email_footer_text_' . $this->id, LP()->settings->get( 'emails_general.footer_text' ) );
  283. }
  284. public function get_content_plain() {
  285. }
  286. public function get_content_html() {
  287. }
  288. public function get_headers() {
  289. return apply_filters( 'learn_press_email_headers', "Content-Type: " . $this->get_content_format() . "\r\n", $this->id, $this->object );
  290. }
  291. public function get_attachments() {
  292. return apply_filters( 'learn_press_email_attachments', array(), $this->id, $this->object );
  293. }
  294. function get_from_address() {
  295. return sanitize_email( LP()->settings->get( 'emails_general.from_email' ) );
  296. }
  297. function get_from_name() {
  298. return sanitize_email( LP()->settings->get( 'emails_general.from_name' ) );
  299. }
  300. public function get_blogname() {
  301. return wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
  302. }
  303. function get_content_format() {
  304. switch ( $this->get_email_format() ) {
  305. case 'html' :
  306. return 'text/html';
  307. case 'multipart' :
  308. return 'multipart/alternative';
  309. default :
  310. return 'text/plain';
  311. }
  312. }
  313. /**
  314. * @return string
  315. */
  316. public function get_email_format() {
  317. return $this->email_format && class_exists( 'DOMDocument' ) ? $this->email_format : 'plain_text';
  318. }
  319. public function apply_style_inline( $content ) {
  320. if ( in_array( $this->get_content_format(), array( 'text/html', 'multipart/alternative' ) ) && class_exists( 'DOMDocument' ) ) {
  321. // get CSS styles
  322. ob_start();
  323. learn_press_get_template( 'emails/email-styles.php' );
  324. $css = apply_filters( 'learn_press_email_styles', ob_get_clean() );
  325. try {
  326. LP()->_include( 'libraries/class-emogrifier.php' );
  327. // apply CSS styles inline for picky email clients
  328. $emogrifier = new Emogrifier( $content, $css );
  329. $content = $emogrifier->emogrify();
  330. } catch ( Exception $e ) {
  331. }
  332. }
  333. return $content;
  334. }
  335. public function get_template( $type ) {
  336. $type = esc_attr( basename( $type ) );
  337. if ( 'template_html' == $type ) {
  338. return $this->template_html;
  339. } else if ( 'template_plain' == $type ) {
  340. return $this->template_plain;
  341. }
  342. return '';
  343. }
  344. protected function save_template( $code, $path ) {
  345. if ( current_user_can( 'edit_themes' ) && !empty( $code ) && !empty( $path ) ) {
  346. $saved = false;
  347. $file = get_stylesheet_directory() . '/' . learn_press_template_path() . '/' . $path;
  348. $code = stripslashes( $code );
  349. if ( is_writeable( $file ) ) {
  350. $f = fopen( $file, 'w+' );
  351. if ( $f !== false ) {
  352. fwrite( $f, $code );
  353. fclose( $f );
  354. $saved = true;
  355. }
  356. }
  357. if ( !$saved ) {
  358. $redirect = add_query_arg( 'learn_press_error', urlencode( __( 'Could not write to template file.', 'learnpress' ) ) );
  359. wp_redirect( $redirect );
  360. exit;
  361. }
  362. }
  363. }
  364. public function get_theme_template_file( $template ) {
  365. return get_stylesheet_directory() . '/' . apply_filters( 'learn_press_template_directory', 'learnpress', $template ) . '/' . $template;
  366. }
  367. function admin_options( $obj ) {
  368. }
  369. public function send( $to, $subject, $message, $headers, $attachments ) {
  370. add_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
  371. add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
  372. add_filter( 'wp_mail_content_type', array( $this, 'get_content_format' ) );
  373. $message = apply_filters( 'learn_press_mail_content', $this->apply_style_inline( $message ) );
  374. $return = wp_mail( $to, $subject, $message, $headers, $attachments );
  375. if ( LP()->settings->get( 'debug' ) == 'yes' ) {
  376. ob_start();
  377. echo get_class( $this ) . '::' . __FUNCTION__ . "\n";
  378. print_r( func_get_args() );
  379. $log = ob_get_clean();
  380. LP_Debug::instance()->add( $log );
  381. }
  382. remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ) );
  383. remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ) );
  384. remove_filter( 'wp_mail_content_type', array( $this, 'get_content_format' ) );
  385. return $return;
  386. }
  387. function _send( $from, $to, $subject, $message ) {
  388. }
  389. /**
  390. * @param string $format
  391. *
  392. * @return array
  393. */
  394. function get_template_data( $format = 'plain' ) {
  395. return array( 'plain_text' => $format == 'plain' );
  396. }
  397. }