Passed
Push — master ( a08dcb...182caa )
by Brian
10:26
created

WPInv_GetPaid_Widget::output()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 69
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 33
nc 11
nop 3
dl 0
loc 69
rs 8.1475
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
    exit;
4
}
5
6
/**
7
 * getpaid button widget.
8
 *
9
 */
10
class WPInv_GetPaid_Widget extends WP_Super_Duper {
11
12
    /**
13
     * Register the widget with WordPress.
14
     *
15
     */
16
    public function __construct() {
17
18
        $options = array(
19
            'textdomain'    => 'invoicing',
20
            'block-icon'    => 'admin-site',
21
            'block-category'=> 'widgets',
22
            'block-keywords'=> "['invoicing','buy', 'buy item', 'form']",
23
            'class_name'     => __CLASS__,
24
            'base_id'       => 'getpaid',
25
            'name'          => __('Invoicing > GetPaid Button','invoicing'),
26
            'widget_ops'    => array(
27
                'classname'   => 'wpinv-getpaid-class wpi-g bsui',
28
                'description' => esc_html__('Displays a button that loads a payment form in a popup.','invoicing'),
29
            ),
30
            'arguments'     => array(
31
32
                'title'  => array(
33
                    'title'       => __( 'Widget title', 'invoicing' ),
34
                    'desc'        => __( 'Enter widget title.', 'invoicing' ),
35
                    'type'        => 'text',
36
                    'desc_tip'    => true,
37
                    'default'     => '',
38
                    'advanced'    => false
39
				),
40
41
                'form'  => array(
42
	                'title'       => __( 'Form', 'invoicing' ),
43
	                'desc'        => __( 'Enter a form id in case you want to display a specific payment form', 'invoicing' ),
44
	                'type'        => 'text',
45
	                'desc_tip'    => true,
46
	                'default'     => '',
47
	                'placeholder' => __('1','invoicing'),
48
	                'advanced'    => false
49
				),
50
51
				'item'  => array(
52
	                'title'       => __( 'Item', 'invoicing' ),
53
	                'desc'        => __( 'Enter an item id in case you want to sell an item', 'invoicing' ),
54
	                'type'        => 'text',
55
	                'desc_tip'    => true,
56
	                'default'     => '',
57
	                'placeholder' => __('1','invoicing'),
58
	                'advanced'    => false
59
				),
60
61
                'button'  => array(
62
	                'title'       => __( 'Button Label', 'invoicing' ),
63
	                'desc'        => __( 'Enter button label. Default "Buy Now".', 'invoicing' ),
64
	                'type'        => 'text',
65
	                'desc_tip'    => true,
66
	                'default'     => __( 'Buy Now', 'invoicing' ),
67
	                'advanced'    => false
68
				)
69
70
            )
71
72
        );
73
74
75
        parent::__construct( $options );
76
    }
77
78
	/**
79
	 * The Super block output function.
80
	 *
81
	 * @param array $args
82
	 * @param array $widget_args
83
	 * @param string $content
84
	 *
85
	 * @return string
86
	 */
87
    public function output( $args = array(), $widget_args = array(), $content = '' ) {
88
89
	    // Do we have a payment form?
90
		if ( empty( $args['form'] ) && empty( $args['item'] ) ) {
91
			return aui()->alert(
0 ignored issues
show
Bug introduced by
The function aui was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
			return /** @scrutinizer ignore-call */ aui()->alert(
Loading history...
92
				array(
93
					'type'    => 'warning',
94
					'content' => __( 'No payment form or item selected', 'invoicing' ),
95
				)
96
			);
97
98
		}
99
100
	    $defaults = array(
101
		    'item'    => '',
102
		    'button'  => __( 'Buy Now', 'invoicing' ),
103
		    'form'    => '',
104
	    );
105
106
	    /**
107
	     * Parse incoming $args into an array and merge it with $defaults
108
	     */
109
		$args = wp_parse_args( $args, $defaults );
110
		
111
		// Payment form?
112
		if ( ! empty( $args['form'] ) ) {
113
114
			// Ensure that it is published.
115
			if ( 'publish' != get_post_status( $args['form'] ) ) {
116
				return aui()->alert(
117
					array(
118
						'type'    => 'warning',
119
						'content' => __( 'This payment form is no longer active', 'invoicing' ),
120
					)
121
				);
122
			}
123
124
			$attrs = array(
125
				'form' => $args['form']
126
			);
127
128
		} else {
129
130
			// Ensure that it is published.
131
			if ( 'publish' != get_post_status( $args['item'] ) ) {
132
				return aui()->alert(
133
					array(
134
						'type'    => 'warning',
135
						'content' => __( 'This item is no longer active', 'invoicing' ),
136
					)
137
				);
138
			}
139
140
			$attrs = array(
141
				'item' => $args['item']
142
			);
143
144
		}
145
146
		$label = ! empty( $args['button'] ) ? sanitize_text_field( $args['button'] ) : __( 'Buy Now', 'invoicing' );
147
		$attr  = '';
148
149
		foreach( $attrs as $key => $val ) {
150
			$key  = sanitize_text_field( $key );
151
			$val  = esc_attr( $val );
152
			$attr .= " $key='$val' ";
153
		}
154
155
		return "<button class='btn btn-primary getpaid-payment-button' type='button' $attr>$label</button>";
156
157
    }
158
159
}
160