Update::admin_redirects()   C
last analyzed

Complexity

Conditions 7
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 3
nop 0
dl 0
loc 23
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * Update Plugin
4
 *
5
 * @package SimpleCalendar/Updates
6
 */
7
namespace SimpleCalendar;
8
9
if ( ! defined( 'ABSPATH' ) ) {
10
	exit;
11
}
12
13
/**
14
 * Update script.
15
 *
16
 * Updates the installed plugin to the current version.
17
 *
18
 * @since 3.0.0
19
 */
20
class Update {
21
22
	/**
23
	 * Previous version.
24
	 *
25
	 * @access protected
26
	 * @var string
27
	 */
28
	private $installed_ver = '0.0.0';
29
30
	/**
31
	 * Current version.
32
	 *
33
	 * @access private
34
	 * @var string
35
	 */
36
	private $new_ver = '0.0.0';
37
38
	/**
39
	 * Existing posts.
40
	 *
41
	 * @access private
42
	 * @var array
43
	 */
44
	private $posts = array();
45
46
	/**
47
	 * Update path.
48
	 *
49
	 * @access private
50
	 *
51
	 * @var array
52
	 */
53
	private $update_path = array(
54
		'2.1.0',
55
		'2.2.0',
56
		'3.0.0',
57
		'3.0.13',
58
	);
59
60
	/**
61
	 * Constructor.
62
	 *
63
	 * @since 3.0.0
64
	 *
65
	 * @param string $version (optional) Current plugin version, defaults to value in plugin constant.
66
	 */
67
	public function __construct( $version = SIMPLE_CALENDAR_VERSION ) {
68
		// Look for previous version in current or legacy option, null for fresh install.
69
		$installed = get_option( 'simple-calendar_version', null );
70
		$this->installed_ver = is_null( $installed ) ? get_option( 'gce_version', null ) : $installed;
71
		$this->new_ver = $version;
72
73
		if ( version_compare( $this->installed_ver, $this->new_ver, '<' ) ) {
74
			$this->run_updates();
75
		}
76
	}
77
78
	/**
79
	 * Update to current version.
80
	 *
81
	 * Runs all the update scripts through version steps.
82
	 *
83
	 * @since 3.0.0
84
	 */
85
	public function run_updates() {
86
87
		do_action( 'simcal_before_update', $this->installed_ver );
88
89
		if ( ! is_null( $this->installed_ver ) ) {
90
91
			if ( version_compare( $this->installed_ver, $this->new_ver ) === -1 ) {
92
93
				$post_type = version_compare( $this->installed_ver, '3.0.0' ) === -1 ? 'gce_feed' : 'calendar';
94
				$this->posts = $this->get_posts( $post_type );
95
96
				foreach ( $this->update_path as $update_to ) {
97
					if ( version_compare( $this->installed_ver, $update_to, '<' ) ) {
98
						$this->update( $update_to );
99
					}
100
				}
101
102
			}
103
104
			simcal_delete_feed_transients();
105
106
		} else {
107
108
			new Post_Types();
109
			flush_rewrite_rules();
110
111
		}
112
113
		do_action( 'simcal_updated', $this->new_ver );
114
115
		// Redirect to a welcome page if new install or major update.
116
		if ( is_null( $this->installed_ver ) ) {
117
			set_transient( '_simple-calendar_activation_redirect', 'fresh', 60 );
118
		} else {
119
			$major_new = substr( $this->new_ver, 0, strrpos( $this->new_ver, '.' ) );
120
			$major_old = substr( $this->installed_ver, 0, strrpos( $this->installed_ver, '.' ) );
121
			if ( version_compare( $major_new, $major_old, '>' ) ) {
122
				set_transient( '_simple-calendar_activation_redirect', 'update', 60 );
123
			} elseif ( $major_old == $major_new ) {
124
				$version = explode( '.', $this->new_ver );
125
				end( $version );
126
				if ( 0 === intval( current( $version ) ) ) {
127
					set_transient( '_simple-calendar_activation_redirect', 'update', 60 );
128
				}
129
			}
130
		}
131
132
		$this->admin_redirects();
133
134
		update_option( 'simple-calendar_version', $this->new_ver );
135
	}
136
137
	/**
138
	 * Handle redirects to welcome page after install and updates.
139
	 *
140
	 * Transient must be present, the user must have access rights, and we must ignore the network/bulk plugin updaters.
141
	 *
142
	 * @since 3.0.0
143
	 */
144
	public function admin_redirects() {
0 ignored issues
show
Coding Style introduced by
admin_redirects uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
145
146
		$transient = get_transient( '_simple-calendar_activation_redirect' );
147
148
		if ( ! $transient || is_network_admin() || isset( $_GET['activate-multi'] ) || ! current_user_can( 'manage_options' ) ) {
149
			return;
150
		}
151
152
		delete_transient( '_simple-calendar_activation_redirect' );
153
154
		// Do not redirect if already on welcome page screen.
155
		if ( ! empty( $_GET['page'] ) && in_array( $_GET['page'], array( 'simple-calendar_about' ) ) ) {
156
			return;
157
		}
158
159
		$url = add_query_arg(
160
				'simcal_install',
161
				esc_attr( $transient ),
162
				admin_url( 'index.php?page=simple-calendar_about' )
163
		);
164
		wp_safe_redirect( $url );
165
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method admin_redirects() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
166
	}
167
168
	/**
169
	 * Get posts.
170
	 *
171
	 * @since  3.0.0
172
	 *
173
	 * @param  $post_type
174
	 *
175
	 * @return array
176
	 */
177
	private function get_posts( $post_type ) {
178
179
		$posts = array();
180
181
		if ( ! empty( $post_type ) ) {
182
183
			// https://core.trac.wordpress.org/ticket/18408
184
			$posts = get_posts( array(
185
				'post_type'   => $post_type,
186
				'post_status' => array(
187
					'draft',
188
					'future',
189
					'publish',
190
					'pending',
191
					'private',
192
					'trash',
193
				),
194
				'nopaging'    => true,
195
			) );
196
197
			wp_reset_postdata();
198
		}
199
200
		return $posts;
201
	}
202
203
	/**
204
	 * Update.
205
	 *
206
	 * Runs an update script for the specified version passed in argument.
207
	 *
208
	 * @since 3.0.0
209
	 *
210
	 * @param string $version
211
	 */
212
	private function update( $version ) {
213
214
		$update_v = '\\' . __NAMESPACE__ . '\Updates\\Update_V' . str_replace( '.', '', $version );
215
216
		if ( class_exists( $update_v ) ) {
217
			new $update_v( $this->posts );
218
		}
219
	}
220
221
}
222