mailchimp.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. if ( ! class_exists( 'Jetpack_MailChimp_Subscriber_Popup_Widget' ) ) {
  3. if ( ! class_exists( 'MailChimp_Subscriber_Popup' ) ) {
  4. include_once JETPACK__PLUGIN_DIR . 'modules/shortcodes/mailchimp.php';
  5. }
  6. //register MailChimp Subscriber Popup widget
  7. function jetpack_mailchimp_subscriber_popup_widget_init() {
  8. register_widget( 'Jetpack_MailChimp_Subscriber_Popup_Widget' );
  9. }
  10. add_action( 'widgets_init', 'jetpack_mailchimp_subscriber_popup_widget_init' );
  11. /**
  12. * Add a MailChimp subscription form.
  13. */
  14. class Jetpack_MailChimp_Subscriber_Popup_Widget extends WP_Widget {
  15. /**
  16. * Constructor
  17. */
  18. function __construct() {
  19. parent::__construct(
  20. 'widget_mailchimp_subscriber_popup',
  21. /** This filter is documented in modules/widgets/facebook-likebox.php */
  22. apply_filters( 'jetpack_widget_name', __( 'MailChimp Subscriber Popup', 'jetpack' ) ),
  23. array(
  24. 'classname' => 'widget_mailchimp_subscriber_popup',
  25. 'description' => __( 'Allows displaying a popup subscription form to visitors.', 'jetpack' ),
  26. 'customize_selective_refresh' => true,
  27. )
  28. );
  29. }
  30. /**
  31. * Outputs the HTML for this widget.
  32. *
  33. * @param array $args An array of standard parameters for widgets in this theme
  34. * @param array $instance An array of settings for this widget instance
  35. *
  36. * @return void Echoes it's output
  37. **/
  38. function widget( $args, $instance ) {
  39. $instance = wp_parse_args( $instance, array( 'code' => '' ) );
  40. // Regular expresion that will match maichimp shortcode.
  41. $regex = "(\[mailchimp_subscriber_popup[^\]]+\])";
  42. // Check if the shortcode exists.
  43. preg_match( $regex, $instance['code'], $matches );
  44. // Process the shortcode only, if exists.
  45. if ( ! empty( $matches[0] ) ) {
  46. echo do_shortcode( $matches[0] );
  47. }
  48. /** This action is documented in modules/widgets/gravatar-profile.php */
  49. do_action( 'jetpack_stats_extra', 'widget_view', 'mailchimp_subscriber_popup' );
  50. }
  51. /**
  52. * Deals with the settings when they are saved by the admin.
  53. *
  54. * @param array $new_instance New configuration values
  55. * @param array $old_instance Old configuration values
  56. *
  57. * @return array
  58. */
  59. function update( $new_instance, $old_instance ) {
  60. $instance = array();
  61. $instance['code'] = MailChimp_Subscriber_Popup::reversal( $new_instance['code'] );
  62. return $instance;
  63. }
  64. /**
  65. * Displays the form for this widget on the Widgets page of the WP Admin area.
  66. *
  67. * @param array $instance Instance configuration.
  68. *
  69. * @return void
  70. */
  71. function form( $instance ) {
  72. $instance = wp_parse_args( $instance, array( 'code' => '' ) );
  73. ?>
  74. <p>
  75. <label for="<?php echo esc_attr( $this->get_field_id( 'code' ) ); ?>">
  76. <?php printf( __( 'Code: <a href="%s" target="_blank">( ? )</a>', 'jetpack' ), 'https://en.support.wordpress.com/mailchimp/' ); ?>
  77. </label>
  78. <textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'code' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'code' ) ); ?>" rows="3"><?php echo esc_textarea( $instance['code'] ); ?></textarea>
  79. </p>
  80. <?php
  81. }
  82. }
  83. }