internet-defense-league.php
8.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
/**
* Jetpack_Internet_Defense_League_Widget main class.
*/
class Jetpack_Internet_Defense_League_Widget extends WP_Widget {
/**
* Default widget settings.
*
* @var array
*/
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.
*
* @var string
*/
public $badge;
/**
* Badge display options.
*
* @var array
*/
public $badges = array();
/**
* Jetpack_Internet_Defense_League_Widget constructor.
*/
public function __construct() {
parent::__construct(
'internet_defense_league_widget',
/** This filter is documented in modules/widgets/facebook-likebox.php */
apply_filters( 'jetpack_widget_name', esc_html__( 'Internet Defense League', 'jetpack' ) ),
array(
'description' => esc_html__( 'Show your support for the Internet Defense League.', 'jetpack' ),
'customize_selective_refresh' => true,
)
);
// 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 ),
);
add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) );
}
/**
* Remove the "Internet Defense League" widget from the Legacy Widget block
*
* @param array $widget_types List of widgets that are currently removed from the Legacy Widget block.
* @return array $widget_types New list of widgets that will be removed.
*/
public function hide_widget_in_block_editor( $widget_types ) {
$widget_types[] = 'internet_defense_league_widget';
return $widget_types;
}
/**
* Display the Widget.
*
* @see WP_Widget::widget()
*
* @param array $args Display arguments.
* @param array $instance The settings for the particular instance of the widget.
*/
public function widget( $args, $instance ) {
$instance = wp_parse_args( $instance, $this->defaults );
if ( 'none' !== $instance['badge'] ) {
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 = jetpack_photon_url( $badge_url );
$alt_text = esc_html__( 'Member of The Internet Defense League', 'jetpack' );
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' ) );
}
/** This action is already documented in modules/widgets/gravatar-profile.php */
do_action( 'jetpack_stats_extra', 'widget_view', 'internet_defense_league' );
}
/**
* Inline footer script.
*/
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
}
/**
* Widget form in the dashboard.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
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 '</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>'
),
array(
'a' => array(
'href' => array(),
),
)
) . '</p>';
}
/**
* Display a select form field.
*
* @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>';
}
/**
* Update widget.
*
* @see WP_Widget::update()
*
* @param array $new_instance New widget instance data.
* @param array $old_instance Old widget instance data.
*/
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'];
return $instance;
}
}
/**
* Register the widget.
*/
function jetpack_internet_defense_league_init() {
register_widget( 'Jetpack_Internet_Defense_League_Widget' );
}
add_action( 'widgets_init', 'jetpack_internet_defense_league_init' );