Nav apraksta

internet-defense-league.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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://www.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://www.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. // On AMP endpoints, prevent a validation error from the inline script.
  73. if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) {
  74. return;
  75. }
  76. ?>
  77. <script type="text/javascript">
  78. window._idl = {};
  79. _idl.campaign = "<?php echo esc_js( $this->campaign ); ?>";
  80. _idl.variant = "<?php echo esc_js( $this->variant ); ?>";
  81. (function() {
  82. var idl = document.createElement('script');
  83. idl.async = true;
  84. idl.src = 'https://members.internetdefenseleague.org/include/?url=' + (_idl.url || '') + '&campaign=' + (_idl.campaign || '') + '&variant=' + (_idl.variant || 'banner');
  85. document.getElementsByTagName('body')[0].appendChild(idl);
  86. })();
  87. </script>
  88. <?php
  89. }
  90. public function form( $instance ) {
  91. $instance = wp_parse_args( $instance, $this->defaults );
  92. // Hide first two form fields if no current campaigns.
  93. if ( false === $this->no_current ) {
  94. echo '<p><label>';
  95. echo esc_html__( 'Which Internet Defense League campaign do you want to participate in?', 'jetpack' ) . '<br />';
  96. $this->select( 'campaign', $this->campaigns, $instance['campaign'] );
  97. echo '</label></p>';
  98. echo '<p><label>';
  99. echo esc_html__( 'How do you want to promote the campaign?', 'jetpack' ) . '<br />';
  100. $this->select( 'variant', $this->variants, $instance['variant'] );
  101. echo '</label></p>';
  102. }
  103. echo '<p><label>';
  104. echo esc_html__( 'Which badge would you like to display?', 'jetpack' ) . '<br />';
  105. $this->select( 'badge', $this->badges, $instance['badge'] );
  106. echo '</label></p>';
  107. /* translators: %s is a name of an internet campaign called the "Internet Defense League" */
  108. 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>';
  109. }
  110. public function select( $field_name, $options, $default = null ) {
  111. echo '<select class="widefat" name="' . $this->get_field_name( $field_name ) . '">';
  112. foreach ( $options as $option_slug => $option_name ) {
  113. echo '<option value="' . esc_attr( $option_slug ) . '"' . selected( $option_slug, $default, false ) . '>' . esc_html( $option_name ) . '</option>';
  114. }
  115. echo '</select>';
  116. }
  117. public function update( $new_instance, $old_instance ) {
  118. $instance = array();
  119. $instance['campaign'] = ( isset( $new_instance['campaign'] ) && isset( $this->campaigns[ $new_instance['campaign'] ] ) ) ? $new_instance['campaign'] : $this->defaults['campaign'];
  120. $instance['variant'] = ( isset( $new_instance['variant'] ) && isset( $this->variants[ $new_instance['variant'] ] ) ) ? $new_instance['variant'] : $this->defaults['variant'];
  121. $instance['badge'] = ( isset( $new_instance['badge'] ) && isset( $this->badges[ $new_instance['badge'] ] ) ) ? $new_instance['badge'] : $this->defaults['badge'];
  122. return $instance;
  123. }
  124. }
  125. function jetpack_internet_defense_league_init() {
  126. register_widget( 'Jetpack_Internet_Defense_League_Widget' );
  127. }
  128. add_action( 'widgets_init', 'jetpack_internet_defense_league_init' );