No Description

Newsletters.php 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. class PUM_Newsletters {
  9. /**
  10. * @var WP_Error
  11. */
  12. public static $errors;
  13. public static $disabled = false;
  14. public static function init() {
  15. if ( doing_action( 'plugins_loaded' ) || ! did_action( 'plugins_loaded' ) ) {
  16. add_action( 'plugins_loaded', array( __CLASS__, 'delayed_init' ), 11 );
  17. } else {
  18. self::delayed_init();
  19. }
  20. }
  21. public static function delayed_init() {
  22. // TODO Once PUM-Aweber has been updated properly for a few months remove these if checks.
  23. // TODO Consider adding notice to update aweber.
  24. self::$disabled = in_array( true, array(
  25. class_exists( 'PUM_Aweber_Integration' ) && defined( 'PUM_AWEBER_INTEGRATION_VER' ) && version_compare( PUM_AWEBER_INTEGRATION_VER, '1.1.0', '<' ),
  26. class_exists( 'PUM_MailChimp_Integration' ) && defined( 'PUM_MAILCHIMP_INTEGRATION_VER' ) && PUM_MAILCHIMP_INTEGRATION_VER,
  27. class_exists( 'PUM_MCI' ) && version_compare( PUM_MCI::$VER, '1.3.0', '<' ),
  28. ) );
  29. // Checks for single very specific versions.
  30. if ( self::$disabled ) {
  31. return;
  32. }
  33. require_once Popup_Maker::$DIR . 'includes/functions/newsletter.php';
  34. do_action( 'pum_newsletter_init' );
  35. PUM_Shortcode_Subscribe::init();
  36. add_action( 'wp_ajax_pum_sub_form', array( __CLASS__, 'ajax_request' ) );
  37. add_action( 'wp_ajax_nopriv_pum_sub_form', array( __CLASS__, 'ajax_request' ) );
  38. add_filter( 'pum_sub_form_sanitization', array( __CLASS__, 'sanitization' ), 0 );
  39. add_filter( 'pum_sub_form_validation', array( __CLASS__, 'validation' ), 0, 2 );
  40. add_action( 'pum_sub_form_success', array( __CLASS__, 'record_submission' ), 0 );
  41. }
  42. /**
  43. * Submits the form using ajax
  44. */
  45. public static function ajax_request() {
  46. self::$errors = new WP_Error;
  47. $values = isset( $_REQUEST['values'] ) ? $_REQUEST['values'] : array();
  48. if ( empty( $values['popup_id'] ) && ! empty( $values['pum_form_popup_id'] ) ) {
  49. $values['popup_id'] = absint( $values['pum_form_popup_id'] );
  50. }
  51. // Clean JSON passed values.
  52. $values = PUM_Utils_Array::fix_json_boolean_values( $values );
  53. do_action( 'pum_sub_form_ajax_override', $values );
  54. // Allow sanitization & manipulation of form values prior to usage.
  55. $values = apply_filters( 'pum_sub_form_sanitization', $values );
  56. // Allow validation of the data.
  57. self::$errors = apply_filters( 'pum_sub_form_validation', self::$errors, $values );
  58. if ( self::$errors->get_error_code() ) {
  59. self::send_errors( self::$errors );
  60. }
  61. $response = array();
  62. // Process the submission and pass the $response array as a reference variable so data can be added..
  63. do_action_ref_array( 'pum_sub_form_submission', array( $values, &$response, &self::$errors ) );
  64. $error_code = self::$errors->get_error_code();
  65. $already_subscribed = 'already_subscribed' === $error_code;
  66. $success = empty( $error_code ) || $already_subscribed ? true : false;
  67. if ( ! $success && ! $already_subscribed ) {
  68. do_action( 'pum_sub_form_errors', $values, self::$errors );
  69. switch ( $error_code ) {
  70. case 'api_errors':
  71. $response['message'] = pum_get_newsletter_provider_message( $values['provider'], 'error', $values );
  72. break;
  73. }
  74. self::send_errors( self::$errors, $response );
  75. } else {
  76. do_action( 'pum_sub_form_success', $values );
  77. if ( $already_subscribed ) {
  78. $response['already_subscribed'] = true;
  79. }
  80. $response["message"] = pum_get_newsletter_provider_message( $values['provider'], $already_subscribed ? 'already_subscribed' : 'success', $values );
  81. self::send_success( $response );
  82. }
  83. // Don't let it keep going.
  84. die();
  85. }
  86. /**
  87. * Process and send error messages.
  88. *
  89. * Optionally pass extra data to send back to front end.
  90. *
  91. * @param $errors WP_Error
  92. * @param array $extra_response_args
  93. */
  94. public static function send_errors( WP_Error $errors, $extra_response_args = array() ) {
  95. if ( ! $errors || ! is_wp_error( $errors ) ) {
  96. $errors = self::$errors;
  97. }
  98. $response = array_merge( $extra_response_args, array(
  99. 'errors' => self::prepare_errors( $errors ),
  100. ) );
  101. wp_send_json_error( $response );
  102. die();
  103. }
  104. /**
  105. * Send a success response with passed data.
  106. *
  107. * @param array|mixed $response
  108. */
  109. public static function send_success( $response = array() ) {
  110. wp_send_json_success( array_filter( $response ) );
  111. die;
  112. }
  113. /**
  114. * Prepare errors for response.
  115. *
  116. * @param WP_Error $_errors
  117. *
  118. * @return array
  119. */
  120. public static function prepare_errors( WP_Error $_errors ) {
  121. if ( ! $_errors || ! is_wp_error( $_errors ) ) {
  122. $_errors = self::$errors;
  123. }
  124. $errors = array();
  125. foreach ( $_errors->get_error_codes() as $code ) {
  126. $errors[] = array(
  127. 'code' => $code,
  128. 'field' => $_errors->get_error_data( $code ),
  129. 'message' => $_errors->get_error_message( $code ),
  130. );
  131. }
  132. return $errors;
  133. }
  134. /**
  135. * Records the submission into a database table.
  136. *
  137. * @param array $values
  138. */
  139. public static function record_submission( $values = array() ) {
  140. $data = wp_parse_args( $values, array(
  141. 'uuid' => self::uuid(),
  142. 'user_id' => get_current_user_id(),
  143. 'popup_id' => 0,
  144. 'email_hash' => '',
  145. 'email' => '',
  146. 'name' => '',
  147. 'fname' => '',
  148. 'lname' => '',
  149. 'consent' => 'no',
  150. 'consent_args' => '',
  151. ) );
  152. $subscriber_id = PUM_DB_Subscribers::instance()->insert( $data );
  153. if ( is_user_logged_in() && $subscriber_id ) {
  154. update_user_meta( get_current_user_id(), 'pum_subscribed', true );
  155. }
  156. }
  157. /**
  158. * Return the current or new uuid.
  159. *
  160. * @return mixed|string
  161. */
  162. public static function uuid() {
  163. static $uuid;
  164. if ( ! isset( $uuid ) ) {
  165. $uuid = PUM_GA::get_uuid();
  166. }
  167. return $uuid;
  168. }
  169. /**
  170. * Provides basic field sanitization.
  171. *
  172. * @param array $values
  173. *
  174. * @return array
  175. */
  176. public static function sanitization( $values = array() ) {
  177. $values = wp_parse_args( $values, array(
  178. 'provider' => pum_get_option( 'newsletter_default_provider', 'none' ),
  179. 'consent' => 'no',
  180. 'consent_args' => array(),
  181. ) );
  182. $values['provider'] = sanitize_text_field( $values['provider'] );
  183. $values['provider'] = sanitize_text_field( $values['provider'] );
  184. if ( ! empty( $values['consent_args'] ) && is_string( $values['consent_args'] ) ) {
  185. if ( strpos( $values['consent_args'], '\"' ) >= 0 ) {
  186. $values['consent_args'] = stripslashes( $values["consent_args"] );
  187. }
  188. $values['consent_args'] = (array) json_decode( $values['consent_args'] );
  189. }
  190. $values['consent_args'] = wp_parse_args( $values['consent_args'], array(
  191. 'enabled' => 'no',
  192. 'required' => false,
  193. 'text' => '',
  194. ) );
  195. // Anonymize the data if they didn't consent and privacy is enabled.
  196. if ( $values['consent_args']['enabled'] === 'yes' && ! $values['consent_args']['required'] && $values['consent'] === 'no' ) {
  197. $values['uuid'] = '';
  198. $values['user_id'] = 0;
  199. $values['name'] = '';
  200. $values['fname'] = '';
  201. $values['lname'] = '';
  202. $values['email'] = function_exists( 'wp_privacy_anonymize_data' ) ? wp_privacy_anonymize_data( 'email', $values['email'] ) : 'deleted@site.invalid';
  203. }
  204. // Split name into fname & lname or vice versa.
  205. if ( isset( $values['name'] ) ) {
  206. $values['name'] = trim( sanitize_text_field( $values["name"] ) );
  207. //Creates last name
  208. $name = explode( " ", $values['name'] );
  209. if ( ! isset( $name[1] ) ) {
  210. $name[1] = '';
  211. }
  212. $values['fname'] = trim( $name[0] );
  213. $values['lname'] = trim( $name[1] );
  214. } else {
  215. $values['fname'] = isset( $values["fname"] ) ? sanitize_text_field( $values["fname"] ) : '';
  216. $values['lname'] = isset( $values["lname"] ) ? sanitize_text_field( $values["lname"] ) : '';
  217. $values['name'] = trim( $values['fname'] . ' ' . $values['lname'] );
  218. }
  219. $values['email'] = sanitize_email( $values["email"] );
  220. $values['email_hash'] = md5( $values['email'] );
  221. return $values;
  222. }
  223. /**
  224. * Provides basic field validation.
  225. *
  226. * @param WP_Error $errors
  227. * @param array $values
  228. *
  229. * @return WP_Error
  230. */
  231. public static function validation( $errors, $values = array() ) {
  232. if ( ! isset( $values["email"] ) || empty( $values["email"] ) ) {
  233. $errors->add( 'empty_email', pum_get_newsletter_provider_message( $values['provider'], 'empty_email', $values ), 'email' );
  234. } elseif ( ! is_email( $values["email"] ) ) {
  235. $errors->add( 'invalid_email', pum_get_newsletter_provider_message( $values['provider'], 'invalid_email', $values ), 'email' );
  236. }
  237. if ( $values['consent_args']['enabled'] === 'yes' && $values['consent_args']['required'] && $values['consent'] === 'no' ) {
  238. $errors->add( 'consent_required', pum_get_newsletter_provider_message( $values['provider'], 'consent_required', $values ), 'consent' );
  239. }
  240. return $errors;
  241. }
  242. }