Jetpack_Internet_Defense_League_Widget

The WordPress Core Jetpack Internet Defense League Widget class.

Defined (1)

The class is defined in the following location(s).

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