Passed
Push — master ( 03949c...9a5a12 )
by Brian
11:47 queued 07:40
created

GetPaid_WP_All_Import   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
dl 0
loc 83
rs 10
c 1
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get_item_fields() 0 20 1
A import_items() 0 22 6
A __construct() 0 7 1
A add_item_fields() 0 4 2
1
<?php
2
/**
3
 * Links GetPaid to WP All Import.
4
 *
5
 * @package GetPaid
6
 */
7
8
defined( 'ABSPATH' ) || exit;
9
10
include plugin_dir_path( __FILE__ ) . 'rapid-addon.php';
11
12
/**
13
 * WP All Import class.
14
 */
15
class GetPaid_WP_All_Import {
16
17
	/**
18
	 * @var RapidAddon
19
	 */
20
	protected $add_on;
21
22
    /**
23
	 * Class constructor.
24
	 */
25
    public function __construct() {
26
27
		// Register the add-on.
28
		$this->add_on = new RapidAddon( 'GetPaid', 'GetPaid_WP_All_Import_Addon' );
29
		$this->add_on->set_import_function( array( $this, 'import_items' ) );
30
		$this->add_item_fields();
31
		$this->add_on->run( array( 'post_types' => array( 'wpi_item' ) ) );
32
33
	}
34
35
	/**
36
	 * Retrieves item fields.
37
	 */
38
    public function get_item_fields() {
39
40
		return array(
41
			'description'          => __( 'Item Description', 'invoicing' ),
42
			'price'                => __( 'Price', 'invoicing' ),
43
			'vat_rule'             => __( 'VAT Rule', 'invoicing' ),
44
			'vat_class'            => __( 'VAT Class', 'invoicing' ),
45
			'type'                 => __( 'Trial Interval', 'invoicing' ),
46
			'custom_id'            => __( 'Custom ID', 'invoicing' ),
47
			'custom_name'          => __( 'Custom Name', 'invoicing' ),
48
			'custom_singular_name' => __( 'Custom Singular Name', 'invoicing' ),
49
			'is_dynamic_pricing'   => __( 'Users can name their own price', 'invoicing' ),
50
			'minimum_price'        => __( 'Minimum price', 'invoicing' ),
51
			'is_recurring'         => __( 'Is Recurring', 'invoicing' ),
52
			'recurring_period'     => __( 'Recurring Period', 'invoicing' ),
53
			'recurring_interval'   => __( 'Recurring Interval', 'invoicing' ),
54
			'recurring_limit'      => __( 'Recurring Limit', 'invoicing' ),
55
			'is_free_trial'        => __( 'Is free trial', 'invoicing' ),
56
			'trial_period'         => __( 'Trial Period', 'invoicing' ),
57
			'trial_interval'       => __( 'Trial Interval', 'invoicing' ),
58
		);
59
60
    }
61
62
	/**
63
	 * Registers item fields.
64
	 */
65
    public function add_item_fields() {
66
67
		foreach ( $this->get_item_fields() as $key => $label ) {
68
			$this->add_on->add_field( $key, $label, 'text' );
69
		}
70
71
    }
72
73
	/**
74
	 * Handles item imports.
75
	 */
76
    public function import_items( $post_id, $data, $import_options, $_post ) {
77
78
		$item     = wpinv_get_item( $post_id );
79
		$prepared = array();
80
81
		if ( empty( $item ) ) {
82
			return;
83
		}
84
85
		foreach ( array_keys( $this->get_item_fields() ) as $field ) { 
86
			// Make sure the user has allowed this field to be updated.
87
			if ( empty( $_post['ID'] ) || $this->add_on->can_update_meta( $field, $import_options ) ) { 
88
	
89
				// Update the custom field with the imported data.
90
				$prepared[ $field ] = $data[ $field ];
91
			} 
92
		}
93
94
		// Only update if we have something to update.
95
		if ( ! empty( $prepared ) ) {
96
			$item->set_props( $prepared );
97
			$item->save();
98
		}
99
100
    }
101
102
}
103
104
new GetPaid_WP_All_Import();
105