Completed
Push — add/related-posts-customize ( 4a306a...1c5144 )
by
unknown
43:30 queued 35:07
created

WordAds_Sidebar_Widget::widget()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 46
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 33
nc 8
nop 2
dl 0
loc 46
rs 5.5555
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
12 View Code Duplication
	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 a WordAd 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
		$about = __( 'About these ads', 'jetpack' );
31
		$width = WordAds::$ad_tag_ids[$instance['unit']]['width'];
32
		$height = WordAds::$ad_tag_ids[$instance['unit']]['height'];
33
		$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...
34
		if ( $wordads->option( 'wordads_house', true ) ) {
35
			$ad_url = 'https://s0.wp.com/wp-content/blog-plugins/wordads/house/';
36
			if ( 'leaderboard' == $instance['unit'] && ! $this->params->mobile_device ) {
37
				$ad_url .= 'leaderboard.png';
38
			} else if ( 'wideskyscraper' == $instance['unit'] ) {
39
				$ad_url .= 'widesky.png';
40
			} else {
41
				$ad_url .= 'mrec.png';
42
			}
43
44
			$snippet = <<<HTML
45
			<a href="https://wordpress.com/create/" target="_blank">
46
				<img src="$ad_url" alt="WordPress.com: Grow Your Business" width="$width" height="$height" />
47
			</a>
48
HTML;
49
		} else {
50
			$section_id = 0 === $wordads->params->blog_id ? WORDADS_API_TEST_ID : $wordads->params->blog_id . '3';
51
			$data_tags = ( $wordads->params->cloudflare ) ? ' data-cfasync="false"' : '';
52
			$snippet = <<<HTML
53
			<script$data_tags type='text/javascript'>
54
				(function(g){g.__ATA.initAd({sectionId:$section_id, width:$width, height:$height});})(window);
55
			</script>
56
HTML;
57
		}
58
59
		echo <<< HTML
60
		<div class="wpcnt">
61
			<div class="wpa">
62
				<a class="wpa-about" href="https://en.wordpress.com/about-these-ads/" rel="nofollow">$about</a>
63
				<div class="u {$instance['unit']}">
64
					$snippet
65
				</div>
66
			</div>
67
		</div>
68
HTML;
69
	}
70
71
	public function form( $instance ) {
72
		// ad unit type
73
		if ( isset( $instance['unit'] ) ) {
74
			$unit = $instance['unit'];
75
		} else {
76
			$unit = 'mrec';
77
		}
78
		?>
79
		<p>
80
			<label for="<?php echo esc_attr( $this->get_field_id( 'unit' ) ); ?>"><?php _e( 'Tag Dimensions:', 'jetpack' ); ?></label>
81
			<select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'unit' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'unit' ) ); ?>">
82
		<?php
83
		foreach ( WordAds::$ad_tag_ids as $ad_unit => $properties ) {
84
				if ( ! in_array( $ad_unit, self::$allowed_tags ) ) {
85
					continue;
86
				}
87
88
				$splits = explode( '_', $properties['tag'] );
89
				$unit_pretty = "{$splits[0]} {$splits[1]}";
90
				$selected = selected( $ad_unit, $unit, false );
91
				echo "<option value='", esc_attr( $ad_unit ) ,"' ", $selected, '>', esc_html( $unit_pretty ) , '</option>';
92
			}
93
		?>
94
			</select>
95
		</p>
96
		<?php
97
	}
98
99
	public function update( $new_instance, $old_instance ) {
100
		$instance = $old_instance;
101
102
		if ( in_array( $new_instance['unit'], self::$allowed_tags ) ) {
103
			$instance['unit'] = $new_instance['unit'];
104
		} else {
105
			$instance['unit'] = 'mrec';
106
		}
107
108
		return $instance;
109
	}
110
}
111
112
add_action(
113
	'widgets_init',
114
	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...
115
		'',
116
		'return register_widget( "WordAds_Sidebar_Widget" );'
117
	)
118
);
119