Complex classes like FS_Plugin_Updater often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FS_Plugin_Updater, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class FS_Plugin_Updater { |
||
19 | |||
20 | /** |
||
21 | * @var Freemius |
||
22 | * @since 1.0.4 |
||
23 | */ |
||
24 | private $_fs; |
||
25 | /** |
||
26 | * @var FS_Logger |
||
27 | * @since 1.0.4 |
||
28 | */ |
||
29 | private $_logger; |
||
30 | /** |
||
31 | * @var object |
||
32 | * @since 1.1.8.1 |
||
33 | */ |
||
34 | private $_update_details; |
||
35 | |||
36 | function __construct( Freemius $freemius ) { |
||
43 | |||
44 | /** |
||
45 | * Initiate required filters. |
||
46 | * |
||
47 | * @author Vova Feldman (@svovaf) |
||
48 | * @since 1.0.4 |
||
49 | */ |
||
50 | private function _filters() { |
||
91 | |||
92 | /** |
||
93 | * Capture plugin update row by turning output buffering. |
||
94 | * |
||
95 | * @author Vova Feldman (@svovaf) |
||
96 | * @since 1.1.6 |
||
97 | */ |
||
98 | function catch_plugin_update_row() { |
||
101 | |||
102 | /** |
||
103 | * Overrides default update message format with "renew your license" message. |
||
104 | * |
||
105 | * @author Vova Feldman (@svovaf) |
||
106 | * @since 1.1.6 |
||
107 | * |
||
108 | * @param string $file |
||
109 | * @param array $plugin_data |
||
110 | */ |
||
111 | function edit_and_echo_plugin_update_row( $file, $plugin_data ) { |
||
135 | |||
136 | /** |
||
137 | * Since WP version 3.6, a new security feature was added that denies access to repository with a local ip. |
||
138 | * During development mode we want to be able updating plugin versions via our localhost repository. This |
||
139 | * filter white-list all domains including "api.freemius". |
||
140 | * |
||
141 | * @link http://www.emanueletessore.com/wordpress-download-failed-valid-url-provided/ |
||
142 | * |
||
143 | * @author Vova Feldman (@svovaf) |
||
144 | * @since 1.0.4 |
||
145 | * |
||
146 | * @param bool $allow |
||
147 | * @param string $host |
||
148 | * @param string $url |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | function http_request_host_is_external_filter( $allow, $host, $url ) { |
||
155 | |||
156 | /** |
||
157 | * Check for Updates at the defined API endpoint and modify the update array. |
||
158 | * |
||
159 | * This function dives into the update api just when WordPress creates its update array, |
||
160 | * then adds a custom API call and injects the custom plugin data retrieved from the API. |
||
161 | * It is reassembled from parts of the native WordPress plugin update code. |
||
162 | * See wp-includes/update.php line 121 for the original wp_update_plugins() function. |
||
163 | * |
||
164 | * @author Vova Feldman (@svovaf) |
||
165 | * @since 1.0.4 |
||
166 | * |
||
167 | * @uses FS_Api |
||
168 | * |
||
169 | * @param object $transient_data Update array build by WordPress. |
||
170 | * |
||
171 | * @return object Modified update array with custom plugin data. |
||
172 | */ |
||
173 | function pre_set_site_transient_update_plugins_filter( $transient_data ) { |
||
216 | |||
217 | /** |
||
218 | * Try to fetch plugin's info from .org repository. |
||
219 | * |
||
220 | * @author Vova Feldman (@svovaf) |
||
221 | * @since 1.0.5 |
||
222 | * |
||
223 | * @param string $action |
||
224 | * @param object $args |
||
225 | * |
||
226 | * @return bool|mixed |
||
227 | */ |
||
228 | static function _fetch_plugin_info_from_repository( $action, $args ) { |
||
256 | |||
257 | /** |
||
258 | * Updates information on the "View version x.x details" page with custom data. |
||
259 | * |
||
260 | * @author Vova Feldman (@svovaf) |
||
261 | * @since 1.0.4 |
||
262 | * |
||
263 | * @uses FS_Api |
||
264 | * |
||
265 | * @param object $data |
||
266 | * @param string $action |
||
267 | * @param mixed $args |
||
268 | * |
||
269 | * @return object |
||
270 | */ |
||
271 | function plugins_api_filter( $data, $action = '', $args = null ) { |
||
345 | |||
346 | /** |
||
347 | * @author Vova Feldman (@svovaf) |
||
348 | * @since 1.2.1.7 |
||
349 | * |
||
350 | * @param number|bool $addon_id |
||
351 | * |
||
352 | * @return object |
||
353 | */ |
||
354 | private function get_latest_download_details( $addon_id = false ) { |
||
357 | |||
358 | /** |
||
359 | * Checks if a given basename has a matching folder name |
||
360 | * with the current context plugin. |
||
361 | * |
||
362 | * @author Vova Feldman (@svovaf) |
||
363 | * @since 1.2.1.6 |
||
364 | * |
||
365 | * @param string $basename Current plugin's basename. |
||
366 | * |
||
367 | * @return bool |
||
368 | */ |
||
369 | private function is_correct_folder_name( $basename = '' ) { |
||
376 | |||
377 | /** |
||
378 | * This is a special after upgrade handler for migrating modules |
||
379 | * that didn't use the '-premium' suffix folder structure before |
||
380 | * the migration. |
||
381 | * |
||
382 | * @author Vova Feldman (@svovaf) |
||
383 | * @since 1.2.1.6 |
||
384 | * |
||
385 | * @param bool $response Install response. |
||
386 | * @param array $hook_extra Extra arguments passed to hooked filters. |
||
387 | * @param array $result Installation result data. |
||
388 | * |
||
389 | * @return bool |
||
390 | */ |
||
391 | function _maybe_update_folder_name( $response, $hook_extra, $result ) { |
||
427 | |||
428 | #---------------------------------------------------------------------------------- |
||
429 | #region Auto Activation |
||
430 | #---------------------------------------------------------------------------------- |
||
431 | |||
432 | /** |
||
433 | * Installs and active a plugin when explicitly requested that from a 3rd party service. |
||
434 | * |
||
435 | * This logic was inspired by the TGMPA GPL licensed library by Thomas Griffin. |
||
436 | * |
||
437 | * @link http://tgmpluginactivation.com/ |
||
438 | * |
||
439 | * @author Vova Feldman |
||
440 | * @since 1.2.1.7 |
||
441 | * |
||
442 | * @link https://make.wordpress.org/plugins/2017/03/16/clarification-of-guideline-8-executable-code-and-installs/ |
||
443 | * |
||
444 | * @uses WP_Filesystem |
||
445 | * @uses WP_Error |
||
446 | * @uses WP_Upgrader |
||
447 | * @uses Plugin_Upgrader |
||
448 | * @uses Plugin_Installer_Skin |
||
449 | * @uses Plugin_Upgrader_Skin |
||
450 | * |
||
451 | * @param number|bool $plugin_id |
||
452 | * |
||
453 | * @return array |
||
454 | */ |
||
455 | function install_and_activate_plugin( $plugin_id = false ) { |
||
596 | |||
597 | /** |
||
598 | * Tries to activate a plugin. If fails, returns the error. |
||
599 | * |
||
600 | * @author Vova Feldman |
||
601 | * @since 1.2.1.7 |
||
602 | * |
||
603 | * @param string $file_path Path within wp-plugins/ to main plugin file. |
||
604 | * This determines the styling of the output messages. |
||
605 | * |
||
606 | * @return bool|WP_Error |
||
607 | */ |
||
608 | protected function try_activate_plugin( $file_path ) { |
||
615 | |||
616 | /** |
||
617 | * Check if a premium module version is already active. |
||
618 | * |
||
619 | * @author Vova Feldman |
||
620 | * @since 1.2.1.7 |
||
621 | * |
||
622 | * @param number|bool $plugin_id |
||
623 | * |
||
624 | * @return bool |
||
625 | */ |
||
626 | private function is_premium_plugin_active( $plugin_id = false ) { |
||
633 | |||
634 | /** |
||
635 | * Adjust the plugin directory name if necessary. |
||
636 | * Assumes plugin has a folder (not a single file plugin). |
||
637 | * |
||
638 | * The final destination directory of a plugin is based on the subdirectory name found in the |
||
639 | * (un)zipped source. In some cases this subdirectory name is not the same as the expected |
||
640 | * slug and the plugin will not be recognized as installed. This is fixed by adjusting |
||
641 | * the temporary unzipped source subdirectory name to the expected plugin slug. |
||
642 | * |
||
643 | * @author Vova Feldman |
||
644 | * @since 1.2.1.7 |
||
645 | * |
||
646 | * @param string $source Path to upgrade/zip-file-name.tmp/subdirectory/. |
||
647 | * @param string $remote_source Path to upgrade/zip-file-name.tmp. |
||
648 | * @param \WP_Upgrader $upgrader Instance of the upgrader which installs the plugin. |
||
649 | * |
||
650 | * @return string|WP_Error |
||
651 | */ |
||
652 | function _maybe_adjust_source_dir( $source, $remote_source, $upgrader ) { |
||
681 | |||
682 | #endregion |
||
683 | } |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.