Passed
Pull Request — master (#69)
by
unknown
04:21
created

install.php ➔ wpinv_get_core_capabilities()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 21
nc 2
nop 0
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
1
<?php
2
register_activation_hook(WPINV_PLUGIN_FILE, 'wpinv_plugin_activation');
3
register_deactivation_hook(WPINV_PLUGIN_FILE, 'wpinv_plugin_deactivation');
4
5
function wpinv_plugin_activation($network_wide = false)
6
{
7
    set_transient('_wpinv_activation_redirect', true, 30);
8
    wpinv_install($network_wide);
9
}
10
11
function wpinv_plugin_deactivation()
12
{
13
    wpinv_remove_admin_caps();
14
}
15
16
function wpinv_install($network_wide = false)
17
{
18
    global $wpdb;
19
20
    if (is_multisite() && $network_wide) {
21
        foreach ($wpdb->get_col("SELECT blog_id FROM $wpdb->blogs LIMIT 100") as $blog_id) {
22
            switch_to_blog($blog_id);
23
            wpinv_run_install();
24
            restore_current_blog();
25
        }
26
    } else {
27
        wpinv_run_install();
28
    }
29
}
30
31
function wpinv_run_install()
32
{
33
    global $wpdb, $wpinv_options, $wp_version, $wpi_session;
34
35
    // Setup the invoice Custom Post Type
36
    wpinv_register_post_types();
37
38
    // Clear the permalinks
39
    flush_rewrite_rules(false);
40
41
    // Add Upgraded From Option
42
    $current_version = get_option('wpinv_version');
43
    if ($current_version) {
44
        update_option('wpinv_version_upgraded_from', $current_version);
45
    }
46
47
    wpinv_create_pages();
48
    wpinv_add_admin_caps();
49
50
    // Pull options from WP, not GD Invoice's global
51
    $options = get_option('wpinv_settings', array());
52
53
    // Populate some default values
54
    foreach (wpinv_get_registered_settings() as $tab => $sections) {
55
        foreach ($sections as $section => $settings) {
56
            // Check for backwards compatibility
57
            $tab_sections = wpinv_get_settings_tab_sections($tab);
0 ignored issues
show
Documentation introduced by
$tab is of type integer|string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58 View Code Duplication
            if (!is_array($tab_sections) || !array_key_exists($section, $tab_sections)) {
59
                $section = 'main';
60
                $settings = $sections;
61
            }
62
63
            foreach ($settings as $option) {
64
                if (!empty($option['id']) && !isset($wpinv_options[$option['id']])) {
65
                    if ('checkbox' == $option['type'] && !empty($option['std'])) {
66
                        $options[$option['id']] = '1';
67
                    } else if (!empty($option['std'])) {
68
                        $options[$option['id']] = $option['std'];
69
                    }
70
                }
71
            }
72
        }
73
    }
74
75
    $merged_options = array_merge($wpinv_options, $options);
76
    $wpinv_options = $merged_options;
77
78
    update_option('wpinv_settings', $merged_options);
79
    update_option('wpinv_version', WPINV_VERSION);
80
81
    // Check for PHP Session support, and enable if available
82
    $wpi_session->use_php_sessions();
83
84
    // Add a temporary option to note that GD Invoice pages have been created
85
    set_transient('_wpinv_installed', $merged_options, 30);
86
87
    // Bail if activating from network, or bulk
88
    if (is_network_admin() || isset($_GET['activate-multi'])) {
89
        return;
90
    }
91
92
    // Add the transient to redirect
93
    set_transient('_wpinv_activation_redirect', true, 30);
94
}
95
96
/**
97
 * When a new Blog is created in multisite, see if Invoicing is network activated, and run the installer.
98
 *
99
 */
100
function wpinv_new_blog_created($blog_id, $user_id, $domain, $path, $site_id, $meta)
0 ignored issues
show
Unused Code introduced by
The parameter $user_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $domain is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $site_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $meta is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
101
{
102
    if (is_plugin_active_for_network(plugin_basename(WPINV_PLUGIN_FILE))) {
103
        switch_to_blog($blog_id);
104
        wpinv_run_install();
105
        restore_current_blog();
106
    }
107
}
108
109
add_action('wpmu_new_blog', 'wpinv_new_blog_created', 10, 6);
110
111
/**
112
 * Post-installation.
113
 *
114
 * Runs just after plugin installation and exposes the wpinv_after_install hook.
115
 */
116
function wpinv_after_install()
117
{
118
    if (!is_admin()) {
119
        return;
120
    }
121
122
    $wpinv_options = get_transient('_wpinv_installed');
123
    $wpinv_table_check = get_option('_wpinv_table_check', false);
124
125
    if (false === $wpinv_table_check || current_time('timestamp') > $wpinv_table_check) {
126
        update_option('_wpinv_table_check', (current_time('timestamp') + WEEK_IN_SECONDS));
127
    }
128
129
    if (false !== $wpinv_options) {
130
        // Delete the transient
131
        delete_transient('_wpinv_installed');
132
    }
133
}
134
135
add_action('admin_init', 'wpinv_after_install');
136
137
function wpinv_create_pages()
138
{
139
140
    $pages = apply_filters('wpinv_create_pages', array(
141
        'checkout_page' => array(
142
            'name' => _x('wpi-checkout', 'Page slug', 'invoicing'),
143
            'title' => _x('Checkout', 'Page title', 'invoicing'),
144
            'content' => '[' . apply_filters('wpinv_checkout_shortcode_tag', 'wpinv_checkout') . ']',
145
            'parent' => '',
146
        ),
147
        'invoice_history_page' => array(
148
            'name' => _x('wpi-history', 'Page slug', 'invoicing'),
149
            'title' => _x('Invoice History', 'Page title', 'invoicing'),
150
            'content' => '[' . apply_filters('wpinv_history_shortcode_tag', 'wpinv_history') . ']',
151
            'parent' => 'wpi-checkout',
152
        ),
153
        'success_page' => array(
154
            'name' => _x('wpinv-receipt', 'Page slug', 'invoicing'),
155
            'title' => _x('Payment Confirmation', 'Page title', 'invoicing'),
156
            'content' => '[' . apply_filters('wpinv_receipt_shortcode_tag', 'wpinv_receipt') . ']',
157
            'parent' => 'wpi-checkout',
158
        ),
159
        'failure_page' => array(
160
            'name' => _x('wpinv-transaction-failed', 'Page slug', 'invoicing'),
161
            'title' => _x('Transaction Failed', 'Page title', 'invoicing'),
162
            'content' => __('Your transaction failed, please try again or contact site support.', 'invoicing'),
163
            'parent' => 'wpi-checkout',
164
        ),
165
    ));
166
167
    foreach ($pages as $key => $page) {
168
        wpinv_create_page(esc_sql($page['name']), $key, $page['title'], $page['content'], $page['parent']);
169
    }
170
}
171
172
function wpinv_get_core_capabilities()
173
{
174
    $capabilities = array();
175
176
    $capabilities['core'] = array(
177
        'manage_invoicing',
178
    );
179
180
    $capability_types = array('wpi_invoice', 'wpi_quote', 'wpi_item', 'wpi_discount');
181
182
    foreach ($capability_types as $capability_type) {
183
184
        $capabilities[$capability_type] = array(
185
            // Post type
186
            "edit_{$capability_type}",
187
            "read_{$capability_type}",
188
            "delete_{$capability_type}",
189
            "edit_{$capability_type}s",
190
            "edit_others_{$capability_type}s",
191
            "publish_{$capability_type}s",
192
            "read_private_{$capability_type}s",
193
            "delete_{$capability_type}s",
194
            "delete_private_{$capability_type}s",
195
            "delete_published_{$capability_type}s",
196
            "delete_others_{$capability_type}s",
197
            "edit_private_{$capability_type}s",
198
            "edit_published_{$capability_type}s",
199
        );
200
    }
201
202
    return $capabilities;
203
}
204
205
function wpinv_add_admin_caps()
206
{
207
    global $wp_roles;
208
209
    $capabilities = wpinv_get_core_capabilities();
210
211
    foreach ($capabilities as $cap_group) {
212
        foreach ($cap_group as $cap) {
213
            $wp_roles->add_cap('administrator', $cap);
214
        }
215
    }
216
}
217
218
function wpinv_remove_admin_caps()
219
{
220
    global $wp_roles;
221
222
    $capabilities = wpinv_get_core_capabilities();
223
224
    foreach ($capabilities as $cap_group) {
225
        foreach ($cap_group as $cap) {
226
            $wp_roles->remove_cap('administrator', $cap);
227
        }
228
    }
229
}