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