Completed
Push — add/internet-defense-league-wi... ( 84411a...70ca32 )
by
unknown
207:26 queued 198:56
created

Jetpack_Internet_Defense_League_Widget::widget()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 6
nop 2
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
1
<?php /*
2
3
**************************************************************************
4
5
Plugin Name:  Internet Defense League
6
Description:  Displays your support for the Internet Defense League.
7
Author:       Automattic Inc.
8
Author URI:   https://automattic.com/
9
10
**************************************************************************/
11
12
class Jetpack_Internet_Defense_League_Widget extends WP_Widget {
13
14
	public $defaults = array();
15
16
	public $variant;
17
	public $variants = array();
18
19
	public $campaign;
20
	public $campaigns  = array();
21
	public $no_current = true;
22
23
	public $badge;
24
	public $badges = array();
25
26
	function __construct() {
27
		parent::__construct(
28
			'internet_defense_league_widget',
29
			/** This filter is documented in modules/widgets/facebook-likebox.php */
30
			apply_filters( 'jetpack_widget_name_widget', esc_html__( 'Internet Defense League', 'jetpack' ) ),
31
			array(
32
				'description' => __( 'Show your support for the Internet Defense League.', 'jetpack' ),
33
			)
34
		);
35
36
		// When enabling campaigns other than 'none' or empty, change $no_current to false above.
37
		$this->campaigns = array(
38
			''       => __( 'All current and future campaigns', 'jetpack' ),
39
			'none'   => __( 'None, just display the badge please', 'jetpack' ),
40
		);
41
42
		$this->variants = array(
43
			'banner' => __( 'Banner at the top of my site', 'jetpack' ),
44
			'modal'  => __( 'Modal (Overlay Box)', 'jetpack' ),
45
		);
46
47
		$this->badges = array(
48
			'shield_badge'   => __( 'Shield Badge', 'jetpack' ),
49
			'super_badge'    => __( 'Super Badge', 'jetpack' ),
50
			'side_bar_badge' => __( 'Red Cat Badge', 'jetpack' ),
51
		);
52
53
		if ( $this->no_current === false ) {
54
			$this->badges[ 'none' ] = __( 'Don\'t display a badge (just the campaign)', 'jetpack' );
55
		}
56
57
		$this->defaults = array(
58
			'campaign' => key( $this->campaigns ),
59
			'variant'  => key( $this->variants ),
60
			'badge'    => key( $this->badges ),
61
		);
62
	}
63
64
	public function widget( $args, $instance ) {
65
		$instance = wp_parse_args( $instance, $this->defaults );
66
67
		if ( 'none' != $instance['badge'] ) {
68
			if ( ! isset( $this->badges[ $instance['badge'] ] ) ) {
69
				$instance['badge'] = $this->defaults['badge'];
70
			}
71
			$badge_url = esc_url( 'https://internetdefenseleague.org/images/badges/final/' . $instance['badge'] . '.png' );
72
			$photon_badge_url = jetpack_photon_url( $badge_url );
73
			echo $args['before_widget'];
74
			echo '<p><a href="https://internetdefenseleague.org/"><img src="' . $photon_badge_url . '" alt="Member of The Internet Defense League" style="max-width: 100%; height: auto;" /></a></p>';
75
			echo $args['after_widget'];
76
			do_action( 'jetpack_stats_extra', 'widget_view', 'internet_defense_league' );
77
		}
78
79
		if ( 'none' != $instance['campaign'] ) {
80
			$this->campaign = $instance['campaign'];
81
			$this->variant  = $instance['variant'];
82
			add_action( 'wp_footer', array( $this, 'footer_script' ) );
83
			do_action( 'jetpack_stats_extra', 'widget_view', 'internet_defense_league' );
84
		}
85
	}
86
87
	public function footer_script() {
88
		if ( ! isset( $this->campaigns[ $this->campaign ] ) )
89
			$this->campaign = $this->defaults['campaign'];
90
91
		if ( ! isset( $this->variants[ $this->variant ] ) )
92
			$this->variant = $this->defaults['variant'];
93
		?>
94
		<script type="text/javascript">
95
			window._idl = {};
96
			_idl.campaign = "<?php echo esc_js( $this->campaign ); ?>";
97
			_idl.variant = "<?php echo esc_js( $this->variant ); ?>";
98
			(function() {
99
				var idl = document.createElement('script');
100
				idl.type = 'text/javascript';
101
				idl.async = true;
102
				idl.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'members.internetdefenseleague.org/include/?url=' + (_idl.url || '') + '&campaign=' + (_idl.campaign || '') + '&variant=' + (_idl.variant || 'banner');
103
				document.getElementsByTagName('body')[0].appendChild(idl);
104
			})();
105
		</script>
106
		<?php
107
	}
108
109
	public function form( $instance ) {
110
		$instance = wp_parse_args( $instance, $this->defaults );
111
112
		// Hide first two form fields if no current campaigns.
113
		if ( false === $this->no_current ) {
114
			echo '<p><label>';
115
			echo __( 'Which Internet Defense League campaign do you want to participate in?', 'jetpack' ) . '<br />';
116
			$this->select( 'campaign', $this->campaigns, $instance['campaign'] );
117
			echo '</label></p>';
118
119
			echo '<p><label>';
120
			echo __( 'How do you want to promote the campaign?', 'jetpack' ) . '<br />';
121
			$this->select( 'variant', $this->variants, $instance['variant'] );
122
			echo '</label></p>';
123
		}
124
125
		echo '<p><label>';
126
		echo __( 'Which badge would you like to display?', 'jetpack' ) . '<br />';
127
		$this->select( 'badge', $this->badges, $instance['badge'] );
128
		echo '</label></p>';
129
130
		/* translators: %s is a name of an internet campaign called the "Internet Defense League" */
131
		echo '<p>' . sprintf( _x( 'Learn more about the %s', 'the Internet Defense League', 'jetpack' ), '<a href="https://www.internetdefenseleague.org/">Internet Defense League</a>' ) . '</p>';
132
	}
133
134
	public function select( $field_name, $options, $default = null ) {
135
		echo '<select class="widefat" name="' . $this->get_field_name( $field_name ) . '">';
136
		foreach ( $options as $option_slug => $option_name ) {
137
			echo '<option value="' . esc_attr( $option_slug ) . '"' . selected( $option_slug, $default, false ) . '>' . esc_html( $option_name ) . '</option>';
138
		}
139
		echo '</select>';
140
	}
141
142
	public function update( $new_instance, $old_instance ) {
143
		$instance = array();
144
145
		$instance['campaign'] = ( isset( $new_instance['campaign'] ) && isset( $this->campaigns[ $new_instance['campaign'] ] ) ) ? $new_instance['campaign'] : $this->defaults['campaign'];
146
		$instance['variant']  = ( isset( $new_instance['variant'] )  && isset( $this->variants[  $new_instance['variant']  ] ) ) ? $new_instance['variant']  : $this->defaults['variant'];
147
		$instance['badge']    = ( isset( $new_instance['badge'] )    && isset( $this->badges[    $new_instance['badge'] ] ) )    ? $new_instance['badge']    : $this->defaults['badge'];
148
149
		return $instance;
150
	}
151
}
152
153
function jetpack_internet_defense_league_init() {
154
	if ( Jetpack::is_active() ) {
155
		register_widget( 'Jetpack_Internet_Defense_League_Widget' );
156
	}
157
}
158
159
add_action( 'widgets_init', 'jetpack_internet_defense_league_init' );
160