Passed
Push — master ( 5d063b...c346b8 )
by Stiofan
41s queued 12s
created

WPInv_Admin_Menus::addons_page()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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