| @@ -1,0 +1,188 @@ | ||
| 1 | +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | |
| 2 | + | |
| 3 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 4 | + exit( 0 ); | |
| 5 | +} | |
| 6 | + | |
| 7 | +// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files. | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * Jetpack_Internet_Defense_League_Widget main class. | |
| 11 | + */ | |
| 12 | +class Jetpack_Internet_Defense_League_Widget extends WP_Widget { | |
| 13 | + /** | |
| 14 | + * Default widget settings. | |
| 15 | + * | |
| 16 | + * @var array | |
| 17 | + */ | |
| 18 | + public $defaults = array(); | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Selected badge to display. | |
| 22 | + * | |
| 23 | + * @var string | |
| 24 | + */ | |
| 25 | + public $badge; | |
| 26 | + /** | |
| 27 | + * Badge display options. | |
| 28 | + * | |
| 29 | + * @var array | |
| 30 | + */ | |
| 31 | + public $badges = array(); | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * Jetpack_Internet_Defense_League_Widget constructor. | |
| 35 | + */ | |
| 36 | + public function __construct() { | |
| 37 | + parent::__construct( | |
| 38 | + 'internet_defense_league_widget', | |
| 39 | + /** This filter is documented in modules/widgets/facebook-likebox.php */ | |
| 40 | + apply_filters( 'jetpack_widget_name', esc_html__( 'Internet Defense League', 'jetpack' ) ), | |
| 41 | + array( | |
| 42 | + 'description' => esc_html__( 'Show your support for the Internet Defense League.', 'jetpack' ), | |
| 43 | + 'customize_selective_refresh' => true, | |
| 44 | + ) | |
| 45 | + ); | |
| 46 | + | |
| 47 | + $this->badges = array( | |
| 48 | + 'shield_badge' => esc_html__( 'Shield Badge', 'jetpack' ), | |
| 49 | + 'super_badge' => esc_html__( 'Super Badge', 'jetpack' ), | |
| 50 | + 'side_bar_badge' => esc_html__( 'Red Cat Badge', 'jetpack' ), | |
| 51 | + ); | |
| 52 | + | |
| 53 | + $this->defaults = array( | |
| 54 | + 'badge' => key( $this->badges ), | |
| 55 | + ); | |
| 56 | + | |
| 57 | + add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) ); | |
| 58 | + } | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * Remove the "Internet Defense League" widget from the Legacy Widget block | |
| 62 | + * | |
| 63 | + * @param array $widget_types List of widgets that are currently removed from the Legacy Widget block. | |
| 64 | + * @return array $widget_types New list of widgets that will be removed. | |
| 65 | + */ | |
| 66 | + public function hide_widget_in_block_editor( $widget_types ) { | |
| 67 | + $widget_types[] = 'internet_defense_league_widget'; | |
| 68 | + return $widget_types; | |
| 69 | + } | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * Display the Widget. | |
| 73 | + * | |
| 74 | + * @see WP_Widget::widget() | |
| 75 | + * | |
| 76 | + * @param array $args Display arguments. | |
| 77 | + * @param array $instance The settings for the particular instance of the widget. | |
| 78 | + */ | |
| 79 | + public function widget( $args, $instance ) { | |
| 80 | + $instance = wp_parse_args( $instance, $this->defaults ); | |
| 81 | + | |
| 82 | + if ( 'none' !== $instance['badge'] ) { | |
| 83 | + if ( ! isset( $this->badges[ $instance['badge'] ] ) ) { | |
| 84 | + $instance['badge'] = $this->defaults['badge']; | |
| 85 | + } | |
| 86 | + $badge_url = 'internet-defense-league/' . $instance['badge'] . '.png'; | |
| 87 | + | |
| 88 | + echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 89 | + | |
| 90 | + printf( | |
| 91 | + '<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>', | |
| 92 | + esc_url( 'https://www.fightforthefuture.org/' ), | |
| 93 | + esc_url( plugins_url( $badge_url, __FILE__ ) ), | |
| 94 | + esc_attr__( 'Member of The Internet Defense League', 'jetpack' ) | |
| 95 | + ); | |
| 96 | + | |
| 97 | + echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 98 | + } | |
| 99 | + | |
| 100 | + /** This action is already documented in modules/widgets/gravatar-profile.php */ | |
| 101 | + do_action( 'jetpack_stats_extra', 'widget_view', 'internet_defense_league' ); | |
| 102 | + } | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * Inline footer script. | |
| 106 | + * | |
| 107 | + * @deprecated 12.5 This widget does not enqueue scripts anymore. | |
| 108 | + */ | |
| 109 | + public function footer_script() { | |
| 110 | + _deprecated_function( __METHOD__, 'jetpack-12.5' ); | |
| 111 | + } | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * Widget form in the dashboard. | |
| 115 | + * | |
| 116 | + * @see WP_Widget::form() | |
| 117 | + * | |
| 118 | + * @param array $instance Previously saved values from database. | |
| 119 | + * @return string|void | |
| 120 | + */ | |
| 121 | + public function form( $instance ) { | |
| 122 | + $instance = wp_parse_args( $instance, $this->defaults ); | |
| 123 | + | |
| 124 | + echo '<p><label>'; | |
| 125 | + echo esc_html__( 'Which badge would you like to display?', 'jetpack' ) . '<br />'; | |
| 126 | + | |
| 127 | + echo '<select class="widefat" name="' . esc_attr( $this->get_field_name( 'badge' ) ) . '">'; | |
| 128 | + foreach ( $this->badges as $option_slug => $option_name ) { | |
| 129 | + echo '<option value="' . esc_attr( $option_slug ) . '"' . selected( $option_slug, $instance['badge'], false ) . '>' . esc_html( $option_name ) . '</option>'; | |
| 130 | + } | |
| 131 | + echo '</select>'; | |
| 132 | + echo '</label></p>'; | |
| 133 | + | |
| 134 | + echo '<p>' . wp_kses( | |
| 135 | + sprintf( | |
| 136 | + /* translators: %s is an HTML link to the website of an internet campaign called the "Internet Defense League" */ | |
| 137 | + _x( 'Learn more about the %s', 'the Internet Defense League', 'jetpack' ), | |
| 138 | + '<a href="https://www.fightforthefuture.org/" target="_blank" rel="noopener noreferrer">Internet Defense League</a>' | |
| 139 | + ), | |
| 140 | + array( | |
| 141 | + 'a' => array( | |
| 142 | + 'href' => array(), | |
| 143 | + 'rel' => array(), | |
| 144 | + 'target' => array(), | |
| 145 | + ), | |
| 146 | + ) | |
| 147 | + ) . '</p>'; | |
| 148 | + } | |
| 149 | + | |
| 150 | + /** | |
| 151 | + * Display a select form field. | |
| 152 | + * | |
| 153 | + * @deprecated 12.5 This widget only has one option now, no need for an extracted method. | |
| 154 | + * | |
| 155 | + * @param string $field_name Name of the field. | |
| 156 | + * @param array $options Array of options. | |
| 157 | + * @param string $default Default option. | |
| 158 | + */ | |
| 159 | + public function select( $field_name, $options, $default = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 160 | + _deprecated_function( __METHOD__, 'jetpack-12.5' ); | |
| 161 | + } | |
| 162 | + | |
| 163 | + /** | |
| 164 | + * Update widget. | |
| 165 | + * | |
| 166 | + * @see WP_Widget::update() | |
| 167 | + * | |
| 168 | + * @param array $new_instance New widget instance data. | |
| 169 | + * @param array $old_instance Old widget instance data. | |
| 170 | + */ | |
| 171 | + public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 172 | + $instance = array(); | |
| 173 | + | |
| 174 | + $instance['badge'] = ( isset( $new_instance['badge'] ) && isset( $this->badges[ $new_instance['badge'] ] ) ) | |
| 175 | + ? $new_instance['badge'] | |
| 176 | + : $this->defaults['badge']; | |
| 177 | + | |
| 178 | + return $instance; | |
| 179 | + } | |
| 180 | +} | |
| 181 | + | |
| 182 | +/** | |
| 183 | + * Register the widget. | |
| 184 | + */ | |
| 185 | +function jetpack_internet_defense_league_init() { | |
| 186 | + register_widget( 'Jetpack_Internet_Defense_League_Widget' ); | |
| 187 | +} | |
| 188 | +add_action( 'widgets_init', 'jetpack_internet_defense_league_init' ); | |