class-component-mailchimp-signup.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. /**
  12. * The name of the mailchimp signup meta key.
  13. *
  14. * @var string
  15. */
  16. const META_NAME = 'wpseo-has-mailchimp-signup';
  17. /**
  18. * Gets the component identifier.
  19. *
  20. * @return string
  21. */
  22. public function get_identifier() {
  23. return 'MailchimpSignup';
  24. }
  25. /**
  26. * Gets the field.
  27. *
  28. * @return WPSEO_Config_Field
  29. */
  30. public function get_field() {
  31. return new WPSEO_Config_Field_Mailchimp_Signup();
  32. }
  33. /**
  34. * Get the data for the field.
  35. *
  36. * @return mixed
  37. */
  38. public function get_data() {
  39. $data = [
  40. 'hasSignup' => $this->has_mailchimp_signup(),
  41. ];
  42. return $data;
  43. }
  44. /**
  45. * Save data.
  46. *
  47. * @param array $data Data containing changes.
  48. *
  49. * @return mixed
  50. */
  51. public function set_data( $data ) {
  52. $has_saved = false;
  53. if ( ! empty( $data['hasSignup'] ) ) {
  54. // Saves the user meta.
  55. update_user_meta( get_current_user_id(), self::META_NAME, true );
  56. $has_saved = ( $data['hasSignup'] === $this->has_mailchimp_signup() );
  57. }
  58. // Collect results to return to the configurator.
  59. $results = [
  60. 'hasSignup' => $has_saved,
  61. ];
  62. return $results;
  63. }
  64. /**
  65. * Checks if the user has entered their email for mailchimp already.
  66. *
  67. * @return bool
  68. */
  69. protected function has_mailchimp_signup() {
  70. $user_meta = get_user_meta( get_current_user_id(), self::META_NAME, true );
  71. return ( ! empty( $user_meta ) );
  72. }
  73. }