Passed
Pull Request — master (#328)
by Brian
04:47
created

WPInv_Admin_Menus::add_settings_menu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
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_addons_menu' ), 100 );
18
        add_action( 'admin_menu', array( $this, 'add_settings_menu' ), 60 );
19
        add_action( 'admin_menu', array( $this, 'remove_admin_submenus' ), 10 );
20
        add_action( 'admin_head-nav-menus.php', array( $this, 'add_nav_menu_meta_boxes' ) );
21
    }
22
23
    public function admin_menu() {
24
        global $menu, $submenu;
25
26
        if ( ! wpinv_current_user_can_manage_invoicing() ) {
27
            return;
28
        }
29
30
        $capability = apply_filters( 'invoicing_capability', wpinv_get_capability() );
31
32
        if ( wpinv_current_user_can_manage_invoicing() ) {
33
            $menu[] = array( '', 'read', 'separator-wpinv', '', 'wp-menu-separator wpinv' );
34
35
            // Allow users with 'manage_invocing' capability to create new invoices
36
            $submenu['post-new.php?post_type=wpi_item'][]     = array( '', '', 'post-new.php?post_type=wpi_item', '' );
37
            $submenu['post-new.php?post_type=wpi_invoice'][]  = array( '', '', 'post-new.php?post_type=wpi_invoice', '' );
38
            $submenu['post-new.php?post_type=wpi_discount'][] = array( '', '', 'post-new.php?post_type=wpi_discount', '' );
39
40
        }
41
42
        $wpi_invoice = get_post_type_object( 'wpi_invoice' );
43
44
        add_menu_page( __( 'Invoicing', 'invoicing' ), __( 'Invoicing', 'invoicing' ), $capability, 'wpinv', null, $wpi_invoice->menu_icon, '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

44
        add_menu_page( __( 'Invoicing', 'invoicing' ), __( 'Invoicing', 'invoicing' ), $capability, 'wpinv', null, $wpi_invoice->menu_icon, /** @scrutinizer ignore-type */ '54.123460' );
Loading history...
45
46
    }
47
48
    /**
49
     * Registers the settings menu.
50
     */
51
    public function add_settings_menu() {
52
        add_submenu_page(
53
            'wpinv',
54
            __( 'Invoice Settings', 'invoicing' ),
55
            __( 'Settings', 'invoicing' ),
56
            apply_filters( 'invoicing_capability', wpinv_get_capability() ),
57
            'wpinv-settings',
58
            array( $this, 'options_page' )
59
        );
60
    }
61
62
    public function add_addons_menu(){
63
        if ( !apply_filters( 'wpi_show_addons_page', true ) ) {
64
            return;
65
        }
66
67
        add_submenu_page(
68
            "wpinv",
69
            __('Invoicing extensions', 'invoicing'),
70
            __('Extensions', 'invoicing'),
71
            'manage_options',
72
            'wpi-addons',
73
            array( $this, 'addons_page' )
74
        );
75
    }
76
77
    public function addons_page(){
78
        $addon_obj = new WPInv_Admin_Addons();
79
        $addon_obj->output();
80
    }
81
82
    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...
83
        $page       = isset( $_GET['page'] )                ? strtolower( $_GET['page'] )               : false;
84
85
        if ( $page !== 'wpinv-settings' ) {
86
            return;
87
        }
88
89
        $settings_tabs = wpinv_get_settings_tabs();
90
        $settings_tabs = empty($settings_tabs) ? array() : $settings_tabs;
91
        $active_tab    = isset( $_GET['tab'] ) && array_key_exists( $_GET['tab'], $settings_tabs ) ? sanitize_text_field( $_GET['tab'] ) : 'general';
92
        $sections      = wpinv_get_settings_tab_sections( $active_tab );
93
        $key           = 'main';
94
95
        if ( is_array( $sections ) ) {
96
            $key = key( $sections );
97
        }
98
99
        $registered_sections = wpinv_get_settings_tab_sections( $active_tab );
100
        $section             = isset( $_GET['section'] ) && ! empty( $registered_sections ) && array_key_exists( $_GET['section'], $registered_sections ) ? $_GET['section'] : $key;
101
        ob_start();
102
        ?>
103
        <div class="wrap">
104
            <h1 class="nav-tab-wrapper">
105
                <?php
106
                foreach( wpinv_get_settings_tabs() as $tab_id => $tab_name ) {
107
                    $tab_url = add_query_arg( array(
108
                        'settings-updated' => false,
109
                        'tab' => $tab_id,
110
                    ) );
111
112
                    // Remove the section from the tabs so we always end up at the main section
113
                    $tab_url = remove_query_arg( 'section', $tab_url );
114
                    $tab_url = remove_query_arg( 'wpi_sub', $tab_url );
115
116
                    $active = $active_tab == $tab_id ? ' nav-tab-active' : '';
117
118
                    echo '<a href="' . esc_url( $tab_url ) . '" title="' . esc_attr( $tab_name ) . '" class="nav-tab' . $active . '">';
119
                    echo esc_html( $tab_name );
120
                    echo '</a>';
121
                }
122
                ?>
123
            </h1>
124
            <?php
125
            $number_of_sections = count( $sections );
126
            $number = 0;
127
            if ( $number_of_sections > 1 ) {
128
                echo '<div><ul class="subsubsub">';
129
                foreach( $sections as $section_id => $section_name ) {
130
                    echo '<li>';
131
                    $number++;
132
                    $tab_url = add_query_arg( array(
133
                        'settings-updated' => false,
134
                        'tab' => $active_tab,
135
                        'section' => $section_id
136
                    ) );
137
                    $tab_url = remove_query_arg( 'wpi_sub', $tab_url );
138
                    $class = '';
139
                    if ( $section == $section_id ) {
140
                        $class = 'current';
141
                    }
142
                    echo '<a class="' . $class . '" href="' . esc_url( $tab_url ) . '">' . $section_name . '</a>';
143
144
                    if ( $number != $number_of_sections ) {
145
                        echo ' | ';
146
                    }
147
                    echo '</li>';
148
                }
149
                echo '</ul></div>';
150
            }
151
            ?>
152
            <div id="tab_container">
153
                <form method="post" action="options.php">
154
                    <table class="form-table">
155
                        <?php
156
                        settings_fields( 'wpinv_settings' );
157
158
                        if ( 'main' === $section ) {
159
                            do_action( 'wpinv_settings_tab_top', $active_tab );
160
                        }
161
162
                        do_action( 'wpinv_settings_tab_top_' . $active_tab . '_' . $section, $active_tab, $section );
163
                        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

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