install.php ➔ wpbo_activate()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 26
rs 8.439
1
<?php
2
/**
3
 * BetterOptin Installer
4
 *
5
 * @package   BetterOptin/Install
6
 * @author    ThemeAvenue <[email protected]>
7
 * @license   GPL-2.0+
8
 * @link      http://themeavenue.net
9
 * @copyright 2015 ThemeAvenue
10
 */
11
12
// If this file is called directly, abort.
13
if ( ! defined( 'WPINC' ) ) {
14
	die;
15
}
16
17
register_activation_hook( WPBO_PLUGIN_FILE, 'wpbo_activate' );
18
add_action( 'wpmu_new_blog', 'wpbo_activate_new_site' );
19
20
/**
21
 * Fired when the plugin is activated.
22
 *
23
 * @since    1.0.0
24
 *
25
 * @param    boolean    $network_wide    True if WPMU superadmin uses
26
 *                                       "Network Activate" action, false if
27
 *                                       WPMU is disabled or plugin is
28
 *                                       activated on an individual blog.
29
 */
30
function wpbo_activate( $network_wide ) {
31
32
	if ( function_exists( 'is_multisite' ) && is_multisite() ) {
33
34
		if ( $network_wide  ) {
35
36
			// Get all blog ids
37
			$blog_ids = wpbo_get_blog_ids();
38
39
			foreach ( $blog_ids as $blog_id ) {
0 ignored issues
show
Bug introduced by
The expression $blog_ids of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
40
41
				switch_to_blog( $blog_id );
42
				wpbo_single_activate();
43
			}
44
45
			restore_current_blog();
46
47
		} else {
48
			wpbo_single_activate();
49
		}
50
51
	} else {
52
		wpbo_single_activate();
53
	}
54
55
}
56
57
/**
58
 * Get all blog ids of blogs in the current network that are:
59
 * - not archived
60
 * - not spam
61
 * - not deleted
62
 *
63
 * @since    1.0.0
64
 *
65
 * @return   array|false    The blog ids, false if no matches.
66
 */
67
function wpbo_get_blog_ids() {
68
69
	global $wpdb;
70
71
	// get an array of blog ids
72
	$sql = "SELECT blog_id FROM $wpdb->blogs
73
			WHERE archived = '0' AND spam = '0'
74
			AND deleted = '0'";
75
76
	return $wpdb->get_col( $sql );
77
78
}
79
80
/**
81
 * Fired for each blog when the plugin is activated.
82
 *
83
 * @since    1.0.0
84
 */
85
function wpbo_single_activate() {
86
87
	/* Add new role */
88
	$subscriber = get_role( 'subscriber' );
89
	add_role( 'betteroptin', 'BetterOptin', $subscriber->capabilities );
90
91
	/* Create database table */
92
	wpbo_create_table();
93
	wpbo_failsafe_create_table();
94
95
	/* Write database version */
96
	update_option( 'wpbo_db_version', WPBO_DB_VERSION );
97
98
	$defaults = get_option( 'wpbo_options', array() );
99
100
	// Register default options
101
	if ( empty( $defaults ) ) {
102
103
		// Get default options
104
		if ( ! function_exists( 'wpbo_settings_general' ) ) {
105
			require( WPBO_PATH . 'includes/admin/settings/settings-general.php' );
106
		}
107
108
		$options = apply_filters( 'wpbo_plugin_settings', array() );
109
110
		foreach ( $options as $section_id => $section ) {
111
112
			foreach ( $section['options'] as $option ) {
113
114
				if ( ! isset( $option['id'] ) ) {
115
					continue;
116
				}
117
118
				$value                     = isset( $option['default'] ) ? $option['default'] : '';
119
				$defaults[ $option['id'] ] = $value;
120
121
			}
122
123
		}
124
125
		if ( ! empty( $defaults ) ) {
126
			update_option( 'wpbo_options', serialize( $defaults ) );
127
		}
128
129
	}
130
131
	/**
132
	 * Add an option in DB to know when the plugin has just been activated.
133
	 *
134
	 * @link http://stackoverflow.com/questions/7738953/is-there-a-way-to-determine-if-a-wordpress-plugin-is-just-installed/13927297#13927297
135
	 */
136
	add_option( 'wpbo_just_activated', true );
137
138
}
139
140
/**
141
 * Fired when a new site is activated with a WPMU environment.
142
 *
143
 * @since    1.0.0
144
 *
145
 * @param    int    $blog_id    ID of the new blog.
146
 */
147
function wpbo_activate_new_site( $blog_id ) {
148
149
	if ( 1 !== did_action( 'wpmu_new_blog' ) ) {
150
		return;
151
	}
152
153
	switch_to_blog( $blog_id );
154
	wpbo_single_activate();
155
	restore_current_blog();
156
157
}
158
159
/**
160
 * Redirect the user to the about page if the plugin was just installed
161
 *
162
 * @return void
163
 */
164
function wpbo_maybe_redirect_about() {
165
166
	if ( ! is_admin() ) {
167
		return;
168
	}
169
170
	$activated = get_option( 'wpbo_just_activated', false );
171
172
	/**
173
	 * First thing we check if the plugin has just been activated.
174
	 * If so, we take the user to the about page and delete the
175
	 * option we used for the check.
176
	 */
177
	if ( $activated ) {
178
179
		/* Delete the option */
180
		delete_option( 'wpbo_just_activated' );
181
182
		/* Redirect to about page */
183
		wp_redirect( add_query_arg( array(
184
			'post_type' => 'wpbo-popup',
185
			'page'      => 'wpbo-about'
186
		), admin_url( 'edit.php' ) ) );
187
188
		/* Don't do anything else */
189
		exit;
190
191
	}
192
193
}