Completed
Push — master ( d4bee7...20651b )
by Brian
20s queued 16s
created

WPInv_Payment_Form_Widget   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 57
c 2
b 0
f 0
dl 0
loc 109
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 48 2
A output() 0 44 4
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
    exit; // Exit if accessed directly
5
}
6
7
/**
8
 * Invoicing payment form widget.
9
 *
10
 */
11
class WPInv_Payment_Form_Widget extends WP_Super_Duper {
12
13
    /**
14
     * Register the widget with WordPress.
15
     *
16
     */
17
    public function __construct() {
18
19
		$forms = get_posts(
20
			array(
21
				'post_type'      => 'wpi_payment_form',
22
				'orderby'        => 'title',
23
				'order'          => 'ASC',
24
				'posts_per_page' => -1,
25
				'post_status'    => array( 'publish' ),
26
			)
27
		);
28
29
		$options = array(
30
			'' => __('Select a Form','invoicing')
31
		);
32
33
		foreach( $forms as $form ) {
34
			$options[ $form->ID ] = $form->post_title;
35
		}
36
37
        $options = array(
38
            'textdomain'    => 'invoicing',
39
            'block-icon'    => 'admin-site',
40
            'block-category'=> 'widgets',
41
            'block-keywords'=> "['invoicing','buy', 'buy item', 'pay', 'payment form']",
42
            'class_name'     => __CLASS__,
43
            'base_id'       => 'wpinv_payment_form',
44
            'name'          => __('Invoicing > Payment Form','invoicing'),
45
            'widget_ops'    => array(
46
                'classname'   => 'wpinv-payment-form-class bsui',
47
                'description' => esc_html__('Displays a payment form.','invoicing'),
48
            ),
49
            'arguments'           => array(
50
                'form'            => array(
51
                    'title'       => __( 'Payment Form', 'invoicing' ),
52
                    'desc'        => __( 'Select your payment form.', 'invoicing' ),
53
					'type'        => 'select',
54
					'options'     =>  $options,
55
                    'desc_tip'    => true,
56
                    'default'     => '',
57
                    'advanced'    => false
58
                ),
59
            )
60
61
        );
62
63
64
        parent::__construct( $options );
65
    }
66
67
	/**
68
	 * The Super block output function.
69
	 *
70
	 * @param array $args
71
	 * @param array $widget_args
72
	 * @param string $content
73
	 *
74
	 * @return string
75
	 */
76
    public function output( $args = array(), $widget_args = array(), $content = '' ) {
77
		global $invoicing;
78
79
		// Do we have a payment form?
80
		if ( empty( $args['form'] ) ) {
81
			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

81
			return /** @scrutinizer ignore-call */ aui()->alert(
Loading history...
82
				array(
83
					'type'    => 'warning',
84
					'content' => __( 'No payment form selected', 'invoicing' ),
85
				)
86
			);
87
88
		}
89
90
		// If yes, ensure that it is published.
91
		if ( 'publish' != get_post_status( $args['form'] ) ) {
92
			return aui()->alert(
93
				array(
94
					'type'    => 'warning',
95
					'content' => __( 'This payment form is no longer active', 'invoicing' ),
96
				)
97
			);
98
		}
99
100
		// Get the form elements and items.
101
		$elements = $invoicing->form_elements->get_form_elements( $args['form'] );
102
		$items    = $invoicing->form_elements->get_form_items( $args['form'] );
103
104
		ob_start();
105
		echo "<form class='wpinv_payment_form'>";
106
		echo "<input type='hidden' name='form_id' value='{$args['form']}'/>";
107
		wp_nonce_field( 'wpinv_payment_form', 'wpinv_payment_form' );
108
109
		foreach ( $elements as $element ) {
110
			do_action( 'wpinv_frontend_render_payment_form_element', $element, $items, $args['form'] );
111
			do_action( "wpinv_frontend_render_payment_form_{$element['type']}", $element, $items, $args['form'] );
112
		}
113
114
		echo "<div class='wpinv_payment_form_errors alert alert-danger d-none'></div>";
115
		echo '</form>';
116
117
		$content = ob_get_clean();
118
		
119
		return str_replace( 'sr-only', '', $content );
120
121
    }
122
123
}
124