Passed
Push — master ( 9a5a12...d6bea2 )
by Brian
04:47
created

GetPaid_WP_All_Import::get_item_fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 20
rs 9.6666
c 1
b 0
f 0
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_ons;
21
22
	/**
23
	 * @var array
24
	 */
25
	protected $datastores = array(
26
		'item'     =>'WPInv_Item',
27
		'invoice'  =>'WPInv_Invoice',
28
		'discount' =>'WPInv_Discount',
29
	);
30
31
    /**
32
	 * Class constructor.
33
	 */
34
    public function __construct() {
35
36
		// Init each store separately.
37
		foreach ( array_keys( $this->datastores ) as $key ) {
38
			$this->init_store( $key );
39
		}
40
41
	}
42
43
	/**
44
	 * Inits a store.
45
	 */
46
    public function init_store( $key ) {
47
48
		// Register the add-on.
49
		$this->add_ons[ $key ] = new RapidAddon( 'GetPaid', 'getpaid_wp_al_import_' . $key );
50
51
		// Create import function.
52
		$import_function = function ( $post_id, $data, $import_options, $_post ) use ( $key ) {
53
			$this->import_store( $key, $post_id, $data, $import_options, $_post );
54
        };
55
56
		$this->add_ons[ $key ]->set_import_function( $import_function );
57
58
		// Register store fields.
59
		$this->add_store_fields( $key );
60
61
		// Only load on the correct post type.
62
		$this->add_ons[ $key ]->run( array( 'post_types' => array( 'wpi_' . $key ) ) );
63
64
		// Disable images.
65
		$this->add_ons[ $key ]->disable_default_images();
66
67
	}
68
69
	/**
70
	 * Retrieves store fields.
71
	 */
72
    public function get_store_fields( $key ) {
73
74
		// Fetch from data/invoice-schema.php, from data/discount-schema.php, from data/item-schema.php
75
		$fields = wpinv_get_data( $key . '-schema' );
76
77
		if ( empty( $fields ) ) {
78
			return array();
79
		}
80
81
		// Clean the fields.
82
		$prepared = array();
83
		foreach ( $fields as $id => $field ) {
84
85
			// Skip read only fields.
86
			if ( ! empty( $field['readonly'] ) ) {
87
				continue;
88
			}
89
90
			$prepared[ $id ] = $field;
91
92
		}
93
94
		return $prepared;
95
96
	}
97
98
	/**
99
	 * Registers store fields.
100
	 */
101
    public function add_store_fields( $key ) {
102
103
		foreach ( $this->get_store_fields( $key ) as $field_id => $data ) {
104
			$this->add_ons[ $key ]->add_field( $field_id, $data['description'], 'text' );
105
		}
106
107
    }
108
109
	/**
110
	 * Handles store imports.
111
	 */
112
    public function import_store( $key, $post_id, $data, $import_options, $_post ) {
113
114
		// Is the store class set?
115
		if ( ! isset( $this->datastores[ $key ] ) ) {
116
			return;
117
		}
118
119
		/**@var GetPaid_Data */
120
		$data_store = new $this->datastores[ $key ]( $post_id );
121
122
		// Abort if the invoice/item/discount does not exist.
123
		if ( ! $data_store->exists() ) {
124
			return;
125
		}
126
127
		// Prepare data props.
128
		$prepared = array();
129
130
		foreach ( array_keys( $this->get_store_fields( $key ) ) as $field ) { 
131
			// Make sure the user has allowed this field to be updated.
132
			if ( empty( $_post['ID'] ) || $this->add_ons[ $key ]->can_update_meta( $field, $import_options ) ) { 
133
	
134
				// Update the custom field with the imported data.
135
				$prepared[ $field ] = $data[ $field ];
136
			} 
137
		}
138
139
		// Only update if we have something to update.
140
		if ( ! empty( $prepared ) ) {
141
			$data_store->set_props( $prepared );
142
			$data_store->save();
143
		}
144
145
    }
146
147
}
148
149
new GetPaid_WP_All_Import();
150