Completed
Push — update/only-hide-cards-if-rewi... ( 1d27cc...55885e )
by
unknown
215:14 queued 204:17
created

WordAds_Sidebar_Widget::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
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 View Code Duplication
	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 = '';
0 ignored issues
show
Unused Code introduced by
$snippet is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
45
		if ( $wordads->option( 'wordads_house', true ) ) {
46
			$unit = 'mrec';
47
			if ( 'leaderboard' == $instance['unit'] && ! $this->params->mobile_device ) {
48
				$unit = 'leaderboard';
49
			} else if ( '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 );
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(
0 ignored issues
show
Security Best Practice introduced by
The use of create_function is highly discouraged, better use a closure.

create_function can pose a great security vulnerability as it is similar to eval, and could be used for arbitrary code execution. We highly recommend to use a closure instead.

// Instead of
$function = create_function('$a, $b', 'return $a + $b');

// Better use
$function = function($a, $b) { return $a + $b; }
Loading history...
114
		'',
115
		'return register_widget( "WordAds_Sidebar_Widget" );'
116
	)
117
);
118