Passed
Push — master ( 919089...f59267 )
by Stiofan
41s queued 11s
created

WPInv_Admin_Menus::nav_menu_links()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
1
<?php
2
/**
3
 * Setup menus in WP admin.
4
 */
5
6
defined( 'ABSPATH' ) || exit;
7
8
/**
9
 * WC_Admin_Menus Class.
10
 */
11
class WPInv_Admin_Menus {
12
    /**
13
     * Hook in tabs.
14
     */
15
    public function __construct() {
16
        add_action( 'admin_menu', array( $this, 'admin_menu' ), 10 );
17
        add_action( 'admin_menu', array( $this, 'remove_admin_submenus' ), 10 );
18
        add_action( 'admin_head-nav-menus.php', array( $this, 'add_nav_menu_meta_boxes' ) );
19
    }
20
21
    public function admin_menu() {
22
        global $menu;
23
24
        if ( !(current_user_can( 'manage_invoicing' ) || current_user_can( 'manage_options' )) ) {
25
            return;
26
        }
27
28
        $capability = apply_filters( 'invoicing_capability', 'manage_invoicing' );
29
30
        if ( current_user_can( 'manage_options' ) ) {
31
            $menu[] = array( '', 'read', 'separator-wpinv', '', 'wp-menu-separator wpinv' );
32
        }
33
34
        $wpi_invoice = get_post_type_object( 'wpi_invoice' );
35
36
        add_menu_page( __( 'Invoicing', 'invoicing' ), __( 'Invoicing', 'invoicing' ), $capability, 'wpinv', null, $wpi_invoice->menu_icon, '54.123460' );
37
38
        add_submenu_page( 'wpinv', __( 'Invoice Settings', 'invoicing' ), __( 'Settings', 'invoicing' ), $capability, 'wpinv-settings', array( $this, 'options_page' ));
39
    }
40
41
    function options_page() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
42
        $page       = isset( $_GET['page'] )                ? strtolower( $_GET['page'] )               : false;
43
44
        if ( $page !== 'wpinv-settings' ) {
45
            return;
46
        }
47
48
        $settings_tabs = wpinv_get_settings_tabs();
49
        $settings_tabs = empty($settings_tabs) ? array() : $settings_tabs;
50
        $active_tab    = isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], $settings_tabs ) ? sanitize_text_field( $_GET['tab'] ) : 'general';
51
        $sections      = wpinv_get_settings_tab_sections( $active_tab );
52
        $key           = 'main';
53
54
        if ( is_array( $sections ) ) {
55
            $key = key( $sections );
56
        }
57
58
        $registered_sections = wpinv_get_settings_tab_sections( $active_tab );
59
        $section             = isset( $_GET['section'] ) && ! empty( $registered_sections ) && array_key_exists( $_GET['section'], $registered_sections ) ? $_GET['section'] : $key;
60
        ob_start();
61
        ?>
62
        <div class="wrap">
63
            <h1 class="nav-tab-wrapper">
64
                <?php
65
                foreach( wpinv_get_settings_tabs() as $tab_id => $tab_name ) {
66
                    $tab_url = add_query_arg( array(
67
                        'settings-updated' => false,
68
                        'tab' => $tab_id,
69
                    ) );
70
71
                    // Remove the section from the tabs so we always end up at the main section
72
                    $tab_url = remove_query_arg( 'section', $tab_url );
73
                    $tab_url = remove_query_arg( 'wpi_sub', $tab_url );
74
75
                    $active = $active_tab == $tab_id ? ' nav-tab-active' : '';
76
77
                    echo '<a href="' . esc_url( $tab_url ) . '" title="' . esc_attr( $tab_name ) . '" class="nav-tab' . $active . '">';
78
                    echo esc_html( $tab_name );
79
                    echo '</a>';
80
                }
81
                ?>
82
            </h1>
83
            <?php
84
            $number_of_sections = count( $sections );
85
            $number = 0;
86
            if ( $number_of_sections > 1 ) {
87
                echo '<div><ul class="subsubsub">';
88
                foreach( $sections as $section_id => $section_name ) {
89
                    echo '<li>';
90
                    $number++;
91
                    $tab_url = add_query_arg( array(
92
                        'settings-updated' => false,
93
                        'tab' => $active_tab,
94
                        'section' => $section_id
95
                    ) );
96
                    $tab_url = remove_query_arg( 'wpi_sub', $tab_url );
97
                    $class = '';
98
                    if ( $section == $section_id ) {
99
                        $class = 'current';
100
                    }
101
                    echo '<a class="' . $class . '" href="' . esc_url( $tab_url ) . '">' . $section_name . '</a>';
102
103
                    if ( $number != $number_of_sections ) {
104
                        echo ' | ';
105
                    }
106
                    echo '</li>';
107
                }
108
                echo '</ul></div>';
109
            }
110
            ?>
111
            <div id="tab_container">
112
                <form method="post" action="options.php">
113
                    <table class="form-table">
114
                        <?php
115
                        settings_fields( 'wpinv_settings' );
116
117
                        if ( 'main' === $section ) {
118
                            do_action( 'wpinv_settings_tab_top', $active_tab );
119
                        }
120
121
                        do_action( 'wpinv_settings_tab_top_' . $active_tab . '_' . $section, $active_tab, $section );
122
                        do_settings_sections( 'wpinv_settings_' . $active_tab . '_' . $section, $active_tab, $section );
123
                        do_action( 'wpinv_settings_tab_bottom_' . $active_tab . '_' . $section, $active_tab, $section );
124
125
                        // For backwards compatibility
126
                        if ( 'main' === $section ) {
127
                            do_action( 'wpinv_settings_tab_bottom', $active_tab );
128
                        }
129
                        ?>
130
                    </table>
131
                    <?php submit_button(); ?>
132
                </form>
133
            </div><!-- #tab_container-->
134
        </div><!-- .wrap -->
135
        <?php
136
        $content = ob_get_clean();
137
        echo $content;
138
    }
139
140
    public function remove_admin_submenus() {
141
        remove_submenu_page( 'edit.php?post_type=wpi_invoice', 'post-new.php?post_type=wpi_invoice' );
142
    }
143
144
    public function add_nav_menu_meta_boxes(){
145
        add_meta_box( 'wpinv_endpoints_nav_link', __( 'Invoicing Pages', 'invoicing' ), array( $this, 'nav_menu_links' ), 'nav-menus', 'side', 'low' );
146
    }
147
148
    public function nav_menu_links(){
149
        $endpoints = $this->get_menu_items();
150
        ?>
151
        <div id="invoicing-endpoints" class="posttypediv">
152
        <?php if(!empty($endpoints['pages'])){ ?>
153
            <div id="tabs-panel-invoicing-endpoints" class="tabs-panel tabs-panel-active">
154
                <ul id="invoicing-endpoints-checklist" class="categorychecklist form-no-clear">
155
                    <?php
156
                    $walker = new Walker_Nav_Menu_Checklist(array());
157
                    echo walk_nav_menu_tree(array_map('wp_setup_nav_menu_item', $endpoints['pages']), 0, (object) array('walker' => $walker));
158
                    ?>
159
                </ul>
160
            </div>
161
        <?php } ?>
162
        <p class="button-controls">
163
        <span class="list-controls">
164
            <a href="<?php echo admin_url( 'nav-menus.php?page-tab=all&selectall=1#invoicing-endpoints' ); ?>" class="select-all"><?php _e( 'Select all', 'invoicing' ); ?></a>
165
        </span>
166
            <span class="add-to-menu">
167
            <input type="submit" class="button-secondary submit-add-to-menu right" value="<?php esc_attr_e( 'Add to menu', 'invoicing' ); ?>" name="add-post-type-menu-item" id="submit-invoicing-endpoints">
168
            <span class="spinner"></span>
169
        </span>
170
        </p>
171
        <?php
172
    }
173
174
    public function get_menu_items(){
175
        $items = array();
176
177
        $wpinv_history_page_id = (int)wpinv_get_option( 'invoice_history_page' );
178 View Code Duplication
        if($wpinv_history_page_id > 0){
179
            $item = new stdClass();
180
            $item->object_id = $wpinv_history_page_id;
181
            $item->db_id = 0;
182
            $item->object =  'page';
183
            $item->menu_item_parent = 0;
184
            $item->type = 'post_type';
185
            $item->title = __('Invoice History Page','invoicing');
186
            $item->url = get_permalink( $wpinv_history_page_id );
187
            $item->target = '';
188
            $item->attr_title = '';
189
            $item->classes = array('wpinv-menu-item');
190
            $item->xfn = '';
191
192
            $items['pages'][] = $item;
193
        }
194
195
        $wpinv_sub_history_page_id = (int)wpinv_get_option( 'invoice_subscription_page' );
196 View Code Duplication
        if($wpinv_sub_history_page_id > 0){
197
            $item = new stdClass();
198
            $item->object_id = $wpinv_sub_history_page_id;
199
            $item->db_id = 0;
200
            $item->object =  'page';
201
            $item->menu_item_parent = 0;
202
            $item->type = 'post_type';
203
            $item->title = __('Invoice Subscriptions Page','invoicing');
204
            $item->url = get_permalink( $wpinv_sub_history_page_id );
205
            $item->target = '';
206
            $item->attr_title = '';
207
            $item->classes = array('wpinv-menu-item');
208
            $item->xfn = '';
209
210
            $items['pages'][] = $item;
211
        }
212
213
        $wpinv_checkout_page_id = (int)wpinv_get_option( 'checkout_page' );
214 View Code Duplication
        if($wpinv_checkout_page_id > 0){
215
            $item = new stdClass();
216
            $item->object_id = $wpinv_checkout_page_id;
217
            $item->db_id = 0;
218
            $item->object =  'page';
219
            $item->menu_item_parent = 0;
220
            $item->type = 'post_type';
221
            $item->title = __('Checkout Page','invoicing');
222
            $item->url = get_permalink( $wpinv_checkout_page_id );
223
            $item->target = '';
224
            $item->attr_title = '';
225
            $item->classes = array('wpinv-menu-item');
226
            $item->xfn = '';
227
228
            $items['pages'][] = $item;
229
        }
230
231
        $wpinv_tandc_page_id = (int)wpinv_get_option( 'tandc_page' );
232 View Code Duplication
        if($wpinv_tandc_page_id > 0){
233
            $item = new stdClass();
234
            $item->object_id = $wpinv_tandc_page_id;
235
            $item->db_id = 0;
236
            $item->object =  'page';
237
            $item->menu_item_parent = 0;
238
            $item->type = 'post_type';
239
            $item->title = __('Terms & Conditions','invoicing');
240
            $item->url = get_permalink( $wpinv_tandc_page_id );
241
            $item->target = '';
242
            $item->attr_title = '';
243
            $item->classes = array('wpinv-menu-item');
244
            $item->xfn = '';
245
246
            $items['pages'][] = $item;
247
        }
248
249
        $wpinv_success_page_id = (int)wpinv_get_option( 'success_page' );
250 View Code Duplication
        if($wpinv_success_page_id > 0){
251
            $item = new stdClass();
252
            $item->object_id = $wpinv_success_page_id;
253
            $item->db_id = 0;
254
            $item->object =  'page';
255
            $item->menu_item_parent = 0;
256
            $item->type = 'post_type';
257
            $item->title = __('Success Page','invoicing');
258
            $item->url = get_permalink( $wpinv_success_page_id );
259
            $item->target = '';
260
            $item->attr_title = '';
261
            $item->classes = array('wpinv-menu-item');
262
            $item->xfn = '';
263
264
            $items['pages'][] = $item;
265
        }
266
267
        $wpinv_failure_page_id = (int)wpinv_get_option( 'failure_page' );
268 View Code Duplication
        if($wpinv_failure_page_id > 0){
269
            $item = new stdClass();
270
            $item->object_id = $wpinv_failure_page_id;
271
            $item->db_id = 0;
272
            $item->object =  'page';
273
            $item->menu_item_parent = 0;
274
            $item->type = 'post_type';
275
            $item->title = __('Failed Transaction Page','invoicing');
276
            $item->url = get_permalink( $wpinv_failure_page_id );
277
            $item->target = '';
278
            $item->attr_title = '';
279
            $item->classes = array('wpinv-menu-item');
280
            $item->xfn = '';
281
282
            $items['pages'][] = $item;
283
        }
284
285
        return apply_filters( 'wpinv_menu_items', $items );
286
    }
287
288
}
289
290
return new WPInv_Admin_Menus();