1
|
|
|
<?php |
2
|
|
|
// MUST have WordPress. |
3
|
|
|
if ( ! defined( 'WPINC' ) ) { |
4
|
|
|
exit; |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
add_action( 'manage_wpi_discount_posts_custom_column', 'wpinv_discount_custom_column' ); |
8
|
|
|
function wpinv_discount_custom_column( $column ) { |
9
|
|
|
global $post; |
10
|
|
|
|
11
|
|
|
$discount = new WPInv_Discount( $post ); |
12
|
|
|
|
13
|
|
|
switch ( $column ) { |
14
|
|
|
case 'code': |
15
|
|
|
echo esc_html( $discount->get_code() ); |
16
|
|
|
break; |
17
|
|
|
case 'amount': |
18
|
|
|
echo wp_kses_post( $discount->get_formatted_amount() ); |
19
|
|
|
break; |
20
|
|
|
case 'usage': |
21
|
|
|
echo wp_kses_post( $discount->get_usage() ); |
22
|
|
|
break; |
23
|
|
|
case 'start_date': |
24
|
|
|
echo wp_kses_post( getpaid_format_date_value( $discount->get_start_date() ) ); |
25
|
|
|
break; |
26
|
|
|
case 'expiry_date': |
27
|
|
|
echo wp_kses_post( getpaid_format_date_value( $discount->get_expiration_date(), __( 'Never', 'invoicing' ) ) ); |
28
|
|
|
break; |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
add_filter( 'post_row_actions', 'wpinv_post_row_actions', 90, 2 ); |
33
|
|
|
function wpinv_post_row_actions( $actions, $post ) { |
34
|
|
|
$post_type = ! empty( $post->post_type ) ? $post->post_type : ''; |
35
|
|
|
|
36
|
|
|
if ( $post_type == 'wpi_discount' ) { |
37
|
|
|
$actions = wpinv_discount_row_actions( $post, $actions ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return $actions; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
function wpinv_discount_row_actions( $discount, $row_actions ) { |
44
|
|
|
$row_actions = array(); |
45
|
|
|
$edit_link = get_edit_post_link( $discount->ID ); |
46
|
|
|
$row_actions['edit'] = '<a href="' . esc_url( $edit_link ) . '">' . __( 'Edit', 'invoicing' ) . '</a>'; |
47
|
|
|
|
48
|
|
|
if ( in_array( strtolower( $discount->post_status ), array( 'publish' ) ) && wpinv_current_user_can( 'deactivate_discount', array( 'discount' => (int) $discount->ID ) ) ) { |
49
|
|
|
|
50
|
|
|
$url = wp_nonce_url( |
51
|
|
|
add_query_arg( |
52
|
|
|
array( |
53
|
|
|
'getpaid-admin-action' => 'deactivate_discount', |
54
|
|
|
'discount' => $discount->ID, |
55
|
|
|
) |
56
|
|
|
), |
57
|
|
|
'getpaid-nonce', |
58
|
|
|
'getpaid-nonce' |
59
|
|
|
); |
60
|
|
|
$anchor = __( 'Deactivate', 'invoicing' ); |
61
|
|
|
$title = esc_attr__( 'Are you sure you want to deactivate this discount?', 'invoicing' ); |
62
|
|
|
$row_actions['deactivate'] = "<a href='$url' onclick='return confirm(\"$title\")'>$anchor</a>"; |
63
|
|
|
|
64
|
|
|
} elseif ( in_array( strtolower( $discount->post_status ), array( 'pending', 'draft' ) ) && wpinv_current_user_can( 'activate_discount', array( 'discount' => (int) $discount->ID ) ) ) { |
65
|
|
|
|
66
|
|
|
$url = wp_nonce_url( |
67
|
|
|
add_query_arg( |
68
|
|
|
array( |
69
|
|
|
'getpaid-admin-action' => 'activate_discount', |
70
|
|
|
'discount' => $discount->ID, |
71
|
|
|
) |
72
|
|
|
), |
73
|
|
|
'getpaid-nonce', |
74
|
|
|
'getpaid-nonce' |
75
|
|
|
); |
76
|
|
|
$anchor = __( 'Activate', 'invoicing' ); |
77
|
|
|
$title = esc_attr__( 'Are you sure you want to activate this discount?', 'invoicing' ); |
78
|
|
|
$row_actions['activate'] = "<a href='$url' onclick='return confirm(\"$title\")'>$anchor</a>"; |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ( wpinv_current_user_can( 'delete_discount', array( 'discount' => (int) $discount->ID ) ) ) { |
83
|
|
|
$url = esc_url( |
84
|
|
|
wp_nonce_url( |
85
|
|
|
add_query_arg( |
86
|
|
|
array( |
87
|
|
|
'getpaid-admin-action' => 'delete_discount', |
88
|
|
|
'discount' => $discount->ID, |
89
|
|
|
) |
90
|
|
|
), |
91
|
|
|
'getpaid-nonce', |
92
|
|
|
'getpaid-nonce' |
93
|
|
|
) |
94
|
|
|
); |
95
|
|
|
|
96
|
|
|
$anchor = __( 'Delete', 'invoicing' ); |
97
|
|
|
$title = esc_attr__( 'Are you sure you want to delete this discount?', 'invoicing' ); |
98
|
|
|
$row_actions['delete'] = "<a href='$url' onclick='return confirm(\"$title\")'>$anchor</a>"; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$row_actions = apply_filters( 'wpinv_discount_row_actions', $row_actions, $discount ); |
102
|
|
|
|
103
|
|
|
return $row_actions; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
function wpinv_restrict_manage_posts() { |
107
|
|
|
global $typenow; |
108
|
|
|
|
109
|
|
|
if ( 'wpi_discount' == $typenow ) { |
110
|
|
|
wpinv_discount_filters(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
add_action( 'restrict_manage_posts', 'wpinv_restrict_manage_posts', 10 ); |
114
|
|
|
|
115
|
|
|
function wpinv_discount_filters() { |
116
|
|
|
|
117
|
|
|
?> |
118
|
|
|
<select name="discount_type" id="dropdown_wpinv_discount_type"> |
119
|
|
|
<option value=""><?php esc_html_e( 'Show all types', 'invoicing' ); ?></option> |
120
|
|
|
<?php |
121
|
|
|
$types = wpinv_get_discount_types(); |
122
|
|
|
|
123
|
|
|
foreach ( $types as $name => $type ) { |
124
|
|
|
echo '<option value="' . esc_attr( $name ) . '"'; |
125
|
|
|
|
126
|
|
|
if ( isset( $_GET['discount_type'] ) ) { |
127
|
|
|
selected( $name, sanitize_text_field( $_GET['discount_type'] ) ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
echo '>' . esc_html__( $type, 'invoicing' ) . '</option>'; |
131
|
|
|
} |
132
|
|
|
?> |
133
|
|
|
</select> |
134
|
|
|
<?php |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
function wpinv_request( $vars ) { |
138
|
|
|
global $typenow, $wp_post_statuses; |
139
|
|
|
|
140
|
|
|
if ( getpaid_is_invoice_post_type( $typenow ) ) { |
141
|
|
|
if ( ! isset( $vars['post_status'] ) ) { |
142
|
|
|
$post_statuses = wpinv_get_invoice_statuses( false, false, $typenow ); |
143
|
|
|
|
144
|
|
|
foreach ( $post_statuses as $status => $value ) { |
145
|
|
|
if ( isset( $wp_post_statuses[ $status ] ) && false === $wp_post_statuses[ $status ]->show_in_admin_all_list ) { |
146
|
|
|
unset( $post_statuses[ $status ] ); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$vars['post_status'] = array_keys( $post_statuses ); |
151
|
|
|
} |
152
|
|
|
} elseif ( 'wpi_discount' == $typenow ) { |
153
|
|
|
$meta_query = ! empty( $vars['meta_query'] ) ? $vars['meta_query'] : array(); |
154
|
|
|
// Filter vat rule type |
155
|
|
|
if ( isset( $_GET['discount_type'] ) && $_GET['discount_type'] !== '' ) { |
156
|
|
|
$meta_query[] = array( |
157
|
|
|
'key' => '_wpi_discount_type', |
158
|
|
|
'value' => sanitize_key( urldecode( $_GET['discount_type'] ) ), |
159
|
|
|
'compare' => '=', |
160
|
|
|
); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if ( ! empty( $meta_query ) ) { |
164
|
|
|
$vars['meta_query'] = $meta_query; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $vars; |
169
|
|
|
} |
170
|
|
|
add_filter( 'request', 'wpinv_request' ); |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Create a page and store the ID in an option. |
174
|
|
|
* |
175
|
|
|
* @param mixed $slug Slug for the new page |
176
|
|
|
* @param string $option Option name to store the page's ID |
177
|
|
|
* @param string $page_title (default: '') Title for the new page |
178
|
|
|
* @param string $page_content (default: '') Content for the new page |
179
|
|
|
* @param int $post_parent (default: 0) Parent for the new page |
180
|
|
|
* @return int page ID |
181
|
|
|
*/ |
182
|
|
|
function wpinv_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0 ) { |
183
|
|
|
global $wpdb; |
184
|
|
|
|
185
|
|
|
$option_value = wpinv_get_option( $option ); |
186
|
|
|
|
187
|
|
|
if ( ! empty( $option_value ) && ( $page_object = get_post( $option_value ) ) ) { |
188
|
|
|
if ( 'page' === $page_object->post_type && ! in_array( $page_object->post_status, array( 'pending', 'trash', 'future', 'auto-draft' ) ) ) { |
189
|
|
|
// Valid page is already in place |
190
|
|
|
return $page_object->ID; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if ( ! empty( $post_parent ) ) { |
195
|
|
|
$page = get_page_by_path( $post_parent ); |
196
|
|
|
if ( $page ) { |
197
|
|
|
$post_parent = $page->ID; |
198
|
|
|
} else { |
199
|
|
|
$post_parent = ''; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
// Search for an existing page with the specified page slug |
204
|
|
|
$valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'future', 'auto-draft' ) AND post_name = %s LIMIT 1;", $slug ) ); |
205
|
|
|
|
206
|
|
|
$valid_page_found = apply_filters( 'wpinv_create_page_id', $valid_page_found, $slug, $page_content ); |
207
|
|
|
|
208
|
|
|
if ( $valid_page_found ) { |
209
|
|
|
if ( $option ) { |
210
|
|
|
wpinv_update_option( $option, $valid_page_found ); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
return $valid_page_found; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
// Search for an existing page with the specified page slug |
217
|
|
|
$trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_name = %s LIMIT 1;", $slug ) ); |
218
|
|
|
|
219
|
|
|
if ( $trashed_page_found ) { |
220
|
|
|
$page_id = $trashed_page_found; |
221
|
|
|
|
222
|
|
|
$page_data = array( |
223
|
|
|
'ID' => $page_id, |
224
|
|
|
'post_status' => 'publish', |
225
|
|
|
'post_parent' => $post_parent, |
226
|
|
|
); |
227
|
|
|
|
228
|
|
|
wp_update_post( $page_data ); |
229
|
|
|
} else { |
230
|
|
|
$page_data = array( |
231
|
|
|
'post_status' => 'publish', |
232
|
|
|
'post_type' => 'page', |
233
|
|
|
'post_author' => 1, |
234
|
|
|
'post_name' => $slug, |
235
|
|
|
'post_title' => $page_title, |
236
|
|
|
'post_content' => $page_content, |
237
|
|
|
'post_parent' => $post_parent, |
238
|
|
|
'comment_status' => 'closed', |
239
|
|
|
); |
240
|
|
|
|
241
|
|
|
$page_id = wp_insert_post( $page_data ); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
if ( $option ) { |
245
|
|
|
wpinv_update_option( $option, (int) $page_id ); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return $page_id; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Tell AyeCode UI to load on certain admin pages. |
253
|
|
|
* |
254
|
|
|
* @param $screen_ids |
255
|
|
|
* |
256
|
|
|
* @return array |
257
|
|
|
*/ |
258
|
|
|
function wpinv_add_aui_screens( $screen_ids ) { |
259
|
|
|
|
260
|
|
|
// load on these pages if set |
261
|
|
|
$screen_ids = array_merge( $screen_ids, wpinv_get_screen_ids() ); |
262
|
|
|
|
263
|
|
|
return $screen_ids; |
264
|
|
|
} |
265
|
|
|
add_filter( 'aui_screen_ids', 'wpinv_add_aui_screens' ); |
266
|
|
|
|