Skip to content

Commit

Permalink
Widgets: refactor Internet Defense League widget to work locally (#32196
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jeherve authored Aug 10, 2023
1 parent 9bc1311 commit f811ae7
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Internet Defense League Widget: remove campaign management and load images locally.
145 changes: 31 additions & 114 deletions projects/plugins/jetpack/modules/widgets/internet-defense-league.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.

use Automattic\Jetpack\Image_CDN\Image_CDN_Core;

/**
* Jetpack_Internet_Defense_League_Widget main class.
*/
Expand All @@ -15,38 +13,6 @@ class Jetpack_Internet_Defense_League_Widget extends WP_Widget {
*/
public $defaults = array();

/**
* Selected display variant.
*
* @var string
*/
public $variant;
/**
* Display variants.
*
* @var array
*/
public $variants = array();

/**
* Selected campaign.
*
* @var string
*/
public $campaign;
/**
* Campaign options.
*
* @var array
*/
public $campaigns = array();
/**
* False when enabling campaigns other than 'none' or empty.
*
* @var bool
*/
public $no_current = true;

/**
* Selected badge to display.
*
Expand Down Expand Up @@ -74,31 +40,14 @@ public function __construct() {
)
);

// When enabling campaigns other than 'none' or empty, change $no_current to false above.
$this->campaigns = array(
'' => esc_html__( 'All current and future campaigns', 'jetpack' ),
'none' => esc_html__( 'None, just display the badge please', 'jetpack' ),
);

$this->variants = array(
'banner' => esc_html__( 'Banner at the top of my site', 'jetpack' ),
'modal' => esc_html__( 'Modal (Overlay Box)', 'jetpack' ),
);

$this->badges = array(
'shield_badge' => esc_html__( 'Shield Badge', 'jetpack' ),
'super_badge' => esc_html__( 'Super Badge', 'jetpack' ),
'side_bar_badge' => esc_html__( 'Red Cat Badge', 'jetpack' ),
);

if ( false === $this->no_current ) {
$this->badges['none'] = esc_html__( 'Don\'t display a badge (just the campaign)', 'jetpack' );
}

$this->defaults = array(
'campaign' => key( $this->campaigns ),
'variant' => key( $this->variants ),
'badge' => key( $this->badges ),
'badge' => key( $this->badges ),
);

add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) );
Expand Down Expand Up @@ -130,18 +79,18 @@ public function widget( $args, $instance ) {
if ( ! isset( $this->badges[ $instance['badge'] ] ) ) {
$instance['badge'] = $this->defaults['badge'];
}
$badge_url = esc_url( 'https://www.internetdefenseleague.org/images/badges/final/' . $instance['badge'] . '.png' );
$photon_badge_url = Image_CDN_Core::cdn_url( $badge_url );
$alt_text = esc_html__( 'Member of The Internet Defense League', 'jetpack' );
$badge_url = 'internet-defense-league/' . $instance['badge'] . '.png';

echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<p><a href="https://www.internetdefenseleague.org/"><img src="' . esc_url( $photon_badge_url ) . '" alt="' . esc_attr( $alt_text ) . '" style="max-width: 100%; height: auto;" /></a></p>';
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

if ( 'none' !== $instance['campaign'] ) {
$this->campaign = $instance['campaign'];
$this->variant = $instance['variant'];
add_action( 'wp_footer', array( $this, 'footer_script' ) );
printf(
'<p><a href="%1$s" target="_blank" rel="noopener noreferrer"><img src="%2$s" alt="%3$s" style="max-width: 100%%; height: auto;" /></a></p>',
esc_url( 'https://www.fightforthefuture.org/' ),
esc_url( plugins_url( $badge_url, __FILE__ ) ),
esc_attr__( 'Member of The Internet Defense League', 'jetpack' )
);

echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

/** This action is already documented in modules/widgets/gravatar-profile.php */
Expand All @@ -150,34 +99,11 @@ public function widget( $args, $instance ) {

/**
* Inline footer script.
*
* @deprecated $$next-version$$ This widget does not enqueue scripts anymore.
*/
public function footer_script() {
if ( ! isset( $this->campaigns[ $this->campaign ] ) ) {
$this->campaign = $this->defaults['campaign'];
}

if ( ! isset( $this->variants[ $this->variant ] ) ) {
$this->variant = $this->defaults['variant'];
}

// On AMP endpoints, prevent a validation error from the inline script.
if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
return;
}

?>
<script type="text/javascript">
window._idl = {};
_idl.campaign = "<?php echo esc_js( $this->campaign ); ?>";
_idl.variant = "<?php echo esc_js( $this->variant ); ?>";
(function() {
var idl = document.createElement('script');
idl.async = true;
idl.src = 'https://members.internetdefenseleague.org/include/?url=' + (_idl.url || '') + '&campaign=' + (_idl.campaign || '') + '&variant=' + (_idl.variant || 'banner');
document.getElementsByTagName('body')[0].appendChild(idl);
})();
</script>
<?php
_deprecated_function( __METHOD__, 'jetpack-$$next-version$$' );
}

/**
Expand All @@ -190,33 +116,27 @@ public function footer_script() {
public function form( $instance ) {
$instance = wp_parse_args( $instance, $this->defaults );

// Hide first two form fields if no current campaigns.
if ( false === $this->no_current ) {
echo '<p><label>';
echo esc_html__( 'Which Internet Defense League campaign do you want to participate in?', 'jetpack' ) . '<br />';
$this->select( 'campaign', $this->campaigns, $instance['campaign'] );
echo '</label></p>';

echo '<p><label>';
echo esc_html__( 'How do you want to promote the campaign?', 'jetpack' ) . '<br />';
$this->select( 'variant', $this->variants, $instance['variant'] );
echo '</label></p>';
}

echo '<p><label>';
echo esc_html__( 'Which badge would you like to display?', 'jetpack' ) . '<br />';
$this->select( 'badge', $this->badges, $instance['badge'] );

echo '<select class="widefat" name="' . esc_attr( $this->get_field_name( 'badge' ) ) . '">';
foreach ( $this->badges as $option_slug => $option_name ) {
echo '<option value="' . esc_attr( $option_slug ) . '"' . selected( $option_slug, $instance['badge'], false ) . '>' . esc_html( $option_name ) . '</option>';
}
echo '</select>';
echo '</label></p>';

echo '<p>' . wp_kses(
sprintf(
/* translators: %s is an HTML link to the website of an internet campaign called the "Internet Defense League" */
_x( 'Learn more about the %s', 'the Internet Defense League', 'jetpack' ),
'<a href="https://www.internetdefenseleague.org/">Internet Defense League</a>'
'<a href="https://www.fightforthefuture.org/" target="_blank" rel="noopener noreferrer">Internet Defense League</a>'
),
array(
'a' => array(
'href' => array(),
'href' => array(),
'rel' => array(),
'target' => array(),
),
)
) . '</p>';
Expand All @@ -225,16 +145,14 @@ public function form( $instance ) {
/**
* Display a select form field.
*
* @deprecated $$next-version$$ This widget only has one option now, no need for an extracted method.
*
* @param string $field_name Name of the field.
* @param array $options Array of options.
* @param string $default Default option.
*/
public function select( $field_name, $options, $default = null ) {
echo '<select class="widefat" name="' . esc_attr( $this->get_field_name( $field_name ) ) . '">';
foreach ( $options as $option_slug => $option_name ) {
echo '<option value="' . esc_attr( $option_slug ) . '"' . selected( $option_slug, $default, false ) . '>' . esc_html( $option_name ) . '</option>';
}
echo '</select>';
public function select( $field_name, $options, $default = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
_deprecated_function( __METHOD__, 'jetpack-$$next-version$$' );
}

/**
Expand All @@ -248,9 +166,9 @@ public function select( $field_name, $options, $default = null ) {
public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$instance = array();

$instance['campaign'] = ( isset( $new_instance['campaign'] ) && isset( $this->campaigns[ $new_instance['campaign'] ] ) ) ? $new_instance['campaign'] : $this->defaults['campaign'];
$instance['variant'] = ( isset( $new_instance['variant'] ) && isset( $this->variants[ $new_instance['variant'] ] ) ) ? $new_instance['variant'] : $this->defaults['variant'];
$instance['badge'] = ( isset( $new_instance['badge'] ) && isset( $this->badges[ $new_instance['badge'] ] ) ) ? $new_instance['badge'] : $this->defaults['badge'];
$instance['badge'] = ( isset( $new_instance['badge'] ) && isset( $this->badges[ $new_instance['badge'] ] ) )
? $new_instance['badge']
: $this->defaults['badge'];

return $instance;
}
Expand All @@ -262,5 +180,4 @@ public function update( $new_instance, $old_instance ) { // phpcs:ignore Variabl
function jetpack_internet_defense_league_init() {
register_widget( 'Jetpack_Internet_Defense_League_Widget' );
}

add_action( 'widgets_init', 'jetpack_internet_defense_league_init' );
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f811ae7

Please sign in to comment.