widgets.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Widget for inserting an ad into your sidebar |
| 5 | * |
| 6 | * @since 4.5.0 |
| 7 | */ |
| 8 | class WordAds_Sidebar_Widget extends WP_Widget { |
| 9 | |
| 10 | private static $allowed_tags = array( 'mrec', 'wideskyscraper' ); |
| 11 | |
| 12 | function __construct() { |
| 13 | parent::__construct( |
| 14 | 'wordads_sidebar_widget', |
| 15 | /** This filter is documented in modules/widgets/facebook-likebox.php */ |
| 16 | apply_filters( 'jetpack_widget_name', 'Ads' ), |
| 17 | array( |
| 18 | 'description' => __( 'Insert an ad unit wherever you can place a widget.', 'jetpack' ), |
| 19 | 'customize_selective_refresh' => true |
| 20 | ) |
| 21 | ); |
| 22 | } |
| 23 | |
| 24 | public function widget( $args, $instance ) { |
| 25 | global $wordads; |
| 26 | if ( $wordads->should_bail() ) { |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | if ( ! isset( $instance['unit'] ) ) { |
| 31 | $instance['unit'] = 'mrec'; |
| 32 | } |
| 33 | |
| 34 | $about = __( 'Advertisements', 'jetpack' ); |
| 35 | $width = WordAds::$ad_tag_ids[$instance['unit']]['width']; |
| 36 | $height = WordAds::$ad_tag_ids[$instance['unit']]['height']; |
| 37 | |
| 38 | $snippet = ''; |
| 39 | if ( $wordads->option( 'wordads_house', true ) ) { |
| 40 | $unit = 'mrec'; |
| 41 | if ( 'leaderboard' == $instance['unit'] && ! $this->params->mobile_device ) { |
| 42 | $unit = 'leaderboard'; |
| 43 | } else if ( 'wideskyscraper' == $instance['unit'] ) { |
| 44 | $unit = 'widesky'; |
| 45 | } |
| 46 | |
| 47 | $snippet = $wordads->get_house_ad( $unit ); |
| 48 | } else { |
| 49 | $section_id = 0 === $wordads->params->blog_id ? WORDADS_API_TEST_ID : $wordads->params->blog_id . '3'; |
| 50 | $data_tags = ( $wordads->params->cloudflare ) ? ' data-cfasync="false"' : ''; |
| 51 | $snippet = <<<HTML |
| 52 | <script$data_tags type='text/javascript'> |
| 53 | (function(g){g.__ATA.initAd({sectionId:$section_id, width:$width, height:$height});})(window); |
| 54 | </script> |
| 55 | HTML; |
| 56 | } |
| 57 | |
| 58 | echo <<< HTML |
| 59 | <div class="wpcnt"> |
| 60 | <div class="wpa"> |
| 61 | <span class="wpa-about">$about</span> |
| 62 | <div class="u {$instance['unit']}"> |
| 63 | $snippet |
| 64 | </div> |
| 65 | </div> |
| 66 | </div> |
| 67 | HTML; |
| 68 | } |
| 69 | |
| 70 | public function form( $instance ) { |
| 71 | // ad unit type |
| 72 | if ( isset( $instance['unit'] ) ) { |
| 73 | $unit = $instance['unit']; |
| 74 | } else { |
| 75 | $unit = 'mrec'; |
| 76 | } |
| 77 | ?> |
| 78 | <p> |
| 79 | <label for="<?php echo esc_attr( $this->get_field_id( 'unit' ) ); ?>"><?php _e( 'Tag Dimensions:', 'jetpack' ); ?></label> |
| 80 | <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'unit' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'unit' ) ); ?>"> |
| 81 | <?php |
| 82 | foreach ( WordAds::$ad_tag_ids as $ad_unit => $properties ) { |
| 83 | if ( ! in_array( $ad_unit, self::$allowed_tags ) ) { |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | $splits = explode( '_', $properties['tag'] ); |
| 88 | $unit_pretty = "{$splits[0]} {$splits[1]}"; |
| 89 | $selected = selected( $ad_unit, $unit, false ); |
| 90 | echo "<option value='", esc_attr( $ad_unit ) ,"' ", $selected, '>', esc_html( $unit_pretty ) , '</option>'; |
| 91 | } |
| 92 | ?> |
| 93 | </select> |
| 94 | </p> |
| 95 | <?php |
| 96 | } |
| 97 | |
| 98 | public function update( $new_instance, $old_instance ) { |
| 99 | $instance = $old_instance; |
| 100 | |
| 101 | if ( in_array( $new_instance['unit'], self::$allowed_tags ) ) { |
| 102 | $instance['unit'] = $new_instance['unit']; |
| 103 | } else { |
| 104 | $instance['unit'] = 'mrec'; |
| 105 | } |
| 106 | |
| 107 | return $instance; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | add_action( |
| 112 | 'widgets_init', |
| 113 | create_function( |
| 114 | '', |
| 115 | 'return register_widget( "WordAds_Sidebar_Widget" );' |
| 116 | ) |
| 117 | ); |
| 118 |