Passed
Push — master ( 0345e8...74c2eb )
by Christian
02:50
created

widget.php (120 issues)

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
0 ignored issues
show
Class file names should be based on the class name with "class-" prepended. Expected class-podlove-subscribe-button-widget.php, but found widget.php.
Loading history...
2
3
namespace PodloveSubscribeButton;
4
5
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
6
7
class Podlove_Subscribe_Button_Widget extends \WP_Widget {
8
9
	public function __construct() {
10
		parent::__construct(
11
					'podlove_subscribe_button_wp_plugin_widget',
0 ignored issues
show
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 20.
Loading history...
12
					( self::is_podlove_publisher_active() ? 'Podlove Subscribe Button' : 'Podlove Subscribe Button (WordPress plugin)' ),
0 ignored issues
show
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 20.
Loading history...
13
					array( 'description' => __( 'Adds a Podlove Subscribe Button to your Sidebar', 'podlove-subscribe-button' ), )
0 ignored issues
show
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 20.
Loading history...
Comma not allowed after last value in single-line array declaration
Loading history...
14
				);
0 ignored issues
show
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 16.
Loading history...
15
	}
16
17
	public static $widget_settings = array('infotext', 'title', 'size', 'style', 'format', 'autowidth', 'button', 'color');
0 ignored issues
show
Missing space after array opener.
Loading history...
Missing space before array closer.
Loading history...
18
19
	public static function is_podlove_publisher_active() {
20
		if ( is_plugin_active("podlove-publisher/plugin.php") )
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
Coding Style Comprehensibility introduced by
The string literal podlove-publisher/plugin.php does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
21
			return true;
22
23
		return false;
24
	}
25
26
	public function widget( $args, $instance ) {
27
		// Fetch the (network)button by it's name
28 View Code Duplication
		if ( ! $button = \PodloveSubscribeButton\Model\Button::get_button_by_name($instance['button']) )
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
Assignments must be the first block of code on a line
Loading history...
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
29
			return sprintf( __('Oops. There is no button with the ID "%s".', 'podlove-subscribe-button'), $args['button'] );
0 ignored issues
show
A gettext call containing placeholders was found, but was not accompanied by a "translators:" comment on the line above to clarify the meaning of the placeholders.
Loading history...
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
30
31
		echo $args['before_widget'];
32
		echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
0 ignored issues
show
Concat operator must be surrounded by a single space
Loading history...
33
34
		echo $button->render(
35
				\PodloveSubscribeButton::get_array_value_with_fallback($instance, 'size'),
0 ignored issues
show
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
36
				\PodloveSubscribeButton::get_array_value_with_fallback($instance, 'autowidth'),
0 ignored issues
show
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
37
				\PodloveSubscribeButton::get_array_value_with_fallback($instance, 'style'),
0 ignored issues
show
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
38
				\PodloveSubscribeButton::get_array_value_with_fallback($instance, 'format'), 
0 ignored issues
show
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
39
				\PodloveSubscribeButton::get_array_value_with_fallback($instance, 'color')
0 ignored issues
show
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
40
			);
0 ignored issues
show
This line of the multi-line function call does not seem to be indented correctly. Expected 8 spaces, but found 12.
Loading history...
41
		
42
		if ( strlen($instance['infotext']) )
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
43
			echo wpautop($instance['infotext']);
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
44
45
		echo $args['after_widget'];
46
	}	
0 ignored issues
show
Spaces must be used for mid-line alignment; tabs are not allowed
Loading history...
47
48
	public function form( $instance ) {
49
		foreach (self::$widget_settings as $setting) {
0 ignored issues
show
No space after opening parenthesis is prohibited
Loading history...
No space before closing parenthesis is prohibited
Loading history...
50
			$$setting = isset( $instance[$setting] ) ? $instance[$setting] : '';
0 ignored issues
show
Array keys must be surrounded by spaces unless they contain a string or an integer.
Loading history...
51
		}
52
53
		$buttons = \PodloveSubscribeButton\Model\Button::all();
0 ignored issues
show
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
54
		if ( is_multisite() )
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
55
			$network_buttons = \PodloveSubscribeButton\Model\NetworkButton::all();
56
57
		$buttons_as_options = function ($buttons) {
0 ignored issues
show
No space after opening parenthesis is prohibited
Loading history...
Expected 1 spaces between opening bracket and argument "$buttons"; 0 found
Loading history...
No space before closing parenthesis is prohibited
Loading history...
58
			foreach ($buttons as $subscribebutton) {
0 ignored issues
show
No space after opening parenthesis is prohibited
Loading history...
No space before closing parenthesis is prohibited
Loading history...
59
				echo "<option value='".$subscribebutton->name."' ".( $subscribebutton->name == $button ? 'selected=\"selected\"' : '' )." >".$subscribebutton->title." (".$subscribebutton->name.")</option>";
0 ignored issues
show
The variable $button does not exist. Did you mean $buttons?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
Concat operator must be surrounded by a single space
Loading history...
Coding Style Comprehensibility introduced by
The string literal > does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal ( does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal )</option> does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
60
			}
61
		}
62
		?>
63
		<p>
64
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title', 'podlove-subscribe-button' ); ?></label> 
65
			<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $title; ?>" />
0 ignored issues
show
The variable $title does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
66
67
			<label for="<?php echo $this->get_field_id( 'color' ); ?>"><?php _e( 'Color', 'podlove-subscribe-button' ); ?></label>
68
			<input class="podlove_subscribe_button_color" id="<?php echo $this->get_field_id( 'color' ); ?>" name="<?php echo $this->get_field_name( 'color' ); ?>" value="<?php echo $color; ?>" />
0 ignored issues
show
The variable $color does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
69
			<style type="text/css">
70
				.sp-replacer {
71
					display: flex;
72
				}
73
				.sp-preview {
74
					flex-grow: 10;
75
				}
76
			</style>
77
78
			<label for="<?php echo $this->get_field_id( 'button' ); ?>"><?php _e( 'Button', 'podlove-subscribe-button' ); ?></label> 
79
			<select class="widefat" id="<?php echo $this->get_field_id( 'button' ); ?>"
80
				      name="<?php echo $this->get_field_name( 'button' ); ?>">
0 ignored issues
show
Found precision alignment of 2 spaces.
Loading history...
81
				<?php if ( isset($network_buttons) && count($network_buttons) > 0 ) : ?>
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
82
					<optgroup label="<?php _e('Local', 'podlove'); ?>">
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
83
						<?php $buttons_as_options($buttons); ?>
84
					</optgroup>
85
					<optgroup label="<?php _e('Network', 'podlove'); ?>">
0 ignored issues
show
Expected 1 spaces after opening bracket; 0 found
Loading history...
Expected 1 spaces before closing bracket; 0 found
Loading history...
86
						<?php $buttons_as_options($network_buttons); ?>
87
					</optgroup>
88
				<?php else : 
0 ignored issues
show
Opening PHP tag must be on a line by itself
Loading history...
89
					$buttons_as_options($buttons);
90
				 endif; ?>
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
Closing PHP tag must be on a line by itself
Loading history...
91
			</select>
92
93
			<?php
94
			$customize_options = array(
95
					'size' => array(
0 ignored issues
show
Array item not aligned correctly; expected 16 spaces but found 20
Loading history...
Array double arrow not aligned correctly; expected 6 space(s) between "'size'" and double arrow, but found 1.
Loading history...
96
							'name' => 'Size',
0 ignored issues
show
Multi-line array item not aligned correctly; expected 24 spaces, but found 28
Loading history...
Array item not aligned correctly; expected 24 spaces but found 28
Loading history...
Array double arrow not aligned correctly; expected 4 space(s) between "'name'" and double arrow, but found 1.
Loading history...
97
							'options' => \PodloveSubscribeButton\Model\Button::$size
0 ignored issues
show
Array item not aligned correctly; expected 24 spaces but found 28
Loading history...
Each array item in a multi-line array declaration must end in a comma
Loading history...
98
						),
0 ignored issues
show
Array closer not aligned correctly; expected 20 space(s) but found 24
Loading history...
99
					'style' => array(
0 ignored issues
show
Array item not aligned correctly; expected 16 spaces but found 20
Loading history...
Array double arrow not aligned correctly; expected 5 space(s) between "'style'" and double arrow, but found 1.
Loading history...
100
							'name' => 'Style',
0 ignored issues
show
Multi-line array item not aligned correctly; expected 24 spaces, but found 28
Loading history...
Array item not aligned correctly; expected 24 spaces but found 28
Loading history...
Array double arrow not aligned correctly; expected 4 space(s) between "'name'" and double arrow, but found 1.
Loading history...
101
							'options' => \PodloveSubscribeButton\Model\Button::$style
0 ignored issues
show
Array item not aligned correctly; expected 24 spaces but found 28
Loading history...
Each array item in a multi-line array declaration must end in a comma
Loading history...
102
						),
0 ignored issues
show
Array closer not aligned correctly; expected 20 space(s) but found 24
Loading history...
103
					'format' => array(
0 ignored issues
show
Array item not aligned correctly; expected 16 spaces but found 20
Loading history...
Array double arrow not aligned correctly; expected 4 space(s) between "'format'" and double arrow, but found 1.
Loading history...
104
							'name' => 'Format',
0 ignored issues
show
Multi-line array item not aligned correctly; expected 24 spaces, but found 28
Loading history...
Array item not aligned correctly; expected 24 spaces but found 28
Loading history...
Array double arrow not aligned correctly; expected 4 space(s) between "'name'" and double arrow, but found 1.
Loading history...
105
							'options' => \PodloveSubscribeButton\Model\Button::$format
0 ignored issues
show
Array item not aligned correctly; expected 24 spaces but found 28
Loading history...
Each array item in a multi-line array declaration must end in a comma
Loading history...
106
						),
0 ignored issues
show
Array closer not aligned correctly; expected 20 space(s) but found 24
Loading history...
107
					'autowidth' => array(
0 ignored issues
show
Array item not aligned correctly; expected 16 spaces but found 20
Loading history...
108
							'name' => 'Autowidth',
0 ignored issues
show
Multi-line array item not aligned correctly; expected 24 spaces, but found 28
Loading history...
Array item not aligned correctly; expected 24 spaces but found 28
Loading history...
Array double arrow not aligned correctly; expected 4 space(s) between "'name'" and double arrow, but found 1.
Loading history...
109
							'options' => \PodloveSubscribeButton\Model\Button::$width
0 ignored issues
show
Array item not aligned correctly; expected 24 spaces but found 28
Loading history...
Each array item in a multi-line array declaration must end in a comma
Loading history...
110
						)
0 ignored issues
show
Each array item in a multi-line array declaration must end in a comma
Loading history...
Array closer not aligned correctly; expected 20 space(s) but found 24
Loading history...
111
				);
0 ignored issues
show
Array closer not aligned correctly; expected 12 space(s) but found 16
Loading history...
112
113
			foreach ($customize_options as $slug => $properties) : ?>
0 ignored issues
show
No space after opening parenthesis is prohibited
Loading history...
No space before closing parenthesis is prohibited
Loading history...
Closing PHP tag must be on a line by itself
Loading history...
114
				<label for="<?php echo $this->get_field_id( $slug ); ?>"><?php _e( $properties['name'], 'podlove-subscribe-button' ); ?></label> 
0 ignored issues
show
The $text arg must be a single string literal, not "$properties['name']".
Loading history...
115
				<select class="widefat" id="<?php echo $this->get_field_id( $slug ); ?>" name="<?php echo $this->get_field_name( $slug ); ?>">
116
					<option value="default" <?php echo ( $$slug == 'default' ? 'selected="selected"' : '' ); ?>><?php _e( 'Default ' . $properties['name'], 'podlove-subscribe-button' ) ?></option>
0 ignored issues
show
Use Yoda Condition checks, you must.
Loading history...
Inline PHP statement must end with a semicolon
Loading history...
The $text arg must be a single string literal, not "'Default '.$properties['name']".
Loading history...
117
					<optgroup>
118
						<?php foreach ( $properties['options'] as $property => $name ) : ?>
119
						<option value="<?php echo $property; ?>" <?php echo ( $$slug == $property ? 'selected="selected"' : '' ); ?>><?php _e( $name, 'podlove-subscribe-button' ) ?></option>
0 ignored issues
show
Inline PHP statement must end with a semicolon
Loading history...
The $text arg must be a single string literal, not "$name".
Loading history...
120
						<?php endforeach; ?>
121
					</optgroup>
122
				</select>
123
			<?php endforeach; ?>
124
		
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
125
			<label for="<?php echo $this->get_field_id( 'infotext' ); ?>"><?php _e( 'Description', 'podlove-subscribe-button' ); ?></label> 
126
			<textarea class="widefat" rows="10" id="<?php echo $this->get_field_id( 'infotext' ); ?>" name="<?php echo $this->get_field_name( 'infotext' ); ?>"><?php echo $infotext; ?></textarea>
0 ignored issues
show
The variable $infotext does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
127
		</p>
128
		<?php 
129
	}
130
131
	public function update( $new_instance, $old_instance ) {
132
		$instance = array();
133
134
		foreach (self::$widget_settings as $setting) {
0 ignored issues
show
No space after opening parenthesis is prohibited
Loading history...
No space before closing parenthesis is prohibited
Loading history...
135
			$instance[$setting]  = ( ! empty( $new_instance[$setting] ) ) ? strip_tags( $new_instance[$setting] ) : '';
0 ignored issues
show
Array keys must be surrounded by spaces unless they contain a string or an integer.
Loading history...
Equals sign not aligned correctly; expected 1 space but found 2 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
136
		}
137
138
		return $instance;
139
	}
140
}
141
add_action( 'widgets_init', function(){
0 ignored issues
show
Expected 1 space before opening brace; found 0
Loading history...
142
     register_widget( '\PodloveSubscribeButton\Podlove_Subscribe_Button_Widget' );
0 ignored issues
show
Found precision alignment of 1 spaces.
Loading history...
143
});