-
Notifications
You must be signed in to change notification settings - Fork 16
/
class-nwsi-order-model.php
100 lines (86 loc) · 2.8 KB
/
class-nwsi-order-model.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
if ( !defined( "ABSPATH" ) ) {
exit;
}
if ( !class_exists( "WC_Order" ) ) {
error_log( "WC_Order class missing! Check if WooCommerce plugin is installed properly." );
return;
}
require_once( "interface-nwsi-model.php" );
if ( !class_exists( "NWSI_Order_Model" ) ) {
/**
* Class which extends WC_Order and provides methods for easier data access.
*
* @version 0.9.2
*/
class NWSI_Order_Model extends WC_Order implements NWSI_Model {
/**
* Class constructor.
*
* @override
* @see https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html
* @param int|WC_Order $order Defaults to empty string.
*/
public function __construct( $order = "" ) {
parent::__construct( $order );
}
/**
* Return order meta keys which are collected from the WC_Order class
*
* @since 0.1
* @return array
*/
public function get_property_keys() {
$data = $this->get_data();
$data_keys = $this->get_data_keys();
// keys which hold subarrays in $data
$parent_keys = array( "shipping", "billing" );
for ( $i = 0; $i < count( $data_keys ) ; $i++ ) {
foreach ( $parent_keys as $parent_key ) {
if ( isset( $data_keys[$i] ) && $parent_key === $data_keys[$i] ) {
unset( $data_keys[$i] );
foreach ( $data[$parent_key] as $child_key => $child_value ) {
array_push( $data_keys, $parent_key . "_" . $child_key );
}
}
}
}
$include_db_keys = false;
if ( has_filter( "nwsi_include_order_keys_from_database" ) ) {
$include_db_keys = (bool) apply_filters( "nwsi_include_order_keys_from_database" );
}
if ( $include_db_keys ) {
// combine with order meta keys from the database
require_once( NWSI_DIR_PATH . "includes/controllers/core/class-nwsi-db.php" );
$db = new NWSI_DB();
$keys = array_merge( $data_keys, $db->get_order_meta_keys() );
} else {
$keys = $data_keys;
}
$unique_keys = array_unique( $keys );
sort( $unique_keys, SORT_STRING );
if ( has_filter( "nwsi_order_property_keys" ) ) {
$unique_keys = (array) apply_filters( "nwsi_order_property_keys", $unique_keys );
}
return $unique_keys;
}
/**
* Return property value.
*
* @since 0.1
* @param string $property_name
* @return string
*/
public function get( $property_name ) {
$value = null;
if ( method_exists( $this, "get_" . $property_name ) ) {
$value = $this->{"get_" . $property_name}();
}
if ( has_filter( "nwsi_get_order_property_key_" . $property_name ) ) {
return apply_filters( "nwsi_get_order_property_key_" . $property_name, $value, $this );
} else {
return $value;
}
}
}
}