Caldera_Forms_Email_Settings

Handles settings for Caldera Forms emails.

Defined (1)

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

/classes/email/settings.php  
  1. class Caldera_Forms_Email_Settings { 
  2.  
  3. /** 
  4. * Name of options key to store settings 
  5. * @since 1.4.0 
  6. * @var string 
  7. */ 
  8. protected static $option_key = '_caldera_forms_email_api_settings'; 
  9.  
  10. /** 
  11. * Current settings 
  12. * @since 1.4.0 
  13. * @var array 
  14. */ 
  15. protected static $settings; 
  16.  
  17. /** 
  18. * Nonce action for settings UI 
  19. *  
  20. * @since 1.4.0 
  21. *  
  22. * @var string 
  23. */ 
  24. protected static $nonce_action = 'cf-emails'; 
  25.  
  26. /** 
  27. * Default settings 
  28. * @since 1.4.0 
  29. * @var array 
  30. */ 
  31. protected static $defaults = array( 
  32. 'sendgrid' => array( 
  33. 'key' => false 
  34. ),  
  35. 'wp' => array( 
  36.  
  37. ),  
  38. 'method' => 'wp' 
  39. ); 
  40.  
  41. /** 
  42. * Get settings 
  43. * @since 1.4.0 
  44. * @return array 
  45. */ 
  46. public static function get_settings() { 
  47. if ( null == self::$settings ) { 
  48. self::$settings = get_option( self::$option_key, array() ); 
  49. self::$settings = wp_parse_args( self::$settings, self::$defaults ); 
  50.  
  51. return self::$settings; 
  52.  
  53. /** 
  54. * Get API keys 
  55. * @since 1.4.0 
  56. * @param string $api Which API to get key for 
  57. * @return mixed 
  58. */ 
  59. public static function get_key( $api ) { 
  60. if( self::is_allowed_method( $api ) ) { 
  61. return self::$settings[ $api ]['key']; 
  62.  
  63.  
  64. /** 
  65. * Save an API key 
  66. * @since 1.4.0 
  67. * @param string $api API to save key for 
  68. * @param string $key API Key 
  69. * @param bool $save Optional. If true, the default, settings will be saved. 
  70. */ 
  71. public static function save_key( $api, $key, $save = true ) { 
  72. if ( self::is_allowed_method( $api ) ) { 
  73. if( ! is_array( self::$settings[ $api ] ) ) { 
  74. self::$settings[ $api ] = array( 'key' => false ); 
  75.  
  76. self::$settings[ $api ][ 'key' ] = $key; 
  77. if ( $save ) { 
  78. self::update_settings(); 
  79.  
  80.  
  81.  
  82.  
  83. /** 
  84. * Check if is an allowed API 
  85. *  
  86. * @since 1.4.0 
  87. *  
  88. * @param string $api Name of API 
  89. * @return bool 
  90. */ 
  91. public static function is_allowed_method( $api ) { 
  92. return in_array( $api, self::allowed_apis() ); 
  93.  
  94.  
  95. /** 
  96. * Save email settings 
  97. *  
  98. * @uses "wp_ajax_cf_email_save" action 
  99. *  
  100. * @since 1.3.5 
  101. */ 
  102. public static function save() { 
  103. if( ! current_user_can( Caldera_Forms::get_manage_cap( 'admin' ) ) ) { 
  104. wp_die(); 
  105.  
  106. if( isset( $_POST[ 'nonce' ] ) && wp_verify_nonce( $_POST[ 'nonce' ], self::$nonce_action ) ) { 
  107. if( isset( $_POST[ 'method' ] ) ) { 
  108. if ( self::is_allowed_method( $_POST[ 'method' ] ) ) { 
  109. self::$settings[ 'method' ] = $_POST[ 'method' ]; 
  110.  
  111. foreach( self::allowed_apis() as $api ) { 
  112. if( isset( $_POST[ $api ] ) && is_string( $_POST[ $api ] ) ) { 
  113. self::save_key( $api, trim( strip_tags( $_POST[ $api ] ) ), false ); 
  114.  
  115. self::update_settings(); 
  116. status_header( 200); 
  117. wp_send_json_success(); 
  118.  
  119.  
  120.  
  121. wp_send_json_error(); 
  122.  
  123.  
  124.  
  125. /** 
  126. * If possible add hook to use set API 
  127. * @uses "init" action 
  128. * @since 1.4.0 
  129. */ 
  130. public static function maybe_add_hooks() { 
  131. //don't load in PHP 5.2 
  132. if ( ! version_compare( PHP_VERSION, '5.3.0', '>=' ) ) { 
  133. return; 
  134.  
  135. self::get_settings(); 
  136.  
  137. if( 'wp' !== self::get_method() ) { 
  138. foreach ( self::allowed_apis() as $api ) { 
  139. if ( self::valid( $api ) ) { 
  140. add_filter( 'caldera_forms_mailer', array( 'Caldera_Forms_Email_Callbacks', $api ), 26, 3 ); 
  141. break; 
  142.  
  143.  
  144.  
  145.  
  146.  
  147. /** 
  148. * Create nonce field for settings 
  149. * @since 1.4.0 
  150. * @return string 
  151. */ 
  152. public static function nonce_field() { 
  153. return wp_nonce_field( self::$nonce_action, 'cfemail', false, false ); 
  154.  
  155.  
  156. /** 
  157. * Is API valid? 
  158. * @since 1.4.0 
  159. * @param string $api API name 
  160.  
  161. * @return bool 
  162. */ 
  163. protected static function valid( $api ) { 
  164. self::get_settings(); 
  165. if ( self::is_allowed_method( $api ) ) { 
  166. if ( isset( self::$settings[ $api ]['key'] ) && ! empty( self::$settings[ $api ]['key'] ) ) { 
  167. return true; 
  168.  
  169.  
  170.  
  171.  
  172. /** 
  173. * Get an array of valid APIs 
  174. * @since 1.4.0 
  175. * @return array 
  176. */ 
  177. protected static function allowed_apis() { 
  178. /** 
  179. * Filter allowed email APIs 
  180. * @since 1.4.0 
  181. * @param array $apis Array of API names that are valid. 
  182. */ 
  183. return apply_filters( 'caldera_forms_allowed_email_apis', array( 
  184. 'wp',  
  185. 'sendgrid',  
  186. 'caldera' 
  187. ) ); 
  188.  
  189. /** 
  190. * Create email settings UI 
  191. * @uses "caldera_forms_admin_footer" action 
  192. * @since 1.4.0 
  193. */ 
  194. public static function ui() { 
  195. if ( ! version_compare( PHP_VERSION, '5.4.0', '>=' ) ) { 
  196. printf( '<div class="notice notice-error error"><p>%s</p>', esc_html__( 'Switching email services requires PHP 5.4 or later. PHP 5.6 is strongly recommended.', 'caldera-forms' ) ); 
  197. }else{ 
  198. include CFCORE_PATH . '/ui/emails/settings.php'; 
  199.  
  200.  
  201.  
  202.  
  203.  
  204. /** 
  205. * Get current method being used 
  206. * @since 1.4.0 
  207. * @return string 
  208. */ 
  209. public static function get_method() { 
  210. if( ! isset( self::$settings[ 'method' ] ) ) { 
  211. return 'wp'; 
  212.  
  213.  
  214. return self::$settings[ 'method' ]; 
  215.  
  216. /** 
  217. * Update email settings 
  218. * @since 1.4.0 
  219. */ 
  220. protected static function update_settings() { 
  221. update_option( self::$option_key, self::$settings ); 
  222.  
  223. /** 
  224. * Sanitize/validate save of this setting 
  225. * @since 1.4.0 
  226. * @uses "pre_update_option__caldera_forms_email_api_settings" 
  227. * @param mixed $values Values to be saved 
  228. * @return array 
  229. */ 
  230. public static function sanitize_save( $values ) { 
  231. if( ! is_array( $values ) ) { 
  232. return self::$defaults; 
  233.  
  234. foreach ( $values as $key => $value ) { 
  235. if( ! array_key_exists( $key, self::$defaults ) ) { 
  236. unset( $values[ $key ] ); 
  237. }elseif( 'method' == $key ) { 
  238. if( ! self::is_allowed_method( $value ) ) { 
  239. $values[ 'method' ] = 'wp'; 
  240. }else{ 
  241. foreach( $value as $k => $v ) { 
  242. if( ! in_array( $k, array( 'key', 'use' ) ) ) { 
  243. unset( $values[ $key ][ $k ] ); 
  244.  
  245. foreach ( self::allowed_apis() as $api ) { 
  246. if( ! isset( $values[ $api ] ) ) { 
  247. $values[ $api ] = self::$defaults[ $api ]; 
  248.  
  249.  
  250. return $values; 
  251.  
  252.