Passed
Pull Request — master (#236)
by Patrik
03:27
created

WPInv_Buy_Item_Widget::output()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
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( $args['post_id'] ) ? (int)$args['post_id'] : 0;
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
}