| 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 |
private static $num_widgets = 0; |
| 12 |
|
| 13 |
function __construct() { |
| 14 |
parent::__construct( |
| 15 |
'wordads_sidebar_widget', |
| 16 |
/** This filter is documented in modules/widgets/facebook-likebox.php */ |
| 17 |
apply_filters( 'jetpack_widget_name', 'Ads' ), |
| 18 |
array( |
| 19 |
'description' => __( 'Insert an ad unit wherever you can place a widget.', 'jetpack' ), |
| 20 |
'customize_selective_refresh' => true, |
| 21 |
) |
| 22 |
); |
| 23 |
} |
| 24 |
|
| 25 |
public function widget( $args, $instance ) { |
| 26 |
global $wordads; |
| 27 |
if ( $wordads->should_bail() ) { |
| 28 |
return false; |
| 29 |
} |
| 30 |
|
| 31 |
if ( ! isset( $instance['unit'] ) ) { |
| 32 |
$instance['unit'] = 'mrec'; |
| 33 |
} |
| 34 |
|
| 35 |
self::$num_widgets++; |
| 36 |
$about = __( 'Advertisements', 'jetpack' ); |
| 37 |
$width = WordAds::$ad_tag_ids[ $instance['unit'] ]['width']; |
| 38 |
$height = WordAds::$ad_tag_ids[ $instance['unit'] ]['height']; |
| 39 |
$unit_id = 1 == self::$num_widgets ? 3 : self::$num_widgets + 3; // 2nd belowpost is '4' |
| 40 |
$section_id = 0 === $wordads->params->blog_id ? |
| 41 |
WORDADS_API_TEST_ID : |
| 42 |
$wordads->params->blog_id . $unit_id; |
| 43 |
|
| 44 |
$snippet = ''; |
| 45 |
if ( $wordads->option( 'wordads_house', true ) ) { |
| 46 |
$unit = 'mrec'; |
| 47 |
if ( 'leaderboard' == $instance['unit'] && ! $this->params->mobile_device ) { |
| 48 |
$unit = 'leaderboard'; |
| 49 |
} elseif ( 'wideskyscraper' == $instance['unit'] ) { |
| 50 |
$unit = 'widesky'; |
| 51 |
} |
| 52 |
|
| 53 |
$snippet = $wordads->get_house_ad( $unit ); |
| 54 |
} else { |
| 55 |
$snippet = $wordads->get_ad_snippet( $section_id, $height, $width, 'widget' ); |
| 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 |
function jetpack_wordads_widgets_init_callback() { |
| 112 |
return register_widget( 'WordAds_Sidebar_Widget' ); |
| 113 |
} |
| 114 |
|
| 115 |
add_action( 'widgets_init', 'jetpack_wordads_widgets_init_callback' ); |
| 116 |
|