Passed
Push — master ( f8ba18...48d7e6 )
by Brian
08:22 queued 04:03
created

WPInv_Payment_Form_Elements::edit_address_template()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 58
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 28
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 58
rs 9.472

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
3
if ( ! defined( 'ABSPATH' ) ) {
4
    exit; // Exit if accessed directly
5
}
6
7
class WPInv_Payment_Form_Elements {
8
9
    /**
10
     * @param array payment form elements
11
     */
12
    protected $elements;
13
14
    public function __construct() {
15
16
        foreach( $this->get_elements() as $element ) {
17
            $element = $element['type'];
18
19
            if ( method_exists( $this, "render_{$element}_template" ) ) {
20
                add_action( 'wpinv_payment_form_render_element_template', array( $this, "render_{$element}_template" ), 10, 2 );
21
            }
22
23
        }
24
25
    }
26
27
    /**
28
     * Returns all the elements that can be added to a form.
29
     */
30
    public function get_elements() {
31
32
        if ( ! empty( $this->elements ) ) {
33
            return $this->elements;
34
        }
35
36
        $this->elements = wpinv_get_data( 'payment-form-elements' );
37
38
        $this->elements = apply_filters( 'wpinv_filter_core_payment_form_elements', $this->elements );
39
        return $this->elements;
40
    }
41
42
    /**
43
     * Returns the restrict markup.
44
     */
45
    public function get_restrict_markup( $field, $field_type ) {
46
        $restrict = "$field.type=='$field_type'";
47
        return "v-if=\"$restrict\"";
48
    }
49
50
    /**
51
     * Returns an array of items for the currently being edited form.
52
     */
53
    public function get_form_items( $id = false ) {
54
        $form = new GetPaid_Payment_Form( $id );
55
56
        // Is this a default form?
57
        if ( $form->is_default() ) {
58
            return array();
59
        }
60
61
        return $form->get_items( 'view', 'arrays' );
62
    }
63
64
    /**
65
     * Converts form items for use.
66
     */
67
    public function convert_checkout_items( $items, $invoice ) {
68
69
        $converted = array();
70
        foreach ( $items as $item ) {
71
72
            $item_id = $item['id'];
73
            $_item   = new WPInv_Item( $item_id );
74
75
            if( ! $_item ) {
76
                continue;
77
            }
78
79
            $converted[] = array(
80
                'title'            => esc_html( wpinv_get_cart_item_name( $item ) ) . wpinv_get_item_suffix( $_item ),
81
                'id'               => $item['id'],
82
                'price'            => $item['subtotal'],
83
                'custom_price'     => $_item->get_is_dynamic_pricing(),
84
                'recurring'        => $_item->is_recurring(),
85
                'description'      => apply_filters( 'wpinv_checkout_cart_line_item_summary', '', $item, $_item, $invoice ),
86
                'minimum_price'    => $_item->get_minimum_price(),
87
                'allow_quantities' => false,
88
                'quantity'         => $item['quantity'],
89
                'required'         => true,
90
            );
91
        }
92
        return $converted;
93
94
    }
95
96
    /**
97
     * Converts an array of id => quantity for use.
98
     */
99
    public function convert_normal_items( $items ) {
100
101
        $converted = array();
102
        foreach ( $items as $item_id => $quantity ) {
103
104
            $item   = new WPInv_Item( $item_id );
105
106
            if( ! $item ) {
107
                continue;
108
            }
109
110
            $converted[] = array(
111
                'title'            => esc_html( $item->get_name() ) . wpinv_get_item_suffix( $item ),
112
                'id'               => $item_id,
113
                'price'            => $item->get_price(),
114
                'custom_price'     => $item->get_is_dynamic_pricing(),
115
                'recurring'        => $item->is_recurring(),
116
                'description'      => $item->get_summary(),
117
                'minimum_price'    => $item->get_minimum_price(),
118
                'allow_quantities' => ! empty( $quantity ),
119
                'quantity'         => empty( $quantity ) ? 1 : $quantity,
120
                'required'         => true,
121
            );
122
123
        }
124
125
        return $converted;
126
127
    }
128
129
    /**
130
     * Returns an array of elements for the currently being edited form.
131
     */
132
    public function get_form_elements( $id = false ) {
133
134
        if ( empty( $id ) ) {
135
            return wpinv_get_data( 'sample-payment-form' );
136
        }
137
        
138
        $form_elements = get_post_meta( $id, 'wpinv_form_elements', true );
139
140
        if ( is_array( $form_elements ) ) {
141
            return $form_elements;
142
        }
143
144
        return wpinv_get_data( 'sample-payment-form' );
145
    }
146
147
}
148