Passed
Push — master ( 877d43...e9e8ae )
by Brian
06:03
created

WP_Super_Duper_Widget::form()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Contains the shortcode class.
4
 *
5
 * @since 2.0.0
6
 */
7
8
defined( 'ABSPATH' ) || exit;
9
10
/**
11
 *
12
 * The widget super duper class.
13
 *
14
 *
15
 * @since 2.0.0
16
 * @version 2.0.0
17
 */
18
class WP_Super_Duper_Widget extends WP_Widget {
19
20
	/**
21
	 * @var WP_Super_Duper
22
	 */
23
	protected $sd;
24
25
	/**
26
	 * Class constructor.
27
	 *
28
	 * @param WP_Super_Duper $super_duper
29
	 */
30
	public function __construct( $super_duper ) {
31
		$this->sd = $super_duper;
32
33
		// Register widget.
34
		$widget_ops = $super_duper->options['widget_ops'];
35
36
		// Only overwrite if not set already.
37
		if ( ! isset( $widget_ops['show_instance_in_rest'] ) ) {
38
			$widget_ops['show_instance_in_rest'] = true;
39
		}
40
41
		parent::__construct( $super_duper->options['base_id'], $super_duper->options['name'], $widget_ops );
42
43
		if ( did_action( 'widgets_init' ) || doing_action( 'widgets_init' ) ) {
44
			$this->register_widget();
45
		} else {
46
			add_action( 'widgets_init', array( $this, 'register_widget' ) );
47
		}
48
49
		add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) );
50
		add_action( 'elementor/editor/after_enqueue_styles', array( __CLASS__, 'elementor_editor_styles' ) );
51
		add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget' ) );
52
53
	}
54
55
	/**
56
	 * Registers the widget.
57
	 */
58
	public function register_widget() {
59
		register_widget( $this );
60
	}
61
62
	/**
63
	 * Enqeues scripts.
64
	 *
65
	 * @param WP_Super_Duper $super_duper
66
	 */
67
	public static function enqueue_scripts() {
68
		wp_add_inline_script( 'admin-widgets', WP_Super_Duper::widget_js() );
69
		wp_add_inline_script( 'customize-controls', WP_Super_Duper::widget_js() );
70
		wp_add_inline_style( 'widgets', WP_Super_Duper::widget_css() );
71
	}
72
73
	/**
74
	 * Add our widget CSS to elementor editor.
75
	 */
76
	public static function elementor_editor_styles() {
77
		wp_add_inline_style( 'elementor-editor', WP_Super_Duper::widget_css( false ) );
78
	}
79
80
	/**
81
	 * Outputs the content of the widget
82
	 *
83
	 * @param array $args
84
	 * @param array $instance
85
	 */
86
	public function widget( $args, $instance ) {
87
88
		// Prepare output args.
89
		$argument_values = $this->sd->argument_values( $instance );
90
		$argument_values = $this->sd->string_to_bool( $argument_values );
91
		$output          = $this->sd->output( $argument_values, $args );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $output is correct as $this->sd->output($argument_values, $args) targeting WP_Super_Duper::output() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
92
		$no_wrap         = ! empty( $argument_values['no_wrap'] );
93
94
		ob_start();
95
		if ( $output && ! $no_wrap ) {
0 ignored issues
show
introduced by
$output is defined implicitly as null, thus it is always evaluated to false.
Loading history...
96
97
			$class_original = $this->sd->options['widget_ops']['classname'];
98
			$class          = $this->sd->options['widget_ops']['classname'] . ' sdel-' . $this->sd->get_instance_hash();
99
100
			// Before widget
101
			$before_widget = $args['before_widget'];
102
			$before_widget = str_replace( $class_original, $class, $before_widget );
103
			$before_widget = apply_filters( 'wp_super_duper_before_widget', $before_widget, $args, $instance, $this );
104
			$before_widget = apply_filters( 'wp_super_duper_before_widget_' . $this->sd->base_id, $before_widget, $args, $instance, $this );
105
106
			// After widget
107
			$after_widget = $args['after_widget'];
108
			$after_widget = apply_filters( 'wp_super_duper_after_widget', $after_widget, $args, $instance, $this );
109
			$after_widget = apply_filters( 'wp_super_duper_after_widget_' . $this->sd->base_id, $after_widget, $args, $instance, $this );
110
111
			echo $before_widget;
112
113
			// elementor strips the widget wrapping div so we check for and add it back if needed
114
			if ( $this->is_elementor_widget_output() ) {
115
				// Filter class & attrs for elementor widget output.
116
				$class = apply_filters( 'wp_super_duper_div_classname', $class, $args, $this->sd, $this );
117
				$class = apply_filters( 'wp_super_duper_div_classname_' . $this->sd->base_id, $class, $args, $this->sd, $this );
118
119
				$attrs = apply_filters( 'wp_super_duper_div_attrs', '', $args, $this->sd, $this );
120
				$attrs = apply_filters( 'wp_super_duper_div_attrs_' . $this->sd->base_id, $attrs, $args, $this->sd, $this );
121
122
				echo "<span class='" . esc_attr( $class ) . "' " . $attrs . ">";
123
			}
124
125
			echo $this->sd->output_title( $args, $instance );
126
			echo $output;
127
			if ( $this->is_elementor_widget_output() ) {
128
				echo "</span>";
129
			}
130
131
			echo $after_widget;
132
		} elseif ( $this->sd->is_preview() && $output == '' ) {// if preview show a placeholder if empty
133
			$output = $this->sd->preview_placeholder_text( "{{" . $this->base_id . "}}" );
0 ignored issues
show
Bug Best Practice introduced by
The property base_id does not exist on WP_Super_Duper_Widget. Did you maybe forget to declare it?
Loading history...
134
			echo $output;
135
		} elseif ( $output && $no_wrap ) {
0 ignored issues
show
introduced by
$output is defined implicitly as null, thus it is always evaluated to false.
Loading history...
136
			echo $output;
137
		}
138
		$output = ob_get_clean();
139
140
		$output = apply_filters( 'wp_super_duper_widget_output', $output, $instance, $args, $this );
141
142
		echo $output;
143
	}
144
145
	/**
146
	 * Tests if the current output is inside a elementor container.
147
	 *
148
	 * @since 1.0.4
149
	 * @return bool
150
	 */
151
	public function is_elementor_widget_output() {
152
		return defined( 'ELEMENTOR_VERSION' ) && isset( $this->number ) && $this->number == 'REPLACE_TO_ID';
153
	}
154
155
	/**
156
	 * Outputs the options form inputs for the widget.
157
	 *
158
	 * @param array $instance The widget options.
159
	 */
160
	public function form( $instance ) {
161
		$this->sd->form( $instance );
162
	}
163
164
	/**
165
	 * Processing widget options on save
166
	 *
167
	 * @param array $new_instance The new options
168
	 * @param array $old_instance The previous options
169
	 *
170
	 * @return array
171
	 * @todo we should add some sanitation here.
172
	 */
173
	public function update( $new_instance, $old_instance ) {
174
175
		// Save the widget.
176
		$instance = array_merge( (array) $old_instance, (array) $new_instance );
177
178
		// set widget instance
179
		$this->sd->instance = $instance;
180
181
		if ( empty( $this->arguments ) ) {
182
			$this->sd->get_arguments();
183
		}
184
185
		// check for checkboxes
186
		if ( ! empty( $this->sd->arguments ) ) {
187
			foreach ( $this->sd->arguments as $argument ) {
188
				if ( isset( $argument['type'] ) && $argument['type'] == 'checkbox' && ! isset( $new_instance[ $argument['name'] ] ) ) {
189
					$instance[ $argument['name'] ] = '0';
190
				}
191
			}
192
		}
193
194
		return $instance;
195
	}
196
197
	/**
198
	 * Hides this widget from the block widgets inserter function.
199
	 *
200
	 * @param array $widget_types List of hidden widgets.
201
	 *
202
	 * @return array
203
	 */
204
	public function hide_widget( $widget_types ) {
205
		$widget_types[] = $this->id_base;
206
207
		return $widget_types;
208
	}
209
210
}
211