Completed
Push — add/internet-defense-league-wi... ( 031309...76a219 )
by
unknown
177:58 queued 168:44
created

Jetpack_Internet_Defense_League_Widget   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 135
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 0

6 Methods

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