GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — hotfix/unyson-support ( a83d09 )
by Brad
02:40
created

handle_extension_action()   D

Complexity

Conditions 13
Paths 20

Size

Total Lines 86
Code Lines 51

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 13
eloc 51
nc 20
nop 0
dl 0
loc 86
rs 4.9922

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * FooGallery Admin Extension class
4
 */
5
6
if ( ! class_exists( 'FooGallery_Admin_Extensions' ) ) {
7
8
	class FooGallery_Admin_Extensions {
9
10
		function __construct() {
11
			add_action( 'init', array( $this, 'init' ) );
12
			add_action( 'deactivated_plugin', array( $this, 'handle_extensions_deactivation' ), 10, 2 );
13
			add_action( 'activated_plugin', array( $this, 'handle_extensions_activation' ), 10, 2 );
14
		}
15
16
		function init() {
17
			add_action( 'admin_init', array( $this, 'handle_extension_action' ) );
18
			add_action( 'admin_init', array( $this, 'redirect_on_activation' ) );
19
		}
20
21
		function handle_extensions_deactivation( $plugin, $network_deactivating ) {
22
			//make sure that if we are dealing with a FooGallery extension, that we deactivate it too
23
			$api = new FooGallery_Extensions_API();
24
			$api->handle_wordpress_plugin_deactivation( $plugin );
25
		}
26
27
		function handle_extensions_activation( $plugin, $network_deactivating ) {
28
			//make sure that if we are dealing with a FooGallery extension, that we deactivate it too
29
			$api = new FooGallery_Extensions_API();
30
			$api->handle_wordpress_plugin_activation( $plugin );
31
		}
32
33
		function handle_extension_action() {
34
			$action         = safe_get_from_request( 'action' );
35
			$extension_slug = safe_get_from_request( 'extension' );
36
			$has_error      = safe_get_from_request( 'has_error' );
37
38
			if ( ( 'download' === $action || 'activate' === $action || 'deactivate' === $action ) && $extension_slug ) {
39
				$api = new FooGallery_Extensions_API();
40
41
				$fatal_error_redirect = remove_query_arg( 'action' );
42
				wp_redirect( add_query_arg( 'has_error', 'yes', $fatal_error_redirect ) ); // we'll override this later if the plugin can be included without fatal error
43
				ob_start();
44
45
				switch ( $action ) {
46
					case 'download':
47
						$result = $api->download( $extension_slug );
48
						break;
49
					case 'activate':
50
						$result = $api->activate( $extension_slug );
51
						break;
52
					case 'deactivate':
53
						$result = $api->deactivate( $extension_slug );
54
						break;
55
				}
56
57
				//if we get here then no fatal error - cool!
58
				ob_end_clean();
59
60
				//store the result in a short-lived transient
61
				if ( isset($result) ) {
62
					set_transient( FOOGALLERY_EXTENSIONS_MESSAGE_TRANSIENT_KEY, $result, 30 );
63
				}
64
65
				//first, remove unwanted query args
66
				$redirect_url = remove_query_arg( array( 'extension', 'action' ) );
67
				//then add a query arg for our message
68
				$redirect_url = add_query_arg( 'show_message', 'yes', $redirect_url );
69
				//finally, allow extensions to override their own redirect
70
				$redirect_url = apply_filters( 'foogallery_extensions_redirect_url-' . $extension_slug, $redirect_url, $action );
71
72
				//redirect to this page, so the plugin can be properly activated/deactivated etc
73
				if ( $redirect_url ) {
74
					wp_redirect( $redirect_url );
75
					die();
76
				}
77
			} else if ( 'reload' === $action ) {
78
				$api = new FooGallery_Extensions_API();
79
				$api->reload();
80
81
				//first, remove unwanted query args
82
				$redirect_url = remove_query_arg( array( 'extension', 'action' ) );
83
84
				if ( ! $api->has_extension_loading_errors() ) {
85
					$result = array(
86
						'message' => __( 'The extensions have been reloaded', 'foogallery' ),
87
						'type'    => 'success',
88
					);
89
90
					set_transient( FOOGALLERY_EXTENSIONS_MESSAGE_TRANSIENT_KEY, $result, 30 );
91
92
					//then add a query arg for our message
93
					$redirect_url = add_query_arg( 'show_message', 'yes', $redirect_url );
94
				}
95
96
				wp_redirect( $redirect_url );
97
				die();
98
			} else if ( $has_error ) {
99
				$api = new FooGallery_Extensions_API();
100
				$api->deactivate( $extension_slug, true, false );
101
102
				$result = array(
103
					'message' => __( 'The extension could not be activated due to an error!', 'foogallery' ),
104
					'type'    => 'error',
105
				);
106
107
				set_transient( FOOGALLERY_EXTENSIONS_MESSAGE_TRANSIENT_KEY, $result, 30 );
108
109
				$api->add_to_error_extensions( $extension_slug, __( 'Activation Error!', 'foogallery' ) );
110
111
				//first, remove unwanted query args
112
				$redirect_url = remove_query_arg( array( 'extension', 'action', 'has_error' ) );
113
				//then add a query arg for our message
114
				$redirect_url = add_query_arg( 'show_message', 'yes', $redirect_url );
115
116
				wp_redirect( $redirect_url );
117
			}
118
		}
119
120
		function redirect_on_activation() {
121
			// Bail if no activation redirect
122
			if ( ! get_transient( FOOGALLERY_ACTIVATION_REDIRECT_TRANSIENT_KEY ) ) {
123
				return;
124
			}
125
126
			// Delete the redirect transient
127
			delete_transient( FOOGALLERY_ACTIVATION_REDIRECT_TRANSIENT_KEY );
128
129
			// Bail if activating from network, or bulk
130
			if ( is_network_admin() || isset($_GET['activate-multi']) ) {
131
				return;
132
			}
133
134
			wp_safe_redirect( foogallery_admin_help_url() );
135
			exit;
136
		}
137
	}
138
}
139