@@ -15,322 +15,322 @@ |
||
15 | 15 | class GetPaid_Post_Types { |
16 | 16 | |
17 | 17 | /** |
18 | - * Hook in methods. |
|
19 | - */ |
|
20 | - public function __construct() { |
|
21 | - add_action( 'init', array( __CLASS__, 'register_post_types' ), 1 ); |
|
22 | - add_action( 'init', array( __CLASS__, 'register_post_status' ), 4 ); |
|
23 | - add_action( 'getpaid_flush_rewrite_rules', array( __CLASS__, 'flush_rewrite_rules' ) ); |
|
24 | - add_action( 'getpaid_after_register_post_types', array( __CLASS__, 'maybe_flush_rewrite_rules' ) ); |
|
25 | - } |
|
18 | + * Hook in methods. |
|
19 | + */ |
|
20 | + public function __construct() { |
|
21 | + add_action( 'init', array( __CLASS__, 'register_post_types' ), 1 ); |
|
22 | + add_action( 'init', array( __CLASS__, 'register_post_status' ), 4 ); |
|
23 | + add_action( 'getpaid_flush_rewrite_rules', array( __CLASS__, 'flush_rewrite_rules' ) ); |
|
24 | + add_action( 'getpaid_after_register_post_types', array( __CLASS__, 'maybe_flush_rewrite_rules' ) ); |
|
25 | + } |
|
26 | 26 | |
27 | - /** |
|
28 | - * Register core post types. |
|
29 | - */ |
|
30 | - public static function register_post_types() { |
|
27 | + /** |
|
28 | + * Register core post types. |
|
29 | + */ |
|
30 | + public static function register_post_types() { |
|
31 | 31 | |
32 | - if ( ! is_blog_installed() || post_type_exists( 'wpi_item' ) ) { |
|
33 | - return; |
|
34 | - } |
|
32 | + if ( ! is_blog_installed() || post_type_exists( 'wpi_item' ) ) { |
|
33 | + return; |
|
34 | + } |
|
35 | 35 | |
36 | - // Fires before registering post types. |
|
37 | - do_action( 'getpaid_register_post_types' ); |
|
36 | + // Fires before registering post types. |
|
37 | + do_action( 'getpaid_register_post_types' ); |
|
38 | 38 | |
39 | - // Register item post type. |
|
40 | - register_post_type( |
|
41 | - 'wpi_item', |
|
42 | - apply_filters( |
|
43 | - 'wpinv_register_post_type_invoice_item', |
|
44 | - array( |
|
45 | - 'labels' => array( |
|
46 | - 'name' => _x( 'Items', 'post type general name', 'invoicing' ), |
|
47 | - 'singular_name' => _x( 'Item', 'post type singular name', 'invoicing' ), |
|
48 | - 'menu_name' => _x( 'Items', 'admin menu', 'invoicing' ), |
|
49 | - 'name_admin_bar' => _x( 'Item', 'add new on admin bar', 'invoicing' ), |
|
50 | - 'add_new' => _x( 'Add New', 'Item', 'invoicing' ), |
|
51 | - 'add_new_item' => __( 'Add New Item', 'invoicing' ), |
|
52 | - 'new_item' => __( 'New Item', 'invoicing' ), |
|
53 | - 'edit_item' => __( 'Edit Item', 'invoicing' ), |
|
54 | - 'view_item' => __( 'View Item', 'invoicing' ), |
|
55 | - 'all_items' => __( 'Items', 'invoicing' ), |
|
56 | - 'search_items' => __( 'Search items', 'invoicing' ), |
|
57 | - 'parent_item_colon' => __( 'Parent item:', 'invoicing' ), |
|
58 | - 'not_found' => __( 'No items found.', 'invoicing' ), |
|
59 | - 'not_found_in_trash' => __( 'No items found in trash.', 'invoicing' ) |
|
60 | - ), |
|
61 | - 'description' => __( 'This is where you can add new invoice items.', 'invoicing' ), |
|
62 | - 'public' => false, |
|
63 | - 'has_archive' => false, |
|
64 | - '_builtin' => false, |
|
65 | - 'show_ui' => true, |
|
66 | - 'show_in_menu' => wpinv_current_user_can_manage_invoicing() ? 'wpinv' : false, |
|
67 | - 'show_in_nav_menus' => false, |
|
68 | - 'supports' => array( 'title', 'excerpt' ), |
|
69 | - 'rewrite' => false, |
|
70 | - 'query_var' => false, |
|
71 | - 'capability_type' => 'wpi_item', |
|
72 | - 'map_meta_cap' => true, |
|
73 | - 'show_in_admin_bar' => true, |
|
74 | - 'can_export' => true, |
|
75 | - ) |
|
76 | - ) |
|
77 | - ); |
|
39 | + // Register item post type. |
|
40 | + register_post_type( |
|
41 | + 'wpi_item', |
|
42 | + apply_filters( |
|
43 | + 'wpinv_register_post_type_invoice_item', |
|
44 | + array( |
|
45 | + 'labels' => array( |
|
46 | + 'name' => _x( 'Items', 'post type general name', 'invoicing' ), |
|
47 | + 'singular_name' => _x( 'Item', 'post type singular name', 'invoicing' ), |
|
48 | + 'menu_name' => _x( 'Items', 'admin menu', 'invoicing' ), |
|
49 | + 'name_admin_bar' => _x( 'Item', 'add new on admin bar', 'invoicing' ), |
|
50 | + 'add_new' => _x( 'Add New', 'Item', 'invoicing' ), |
|
51 | + 'add_new_item' => __( 'Add New Item', 'invoicing' ), |
|
52 | + 'new_item' => __( 'New Item', 'invoicing' ), |
|
53 | + 'edit_item' => __( 'Edit Item', 'invoicing' ), |
|
54 | + 'view_item' => __( 'View Item', 'invoicing' ), |
|
55 | + 'all_items' => __( 'Items', 'invoicing' ), |
|
56 | + 'search_items' => __( 'Search items', 'invoicing' ), |
|
57 | + 'parent_item_colon' => __( 'Parent item:', 'invoicing' ), |
|
58 | + 'not_found' => __( 'No items found.', 'invoicing' ), |
|
59 | + 'not_found_in_trash' => __( 'No items found in trash.', 'invoicing' ) |
|
60 | + ), |
|
61 | + 'description' => __( 'This is where you can add new invoice items.', 'invoicing' ), |
|
62 | + 'public' => false, |
|
63 | + 'has_archive' => false, |
|
64 | + '_builtin' => false, |
|
65 | + 'show_ui' => true, |
|
66 | + 'show_in_menu' => wpinv_current_user_can_manage_invoicing() ? 'wpinv' : false, |
|
67 | + 'show_in_nav_menus' => false, |
|
68 | + 'supports' => array( 'title', 'excerpt' ), |
|
69 | + 'rewrite' => false, |
|
70 | + 'query_var' => false, |
|
71 | + 'capability_type' => 'wpi_item', |
|
72 | + 'map_meta_cap' => true, |
|
73 | + 'show_in_admin_bar' => true, |
|
74 | + 'can_export' => true, |
|
75 | + ) |
|
76 | + ) |
|
77 | + ); |
|
78 | 78 | |
79 | - // Register payment form post type. |
|
80 | - register_post_type( |
|
81 | - 'wpi_payment_form', |
|
82 | - apply_filters( |
|
83 | - 'wpinv_register_post_type_payment_form', |
|
84 | - array( |
|
85 | - 'labels' => array( |
|
86 | - 'name' => _x( 'Payment Forms', 'post type general name', 'invoicing' ), |
|
87 | - 'singular_name' => _x( 'Payment Form', 'post type singular name', 'invoicing' ), |
|
88 | - 'menu_name' => _x( 'Payment Forms', 'admin menu', 'invoicing' ), |
|
89 | - 'name_admin_bar' => _x( 'Payment Form', 'add new on admin bar', 'invoicing' ), |
|
90 | - 'add_new' => _x( 'Add New', 'Payment Form', 'invoicing' ), |
|
91 | - 'add_new_item' => __( 'Add New Payment Form', 'invoicing' ), |
|
92 | - 'new_item' => __( 'New Payment Form', 'invoicing' ), |
|
93 | - 'edit_item' => __( 'Edit Payment Form', 'invoicing' ), |
|
94 | - 'view_item' => __( 'View Payment Form', 'invoicing' ), |
|
95 | - 'all_items' => __( 'Payment Forms', 'invoicing' ), |
|
96 | - 'search_items' => __( 'Search Payment Forms', 'invoicing' ), |
|
97 | - 'parent_item_colon' => __( 'Parent Payment Forms:', 'invoicing' ), |
|
98 | - 'not_found' => __( 'No payment forms found.', 'invoicing' ), |
|
99 | - 'not_found_in_trash' => __( 'No payment forms found in trash.', 'invoicing' ) |
|
100 | - ), |
|
101 | - 'description' => __( 'Add new payment forms.', 'invoicing' ), |
|
102 | - 'public' => false, |
|
103 | - 'show_ui' => true, |
|
104 | - 'show_in_menu' => wpinv_current_user_can_manage_invoicing() ? 'wpinv' : true, |
|
105 | - 'show_in_nav_menus' => false, |
|
106 | - 'query_var' => false, |
|
107 | - 'rewrite' => true, |
|
108 | - 'map_meta_cap' => true, |
|
109 | - 'has_archive' => false, |
|
110 | - 'hierarchical' => false, |
|
111 | - 'menu_position' => null, |
|
112 | - 'supports' => array( 'title' ), |
|
113 | - 'menu_icon' => 'dashicons-media-form', |
|
114 | - ) |
|
115 | - ) |
|
116 | - ); |
|
79 | + // Register payment form post type. |
|
80 | + register_post_type( |
|
81 | + 'wpi_payment_form', |
|
82 | + apply_filters( |
|
83 | + 'wpinv_register_post_type_payment_form', |
|
84 | + array( |
|
85 | + 'labels' => array( |
|
86 | + 'name' => _x( 'Payment Forms', 'post type general name', 'invoicing' ), |
|
87 | + 'singular_name' => _x( 'Payment Form', 'post type singular name', 'invoicing' ), |
|
88 | + 'menu_name' => _x( 'Payment Forms', 'admin menu', 'invoicing' ), |
|
89 | + 'name_admin_bar' => _x( 'Payment Form', 'add new on admin bar', 'invoicing' ), |
|
90 | + 'add_new' => _x( 'Add New', 'Payment Form', 'invoicing' ), |
|
91 | + 'add_new_item' => __( 'Add New Payment Form', 'invoicing' ), |
|
92 | + 'new_item' => __( 'New Payment Form', 'invoicing' ), |
|
93 | + 'edit_item' => __( 'Edit Payment Form', 'invoicing' ), |
|
94 | + 'view_item' => __( 'View Payment Form', 'invoicing' ), |
|
95 | + 'all_items' => __( 'Payment Forms', 'invoicing' ), |
|
96 | + 'search_items' => __( 'Search Payment Forms', 'invoicing' ), |
|
97 | + 'parent_item_colon' => __( 'Parent Payment Forms:', 'invoicing' ), |
|
98 | + 'not_found' => __( 'No payment forms found.', 'invoicing' ), |
|
99 | + 'not_found_in_trash' => __( 'No payment forms found in trash.', 'invoicing' ) |
|
100 | + ), |
|
101 | + 'description' => __( 'Add new payment forms.', 'invoicing' ), |
|
102 | + 'public' => false, |
|
103 | + 'show_ui' => true, |
|
104 | + 'show_in_menu' => wpinv_current_user_can_manage_invoicing() ? 'wpinv' : true, |
|
105 | + 'show_in_nav_menus' => false, |
|
106 | + 'query_var' => false, |
|
107 | + 'rewrite' => true, |
|
108 | + 'map_meta_cap' => true, |
|
109 | + 'has_archive' => false, |
|
110 | + 'hierarchical' => false, |
|
111 | + 'menu_position' => null, |
|
112 | + 'supports' => array( 'title' ), |
|
113 | + 'menu_icon' => 'dashicons-media-form', |
|
114 | + ) |
|
115 | + ) |
|
116 | + ); |
|
117 | 117 | |
118 | - // Register invoice post type. |
|
119 | - register_post_type( |
|
120 | - 'wpi_invoice', |
|
121 | - apply_filters( |
|
122 | - 'wpinv_register_post_type_invoice', |
|
123 | - array( |
|
124 | - 'labels' => array( |
|
125 | - 'name' => __( 'Invoices', 'invoicing' ), |
|
126 | - 'singular_name' => __( 'Invoice', 'invoicing' ), |
|
127 | - 'all_items' => __( 'Invoices', 'invoicing' ), |
|
128 | - 'menu_name' => _x( 'Invoices', 'Admin menu name', 'invoicing' ), |
|
129 | - 'add_new' => __( 'Add New', 'invoicing' ), |
|
130 | - 'add_new_item' => __( 'Add new invoice', 'invoicing' ), |
|
131 | - 'edit' => __( 'Edit', 'invoicing' ), |
|
132 | - 'edit_item' => __( 'Edit invoice', 'invoicing' ), |
|
133 | - 'new_item' => __( 'New invoice', 'invoicing' ), |
|
134 | - 'view_item' => __( 'View invoice', 'invoicing' ), |
|
135 | - 'view_items' => __( 'View Invoices', 'invoicing' ), |
|
136 | - 'search_items' => __( 'Search invoices', 'invoicing' ), |
|
137 | - 'not_found' => __( 'No invoices found', 'invoicing' ), |
|
138 | - 'not_found_in_trash' => __( 'No invoices found in trash', 'invoicing' ), |
|
139 | - 'parent' => __( 'Parent invoice', 'invoicing' ), |
|
140 | - 'featured_image' => __( 'Invoice image', 'invoicing' ), |
|
141 | - 'set_featured_image' => __( 'Set invoice image', 'invoicing' ), |
|
142 | - 'remove_featured_image' => __( 'Remove invoice image', 'invoicing' ), |
|
143 | - 'use_featured_image' => __( 'Use as invoice image', 'invoicing' ), |
|
144 | - 'insert_into_item' => __( 'Insert into invoice', 'invoicing' ), |
|
145 | - 'uploaded_to_this_item' => __( 'Uploaded to this invoice', 'invoicing' ), |
|
146 | - 'filter_items_list' => __( 'Filter invoices', 'invoicing' ), |
|
147 | - 'items_list_navigation' => __( 'Invoices navigation', 'invoicing' ), |
|
148 | - 'items_list' => __( 'Invoices list', 'invoicing' ), |
|
149 | - ), |
|
150 | - 'description' => __( 'This is where invoices are stored.', 'invoicing' ), |
|
151 | - 'public' => true, |
|
152 | - 'has_archive' => false, |
|
153 | - 'publicly_queryable' => true, |
|
154 | - 'exclude_from_search' => true, |
|
155 | - 'show_ui' => true, |
|
156 | - 'show_in_menu' => wpinv_current_user_can_manage_invoicing() ? 'wpinv' : false, |
|
157 | - 'show_in_nav_menus' => false, |
|
158 | - 'supports' => array( 'title', 'author', 'excerpt' ), |
|
159 | - 'rewrite' => array( |
|
160 | - 'slug' => 'invoice', |
|
161 | - 'with_front' => false, |
|
162 | - ), |
|
163 | - 'query_var' => false, |
|
164 | - 'capability_type' => 'wpi_invoice', |
|
165 | - 'map_meta_cap' => true, |
|
166 | - 'show_in_admin_bar' => true, |
|
167 | - 'can_export' => true, |
|
168 | - 'hierarchical' => false, |
|
169 | - 'menu_position' => null, |
|
170 | - 'menu_icon' => 'dashicons-media-spreadsheet', |
|
171 | - ) |
|
172 | - ) |
|
173 | - ); |
|
118 | + // Register invoice post type. |
|
119 | + register_post_type( |
|
120 | + 'wpi_invoice', |
|
121 | + apply_filters( |
|
122 | + 'wpinv_register_post_type_invoice', |
|
123 | + array( |
|
124 | + 'labels' => array( |
|
125 | + 'name' => __( 'Invoices', 'invoicing' ), |
|
126 | + 'singular_name' => __( 'Invoice', 'invoicing' ), |
|
127 | + 'all_items' => __( 'Invoices', 'invoicing' ), |
|
128 | + 'menu_name' => _x( 'Invoices', 'Admin menu name', 'invoicing' ), |
|
129 | + 'add_new' => __( 'Add New', 'invoicing' ), |
|
130 | + 'add_new_item' => __( 'Add new invoice', 'invoicing' ), |
|
131 | + 'edit' => __( 'Edit', 'invoicing' ), |
|
132 | + 'edit_item' => __( 'Edit invoice', 'invoicing' ), |
|
133 | + 'new_item' => __( 'New invoice', 'invoicing' ), |
|
134 | + 'view_item' => __( 'View invoice', 'invoicing' ), |
|
135 | + 'view_items' => __( 'View Invoices', 'invoicing' ), |
|
136 | + 'search_items' => __( 'Search invoices', 'invoicing' ), |
|
137 | + 'not_found' => __( 'No invoices found', 'invoicing' ), |
|
138 | + 'not_found_in_trash' => __( 'No invoices found in trash', 'invoicing' ), |
|
139 | + 'parent' => __( 'Parent invoice', 'invoicing' ), |
|
140 | + 'featured_image' => __( 'Invoice image', 'invoicing' ), |
|
141 | + 'set_featured_image' => __( 'Set invoice image', 'invoicing' ), |
|
142 | + 'remove_featured_image' => __( 'Remove invoice image', 'invoicing' ), |
|
143 | + 'use_featured_image' => __( 'Use as invoice image', 'invoicing' ), |
|
144 | + 'insert_into_item' => __( 'Insert into invoice', 'invoicing' ), |
|
145 | + 'uploaded_to_this_item' => __( 'Uploaded to this invoice', 'invoicing' ), |
|
146 | + 'filter_items_list' => __( 'Filter invoices', 'invoicing' ), |
|
147 | + 'items_list_navigation' => __( 'Invoices navigation', 'invoicing' ), |
|
148 | + 'items_list' => __( 'Invoices list', 'invoicing' ), |
|
149 | + ), |
|
150 | + 'description' => __( 'This is where invoices are stored.', 'invoicing' ), |
|
151 | + 'public' => true, |
|
152 | + 'has_archive' => false, |
|
153 | + 'publicly_queryable' => true, |
|
154 | + 'exclude_from_search' => true, |
|
155 | + 'show_ui' => true, |
|
156 | + 'show_in_menu' => wpinv_current_user_can_manage_invoicing() ? 'wpinv' : false, |
|
157 | + 'show_in_nav_menus' => false, |
|
158 | + 'supports' => array( 'title', 'author', 'excerpt' ), |
|
159 | + 'rewrite' => array( |
|
160 | + 'slug' => 'invoice', |
|
161 | + 'with_front' => false, |
|
162 | + ), |
|
163 | + 'query_var' => false, |
|
164 | + 'capability_type' => 'wpi_invoice', |
|
165 | + 'map_meta_cap' => true, |
|
166 | + 'show_in_admin_bar' => true, |
|
167 | + 'can_export' => true, |
|
168 | + 'hierarchical' => false, |
|
169 | + 'menu_position' => null, |
|
170 | + 'menu_icon' => 'dashicons-media-spreadsheet', |
|
171 | + ) |
|
172 | + ) |
|
173 | + ); |
|
174 | 174 | |
175 | - // Register discount post type. |
|
176 | - register_post_type( |
|
177 | - 'wpi_discount', |
|
178 | - apply_filters( |
|
179 | - 'wpinv_register_post_type_discount', |
|
180 | - array( |
|
181 | - 'labels' => array( |
|
182 | - 'name' => __( 'Discounts', 'invoicing' ), |
|
183 | - 'singular_name' => __( 'Discount', 'invoicing' ), |
|
184 | - 'all_items' => __( 'Discounts', 'invoicing' ), |
|
185 | - 'menu_name' => _x( 'Discounts', 'Admin menu name', 'invoicing' ), |
|
186 | - 'add_new' => __( 'Add New', 'invoicing' ), |
|
187 | - 'add_new_item' => __( 'Add new discount', 'invoicing' ), |
|
188 | - 'edit' => __( 'Edit', 'invoicing' ), |
|
189 | - 'edit_item' => __( 'Edit discount', 'invoicing' ), |
|
190 | - 'new_item' => __( 'New discount', 'invoicing' ), |
|
191 | - 'view_item' => __( 'View discount', 'invoicing' ), |
|
192 | - 'view_items' => __( 'View Discounts', 'invoicing' ), |
|
193 | - 'search_items' => __( 'Search discounts', 'invoicing' ), |
|
194 | - 'not_found' => __( 'No discounts found', 'invoicing' ), |
|
195 | - 'not_found_in_trash' => __( 'No discounts found in trash', 'invoicing' ), |
|
196 | - 'parent' => __( 'Parent discount', 'invoicing' ), |
|
197 | - 'featured_image' => __( 'Discount image', 'invoicing' ), |
|
198 | - 'set_featured_image' => __( 'Set discount image', 'invoicing' ), |
|
199 | - 'remove_featured_image' => __( 'Remove discount image', 'invoicing' ), |
|
200 | - 'use_featured_image' => __( 'Use as discount image', 'invoicing' ), |
|
201 | - 'insert_into_item' => __( 'Insert into discount', 'invoicing' ), |
|
202 | - 'uploaded_to_this_item' => __( 'Uploaded to this discount', 'invoicing' ), |
|
203 | - 'filter_items_list' => __( 'Filter discounts', 'invoicing' ), |
|
204 | - 'items_list_navigation' => __( 'Discount navigation', 'invoicing' ), |
|
205 | - 'items_list' => __( 'Discounts list', 'invoicing' ), |
|
206 | - ), |
|
207 | - 'description' => __( 'This is where you can add new discounts that users can use in invoices.', 'invoicing' ), |
|
208 | - 'public' => false, |
|
209 | - 'can_export' => true, |
|
210 | - '_builtin' => false, |
|
211 | - 'publicly_queryable' => false, |
|
212 | - 'exclude_from_search'=> true, |
|
213 | - 'show_ui' => true, |
|
214 | - 'show_in_menu' => wpinv_current_user_can_manage_invoicing() ? 'wpinv' : false, |
|
215 | - 'query_var' => false, |
|
216 | - 'rewrite' => false, |
|
217 | - 'capability_type' => 'wpi_discount', |
|
218 | - 'map_meta_cap' => true, |
|
219 | - 'has_archive' => false, |
|
220 | - 'hierarchical' => false, |
|
221 | - 'supports' => array( 'title', 'excerpt' ), |
|
222 | - 'show_in_nav_menus' => false, |
|
223 | - 'show_in_admin_bar' => true, |
|
224 | - 'menu_position' => null, |
|
225 | - ) |
|
226 | - ) |
|
227 | - ); |
|
175 | + // Register discount post type. |
|
176 | + register_post_type( |
|
177 | + 'wpi_discount', |
|
178 | + apply_filters( |
|
179 | + 'wpinv_register_post_type_discount', |
|
180 | + array( |
|
181 | + 'labels' => array( |
|
182 | + 'name' => __( 'Discounts', 'invoicing' ), |
|
183 | + 'singular_name' => __( 'Discount', 'invoicing' ), |
|
184 | + 'all_items' => __( 'Discounts', 'invoicing' ), |
|
185 | + 'menu_name' => _x( 'Discounts', 'Admin menu name', 'invoicing' ), |
|
186 | + 'add_new' => __( 'Add New', 'invoicing' ), |
|
187 | + 'add_new_item' => __( 'Add new discount', 'invoicing' ), |
|
188 | + 'edit' => __( 'Edit', 'invoicing' ), |
|
189 | + 'edit_item' => __( 'Edit discount', 'invoicing' ), |
|
190 | + 'new_item' => __( 'New discount', 'invoicing' ), |
|
191 | + 'view_item' => __( 'View discount', 'invoicing' ), |
|
192 | + 'view_items' => __( 'View Discounts', 'invoicing' ), |
|
193 | + 'search_items' => __( 'Search discounts', 'invoicing' ), |
|
194 | + 'not_found' => __( 'No discounts found', 'invoicing' ), |
|
195 | + 'not_found_in_trash' => __( 'No discounts found in trash', 'invoicing' ), |
|
196 | + 'parent' => __( 'Parent discount', 'invoicing' ), |
|
197 | + 'featured_image' => __( 'Discount image', 'invoicing' ), |
|
198 | + 'set_featured_image' => __( 'Set discount image', 'invoicing' ), |
|
199 | + 'remove_featured_image' => __( 'Remove discount image', 'invoicing' ), |
|
200 | + 'use_featured_image' => __( 'Use as discount image', 'invoicing' ), |
|
201 | + 'insert_into_item' => __( 'Insert into discount', 'invoicing' ), |
|
202 | + 'uploaded_to_this_item' => __( 'Uploaded to this discount', 'invoicing' ), |
|
203 | + 'filter_items_list' => __( 'Filter discounts', 'invoicing' ), |
|
204 | + 'items_list_navigation' => __( 'Discount navigation', 'invoicing' ), |
|
205 | + 'items_list' => __( 'Discounts list', 'invoicing' ), |
|
206 | + ), |
|
207 | + 'description' => __( 'This is where you can add new discounts that users can use in invoices.', 'invoicing' ), |
|
208 | + 'public' => false, |
|
209 | + 'can_export' => true, |
|
210 | + '_builtin' => false, |
|
211 | + 'publicly_queryable' => false, |
|
212 | + 'exclude_from_search'=> true, |
|
213 | + 'show_ui' => true, |
|
214 | + 'show_in_menu' => wpinv_current_user_can_manage_invoicing() ? 'wpinv' : false, |
|
215 | + 'query_var' => false, |
|
216 | + 'rewrite' => false, |
|
217 | + 'capability_type' => 'wpi_discount', |
|
218 | + 'map_meta_cap' => true, |
|
219 | + 'has_archive' => false, |
|
220 | + 'hierarchical' => false, |
|
221 | + 'supports' => array( 'title', 'excerpt' ), |
|
222 | + 'show_in_nav_menus' => false, |
|
223 | + 'show_in_admin_bar' => true, |
|
224 | + 'menu_position' => null, |
|
225 | + ) |
|
226 | + ) |
|
227 | + ); |
|
228 | 228 | |
229 | - do_action( 'getpaid_after_register_post_types' ); |
|
230 | - } |
|
229 | + do_action( 'getpaid_after_register_post_types' ); |
|
230 | + } |
|
231 | 231 | |
232 | - /** |
|
233 | - * Register our custom post statuses. |
|
234 | - */ |
|
235 | - public static function register_post_status() { |
|
232 | + /** |
|
233 | + * Register our custom post statuses. |
|
234 | + */ |
|
235 | + public static function register_post_status() { |
|
236 | 236 | |
237 | - $invoice_statuses = apply_filters( |
|
238 | - 'getpaid_register_invoice_post_statuses', |
|
239 | - array( |
|
237 | + $invoice_statuses = apply_filters( |
|
238 | + 'getpaid_register_invoice_post_statuses', |
|
239 | + array( |
|
240 | 240 | |
241 | - 'wpi-pending' => array( |
|
242 | - 'label' => _x( 'Pending Payment', 'Invoice status', 'invoicing' ), |
|
243 | - 'public' => true, |
|
244 | - 'exclude_from_search' => true, |
|
245 | - 'show_in_admin_all_list' => true, |
|
246 | - 'show_in_admin_status_list' => true, |
|
247 | - /* translators: %s: number of invoices */ |
|
248 | - 'label_count' => _n_noop( 'Pending Payment <span class="count">(%s)</span>', 'Pending Payment <span class="count">(%s)</span>', 'invoicing' ) |
|
249 | - ), |
|
241 | + 'wpi-pending' => array( |
|
242 | + 'label' => _x( 'Pending Payment', 'Invoice status', 'invoicing' ), |
|
243 | + 'public' => true, |
|
244 | + 'exclude_from_search' => true, |
|
245 | + 'show_in_admin_all_list' => true, |
|
246 | + 'show_in_admin_status_list' => true, |
|
247 | + /* translators: %s: number of invoices */ |
|
248 | + 'label_count' => _n_noop( 'Pending Payment <span class="count">(%s)</span>', 'Pending Payment <span class="count">(%s)</span>', 'invoicing' ) |
|
249 | + ), |
|
250 | 250 | |
251 | - 'wpi-processing' => array( |
|
252 | - 'label' => _x( 'Processing', 'Invoice status', 'invoicing' ), |
|
253 | - 'public' => true, |
|
254 | - 'exclude_from_search' => true, |
|
255 | - 'show_in_admin_all_list' => true, |
|
256 | - 'show_in_admin_status_list' => true, |
|
257 | - /* translators: %s: number of invoices */ |
|
258 | - 'label_count' => _n_noop( 'Processing <span class="count">(%s)</span>', 'Processing <span class="count">(%s)</span>', 'invoicing' ) |
|
259 | - ), |
|
251 | + 'wpi-processing' => array( |
|
252 | + 'label' => _x( 'Processing', 'Invoice status', 'invoicing' ), |
|
253 | + 'public' => true, |
|
254 | + 'exclude_from_search' => true, |
|
255 | + 'show_in_admin_all_list' => true, |
|
256 | + 'show_in_admin_status_list' => true, |
|
257 | + /* translators: %s: number of invoices */ |
|
258 | + 'label_count' => _n_noop( 'Processing <span class="count">(%s)</span>', 'Processing <span class="count">(%s)</span>', 'invoicing' ) |
|
259 | + ), |
|
260 | 260 | |
261 | - 'wpi-onhold' => array( |
|
262 | - 'label' => _x( 'On Hold', 'Invoice status', 'invoicing' ), |
|
263 | - 'public' => true, |
|
264 | - 'exclude_from_search' => true, |
|
265 | - 'show_in_admin_all_list' => true, |
|
266 | - 'show_in_admin_status_list' => true, |
|
267 | - /* translators: %s: number of invoices */ |
|
268 | - 'label_count' => _n_noop( 'On Hold <span class="count">(%s)</span>', 'On Hold <span class="count">(%s)</span>', 'invoicing' ) |
|
269 | - ), |
|
261 | + 'wpi-onhold' => array( |
|
262 | + 'label' => _x( 'On Hold', 'Invoice status', 'invoicing' ), |
|
263 | + 'public' => true, |
|
264 | + 'exclude_from_search' => true, |
|
265 | + 'show_in_admin_all_list' => true, |
|
266 | + 'show_in_admin_status_list' => true, |
|
267 | + /* translators: %s: number of invoices */ |
|
268 | + 'label_count' => _n_noop( 'On Hold <span class="count">(%s)</span>', 'On Hold <span class="count">(%s)</span>', 'invoicing' ) |
|
269 | + ), |
|
270 | 270 | |
271 | - 'wpi-cancelled' => array( |
|
272 | - 'label' => _x( 'Cancelled', 'Invoice status', 'invoicing' ), |
|
273 | - 'public' => true, |
|
274 | - 'exclude_from_search' => true, |
|
275 | - 'show_in_admin_all_list' => true, |
|
276 | - 'show_in_admin_status_list' => true, |
|
277 | - /* translators: %s: number of invoices */ |
|
278 | - 'label_count' => _n_noop( 'Cancelled <span class="count">(%s)</span>', 'Cancelled <span class="count">(%s)</span>', 'invoicing' ) |
|
279 | - ), |
|
271 | + 'wpi-cancelled' => array( |
|
272 | + 'label' => _x( 'Cancelled', 'Invoice status', 'invoicing' ), |
|
273 | + 'public' => true, |
|
274 | + 'exclude_from_search' => true, |
|
275 | + 'show_in_admin_all_list' => true, |
|
276 | + 'show_in_admin_status_list' => true, |
|
277 | + /* translators: %s: number of invoices */ |
|
278 | + 'label_count' => _n_noop( 'Cancelled <span class="count">(%s)</span>', 'Cancelled <span class="count">(%s)</span>', 'invoicing' ) |
|
279 | + ), |
|
280 | 280 | |
281 | - 'wpi-refunded' => array( |
|
282 | - 'label' => _x( 'Refunded', 'Invoice status', 'invoicing' ), |
|
283 | - 'public' => true, |
|
284 | - 'exclude_from_search' => true, |
|
285 | - 'show_in_admin_all_list' => true, |
|
286 | - 'show_in_admin_status_list' => true, |
|
287 | - /* translators: %s: number of invoices */ |
|
288 | - 'label_count' => _n_noop( 'Refunded <span class="count">(%s)</span>', 'Refunded <span class="count">(%s)</span>', 'invoicing' ) |
|
289 | - ), |
|
281 | + 'wpi-refunded' => array( |
|
282 | + 'label' => _x( 'Refunded', 'Invoice status', 'invoicing' ), |
|
283 | + 'public' => true, |
|
284 | + 'exclude_from_search' => true, |
|
285 | + 'show_in_admin_all_list' => true, |
|
286 | + 'show_in_admin_status_list' => true, |
|
287 | + /* translators: %s: number of invoices */ |
|
288 | + 'label_count' => _n_noop( 'Refunded <span class="count">(%s)</span>', 'Refunded <span class="count">(%s)</span>', 'invoicing' ) |
|
289 | + ), |
|
290 | 290 | |
291 | - 'wpi-failed' => array( |
|
292 | - 'label' => _x( 'Failed', 'Invoice status', 'invoicing' ), |
|
293 | - 'public' => true, |
|
294 | - 'exclude_from_search' => true, |
|
295 | - 'show_in_admin_all_list' => true, |
|
296 | - 'show_in_admin_status_list' => true, |
|
297 | - /* translators: %s: number of invoices */ |
|
298 | - 'label_count' => _n_noop( 'Failed <span class="count">(%s)</span>', 'Failed <span class="count">(%s)</span>', 'invoicing' ) |
|
299 | - ), |
|
291 | + 'wpi-failed' => array( |
|
292 | + 'label' => _x( 'Failed', 'Invoice status', 'invoicing' ), |
|
293 | + 'public' => true, |
|
294 | + 'exclude_from_search' => true, |
|
295 | + 'show_in_admin_all_list' => true, |
|
296 | + 'show_in_admin_status_list' => true, |
|
297 | + /* translators: %s: number of invoices */ |
|
298 | + 'label_count' => _n_noop( 'Failed <span class="count">(%s)</span>', 'Failed <span class="count">(%s)</span>', 'invoicing' ) |
|
299 | + ), |
|
300 | 300 | |
301 | - 'wpi-renewal' => array( |
|
302 | - 'label' => _x( 'Renewal', 'Invoice status', 'invoicing' ), |
|
303 | - 'public' => true, |
|
304 | - 'exclude_from_search' => true, |
|
305 | - 'show_in_admin_all_list' => true, |
|
306 | - 'show_in_admin_status_list' => true, |
|
307 | - /* translators: %s: number of invoices */ |
|
308 | - 'label_count' => _n_noop( 'Renewal <span class="count">(%s)</span>', 'Renewal <span class="count">(%s)</span>', 'invoicing' ) |
|
309 | - ) |
|
310 | - ) |
|
311 | - ); |
|
301 | + 'wpi-renewal' => array( |
|
302 | + 'label' => _x( 'Renewal', 'Invoice status', 'invoicing' ), |
|
303 | + 'public' => true, |
|
304 | + 'exclude_from_search' => true, |
|
305 | + 'show_in_admin_all_list' => true, |
|
306 | + 'show_in_admin_status_list' => true, |
|
307 | + /* translators: %s: number of invoices */ |
|
308 | + 'label_count' => _n_noop( 'Renewal <span class="count">(%s)</span>', 'Renewal <span class="count">(%s)</span>', 'invoicing' ) |
|
309 | + ) |
|
310 | + ) |
|
311 | + ); |
|
312 | 312 | |
313 | - foreach ( $invoice_statuses as $invoice_statuse => $args ) { |
|
314 | - register_post_status( $invoice_statuse, $args ); |
|
315 | - } |
|
316 | - } |
|
313 | + foreach ( $invoice_statuses as $invoice_statuse => $args ) { |
|
314 | + register_post_status( $invoice_statuse, $args ); |
|
315 | + } |
|
316 | + } |
|
317 | 317 | |
318 | - /** |
|
319 | - * Flush rewrite rules. |
|
320 | - */ |
|
321 | - public static function flush_rewrite_rules() { |
|
322 | - flush_rewrite_rules(); |
|
323 | - } |
|
318 | + /** |
|
319 | + * Flush rewrite rules. |
|
320 | + */ |
|
321 | + public static function flush_rewrite_rules() { |
|
322 | + flush_rewrite_rules(); |
|
323 | + } |
|
324 | 324 | |
325 | - /** |
|
326 | - * Flush rules to prevent 404. |
|
327 | - * |
|
328 | - */ |
|
329 | - public static function maybe_flush_rewrite_rules() { |
|
330 | - if ( ! get_option( 'getpaid_flushed_rewrite_rules' ) ) { |
|
331 | - update_option( 'getpaid_flushed_rewrite_rules', '1' ); |
|
332 | - self::flush_rewrite_rules(); |
|
333 | - } |
|
334 | - } |
|
325 | + /** |
|
326 | + * Flush rules to prevent 404. |
|
327 | + * |
|
328 | + */ |
|
329 | + public static function maybe_flush_rewrite_rules() { |
|
330 | + if ( ! get_option( 'getpaid_flushed_rewrite_rules' ) ) { |
|
331 | + update_option( 'getpaid_flushed_rewrite_rules', '1' ); |
|
332 | + self::flush_rewrite_rules(); |
|
333 | + } |
|
334 | + } |
|
335 | 335 | |
336 | 336 | } |
@@ -9,7 +9,7 @@ discard block |
||
9 | 9 | */ |
10 | 10 | |
11 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
12 | - exit; |
|
12 | + exit; |
|
13 | 13 | } |
14 | 14 | |
15 | 15 | /** |
@@ -21,356 +21,356 @@ discard block |
||
21 | 21 | */ |
22 | 22 | abstract class GetPaid_Data { |
23 | 23 | |
24 | - /** |
|
25 | - * ID for this object. |
|
26 | - * |
|
27 | - * @since 1.0.19 |
|
28 | - * @var int |
|
29 | - */ |
|
30 | - protected $id = 0; |
|
31 | - |
|
32 | - /** |
|
33 | - * Core data for this object. Name value pairs (name + default value). |
|
34 | - * |
|
35 | - * @since 1.0.19 |
|
36 | - * @var array |
|
37 | - */ |
|
38 | - protected $data = array(); |
|
39 | - |
|
40 | - /** |
|
41 | - * Core data changes for this object. |
|
42 | - * |
|
43 | - * @since 1.0.19 |
|
44 | - * @var array |
|
45 | - */ |
|
46 | - protected $changes = array(); |
|
47 | - |
|
48 | - /** |
|
49 | - * This is false until the object is read from the DB. |
|
50 | - * |
|
51 | - * @since 1.0.19 |
|
52 | - * @var bool |
|
53 | - */ |
|
54 | - protected $object_read = false; |
|
55 | - |
|
56 | - /** |
|
57 | - * This is the name of this object type. |
|
58 | - * |
|
59 | - * @since 1.0.19 |
|
60 | - * @var string |
|
61 | - */ |
|
62 | - protected $object_type = 'data'; |
|
63 | - |
|
64 | - /** |
|
65 | - * Extra data for this object. Name value pairs (name + default value). |
|
66 | - * Used as a standard way for sub classes (like item types) to add |
|
67 | - * additional information to an inherited class. |
|
68 | - * |
|
69 | - * @since 1.0.19 |
|
70 | - * @var array |
|
71 | - */ |
|
72 | - protected $extra_data = array(); |
|
73 | - |
|
74 | - /** |
|
75 | - * Set to _data on construct so we can track and reset data if needed. |
|
76 | - * |
|
77 | - * @since 1.0.19 |
|
78 | - * @var array |
|
79 | - */ |
|
80 | - protected $default_data = array(); |
|
81 | - |
|
82 | - /** |
|
83 | - * Contains a reference to the data store for this class. |
|
84 | - * |
|
85 | - * @since 1.0.19 |
|
86 | - * @var GetPaid_Data_Store |
|
87 | - */ |
|
88 | - protected $data_store; |
|
89 | - |
|
90 | - /** |
|
91 | - * Stores meta in cache for future reads. |
|
92 | - * A group must be set to to enable caching. |
|
93 | - * |
|
94 | - * @since 1.0.19 |
|
95 | - * @var string |
|
96 | - */ |
|
97 | - protected $cache_group = ''; |
|
98 | - |
|
99 | - /** |
|
100 | - * Stores the last error. |
|
101 | - * |
|
102 | - * @since 1.0.19 |
|
103 | - * @var string |
|
104 | - */ |
|
105 | - public $last_error = ''; |
|
106 | - |
|
107 | - /** |
|
108 | - * Stores additional meta data. |
|
109 | - * |
|
110 | - * @since 1.0.19 |
|
111 | - * @var array |
|
112 | - */ |
|
113 | - protected $meta_data = null; |
|
114 | - |
|
115 | - /** |
|
116 | - * Default constructor. |
|
117 | - * |
|
118 | - * @param int|object|array $read ID to load from the DB (optional) or already queried data. |
|
119 | - */ |
|
120 | - public function __construct( $read = 0 ) { |
|
121 | - $this->data = array_merge( $this->data, $this->extra_data ); |
|
122 | - $this->default_data = $this->data; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Only store the object ID to avoid serializing the data object instance. |
|
127 | - * |
|
128 | - * @return array |
|
129 | - */ |
|
130 | - public function __sleep() { |
|
131 | - return array( 'id' ); |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * Re-run the constructor with the object ID. |
|
136 | - * |
|
137 | - * If the object no longer exists, remove the ID. |
|
138 | - */ |
|
139 | - public function __wakeup() { |
|
140 | - $this->__construct( absint( $this->id ) ); |
|
141 | - |
|
142 | - if ( ! empty( $this->last_error ) ) { |
|
143 | - $this->set_id( 0 ); |
|
144 | - } |
|
145 | - |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * When the object is cloned, make sure meta is duplicated correctly. |
|
150 | - * |
|
151 | - * @since 1.0.19 |
|
152 | - */ |
|
153 | - public function __clone() { |
|
154 | - $this->maybe_read_meta_data(); |
|
155 | - if ( ! empty( $this->meta_data ) ) { |
|
156 | - foreach ( $this->meta_data as $array_key => $meta ) { |
|
157 | - $this->meta_data[ $array_key ] = clone $meta; |
|
158 | - if ( ! empty( $meta->id ) ) { |
|
159 | - $this->meta_data[ $array_key ]->id = null; |
|
160 | - } |
|
161 | - } |
|
162 | - } |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Get the data store. |
|
167 | - * |
|
168 | - * @since 1.0.19 |
|
169 | - * @return object |
|
170 | - */ |
|
171 | - public function get_data_store() { |
|
172 | - return $this->data_store; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Get the object type. |
|
177 | - * |
|
178 | - * @since 1.0.19 |
|
179 | - * @return string |
|
180 | - */ |
|
181 | - public function get_object_type() { |
|
182 | - return $this->object_type; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Returns the unique ID for this object. |
|
187 | - * |
|
188 | - * @since 1.0.19 |
|
189 | - * @return int |
|
190 | - */ |
|
191 | - public function get_id() { |
|
192 | - return $this->id; |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Get form status. |
|
197 | - * |
|
198 | - * @since 1.0.19 |
|
199 | - * @param string $context View or edit context. |
|
200 | - * @return string |
|
201 | - */ |
|
202 | - public function get_status( $context = 'view' ) { |
|
203 | - return $this->get_prop( 'status', $context ); |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Delete an object, set the ID to 0, and return result. |
|
208 | - * |
|
209 | - * @since 1.0.19 |
|
210 | - * @param bool $force_delete Should the data be deleted permanently. |
|
211 | - * @return bool result |
|
212 | - */ |
|
213 | - public function delete( $force_delete = false ) { |
|
214 | - if ( $this->data_store ) { |
|
215 | - $this->data_store->delete( $this, array( 'force_delete' => $force_delete ) ); |
|
216 | - $this->set_id( 0 ); |
|
217 | - return true; |
|
218 | - } |
|
219 | - return false; |
|
220 | - } |
|
221 | - |
|
222 | - /** |
|
223 | - * Save should create or update based on object existence. |
|
224 | - * |
|
225 | - * @since 1.0.19 |
|
226 | - * @return int |
|
227 | - */ |
|
228 | - public function save() { |
|
229 | - if ( ! $this->data_store ) { |
|
230 | - return $this->get_id(); |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Trigger action before saving to the DB. Allows you to adjust object props before save. |
|
235 | - * |
|
236 | - * @param GetPaid_Data $this The object being saved. |
|
237 | - * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
238 | - */ |
|
239 | - do_action( 'getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
240 | - |
|
241 | - if ( $this->get_id() ) { |
|
242 | - $this->data_store->update( $this ); |
|
243 | - } else { |
|
244 | - $this->data_store->create( $this ); |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * Trigger action after saving to the DB. |
|
249 | - * |
|
250 | - * @param GetPaid_Data $this The object being saved. |
|
251 | - * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
252 | - */ |
|
253 | - do_action( 'getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
254 | - |
|
255 | - return $this->get_id(); |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * Change data to JSON format. |
|
260 | - * |
|
261 | - * @since 1.0.19 |
|
262 | - * @return string Data in JSON format. |
|
263 | - */ |
|
264 | - public function __toString() { |
|
265 | - return wp_json_encode( $this->get_data() ); |
|
266 | - } |
|
267 | - |
|
268 | - /** |
|
269 | - * Returns all data for this object. |
|
270 | - * |
|
271 | - * @since 1.0.19 |
|
272 | - * @return array |
|
273 | - */ |
|
274 | - public function get_data() { |
|
275 | - return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) ); |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * Returns array of expected data keys for this object. |
|
280 | - * |
|
281 | - * @since 1.0.19 |
|
282 | - * @return array |
|
283 | - */ |
|
284 | - public function get_data_keys() { |
|
285 | - return array_keys( $this->data ); |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * Returns all "extra" data keys for an object (for sub objects like item types). |
|
290 | - * |
|
291 | - * @since 1.0.19 |
|
292 | - * @return array |
|
293 | - */ |
|
294 | - public function get_extra_data_keys() { |
|
295 | - return array_keys( $this->extra_data ); |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * Filter null meta values from array. |
|
300 | - * |
|
301 | - * @since 1.0.19 |
|
302 | - * @param mixed $meta Meta value to check. |
|
303 | - * @return bool |
|
304 | - */ |
|
305 | - protected function filter_null_meta( $meta ) { |
|
306 | - return ! is_null( $meta->value ); |
|
307 | - } |
|
308 | - |
|
309 | - /** |
|
310 | - * Get All Meta Data. |
|
311 | - * |
|
312 | - * @since 1.0.19 |
|
313 | - * @return array of objects. |
|
314 | - */ |
|
315 | - public function get_meta_data() { |
|
316 | - $this->maybe_read_meta_data(); |
|
317 | - return array_values( array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ) ); |
|
318 | - } |
|
319 | - |
|
320 | - /** |
|
321 | - * Check if the key is an internal one. |
|
322 | - * |
|
323 | - * @since 1.0.19 |
|
324 | - * @param string $key Key to check. |
|
325 | - * @return bool true if it's an internal key, false otherwise |
|
326 | - */ |
|
327 | - protected function is_internal_meta_key( $key ) { |
|
328 | - $internal_meta_key = ! empty( $key ) && $this->data_store && in_array( $key, $this->data_store->get_internal_meta_keys(), true ); |
|
329 | - |
|
330 | - if ( ! $internal_meta_key ) { |
|
331 | - return false; |
|
332 | - } |
|
333 | - |
|
334 | - $has_setter_or_getter = is_callable( array( $this, 'set_' . $key ) ) || is_callable( array( $this, 'get_' . $key ) ); |
|
335 | - |
|
336 | - if ( ! $has_setter_or_getter ) { |
|
337 | - return false; |
|
338 | - } |
|
339 | - |
|
340 | - /* translators: %s: $key Key to check */ |
|
341 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'getpaid' ), $key ), '1.0.19' ); |
|
342 | - |
|
343 | - return true; |
|
344 | - } |
|
345 | - |
|
346 | - /** |
|
347 | - * Magic method for setting data fields. |
|
348 | - * |
|
349 | - * This method does not update custom fields in the database. |
|
350 | - * |
|
351 | - * @since 1.0.19 |
|
352 | - * @access public |
|
353 | - * |
|
354 | - */ |
|
355 | - public function __set( $key, $value ) { |
|
356 | - |
|
357 | - if ( 'id' == strtolower( $key ) ) { |
|
358 | - return $this->set_id( $value ); |
|
359 | - } |
|
360 | - |
|
361 | - if ( method_exists( $this, "set_$key") ) { |
|
362 | - |
|
363 | - /* translators: %s: $key Key to set */ |
|
364 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'getpaid' ), $key ), '1.0.19' ); |
|
365 | - |
|
366 | - call_user_func( array( $this, "set_$key" ), $value ); |
|
367 | - } else { |
|
368 | - $this->set_prop( $key, $value ); |
|
369 | - } |
|
370 | - |
|
371 | - } |
|
372 | - |
|
373 | - /** |
|
24 | + /** |
|
25 | + * ID for this object. |
|
26 | + * |
|
27 | + * @since 1.0.19 |
|
28 | + * @var int |
|
29 | + */ |
|
30 | + protected $id = 0; |
|
31 | + |
|
32 | + /** |
|
33 | + * Core data for this object. Name value pairs (name + default value). |
|
34 | + * |
|
35 | + * @since 1.0.19 |
|
36 | + * @var array |
|
37 | + */ |
|
38 | + protected $data = array(); |
|
39 | + |
|
40 | + /** |
|
41 | + * Core data changes for this object. |
|
42 | + * |
|
43 | + * @since 1.0.19 |
|
44 | + * @var array |
|
45 | + */ |
|
46 | + protected $changes = array(); |
|
47 | + |
|
48 | + /** |
|
49 | + * This is false until the object is read from the DB. |
|
50 | + * |
|
51 | + * @since 1.0.19 |
|
52 | + * @var bool |
|
53 | + */ |
|
54 | + protected $object_read = false; |
|
55 | + |
|
56 | + /** |
|
57 | + * This is the name of this object type. |
|
58 | + * |
|
59 | + * @since 1.0.19 |
|
60 | + * @var string |
|
61 | + */ |
|
62 | + protected $object_type = 'data'; |
|
63 | + |
|
64 | + /** |
|
65 | + * Extra data for this object. Name value pairs (name + default value). |
|
66 | + * Used as a standard way for sub classes (like item types) to add |
|
67 | + * additional information to an inherited class. |
|
68 | + * |
|
69 | + * @since 1.0.19 |
|
70 | + * @var array |
|
71 | + */ |
|
72 | + protected $extra_data = array(); |
|
73 | + |
|
74 | + /** |
|
75 | + * Set to _data on construct so we can track and reset data if needed. |
|
76 | + * |
|
77 | + * @since 1.0.19 |
|
78 | + * @var array |
|
79 | + */ |
|
80 | + protected $default_data = array(); |
|
81 | + |
|
82 | + /** |
|
83 | + * Contains a reference to the data store for this class. |
|
84 | + * |
|
85 | + * @since 1.0.19 |
|
86 | + * @var GetPaid_Data_Store |
|
87 | + */ |
|
88 | + protected $data_store; |
|
89 | + |
|
90 | + /** |
|
91 | + * Stores meta in cache for future reads. |
|
92 | + * A group must be set to to enable caching. |
|
93 | + * |
|
94 | + * @since 1.0.19 |
|
95 | + * @var string |
|
96 | + */ |
|
97 | + protected $cache_group = ''; |
|
98 | + |
|
99 | + /** |
|
100 | + * Stores the last error. |
|
101 | + * |
|
102 | + * @since 1.0.19 |
|
103 | + * @var string |
|
104 | + */ |
|
105 | + public $last_error = ''; |
|
106 | + |
|
107 | + /** |
|
108 | + * Stores additional meta data. |
|
109 | + * |
|
110 | + * @since 1.0.19 |
|
111 | + * @var array |
|
112 | + */ |
|
113 | + protected $meta_data = null; |
|
114 | + |
|
115 | + /** |
|
116 | + * Default constructor. |
|
117 | + * |
|
118 | + * @param int|object|array $read ID to load from the DB (optional) or already queried data. |
|
119 | + */ |
|
120 | + public function __construct( $read = 0 ) { |
|
121 | + $this->data = array_merge( $this->data, $this->extra_data ); |
|
122 | + $this->default_data = $this->data; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Only store the object ID to avoid serializing the data object instance. |
|
127 | + * |
|
128 | + * @return array |
|
129 | + */ |
|
130 | + public function __sleep() { |
|
131 | + return array( 'id' ); |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * Re-run the constructor with the object ID. |
|
136 | + * |
|
137 | + * If the object no longer exists, remove the ID. |
|
138 | + */ |
|
139 | + public function __wakeup() { |
|
140 | + $this->__construct( absint( $this->id ) ); |
|
141 | + |
|
142 | + if ( ! empty( $this->last_error ) ) { |
|
143 | + $this->set_id( 0 ); |
|
144 | + } |
|
145 | + |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * When the object is cloned, make sure meta is duplicated correctly. |
|
150 | + * |
|
151 | + * @since 1.0.19 |
|
152 | + */ |
|
153 | + public function __clone() { |
|
154 | + $this->maybe_read_meta_data(); |
|
155 | + if ( ! empty( $this->meta_data ) ) { |
|
156 | + foreach ( $this->meta_data as $array_key => $meta ) { |
|
157 | + $this->meta_data[ $array_key ] = clone $meta; |
|
158 | + if ( ! empty( $meta->id ) ) { |
|
159 | + $this->meta_data[ $array_key ]->id = null; |
|
160 | + } |
|
161 | + } |
|
162 | + } |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Get the data store. |
|
167 | + * |
|
168 | + * @since 1.0.19 |
|
169 | + * @return object |
|
170 | + */ |
|
171 | + public function get_data_store() { |
|
172 | + return $this->data_store; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Get the object type. |
|
177 | + * |
|
178 | + * @since 1.0.19 |
|
179 | + * @return string |
|
180 | + */ |
|
181 | + public function get_object_type() { |
|
182 | + return $this->object_type; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Returns the unique ID for this object. |
|
187 | + * |
|
188 | + * @since 1.0.19 |
|
189 | + * @return int |
|
190 | + */ |
|
191 | + public function get_id() { |
|
192 | + return $this->id; |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Get form status. |
|
197 | + * |
|
198 | + * @since 1.0.19 |
|
199 | + * @param string $context View or edit context. |
|
200 | + * @return string |
|
201 | + */ |
|
202 | + public function get_status( $context = 'view' ) { |
|
203 | + return $this->get_prop( 'status', $context ); |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Delete an object, set the ID to 0, and return result. |
|
208 | + * |
|
209 | + * @since 1.0.19 |
|
210 | + * @param bool $force_delete Should the data be deleted permanently. |
|
211 | + * @return bool result |
|
212 | + */ |
|
213 | + public function delete( $force_delete = false ) { |
|
214 | + if ( $this->data_store ) { |
|
215 | + $this->data_store->delete( $this, array( 'force_delete' => $force_delete ) ); |
|
216 | + $this->set_id( 0 ); |
|
217 | + return true; |
|
218 | + } |
|
219 | + return false; |
|
220 | + } |
|
221 | + |
|
222 | + /** |
|
223 | + * Save should create or update based on object existence. |
|
224 | + * |
|
225 | + * @since 1.0.19 |
|
226 | + * @return int |
|
227 | + */ |
|
228 | + public function save() { |
|
229 | + if ( ! $this->data_store ) { |
|
230 | + return $this->get_id(); |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Trigger action before saving to the DB. Allows you to adjust object props before save. |
|
235 | + * |
|
236 | + * @param GetPaid_Data $this The object being saved. |
|
237 | + * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
238 | + */ |
|
239 | + do_action( 'getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
240 | + |
|
241 | + if ( $this->get_id() ) { |
|
242 | + $this->data_store->update( $this ); |
|
243 | + } else { |
|
244 | + $this->data_store->create( $this ); |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * Trigger action after saving to the DB. |
|
249 | + * |
|
250 | + * @param GetPaid_Data $this The object being saved. |
|
251 | + * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
252 | + */ |
|
253 | + do_action( 'getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
254 | + |
|
255 | + return $this->get_id(); |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * Change data to JSON format. |
|
260 | + * |
|
261 | + * @since 1.0.19 |
|
262 | + * @return string Data in JSON format. |
|
263 | + */ |
|
264 | + public function __toString() { |
|
265 | + return wp_json_encode( $this->get_data() ); |
|
266 | + } |
|
267 | + |
|
268 | + /** |
|
269 | + * Returns all data for this object. |
|
270 | + * |
|
271 | + * @since 1.0.19 |
|
272 | + * @return array |
|
273 | + */ |
|
274 | + public function get_data() { |
|
275 | + return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) ); |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * Returns array of expected data keys for this object. |
|
280 | + * |
|
281 | + * @since 1.0.19 |
|
282 | + * @return array |
|
283 | + */ |
|
284 | + public function get_data_keys() { |
|
285 | + return array_keys( $this->data ); |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * Returns all "extra" data keys for an object (for sub objects like item types). |
|
290 | + * |
|
291 | + * @since 1.0.19 |
|
292 | + * @return array |
|
293 | + */ |
|
294 | + public function get_extra_data_keys() { |
|
295 | + return array_keys( $this->extra_data ); |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * Filter null meta values from array. |
|
300 | + * |
|
301 | + * @since 1.0.19 |
|
302 | + * @param mixed $meta Meta value to check. |
|
303 | + * @return bool |
|
304 | + */ |
|
305 | + protected function filter_null_meta( $meta ) { |
|
306 | + return ! is_null( $meta->value ); |
|
307 | + } |
|
308 | + |
|
309 | + /** |
|
310 | + * Get All Meta Data. |
|
311 | + * |
|
312 | + * @since 1.0.19 |
|
313 | + * @return array of objects. |
|
314 | + */ |
|
315 | + public function get_meta_data() { |
|
316 | + $this->maybe_read_meta_data(); |
|
317 | + return array_values( array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ) ); |
|
318 | + } |
|
319 | + |
|
320 | + /** |
|
321 | + * Check if the key is an internal one. |
|
322 | + * |
|
323 | + * @since 1.0.19 |
|
324 | + * @param string $key Key to check. |
|
325 | + * @return bool true if it's an internal key, false otherwise |
|
326 | + */ |
|
327 | + protected function is_internal_meta_key( $key ) { |
|
328 | + $internal_meta_key = ! empty( $key ) && $this->data_store && in_array( $key, $this->data_store->get_internal_meta_keys(), true ); |
|
329 | + |
|
330 | + if ( ! $internal_meta_key ) { |
|
331 | + return false; |
|
332 | + } |
|
333 | + |
|
334 | + $has_setter_or_getter = is_callable( array( $this, 'set_' . $key ) ) || is_callable( array( $this, 'get_' . $key ) ); |
|
335 | + |
|
336 | + if ( ! $has_setter_or_getter ) { |
|
337 | + return false; |
|
338 | + } |
|
339 | + |
|
340 | + /* translators: %s: $key Key to check */ |
|
341 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'getpaid' ), $key ), '1.0.19' ); |
|
342 | + |
|
343 | + return true; |
|
344 | + } |
|
345 | + |
|
346 | + /** |
|
347 | + * Magic method for setting data fields. |
|
348 | + * |
|
349 | + * This method does not update custom fields in the database. |
|
350 | + * |
|
351 | + * @since 1.0.19 |
|
352 | + * @access public |
|
353 | + * |
|
354 | + */ |
|
355 | + public function __set( $key, $value ) { |
|
356 | + |
|
357 | + if ( 'id' == strtolower( $key ) ) { |
|
358 | + return $this->set_id( $value ); |
|
359 | + } |
|
360 | + |
|
361 | + if ( method_exists( $this, "set_$key") ) { |
|
362 | + |
|
363 | + /* translators: %s: $key Key to set */ |
|
364 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'getpaid' ), $key ), '1.0.19' ); |
|
365 | + |
|
366 | + call_user_func( array( $this, "set_$key" ), $value ); |
|
367 | + } else { |
|
368 | + $this->set_prop( $key, $value ); |
|
369 | + } |
|
370 | + |
|
371 | + } |
|
372 | + |
|
373 | + /** |
|
374 | 374 | * Margic method for retrieving a property. |
375 | 375 | */ |
376 | 376 | public function __get( $key ) { |
@@ -378,10 +378,10 @@ discard block |
||
378 | 378 | // Check if we have a helper method for that. |
379 | 379 | if ( method_exists( $this, 'get_' . $key ) ) { |
380 | 380 | |
381 | - if ( 'post_type' != $key ) { |
|
382 | - /* translators: %s: $key Key to set */ |
|
383 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'getpaid' ), $key ), '1.0.19' ); |
|
384 | - } |
|
381 | + if ( 'post_type' != $key ) { |
|
382 | + /* translators: %s: $key Key to set */ |
|
383 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'getpaid' ), $key ), '1.0.19' ); |
|
384 | + } |
|
385 | 385 | |
386 | 386 | return call_user_func( array( $this, 'get_' . $key ) ); |
387 | 387 | } |
@@ -391,509 +391,509 @@ discard block |
||
391 | 391 | return $this->post->$key; |
392 | 392 | } |
393 | 393 | |
394 | - return $this->get_prop( $key ); |
|
395 | - |
|
396 | - } |
|
397 | - |
|
398 | - /** |
|
399 | - * Get Meta Data by Key. |
|
400 | - * |
|
401 | - * @since 1.0.19 |
|
402 | - * @param string $key Meta Key. |
|
403 | - * @param bool $single return first found meta with key, or all with $key. |
|
404 | - * @param string $context What the value is for. Valid values are view and edit. |
|
405 | - * @return mixed |
|
406 | - */ |
|
407 | - public function get_meta( $key = '', $single = true, $context = 'view' ) { |
|
408 | - |
|
409 | - // Check if this is an internal meta key. |
|
410 | - $_key = str_replace( '_wpinv', '', $key ); |
|
411 | - $_key = str_replace( 'wpinv', '', $_key ); |
|
412 | - if ( $this->is_internal_meta_key( $_key ) ) { |
|
413 | - $function = 'get_' . $_key; |
|
414 | - |
|
415 | - if ( is_callable( array( $this, $function ) ) ) { |
|
416 | - return $this->{$function}(); |
|
417 | - } |
|
418 | - } |
|
419 | - |
|
420 | - // Read the meta data if not yet read. |
|
421 | - $this->maybe_read_meta_data(); |
|
422 | - $meta_data = $this->get_meta_data(); |
|
423 | - $array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true ); |
|
424 | - $value = $single ? '' : array(); |
|
425 | - |
|
426 | - if ( ! empty( $array_keys ) ) { |
|
427 | - // We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()). |
|
428 | - if ( $single ) { |
|
429 | - $value = $meta_data[ current( $array_keys ) ]->value; |
|
430 | - } else { |
|
431 | - $value = array_intersect_key( $meta_data, array_flip( $array_keys ) ); |
|
432 | - } |
|
433 | - } |
|
434 | - |
|
435 | - if ( 'view' === $context ) { |
|
436 | - $value = apply_filters( $this->get_hook_prefix() . $key, $value, $this ); |
|
437 | - } |
|
438 | - |
|
439 | - return $value; |
|
440 | - } |
|
441 | - |
|
442 | - /** |
|
443 | - * See if meta data exists, since get_meta always returns a '' or array(). |
|
444 | - * |
|
445 | - * @since 1.0.19 |
|
446 | - * @param string $key Meta Key. |
|
447 | - * @return boolean |
|
448 | - */ |
|
449 | - public function meta_exists( $key = '' ) { |
|
450 | - $this->maybe_read_meta_data(); |
|
451 | - $array_keys = wp_list_pluck( $this->get_meta_data(), 'key' ); |
|
452 | - return in_array( $key, $array_keys, true ); |
|
453 | - } |
|
454 | - |
|
455 | - /** |
|
456 | - * Set all meta data from array. |
|
457 | - * |
|
458 | - * @since 1.0.19 |
|
459 | - * @param array $data Key/Value pairs. |
|
460 | - */ |
|
461 | - public function set_meta_data( $data ) { |
|
462 | - if ( ! empty( $data ) && is_array( $data ) ) { |
|
463 | - $this->maybe_read_meta_data(); |
|
464 | - foreach ( $data as $meta ) { |
|
465 | - $meta = (array) $meta; |
|
466 | - if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) { |
|
467 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
468 | - array( |
|
469 | - 'id' => $meta['id'], |
|
470 | - 'key' => $meta['key'], |
|
471 | - 'value' => $meta['value'], |
|
472 | - ) |
|
473 | - ); |
|
474 | - } |
|
475 | - } |
|
476 | - } |
|
477 | - } |
|
478 | - |
|
479 | - /** |
|
480 | - * Add meta data. |
|
481 | - * |
|
482 | - * @since 1.0.19 |
|
483 | - * |
|
484 | - * @param string $key Meta key. |
|
485 | - * @param string|array $value Meta value. |
|
486 | - * @param bool $unique Should this be a unique key?. |
|
487 | - */ |
|
488 | - public function add_meta_data( $key, $value, $unique = false ) { |
|
489 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
490 | - $function = 'set_' . $key; |
|
491 | - |
|
492 | - if ( is_callable( array( $this, $function ) ) ) { |
|
493 | - return $this->{$function}( $value ); |
|
494 | - } |
|
495 | - } |
|
496 | - |
|
497 | - $this->maybe_read_meta_data(); |
|
498 | - if ( $unique ) { |
|
499 | - $this->delete_meta_data( $key ); |
|
500 | - } |
|
501 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
502 | - array( |
|
503 | - 'key' => $key, |
|
504 | - 'value' => $value, |
|
505 | - ) |
|
506 | - ); |
|
507 | - } |
|
508 | - |
|
509 | - /** |
|
510 | - * Update meta data by key or ID, if provided. |
|
511 | - * |
|
512 | - * @since 1.0.19 |
|
513 | - * |
|
514 | - * @param string $key Meta key. |
|
515 | - * @param string|array $value Meta value. |
|
516 | - * @param int $meta_id Meta ID. |
|
517 | - */ |
|
518 | - public function update_meta_data( $key, $value, $meta_id = 0 ) { |
|
519 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
520 | - $function = 'set_' . $key; |
|
521 | - |
|
522 | - if ( is_callable( array( $this, $function ) ) ) { |
|
523 | - return $this->{$function}( $value ); |
|
524 | - } |
|
525 | - } |
|
526 | - |
|
527 | - $this->maybe_read_meta_data(); |
|
528 | - |
|
529 | - $array_key = false; |
|
530 | - |
|
531 | - if ( $meta_id ) { |
|
532 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true ); |
|
533 | - $array_key = $array_keys ? current( $array_keys ) : false; |
|
534 | - } else { |
|
535 | - // Find matches by key. |
|
536 | - $matches = array(); |
|
537 | - foreach ( $this->meta_data as $meta_data_array_key => $meta ) { |
|
538 | - if ( $meta->key === $key ) { |
|
539 | - $matches[] = $meta_data_array_key; |
|
540 | - } |
|
541 | - } |
|
542 | - |
|
543 | - if ( ! empty( $matches ) ) { |
|
544 | - // Set matches to null so only one key gets the new value. |
|
545 | - foreach ( $matches as $meta_data_array_key ) { |
|
546 | - $this->meta_data[ $meta_data_array_key ]->value = null; |
|
547 | - } |
|
548 | - $array_key = current( $matches ); |
|
549 | - } |
|
550 | - } |
|
551 | - |
|
552 | - if ( false !== $array_key ) { |
|
553 | - $meta = $this->meta_data[ $array_key ]; |
|
554 | - $meta->key = $key; |
|
555 | - $meta->value = $value; |
|
556 | - } else { |
|
557 | - $this->add_meta_data( $key, $value, true ); |
|
558 | - } |
|
559 | - } |
|
560 | - |
|
561 | - /** |
|
562 | - * Delete meta data. |
|
563 | - * |
|
564 | - * @since 1.0.19 |
|
565 | - * @param string $key Meta key. |
|
566 | - */ |
|
567 | - public function delete_meta_data( $key ) { |
|
568 | - $this->maybe_read_meta_data(); |
|
569 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key, true ); |
|
570 | - |
|
571 | - if ( $array_keys ) { |
|
572 | - foreach ( $array_keys as $array_key ) { |
|
573 | - $this->meta_data[ $array_key ]->value = null; |
|
574 | - } |
|
575 | - } |
|
576 | - } |
|
577 | - |
|
578 | - /** |
|
579 | - * Delete meta data. |
|
580 | - * |
|
581 | - * @since 1.0.19 |
|
582 | - * @param int $mid Meta ID. |
|
583 | - */ |
|
584 | - public function delete_meta_data_by_mid( $mid ) { |
|
585 | - $this->maybe_read_meta_data(); |
|
586 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), (int) $mid, true ); |
|
587 | - |
|
588 | - if ( $array_keys ) { |
|
589 | - foreach ( $array_keys as $array_key ) { |
|
590 | - $this->meta_data[ $array_key ]->value = null; |
|
591 | - } |
|
592 | - } |
|
593 | - } |
|
594 | - |
|
595 | - /** |
|
596 | - * Read meta data if null. |
|
597 | - * |
|
598 | - * @since 1.0.19 |
|
599 | - */ |
|
600 | - protected function maybe_read_meta_data() { |
|
601 | - if ( is_null( $this->meta_data ) ) { |
|
602 | - $this->read_meta_data(); |
|
603 | - } |
|
604 | - } |
|
605 | - |
|
606 | - /** |
|
607 | - * Read Meta Data from the database. Ignore any internal properties. |
|
608 | - * Uses it's own caches because get_metadata does not provide meta_ids. |
|
609 | - * |
|
610 | - * @since 1.0.19 |
|
611 | - * @param bool $force_read True to force a new DB read (and update cache). |
|
612 | - */ |
|
613 | - public function read_meta_data( $force_read = false ) { |
|
614 | - $this->meta_data = array(); |
|
615 | - $cache_loaded = false; |
|
616 | - |
|
617 | - if ( ! $this->get_id() ) { |
|
618 | - return; |
|
619 | - } |
|
620 | - |
|
621 | - if ( ! $this->data_store ) { |
|
622 | - return; |
|
623 | - } |
|
624 | - |
|
625 | - if ( ! empty( $this->cache_group ) ) { |
|
626 | - $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
627 | - } |
|
628 | - |
|
629 | - if ( ! $force_read ) { |
|
630 | - if ( ! empty( $this->cache_group ) ) { |
|
631 | - $cached_meta = wp_cache_get( $cache_key, $this->cache_group ); |
|
632 | - $cache_loaded = ! empty( $cached_meta ); |
|
633 | - } |
|
634 | - } |
|
635 | - |
|
636 | - $raw_meta_data = $cache_loaded ? $cached_meta : $this->data_store->read_meta( $this ); |
|
637 | - if ( $raw_meta_data ) { |
|
638 | - foreach ( $raw_meta_data as $meta ) { |
|
639 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
640 | - array( |
|
641 | - 'id' => (int) $meta->meta_id, |
|
642 | - 'key' => $meta->meta_key, |
|
643 | - 'value' => maybe_unserialize( $meta->meta_value ), |
|
644 | - ) |
|
645 | - ); |
|
646 | - } |
|
647 | - |
|
648 | - if ( ! $cache_loaded && ! empty( $this->cache_group ) ) { |
|
649 | - wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group ); |
|
650 | - } |
|
651 | - } |
|
652 | - } |
|
653 | - |
|
654 | - /** |
|
655 | - * Update Meta Data in the database. |
|
656 | - * |
|
657 | - * @since 1.0.19 |
|
658 | - */ |
|
659 | - public function save_meta_data() { |
|
660 | - if ( ! $this->data_store || is_null( $this->meta_data ) ) { |
|
661 | - return; |
|
662 | - } |
|
663 | - foreach ( $this->meta_data as $array_key => $meta ) { |
|
664 | - if ( is_null( $meta->value ) ) { |
|
665 | - if ( ! empty( $meta->id ) ) { |
|
666 | - $this->data_store->delete_meta( $this, $meta ); |
|
667 | - unset( $this->meta_data[ $array_key ] ); |
|
668 | - } |
|
669 | - } elseif ( empty( $meta->id ) ) { |
|
670 | - $meta->id = $this->data_store->add_meta( $this, $meta ); |
|
671 | - $meta->apply_changes(); |
|
672 | - } else { |
|
673 | - if ( $meta->get_changes() ) { |
|
674 | - $this->data_store->update_meta( $this, $meta ); |
|
675 | - $meta->apply_changes(); |
|
676 | - } |
|
677 | - } |
|
678 | - } |
|
679 | - if ( ! empty( $this->cache_group ) ) { |
|
680 | - $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
681 | - wp_cache_delete( $cache_key, $this->cache_group ); |
|
682 | - } |
|
683 | - } |
|
684 | - |
|
685 | - /** |
|
686 | - * Set ID. |
|
687 | - * |
|
688 | - * @since 1.0.19 |
|
689 | - * @param int $id ID. |
|
690 | - */ |
|
691 | - public function set_id( $id ) { |
|
692 | - $this->id = absint( $id ); |
|
693 | - } |
|
694 | - |
|
695 | - /** |
|
696 | - * Sets item status. |
|
697 | - * |
|
698 | - * @since 1.0.19 |
|
699 | - * @param string $status New status. |
|
700 | - * @return array details of change. |
|
701 | - */ |
|
702 | - public function set_status( $status ) { |
|
394 | + return $this->get_prop( $key ); |
|
395 | + |
|
396 | + } |
|
397 | + |
|
398 | + /** |
|
399 | + * Get Meta Data by Key. |
|
400 | + * |
|
401 | + * @since 1.0.19 |
|
402 | + * @param string $key Meta Key. |
|
403 | + * @param bool $single return first found meta with key, or all with $key. |
|
404 | + * @param string $context What the value is for. Valid values are view and edit. |
|
405 | + * @return mixed |
|
406 | + */ |
|
407 | + public function get_meta( $key = '', $single = true, $context = 'view' ) { |
|
408 | + |
|
409 | + // Check if this is an internal meta key. |
|
410 | + $_key = str_replace( '_wpinv', '', $key ); |
|
411 | + $_key = str_replace( 'wpinv', '', $_key ); |
|
412 | + if ( $this->is_internal_meta_key( $_key ) ) { |
|
413 | + $function = 'get_' . $_key; |
|
414 | + |
|
415 | + if ( is_callable( array( $this, $function ) ) ) { |
|
416 | + return $this->{$function}(); |
|
417 | + } |
|
418 | + } |
|
419 | + |
|
420 | + // Read the meta data if not yet read. |
|
421 | + $this->maybe_read_meta_data(); |
|
422 | + $meta_data = $this->get_meta_data(); |
|
423 | + $array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true ); |
|
424 | + $value = $single ? '' : array(); |
|
425 | + |
|
426 | + if ( ! empty( $array_keys ) ) { |
|
427 | + // We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()). |
|
428 | + if ( $single ) { |
|
429 | + $value = $meta_data[ current( $array_keys ) ]->value; |
|
430 | + } else { |
|
431 | + $value = array_intersect_key( $meta_data, array_flip( $array_keys ) ); |
|
432 | + } |
|
433 | + } |
|
434 | + |
|
435 | + if ( 'view' === $context ) { |
|
436 | + $value = apply_filters( $this->get_hook_prefix() . $key, $value, $this ); |
|
437 | + } |
|
438 | + |
|
439 | + return $value; |
|
440 | + } |
|
441 | + |
|
442 | + /** |
|
443 | + * See if meta data exists, since get_meta always returns a '' or array(). |
|
444 | + * |
|
445 | + * @since 1.0.19 |
|
446 | + * @param string $key Meta Key. |
|
447 | + * @return boolean |
|
448 | + */ |
|
449 | + public function meta_exists( $key = '' ) { |
|
450 | + $this->maybe_read_meta_data(); |
|
451 | + $array_keys = wp_list_pluck( $this->get_meta_data(), 'key' ); |
|
452 | + return in_array( $key, $array_keys, true ); |
|
453 | + } |
|
454 | + |
|
455 | + /** |
|
456 | + * Set all meta data from array. |
|
457 | + * |
|
458 | + * @since 1.0.19 |
|
459 | + * @param array $data Key/Value pairs. |
|
460 | + */ |
|
461 | + public function set_meta_data( $data ) { |
|
462 | + if ( ! empty( $data ) && is_array( $data ) ) { |
|
463 | + $this->maybe_read_meta_data(); |
|
464 | + foreach ( $data as $meta ) { |
|
465 | + $meta = (array) $meta; |
|
466 | + if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) { |
|
467 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
468 | + array( |
|
469 | + 'id' => $meta['id'], |
|
470 | + 'key' => $meta['key'], |
|
471 | + 'value' => $meta['value'], |
|
472 | + ) |
|
473 | + ); |
|
474 | + } |
|
475 | + } |
|
476 | + } |
|
477 | + } |
|
478 | + |
|
479 | + /** |
|
480 | + * Add meta data. |
|
481 | + * |
|
482 | + * @since 1.0.19 |
|
483 | + * |
|
484 | + * @param string $key Meta key. |
|
485 | + * @param string|array $value Meta value. |
|
486 | + * @param bool $unique Should this be a unique key?. |
|
487 | + */ |
|
488 | + public function add_meta_data( $key, $value, $unique = false ) { |
|
489 | + if ( $this->is_internal_meta_key( $key ) ) { |
|
490 | + $function = 'set_' . $key; |
|
491 | + |
|
492 | + if ( is_callable( array( $this, $function ) ) ) { |
|
493 | + return $this->{$function}( $value ); |
|
494 | + } |
|
495 | + } |
|
496 | + |
|
497 | + $this->maybe_read_meta_data(); |
|
498 | + if ( $unique ) { |
|
499 | + $this->delete_meta_data( $key ); |
|
500 | + } |
|
501 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
502 | + array( |
|
503 | + 'key' => $key, |
|
504 | + 'value' => $value, |
|
505 | + ) |
|
506 | + ); |
|
507 | + } |
|
508 | + |
|
509 | + /** |
|
510 | + * Update meta data by key or ID, if provided. |
|
511 | + * |
|
512 | + * @since 1.0.19 |
|
513 | + * |
|
514 | + * @param string $key Meta key. |
|
515 | + * @param string|array $value Meta value. |
|
516 | + * @param int $meta_id Meta ID. |
|
517 | + */ |
|
518 | + public function update_meta_data( $key, $value, $meta_id = 0 ) { |
|
519 | + if ( $this->is_internal_meta_key( $key ) ) { |
|
520 | + $function = 'set_' . $key; |
|
521 | + |
|
522 | + if ( is_callable( array( $this, $function ) ) ) { |
|
523 | + return $this->{$function}( $value ); |
|
524 | + } |
|
525 | + } |
|
526 | + |
|
527 | + $this->maybe_read_meta_data(); |
|
528 | + |
|
529 | + $array_key = false; |
|
530 | + |
|
531 | + if ( $meta_id ) { |
|
532 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true ); |
|
533 | + $array_key = $array_keys ? current( $array_keys ) : false; |
|
534 | + } else { |
|
535 | + // Find matches by key. |
|
536 | + $matches = array(); |
|
537 | + foreach ( $this->meta_data as $meta_data_array_key => $meta ) { |
|
538 | + if ( $meta->key === $key ) { |
|
539 | + $matches[] = $meta_data_array_key; |
|
540 | + } |
|
541 | + } |
|
542 | + |
|
543 | + if ( ! empty( $matches ) ) { |
|
544 | + // Set matches to null so only one key gets the new value. |
|
545 | + foreach ( $matches as $meta_data_array_key ) { |
|
546 | + $this->meta_data[ $meta_data_array_key ]->value = null; |
|
547 | + } |
|
548 | + $array_key = current( $matches ); |
|
549 | + } |
|
550 | + } |
|
551 | + |
|
552 | + if ( false !== $array_key ) { |
|
553 | + $meta = $this->meta_data[ $array_key ]; |
|
554 | + $meta->key = $key; |
|
555 | + $meta->value = $value; |
|
556 | + } else { |
|
557 | + $this->add_meta_data( $key, $value, true ); |
|
558 | + } |
|
559 | + } |
|
560 | + |
|
561 | + /** |
|
562 | + * Delete meta data. |
|
563 | + * |
|
564 | + * @since 1.0.19 |
|
565 | + * @param string $key Meta key. |
|
566 | + */ |
|
567 | + public function delete_meta_data( $key ) { |
|
568 | + $this->maybe_read_meta_data(); |
|
569 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key, true ); |
|
570 | + |
|
571 | + if ( $array_keys ) { |
|
572 | + foreach ( $array_keys as $array_key ) { |
|
573 | + $this->meta_data[ $array_key ]->value = null; |
|
574 | + } |
|
575 | + } |
|
576 | + } |
|
577 | + |
|
578 | + /** |
|
579 | + * Delete meta data. |
|
580 | + * |
|
581 | + * @since 1.0.19 |
|
582 | + * @param int $mid Meta ID. |
|
583 | + */ |
|
584 | + public function delete_meta_data_by_mid( $mid ) { |
|
585 | + $this->maybe_read_meta_data(); |
|
586 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), (int) $mid, true ); |
|
587 | + |
|
588 | + if ( $array_keys ) { |
|
589 | + foreach ( $array_keys as $array_key ) { |
|
590 | + $this->meta_data[ $array_key ]->value = null; |
|
591 | + } |
|
592 | + } |
|
593 | + } |
|
594 | + |
|
595 | + /** |
|
596 | + * Read meta data if null. |
|
597 | + * |
|
598 | + * @since 1.0.19 |
|
599 | + */ |
|
600 | + protected function maybe_read_meta_data() { |
|
601 | + if ( is_null( $this->meta_data ) ) { |
|
602 | + $this->read_meta_data(); |
|
603 | + } |
|
604 | + } |
|
605 | + |
|
606 | + /** |
|
607 | + * Read Meta Data from the database. Ignore any internal properties. |
|
608 | + * Uses it's own caches because get_metadata does not provide meta_ids. |
|
609 | + * |
|
610 | + * @since 1.0.19 |
|
611 | + * @param bool $force_read True to force a new DB read (and update cache). |
|
612 | + */ |
|
613 | + public function read_meta_data( $force_read = false ) { |
|
614 | + $this->meta_data = array(); |
|
615 | + $cache_loaded = false; |
|
616 | + |
|
617 | + if ( ! $this->get_id() ) { |
|
618 | + return; |
|
619 | + } |
|
620 | + |
|
621 | + if ( ! $this->data_store ) { |
|
622 | + return; |
|
623 | + } |
|
624 | + |
|
625 | + if ( ! empty( $this->cache_group ) ) { |
|
626 | + $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
627 | + } |
|
628 | + |
|
629 | + if ( ! $force_read ) { |
|
630 | + if ( ! empty( $this->cache_group ) ) { |
|
631 | + $cached_meta = wp_cache_get( $cache_key, $this->cache_group ); |
|
632 | + $cache_loaded = ! empty( $cached_meta ); |
|
633 | + } |
|
634 | + } |
|
635 | + |
|
636 | + $raw_meta_data = $cache_loaded ? $cached_meta : $this->data_store->read_meta( $this ); |
|
637 | + if ( $raw_meta_data ) { |
|
638 | + foreach ( $raw_meta_data as $meta ) { |
|
639 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
640 | + array( |
|
641 | + 'id' => (int) $meta->meta_id, |
|
642 | + 'key' => $meta->meta_key, |
|
643 | + 'value' => maybe_unserialize( $meta->meta_value ), |
|
644 | + ) |
|
645 | + ); |
|
646 | + } |
|
647 | + |
|
648 | + if ( ! $cache_loaded && ! empty( $this->cache_group ) ) { |
|
649 | + wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group ); |
|
650 | + } |
|
651 | + } |
|
652 | + } |
|
653 | + |
|
654 | + /** |
|
655 | + * Update Meta Data in the database. |
|
656 | + * |
|
657 | + * @since 1.0.19 |
|
658 | + */ |
|
659 | + public function save_meta_data() { |
|
660 | + if ( ! $this->data_store || is_null( $this->meta_data ) ) { |
|
661 | + return; |
|
662 | + } |
|
663 | + foreach ( $this->meta_data as $array_key => $meta ) { |
|
664 | + if ( is_null( $meta->value ) ) { |
|
665 | + if ( ! empty( $meta->id ) ) { |
|
666 | + $this->data_store->delete_meta( $this, $meta ); |
|
667 | + unset( $this->meta_data[ $array_key ] ); |
|
668 | + } |
|
669 | + } elseif ( empty( $meta->id ) ) { |
|
670 | + $meta->id = $this->data_store->add_meta( $this, $meta ); |
|
671 | + $meta->apply_changes(); |
|
672 | + } else { |
|
673 | + if ( $meta->get_changes() ) { |
|
674 | + $this->data_store->update_meta( $this, $meta ); |
|
675 | + $meta->apply_changes(); |
|
676 | + } |
|
677 | + } |
|
678 | + } |
|
679 | + if ( ! empty( $this->cache_group ) ) { |
|
680 | + $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
681 | + wp_cache_delete( $cache_key, $this->cache_group ); |
|
682 | + } |
|
683 | + } |
|
684 | + |
|
685 | + /** |
|
686 | + * Set ID. |
|
687 | + * |
|
688 | + * @since 1.0.19 |
|
689 | + * @param int $id ID. |
|
690 | + */ |
|
691 | + public function set_id( $id ) { |
|
692 | + $this->id = absint( $id ); |
|
693 | + } |
|
694 | + |
|
695 | + /** |
|
696 | + * Sets item status. |
|
697 | + * |
|
698 | + * @since 1.0.19 |
|
699 | + * @param string $status New status. |
|
700 | + * @return array details of change. |
|
701 | + */ |
|
702 | + public function set_status( $status ) { |
|
703 | 703 | $old_status = $this->get_status(); |
704 | 704 | |
705 | - $this->set_prop( 'status', $status ); |
|
706 | - |
|
707 | - return array( |
|
708 | - 'from' => $old_status, |
|
709 | - 'to' => $status, |
|
710 | - ); |
|
711 | - } |
|
712 | - |
|
713 | - /** |
|
714 | - * Set all props to default values. |
|
715 | - * |
|
716 | - * @since 1.0.19 |
|
717 | - */ |
|
718 | - public function set_defaults() { |
|
719 | - $this->data = $this->default_data; |
|
720 | - $this->changes = array(); |
|
721 | - $this->set_object_read( false ); |
|
722 | - } |
|
723 | - |
|
724 | - /** |
|
725 | - * Set object read property. |
|
726 | - * |
|
727 | - * @since 1.0.19 |
|
728 | - * @param boolean $read Should read?. |
|
729 | - */ |
|
730 | - public function set_object_read( $read = true ) { |
|
731 | - $this->object_read = (bool) $read; |
|
732 | - } |
|
733 | - |
|
734 | - /** |
|
735 | - * Get object read property. |
|
736 | - * |
|
737 | - * @since 1.0.19 |
|
738 | - * @return boolean |
|
739 | - */ |
|
740 | - public function get_object_read() { |
|
741 | - return (bool) $this->object_read; |
|
742 | - } |
|
743 | - |
|
744 | - /** |
|
745 | - * Set a collection of props in one go, collect any errors, and return the result. |
|
746 | - * Only sets using public methods. |
|
747 | - * |
|
748 | - * @since 1.0.19 |
|
749 | - * |
|
750 | - * @param array $props Key value pairs to set. Key is the prop and should map to a setter function name. |
|
751 | - * @param string $context In what context to run this. |
|
752 | - * |
|
753 | - * @return bool|WP_Error |
|
754 | - */ |
|
755 | - public function set_props( $props, $context = 'set' ) { |
|
756 | - $errors = false; |
|
757 | - |
|
758 | - foreach ( $props as $prop => $value ) { |
|
759 | - try { |
|
760 | - /** |
|
761 | - * Checks if the prop being set is allowed, and the value is not null. |
|
762 | - */ |
|
763 | - if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) { |
|
764 | - continue; |
|
765 | - } |
|
766 | - $setter = "set_$prop"; |
|
767 | - |
|
768 | - if ( is_callable( array( $this, $setter ) ) ) { |
|
769 | - $this->{$setter}( $value ); |
|
770 | - } |
|
771 | - } catch ( Exception $e ) { |
|
772 | - if ( ! $errors ) { |
|
773 | - $errors = new WP_Error(); |
|
774 | - } |
|
775 | - $errors->add( $e->getCode(), $e->getMessage() ); |
|
776 | - $this->last_error = $e->getMessage(); |
|
777 | - } |
|
778 | - } |
|
779 | - |
|
780 | - return $errors && count( $errors->get_error_codes() ) ? $errors : true; |
|
781 | - } |
|
782 | - |
|
783 | - /** |
|
784 | - * Sets a prop for a setter method. |
|
785 | - * |
|
786 | - * This stores changes in a special array so we can track what needs saving |
|
787 | - * the the DB later. |
|
788 | - * |
|
789 | - * @since 1.0.19 |
|
790 | - * @param string $prop Name of prop to set. |
|
791 | - * @param mixed $value Value of the prop. |
|
792 | - */ |
|
793 | - protected function set_prop( $prop, $value ) { |
|
794 | - if ( array_key_exists( $prop, $this->data ) ) { |
|
795 | - if ( true === $this->object_read ) { |
|
796 | - if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) { |
|
797 | - $this->changes[ $prop ] = $value; |
|
798 | - } |
|
799 | - } else { |
|
800 | - $this->data[ $prop ] = $value; |
|
801 | - } |
|
802 | - } |
|
803 | - } |
|
804 | - |
|
805 | - /** |
|
806 | - * Return data changes only. |
|
807 | - * |
|
808 | - * @since 1.0.19 |
|
809 | - * @return array |
|
810 | - */ |
|
811 | - public function get_changes() { |
|
812 | - return $this->changes; |
|
813 | - } |
|
814 | - |
|
815 | - /** |
|
816 | - * Merge changes with data and clear. |
|
817 | - * |
|
818 | - * @since 1.0.19 |
|
819 | - */ |
|
820 | - public function apply_changes() { |
|
821 | - $this->data = array_replace_recursive( $this->data, $this->changes ); |
|
822 | - $this->changes = array(); |
|
823 | - } |
|
824 | - |
|
825 | - /** |
|
826 | - * Prefix for action and filter hooks on data. |
|
827 | - * |
|
828 | - * @since 1.0.19 |
|
829 | - * @return string |
|
830 | - */ |
|
831 | - protected function get_hook_prefix() { |
|
832 | - return 'wpinv_get_' . $this->object_type . '_'; |
|
833 | - } |
|
834 | - |
|
835 | - /** |
|
836 | - * Gets a prop for a getter method. |
|
837 | - * |
|
838 | - * Gets the value from either current pending changes, or the data itself. |
|
839 | - * Context controls what happens to the value before it's returned. |
|
840 | - * |
|
841 | - * @since 1.0.19 |
|
842 | - * @param string $prop Name of prop to get. |
|
843 | - * @param string $context What the value is for. Valid values are view and edit. |
|
844 | - * @return mixed |
|
845 | - */ |
|
846 | - protected function get_prop( $prop, $context = 'view' ) { |
|
847 | - $value = null; |
|
848 | - |
|
849 | - if ( array_key_exists( $prop, $this->data ) ) { |
|
850 | - $value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ]; |
|
851 | - |
|
852 | - if ( 'view' === $context ) { |
|
853 | - $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this ); |
|
854 | - } |
|
855 | - } |
|
856 | - |
|
857 | - return $value; |
|
858 | - } |
|
859 | - |
|
860 | - /** |
|
861 | - * Sets a date prop whilst handling formatting and datetime objects. |
|
862 | - * |
|
863 | - * @since 1.0.19 |
|
864 | - * @param string $prop Name of prop to set. |
|
865 | - * @param string|integer $value Value of the prop. |
|
866 | - */ |
|
867 | - protected function set_date_prop( $prop, $value ) { |
|
868 | - |
|
869 | - if ( empty( $value ) ) { |
|
870 | - $this->set_prop( $prop, null ); |
|
871 | - return; |
|
872 | - } |
|
873 | - $this->set_prop( $prop, $value ); |
|
874 | - |
|
875 | - } |
|
876 | - |
|
877 | - /** |
|
878 | - * When invalid data is found, throw an exception unless reading from the DB. |
|
879 | - * |
|
880 | - * @since 1.0.19 |
|
881 | - * @param string $code Error code. |
|
882 | - * @param string $message Error message. |
|
883 | - */ |
|
884 | - protected function error( $code, $message ) { |
|
885 | - $this->last_error = $message; |
|
886 | - } |
|
887 | - |
|
888 | - /** |
|
889 | - * Checks if the object is saved in the database |
|
890 | - * |
|
891 | - * @since 1.0.19 |
|
892 | - * @return bool |
|
893 | - */ |
|
894 | - public function exists() { |
|
895 | - $id = $this->get_id(); |
|
896 | - return ! empty( $id ); |
|
897 | - } |
|
705 | + $this->set_prop( 'status', $status ); |
|
706 | + |
|
707 | + return array( |
|
708 | + 'from' => $old_status, |
|
709 | + 'to' => $status, |
|
710 | + ); |
|
711 | + } |
|
712 | + |
|
713 | + /** |
|
714 | + * Set all props to default values. |
|
715 | + * |
|
716 | + * @since 1.0.19 |
|
717 | + */ |
|
718 | + public function set_defaults() { |
|
719 | + $this->data = $this->default_data; |
|
720 | + $this->changes = array(); |
|
721 | + $this->set_object_read( false ); |
|
722 | + } |
|
723 | + |
|
724 | + /** |
|
725 | + * Set object read property. |
|
726 | + * |
|
727 | + * @since 1.0.19 |
|
728 | + * @param boolean $read Should read?. |
|
729 | + */ |
|
730 | + public function set_object_read( $read = true ) { |
|
731 | + $this->object_read = (bool) $read; |
|
732 | + } |
|
733 | + |
|
734 | + /** |
|
735 | + * Get object read property. |
|
736 | + * |
|
737 | + * @since 1.0.19 |
|
738 | + * @return boolean |
|
739 | + */ |
|
740 | + public function get_object_read() { |
|
741 | + return (bool) $this->object_read; |
|
742 | + } |
|
743 | + |
|
744 | + /** |
|
745 | + * Set a collection of props in one go, collect any errors, and return the result. |
|
746 | + * Only sets using public methods. |
|
747 | + * |
|
748 | + * @since 1.0.19 |
|
749 | + * |
|
750 | + * @param array $props Key value pairs to set. Key is the prop and should map to a setter function name. |
|
751 | + * @param string $context In what context to run this. |
|
752 | + * |
|
753 | + * @return bool|WP_Error |
|
754 | + */ |
|
755 | + public function set_props( $props, $context = 'set' ) { |
|
756 | + $errors = false; |
|
757 | + |
|
758 | + foreach ( $props as $prop => $value ) { |
|
759 | + try { |
|
760 | + /** |
|
761 | + * Checks if the prop being set is allowed, and the value is not null. |
|
762 | + */ |
|
763 | + if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) { |
|
764 | + continue; |
|
765 | + } |
|
766 | + $setter = "set_$prop"; |
|
767 | + |
|
768 | + if ( is_callable( array( $this, $setter ) ) ) { |
|
769 | + $this->{$setter}( $value ); |
|
770 | + } |
|
771 | + } catch ( Exception $e ) { |
|
772 | + if ( ! $errors ) { |
|
773 | + $errors = new WP_Error(); |
|
774 | + } |
|
775 | + $errors->add( $e->getCode(), $e->getMessage() ); |
|
776 | + $this->last_error = $e->getMessage(); |
|
777 | + } |
|
778 | + } |
|
779 | + |
|
780 | + return $errors && count( $errors->get_error_codes() ) ? $errors : true; |
|
781 | + } |
|
782 | + |
|
783 | + /** |
|
784 | + * Sets a prop for a setter method. |
|
785 | + * |
|
786 | + * This stores changes in a special array so we can track what needs saving |
|
787 | + * the the DB later. |
|
788 | + * |
|
789 | + * @since 1.0.19 |
|
790 | + * @param string $prop Name of prop to set. |
|
791 | + * @param mixed $value Value of the prop. |
|
792 | + */ |
|
793 | + protected function set_prop( $prop, $value ) { |
|
794 | + if ( array_key_exists( $prop, $this->data ) ) { |
|
795 | + if ( true === $this->object_read ) { |
|
796 | + if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) { |
|
797 | + $this->changes[ $prop ] = $value; |
|
798 | + } |
|
799 | + } else { |
|
800 | + $this->data[ $prop ] = $value; |
|
801 | + } |
|
802 | + } |
|
803 | + } |
|
804 | + |
|
805 | + /** |
|
806 | + * Return data changes only. |
|
807 | + * |
|
808 | + * @since 1.0.19 |
|
809 | + * @return array |
|
810 | + */ |
|
811 | + public function get_changes() { |
|
812 | + return $this->changes; |
|
813 | + } |
|
814 | + |
|
815 | + /** |
|
816 | + * Merge changes with data and clear. |
|
817 | + * |
|
818 | + * @since 1.0.19 |
|
819 | + */ |
|
820 | + public function apply_changes() { |
|
821 | + $this->data = array_replace_recursive( $this->data, $this->changes ); |
|
822 | + $this->changes = array(); |
|
823 | + } |
|
824 | + |
|
825 | + /** |
|
826 | + * Prefix for action and filter hooks on data. |
|
827 | + * |
|
828 | + * @since 1.0.19 |
|
829 | + * @return string |
|
830 | + */ |
|
831 | + protected function get_hook_prefix() { |
|
832 | + return 'wpinv_get_' . $this->object_type . '_'; |
|
833 | + } |
|
834 | + |
|
835 | + /** |
|
836 | + * Gets a prop for a getter method. |
|
837 | + * |
|
838 | + * Gets the value from either current pending changes, or the data itself. |
|
839 | + * Context controls what happens to the value before it's returned. |
|
840 | + * |
|
841 | + * @since 1.0.19 |
|
842 | + * @param string $prop Name of prop to get. |
|
843 | + * @param string $context What the value is for. Valid values are view and edit. |
|
844 | + * @return mixed |
|
845 | + */ |
|
846 | + protected function get_prop( $prop, $context = 'view' ) { |
|
847 | + $value = null; |
|
848 | + |
|
849 | + if ( array_key_exists( $prop, $this->data ) ) { |
|
850 | + $value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ]; |
|
851 | + |
|
852 | + if ( 'view' === $context ) { |
|
853 | + $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this ); |
|
854 | + } |
|
855 | + } |
|
856 | + |
|
857 | + return $value; |
|
858 | + } |
|
859 | + |
|
860 | + /** |
|
861 | + * Sets a date prop whilst handling formatting and datetime objects. |
|
862 | + * |
|
863 | + * @since 1.0.19 |
|
864 | + * @param string $prop Name of prop to set. |
|
865 | + * @param string|integer $value Value of the prop. |
|
866 | + */ |
|
867 | + protected function set_date_prop( $prop, $value ) { |
|
868 | + |
|
869 | + if ( empty( $value ) ) { |
|
870 | + $this->set_prop( $prop, null ); |
|
871 | + return; |
|
872 | + } |
|
873 | + $this->set_prop( $prop, $value ); |
|
874 | + |
|
875 | + } |
|
876 | + |
|
877 | + /** |
|
878 | + * When invalid data is found, throw an exception unless reading from the DB. |
|
879 | + * |
|
880 | + * @since 1.0.19 |
|
881 | + * @param string $code Error code. |
|
882 | + * @param string $message Error message. |
|
883 | + */ |
|
884 | + protected function error( $code, $message ) { |
|
885 | + $this->last_error = $message; |
|
886 | + } |
|
887 | + |
|
888 | + /** |
|
889 | + * Checks if the object is saved in the database |
|
890 | + * |
|
891 | + * @since 1.0.19 |
|
892 | + * @return bool |
|
893 | + */ |
|
894 | + public function exists() { |
|
895 | + $id = $this->get_id(); |
|
896 | + return ! empty( $id ); |
|
897 | + } |
|
898 | 898 | |
899 | 899 | } |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | * |
6 | 6 | */ |
7 | 7 | if ( ! defined( 'ABSPATH' ) ) { |
8 | - exit; |
|
8 | + exit; |
|
9 | 9 | } |
10 | 10 | |
11 | 11 | /** |
@@ -15,510 +15,510 @@ discard block |
||
15 | 15 | */ |
16 | 16 | class GetPaid_Invoice_Data_Store extends GetPaid_Data_Store_WP { |
17 | 17 | |
18 | - /** |
|
19 | - * Data stored in meta keys, but not considered "meta" for a discount. |
|
20 | - * |
|
21 | - * @since 1.0.19 |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - protected $internal_meta_keys = array( |
|
25 | - '_wpinv_subscr_profile_id', |
|
26 | - '_wpinv_taxes', |
|
27 | - '_wpinv_fees', |
|
28 | - '_wpinv_discounts', |
|
29 | - '_wpinv_submission_id', |
|
30 | - '_wpinv_payment_form', |
|
31 | - '_wpinv_is_viewed', |
|
32 | - 'wpinv_email_cc', |
|
33 | - 'wpinv_template' |
|
34 | - ); |
|
35 | - |
|
36 | - /** |
|
37 | - * A map of meta keys to data props. |
|
38 | - * |
|
39 | - * @since 1.0.19 |
|
40 | - * |
|
41 | - * @var array |
|
42 | - */ |
|
43 | - protected $meta_key_to_props = array( |
|
44 | - '_wpinv_subscr_profile_id' => 'subscription_id', |
|
45 | - '_wpinv_taxes' => 'taxes', |
|
46 | - '_wpinv_fees' => 'fees', |
|
47 | - '_wpinv_discounts' => 'discounts', |
|
48 | - '_wpinv_submission_id' => 'submission_id', |
|
49 | - '_wpinv_payment_form' => 'payment_form', |
|
50 | - '_wpinv_is_viewed' => 'is_viewed', |
|
51 | - 'wpinv_email_cc' => 'email_cc', |
|
52 | - 'wpinv_template' => 'template', |
|
53 | - ); |
|
54 | - |
|
55 | - /** |
|
56 | - * A map of database fields to data props. |
|
57 | - * |
|
58 | - * @since 1.0.19 |
|
59 | - * |
|
60 | - * @var array |
|
61 | - */ |
|
62 | - protected $database_fields_to_props = array( |
|
63 | - 'post_id' => 'id', |
|
64 | - 'number' => 'number', |
|
65 | - 'currency' => 'currency', |
|
66 | - 'key' => 'key', |
|
67 | - 'type' => 'type', |
|
68 | - 'mode' => 'mode', |
|
69 | - 'user_ip' => 'user_ip', |
|
70 | - 'first_name' => 'first_name', |
|
71 | - 'last_name' => 'last_name', |
|
72 | - 'address' => 'address', |
|
73 | - 'city' => 'city', |
|
74 | - 'state' => 'state', |
|
75 | - 'country' => 'country', |
|
76 | - 'zip' => 'zip', |
|
77 | - 'zip' => 'zip', |
|
78 | - 'adddress_confirmed' => 'address_confirmed', |
|
79 | - 'gateway' => 'gateway', |
|
80 | - 'transaction_id' => 'transaction_id', |
|
81 | - 'currency' => 'currency', |
|
82 | - 'subtotal' => 'subtotal', |
|
83 | - 'tax' => 'total_tax', |
|
84 | - 'fees_total' => 'total_fees', |
|
85 | - 'discount' => 'total_discount', |
|
86 | - 'total' => 'total', |
|
87 | - 'discount_code' => 'discount_code', |
|
88 | - 'disable_taxes' => 'disable_taxes', |
|
89 | - 'due_date' => 'due_date', |
|
90 | - 'completed_date' => 'completed_date', |
|
91 | - 'company' => 'company', |
|
92 | - 'vat_number' => 'vat_number', |
|
93 | - 'vat_rate' => 'vat_rate', |
|
94 | - ); |
|
95 | - |
|
96 | - /* |
|
18 | + /** |
|
19 | + * Data stored in meta keys, but not considered "meta" for a discount. |
|
20 | + * |
|
21 | + * @since 1.0.19 |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + protected $internal_meta_keys = array( |
|
25 | + '_wpinv_subscr_profile_id', |
|
26 | + '_wpinv_taxes', |
|
27 | + '_wpinv_fees', |
|
28 | + '_wpinv_discounts', |
|
29 | + '_wpinv_submission_id', |
|
30 | + '_wpinv_payment_form', |
|
31 | + '_wpinv_is_viewed', |
|
32 | + 'wpinv_email_cc', |
|
33 | + 'wpinv_template' |
|
34 | + ); |
|
35 | + |
|
36 | + /** |
|
37 | + * A map of meta keys to data props. |
|
38 | + * |
|
39 | + * @since 1.0.19 |
|
40 | + * |
|
41 | + * @var array |
|
42 | + */ |
|
43 | + protected $meta_key_to_props = array( |
|
44 | + '_wpinv_subscr_profile_id' => 'subscription_id', |
|
45 | + '_wpinv_taxes' => 'taxes', |
|
46 | + '_wpinv_fees' => 'fees', |
|
47 | + '_wpinv_discounts' => 'discounts', |
|
48 | + '_wpinv_submission_id' => 'submission_id', |
|
49 | + '_wpinv_payment_form' => 'payment_form', |
|
50 | + '_wpinv_is_viewed' => 'is_viewed', |
|
51 | + 'wpinv_email_cc' => 'email_cc', |
|
52 | + 'wpinv_template' => 'template', |
|
53 | + ); |
|
54 | + |
|
55 | + /** |
|
56 | + * A map of database fields to data props. |
|
57 | + * |
|
58 | + * @since 1.0.19 |
|
59 | + * |
|
60 | + * @var array |
|
61 | + */ |
|
62 | + protected $database_fields_to_props = array( |
|
63 | + 'post_id' => 'id', |
|
64 | + 'number' => 'number', |
|
65 | + 'currency' => 'currency', |
|
66 | + 'key' => 'key', |
|
67 | + 'type' => 'type', |
|
68 | + 'mode' => 'mode', |
|
69 | + 'user_ip' => 'user_ip', |
|
70 | + 'first_name' => 'first_name', |
|
71 | + 'last_name' => 'last_name', |
|
72 | + 'address' => 'address', |
|
73 | + 'city' => 'city', |
|
74 | + 'state' => 'state', |
|
75 | + 'country' => 'country', |
|
76 | + 'zip' => 'zip', |
|
77 | + 'zip' => 'zip', |
|
78 | + 'adddress_confirmed' => 'address_confirmed', |
|
79 | + 'gateway' => 'gateway', |
|
80 | + 'transaction_id' => 'transaction_id', |
|
81 | + 'currency' => 'currency', |
|
82 | + 'subtotal' => 'subtotal', |
|
83 | + 'tax' => 'total_tax', |
|
84 | + 'fees_total' => 'total_fees', |
|
85 | + 'discount' => 'total_discount', |
|
86 | + 'total' => 'total', |
|
87 | + 'discount_code' => 'discount_code', |
|
88 | + 'disable_taxes' => 'disable_taxes', |
|
89 | + 'due_date' => 'due_date', |
|
90 | + 'completed_date' => 'completed_date', |
|
91 | + 'company' => 'company', |
|
92 | + 'vat_number' => 'vat_number', |
|
93 | + 'vat_rate' => 'vat_rate', |
|
94 | + ); |
|
95 | + |
|
96 | + /* |
|
97 | 97 | |-------------------------------------------------------------------------- |
98 | 98 | | CRUD Methods |
99 | 99 | |-------------------------------------------------------------------------- |
100 | 100 | */ |
101 | - /** |
|
102 | - * Method to create a new invoice in the database. |
|
103 | - * |
|
104 | - * @param WPInv_Invoice $invoice Invoice object. |
|
105 | - */ |
|
106 | - public function create( &$invoice ) { |
|
107 | - $invoice->set_version( WPINV_VERSION ); |
|
108 | - $invoice->set_date_created( current_time('mysql') ); |
|
109 | - |
|
110 | - // Create a new post. |
|
111 | - $id = wp_insert_post( |
|
112 | - apply_filters( |
|
113 | - 'getpaid_new_invoice_data', |
|
114 | - array( |
|
115 | - 'post_date' => $invoice->get_date_created( 'edit' ), |
|
116 | - 'post_type' => $invoice->get_post_type( 'edit' ), |
|
117 | - 'post_status' => $this->get_post_status( $invoice ), |
|
118 | - 'ping_status' => 'closed', |
|
119 | - 'post_author' => $invoice->get_user_id( 'edit' ), |
|
120 | - 'post_title' => $invoice->get_title( 'edit' ), |
|
121 | - 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
122 | - 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
123 | - 'post_name' => $invoice->get_path( 'edit' ), |
|
124 | - ) |
|
125 | - ), |
|
126 | - true |
|
127 | - ); |
|
128 | - |
|
129 | - if ( $id && ! is_wp_error( $id ) ) { |
|
130 | - |
|
131 | - // Update the new id and regenerate a title. |
|
132 | - $invoice->set_id( $id ); |
|
133 | - wp_update_post( array( 'ID' => $invoice->get_id(), 'post_title' => $invoice->get_number( 'edit' ) ) ); |
|
134 | - |
|
135 | - // Ensure both the key and number are set. |
|
136 | - $invoice->get_key(); |
|
137 | - $invoice->get_number(); |
|
138 | - |
|
139 | - // Save special fields and items. |
|
140 | - $this->save_special_fields( $invoice ); |
|
141 | - $this->save_items( $invoice ); |
|
142 | - |
|
143 | - // Update meta data. |
|
144 | - $this->update_post_meta( $invoice ); |
|
145 | - $invoice->save_meta_data(); |
|
146 | - |
|
147 | - // Apply changes. |
|
148 | - $invoice->apply_changes(); |
|
149 | - $this->clear_caches( $invoice ); |
|
150 | - |
|
151 | - // Fires after a new invoice is created. |
|
152 | - do_action( 'getpaid_new_' . $invoice->get_type(), $invoice->get_id(), $invoice ); |
|
153 | - return true; |
|
154 | - } |
|
155 | - |
|
156 | - if ( is_wp_error( $id ) ) { |
|
157 | - $invoice->last_error = $id->get_error_message(); |
|
158 | - } |
|
159 | - |
|
160 | - return false; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Method to read an invoice from the database. |
|
165 | - * |
|
166 | - * @param WPInv_Invoice $invoice Invoice object. |
|
167 | - * |
|
168 | - */ |
|
169 | - public function read( &$invoice ) { |
|
170 | - |
|
171 | - $invoice->set_defaults(); |
|
172 | - $invoice_object = get_post( $invoice->get_id() ); |
|
173 | - |
|
174 | - if ( ! $invoice->get_id() || ! $invoice_object || ! getpaid_is_invoice_post_type( $invoice_object->post_type ) ) { |
|
175 | - $invoice->last_error = __( 'Invalid invoice.', 'invoicing' ); |
|
176 | - $invoice->set_id( 0 ); |
|
177 | - return false; |
|
178 | - } |
|
179 | - |
|
180 | - $invoice->set_props( |
|
181 | - array( |
|
182 | - 'date_created' => 0 < $invoice_object->post_date ? $invoice_object->post_date : null, |
|
183 | - 'date_modified' => 0 < $invoice_object->post_modified ? $invoice_object->post_modified : null, |
|
184 | - 'status' => $invoice_object->post_status, |
|
185 | - 'author' => $invoice_object->post_author, |
|
186 | - 'description' => $invoice_object->post_excerpt, |
|
187 | - 'parent_id' => $invoice_object->post_parent, |
|
188 | - 'name' => $invoice_object->post_title, |
|
189 | - 'path' => $invoice_object->post_name, |
|
190 | - 'post_type' => $invoice_object->post_type, |
|
191 | - ) |
|
192 | - ); |
|
193 | - |
|
194 | - $invoice->set_type( $invoice_object->post_type ); |
|
195 | - |
|
196 | - $this->read_object_data( $invoice, $invoice_object ); |
|
197 | - $this->add_special_fields( $invoice ); |
|
198 | - $this->add_items( $invoice ); |
|
199 | - $invoice->read_meta_data(); |
|
200 | - $invoice->set_object_read( true ); |
|
201 | - do_action( 'getpaid_read_' . $invoice->get_type(), $invoice->get_id(), $invoice ); |
|
202 | - |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * Method to update an invoice in the database. |
|
207 | - * |
|
208 | - * @param WPInv_Invoice $invoice Invoice object. |
|
209 | - */ |
|
210 | - public function update( &$invoice ) { |
|
211 | - $invoice->save_meta_data(); |
|
212 | - $invoice->set_version( WPINV_VERSION ); |
|
213 | - |
|
214 | - if ( null === $invoice->get_date_created( 'edit' ) ) { |
|
215 | - $invoice->set_date_created( current_time('mysql') ); |
|
216 | - } |
|
217 | - |
|
218 | - // Ensure both the key and number are set. |
|
219 | - $invoice->get_key(); |
|
220 | - $invoice->get_number(); |
|
221 | - |
|
222 | - // Grab the current status so we can compare. |
|
223 | - $previous_status = get_post_status( $invoice->get_id() ); |
|
224 | - |
|
225 | - $changes = $invoice->get_changes(); |
|
226 | - |
|
227 | - // Only update the post when the post data changes. |
|
228 | - if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'description', 'parent_id', 'post_excerpt', 'path' ), array_keys( $changes ) ) ) { |
|
229 | - $post_data = array( |
|
230 | - 'post_date' => $invoice->get_date_created( 'edit' ), |
|
231 | - 'post_date_gmt' => $invoice->get_date_created_gmt( 'edit' ), |
|
232 | - 'post_status' => $invoice->get_status( 'edit' ), |
|
233 | - 'post_title' => $invoice->get_name( 'edit' ), |
|
234 | - 'post_author' => $invoice->get_user_id( 'edit' ), |
|
235 | - 'post_modified' => $invoice->get_date_modified( 'edit' ), |
|
236 | - 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
237 | - 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
238 | - 'post_name' => $invoice->get_path( 'edit' ), |
|
239 | - 'post_type' => $invoice->get_post_type( 'edit' ), |
|
240 | - ); |
|
241 | - |
|
242 | - /** |
|
243 | - * When updating this object, to prevent infinite loops, use $wpdb |
|
244 | - * to update data, since wp_update_post spawns more calls to the |
|
245 | - * save_post action. |
|
246 | - * |
|
247 | - * This ensures hooks are fired by either WP itself (admin screen save), |
|
248 | - * or an update purely from CRUD. |
|
249 | - */ |
|
250 | - if ( doing_action( 'save_post' ) ) { |
|
251 | - $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $invoice->get_id() ) ); |
|
252 | - clean_post_cache( $invoice->get_id() ); |
|
253 | - } else { |
|
254 | - wp_update_post( array_merge( array( 'ID' => $invoice->get_id() ), $post_data ) ); |
|
255 | - } |
|
256 | - $invoice->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
257 | - } |
|
258 | - |
|
259 | - // Update meta data. |
|
260 | - $this->update_post_meta( $invoice ); |
|
261 | - |
|
262 | - // Save special fields and items. |
|
263 | - $this->save_special_fields( $invoice ); |
|
264 | - $this->save_items( $invoice ); |
|
265 | - |
|
266 | - // Apply the changes. |
|
267 | - $invoice->apply_changes(); |
|
268 | - |
|
269 | - // Clear caches. |
|
270 | - $this->clear_caches( $invoice ); |
|
271 | - |
|
272 | - // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
273 | - $new_status = $invoice->get_status( 'edit' ); |
|
274 | - |
|
275 | - if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
276 | - do_action( 'getpaid_new_' . $invoice->get_type(), $invoice->get_id(), $invoice ); |
|
277 | - } else { |
|
278 | - do_action( 'getpaid_update_' . $invoice->get_type(), $invoice->get_id(), $invoice ); |
|
279 | - } |
|
280 | - |
|
281 | - } |
|
282 | - |
|
283 | - /* |
|
101 | + /** |
|
102 | + * Method to create a new invoice in the database. |
|
103 | + * |
|
104 | + * @param WPInv_Invoice $invoice Invoice object. |
|
105 | + */ |
|
106 | + public function create( &$invoice ) { |
|
107 | + $invoice->set_version( WPINV_VERSION ); |
|
108 | + $invoice->set_date_created( current_time('mysql') ); |
|
109 | + |
|
110 | + // Create a new post. |
|
111 | + $id = wp_insert_post( |
|
112 | + apply_filters( |
|
113 | + 'getpaid_new_invoice_data', |
|
114 | + array( |
|
115 | + 'post_date' => $invoice->get_date_created( 'edit' ), |
|
116 | + 'post_type' => $invoice->get_post_type( 'edit' ), |
|
117 | + 'post_status' => $this->get_post_status( $invoice ), |
|
118 | + 'ping_status' => 'closed', |
|
119 | + 'post_author' => $invoice->get_user_id( 'edit' ), |
|
120 | + 'post_title' => $invoice->get_title( 'edit' ), |
|
121 | + 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
122 | + 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
123 | + 'post_name' => $invoice->get_path( 'edit' ), |
|
124 | + ) |
|
125 | + ), |
|
126 | + true |
|
127 | + ); |
|
128 | + |
|
129 | + if ( $id && ! is_wp_error( $id ) ) { |
|
130 | + |
|
131 | + // Update the new id and regenerate a title. |
|
132 | + $invoice->set_id( $id ); |
|
133 | + wp_update_post( array( 'ID' => $invoice->get_id(), 'post_title' => $invoice->get_number( 'edit' ) ) ); |
|
134 | + |
|
135 | + // Ensure both the key and number are set. |
|
136 | + $invoice->get_key(); |
|
137 | + $invoice->get_number(); |
|
138 | + |
|
139 | + // Save special fields and items. |
|
140 | + $this->save_special_fields( $invoice ); |
|
141 | + $this->save_items( $invoice ); |
|
142 | + |
|
143 | + // Update meta data. |
|
144 | + $this->update_post_meta( $invoice ); |
|
145 | + $invoice->save_meta_data(); |
|
146 | + |
|
147 | + // Apply changes. |
|
148 | + $invoice->apply_changes(); |
|
149 | + $this->clear_caches( $invoice ); |
|
150 | + |
|
151 | + // Fires after a new invoice is created. |
|
152 | + do_action( 'getpaid_new_' . $invoice->get_type(), $invoice->get_id(), $invoice ); |
|
153 | + return true; |
|
154 | + } |
|
155 | + |
|
156 | + if ( is_wp_error( $id ) ) { |
|
157 | + $invoice->last_error = $id->get_error_message(); |
|
158 | + } |
|
159 | + |
|
160 | + return false; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Method to read an invoice from the database. |
|
165 | + * |
|
166 | + * @param WPInv_Invoice $invoice Invoice object. |
|
167 | + * |
|
168 | + */ |
|
169 | + public function read( &$invoice ) { |
|
170 | + |
|
171 | + $invoice->set_defaults(); |
|
172 | + $invoice_object = get_post( $invoice->get_id() ); |
|
173 | + |
|
174 | + if ( ! $invoice->get_id() || ! $invoice_object || ! getpaid_is_invoice_post_type( $invoice_object->post_type ) ) { |
|
175 | + $invoice->last_error = __( 'Invalid invoice.', 'invoicing' ); |
|
176 | + $invoice->set_id( 0 ); |
|
177 | + return false; |
|
178 | + } |
|
179 | + |
|
180 | + $invoice->set_props( |
|
181 | + array( |
|
182 | + 'date_created' => 0 < $invoice_object->post_date ? $invoice_object->post_date : null, |
|
183 | + 'date_modified' => 0 < $invoice_object->post_modified ? $invoice_object->post_modified : null, |
|
184 | + 'status' => $invoice_object->post_status, |
|
185 | + 'author' => $invoice_object->post_author, |
|
186 | + 'description' => $invoice_object->post_excerpt, |
|
187 | + 'parent_id' => $invoice_object->post_parent, |
|
188 | + 'name' => $invoice_object->post_title, |
|
189 | + 'path' => $invoice_object->post_name, |
|
190 | + 'post_type' => $invoice_object->post_type, |
|
191 | + ) |
|
192 | + ); |
|
193 | + |
|
194 | + $invoice->set_type( $invoice_object->post_type ); |
|
195 | + |
|
196 | + $this->read_object_data( $invoice, $invoice_object ); |
|
197 | + $this->add_special_fields( $invoice ); |
|
198 | + $this->add_items( $invoice ); |
|
199 | + $invoice->read_meta_data(); |
|
200 | + $invoice->set_object_read( true ); |
|
201 | + do_action( 'getpaid_read_' . $invoice->get_type(), $invoice->get_id(), $invoice ); |
|
202 | + |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * Method to update an invoice in the database. |
|
207 | + * |
|
208 | + * @param WPInv_Invoice $invoice Invoice object. |
|
209 | + */ |
|
210 | + public function update( &$invoice ) { |
|
211 | + $invoice->save_meta_data(); |
|
212 | + $invoice->set_version( WPINV_VERSION ); |
|
213 | + |
|
214 | + if ( null === $invoice->get_date_created( 'edit' ) ) { |
|
215 | + $invoice->set_date_created( current_time('mysql') ); |
|
216 | + } |
|
217 | + |
|
218 | + // Ensure both the key and number are set. |
|
219 | + $invoice->get_key(); |
|
220 | + $invoice->get_number(); |
|
221 | + |
|
222 | + // Grab the current status so we can compare. |
|
223 | + $previous_status = get_post_status( $invoice->get_id() ); |
|
224 | + |
|
225 | + $changes = $invoice->get_changes(); |
|
226 | + |
|
227 | + // Only update the post when the post data changes. |
|
228 | + if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'name', 'author', 'description', 'parent_id', 'post_excerpt', 'path' ), array_keys( $changes ) ) ) { |
|
229 | + $post_data = array( |
|
230 | + 'post_date' => $invoice->get_date_created( 'edit' ), |
|
231 | + 'post_date_gmt' => $invoice->get_date_created_gmt( 'edit' ), |
|
232 | + 'post_status' => $invoice->get_status( 'edit' ), |
|
233 | + 'post_title' => $invoice->get_name( 'edit' ), |
|
234 | + 'post_author' => $invoice->get_user_id( 'edit' ), |
|
235 | + 'post_modified' => $invoice->get_date_modified( 'edit' ), |
|
236 | + 'post_excerpt' => $invoice->get_description( 'edit' ), |
|
237 | + 'post_parent' => $invoice->get_parent_id( 'edit' ), |
|
238 | + 'post_name' => $invoice->get_path( 'edit' ), |
|
239 | + 'post_type' => $invoice->get_post_type( 'edit' ), |
|
240 | + ); |
|
241 | + |
|
242 | + /** |
|
243 | + * When updating this object, to prevent infinite loops, use $wpdb |
|
244 | + * to update data, since wp_update_post spawns more calls to the |
|
245 | + * save_post action. |
|
246 | + * |
|
247 | + * This ensures hooks are fired by either WP itself (admin screen save), |
|
248 | + * or an update purely from CRUD. |
|
249 | + */ |
|
250 | + if ( doing_action( 'save_post' ) ) { |
|
251 | + $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $invoice->get_id() ) ); |
|
252 | + clean_post_cache( $invoice->get_id() ); |
|
253 | + } else { |
|
254 | + wp_update_post( array_merge( array( 'ID' => $invoice->get_id() ), $post_data ) ); |
|
255 | + } |
|
256 | + $invoice->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
257 | + } |
|
258 | + |
|
259 | + // Update meta data. |
|
260 | + $this->update_post_meta( $invoice ); |
|
261 | + |
|
262 | + // Save special fields and items. |
|
263 | + $this->save_special_fields( $invoice ); |
|
264 | + $this->save_items( $invoice ); |
|
265 | + |
|
266 | + // Apply the changes. |
|
267 | + $invoice->apply_changes(); |
|
268 | + |
|
269 | + // Clear caches. |
|
270 | + $this->clear_caches( $invoice ); |
|
271 | + |
|
272 | + // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
273 | + $new_status = $invoice->get_status( 'edit' ); |
|
274 | + |
|
275 | + if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
276 | + do_action( 'getpaid_new_' . $invoice->get_type(), $invoice->get_id(), $invoice ); |
|
277 | + } else { |
|
278 | + do_action( 'getpaid_update_' . $invoice->get_type(), $invoice->get_id(), $invoice ); |
|
279 | + } |
|
280 | + |
|
281 | + } |
|
282 | + |
|
283 | + /* |
|
284 | 284 | |-------------------------------------------------------------------------- |
285 | 285 | | Additional Methods |
286 | 286 | |-------------------------------------------------------------------------- |
287 | 287 | */ |
288 | 288 | |
289 | - /** |
|
289 | + /** |
|
290 | 290 | * Retrieves special fields and adds to the invoice. |
291 | - * |
|
292 | - * @param WPInv_Invoice $invoice Invoice object. |
|
291 | + * |
|
292 | + * @param WPInv_Invoice $invoice Invoice object. |
|
293 | 293 | */ |
294 | 294 | public function add_special_fields( &$invoice ) { |
295 | - global $wpdb; |
|
295 | + global $wpdb; |
|
296 | 296 | |
297 | - // Maybe retrieve from the cache. |
|
298 | - $data = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
297 | + // Maybe retrieve from the cache. |
|
298 | + $data = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
299 | 299 | |
300 | - // If not found, retrieve from the db. |
|
301 | - if ( false === $data ) { |
|
302 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
300 | + // If not found, retrieve from the db. |
|
301 | + if ( false === $data ) { |
|
302 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
303 | 303 | |
304 | - $data = $wpdb->get_row( |
|
305 | - $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d LIMIT 1", $invoice->get_id() ), |
|
306 | - ARRAY_A |
|
307 | - ); |
|
304 | + $data = $wpdb->get_row( |
|
305 | + $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d LIMIT 1", $invoice->get_id() ), |
|
306 | + ARRAY_A |
|
307 | + ); |
|
308 | 308 | |
309 | - // Update the cache with our data |
|
310 | - wp_cache_set( $invoice->get_id(), $data, 'getpaid_invoice_special_fields' ); |
|
309 | + // Update the cache with our data |
|
310 | + wp_cache_set( $invoice->get_id(), $data, 'getpaid_invoice_special_fields' ); |
|
311 | 311 | |
312 | - } |
|
312 | + } |
|
313 | 313 | |
314 | - // Abort if the data does not exist. |
|
315 | - if ( empty( $data ) ) { |
|
316 | - $invoice->set_object_read( true ); |
|
317 | - $invoice->set_props( wpinv_get_user_address( $invoice->get_user_id() ) ); |
|
318 | - return; |
|
319 | - } |
|
314 | + // Abort if the data does not exist. |
|
315 | + if ( empty( $data ) ) { |
|
316 | + $invoice->set_object_read( true ); |
|
317 | + $invoice->set_props( wpinv_get_user_address( $invoice->get_user_id() ) ); |
|
318 | + return; |
|
319 | + } |
|
320 | 320 | |
321 | - $props = array(); |
|
321 | + $props = array(); |
|
322 | 322 | |
323 | - foreach ( $this->database_fields_to_props as $db_field => $prop ) { |
|
323 | + foreach ( $this->database_fields_to_props as $db_field => $prop ) { |
|
324 | 324 | |
325 | - if ( $db_field == 'post_id' ) { |
|
326 | - continue; |
|
327 | - } |
|
328 | - |
|
329 | - $props[ $prop ] = $data[ $db_field ]; |
|
330 | - } |
|
331 | - |
|
332 | - $invoice->set_props( $props ); |
|
333 | - |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * Gets a list of special fields that need updated based on change state |
|
338 | - * or if they are present in the database or not. |
|
339 | - * |
|
340 | - * @param WPInv_Invoice $invoice The Invoice object. |
|
341 | - * @return array A mapping of field keys => prop names, filtered by ones that should be updated. |
|
342 | - */ |
|
343 | - protected function get_special_fields_to_update( $invoice ) { |
|
344 | - $fields_to_update = array(); |
|
345 | - $changed_props = $invoice->get_changes(); |
|
346 | - |
|
347 | - // Props should be updated if they are a part of the $changed array or don't exist yet. |
|
348 | - foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
349 | - if ( array_key_exists( $prop, $changed_props ) ) { |
|
350 | - $fields_to_update[ $database_field ] = $prop; |
|
351 | - } |
|
352 | - } |
|
353 | - |
|
354 | - return $fields_to_update; |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * Helper method that updates all the database fields for an invoice based on it's settings in the WPInv_Invoice class. |
|
359 | - * |
|
360 | - * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
361 | - * @since 1.0.19 |
|
362 | - */ |
|
363 | - protected function update_special_fields( &$invoice ) { |
|
364 | - global $wpdb; |
|
365 | - |
|
366 | - $updated_props = array(); |
|
367 | - $fields_to_update = $this->get_special_fields_to_update( $invoice ); |
|
368 | - |
|
369 | - foreach ( $fields_to_update as $database_field => $prop ) { |
|
370 | - $value = $invoice->{"get_$prop"}( 'edit' ); |
|
371 | - $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
372 | - $value = is_bool( $value ) ? ( int ) $value : $value; |
|
373 | - $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
374 | - } |
|
375 | - |
|
376 | - if ( ! empty( $updated_props ) ) { |
|
377 | - |
|
378 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
379 | - $wpdb->update( $table, $updated_props, array( 'post_id' => $invoice->get_id() ) ); |
|
380 | - wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
381 | - do_action( "getpaid_invoice_update_database_fields", $invoice, $updated_props ); |
|
382 | - |
|
383 | - } |
|
384 | - |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * Helper method that inserts special fields to the database. |
|
389 | - * |
|
390 | - * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
391 | - * @since 1.0.19 |
|
392 | - */ |
|
393 | - protected function insert_special_fields( &$invoice ) { |
|
394 | - global $wpdb; |
|
395 | - |
|
396 | - $updated_props = array(); |
|
397 | - |
|
398 | - foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
399 | - $value = $invoice->{"get_$prop"}( 'edit' ); |
|
400 | - $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
401 | - $value = is_bool( $value ) ? ( int ) $value : $value; |
|
402 | - $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
403 | - } |
|
404 | - |
|
405 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
406 | - $wpdb->insert( $table, $updated_props ); |
|
407 | - wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
408 | - do_action( "getpaid_invoice_insert_database_fields", $invoice, $updated_props ); |
|
409 | - |
|
410 | - } |
|
411 | - |
|
412 | - /** |
|
325 | + if ( $db_field == 'post_id' ) { |
|
326 | + continue; |
|
327 | + } |
|
328 | + |
|
329 | + $props[ $prop ] = $data[ $db_field ]; |
|
330 | + } |
|
331 | + |
|
332 | + $invoice->set_props( $props ); |
|
333 | + |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * Gets a list of special fields that need updated based on change state |
|
338 | + * or if they are present in the database or not. |
|
339 | + * |
|
340 | + * @param WPInv_Invoice $invoice The Invoice object. |
|
341 | + * @return array A mapping of field keys => prop names, filtered by ones that should be updated. |
|
342 | + */ |
|
343 | + protected function get_special_fields_to_update( $invoice ) { |
|
344 | + $fields_to_update = array(); |
|
345 | + $changed_props = $invoice->get_changes(); |
|
346 | + |
|
347 | + // Props should be updated if they are a part of the $changed array or don't exist yet. |
|
348 | + foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
349 | + if ( array_key_exists( $prop, $changed_props ) ) { |
|
350 | + $fields_to_update[ $database_field ] = $prop; |
|
351 | + } |
|
352 | + } |
|
353 | + |
|
354 | + return $fields_to_update; |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * Helper method that updates all the database fields for an invoice based on it's settings in the WPInv_Invoice class. |
|
359 | + * |
|
360 | + * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
361 | + * @since 1.0.19 |
|
362 | + */ |
|
363 | + protected function update_special_fields( &$invoice ) { |
|
364 | + global $wpdb; |
|
365 | + |
|
366 | + $updated_props = array(); |
|
367 | + $fields_to_update = $this->get_special_fields_to_update( $invoice ); |
|
368 | + |
|
369 | + foreach ( $fields_to_update as $database_field => $prop ) { |
|
370 | + $value = $invoice->{"get_$prop"}( 'edit' ); |
|
371 | + $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
372 | + $value = is_bool( $value ) ? ( int ) $value : $value; |
|
373 | + $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
374 | + } |
|
375 | + |
|
376 | + if ( ! empty( $updated_props ) ) { |
|
377 | + |
|
378 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
379 | + $wpdb->update( $table, $updated_props, array( 'post_id' => $invoice->get_id() ) ); |
|
380 | + wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
381 | + do_action( "getpaid_invoice_update_database_fields", $invoice, $updated_props ); |
|
382 | + |
|
383 | + } |
|
384 | + |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * Helper method that inserts special fields to the database. |
|
389 | + * |
|
390 | + * @param WPInv_Invoice $invoice WPInv_Invoice object. |
|
391 | + * @since 1.0.19 |
|
392 | + */ |
|
393 | + protected function insert_special_fields( &$invoice ) { |
|
394 | + global $wpdb; |
|
395 | + |
|
396 | + $updated_props = array(); |
|
397 | + |
|
398 | + foreach ( $this->database_fields_to_props as $database_field => $prop ) { |
|
399 | + $value = $invoice->{"get_$prop"}( 'edit' ); |
|
400 | + $value = is_string( $value ) ? wp_slash( $value ) : $value; |
|
401 | + $value = is_bool( $value ) ? ( int ) $value : $value; |
|
402 | + $updated_props[ $database_field ] = maybe_serialize( $value ); |
|
403 | + } |
|
404 | + |
|
405 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
406 | + $wpdb->insert( $table, $updated_props ); |
|
407 | + wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_special_fields' ); |
|
408 | + do_action( "getpaid_invoice_insert_database_fields", $invoice, $updated_props ); |
|
409 | + |
|
410 | + } |
|
411 | + |
|
412 | + /** |
|
413 | 413 | * Saves all special fields. |
414 | - * |
|
415 | - * @param WPInv_Invoice $invoice Invoice object. |
|
414 | + * |
|
415 | + * @param WPInv_Invoice $invoice Invoice object. |
|
416 | 416 | */ |
417 | 417 | public function save_special_fields( $invoice ) { |
418 | - global $wpdb; |
|
418 | + global $wpdb; |
|
419 | 419 | |
420 | - // The invoices table. |
|
421 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
422 | - $id = (int) $invoice->get_id(); |
|
420 | + // The invoices table. |
|
421 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
422 | + $id = (int) $invoice->get_id(); |
|
423 | 423 | |
424 | - if ( $wpdb->get_var( "SELECT `post_id` FROM $table WHERE `post_id`= $id" ) ) { |
|
424 | + if ( $wpdb->get_var( "SELECT `post_id` FROM $table WHERE `post_id`= $id" ) ) { |
|
425 | 425 | |
426 | - $this->update_special_fields( $invoice ); |
|
426 | + $this->update_special_fields( $invoice ); |
|
427 | 427 | |
428 | - } else { |
|
428 | + } else { |
|
429 | 429 | |
430 | - $this->insert_special_fields( $invoice ); |
|
430 | + $this->insert_special_fields( $invoice ); |
|
431 | 431 | |
432 | - } |
|
432 | + } |
|
433 | 433 | |
434 | - } |
|
434 | + } |
|
435 | 435 | |
436 | - /** |
|
436 | + /** |
|
437 | 437 | * Set's up cart details. |
438 | - * |
|
439 | - * @param WPInv_Invoice $invoice Invoice object. |
|
438 | + * |
|
439 | + * @param WPInv_Invoice $invoice Invoice object. |
|
440 | 440 | */ |
441 | 441 | public function add_items( &$invoice ) { |
442 | - global $wpdb; |
|
442 | + global $wpdb; |
|
443 | 443 | |
444 | - // Maybe retrieve from the cache. |
|
445 | - $items = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
444 | + // Maybe retrieve from the cache. |
|
445 | + $items = wp_cache_get( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
446 | 446 | |
447 | - // If not found, retrieve from the db. |
|
448 | - if ( false === $items ) { |
|
449 | - $table = $wpdb->prefix . 'getpaid_invoice_items'; |
|
447 | + // If not found, retrieve from the db. |
|
448 | + if ( false === $items ) { |
|
449 | + $table = $wpdb->prefix . 'getpaid_invoice_items'; |
|
450 | 450 | |
451 | - $items = $wpdb->get_results( |
|
452 | - $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d", $invoice->get_id() ) |
|
453 | - ); |
|
451 | + $items = $wpdb->get_results( |
|
452 | + $wpdb->prepare( "SELECT * FROM $table WHERE `post_id`=%d", $invoice->get_id() ) |
|
453 | + ); |
|
454 | 454 | |
455 | - // Update the cache with our data |
|
456 | - wp_cache_set( $invoice->get_id(), $items, 'getpaid_invoice_cart_details' ); |
|
455 | + // Update the cache with our data |
|
456 | + wp_cache_set( $invoice->get_id(), $items, 'getpaid_invoice_cart_details' ); |
|
457 | 457 | |
458 | - } |
|
458 | + } |
|
459 | 459 | |
460 | - // Abort if no items found. |
|
460 | + // Abort if no items found. |
|
461 | 461 | if ( empty( $items ) ) { |
462 | 462 | return; |
463 | - } |
|
463 | + } |
|
464 | 464 | |
465 | - foreach ( $items as $item_data ) { |
|
466 | - $item = new GetPaid_Form_Item( $item_data->item_id ); |
|
465 | + foreach ( $items as $item_data ) { |
|
466 | + $item = new GetPaid_Form_Item( $item_data->item_id ); |
|
467 | 467 | |
468 | - // Set item data. |
|
469 | - $item->item_tax = wpinv_sanitize_amount( $item_data->tax ); |
|
470 | - $item->item_discount = wpinv_sanitize_amount( $item_data->discount ); |
|
471 | - $item->set_name( $item_data->item_name ); |
|
472 | - $item->set_description( $item_data->item_description ); |
|
473 | - $item->set_price( $item_data->item_price ); |
|
474 | - $item->set_quantity( $item_data->quantity ); |
|
475 | - $item->set_item_meta( $item_data->meta ); |
|
468 | + // Set item data. |
|
469 | + $item->item_tax = wpinv_sanitize_amount( $item_data->tax ); |
|
470 | + $item->item_discount = wpinv_sanitize_amount( $item_data->discount ); |
|
471 | + $item->set_name( $item_data->item_name ); |
|
472 | + $item->set_description( $item_data->item_description ); |
|
473 | + $item->set_price( $item_data->item_price ); |
|
474 | + $item->set_quantity( $item_data->quantity ); |
|
475 | + $item->set_item_meta( $item_data->meta ); |
|
476 | 476 | |
477 | - $invoice->add_item( $item ); |
|
478 | - } |
|
477 | + $invoice->add_item( $item ); |
|
478 | + } |
|
479 | 479 | |
480 | - } |
|
480 | + } |
|
481 | 481 | |
482 | - /** |
|
482 | + /** |
|
483 | 483 | * Saves cart details. |
484 | - * |
|
485 | - * @param WPInv_Invoice $invoice Invoice object. |
|
484 | + * |
|
485 | + * @param WPInv_Invoice $invoice Invoice object. |
|
486 | 486 | */ |
487 | 487 | public function save_items( $invoice ) { |
488 | 488 | |
489 | - // Delete previously existing items. |
|
490 | - $this->delete_items( $invoice ); |
|
489 | + // Delete previously existing items. |
|
490 | + $this->delete_items( $invoice ); |
|
491 | 491 | |
492 | - $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
492 | + $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
493 | 493 | |
494 | - foreach ( $invoice->get_cart_details() as $item_data ) { |
|
495 | - $item_data = array_map( 'maybe_serialize', $item_data ); |
|
496 | - $GLOBALS['wpdb']->insert( $table, $item_data ); |
|
497 | - } |
|
494 | + foreach ( $invoice->get_cart_details() as $item_data ) { |
|
495 | + $item_data = array_map( 'maybe_serialize', $item_data ); |
|
496 | + $GLOBALS['wpdb']->insert( $table, $item_data ); |
|
497 | + } |
|
498 | 498 | |
499 | - wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
500 | - do_action( "getpaid_invoice_save_items", $invoice ); |
|
499 | + wp_cache_delete( $invoice->get_id(), 'getpaid_invoice_cart_details' ); |
|
500 | + do_action( "getpaid_invoice_save_items", $invoice ); |
|
501 | 501 | |
502 | - } |
|
502 | + } |
|
503 | 503 | |
504 | - /** |
|
504 | + /** |
|
505 | 505 | * Deletes an invoice's cart details from the database. |
506 | - * |
|
507 | - * @param WPInv_Invoice $invoice Invoice object. |
|
506 | + * |
|
507 | + * @param WPInv_Invoice $invoice Invoice object. |
|
508 | 508 | */ |
509 | 509 | public function delete_items( $invoice ) { |
510 | - $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
511 | - return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
512 | - } |
|
510 | + $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoice_items'; |
|
511 | + return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
512 | + } |
|
513 | 513 | |
514 | - /** |
|
514 | + /** |
|
515 | 515 | * Deletes an invoice's special fields from the database. |
516 | - * |
|
517 | - * @param WPInv_Invoice $invoice Invoice object. |
|
516 | + * |
|
517 | + * @param WPInv_Invoice $invoice Invoice object. |
|
518 | 518 | */ |
519 | 519 | public function delete_special_fields( $invoice ) { |
520 | - $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoices'; |
|
521 | - return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
520 | + $table = $GLOBALS['wpdb']->prefix . 'getpaid_invoices'; |
|
521 | + return $GLOBALS['wpdb']->delete( $table, array( 'post_id' => $invoice->get_id() ) ); |
|
522 | 522 | } |
523 | 523 | |
524 | 524 | } |
@@ -14,744 +14,744 @@ discard block |
||
14 | 14 | */ |
15 | 15 | class WPInv_Plugin { |
16 | 16 | |
17 | - /** |
|
18 | - * GetPaid version. |
|
19 | - * |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - public $version; |
|
23 | - |
|
24 | - /** |
|
25 | - * Session instance. |
|
26 | - * |
|
27 | - * @var WPInv_Session_Handler |
|
28 | - */ |
|
29 | - public $session; |
|
30 | - |
|
31 | - /** |
|
32 | - * Notes instance. |
|
33 | - * |
|
34 | - * @var WPInv_Notes |
|
35 | - */ |
|
36 | - public $notes; |
|
37 | - |
|
38 | - /** |
|
39 | - * Reports instance. |
|
40 | - * |
|
41 | - * @var WPInv_Reports |
|
42 | - */ |
|
43 | - public $reports; |
|
44 | - |
|
45 | - /** |
|
46 | - * API instance. |
|
47 | - * |
|
48 | - * @var WPInv_API |
|
49 | - */ |
|
50 | - public $api; |
|
51 | - |
|
52 | - /** |
|
53 | - * Form elements instance. |
|
54 | - * |
|
55 | - * @var WPInv_Payment_Form_Elements |
|
56 | - */ |
|
57 | - public $form_elements; |
|
58 | - |
|
59 | - /** |
|
60 | - * Tax instance. |
|
61 | - * |
|
62 | - * @var WPInv_EUVat |
|
63 | - */ |
|
64 | - public $tax; |
|
65 | - |
|
66 | - /** |
|
67 | - * @param array An array of payment gateways. |
|
68 | - */ |
|
69 | - public $gateways; |
|
70 | - |
|
71 | - /** |
|
72 | - * Post types instance. |
|
73 | - * |
|
74 | - * @var GetPaid_Post_Types |
|
75 | - */ |
|
76 | - public $post_types; |
|
77 | - |
|
78 | - /** |
|
79 | - * Class constructor. |
|
80 | - */ |
|
81 | - public function __construct() { |
|
82 | - $this->define_constants(); |
|
83 | - $this->includes(); |
|
84 | - $this->init_hooks(); |
|
85 | - $this->set_properties(); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Define class properties. |
|
90 | - */ |
|
91 | - public function set_properties() { |
|
92 | - |
|
93 | - $this->session = new WPInv_Session_Handler(); |
|
94 | - $GLOBALS['wpi_session'] = $this->session; // Backwards compatibility. |
|
95 | - $this->notes = new WPInv_Notes(); |
|
96 | - $this->reports = new WPInv_Reports(); |
|
97 | - $this->api = new WPInv_API(); |
|
98 | - $this->form_elements = new WPInv_Payment_Form_Elements(); |
|
99 | - $this->tax = new WPInv_EUVat(); |
|
100 | - $this->post_types = new GetPaid_Post_Types(); |
|
101 | - $this->tax->init(); |
|
102 | - $GLOBALS['wpinv_euvat'] = $this->tax; // Backwards compatibility. |
|
103 | - |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Define plugin constants. |
|
108 | - */ |
|
109 | - public function define_constants() { |
|
110 | - define( 'WPINV_PLUGIN_DIR', plugin_dir_path( WPINV_PLUGIN_FILE ) ); |
|
111 | - define( 'WPINV_PLUGIN_URL', plugin_dir_url( WPINV_PLUGIN_FILE ) ); |
|
112 | - $this->version = WPINV_VERSION; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Hook into actions and filters. |
|
117 | - * |
|
118 | - * @since 1.0.19 |
|
119 | - */ |
|
120 | - protected function init_hooks() { |
|
121 | - /* Internationalize the text strings used. */ |
|
122 | - add_action( 'plugins_loaded', array( &$this, 'plugins_loaded' ) ); |
|
123 | - |
|
124 | - /* Perform actions on admin initialization. */ |
|
125 | - add_action( 'admin_init', array( &$this, 'admin_init') ); |
|
126 | - |
|
127 | - // Init the plugin after WordPress inits. |
|
128 | - add_action( 'init', array( $this, 'init' ), 1 ); |
|
129 | - add_action( 'getpaid_init', array( $this, 'maybe_process_ipn' ), 5 ); |
|
130 | - add_action( 'init', array( &$this, 'wpinv_actions' ) ); |
|
131 | - |
|
132 | - if ( class_exists( 'BuddyPress' ) ) { |
|
133 | - add_action( 'bp_include', array( &$this, 'bp_invoicing_init' ) ); |
|
134 | - } |
|
135 | - |
|
136 | - add_action( 'wp_enqueue_scripts', array( &$this, 'enqueue_scripts' ) ); |
|
137 | - add_action( 'wp_footer', array( &$this, 'wp_footer' ) ); |
|
138 | - add_action( 'widgets_init', array( &$this, 'register_widgets' ) ); |
|
139 | - add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', array( $this, 'wpseo_exclude_from_sitemap_by_post_ids' ) ); |
|
140 | - |
|
141 | - if ( is_admin() ) { |
|
142 | - add_action( 'admin_enqueue_scripts', array( &$this, 'admin_enqueue_scripts' ) ); |
|
143 | - add_filter( 'admin_body_class', array( &$this, 'admin_body_class' ) ); |
|
144 | - add_action( 'admin_init', array( &$this, 'init_ayecode_connect_helper' ) ); |
|
145 | - |
|
146 | - } else { |
|
147 | - add_filter( 'pre_get_posts', array( &$this, 'pre_get_posts' ) ); |
|
148 | - } |
|
149 | - |
|
150 | - add_action( 'admin_init', array( &$this, 'activation_redirect') ); |
|
151 | - |
|
152 | - // Fires after registering core hooks. |
|
153 | - do_action_ref_array( 'wpinv_actions', array( &$this ) ); |
|
154 | - |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Maybe show the AyeCode Connect Notice. |
|
159 | - */ |
|
160 | - public function init_ayecode_connect_helper(){ |
|
161 | - // AyeCode Connect notice |
|
162 | - if ( is_admin() ){ |
|
163 | - // set the strings so they can be translated |
|
164 | - $strings = array( |
|
165 | - 'connect_title' => __("WP Invoicing - an AyeCode product!","invoicing"), |
|
166 | - 'connect_external' => __( "Please confirm you wish to connect your site?","invoicing" ), |
|
167 | - 'connect' => sprintf( __( "<strong>Have a license?</strong> Forget about entering license keys or downloading zip files, connect your site for instant access. %slearn more%s","invoicing" ),"<a href='https://ayecode.io/introducing-ayecode-connect/' target='_blank'>","</a>" ), |
|
168 | - 'connect_button' => __("Connect Site","invoicing"), |
|
169 | - 'connecting_button' => __("Connecting...","invoicing"), |
|
170 | - 'error_localhost' => __( "This service will only work with a live domain, not a localhost.","invoicing" ), |
|
171 | - 'error' => __( "Something went wrong, please refresh and try again.","invoicing" ), |
|
172 | - ); |
|
173 | - new AyeCode_Connect_Helper($strings,array('wpi-addons')); |
|
174 | - } |
|
175 | - } |
|
176 | - |
|
177 | - public function plugins_loaded() { |
|
178 | - /* Internationalize the text strings used. */ |
|
179 | - $this->load_textdomain(); |
|
180 | - |
|
181 | - do_action( 'wpinv_loaded' ); |
|
182 | - |
|
183 | - // Fix oxygen page builder conflict |
|
184 | - if ( function_exists( 'ct_css_output' ) ) { |
|
185 | - wpinv_oxygen_fix_conflict(); |
|
186 | - } |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Load the translation of the plugin. |
|
191 | - * |
|
192 | - * @since 1.0 |
|
193 | - */ |
|
194 | - public function load_textdomain( $locale = NULL ) { |
|
195 | - if ( empty( $locale ) ) { |
|
196 | - $locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale(); |
|
197 | - } |
|
198 | - |
|
199 | - $locale = apply_filters( 'plugin_locale', $locale, 'invoicing' ); |
|
200 | - |
|
201 | - unload_textdomain( 'invoicing' ); |
|
202 | - load_textdomain( 'invoicing', WP_LANG_DIR . '/invoicing/invoicing-' . $locale . '.mo' ); |
|
203 | - load_plugin_textdomain( 'invoicing', false, WPINV_PLUGIN_DIR . 'languages' ); |
|
204 | - |
|
205 | - /** |
|
206 | - * Define language constants. |
|
207 | - */ |
|
208 | - require_once( WPINV_PLUGIN_DIR . 'language.php' ); |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Include required core files used in admin and on the frontend. |
|
213 | - */ |
|
214 | - public function includes() { |
|
215 | - |
|
216 | - // Start with the settings. |
|
217 | - require_once( WPINV_PLUGIN_DIR . 'includes/admin/register-settings.php' ); |
|
218 | - |
|
219 | - // Packages/libraries. |
|
220 | - require_once( WPINV_PLUGIN_DIR . 'vendor/autoload.php' ); |
|
221 | - require_once( WPINV_PLUGIN_DIR . 'vendor/ayecode/wp-ayecode-ui/ayecode-ui-loader.php' ); |
|
222 | - require_once( WPINV_PLUGIN_DIR . 'includes/libraries/action-scheduler/action-scheduler.php' ); |
|
223 | - |
|
224 | - // Load functions. |
|
225 | - require_once( WPINV_PLUGIN_DIR . 'includes/deprecated-functions.php' ); |
|
226 | - require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-email-functions.php' ); |
|
227 | - require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-general-functions.php' ); |
|
228 | - require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-helper-functions.php' ); |
|
229 | - require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-tax-functions.php' ); |
|
230 | - require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-template-functions.php' ); |
|
231 | - require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-address-functions.php' ); |
|
232 | - require_once( WPINV_PLUGIN_DIR . 'includes/invoice-functions.php' ); |
|
233 | - require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-item-functions.php' ); |
|
234 | - require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-discount-functions.php' ); |
|
235 | - require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-gateway-functions.php' ); |
|
236 | - require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-payment-functions.php' ); |
|
237 | - require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-user-functions.php' ); |
|
238 | - require_once( WPINV_PLUGIN_DIR . 'includes/error-functions.php' ); |
|
239 | - |
|
240 | - // Register autoloader. |
|
241 | - try { |
|
242 | - spl_autoload_register( array( $this, 'autoload' ), true ); |
|
243 | - } catch ( Exception $e ) { |
|
244 | - wpinv_error_log( $e->getMessage(), '', __FILE__, 149, true ); |
|
245 | - } |
|
246 | - |
|
247 | - require_once( WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-session.php' ); |
|
248 | - require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-session-handler.php' ); |
|
249 | - require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-ajax.php' ); |
|
250 | - require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-api.php' ); |
|
251 | - require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-reports.php' ); |
|
252 | - require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-cache-helper.php' ); |
|
253 | - require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-db.php' ); |
|
254 | - require_once( WPINV_PLUGIN_DIR . 'includes/admin/subscriptions.php' ); |
|
255 | - require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-subscriptions-db.php' ); |
|
256 | - require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-subscriptions.php' ); |
|
257 | - require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-subscription.php' ); |
|
258 | - require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-subscriptions-list-table.php' ); |
|
259 | - require_once( WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-privacy.php' ); |
|
260 | - require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-privacy.php' ); |
|
261 | - require_once( WPINV_PLUGIN_DIR . 'includes/libraries/class-ayecode-addons.php' ); |
|
262 | - require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-addons.php' ); |
|
263 | - require_once( WPINV_PLUGIN_DIR . 'widgets/checkout.php' ); |
|
264 | - require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-history.php' ); |
|
265 | - require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-receipt.php' ); |
|
266 | - require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-messages.php' ); |
|
267 | - require_once( WPINV_PLUGIN_DIR . 'widgets/subscriptions.php' ); |
|
268 | - require_once( WPINV_PLUGIN_DIR . 'widgets/buy-item.php' ); |
|
269 | - require_once( WPINV_PLUGIN_DIR . 'widgets/getpaid.php' ); |
|
270 | - require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-payment-form-elements.php' ); |
|
271 | - |
|
272 | - /** |
|
273 | - * Load the tax class. |
|
274 | - */ |
|
275 | - if ( ! class_exists( 'WPInv_EUVat' ) ) { |
|
276 | - require_once( WPINV_PLUGIN_DIR . 'includes/libraries/wpinv-euvat/class-wpinv-euvat.php' ); |
|
277 | - } |
|
278 | - |
|
279 | - $gateways = array_keys( wpinv_get_enabled_payment_gateways() ); |
|
280 | - if ( !empty( $gateways ) ) { |
|
281 | - foreach ( $gateways as $gateway ) { |
|
282 | - if ( $gateway == 'manual' ) { |
|
283 | - continue; |
|
284 | - } |
|
285 | - |
|
286 | - $gateway_file = WPINV_PLUGIN_DIR . 'includes/gateways/' . $gateway . '.php'; |
|
287 | - |
|
288 | - if ( file_exists( $gateway_file ) ) { |
|
289 | - require_once( $gateway_file ); |
|
290 | - } |
|
291 | - } |
|
292 | - } |
|
293 | - |
|
294 | - if ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) { |
|
295 | - GetPaid_Post_Types_Admin::init(); |
|
296 | - |
|
297 | - require_once( WPINV_PLUGIN_DIR . 'includes/admin/wpinv-upgrade-functions.php' ); |
|
298 | - require_once( WPINV_PLUGIN_DIR . 'includes/admin/wpinv-admin-functions.php' ); |
|
299 | - //require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-recurring-admin.php' ); |
|
300 | - require_once( WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-payment-form.php' ); |
|
301 | - require_once( WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-invoice-notes.php' ); |
|
302 | - require_once( WPINV_PLUGIN_DIR . 'includes/admin/admin-pages.php' ); |
|
303 | - require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-admin-menus.php' ); |
|
304 | - require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-users.php' ); |
|
305 | - require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-getpaid-admin-profile.php' ); |
|
306 | - //require_once( WPINV_PLUGIN_DIR . 'includes/admin/subscriptions.php' ); |
|
307 | - // load the user class only on the users.php page |
|
308 | - global $pagenow; |
|
309 | - if($pagenow=='users.php'){ |
|
310 | - new WPInv_Admin_Users(); |
|
311 | - } |
|
312 | - } |
|
313 | - |
|
314 | - // Register cli commands |
|
315 | - if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
316 | - require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-cli.php' ); |
|
317 | - WP_CLI::add_command( 'invoicing', 'WPInv_CLI' ); |
|
318 | - } |
|
319 | - |
|
320 | - // include css inliner |
|
321 | - if ( ! class_exists( 'Emogrifier' ) && class_exists( 'DOMDocument' ) ) { |
|
322 | - include_once( WPINV_PLUGIN_DIR . 'includes/libraries/class-emogrifier.php' ); |
|
323 | - } |
|
324 | - |
|
325 | - require_once( WPINV_PLUGIN_DIR . 'includes/admin/install.php' ); |
|
326 | - } |
|
327 | - |
|
328 | - /** |
|
329 | - * Class autoloader |
|
330 | - * |
|
331 | - * @param string $class_name The name of the class to load. |
|
332 | - * @access public |
|
333 | - * @since 1.0.19 |
|
334 | - * @return void |
|
335 | - */ |
|
336 | - public function autoload( $class_name ) { |
|
337 | - |
|
338 | - // Normalize the class name... |
|
339 | - $class_name = strtolower( $class_name ); |
|
340 | - |
|
341 | - // ... and make sure it is our class. |
|
342 | - if ( false === strpos( $class_name, 'getpaid_' ) && false === strpos( $class_name, 'wpinv_' ) ) { |
|
343 | - return; |
|
344 | - } |
|
345 | - |
|
346 | - // Next, prepare the file name from the class. |
|
347 | - $file_name = 'class-' . str_replace( '_', '-', $class_name ) . '.php'; |
|
348 | - |
|
349 | - // Base path of the classes. |
|
350 | - $plugin_path = untrailingslashit( WPINV_PLUGIN_DIR ); |
|
351 | - |
|
352 | - // And an array of possible locations in order of importance. |
|
353 | - $locations = array( |
|
354 | - "$plugin_path/includes", |
|
355 | - "$plugin_path/includes/data-stores", |
|
356 | - "$plugin_path/includes/gateways", |
|
357 | - "$plugin_path/includes/api", |
|
358 | - "$plugin_path/includes/admin", |
|
359 | - "$plugin_path/includes/admin/meta-boxes", |
|
360 | - ); |
|
361 | - |
|
362 | - foreach ( apply_filters( 'getpaid_autoload_locations', $locations ) as $location ) { |
|
363 | - |
|
364 | - if ( file_exists( trailingslashit( $location ) . $file_name ) ) { |
|
365 | - include trailingslashit( $location ) . $file_name; |
|
366 | - break; |
|
367 | - } |
|
368 | - |
|
369 | - } |
|
370 | - |
|
371 | - } |
|
372 | - |
|
373 | - /** |
|
374 | - * Inits hooks etc. |
|
375 | - */ |
|
376 | - public function init() { |
|
377 | - |
|
378 | - // Fires before getpaid inits. |
|
379 | - do_action( 'before_getpaid_init', $this ); |
|
380 | - |
|
381 | - // Load default gateways. |
|
382 | - $gateways = apply_filters( |
|
383 | - 'getpaid_default_gateways', |
|
384 | - array( |
|
385 | - 'manual' => 'GetPaid_Manual_Gateway', |
|
386 | - 'paypal' => 'GetPaid_Paypal_Gateway', |
|
387 | - 'worldpay' => 'GetPaid_Worldpay_Gateway', |
|
388 | - 'bank_transfer' => 'GetPaid_Bank_Transfer_Gateway', |
|
389 | - 'authorizenet' => 'GetPaid_Authorize_Net_Gateway', |
|
390 | - ) |
|
391 | - ); |
|
392 | - |
|
393 | - foreach ( $gateways as $id => $class ) { |
|
394 | - $this->gateways[ $id ] = new $class(); |
|
395 | - } |
|
396 | - |
|
397 | - // Fires after getpaid inits. |
|
398 | - do_action( 'getpaid_init', $this ); |
|
399 | - |
|
400 | - } |
|
401 | - |
|
402 | - /** |
|
403 | - * Checks if this is an IPN request and processes it. |
|
404 | - */ |
|
405 | - public function maybe_process_ipn() { |
|
406 | - |
|
407 | - // Ensure that this is an IPN request. |
|
408 | - if ( empty( $_GET['wpi-listener'] ) || 'IPN' !== $_GET['wpi-listener'] || empty( $_GET['wpi-gateway'] ) ) { |
|
409 | - return; |
|
410 | - } |
|
411 | - |
|
412 | - $gateway = wpinv_clean( $_GET['wpi-gateway'] ); |
|
413 | - |
|
414 | - do_action( 'wpinv_verify_payment_ipn', $gateway ); |
|
415 | - do_action( "wpinv_verify_{$gateway}_ipn" ); |
|
416 | - exit; |
|
417 | - |
|
418 | - } |
|
419 | - |
|
420 | - public function admin_init() { |
|
421 | - $this->default_payment_form = wpinv_get_default_payment_form(); |
|
422 | - add_action( 'admin_print_scripts-edit.php', array( &$this, 'admin_print_scripts_edit_php' ) ); |
|
423 | - } |
|
424 | - |
|
425 | - public function activation_redirect() { |
|
426 | - // Bail if no activation redirect |
|
427 | - if ( !get_transient( '_wpinv_activation_redirect' ) ) { |
|
428 | - return; |
|
429 | - } |
|
430 | - |
|
431 | - // Delete the redirect transient |
|
432 | - delete_transient( '_wpinv_activation_redirect' ); |
|
433 | - |
|
434 | - // Bail if activating from network, or bulk |
|
435 | - if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) { |
|
436 | - return; |
|
437 | - } |
|
438 | - |
|
439 | - wp_safe_redirect( admin_url( 'admin.php?page=wpinv-settings&tab=general' ) ); |
|
440 | - exit; |
|
441 | - } |
|
442 | - |
|
443 | - public function enqueue_scripts() { |
|
444 | - $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
445 | - |
|
446 | - $version = filemtime( WPINV_PLUGIN_DIR . 'assets/css/invoice-front.css' ); |
|
447 | - wp_register_style( 'wpinv_front_style', WPINV_PLUGIN_URL . 'assets/css/invoice-front.css', array(), $version ); |
|
448 | - wp_enqueue_style( 'wpinv_front_style' ); |
|
449 | - |
|
450 | - // Register scripts |
|
451 | - wp_register_script( 'jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array( 'jquery' ), '2.70', true ); |
|
452 | - wp_register_script( 'wpinv-front-script', WPINV_PLUGIN_URL . 'assets/js/invoice-front.js', array( 'jquery' ), filemtime( WPINV_PLUGIN_DIR . 'assets/js/invoice-front.js' ) ); |
|
453 | - |
|
454 | - $localize = array(); |
|
455 | - $localize['ajax_url'] = admin_url( 'admin-ajax.php' ); |
|
456 | - $localize['nonce'] = wp_create_nonce( 'wpinv-nonce' ); |
|
457 | - $localize['currency_symbol'] = wpinv_currency_symbol(); |
|
458 | - $localize['currency_pos'] = wpinv_currency_position(); |
|
459 | - $localize['thousand_sep'] = wpinv_thousands_separator(); |
|
460 | - $localize['decimal_sep'] = wpinv_decimal_separator(); |
|
461 | - $localize['decimals'] = wpinv_decimals(); |
|
462 | - $localize['txtComplete'] = __( 'Continue', 'invoicing' ); |
|
463 | - $localize['UseTaxes'] = wpinv_use_taxes(); |
|
464 | - $localize['checkoutNonce'] = wp_create_nonce( 'wpinv_checkout_nonce' ); |
|
465 | - $localize['formNonce'] = wp_create_nonce( 'getpaid_form_nonce' ); |
|
466 | - $localize['connectionError'] = __( 'Could not establish a connection to the server.', 'invoicing' ); |
|
467 | - |
|
468 | - $localize = apply_filters( 'wpinv_front_js_localize', $localize ); |
|
469 | - |
|
470 | - wp_enqueue_script( 'jquery-blockui' ); |
|
471 | - $autofill_api = wpinv_get_option('address_autofill_api'); |
|
472 | - $autofill_active = wpinv_get_option('address_autofill_active'); |
|
473 | - if ( isset( $autofill_active ) && 1 == $autofill_active && !empty( $autofill_api ) && wpinv_is_checkout() ) { |
|
474 | - if ( wp_script_is( 'google-maps-api', 'enqueued' ) ) { |
|
475 | - wp_dequeue_script( 'google-maps-api' ); |
|
476 | - } |
|
477 | - wp_enqueue_script( 'google-maps-api', 'https://maps.googleapis.com/maps/api/js?key=' . $autofill_api . '&libraries=places', array( 'jquery' ), '', false ); |
|
478 | - wp_enqueue_script( 'google-maps-init', WPINV_PLUGIN_URL . 'assets/js/gaaf.js', array( 'jquery', 'google-maps-api' ), '', true ); |
|
479 | - } |
|
480 | - |
|
481 | - wp_enqueue_style( "select2", WPINV_PLUGIN_URL . 'assets/css/select2/select2.css', array(), WPINV_VERSION, 'all' ); |
|
482 | - wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full' . $suffix . '.js', array( 'jquery' ), WPINV_VERSION ); |
|
483 | - |
|
484 | - wp_enqueue_script( 'wpinv-front-script' ); |
|
485 | - wp_localize_script( 'wpinv-front-script', 'WPInv', $localize ); |
|
486 | - |
|
487 | - $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/payment-forms.js' ); |
|
488 | - wp_enqueue_script( 'wpinv-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/payment-forms.js', array( 'wpinv-front-script', 'wp-hooks' ), $version, true ); |
|
489 | - } |
|
490 | - |
|
491 | - public function admin_enqueue_scripts( $hook ) { |
|
492 | - global $post, $pagenow; |
|
493 | - |
|
494 | - $post_type = wpinv_admin_post_type(); |
|
495 | - $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
496 | - $page = isset( $_GET['page'] ) ? strtolower( $_GET['page'] ) : ''; |
|
497 | - |
|
498 | - $jquery_ui_css = false; |
|
499 | - if ( ( $post_type == 'wpi_invoice' || $post_type == 'wpi_quote' || $post_type == 'wpi_discount' ) && ( $pagenow == 'post-new.php' || $pagenow == 'post.php' ) ) { |
|
500 | - $jquery_ui_css = true; |
|
501 | - } else if ( $page == 'wpinv-settings' || $page == 'wpinv-reports' ) { |
|
502 | - $jquery_ui_css = true; |
|
503 | - } |
|
504 | - if ( $jquery_ui_css ) { |
|
505 | - wp_register_style( 'jquery-ui-css', WPINV_PLUGIN_URL . 'assets/css/jquery-ui' . $suffix . '.css', array(), '1.8.16' ); |
|
506 | - wp_enqueue_style( 'jquery-ui-css' ); |
|
507 | - wp_deregister_style( 'yoast-seo-select2' ); |
|
508 | - wp_deregister_style( 'yoast-seo-monorepo' ); |
|
509 | - } |
|
510 | - |
|
511 | - wp_register_style( 'wpinv_meta_box_style', WPINV_PLUGIN_URL . 'assets/css/meta-box.css', array(), WPINV_VERSION ); |
|
512 | - wp_enqueue_style( 'wpinv_meta_box_style' ); |
|
513 | - |
|
514 | - $version = filemtime( WPINV_PLUGIN_DIR . 'assets/css/admin.css' ); |
|
515 | - wp_register_style( 'wpinv_admin_style', WPINV_PLUGIN_URL . 'assets/css/admin.css', array(), $version ); |
|
516 | - wp_enqueue_style( 'wpinv_admin_style' ); |
|
517 | - |
|
518 | - $enqueue = ( $post_type == 'wpi_discount' || $post_type == 'wpi_invoice' && ( $pagenow == 'post-new.php' || $pagenow == 'post.php' ) ); |
|
519 | - if ( $page == 'wpinv-subscriptions' ) { |
|
520 | - wp_enqueue_script( 'jquery-ui-datepicker' ); |
|
521 | - wp_deregister_style( 'yoast-seo-select2' ); |
|
522 | - wp_deregister_style( 'yoast-seo-monorepo' ); |
|
523 | - } |
|
524 | - |
|
525 | - if ( $enqueue_datepicker = apply_filters( 'wpinv_admin_enqueue_jquery_ui_datepicker', $enqueue ) ) { |
|
526 | - wp_enqueue_script( 'jquery-ui-datepicker' ); |
|
527 | - } |
|
528 | - |
|
529 | - wp_enqueue_style( 'wp-color-picker' ); |
|
530 | - wp_enqueue_script( 'wp-color-picker' ); |
|
531 | - |
|
532 | - wp_register_script( 'jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array( 'jquery' ), '2.70', true ); |
|
533 | - |
|
534 | - if (($post_type == 'wpi_invoice' || $post_type == 'wpi_quote') && ($pagenow == 'post-new.php' || $pagenow == 'post.php')) { |
|
535 | - $autofill_api = wpinv_get_option('address_autofill_api'); |
|
536 | - $autofill_active = wpinv_get_option('address_autofill_active'); |
|
537 | - if (isset($autofill_active) && 1 == $autofill_active && !empty($autofill_api)) { |
|
538 | - wp_enqueue_script('google-maps-api', 'https://maps.googleapis.com/maps/api/js?key=' . $autofill_api . '&libraries=places', array('jquery'), '', false); |
|
539 | - wp_enqueue_script('google-maps-init', WPINV_PLUGIN_URL . 'assets/js/gaaf.js', array('jquery'), '', true); |
|
540 | - } |
|
541 | - } |
|
542 | - |
|
543 | - wp_enqueue_style( "select2", WPINV_PLUGIN_URL . 'assets/css/select2/select2.css', array(), WPINV_VERSION, 'all' ); |
|
544 | - wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full' . $suffix . '.js', array( 'jquery' ), WPINV_VERSION ); |
|
545 | - |
|
546 | - $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin.js' ); |
|
547 | - wp_register_script( 'wpinv-admin-script', WPINV_PLUGIN_URL . 'assets/js/admin.js', array( 'jquery', 'jquery-blockui','jquery-ui-tooltip' ), $version ); |
|
548 | - wp_enqueue_script( 'wpinv-admin-script' ); |
|
549 | - |
|
550 | - $localize = array(); |
|
551 | - $localize['ajax_url'] = admin_url( 'admin-ajax.php' ); |
|
552 | - $localize['post_ID'] = isset( $post->ID ) ? $post->ID : ''; |
|
553 | - $localize['wpinv_nonce'] = wp_create_nonce( 'wpinv-nonce' ); |
|
554 | - $localize['add_invoice_note_nonce'] = wp_create_nonce( 'add-invoice-note' ); |
|
555 | - $localize['delete_invoice_note_nonce'] = wp_create_nonce( 'delete-invoice-note' ); |
|
556 | - $localize['invoice_item_nonce'] = wp_create_nonce( 'invoice-item' ); |
|
557 | - $localize['billing_details_nonce'] = wp_create_nonce( 'get-billing-details' ); |
|
558 | - $localize['tax'] = wpinv_tax_amount(); |
|
559 | - $localize['discount'] = wpinv_discount_amount(); |
|
560 | - $localize['currency_symbol'] = wpinv_currency_symbol(); |
|
561 | - $localize['currency_pos'] = wpinv_currency_position(); |
|
562 | - $localize['thousand_sep'] = wpinv_thousands_separator(); |
|
563 | - $localize['decimal_sep'] = wpinv_decimal_separator(); |
|
564 | - $localize['decimals'] = wpinv_decimals(); |
|
565 | - $localize['save_invoice'] = __( 'Save Invoice', 'invoicing' ); |
|
566 | - $localize['status_publish'] = wpinv_status_nicename( 'publish' ); |
|
567 | - $localize['status_pending'] = wpinv_status_nicename( 'wpi-pending' ); |
|
568 | - $localize['delete_tax_rate'] = __( 'Are you sure you wish to delete this tax rate?', 'invoicing' ); |
|
569 | - $localize['OneItemMin'] = __( 'Invoice must contain at least one item', 'invoicing' ); |
|
570 | - $localize['DeleteInvoiceItem'] = __( 'Are you sure you wish to delete this item?', 'invoicing' ); |
|
571 | - $localize['FillBillingDetails'] = __( 'Fill the user\'s billing information? This will remove any currently entered billing information', 'invoicing' ); |
|
572 | - $localize['confirmCalcTotals'] = __( 'Recalculate totals? This will recalculate totals based on the user billing country. If no billing country is set it will use the base country.', 'invoicing' ); |
|
573 | - $localize['AreYouSure'] = __( 'Are you sure?', 'invoicing' ); |
|
574 | - $localize['emptyInvoice'] = __( 'Add at least one item to save invoice!', 'invoicing' ); |
|
575 | - $localize['errDeleteItem'] = __( 'This item is in use! Before delete this item, you need to delete all the invoice(s) using this item.', 'invoicing' ); |
|
576 | - $localize['delete_subscription'] = __( 'Are you sure you want to delete this subscription?', 'invoicing' ); |
|
577 | - $localize['action_edit'] = __( 'Edit', 'invoicing' ); |
|
578 | - $localize['action_cancel'] = __( 'Cancel', 'invoicing' ); |
|
579 | - $localize['item_description'] = __( 'Item Description', 'invoicing' ); |
|
580 | - $localize['discount_description'] = __( 'Discount Description', 'invoicing' ); |
|
581 | - $localize['invoice_description'] = __( 'Invoice Description', 'invoicing' ); |
|
582 | - $localize['searching'] = __( 'Searching', 'invoicing' ); |
|
583 | - |
|
584 | - $localize = apply_filters( 'wpinv_admin_js_localize', $localize ); |
|
585 | - |
|
586 | - wp_localize_script( 'wpinv-admin-script', 'WPInv_Admin', $localize ); |
|
587 | - |
|
588 | - // Load payment form scripts on our admin pages only. |
|
589 | - if ( ( $hook == 'post-new.php' || $hook == 'post.php' ) && 'wpi_payment_form' === $post->post_type ) { |
|
590 | - |
|
591 | - wp_enqueue_script( 'vue', WPINV_PLUGIN_URL . 'assets/js/vue/vue.js', array(), WPINV_VERSION ); |
|
592 | - wp_enqueue_script( 'sortable', WPINV_PLUGIN_URL . 'assets/js/sortable.min.js', array(), WPINV_VERSION ); |
|
593 | - wp_enqueue_script( 'vue_draggable', WPINV_PLUGIN_URL . 'assets/js/vue/vuedraggable.min.js', array( 'sortable', 'vue' ), WPINV_VERSION ); |
|
594 | - |
|
595 | - $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin-payment-forms.js' ); |
|
596 | - wp_register_script( 'wpinv-admin-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/admin-payment-forms.js', array( 'wpinv-admin-script', 'vue_draggable' ), $version ); |
|
597 | - |
|
598 | - wp_localize_script( 'wpinv-admin-payment-form-script', 'wpinvPaymentFormAdmin', array( |
|
599 | - 'elements' => $this->form_elements->get_elements(), |
|
600 | - 'form_elements' => $this->form_elements->get_form_elements( $post->ID ), |
|
601 | - 'all_items' => $this->form_elements->get_published_items(), |
|
602 | - 'currency' => wpinv_currency_symbol(), |
|
603 | - 'position' => wpinv_currency_position(), |
|
604 | - 'decimals' => (int) wpinv_decimals(), |
|
605 | - 'thousands_sep' => wpinv_thousands_separator(), |
|
606 | - 'decimals_sep' => wpinv_decimal_separator(), |
|
607 | - 'form_items' => $this->form_elements->get_form_items( $post->ID ), |
|
608 | - 'is_default' => $post->ID == $this->default_payment_form, |
|
609 | - ) ); |
|
610 | - |
|
611 | - wp_enqueue_script( 'wpinv-admin-payment-form-script' ); |
|
612 | - } |
|
613 | - |
|
614 | - if ( $page == 'wpinv-subscriptions' ) { |
|
615 | - wp_register_script( 'wpinv-sub-admin-script', WPINV_PLUGIN_URL . 'assets/js/subscriptions.js', array( 'wpinv-admin-script' ), WPINV_VERSION ); |
|
616 | - wp_enqueue_script( 'wpinv-sub-admin-script' ); |
|
617 | - } |
|
618 | - |
|
619 | - if ( $page == 'wpinv-reports' ) { |
|
620 | - wp_enqueue_script( 'jquery-flot', WPINV_PLUGIN_URL . 'assets/js/jquery.flot.min.js', array( 'jquery' ), '0.7' ); |
|
621 | - } |
|
622 | - |
|
623 | - } |
|
624 | - |
|
625 | - public function admin_body_class( $classes ) { |
|
626 | - global $pagenow, $post, $current_screen; |
|
627 | - |
|
628 | - if ( !empty( $current_screen->post_type ) && ( $current_screen->post_type == 'wpi_invoice' || $current_screen->post_type == 'wpi_payment_form' || $current_screen->post_type == 'wpi_quote' ) ) { |
|
629 | - $classes .= ' wpinv-cpt'; |
|
630 | - } |
|
631 | - |
|
632 | - $page = isset( $_GET['page'] ) ? strtolower( $_GET['page'] ) : false; |
|
633 | - |
|
634 | - $add_class = $page && $pagenow == 'admin.php' && strpos( $page, 'wpinv-' ) === 0 ? true : false; |
|
635 | - if ( $add_class ) { |
|
636 | - $classes .= ' wpi-' . wpinv_sanitize_key( $page ); |
|
637 | - } |
|
638 | - |
|
639 | - $settings_class = array(); |
|
640 | - if ( $page == 'wpinv-settings' ) { |
|
641 | - if ( !empty( $_REQUEST['tab'] ) ) { |
|
642 | - $settings_class[] = sanitize_text_field( $_REQUEST['tab'] ); |
|
643 | - } |
|
644 | - |
|
645 | - if ( !empty( $_REQUEST['section'] ) ) { |
|
646 | - $settings_class[] = sanitize_text_field( $_REQUEST['section'] ); |
|
647 | - } |
|
648 | - |
|
649 | - $settings_class[] = isset( $_REQUEST['wpi_sub'] ) && $_REQUEST['wpi_sub'] !== '' ? sanitize_text_field( $_REQUEST['wpi_sub'] ) : 'main'; |
|
650 | - } |
|
651 | - |
|
652 | - if ( !empty( $settings_class ) ) { |
|
653 | - $classes .= ' wpi-' . wpinv_sanitize_key( implode( $settings_class, '-' ) ); |
|
654 | - } |
|
655 | - |
|
656 | - $post_type = wpinv_admin_post_type(); |
|
657 | - |
|
658 | - if ( $post_type == 'wpi_invoice' || $post_type == 'wpi_quote' || $add_class !== false ) { |
|
659 | - return $classes .= ' wpinv'; |
|
660 | - } |
|
661 | - |
|
662 | - if ( $pagenow == 'post.php' && $post_type == 'wpi_item' && !empty( $post ) && !wpinv_item_is_editable( $post ) ) { |
|
663 | - $classes .= ' wpi-editable-n'; |
|
664 | - } |
|
665 | - |
|
666 | - return $classes; |
|
667 | - } |
|
668 | - |
|
669 | - public function admin_print_scripts_edit_php() { |
|
670 | - |
|
671 | - } |
|
672 | - |
|
673 | - public function wpinv_actions() { |
|
674 | - if ( isset( $_REQUEST['wpi_action'] ) ) { |
|
675 | - do_action( 'wpinv_' . wpinv_sanitize_key( $_REQUEST['wpi_action'] ), $_REQUEST ); |
|
676 | - } |
|
677 | - } |
|
678 | - |
|
679 | - public function pre_get_posts( $wp_query ) { |
|
680 | - if ( !empty( $wp_query->query_vars['post_type'] ) && $wp_query->query_vars['post_type'] == 'wpi_invoice' && is_user_logged_in() && is_single() && $wp_query->is_main_query() ) { |
|
681 | - $wp_query->query_vars['post_status'] = array_keys( wpinv_get_invoice_statuses() ); |
|
682 | - } |
|
683 | - |
|
684 | - return $wp_query; |
|
685 | - } |
|
686 | - |
|
687 | - public function bp_invoicing_init() { |
|
688 | - require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-bp-core.php' ); |
|
689 | - } |
|
690 | - |
|
691 | - /** |
|
692 | - * Register widgets |
|
693 | - * |
|
694 | - */ |
|
695 | - public function register_widgets() { |
|
696 | - $widgets = apply_filters( |
|
697 | - 'getpaid_widget_classes', |
|
698 | - array( |
|
699 | - 'WPInv_Checkout_Widget', |
|
700 | - 'WPInv_History_Widget', |
|
701 | - 'WPInv_Receipt_Widget', |
|
702 | - 'WPInv_Subscriptions_Widget', |
|
703 | - 'WPInv_Buy_Item_Widget', |
|
704 | - 'WPInv_Messages_Widget', |
|
705 | - 'WPInv_GetPaid_Widget' |
|
706 | - ) |
|
707 | - ); |
|
708 | - |
|
709 | - foreach ( $widgets as $widget ) { |
|
710 | - register_widget( $widget ); |
|
711 | - } |
|
17 | + /** |
|
18 | + * GetPaid version. |
|
19 | + * |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + public $version; |
|
23 | + |
|
24 | + /** |
|
25 | + * Session instance. |
|
26 | + * |
|
27 | + * @var WPInv_Session_Handler |
|
28 | + */ |
|
29 | + public $session; |
|
30 | + |
|
31 | + /** |
|
32 | + * Notes instance. |
|
33 | + * |
|
34 | + * @var WPInv_Notes |
|
35 | + */ |
|
36 | + public $notes; |
|
37 | + |
|
38 | + /** |
|
39 | + * Reports instance. |
|
40 | + * |
|
41 | + * @var WPInv_Reports |
|
42 | + */ |
|
43 | + public $reports; |
|
44 | + |
|
45 | + /** |
|
46 | + * API instance. |
|
47 | + * |
|
48 | + * @var WPInv_API |
|
49 | + */ |
|
50 | + public $api; |
|
51 | + |
|
52 | + /** |
|
53 | + * Form elements instance. |
|
54 | + * |
|
55 | + * @var WPInv_Payment_Form_Elements |
|
56 | + */ |
|
57 | + public $form_elements; |
|
58 | + |
|
59 | + /** |
|
60 | + * Tax instance. |
|
61 | + * |
|
62 | + * @var WPInv_EUVat |
|
63 | + */ |
|
64 | + public $tax; |
|
65 | + |
|
66 | + /** |
|
67 | + * @param array An array of payment gateways. |
|
68 | + */ |
|
69 | + public $gateways; |
|
70 | + |
|
71 | + /** |
|
72 | + * Post types instance. |
|
73 | + * |
|
74 | + * @var GetPaid_Post_Types |
|
75 | + */ |
|
76 | + public $post_types; |
|
77 | + |
|
78 | + /** |
|
79 | + * Class constructor. |
|
80 | + */ |
|
81 | + public function __construct() { |
|
82 | + $this->define_constants(); |
|
83 | + $this->includes(); |
|
84 | + $this->init_hooks(); |
|
85 | + $this->set_properties(); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Define class properties. |
|
90 | + */ |
|
91 | + public function set_properties() { |
|
92 | + |
|
93 | + $this->session = new WPInv_Session_Handler(); |
|
94 | + $GLOBALS['wpi_session'] = $this->session; // Backwards compatibility. |
|
95 | + $this->notes = new WPInv_Notes(); |
|
96 | + $this->reports = new WPInv_Reports(); |
|
97 | + $this->api = new WPInv_API(); |
|
98 | + $this->form_elements = new WPInv_Payment_Form_Elements(); |
|
99 | + $this->tax = new WPInv_EUVat(); |
|
100 | + $this->post_types = new GetPaid_Post_Types(); |
|
101 | + $this->tax->init(); |
|
102 | + $GLOBALS['wpinv_euvat'] = $this->tax; // Backwards compatibility. |
|
103 | + |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Define plugin constants. |
|
108 | + */ |
|
109 | + public function define_constants() { |
|
110 | + define( 'WPINV_PLUGIN_DIR', plugin_dir_path( WPINV_PLUGIN_FILE ) ); |
|
111 | + define( 'WPINV_PLUGIN_URL', plugin_dir_url( WPINV_PLUGIN_FILE ) ); |
|
112 | + $this->version = WPINV_VERSION; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Hook into actions and filters. |
|
117 | + * |
|
118 | + * @since 1.0.19 |
|
119 | + */ |
|
120 | + protected function init_hooks() { |
|
121 | + /* Internationalize the text strings used. */ |
|
122 | + add_action( 'plugins_loaded', array( &$this, 'plugins_loaded' ) ); |
|
123 | + |
|
124 | + /* Perform actions on admin initialization. */ |
|
125 | + add_action( 'admin_init', array( &$this, 'admin_init') ); |
|
126 | + |
|
127 | + // Init the plugin after WordPress inits. |
|
128 | + add_action( 'init', array( $this, 'init' ), 1 ); |
|
129 | + add_action( 'getpaid_init', array( $this, 'maybe_process_ipn' ), 5 ); |
|
130 | + add_action( 'init', array( &$this, 'wpinv_actions' ) ); |
|
131 | + |
|
132 | + if ( class_exists( 'BuddyPress' ) ) { |
|
133 | + add_action( 'bp_include', array( &$this, 'bp_invoicing_init' ) ); |
|
134 | + } |
|
135 | + |
|
136 | + add_action( 'wp_enqueue_scripts', array( &$this, 'enqueue_scripts' ) ); |
|
137 | + add_action( 'wp_footer', array( &$this, 'wp_footer' ) ); |
|
138 | + add_action( 'widgets_init', array( &$this, 'register_widgets' ) ); |
|
139 | + add_filter( 'wpseo_exclude_from_sitemap_by_post_ids', array( $this, 'wpseo_exclude_from_sitemap_by_post_ids' ) ); |
|
140 | + |
|
141 | + if ( is_admin() ) { |
|
142 | + add_action( 'admin_enqueue_scripts', array( &$this, 'admin_enqueue_scripts' ) ); |
|
143 | + add_filter( 'admin_body_class', array( &$this, 'admin_body_class' ) ); |
|
144 | + add_action( 'admin_init', array( &$this, 'init_ayecode_connect_helper' ) ); |
|
145 | + |
|
146 | + } else { |
|
147 | + add_filter( 'pre_get_posts', array( &$this, 'pre_get_posts' ) ); |
|
148 | + } |
|
149 | + |
|
150 | + add_action( 'admin_init', array( &$this, 'activation_redirect') ); |
|
151 | + |
|
152 | + // Fires after registering core hooks. |
|
153 | + do_action_ref_array( 'wpinv_actions', array( &$this ) ); |
|
154 | + |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Maybe show the AyeCode Connect Notice. |
|
159 | + */ |
|
160 | + public function init_ayecode_connect_helper(){ |
|
161 | + // AyeCode Connect notice |
|
162 | + if ( is_admin() ){ |
|
163 | + // set the strings so they can be translated |
|
164 | + $strings = array( |
|
165 | + 'connect_title' => __("WP Invoicing - an AyeCode product!","invoicing"), |
|
166 | + 'connect_external' => __( "Please confirm you wish to connect your site?","invoicing" ), |
|
167 | + 'connect' => sprintf( __( "<strong>Have a license?</strong> Forget about entering license keys or downloading zip files, connect your site for instant access. %slearn more%s","invoicing" ),"<a href='https://ayecode.io/introducing-ayecode-connect/' target='_blank'>","</a>" ), |
|
168 | + 'connect_button' => __("Connect Site","invoicing"), |
|
169 | + 'connecting_button' => __("Connecting...","invoicing"), |
|
170 | + 'error_localhost' => __( "This service will only work with a live domain, not a localhost.","invoicing" ), |
|
171 | + 'error' => __( "Something went wrong, please refresh and try again.","invoicing" ), |
|
172 | + ); |
|
173 | + new AyeCode_Connect_Helper($strings,array('wpi-addons')); |
|
174 | + } |
|
175 | + } |
|
176 | + |
|
177 | + public function plugins_loaded() { |
|
178 | + /* Internationalize the text strings used. */ |
|
179 | + $this->load_textdomain(); |
|
180 | + |
|
181 | + do_action( 'wpinv_loaded' ); |
|
182 | + |
|
183 | + // Fix oxygen page builder conflict |
|
184 | + if ( function_exists( 'ct_css_output' ) ) { |
|
185 | + wpinv_oxygen_fix_conflict(); |
|
186 | + } |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Load the translation of the plugin. |
|
191 | + * |
|
192 | + * @since 1.0 |
|
193 | + */ |
|
194 | + public function load_textdomain( $locale = NULL ) { |
|
195 | + if ( empty( $locale ) ) { |
|
196 | + $locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale(); |
|
197 | + } |
|
198 | + |
|
199 | + $locale = apply_filters( 'plugin_locale', $locale, 'invoicing' ); |
|
200 | + |
|
201 | + unload_textdomain( 'invoicing' ); |
|
202 | + load_textdomain( 'invoicing', WP_LANG_DIR . '/invoicing/invoicing-' . $locale . '.mo' ); |
|
203 | + load_plugin_textdomain( 'invoicing', false, WPINV_PLUGIN_DIR . 'languages' ); |
|
204 | + |
|
205 | + /** |
|
206 | + * Define language constants. |
|
207 | + */ |
|
208 | + require_once( WPINV_PLUGIN_DIR . 'language.php' ); |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Include required core files used in admin and on the frontend. |
|
213 | + */ |
|
214 | + public function includes() { |
|
215 | + |
|
216 | + // Start with the settings. |
|
217 | + require_once( WPINV_PLUGIN_DIR . 'includes/admin/register-settings.php' ); |
|
218 | + |
|
219 | + // Packages/libraries. |
|
220 | + require_once( WPINV_PLUGIN_DIR . 'vendor/autoload.php' ); |
|
221 | + require_once( WPINV_PLUGIN_DIR . 'vendor/ayecode/wp-ayecode-ui/ayecode-ui-loader.php' ); |
|
222 | + require_once( WPINV_PLUGIN_DIR . 'includes/libraries/action-scheduler/action-scheduler.php' ); |
|
223 | + |
|
224 | + // Load functions. |
|
225 | + require_once( WPINV_PLUGIN_DIR . 'includes/deprecated-functions.php' ); |
|
226 | + require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-email-functions.php' ); |
|
227 | + require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-general-functions.php' ); |
|
228 | + require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-helper-functions.php' ); |
|
229 | + require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-tax-functions.php' ); |
|
230 | + require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-template-functions.php' ); |
|
231 | + require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-address-functions.php' ); |
|
232 | + require_once( WPINV_PLUGIN_DIR . 'includes/invoice-functions.php' ); |
|
233 | + require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-item-functions.php' ); |
|
234 | + require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-discount-functions.php' ); |
|
235 | + require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-gateway-functions.php' ); |
|
236 | + require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-payment-functions.php' ); |
|
237 | + require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-user-functions.php' ); |
|
238 | + require_once( WPINV_PLUGIN_DIR . 'includes/error-functions.php' ); |
|
239 | + |
|
240 | + // Register autoloader. |
|
241 | + try { |
|
242 | + spl_autoload_register( array( $this, 'autoload' ), true ); |
|
243 | + } catch ( Exception $e ) { |
|
244 | + wpinv_error_log( $e->getMessage(), '', __FILE__, 149, true ); |
|
245 | + } |
|
246 | + |
|
247 | + require_once( WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-session.php' ); |
|
248 | + require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-session-handler.php' ); |
|
249 | + require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-ajax.php' ); |
|
250 | + require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-api.php' ); |
|
251 | + require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-reports.php' ); |
|
252 | + require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-cache-helper.php' ); |
|
253 | + require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-db.php' ); |
|
254 | + require_once( WPINV_PLUGIN_DIR . 'includes/admin/subscriptions.php' ); |
|
255 | + require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-subscriptions-db.php' ); |
|
256 | + require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-subscriptions.php' ); |
|
257 | + require_once( WPINV_PLUGIN_DIR . 'includes/wpinv-subscription.php' ); |
|
258 | + require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-subscriptions-list-table.php' ); |
|
259 | + require_once( WPINV_PLUGIN_DIR . 'includes/abstracts/abstract-wpinv-privacy.php' ); |
|
260 | + require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-privacy.php' ); |
|
261 | + require_once( WPINV_PLUGIN_DIR . 'includes/libraries/class-ayecode-addons.php' ); |
|
262 | + require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-addons.php' ); |
|
263 | + require_once( WPINV_PLUGIN_DIR . 'widgets/checkout.php' ); |
|
264 | + require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-history.php' ); |
|
265 | + require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-receipt.php' ); |
|
266 | + require_once( WPINV_PLUGIN_DIR . 'widgets/invoice-messages.php' ); |
|
267 | + require_once( WPINV_PLUGIN_DIR . 'widgets/subscriptions.php' ); |
|
268 | + require_once( WPINV_PLUGIN_DIR . 'widgets/buy-item.php' ); |
|
269 | + require_once( WPINV_PLUGIN_DIR . 'widgets/getpaid.php' ); |
|
270 | + require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-payment-form-elements.php' ); |
|
271 | + |
|
272 | + /** |
|
273 | + * Load the tax class. |
|
274 | + */ |
|
275 | + if ( ! class_exists( 'WPInv_EUVat' ) ) { |
|
276 | + require_once( WPINV_PLUGIN_DIR . 'includes/libraries/wpinv-euvat/class-wpinv-euvat.php' ); |
|
277 | + } |
|
278 | + |
|
279 | + $gateways = array_keys( wpinv_get_enabled_payment_gateways() ); |
|
280 | + if ( !empty( $gateways ) ) { |
|
281 | + foreach ( $gateways as $gateway ) { |
|
282 | + if ( $gateway == 'manual' ) { |
|
283 | + continue; |
|
284 | + } |
|
285 | + |
|
286 | + $gateway_file = WPINV_PLUGIN_DIR . 'includes/gateways/' . $gateway . '.php'; |
|
287 | + |
|
288 | + if ( file_exists( $gateway_file ) ) { |
|
289 | + require_once( $gateway_file ); |
|
290 | + } |
|
291 | + } |
|
292 | + } |
|
293 | + |
|
294 | + if ( is_admin() || ( defined( 'WP_CLI' ) && WP_CLI ) ) { |
|
295 | + GetPaid_Post_Types_Admin::init(); |
|
296 | + |
|
297 | + require_once( WPINV_PLUGIN_DIR . 'includes/admin/wpinv-upgrade-functions.php' ); |
|
298 | + require_once( WPINV_PLUGIN_DIR . 'includes/admin/wpinv-admin-functions.php' ); |
|
299 | + //require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-recurring-admin.php' ); |
|
300 | + require_once( WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-payment-form.php' ); |
|
301 | + require_once( WPINV_PLUGIN_DIR . 'includes/admin/meta-boxes/class-mb-invoice-notes.php' ); |
|
302 | + require_once( WPINV_PLUGIN_DIR . 'includes/admin/admin-pages.php' ); |
|
303 | + require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-admin-menus.php' ); |
|
304 | + require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-users.php' ); |
|
305 | + require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-getpaid-admin-profile.php' ); |
|
306 | + //require_once( WPINV_PLUGIN_DIR . 'includes/admin/subscriptions.php' ); |
|
307 | + // load the user class only on the users.php page |
|
308 | + global $pagenow; |
|
309 | + if($pagenow=='users.php'){ |
|
310 | + new WPInv_Admin_Users(); |
|
311 | + } |
|
312 | + } |
|
313 | + |
|
314 | + // Register cli commands |
|
315 | + if ( defined( 'WP_CLI' ) && WP_CLI ) { |
|
316 | + require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-cli.php' ); |
|
317 | + WP_CLI::add_command( 'invoicing', 'WPInv_CLI' ); |
|
318 | + } |
|
319 | + |
|
320 | + // include css inliner |
|
321 | + if ( ! class_exists( 'Emogrifier' ) && class_exists( 'DOMDocument' ) ) { |
|
322 | + include_once( WPINV_PLUGIN_DIR . 'includes/libraries/class-emogrifier.php' ); |
|
323 | + } |
|
324 | + |
|
325 | + require_once( WPINV_PLUGIN_DIR . 'includes/admin/install.php' ); |
|
326 | + } |
|
327 | + |
|
328 | + /** |
|
329 | + * Class autoloader |
|
330 | + * |
|
331 | + * @param string $class_name The name of the class to load. |
|
332 | + * @access public |
|
333 | + * @since 1.0.19 |
|
334 | + * @return void |
|
335 | + */ |
|
336 | + public function autoload( $class_name ) { |
|
337 | + |
|
338 | + // Normalize the class name... |
|
339 | + $class_name = strtolower( $class_name ); |
|
340 | + |
|
341 | + // ... and make sure it is our class. |
|
342 | + if ( false === strpos( $class_name, 'getpaid_' ) && false === strpos( $class_name, 'wpinv_' ) ) { |
|
343 | + return; |
|
344 | + } |
|
345 | + |
|
346 | + // Next, prepare the file name from the class. |
|
347 | + $file_name = 'class-' . str_replace( '_', '-', $class_name ) . '.php'; |
|
348 | + |
|
349 | + // Base path of the classes. |
|
350 | + $plugin_path = untrailingslashit( WPINV_PLUGIN_DIR ); |
|
351 | + |
|
352 | + // And an array of possible locations in order of importance. |
|
353 | + $locations = array( |
|
354 | + "$plugin_path/includes", |
|
355 | + "$plugin_path/includes/data-stores", |
|
356 | + "$plugin_path/includes/gateways", |
|
357 | + "$plugin_path/includes/api", |
|
358 | + "$plugin_path/includes/admin", |
|
359 | + "$plugin_path/includes/admin/meta-boxes", |
|
360 | + ); |
|
361 | + |
|
362 | + foreach ( apply_filters( 'getpaid_autoload_locations', $locations ) as $location ) { |
|
363 | + |
|
364 | + if ( file_exists( trailingslashit( $location ) . $file_name ) ) { |
|
365 | + include trailingslashit( $location ) . $file_name; |
|
366 | + break; |
|
367 | + } |
|
368 | + |
|
369 | + } |
|
370 | + |
|
371 | + } |
|
372 | + |
|
373 | + /** |
|
374 | + * Inits hooks etc. |
|
375 | + */ |
|
376 | + public function init() { |
|
377 | + |
|
378 | + // Fires before getpaid inits. |
|
379 | + do_action( 'before_getpaid_init', $this ); |
|
380 | + |
|
381 | + // Load default gateways. |
|
382 | + $gateways = apply_filters( |
|
383 | + 'getpaid_default_gateways', |
|
384 | + array( |
|
385 | + 'manual' => 'GetPaid_Manual_Gateway', |
|
386 | + 'paypal' => 'GetPaid_Paypal_Gateway', |
|
387 | + 'worldpay' => 'GetPaid_Worldpay_Gateway', |
|
388 | + 'bank_transfer' => 'GetPaid_Bank_Transfer_Gateway', |
|
389 | + 'authorizenet' => 'GetPaid_Authorize_Net_Gateway', |
|
390 | + ) |
|
391 | + ); |
|
392 | + |
|
393 | + foreach ( $gateways as $id => $class ) { |
|
394 | + $this->gateways[ $id ] = new $class(); |
|
395 | + } |
|
396 | + |
|
397 | + // Fires after getpaid inits. |
|
398 | + do_action( 'getpaid_init', $this ); |
|
399 | + |
|
400 | + } |
|
401 | + |
|
402 | + /** |
|
403 | + * Checks if this is an IPN request and processes it. |
|
404 | + */ |
|
405 | + public function maybe_process_ipn() { |
|
406 | + |
|
407 | + // Ensure that this is an IPN request. |
|
408 | + if ( empty( $_GET['wpi-listener'] ) || 'IPN' !== $_GET['wpi-listener'] || empty( $_GET['wpi-gateway'] ) ) { |
|
409 | + return; |
|
410 | + } |
|
411 | + |
|
412 | + $gateway = wpinv_clean( $_GET['wpi-gateway'] ); |
|
413 | + |
|
414 | + do_action( 'wpinv_verify_payment_ipn', $gateway ); |
|
415 | + do_action( "wpinv_verify_{$gateway}_ipn" ); |
|
416 | + exit; |
|
417 | + |
|
418 | + } |
|
419 | + |
|
420 | + public function admin_init() { |
|
421 | + $this->default_payment_form = wpinv_get_default_payment_form(); |
|
422 | + add_action( 'admin_print_scripts-edit.php', array( &$this, 'admin_print_scripts_edit_php' ) ); |
|
423 | + } |
|
424 | + |
|
425 | + public function activation_redirect() { |
|
426 | + // Bail if no activation redirect |
|
427 | + if ( !get_transient( '_wpinv_activation_redirect' ) ) { |
|
428 | + return; |
|
429 | + } |
|
430 | + |
|
431 | + // Delete the redirect transient |
|
432 | + delete_transient( '_wpinv_activation_redirect' ); |
|
433 | + |
|
434 | + // Bail if activating from network, or bulk |
|
435 | + if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) { |
|
436 | + return; |
|
437 | + } |
|
438 | + |
|
439 | + wp_safe_redirect( admin_url( 'admin.php?page=wpinv-settings&tab=general' ) ); |
|
440 | + exit; |
|
441 | + } |
|
442 | + |
|
443 | + public function enqueue_scripts() { |
|
444 | + $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
445 | + |
|
446 | + $version = filemtime( WPINV_PLUGIN_DIR . 'assets/css/invoice-front.css' ); |
|
447 | + wp_register_style( 'wpinv_front_style', WPINV_PLUGIN_URL . 'assets/css/invoice-front.css', array(), $version ); |
|
448 | + wp_enqueue_style( 'wpinv_front_style' ); |
|
449 | + |
|
450 | + // Register scripts |
|
451 | + wp_register_script( 'jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array( 'jquery' ), '2.70', true ); |
|
452 | + wp_register_script( 'wpinv-front-script', WPINV_PLUGIN_URL . 'assets/js/invoice-front.js', array( 'jquery' ), filemtime( WPINV_PLUGIN_DIR . 'assets/js/invoice-front.js' ) ); |
|
453 | + |
|
454 | + $localize = array(); |
|
455 | + $localize['ajax_url'] = admin_url( 'admin-ajax.php' ); |
|
456 | + $localize['nonce'] = wp_create_nonce( 'wpinv-nonce' ); |
|
457 | + $localize['currency_symbol'] = wpinv_currency_symbol(); |
|
458 | + $localize['currency_pos'] = wpinv_currency_position(); |
|
459 | + $localize['thousand_sep'] = wpinv_thousands_separator(); |
|
460 | + $localize['decimal_sep'] = wpinv_decimal_separator(); |
|
461 | + $localize['decimals'] = wpinv_decimals(); |
|
462 | + $localize['txtComplete'] = __( 'Continue', 'invoicing' ); |
|
463 | + $localize['UseTaxes'] = wpinv_use_taxes(); |
|
464 | + $localize['checkoutNonce'] = wp_create_nonce( 'wpinv_checkout_nonce' ); |
|
465 | + $localize['formNonce'] = wp_create_nonce( 'getpaid_form_nonce' ); |
|
466 | + $localize['connectionError'] = __( 'Could not establish a connection to the server.', 'invoicing' ); |
|
467 | + |
|
468 | + $localize = apply_filters( 'wpinv_front_js_localize', $localize ); |
|
469 | + |
|
470 | + wp_enqueue_script( 'jquery-blockui' ); |
|
471 | + $autofill_api = wpinv_get_option('address_autofill_api'); |
|
472 | + $autofill_active = wpinv_get_option('address_autofill_active'); |
|
473 | + if ( isset( $autofill_active ) && 1 == $autofill_active && !empty( $autofill_api ) && wpinv_is_checkout() ) { |
|
474 | + if ( wp_script_is( 'google-maps-api', 'enqueued' ) ) { |
|
475 | + wp_dequeue_script( 'google-maps-api' ); |
|
476 | + } |
|
477 | + wp_enqueue_script( 'google-maps-api', 'https://maps.googleapis.com/maps/api/js?key=' . $autofill_api . '&libraries=places', array( 'jquery' ), '', false ); |
|
478 | + wp_enqueue_script( 'google-maps-init', WPINV_PLUGIN_URL . 'assets/js/gaaf.js', array( 'jquery', 'google-maps-api' ), '', true ); |
|
479 | + } |
|
480 | + |
|
481 | + wp_enqueue_style( "select2", WPINV_PLUGIN_URL . 'assets/css/select2/select2.css', array(), WPINV_VERSION, 'all' ); |
|
482 | + wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full' . $suffix . '.js', array( 'jquery' ), WPINV_VERSION ); |
|
483 | + |
|
484 | + wp_enqueue_script( 'wpinv-front-script' ); |
|
485 | + wp_localize_script( 'wpinv-front-script', 'WPInv', $localize ); |
|
486 | + |
|
487 | + $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/payment-forms.js' ); |
|
488 | + wp_enqueue_script( 'wpinv-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/payment-forms.js', array( 'wpinv-front-script', 'wp-hooks' ), $version, true ); |
|
489 | + } |
|
490 | + |
|
491 | + public function admin_enqueue_scripts( $hook ) { |
|
492 | + global $post, $pagenow; |
|
493 | + |
|
494 | + $post_type = wpinv_admin_post_type(); |
|
495 | + $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
496 | + $page = isset( $_GET['page'] ) ? strtolower( $_GET['page'] ) : ''; |
|
497 | + |
|
498 | + $jquery_ui_css = false; |
|
499 | + if ( ( $post_type == 'wpi_invoice' || $post_type == 'wpi_quote' || $post_type == 'wpi_discount' ) && ( $pagenow == 'post-new.php' || $pagenow == 'post.php' ) ) { |
|
500 | + $jquery_ui_css = true; |
|
501 | + } else if ( $page == 'wpinv-settings' || $page == 'wpinv-reports' ) { |
|
502 | + $jquery_ui_css = true; |
|
503 | + } |
|
504 | + if ( $jquery_ui_css ) { |
|
505 | + wp_register_style( 'jquery-ui-css', WPINV_PLUGIN_URL . 'assets/css/jquery-ui' . $suffix . '.css', array(), '1.8.16' ); |
|
506 | + wp_enqueue_style( 'jquery-ui-css' ); |
|
507 | + wp_deregister_style( 'yoast-seo-select2' ); |
|
508 | + wp_deregister_style( 'yoast-seo-monorepo' ); |
|
509 | + } |
|
510 | + |
|
511 | + wp_register_style( 'wpinv_meta_box_style', WPINV_PLUGIN_URL . 'assets/css/meta-box.css', array(), WPINV_VERSION ); |
|
512 | + wp_enqueue_style( 'wpinv_meta_box_style' ); |
|
513 | + |
|
514 | + $version = filemtime( WPINV_PLUGIN_DIR . 'assets/css/admin.css' ); |
|
515 | + wp_register_style( 'wpinv_admin_style', WPINV_PLUGIN_URL . 'assets/css/admin.css', array(), $version ); |
|
516 | + wp_enqueue_style( 'wpinv_admin_style' ); |
|
517 | + |
|
518 | + $enqueue = ( $post_type == 'wpi_discount' || $post_type == 'wpi_invoice' && ( $pagenow == 'post-new.php' || $pagenow == 'post.php' ) ); |
|
519 | + if ( $page == 'wpinv-subscriptions' ) { |
|
520 | + wp_enqueue_script( 'jquery-ui-datepicker' ); |
|
521 | + wp_deregister_style( 'yoast-seo-select2' ); |
|
522 | + wp_deregister_style( 'yoast-seo-monorepo' ); |
|
523 | + } |
|
524 | + |
|
525 | + if ( $enqueue_datepicker = apply_filters( 'wpinv_admin_enqueue_jquery_ui_datepicker', $enqueue ) ) { |
|
526 | + wp_enqueue_script( 'jquery-ui-datepicker' ); |
|
527 | + } |
|
528 | + |
|
529 | + wp_enqueue_style( 'wp-color-picker' ); |
|
530 | + wp_enqueue_script( 'wp-color-picker' ); |
|
531 | + |
|
532 | + wp_register_script( 'jquery-blockui', WPINV_PLUGIN_URL . 'assets/js/jquery.blockUI.min.js', array( 'jquery' ), '2.70', true ); |
|
533 | + |
|
534 | + if (($post_type == 'wpi_invoice' || $post_type == 'wpi_quote') && ($pagenow == 'post-new.php' || $pagenow == 'post.php')) { |
|
535 | + $autofill_api = wpinv_get_option('address_autofill_api'); |
|
536 | + $autofill_active = wpinv_get_option('address_autofill_active'); |
|
537 | + if (isset($autofill_active) && 1 == $autofill_active && !empty($autofill_api)) { |
|
538 | + wp_enqueue_script('google-maps-api', 'https://maps.googleapis.com/maps/api/js?key=' . $autofill_api . '&libraries=places', array('jquery'), '', false); |
|
539 | + wp_enqueue_script('google-maps-init', WPINV_PLUGIN_URL . 'assets/js/gaaf.js', array('jquery'), '', true); |
|
540 | + } |
|
541 | + } |
|
542 | + |
|
543 | + wp_enqueue_style( "select2", WPINV_PLUGIN_URL . 'assets/css/select2/select2.css', array(), WPINV_VERSION, 'all' ); |
|
544 | + wp_enqueue_script('select2', WPINV_PLUGIN_URL . 'assets/js/select2/select2.full' . $suffix . '.js', array( 'jquery' ), WPINV_VERSION ); |
|
545 | + |
|
546 | + $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin.js' ); |
|
547 | + wp_register_script( 'wpinv-admin-script', WPINV_PLUGIN_URL . 'assets/js/admin.js', array( 'jquery', 'jquery-blockui','jquery-ui-tooltip' ), $version ); |
|
548 | + wp_enqueue_script( 'wpinv-admin-script' ); |
|
549 | + |
|
550 | + $localize = array(); |
|
551 | + $localize['ajax_url'] = admin_url( 'admin-ajax.php' ); |
|
552 | + $localize['post_ID'] = isset( $post->ID ) ? $post->ID : ''; |
|
553 | + $localize['wpinv_nonce'] = wp_create_nonce( 'wpinv-nonce' ); |
|
554 | + $localize['add_invoice_note_nonce'] = wp_create_nonce( 'add-invoice-note' ); |
|
555 | + $localize['delete_invoice_note_nonce'] = wp_create_nonce( 'delete-invoice-note' ); |
|
556 | + $localize['invoice_item_nonce'] = wp_create_nonce( 'invoice-item' ); |
|
557 | + $localize['billing_details_nonce'] = wp_create_nonce( 'get-billing-details' ); |
|
558 | + $localize['tax'] = wpinv_tax_amount(); |
|
559 | + $localize['discount'] = wpinv_discount_amount(); |
|
560 | + $localize['currency_symbol'] = wpinv_currency_symbol(); |
|
561 | + $localize['currency_pos'] = wpinv_currency_position(); |
|
562 | + $localize['thousand_sep'] = wpinv_thousands_separator(); |
|
563 | + $localize['decimal_sep'] = wpinv_decimal_separator(); |
|
564 | + $localize['decimals'] = wpinv_decimals(); |
|
565 | + $localize['save_invoice'] = __( 'Save Invoice', 'invoicing' ); |
|
566 | + $localize['status_publish'] = wpinv_status_nicename( 'publish' ); |
|
567 | + $localize['status_pending'] = wpinv_status_nicename( 'wpi-pending' ); |
|
568 | + $localize['delete_tax_rate'] = __( 'Are you sure you wish to delete this tax rate?', 'invoicing' ); |
|
569 | + $localize['OneItemMin'] = __( 'Invoice must contain at least one item', 'invoicing' ); |
|
570 | + $localize['DeleteInvoiceItem'] = __( 'Are you sure you wish to delete this item?', 'invoicing' ); |
|
571 | + $localize['FillBillingDetails'] = __( 'Fill the user\'s billing information? This will remove any currently entered billing information', 'invoicing' ); |
|
572 | + $localize['confirmCalcTotals'] = __( 'Recalculate totals? This will recalculate totals based on the user billing country. If no billing country is set it will use the base country.', 'invoicing' ); |
|
573 | + $localize['AreYouSure'] = __( 'Are you sure?', 'invoicing' ); |
|
574 | + $localize['emptyInvoice'] = __( 'Add at least one item to save invoice!', 'invoicing' ); |
|
575 | + $localize['errDeleteItem'] = __( 'This item is in use! Before delete this item, you need to delete all the invoice(s) using this item.', 'invoicing' ); |
|
576 | + $localize['delete_subscription'] = __( 'Are you sure you want to delete this subscription?', 'invoicing' ); |
|
577 | + $localize['action_edit'] = __( 'Edit', 'invoicing' ); |
|
578 | + $localize['action_cancel'] = __( 'Cancel', 'invoicing' ); |
|
579 | + $localize['item_description'] = __( 'Item Description', 'invoicing' ); |
|
580 | + $localize['discount_description'] = __( 'Discount Description', 'invoicing' ); |
|
581 | + $localize['invoice_description'] = __( 'Invoice Description', 'invoicing' ); |
|
582 | + $localize['searching'] = __( 'Searching', 'invoicing' ); |
|
583 | + |
|
584 | + $localize = apply_filters( 'wpinv_admin_js_localize', $localize ); |
|
585 | + |
|
586 | + wp_localize_script( 'wpinv-admin-script', 'WPInv_Admin', $localize ); |
|
587 | + |
|
588 | + // Load payment form scripts on our admin pages only. |
|
589 | + if ( ( $hook == 'post-new.php' || $hook == 'post.php' ) && 'wpi_payment_form' === $post->post_type ) { |
|
590 | + |
|
591 | + wp_enqueue_script( 'vue', WPINV_PLUGIN_URL . 'assets/js/vue/vue.js', array(), WPINV_VERSION ); |
|
592 | + wp_enqueue_script( 'sortable', WPINV_PLUGIN_URL . 'assets/js/sortable.min.js', array(), WPINV_VERSION ); |
|
593 | + wp_enqueue_script( 'vue_draggable', WPINV_PLUGIN_URL . 'assets/js/vue/vuedraggable.min.js', array( 'sortable', 'vue' ), WPINV_VERSION ); |
|
594 | + |
|
595 | + $version = filemtime( WPINV_PLUGIN_DIR . 'assets/js/admin-payment-forms.js' ); |
|
596 | + wp_register_script( 'wpinv-admin-payment-form-script', WPINV_PLUGIN_URL . 'assets/js/admin-payment-forms.js', array( 'wpinv-admin-script', 'vue_draggable' ), $version ); |
|
597 | + |
|
598 | + wp_localize_script( 'wpinv-admin-payment-form-script', 'wpinvPaymentFormAdmin', array( |
|
599 | + 'elements' => $this->form_elements->get_elements(), |
|
600 | + 'form_elements' => $this->form_elements->get_form_elements( $post->ID ), |
|
601 | + 'all_items' => $this->form_elements->get_published_items(), |
|
602 | + 'currency' => wpinv_currency_symbol(), |
|
603 | + 'position' => wpinv_currency_position(), |
|
604 | + 'decimals' => (int) wpinv_decimals(), |
|
605 | + 'thousands_sep' => wpinv_thousands_separator(), |
|
606 | + 'decimals_sep' => wpinv_decimal_separator(), |
|
607 | + 'form_items' => $this->form_elements->get_form_items( $post->ID ), |
|
608 | + 'is_default' => $post->ID == $this->default_payment_form, |
|
609 | + ) ); |
|
610 | + |
|
611 | + wp_enqueue_script( 'wpinv-admin-payment-form-script' ); |
|
612 | + } |
|
613 | + |
|
614 | + if ( $page == 'wpinv-subscriptions' ) { |
|
615 | + wp_register_script( 'wpinv-sub-admin-script', WPINV_PLUGIN_URL . 'assets/js/subscriptions.js', array( 'wpinv-admin-script' ), WPINV_VERSION ); |
|
616 | + wp_enqueue_script( 'wpinv-sub-admin-script' ); |
|
617 | + } |
|
618 | + |
|
619 | + if ( $page == 'wpinv-reports' ) { |
|
620 | + wp_enqueue_script( 'jquery-flot', WPINV_PLUGIN_URL . 'assets/js/jquery.flot.min.js', array( 'jquery' ), '0.7' ); |
|
621 | + } |
|
622 | + |
|
623 | + } |
|
624 | + |
|
625 | + public function admin_body_class( $classes ) { |
|
626 | + global $pagenow, $post, $current_screen; |
|
627 | + |
|
628 | + if ( !empty( $current_screen->post_type ) && ( $current_screen->post_type == 'wpi_invoice' || $current_screen->post_type == 'wpi_payment_form' || $current_screen->post_type == 'wpi_quote' ) ) { |
|
629 | + $classes .= ' wpinv-cpt'; |
|
630 | + } |
|
631 | + |
|
632 | + $page = isset( $_GET['page'] ) ? strtolower( $_GET['page'] ) : false; |
|
633 | + |
|
634 | + $add_class = $page && $pagenow == 'admin.php' && strpos( $page, 'wpinv-' ) === 0 ? true : false; |
|
635 | + if ( $add_class ) { |
|
636 | + $classes .= ' wpi-' . wpinv_sanitize_key( $page ); |
|
637 | + } |
|
638 | + |
|
639 | + $settings_class = array(); |
|
640 | + if ( $page == 'wpinv-settings' ) { |
|
641 | + if ( !empty( $_REQUEST['tab'] ) ) { |
|
642 | + $settings_class[] = sanitize_text_field( $_REQUEST['tab'] ); |
|
643 | + } |
|
644 | + |
|
645 | + if ( !empty( $_REQUEST['section'] ) ) { |
|
646 | + $settings_class[] = sanitize_text_field( $_REQUEST['section'] ); |
|
647 | + } |
|
648 | + |
|
649 | + $settings_class[] = isset( $_REQUEST['wpi_sub'] ) && $_REQUEST['wpi_sub'] !== '' ? sanitize_text_field( $_REQUEST['wpi_sub'] ) : 'main'; |
|
650 | + } |
|
651 | + |
|
652 | + if ( !empty( $settings_class ) ) { |
|
653 | + $classes .= ' wpi-' . wpinv_sanitize_key( implode( $settings_class, '-' ) ); |
|
654 | + } |
|
655 | + |
|
656 | + $post_type = wpinv_admin_post_type(); |
|
657 | + |
|
658 | + if ( $post_type == 'wpi_invoice' || $post_type == 'wpi_quote' || $add_class !== false ) { |
|
659 | + return $classes .= ' wpinv'; |
|
660 | + } |
|
661 | + |
|
662 | + if ( $pagenow == 'post.php' && $post_type == 'wpi_item' && !empty( $post ) && !wpinv_item_is_editable( $post ) ) { |
|
663 | + $classes .= ' wpi-editable-n'; |
|
664 | + } |
|
665 | + |
|
666 | + return $classes; |
|
667 | + } |
|
668 | + |
|
669 | + public function admin_print_scripts_edit_php() { |
|
670 | + |
|
671 | + } |
|
672 | + |
|
673 | + public function wpinv_actions() { |
|
674 | + if ( isset( $_REQUEST['wpi_action'] ) ) { |
|
675 | + do_action( 'wpinv_' . wpinv_sanitize_key( $_REQUEST['wpi_action'] ), $_REQUEST ); |
|
676 | + } |
|
677 | + } |
|
678 | + |
|
679 | + public function pre_get_posts( $wp_query ) { |
|
680 | + if ( !empty( $wp_query->query_vars['post_type'] ) && $wp_query->query_vars['post_type'] == 'wpi_invoice' && is_user_logged_in() && is_single() && $wp_query->is_main_query() ) { |
|
681 | + $wp_query->query_vars['post_status'] = array_keys( wpinv_get_invoice_statuses() ); |
|
682 | + } |
|
683 | + |
|
684 | + return $wp_query; |
|
685 | + } |
|
686 | + |
|
687 | + public function bp_invoicing_init() { |
|
688 | + require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-bp-core.php' ); |
|
689 | + } |
|
690 | + |
|
691 | + /** |
|
692 | + * Register widgets |
|
693 | + * |
|
694 | + */ |
|
695 | + public function register_widgets() { |
|
696 | + $widgets = apply_filters( |
|
697 | + 'getpaid_widget_classes', |
|
698 | + array( |
|
699 | + 'WPInv_Checkout_Widget', |
|
700 | + 'WPInv_History_Widget', |
|
701 | + 'WPInv_Receipt_Widget', |
|
702 | + 'WPInv_Subscriptions_Widget', |
|
703 | + 'WPInv_Buy_Item_Widget', |
|
704 | + 'WPInv_Messages_Widget', |
|
705 | + 'WPInv_GetPaid_Widget' |
|
706 | + ) |
|
707 | + ); |
|
708 | + |
|
709 | + foreach ( $widgets as $widget ) { |
|
710 | + register_widget( $widget ); |
|
711 | + } |
|
712 | 712 | |
713 | - } |
|
713 | + } |
|
714 | 714 | |
715 | - /** |
|
716 | - * Remove our pages from yoast sitemaps. |
|
717 | - * |
|
718 | - * @since 1.0.19 |
|
719 | - * @param int[] $excluded_posts_ids |
|
720 | - */ |
|
721 | - public function wpseo_exclude_from_sitemap_by_post_ids( $excluded_posts_ids ){ |
|
715 | + /** |
|
716 | + * Remove our pages from yoast sitemaps. |
|
717 | + * |
|
718 | + * @since 1.0.19 |
|
719 | + * @param int[] $excluded_posts_ids |
|
720 | + */ |
|
721 | + public function wpseo_exclude_from_sitemap_by_post_ids( $excluded_posts_ids ){ |
|
722 | 722 | |
723 | - // Ensure that we have an array. |
|
724 | - if ( ! is_array( $excluded_posts_ids ) ) { |
|
725 | - $excluded_posts_ids = array(); |
|
726 | - } |
|
723 | + // Ensure that we have an array. |
|
724 | + if ( ! is_array( $excluded_posts_ids ) ) { |
|
725 | + $excluded_posts_ids = array(); |
|
726 | + } |
|
727 | 727 | |
728 | - // Prepare our pages. |
|
729 | - $our_pages = array(); |
|
728 | + // Prepare our pages. |
|
729 | + $our_pages = array(); |
|
730 | 730 | |
731 | - // Checkout page. |
|
732 | - $our_pages[] = wpinv_get_option( 'checkout_page', false ); |
|
731 | + // Checkout page. |
|
732 | + $our_pages[] = wpinv_get_option( 'checkout_page', false ); |
|
733 | 733 | |
734 | - // Success page. |
|
735 | - $our_pages[] = wpinv_get_option( 'success_page', false ); |
|
734 | + // Success page. |
|
735 | + $our_pages[] = wpinv_get_option( 'success_page', false ); |
|
736 | 736 | |
737 | - // Failure page. |
|
738 | - $our_pages[] = wpinv_get_option( 'failure_page', false ); |
|
737 | + // Failure page. |
|
738 | + $our_pages[] = wpinv_get_option( 'failure_page', false ); |
|
739 | 739 | |
740 | - // History page. |
|
741 | - $our_pages[] = wpinv_get_option( 'invoice_history_page', false ); |
|
740 | + // History page. |
|
741 | + $our_pages[] = wpinv_get_option( 'invoice_history_page', false ); |
|
742 | 742 | |
743 | - // Subscriptions page. |
|
744 | - $our_pages[] = wpinv_get_option( 'invoice_subscription_page', false ); |
|
743 | + // Subscriptions page. |
|
744 | + $our_pages[] = wpinv_get_option( 'invoice_subscription_page', false ); |
|
745 | 745 | |
746 | - $our_pages = array_map( 'intval', array_filter( $our_pages ) ); |
|
746 | + $our_pages = array_map( 'intval', array_filter( $our_pages ) ); |
|
747 | 747 | |
748 | - $excluded_posts_ids = $excluded_posts_ids + $our_pages; |
|
749 | - return array_unique( $excluded_posts_ids ); |
|
748 | + $excluded_posts_ids = $excluded_posts_ids + $our_pages; |
|
749 | + return array_unique( $excluded_posts_ids ); |
|
750 | 750 | |
751 | - } |
|
751 | + } |
|
752 | 752 | |
753 | - public function wp_footer() { |
|
754 | - echo ' |
|
753 | + public function wp_footer() { |
|
754 | + echo ' |
|
755 | 755 | <div class="bsui"> |
756 | 756 | <div id="getpaid-payment-modal" class="modal" tabindex="-1" role="dialog"> |
757 | 757 | <div class="modal-dialog modal-dialog-centered modal-lg" role="checkout" style="max-width: 650px;"> |
@@ -762,6 +762,6 @@ discard block |
||
762 | 762 | </div> |
763 | 763 | </div> |
764 | 764 | '; |
765 | - } |
|
765 | + } |
|
766 | 766 | |
767 | 767 | } |