Passed
Push — master ( 862b0e...047a35 )
by Stiofan
17:04
created

wpinv_add_aui_screens()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 6
rs 10
1
<?php
2
// MUST have WordPress.
3
if ( !defined( 'WPINC' ) ) {
4
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
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 $discount->get_code();
16
        break;
17
        case 'amount' :
18
            echo $discount->get_formatted_amount();
19
        break;
20
        case 'usage' :
21
            echo $discount->get_usage();
22
        break;
23
        case 'start_date' :
24
            echo getpaid_format_date_value( $discount->get_start_date() );
25
        break;
26
        case 'expiry_date' :
27
            echo 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' ) ) ) {
49
50
        $url    = esc_url(
51
                    wp_nonce_url(
52
                        add_query_arg(
53
                            array(
54
                                'getpaid-admin-action' => 'deactivate_discount',
55
                                'discount'             => $discount->ID,
56
                            )
57
                        ),
58
                        'getpaid-nonce',
59
                        'getpaid-nonce'
60
                    )
61
                );
62
		$anchor = __( 'Deactivate', 'invoicing' );
63
		$title  = esc_attr__( 'Are you sure you want to deactivate this discount?', 'invoicing' );
64
        $row_actions['deactivate'] = "<a href='$url' onclick='return confirm(\"$title\")'>$anchor</a>";
65
66
    } else if( in_array( strtolower( $discount->post_status ),  array( 'pending', 'draft' ) ) ) {
67
68
        $url    = esc_url(
69
            wp_nonce_url(
70
                add_query_arg(
71
                    array(
72
                        'getpaid-admin-action' => 'activate_discount',
73
                        'discount'             => $discount->ID,
74
                    )
75
                ),
76
                'getpaid-nonce',
77
                'getpaid-nonce'
78
            )
79
        );
80
		$anchor = __( 'Activate', 'invoicing' );
81
		$title  = esc_attr__( 'Are you sure you want to activate this discount?', 'invoicing' );
82
        $row_actions['activate'] = "<a href='$url' onclick='return confirm(\"$title\")'>$anchor</a>";
83
84
    }
85
86
    $url    = esc_url(
87
        wp_nonce_url(
88
            add_query_arg(
89
                array(
90
                    'getpaid-admin-action' => 'delete_discount',
91
                    'discount'             => $discount->ID,
92
                )
93
            ),
94
            'getpaid-nonce',
95
            'getpaid-nonce'
96
        )
97
    );
98
	$anchor = __( 'Delete', 'invoicing' );
99
	$title  = esc_attr__( 'Are you sure you want to delete this discount?', 'invoicing' );
100
    $row_actions['delete'] = "<a href='$url' onclick='return confirm(\"$title\")'>$anchor</a>";
101
102
    $row_actions = apply_filters( 'wpinv_discount_row_actions', $row_actions, $discount );
103
104
    return $row_actions;
105
}
106
107
function wpinv_restrict_manage_posts() {
108
    global $typenow;
109
110
    if( 'wpi_discount' == $typenow ) {
111
        wpinv_discount_filters();
112
    }
113
}
114
add_action( 'restrict_manage_posts', 'wpinv_restrict_manage_posts', 10 );
115
116
function wpinv_discount_filters() {
117
118
    ?>
119
    <select name="discount_type" id="dropdown_wpinv_discount_type">
120
        <option value=""><?php _e( 'Show all types', 'invoicing' ); ?></option>
121
        <?php
122
            $types = wpinv_get_discount_types();
123
124
            foreach ( $types as $name => $type ) {
125
                echo '<option value="' . esc_attr( $name ) . '"';
126
127
                if ( isset( $_GET['discount_type'] ) )
128
                    selected( $name, $_GET['discount_type'] );
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
153
    } else if ( 'wpi_discount' == $typenow ) {
154
        $meta_query = !empty( $vars['meta_query'] ) ? $vars['meta_query'] : array();
155
        // Filter vat rule type
156
        if ( isset( $_GET['discount_type'] ) && $_GET['discount_type'] !== '' ) {
157
            $meta_query[] = array(
158
                    'key'   => '_wpi_discount_type',
159
                    'value' => sanitize_text_field( $_GET['discount_type'] ),
160
                    'compare' => '='
161
                );
162
        }
163
164
        if ( !empty( $meta_query ) ) {
165
            $vars['meta_query'] = $meta_query;
166
        }
167
    }
168
169
    return $vars;
170
}
171
add_filter( 'request', 'wpinv_request' );
172
173
/**
174
 * Create a page and store the ID in an option.
175
 *
176
 * @param mixed $slug Slug for the new page
177
 * @param string $option Option name to store the page's ID
178
 * @param string $page_title (default: '') Title for the new page
179
 * @param string $page_content (default: '') Content for the new page
180
 * @param int $post_parent (default: 0) Parent for the new page
181
 * @return int page ID
182
 */
183
function wpinv_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0 ) {
184
    global $wpdb;
185
186
    $option_value = wpinv_get_option( $option );
187
188
    if ( ! empty( $option_value ) && ( $page_object = get_post( $option_value ) ) ) {
189
        if ( 'page' === $page_object->post_type && ! in_array( $page_object->post_status, array( 'pending', 'trash', 'future', 'auto-draft' ) ) ) {
190
            // Valid page is already in place
191
            return $page_object->ID;
192
        }
193
    }
194
195
    if(!empty($post_parent)){
196
        $page = get_page_by_path($post_parent);
197
        if ($page) {
198
            $post_parent = $page->ID;
199
        } else {
200
            $post_parent = '';
201
        }
202
    }
203
204
    if ( strlen( $page_content ) > 0 ) {
205
        // Search for an existing page with the specified page content (typically a shortcode)
206
        $valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) );
207
    } else {
208
        // Search for an existing page with the specified page slug
209
        $valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' )  AND post_name = %s LIMIT 1;", $slug ) );
210
    }
211
212
    $valid_page_found = apply_filters( 'wpinv_create_page_id', $valid_page_found, $slug, $page_content );
213
214
    if ( $valid_page_found ) {
215
        if ( $option ) {
216
            wpinv_update_option( $option, $valid_page_found );
217
        }
218
        return $valid_page_found;
219
    }
220
221
    // Search for a matching valid trashed page
222
    if ( strlen( $page_content ) > 0 ) {
223
        // Search for an existing page with the specified page content (typically a shortcode)
224
        $trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) );
225
    } else {
226
        // Search for an existing page with the specified page slug
227
        $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 ) );
228
    }
229
230
    if ( $trashed_page_found ) {
231
        $page_id   = $trashed_page_found;
232
        $page_data = array(
233
            'ID'             => $page_id,
234
            'post_status'    => 'publish',
235
            'post_parent'    => $post_parent,
236
        );
237
        wp_update_post( $page_data );
238
    } else {
239
        $page_data = array(
240
            'post_status'    => 'publish',
241
            'post_type'      => 'page',
242
            'post_author'    => 1,
243
            'post_name'      => $slug,
244
            'post_title'     => $page_title,
245
            'post_content'   => $page_content,
246
            'post_parent'    => $post_parent,
247
            'comment_status' => 'closed',
248
        );
249
        $page_id = wp_insert_post( $page_data );
250
    }
251
252
    if ( $option ) {
253
        wpinv_update_option( $option, (int) $page_id );
254
    }
255
256
    return $page_id;
257
}
258
259
/**
260
 * Tell AyeCode UI to load on certain admin pages.
261
 *
262
 * @param $screen_ids
263
 *
264
 * @return array
265
 */
266
function wpinv_add_aui_screens($screen_ids){
267
268
    // load on these pages if set
269
    $screen_ids = array_merge( $screen_ids, wpinv_get_screen_ids() );
270
271
    return $screen_ids;
272
}
273
add_filter('aui_screen_ids','wpinv_add_aui_screens');