class-component-mailchimp-signup.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin\ConfigurationUI
  6. */
  7. /**
  8. * Represents the mailchimp signup components.
  9. */
  10. class WPSEO_Config_Component_Mailchimp_Signup implements WPSEO_Config_Component {
  11. const META_NAME = 'wpseo-has-mailchimp-signup';
  12. /**
  13. * Gets the component identifier.
  14. *
  15. * @return string
  16. */
  17. public function get_identifier() {
  18. return 'MailchimpSignup';
  19. }
  20. /**
  21. * Gets the field.
  22. *
  23. * @return WPSEO_Config_Field
  24. */
  25. public function get_field() {
  26. return new WPSEO_Config_Field_Mailchimp_Signup();
  27. }
  28. /**
  29. * Get the data for the field.
  30. *
  31. * @return mixed
  32. */
  33. public function get_data() {
  34. $data = array(
  35. 'hasSignup' => $this->has_mailchimp_signup(),
  36. );
  37. return $data;
  38. }
  39. /**
  40. * Save data
  41. *
  42. * @param array $data Data containing changes.
  43. *
  44. * @return mixed
  45. */
  46. public function set_data( $data ) {
  47. $has_saved = false;
  48. if ( ! empty( $data['hasSignup'] ) ) {
  49. // Saves the user meta.
  50. update_user_meta( get_current_user_id(), self::META_NAME, true );
  51. $has_saved = ( $data['hasSignup'] === $this->has_mailchimp_signup() );
  52. }
  53. // Collect results to return to the configurator.
  54. $results = array(
  55. 'hasSignup' => $has_saved,
  56. );
  57. return $results;
  58. }
  59. /**
  60. * Checks if the user has entered his email for mailchimp already.
  61. *
  62. * @return bool
  63. */
  64. protected function has_mailchimp_signup() {
  65. $user_meta = get_user_meta( get_current_user_id(), self::META_NAME, true );
  66. return ( ! empty( $user_meta ) );
  67. }
  68. }