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