1 | <?php |
||
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 ) { |
||
129 | |||
130 | public function select( $field_name, $options, $default = null ) { |
||
137 | |||
138 | public function update( $new_instance, $old_instance ) { |
||
147 | } |
||
148 | |||
149 | function jetpack_internet_defense_league_init() { |
||
150 | register_widget( 'Jetpack_Internet_Defense_League_Widget' ); |
||
154 |