Passed
Push — master ( 4537f3...b8a531 )
by Brian
04:39
created

WPInv_Admin_Menus::nav_menu_links()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 21
nc 2
nop 0
dl 0
loc 23
rs 9.584
c 1
b 1
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, 'add_customers_menu' ), 18 );
18
        add_action( 'admin_menu', array( $this, 'add_subscriptions_menu' ), 40 );
19
        add_action( 'admin_menu', array( $this, 'add_addons_menu' ), 100 );
20
        add_action( 'admin_menu', array( $this, 'add_settings_menu' ), 60 );
21
        add_action( 'admin_menu', array( $this, 'remove_admin_submenus' ), 10 );
22
        add_action( 'admin_head-nav-menus.php', array( $this, 'add_nav_menu_meta_boxes' ) );
23
    }
24
25
    public function admin_menu() {
26
27
        $capability = apply_filters( 'invoicing_capability', wpinv_get_capability() );
28
        add_menu_page(
29
            __( 'GetPaid', 'invoicing' ),
30
            __( 'GetPaid', 'invoicing' ),
31
            $capability,
32
            'wpinv',
33
            null,
34
            'data:image/svg+xml;base64,' . base64_encode( file_get_contents( WPINV_PLUGIN_DIR . 'assets/images/GetPaid.svg' ) ),
35
            '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

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

205
                        /** @scrutinizer ignore-call */ 
206
                        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...
206
                        do_action( 'wpinv_settings_tab_bottom_' . $active_tab . '_' . $section, $active_tab, $section );
207
                        do_action( 'getpaid_settings_tab_bottom', $active_tab, $section );
208
209
                        // For backwards compatibility
210
                        if ( 'main' === $section ) {
211
                            do_action( 'wpinv_settings_tab_bottom', $active_tab );
212
                        }
213
                        ?>
214
                    </table>
215
                    <?php submit_button(); ?>
216
                </form>
217
            </div><!-- #tab_container-->
218
        </div><!-- .wrap -->
219
        <?php
220
        $content = ob_get_clean();
221
        echo $content;
222
    }
223
224
    public function remove_admin_submenus() {
225
        remove_submenu_page( 'edit.php?post_type=wpi_invoice', 'post-new.php?post_type=wpi_invoice' );
226
    }
227
228
    /**
229
     * Register our own endpoints section.
230
     */
231
    public function add_nav_menu_meta_boxes() {
232
233
        add_meta_box(
234
            'wpinv_endpoints_nav_link',
235
            __( 'GetPaid endpoints', 'invoicing' ),
236
            array( $this, 'nav_menu_links' ),
237
            'nav-menus',
238
            'side',
239
            'low'
240
        );
241
242
    }
243
244
    /**
245
     * Displays GetPaid nav menu links.
246
     */
247
    public function nav_menu_links() {
248
        $endpoints = $this->get_menu_items();
249
        ?>
250
        <div id="invoicing-endpoints" class="posttypediv">
251
            <?php if ( ! empty( $endpoints['pages'] ) ) : ?>
252
                <div id="tabs-panel-invoicing-endpoints" class="tabs-panel tabs-panel-active">
253
                    <ul id="invoicing-endpoints-checklist" class="categorychecklist form-no-clear">
254
                        <?php
255
                            $walker = new Walker_Nav_Menu_Checklist( array() );
256
                            echo walk_nav_menu_tree( array_map( 'wp_setup_nav_menu_item', $endpoints['pages'] ), 0, (object) array( 'walker' => $walker ) );
257
                        ?>
258
                    </ul>
259
                </div>
260
            <?php endif; ?>
261
262
            <p class="button-controls wp-clearfix" data-items-type="invoicing-endpoints">
263
                <span class="list-controls hide-if-no-js">
264
                    <input type="checkbox" id="invoicing-endpoints-tab" class="select-all">
265
                    <label for="invoicing-endpoints-tab"><?php _e( 'Select all', 'invoicing' ); ?></label>
266
                </span>
267
268
                <span class="add-to-menu">
269
                    <input type="submit" class="button submit-add-to-menu right" value="<?php esc_attr_e( 'Add to menu', 'invoicing' ); ?>" name="add-invoicing-endpoints-item" id="submit-invoicing-endpoints">
270
                    <span class="spinner"></span>
271
                </span>
272
            </p>
273
        </div>
274
        <?php
275
    }
276
277
    /**
278
     * Returns the menu entry pages.
279
     *
280
     * @return array.
0 ignored issues
show
Documentation Bug introduced by
The doc comment array. at position 0 could not be parsed: Unknown type name 'array.' at position 0 in array..
Loading history...
281
     */
282
    public function get_menu_items(){
283
        $items = array();
284
285
        $pages = array(
286
            array(
287
                'id'    => wpinv_get_option( 'invoice_history_page' ),
288
                'label' => __( 'My Invoices', 'invoicing' ),
289
            ),
290
            array(
291
                'id'    => wpinv_get_option( 'invoice_subscription_page' ),
292
                'label' => __( 'My Subscriptions', 'invoicing' ),
293
            )
294
        );
295
296
        foreach ( apply_filters( 'getpaid_menu_pages', $pages ) as $page ) {
297
298
            if ( (int) $page['id'] > 0 ) {
299
300
                $item                   = new stdClass();
301
                $item->object_id        = (int) $page['id'];
302
                $item->db_id            = 0;
303
                $item->object           =  'page';
304
                $item->menu_item_parent = 0;
305
                $item->type             = 'post_type';
306
                $item->title            = sanitize_text_field( $page['label'] );
307
                $item->url              = get_permalink( (int) $page['id'] );
308
                $item->target           = '';
309
                $item->attr_title       = '';
310
                $item->classes          = array( 'wpinv-menu-item' );
311
                $item->xfn              = '';
312
313
                $items['pages'][]       = $item;
314
315
            }
316
317
        }
318
319
        return apply_filters( 'wpinv_menu_items', $items );
320
    }
321
322
}
323
324
return new WPInv_Admin_Menus();