Passed
Pull Request — master (#236)
by Patrik
02:53
created

WPInv_Buy_Item_Widget::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 56
rs 8.9599
c 0
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
 * Invoicing buy item widget.
8
 *
9
 */
10
class WPInv_Buy_Item_Widget extends WP_Super_Duper {
11
12
    /**
13
     * Register the widget with WordPress.
14
     *
15
     */
16
    public function __construct() {
17
18
19
        $options = array(
20
            'textdomain'    => 'invoicing',
21
            'block-icon'    => 'admin-site',
22
            'block-category'=> 'widgets',
23
            'block-keywords'=> "['invoicing','buy', 'buy item']",
24
            'class_name'     => __CLASS__,
25
            'base_id'       => 'wpinv_buy',
26
            'name'          => __('Invoicing > Buy Item Button','invoicing'),
27
            'widget_ops'    => array(
28
                'classname'   => 'wpinv-buy-item-class',
29
                'description' => esc_html__('Displays buy invoicing item button.','invoicing'),
30
            ),
31
            'arguments'     => array(
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
                'buy_items'  => array(
41
	                'title'       => __( 'Items to buy', 'invoicing' ),
42
	                'desc'        => __( 'Enter comma separated list of invoicing item id and quantity (item_id|quantity). Ex. 101|2 ', 'invoicing' ),
43
	                'type'        => 'text',
44
	                'desc_tip'    => true,
45
	                'default'     => '',
46
	                'placeholder' => __('Items to buy','invoicing'),
47
	                'advanced'    => false
48
                ),
49
                'button_label'  => array(
50
	                'title'       => __( 'Button Label', 'invoicing' ),
51
	                'desc'        => __( 'Enter button label. Default "Buy Now".', 'invoicing' ),
52
	                'type'        => 'text',
53
	                'desc_tip'    => true,
54
	                'default'     => '',
55
	                'advanced'    => true
56
                ),
57
                'post_id'  => array(
58
	                'title'       => __( 'Post ID', 'invoicing' ),
59
	                'desc'        => __( 'Enter related post ID. This is for 3rd party add ons and not mandatory field.', 'invoicing' ),
60
	                'type'        => 'number',
61
	                'desc_tip'    => true,
62
	                'default'     => '',
63
	                'advanced'    => true
64
                ),
65
            )
66
67
        );
68
69
70
        parent::__construct( $options );
71
    }
72
73
	/**
74
	 * The Super block output function.
75
	 *
76
	 * @param array $args
77
	 * @param array $widget_args
78
	 * @param string $content
79
	 *
80
	 * @return string
81
	 */
82
    public function output( $args = array(), $widget_args = array(), $content = '' ) {
83
84
	    $defaults = array(
85
		    'buy_items'     => '', // should be used like: item_id|quantity,item_id|quantity,item_id|quantity
86
		    'button_label'  => __( 'Buy Now', 'invoicing' ), // the button title
87
		    'post_id'   => '', // any related post_id
88
	    );
89
90
	    /**
91
	     * Parse incoming $args into an array and merge it with $defaults
92
	     */
93
	    $args = wp_parse_args( $args, $defaults );
94
95
	    $post_id = isset( $a['post_id'] ) ? (int)$args['post_id'] : 0;
0 ignored issues
show
Bug introduced by
The variable $a seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
96
97
	    $html = '<div class="wpi-buy-button-wrapper wpi-g">';
98
	    $html .= '<button class="button button-primary wpi-buy-button" type="button" onclick="wpi_buy(this,\'' . $args['buy_items'] . '\',' . $post_id . ');">' . $args['button_label'] . '</button>';
99
	    $html .= wp_nonce_field( 'wpinv_buy_items', 'wpinv_buy_nonce', true, false );
100
	    $html .= '</div>';
101
102
	    return $html;
103
104
    }
105
106
}