Conditions | 15 |
Paths | 6144 |
Total Lines | 87 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
103 | public function process() { |
||
104 | $error = esc_html__( 'Could not install upgrade. Please download from monsterinsights.com and install manually.', 'google-analytics-for-wordpress' ); |
||
105 | |||
106 | // verify params present (oth & download link). |
||
107 | $post_oth = ! empty( $_REQUEST['oth'] ) ? sanitize_text_field( $_REQUEST['oth'] ) : ''; |
||
108 | $post_url = ! empty( $_REQUEST['file'] ) ? $_REQUEST['file'] : ''; |
||
109 | if ( empty( $post_oth ) || empty( $post_url ) ) { |
||
110 | wp_send_json_error( $error ); |
||
111 | } |
||
112 | // Verify oth. |
||
113 | $oth = get_option( 'monsterinsights_connect_token' ); |
||
114 | if ( empty( $oth ) ) { |
||
115 | wp_send_json_error( $error ); |
||
116 | } |
||
117 | if ( ! hash_equals( $oth, $post_oth ) ) { |
||
118 | wp_send_json_error( $error ); |
||
119 | } |
||
120 | // Delete so cannot replay. |
||
121 | delete_option( 'monsterinsights_connect_token' ); |
||
122 | // Set the current screen to avoid undefined notices. |
||
123 | set_current_screen( 'insights_page_monsterinsights_settings' ); |
||
124 | // Prepare variables. |
||
125 | $url = esc_url_raw( |
||
126 | add_query_arg( |
||
127 | array( |
||
128 | 'page' => 'monsterinsights-settings', |
||
129 | ), |
||
130 | admin_url( 'admin.php' ) |
||
131 | ) |
||
132 | ); |
||
133 | // Verify pro not activated. |
||
134 | if ( monsterinsights_is_pro_version() ) { |
||
135 | wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'google-analytics-for-wordpress' ) ); |
||
136 | } |
||
137 | // Verify pro not installed. |
||
138 | $active = activate_plugin( 'google-analytics-premium/googleanalytics-premium.php', $url, false, true ); |
||
139 | if ( ! is_wp_error( $active ) ) { |
||
140 | deactivate_plugins( plugin_basename( MONSTERINSIGHTS_PLUGIN_FILE ) ); |
||
141 | wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'wpforms-lite' ) ); |
||
142 | } |
||
143 | $creds = request_filesystem_credentials( $url, '', false, false, null ); |
||
|
|||
144 | // Check for file system permissions. |
||
145 | if ( false === $creds ) { |
||
146 | wp_send_json_error( $error ); |
||
147 | } |
||
148 | if ( ! WP_Filesystem( $creds ) ) { |
||
149 | wp_send_json_error( $error ); |
||
150 | } |
||
151 | // We do not need any extra credentials if we have gotten this far, so let's install the plugin. |
||
152 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
||
153 | require_once MONSTERINSIGHTS_PLUGIN_DIR . 'includes/admin/licensing/skin.php'; |
||
154 | // Do not allow WordPress to search/download translations, as this will break JS output. |
||
155 | remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 ); |
||
156 | // Create the plugin upgrader with our custom skin. |
||
157 | $installer = new Plugin_Upgrader( new MonsterInsights_Skin() ); |
||
158 | // Error check. |
||
159 | if ( ! method_exists( $installer, 'install' ) ) { |
||
160 | wp_send_json_error( $error ); |
||
161 | } |
||
162 | |||
163 | // Check license key. |
||
164 | $license = get_option( 'monsterinsights_connect', false ); |
||
165 | if ( empty( $license ) ) { |
||
166 | wp_send_json_error( new WP_Error( '403', esc_html__( 'You are not licensed.', 'google-analytics-for-wordpress' ) ) ); |
||
167 | } |
||
168 | |||
169 | $installer->install( $post_url ); // phpcs:ignore |
||
170 | // Flush the cache and return the newly installed plugin basename. |
||
171 | wp_cache_flush(); |
||
172 | |||
173 | if ( $installer->plugin_info() ) { |
||
174 | $plugin_basename = $installer->plugin_info(); |
||
175 | |||
176 | // Deactivate the lite version first. |
||
177 | deactivate_plugins( plugin_basename( MONSTERINSIGHTS_PLUGIN_FILE ) ); |
||
178 | |||
179 | // Activate the plugin silently. |
||
180 | $activated = activate_plugin( $plugin_basename, '', false, true ); |
||
181 | if ( ! is_wp_error( $activated ) ) { |
||
182 | wp_send_json_success( esc_html__( 'Plugin installed & activated.', 'google-analytics-for-wordpress' ) ); |
||
183 | } else { |
||
184 | // Reactivate the lite plugin if pro activation failed. |
||
185 | activate_plugin( plugin_basename( MONSTERINSIGHTS_PLUGIN_FILE ), '', false, true ); |
||
186 | wp_send_json_error( esc_html__( 'Pro version installed but needs to be activated from the Plugins page inside your WordPress admin.', 'google-analytics-for-wordpress' ) ); |
||
187 | } |
||
188 | } |
||
189 | wp_send_json_error( $error ); |
||
190 | } |
||
195 |