Caldera_Forms_Email_Previews

Manages an email preview.

Defined (1)

The class is defined in the following location(s).

/classes/email/previews.php  
  1. class Caldera_Forms_Email_Previews { 
  2.  
  3. /** 
  4. * Form ID 
  5. * @since 1.4.0 
  6. * @var null|string 
  7. */ 
  8. protected $id; 
  9.  
  10. /** 
  11. * Caldera_Forms_Email_Previews constructor. 
  12. * @param null|string $id Optional. Form ID 
  13. * @param bool $view Optional. Whether to view (true) or record (false) the preview. Default is false. 
  14. */ 
  15. public function __construct( $id = null, $view = false ) { 
  16. if( $view && null != $id ) { 
  17. $this->id = $id; 
  18. $this->view(); 
  19. }else{ 
  20. add_filter( 'caldera_forms_mailer', array( $this, 'maybe_create_preview' ), 99, 3 ); 
  21.  
  22.  
  23.  
  24.  
  25. /** 
  26. * Display email preview 
  27. * @since 1.4.0 
  28. */ 
  29. public function view() { 
  30. if ( isset( $this->id ) ) { 
  31. $preview = $this->get_saved( $this->id ); 
  32. if ( is_object( $preview ) && ! empty( $preview ) ) { 
  33. $headers = $this->format_headers( $preview->headers ); 
  34. $message = $preview->message; 
  35. include CFCORE_PATH . 'ui/emails/email-preview.php'; 
  36. exit; 
  37. }else{ 
  38. wp_die( esc_html__( 'There is no saved email preview. Please submit this form with email previewing on and then try again.', 'caldera-forms' ) ); 
  39.  
  40.  
  41.  
  42. /** 
  43. * Format header view 
  44. * Create UL markup from array 
  45. * @since 1.4.0 
  46. * @param $headers 
  47. * @return string 
  48. */ 
  49. public function format_headers( $headers ) { 
  50. $view = array(); 
  51. $pattern = '<li><pre>%s</pre>: <span>%s</span></li>'; 
  52. foreach ( $headers as $header => $value ) { 
  53. if ( is_string( $value ) ) { 
  54. $view[ $header ] = sprintf( $pattern, ucwords( $header ), htmlentities( $value ) ); 
  55. }else{ 
  56. $view[ $header ] = sprintf( $pattern, ucwords( $header ), $this->format_headers( $value ) ); 
  57.  
  58.  
  59. return sprintf( '<ul>%s</ul>', implode( $view ) ); 
  60.  
  61. /** 
  62. * If preview should be recorded, record 
  63. * @since 1.4.0 
  64. * @uses "caldera_forms_mailer" filter 
  65. * @param array $mail 
  66. * @param array $data 
  67. * @param array $form 
  68. * @return array 
  69. */ 
  70. public function maybe_create_preview( $mail, $data, $form ) { 
  71. if( ! empty( $form[ 'mailer' ][ 'preview_email' ] ) ) { 
  72. $this->id = $form[ 'ID' ]; 
  73. $preview = new Caldera_Forms_Email_Preview( $mail ); 
  74. $this->record( $preview ); 
  75.  
  76. return $mail; 
  77.  
  78. /** 
  79. * Get saved preview as stdClass 
  80. *  
  81. * @param null|string $id Optional. Form ID. By default $this->id is used. Pass ID here to reset that property. 
  82. * @return bool|object 
  83. */ 
  84. public function get_saved( $id = null ) { 
  85. if( is_string( $id ) ) { 
  86. $this->id = $id; 
  87.  
  88. $_preview = get_option( $this->key() ); 
  89. if( ! empty( $_preview ) && is_object( $preview = json_decode( $_preview ) ) ) { 
  90. return $preview; 
  91. }else{ 
  92. return false; 
  93.  
  94.  
  95. /** 
  96. * Save preview 
  97. *  
  98. * @since 1.4.0 
  99. *  
  100. * @param Caldera_Forms_Email_Preview $preview Preview object 
  101. */ 
  102. protected function record( Caldera_Forms_Email_Preview $preview ) { 
  103. if ( false == get_option( $this->key( ) ) ) { 
  104. add_option( $this->key(), wp_json_encode( $preview ), false ); 
  105. }else{ 
  106. update_option( $this->key(), wp_json_encode( $preview ), false ); 
  107.  
  108.  
  109. /** 
  110. * Option key 
  111. *  
  112. * @since 1.4.0 
  113. *  
  114. * @return string 
  115. */ 
  116. protected function key( ) { 
  117. return '__cf_email_preview_' . $this->id; 
  118.  
  119.