Passed
Pull Request — master (#365)
by Brian
430:40 queued 310:21
created

WPInv_Admin_Menus::nav_menu_links()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 19
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 20
rs 9.6333
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, 'add_customers_menu' ), 18 );
18
        add_action( 'admin_menu', array( $this, 'add_addons_menu' ), 100 );
19
        add_action( 'admin_menu', array( $this, 'add_settings_menu' ), 60 );
20
        add_action( 'admin_menu', array( $this, 'remove_admin_submenus' ), 10 );
21
        add_action( 'admin_head-nav-menus.php', array( $this, 'add_nav_menu_meta_boxes' ) );
22
    }
23
24
    public function admin_menu() {
25
26
        $capability = apply_filters( 'invoicing_capability', wpinv_get_capability() );
27
        add_menu_page(
28
            __( 'GetPaid', 'invoicing' ),
29
            __( 'GetPaid', 'invoicing' ),
30
            $capability,
31
            'wpinv',
32
            null,
33
            'data:image/svg+xml;base64,' . base64_encode( file_get_contents( WPINV_PLUGIN_DIR . 'assets/images/GetPaid.svg' ) ),
34
            '54.123460'
0 ignored issues
show
Bug introduced by
'54.123460' of type string is incompatible with the type integer expected by parameter $position of add_menu_page(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
            /** @scrutinizer ignore-type */ '54.123460'
Loading history...
35
        );
36
37
    }
38
39
    /**
40
     * Registers the customers menu
41
     */
42
    public function add_customers_menu() {
43
        add_submenu_page(
44
            'wpinv',
45
            __( 'Customers', 'invoicing' ),
46
            __( 'Customers', 'invoicing' ),
47
            wpinv_get_capability(),
48
            'wpinv-customers',
49
            array( $this, 'customers_page' )
50
        );
51
    }
52
53
    /**
54
     * Displays the customers page.
55
     */
56
    public function customers_page() {
57
        require_once( WPINV_PLUGIN_DIR . 'includes/admin/class-wpinv-customers-table.php' );
58
        ?>
59
        <div class="wrap wpi-customers-wrap">
60
            <h1><?php echo esc_html( __( 'Customers', 'invoicing' ) ); ?></h1>
61
            <?php
62
                $table = new WPInv_Customers_Table();
63
                $table->prepare_items();
64
                $table->display();
65
            ?>
66
        </div>
67
        <?php
68
    }
69
70
    /**
71
     * Registers the settings menu.
72
     */
73
    public function add_settings_menu() {
74
        add_submenu_page(
75
            'wpinv',
76
            __( 'Invoice Settings', 'invoicing' ),
77
            __( 'Settings', 'invoicing' ),
78
            apply_filters( 'invoicing_capability', wpinv_get_capability() ),
79
            'wpinv-settings',
80
            array( $this, 'options_page' )
81
        );
82
    }
83
84
    public function add_addons_menu(){
85
        if ( !apply_filters( 'wpi_show_addons_page', true ) ) {
86
            return;
87
        }
88
89
        add_submenu_page(
90
            "wpinv",
91
            __('Invoicing extensions', 'invoicing'),
92
            __('Extensions', 'invoicing'),
93
            'manage_options',
94
            'wpi-addons',
95
            array( $this, 'addons_page' )
96
        );
97
    }
98
99
    public function addons_page(){
100
        $addon_obj = new WPInv_Admin_Addons();
101
        $addon_obj->output();
102
    }
103
104
    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...
105
        $page       = isset( $_GET['page'] )                ? strtolower( $_GET['page'] )               : false;
106
107
        if ( $page !== 'wpinv-settings' ) {
108
            return;
109
        }
110
111
        $settings_tabs = wpinv_get_settings_tabs();
112
        $settings_tabs = empty($settings_tabs) ? array() : $settings_tabs;
113
        $active_tab    = isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], $settings_tabs ) ? sanitize_text_field( $_GET['tab'] ) : 'general';
114
        $sections      = wpinv_get_settings_tab_sections( $active_tab );
115
        $key           = 'main';
116
117
        if ( is_array( $sections ) ) {
118
            $key = key( $sections );
119
        }
120
121
        $registered_sections = wpinv_get_settings_tab_sections( $active_tab );
122
        $section             = isset( $_GET['section'] ) && ! empty( $registered_sections ) && array_key_exists( $_GET['section'], $registered_sections ) ? $_GET['section'] : $key;
123
        ob_start();
124
        ?>
125
        <div class="wrap">
126
            <h1 class="nav-tab-wrapper">
127
                <?php
128
                foreach( wpinv_get_settings_tabs() as $tab_id => $tab_name ) {
129
                    $tab_url = add_query_arg( array(
130
                        'settings-updated' => false,
131
                        'tab' => $tab_id,
132
                    ) );
133
134
                    // Remove the section from the tabs so we always end up at the main section
135
                    $tab_url = remove_query_arg( 'section', $tab_url );
136
                    $tab_url = remove_query_arg( 'wpi_sub', $tab_url );
137
138
                    $active = $active_tab == $tab_id ? ' nav-tab-active' : '';
139
140
                    echo '<a href="' . esc_url( $tab_url ) . '" title="' . esc_attr( $tab_name ) . '" class="nav-tab' . $active . '">';
141
                    echo esc_html( $tab_name );
142
                    echo '</a>';
143
                }
144
                ?>
145
            </h1>
146
            <?php
147
            $number_of_sections = count( $sections );
148
            $number = 0;
149
            if ( $number_of_sections > 1 ) {
150
                echo '<div><ul class="subsubsub">';
151
                foreach( $sections as $section_id => $section_name ) {
152
                    echo '<li>';
153
                    $number++;
154
                    $tab_url = add_query_arg( array(
155
                        'settings-updated' => false,
156
                        'tab' => $active_tab,
157
                        'section' => $section_id
158
                    ) );
159
                    $tab_url = remove_query_arg( 'wpi_sub', $tab_url );
160
                    $class = '';
161
                    if ( $section == $section_id ) {
162
                        $class = 'current';
163
                    }
164
                    echo '<a class="' . $class . '" href="' . esc_url( $tab_url ) . '">' . $section_name . '</a>';
165
166
                    if ( $number != $number_of_sections ) {
167
                        echo ' | ';
168
                    }
169
                    echo '</li>';
170
                }
171
                echo '</ul></div>';
172
            }
173
            ?>
174
            <div id="tab_container">
175
                <form method="post" action="options.php">
176
                    <table class="form-table">
177
                        <?php
178
                        settings_fields( 'wpinv_settings' );
179
180
                        if ( 'main' === $section ) {
181
                            do_action( 'wpinv_settings_tab_top', $active_tab );
182
                        }
183
184
                        do_action( 'wpinv_settings_tab_top_' . $active_tab . '_' . $section, $active_tab, $section );
185
                        do_settings_sections( 'wpinv_settings_' . $active_tab . '_' . $section, $active_tab, $section );
0 ignored issues
show
Unused Code introduced by
The call to do_settings_sections() has too many arguments starting with $active_tab. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

185
                        /** @scrutinizer ignore-call */ 
186
                        do_settings_sections( 'wpinv_settings_' . $active_tab . '_' . $section, $active_tab, $section );

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
186
                        do_action( 'wpinv_settings_tab_bottom_' . $active_tab . '_' . $section, $active_tab, $section );
187
188
                        // For backwards compatibility
189
                        if ( 'main' === $section ) {
190
                            do_action( 'wpinv_settings_tab_bottom', $active_tab );
191
                        }
192
                        ?>
193
                    </table>
194
                    <?php submit_button(); ?>
195
                </form>
196
            </div><!-- #tab_container-->
197
        </div><!-- .wrap -->
198
        <?php
199
        $content = ob_get_clean();
200
        echo $content;
201
    }
202
203
    public function remove_admin_submenus() {
204
        remove_submenu_page( 'edit.php?post_type=wpi_invoice', 'post-new.php?post_type=wpi_invoice' );
205
    }
206
207
    public function add_nav_menu_meta_boxes(){
208
        add_meta_box( 'wpinv_endpoints_nav_link', __( 'Invoicing Pages', 'invoicing' ), array( $this, 'nav_menu_links' ), 'nav-menus', 'side', 'low' );
209
    }
210
211
    public function nav_menu_links(){
212
        $endpoints = $this->get_menu_items();
213
        ?>
214
        <div id="invoicing-endpoints" class="posttypediv">
215
        <?php if(!empty($endpoints['pages'])){ ?>
216
            <div id="tabs-panel-invoicing-endpoints" class="tabs-panel tabs-panel-active">
217
                <ul id="invoicing-endpoints-checklist" class="categorychecklist form-no-clear">
218
                    <?php
219
                    $walker = new Walker_Nav_Menu_Checklist(array());
220
                    echo walk_nav_menu_tree(array_map('wp_setup_nav_menu_item', $endpoints['pages']), 0, (object) array('walker' => $walker));
221
                    ?>
222
                </ul>
223
            </div>
224
        <?php } ?>
225
        <p class="button-controls">
226
        <span class="list-controls">
227
            <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>
228
        </span>
229
            <span class="add-to-menu">
230
            <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">
231
            <span class="spinner"></span>
232
        </span>
233
        </p>
234
        <?php
235
    }
236
237
    public function get_menu_items(){
238
        $items = array();
239
240
        $wpinv_history_page_id = (int)wpinv_get_option( 'invoice_history_page' );
241
        if($wpinv_history_page_id > 0){
242
            $item = new stdClass();
243
            $item->object_id = $wpinv_history_page_id;
244
            $item->db_id = 0;
245
            $item->object =  'page';
246
            $item->menu_item_parent = 0;
247
            $item->type = 'post_type';
248
            $item->title = __('Invoice History Page','invoicing');
249
            $item->url = get_permalink( $wpinv_history_page_id );
250
            $item->target = '';
251
            $item->attr_title = '';
252
            $item->classes = array('wpinv-menu-item');
253
            $item->xfn = '';
254
255
            $items['pages'][] = $item;
256
        }
257
258
        $wpinv_sub_history_page_id = (int)wpinv_get_option( 'invoice_subscription_page' );
259
        if($wpinv_sub_history_page_id > 0){
260
            $item = new stdClass();
261
            $item->object_id = $wpinv_sub_history_page_id;
262
            $item->db_id = 0;
263
            $item->object =  'page';
264
            $item->menu_item_parent = 0;
265
            $item->type = 'post_type';
266
            $item->title = __('Invoice Subscriptions Page','invoicing');
267
            $item->url = get_permalink( $wpinv_sub_history_page_id );
268
            $item->target = '';
269
            $item->attr_title = '';
270
            $item->classes = array('wpinv-menu-item');
271
            $item->xfn = '';
272
273
            $items['pages'][] = $item;
274
        }
275
276
        $wpinv_checkout_page_id = (int)wpinv_get_option( 'checkout_page' );
277
        if($wpinv_checkout_page_id > 0){
278
            $item = new stdClass();
279
            $item->object_id = $wpinv_checkout_page_id;
280
            $item->db_id = 0;
281
            $item->object =  'page';
282
            $item->menu_item_parent = 0;
283
            $item->type = 'post_type';
284
            $item->title = __('Checkout Page','invoicing');
285
            $item->url = get_permalink( $wpinv_checkout_page_id );
286
            $item->target = '';
287
            $item->attr_title = '';
288
            $item->classes = array('wpinv-menu-item');
289
            $item->xfn = '';
290
291
            $items['pages'][] = $item;
292
        }
293
294
        $wpinv_tandc_page_id = (int)wpinv_get_option( 'tandc_page' );
295
        if($wpinv_tandc_page_id > 0){
296
            $item = new stdClass();
297
            $item->object_id = $wpinv_tandc_page_id;
298
            $item->db_id = 0;
299
            $item->object =  'page';
300
            $item->menu_item_parent = 0;
301
            $item->type = 'post_type';
302
            $item->title = __('Terms & Conditions','invoicing');
303
            $item->url = get_permalink( $wpinv_tandc_page_id );
304
            $item->target = '';
305
            $item->attr_title = '';
306
            $item->classes = array('wpinv-menu-item');
307
            $item->xfn = '';
308
309
            $items['pages'][] = $item;
310
        }
311
312
        $wpinv_success_page_id = (int)wpinv_get_option( 'success_page' );
313
        if($wpinv_success_page_id > 0){
314
            $item = new stdClass();
315
            $item->object_id = $wpinv_success_page_id;
316
            $item->db_id = 0;
317
            $item->object =  'page';
318
            $item->menu_item_parent = 0;
319
            $item->type = 'post_type';
320
            $item->title = __('Success Page','invoicing');
321
            $item->url = get_permalink( $wpinv_success_page_id );
322
            $item->target = '';
323
            $item->attr_title = '';
324
            $item->classes = array('wpinv-menu-item');
325
            $item->xfn = '';
326
327
            $items['pages'][] = $item;
328
        }
329
330
        $wpinv_failure_page_id = (int)wpinv_get_option( 'failure_page' );
331
        if($wpinv_failure_page_id > 0){
332
            $item = new stdClass();
333
            $item->object_id = $wpinv_failure_page_id;
334
            $item->db_id = 0;
335
            $item->object =  'page';
336
            $item->menu_item_parent = 0;
337
            $item->type = 'post_type';
338
            $item->title = __('Failed Transaction Page','invoicing');
339
            $item->url = get_permalink( $wpinv_failure_page_id );
340
            $item->target = '';
341
            $item->attr_title = '';
342
            $item->classes = array('wpinv-menu-item');
343
            $item->xfn = '';
344
345
            $items['pages'][] = $item;
346
        }
347
348
        return apply_filters( 'wpinv_menu_items', $items );
349
    }
350
351
}
352
353
return new WPInv_Admin_Menus();