Passed
Push — master ( 8c5c54...76481b )
by Brian
05:35
created

WPInv_Admin_Menus::customers_page()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 18
rs 9.6666
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_head', array( $this, 'set_admin_menu_class' ) );
17
        add_action( 'admin_menu', array( $this, 'admin_menu' ), 10 );
18
        add_action( 'admin_menu', array( $this, 'add_customers_menu' ), 18 );
19
        add_action( 'admin_menu', array( $this, 'add_subscriptions_menu' ), 40 );
20
        add_action( 'admin_menu', array( $this, 'add_addons_menu' ), 100 );
21
        add_action( 'admin_menu', array( $this, 'add_settings_menu' ), 60 );
22
        add_action( 'admin_menu', array( $this, 'remove_admin_submenus' ), 10 );
23
        add_action( 'admin_head-nav-menus.php', array( $this, 'add_nav_menu_meta_boxes' ) );
24
    }
25
26
    /**
27
	 * Highlights sub menus.
28
	 */
29
	public function set_admin_menu_class() {
30
		global $current_screen, $parent_file, $submenu_file;
31
32
        if ( ! empty( $current_screen->id ) && in_array( $current_screen->id , array( 'wpi_discount', 'wpi_payment_form', 'wpi_invoice' ) ) ) {
33
			$parent_file = 'wpinv';
34
			$submenu_file = 'edit.php?post_type=' . $current_screen->id;
35
        }
36
37
    }
38
39
    public function admin_menu() {
40
41
        $capability = apply_filters( 'invoicing_capability', wpinv_get_capability() );
42
        add_menu_page(
43
            __( 'GetPaid', 'invoicing' ),
44
            __( 'GetPaid', 'invoicing' ),
45
            $capability,
46
            'wpinv',
47
            null,
48
            'data:image/svg+xml;base64,' . base64_encode( file_get_contents( WPINV_PLUGIN_DIR . 'assets/images/GetPaid.svg' ) ),
49
            '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

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

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