Completed
Push — branch-4.9-built ( f63861...2af77d )
by
unknown
751:45 queued 732:48
created

__construct()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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