| @@ -1,0 +1,138 @@ | ||
| 1 | +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | |
| 2 | +/** | |
| 3 | + * MailChimp popup widget. | |
| 4 | + * It acts as a wrapper for the mailchimp_subscriber_popup shortcode. | |
| 5 | + * | |
| 6 | + * @package automattic/jetpack | |
| 7 | + */ | |
| 8 | + | |
| 9 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 10 | + exit( 0 ); | |
| 11 | +} | |
| 12 | + | |
| 13 | +// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files. | |
| 14 | + | |
| 15 | +if ( ! class_exists( 'Jetpack_MailChimp_Subscriber_Popup_Widget' ) ) { | |
| 16 | + | |
| 17 | + if ( ! class_exists( 'MailChimp_Subscriber_Popup' ) ) { | |
| 18 | + include_once JETPACK__PLUGIN_DIR . 'modules/shortcodes/mailchimp.php'; | |
| 19 | + } | |
| 20 | + | |
| 21 | + /** | |
| 22 | + * Register MailChimp Subscriber Popup widget. | |
| 23 | + */ | |
| 24 | + function jetpack_mailchimp_subscriber_popup_widget_init() { | |
| 25 | + register_widget( 'Jetpack_MailChimp_Subscriber_Popup_Widget' ); | |
| 26 | + } | |
| 27 | + | |
| 28 | + add_action( 'widgets_init', 'jetpack_mailchimp_subscriber_popup_widget_init' ); | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * Add a MailChimp subscription form. | |
| 32 | + */ | |
| 33 | + class Jetpack_MailChimp_Subscriber_Popup_Widget extends WP_Widget { | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Constructor | |
| 37 | + */ | |
| 38 | + public function __construct() { | |
| 39 | + parent::__construct( | |
| 40 | + 'widget_mailchimp_subscriber_popup', | |
| 41 | + /** This filter is documented in modules/widgets/facebook-likebox.php */ | |
| 42 | + apply_filters( 'jetpack_widget_name', __( 'MailChimp Subscriber Popup', 'jetpack' ) ), | |
| 43 | + array( | |
| 44 | + 'classname' => 'widget_mailchimp_subscriber_popup', | |
| 45 | + 'description' => __( 'Allows displaying a popup subscription form to visitors.', 'jetpack' ), | |
| 46 | + 'customize_selective_refresh' => true, | |
| 47 | + ) | |
| 48 | + ); | |
| 49 | + | |
| 50 | + add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) ); | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * Remove the "Mailchimp Subscriber Popup" widget from the Legacy Widget block | |
| 55 | + * | |
| 56 | + * @param array $widget_types List of widgets that are currently removed from the Legacy Widget block. | |
| 57 | + * @return array $widget_types New list of widgets that will be removed. | |
| 58 | + */ | |
| 59 | + public function hide_widget_in_block_editor( $widget_types ) { | |
| 60 | + $widget_types[] = 'widget_mailchimp_subscriber_popup'; | |
| 61 | + return $widget_types; | |
| 62 | + } | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * Outputs the HTML for this widget. | |
| 66 | + * | |
| 67 | + * @param array $args An array of standard parameters for widgets in this theme. | |
| 68 | + * @param array $instance An array of settings for this widget instance. | |
| 69 | + * | |
| 70 | + * @return void Echoes it's output | |
| 71 | + */ | |
| 72 | + public function widget( $args, $instance ) { | |
| 73 | + $instance = wp_parse_args( $instance, array( 'code' => '' ) ); | |
| 74 | + | |
| 75 | + // Regular expresion that will match maichimp shortcode. | |
| 76 | + $regex = '(\[mailchimp_subscriber_popup[^\]]+\])'; | |
| 77 | + | |
| 78 | + // Check if the shortcode exists. | |
| 79 | + preg_match( $regex, $instance['code'], $matches ); | |
| 80 | + | |
| 81 | + // Process the shortcode only, if exists. | |
| 82 | + if ( ! empty( $matches[0] ) ) { | |
| 83 | + echo do_shortcode( $matches[0] ); | |
| 84 | + } | |
| 85 | + | |
| 86 | + /** This action is documented in modules/widgets/gravatar-profile.php */ | |
| 87 | + do_action( 'jetpack_stats_extra', 'widget_view', 'mailchimp_subscriber_popup' ); | |
| 88 | + } | |
| 89 | + | |
| 90 | + /** | |
| 91 | + * Deals with the settings when they are saved by the admin. | |
| 92 | + * | |
| 93 | + * @param array $new_instance New configuration values. | |
| 94 | + * @param array $old_instance Old configuration values. | |
| 95 | + * | |
| 96 | + * @return array | |
| 97 | + */ | |
| 98 | + public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 99 | + $instance = array(); | |
| 100 | + $instance['code'] = MailChimp_Subscriber_Popup::reversal( $new_instance['code'] ); | |
| 101 | + | |
| 102 | + return $instance; | |
| 103 | + } | |
| 104 | + | |
| 105 | + /** | |
| 106 | + * Displays the form for this widget on the Widgets page of the WP Admin area. | |
| 107 | + * | |
| 108 | + * @param array $instance Instance configuration. | |
| 109 | + * | |
| 110 | + * @return string|void | |
| 111 | + */ | |
| 112 | + public function form( $instance ) { | |
| 113 | + $instance = wp_parse_args( $instance, array( 'code' => '' ) ); | |
| 114 | + | |
| 115 | + $label = sprintf( | |
| 116 | + wp_kses( | |
| 117 | + /* Translators: %s is a link to the MailChimp support docs. */ | |
| 118 | + __( 'Code: <a href="%s" target="_blank">( ? )</a>', 'jetpack' ), | |
| 119 | + array( | |
| 120 | + 'a' => array( | |
| 121 | + 'href' => array(), | |
| 122 | + 'target' => array(), | |
| 123 | + ), | |
| 124 | + ) | |
| 125 | + ), | |
| 126 | + 'https://en.support.wordpress.com/mailchimp/' | |
| 127 | + ); | |
| 128 | + | |
| 129 | + printf( | |
| 130 | + '<p><label for="%1$s">%4$s</label><textarea class="widefat" id="%1$s" name="%2$s" rows="3">%3$s</textarea></p>', | |
| 131 | + esc_attr( $this->get_field_id( 'code' ) ), | |
| 132 | + esc_attr( $this->get_field_name( 'code' ) ), | |
| 133 | + esc_textarea( $instance['code'] ), | |
| 134 | + $label // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- escaped above. | |
| 135 | + ); | |
| 136 | + } | |
| 137 | + } | |
| 138 | +} | |