Completed
Push — add/debugger-blog-token ( 149038...c1053a )
by
unknown
33:02 queued 25:29
created

wordads/php/class-wordads-sidebar-widget.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Widget for adding ads to a sidebar.
4
 *
5
 * @package Jetpack.
6
 */
7
8
/**
9
 * Widget for inserting an ad into your sidebar
10
 *
11
 * @since 4.5.0
12
 */
13
class WordAds_Sidebar_Widget extends WP_Widget {
14
15
	/**
16
	 * Allowed tags.
17
	 *
18
	 * @var string[]
19
	 */
20
	private static $allowed_tags = array( 'mrec', 'wideskyscraper' );
21
22
	/**
23
	 * Number of widgets.
24
	 *
25
	 * @var int
26
	 */
27
	private static $num_widgets = 0;
28
29
	/**
30
	 * WordAds_Sidebar_Widget constructor.
31
	 */
32 View Code Duplication
	public function __construct() {
33
		parent::__construct(
34
			'wordads_sidebar_widget',
35
			/** This filter is documented in modules/widgets/facebook-likebox.php */
36
			apply_filters( 'jetpack_widget_name', 'Ads' ),
37
			array(
38
				'description'                 => __( 'Insert an ad unit wherever you can place a widget.', 'jetpack' ),
39
				'customize_selective_refresh' => true,
40
			)
41
		);
42
	}
43
44
	/**
45
	 * The Widget outputter.
46
	 *
47
	 * @param array $args Widget args.
48
	 * @param array $instance The Widget instance.
49
	 *
50
	 * @return bool|void
51
	 */
52
	public function widget( $args, $instance ) {
53
		global $wordads;
54
		if ( $wordads->should_bail() ) {
55
			return false;
56
		}
57
58
		if ( ! isset( $instance['unit'] ) ) {
59
			$instance['unit'] = 'mrec';
60
		}
61
62
		self::$num_widgets++;
63
		$about      = __( 'Advertisements', 'jetpack' );
64
		$width      = WordAds::$ad_tag_ids[ $instance['unit'] ]['width'];
65
		$height     = WordAds::$ad_tag_ids[ $instance['unit'] ]['height'];
66
		$unit_id    = 1 === self::$num_widgets ? 3 : self::$num_widgets + 3; // 2nd belowpost is '4'
67
		$section_id = 0 === $wordads->params->blog_id ?
68
			WORDADS_API_TEST_ID :
69
			$wordads->params->blog_id . $unit_id;
70
71
		$snippet = '';
0 ignored issues
show
$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...
72
		if ( $wordads->option( 'wordads_house', true ) ) {
73
			$unit = 'mrec';
74
			if ( 'leaderboard' === $instance['unit'] && ! $this->params->mobile_device ) {
75
				$unit = 'leaderboard';
76
			} elseif ( 'wideskyscraper' === $instance['unit'] ) {
77
				$unit = 'widesky';
78
			}
79
80
			$snippet = $wordads->get_house_ad( $unit );
81
		} else {
82
			$snippet = $wordads->get_ad_snippet( $section_id, $height, $width, 'widget' );
83
		}
84
85
		?>
86
		<div class="wpcnt">
87
			<div class="wpa">
88
				<span class="wpa-about"><?php echo esc_html( $about ); ?></span>
89
				<div class="u <?php echo esc_attr( $instance['unit'] ); ?>">
90
					<?php echo $snippet; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
91
				</div>
92
			</div>
93
		</div>
94
		<?php
95
	}
96
97
	/**
98
	 * The widget settings form.
99
	 *
100
	 * @param array $instance Widget instance.
101
	 *
102
	 * @return string|void
103
	 */
104
	public function form( $instance ) {
105
		// ad unit type.
106
		if ( isset( $instance['unit'] ) ) {
107
			$unit = $instance['unit'];
108
		} else {
109
			$unit = 'mrec';
110
		}
111
		?>
112
		<p>
113
			<label for="<?php echo esc_attr( $this->get_field_id( 'unit' ) ); ?>"><?php esc_html_e( 'Tag Dimensions:', 'jetpack' ); ?></label>
114
			<select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'unit' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'unit' ) ); ?>">
115
		<?php
116
		foreach ( WordAds::$ad_tag_ids as $ad_unit => $properties ) {
117
			if ( ! in_array( $ad_unit, self::$allowed_tags, true ) ) {
118
				continue;
119
			}
120
121
				$splits      = explode( '_', $properties['tag'] );
122
				$unit_pretty = "{$splits[0]} {$splits[1]}";
123
				$selected    = selected( $ad_unit, $unit, false );
124
				echo "<option value='", esc_attr( $ad_unit ) ,"' ", esc_attr( $selected ), '>', esc_html( $unit_pretty ) , '</option>';
125
		}
126
		?>
127
			</select>
128
		</p>
129
		<?php
130
	}
131
132
	/**
133
	 * The Widget updater.
134
	 *
135
	 * @param array $new_instance The revised instance.
136
	 * @param array $old_instance Original instance.
137
	 *
138
	 * @return array
139
	 */
140
	public function update( $new_instance, $old_instance ) {
141
		$instance = $old_instance;
142
143
		if ( in_array( $new_instance['unit'], self::$allowed_tags, true ) ) {
144
			$instance['unit'] = $new_instance['unit'];
145
		} else {
146
			$instance['unit'] = 'mrec';
147
		}
148
149
		return $instance;
150
	}
151
}
152