Passed
Push — master ( cf7cb5...555ee7 )
by Brian
09:37 queued 04:37
created

WPInv_GetPaid_Widget::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 60
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 43
nc 1
nop 0
dl 0
loc 60
rs 9.232
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'          => __('GetPaid','invoicing'),
26
            'widget_ops'    => array(
27
                'classname'   => 'getpaid bsui',
28
                'description' => esc_html__('Show payment forms or buttons.','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'       => __( 'Items', 'invoicing' ),
53
	                'desc'        => __( 'Enter comma separated list of invoicing item id and quantity (item_id|quantity). Ex. 101|2. This will be ignored in case you specify a form above. Enter 0 as the quantity to let users select their own quantities', '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', 'invoicing' ),
63
	                'desc'        => __( 'Enter button label in case you would like to display the forms in a popup.', 'invoicing' ),
64
	                'type'        => 'text',
65
	                'desc_tip'    => true,
66
	                'default'     => '',
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
	    // Is the shortcode set up correctly?
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
		// Payment form or button?
100
		if ( ! empty( $args['form'] ) ) {
101
			return $this->handle_payment_form(  $args );
102
		} else {
103
			return $this->handle_buy_item(  $args );
104
		}
105
106
	}
107
108
	/**
109
	 * Displaying a payment form
110
	 *
111
	 * @return string
112
	 */
113
    protected function handle_payment_form( $args = array() ) {
114
115
		if ( empty( $args['button'] ) ) {
116
			return getpaid_display_payment_form( $args['form'] );
117
		}
118
119
		return $this->payment_form_button( $args['form'], $args['button'] );
120
	}
121
122
	/**
123
	 * Displays a payment form button.
124
	 *
125
	 * @return string
126
	 */
127
    protected function payment_form_button( $form, $button ) {
128
		$label = sanitize_text_field( $button );
129
		$form  = esc_attr( $form );
130
		$nonce = wp_create_nonce('getpaid_ajax_form');
131
		return "<button class='btn btn-primary getpaid-payment-button' type='button' data-nonce='$nonce' data-form='$form'>$label</button>";
132
	}
133
134
	/**
135
	 * Selling an item
136
	 *
137
	 * @return string
138
	 */
139
    protected function handle_buy_item( $args = array() ) {
140
141
		if ( empty( $args['button'] ) ) {
142
			return $this->buy_item_form( $args['item'] );
143
		}
144
145
		return $this->buy_item_button( $args['item'], $args['button'] );
146
147
	}
148
149
	/**
150
	 * Displays a buy item form.
151
	 *
152
	 * @return string
153
	 */
154
    protected function buy_item_form( $item ) {
155
		$items = getpaid_convert_items_to_array( $item );
156
		return getpaid_display_item_payment_form( $items );
157
	}
158
159
	/**
160
	 * Displays a buy item button.
161
	 *
162
	 * @return string
163
	 */
164
    protected function buy_item_button( $item, $button ) {
165
		$label = sanitize_text_field( $button );
166
		$item  = esc_attr( $item );
167
		$nonce = wp_create_nonce('getpaid_ajax_form');
168
		return "<button class='btn btn-primary getpaid-payment-button' type='button' data-nonce='$nonce' data-item='$item'>$label</button>";
169
    }
170
171
}
172