internet-defense-league.php 5.9 KB

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