Complex classes like GravityView_Admin_Installer 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 GravityView_Admin_Installer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class GravityView_Admin_Installer { |
||
17 | |||
18 | const EDD_API_URL = 'https://gravityview.co/edd-api/products/'; |
||
19 | |||
20 | const EDD_API_KEY = 'e4c7321c4dcf342c9cb078e27bf4ba97'; |
||
21 | |||
22 | const EDD_API_TOKEN = 'e031fd350b03bc223b10f04d8b5dde42'; |
||
23 | |||
24 | const DOWNLOADS_DATA_TRANSIENT = 'gv_downloads_data'; |
||
25 | |||
26 | const DOWNLOADS_DATA_TRANSIENT_EXPIRY = DAY_IN_SECONDS; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | public $minimum_capability = 'install_plugins'; |
||
32 | |||
33 | public function __construct() { |
||
46 | |||
47 | /** |
||
48 | * Let us operate when GF no-conflict is enabled |
||
49 | * |
||
50 | * @param array $items Scripts or styles to exclude from no-conflict |
||
51 | * |
||
52 | * @return array |
||
53 | */ |
||
54 | public function register_noconflict( $items ) { |
||
60 | |||
61 | |||
62 | /** |
||
63 | * Modify plugins data with custom GV extension info |
||
64 | * |
||
65 | * @return void |
||
66 | */ |
||
67 | public function add_downloads_data_filters() { |
||
68 | |||
69 | $downloads_data = get_site_transient( self::DOWNLOADS_DATA_TRANSIENT ); |
||
70 | |||
71 | if ( ! $downloads_data && ! isset( $_GET['cache'] ) ) { |
||
72 | return; |
||
73 | } |
||
74 | |||
75 | add_filter( 'plugins_api', function ( $data, $action, $args ) use ( $downloads_data ) { |
||
76 | foreach ( $downloads_data as $extension ) { |
||
77 | if ( empty( $extension['info'] ) || empty( $args->slug ) || $args->slug !== $extension['info']['slug'] ) { |
||
78 | continue; |
||
79 | } |
||
80 | |||
81 | return (object) array( |
||
82 | 'slug' => $extension['info']['slug'], |
||
83 | 'name' => $extension['info']['title'], |
||
84 | 'version' => $extension['licensing']['version'], |
||
85 | 'download_link' => $extension['files'][0]['file'], |
||
86 | ); |
||
87 | } |
||
88 | |||
89 | return $data; |
||
90 | }, 10, 3 ); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Add new admin menu |
||
95 | * |
||
96 | * @return void |
||
97 | */ |
||
98 | public function add_admin_menu() { |
||
113 | |||
114 | /** |
||
115 | * When on the Installer page, show a different notice than on the Settings page |
||
116 | * |
||
117 | * @param array $notice |
||
118 | * |
||
119 | * @return string License notice |
||
120 | */ |
||
121 | public function maybe_modify_license_notice( $notice = '' ) { |
||
129 | |||
130 | /** |
||
131 | * Get an array of plugins with textdomains as keys |
||
132 | * |
||
133 | * @since 2.1 |
||
134 | * @since 2.10 Added $textdomain argument. Converted to static method. Made method public (from protected). |
||
135 | * |
||
136 | * @param string $textdomain If set, only return plugins that have a matching textdomain. |
||
137 | * |
||
138 | * @return array { |
||
139 | * @type string $path Path to the plugin. |
||
140 | * @type string $version What version is the plugin. |
||
141 | * @type bool $activated Is the plugin activated. |
||
142 | * } |
||
143 | */ |
||
144 | static public function get_wp_plugins_data( $textdomain = null ) { |
||
165 | |||
166 | /** |
||
167 | * Get downloads data from transient or from API; save transient after getting data from API |
||
168 | * |
||
169 | * @return WP_Error|array If error, returns WP_Error. If not valid JSON, empty array. Otherwise, this structure: { |
||
170 | * @type array $info { |
||
171 | * @type string $id int 17 |
||
172 | * @type string $slug Extension slug |
||
173 | * @type string $title Extension title |
||
174 | * @type string $create_date in '2018-07-19 20:03:10' format |
||
175 | * @type string $modified_date |
||
176 | * @type string $status |
||
177 | * @type string $link URL to public plugin page |
||
178 | * @type string $content |
||
179 | * @type string $excerpt |
||
180 | * @type string $thumbnail URL to thumbnail |
||
181 | * @type array $category Taxonomy details for the plugin's category { |
||
182 | * @type int $term_id => int 30 |
||
183 | * @type string $name => string 'Plugins' (length=7) |
||
184 | * @type string $slug => string 'plugins' (length=7) |
||
185 | * @type int $term_group => int 0 |
||
186 | * @type int $term_taxonomy_id => int 30 |
||
187 | * @type string $taxonomy => string 'download_category' (length=17) |
||
188 | * @type string $description => string '' (length=0) |
||
189 | * @type int $parent => int 0 |
||
190 | * @type int $count => int 4 |
||
191 | * @type string $filter => string 'raw' (length=3) |
||
192 | * } |
||
193 | * @type array $tags {see $category above} |
||
194 | * @type string $textdomain string 'gravityview' (length=11) |
||
195 | * } |
||
196 | * @type array $pricing array of `price_name_slugs` => '00.00' values, if price options exist |
||
197 | * @type array $licensing { |
||
198 | * @type bool $enabled Is licensing enabled for the extension |
||
199 | * @type string $version Version number |
||
200 | * @type string $exp_unit Expiration unit ('years') |
||
201 | * @type string $exp_length Expiration length ('1') |
||
202 | * } |
||
203 | * @type array $files Array of files. Empty if user has no access to the file. { |
||
204 | * @type string $file string URL of the file download |
||
205 | * } |
||
206 | * } |
||
207 | */ |
||
208 | public function get_downloads_data() { |
||
252 | |||
253 | /** |
||
254 | * Save downloads data in a time-bound transient |
||
255 | * |
||
256 | * @param array $data |
||
257 | * |
||
258 | * @return true if successful, false otherwise |
||
259 | */ |
||
260 | public function set_downloads_data( $data ) { |
||
263 | |||
264 | /** |
||
265 | * Delete downloads data transient |
||
266 | * |
||
267 | * @return bool true if successful, false otherwise |
||
268 | */ |
||
269 | public function delete_downloads_data() { |
||
272 | |||
273 | /** |
||
274 | * Display a grid of available downloads and controls to install/activate/deactivate them |
||
275 | * |
||
276 | * @since 2.1 |
||
277 | * |
||
278 | * @return void |
||
279 | */ |
||
280 | public function render_screen() { |
||
333 | |||
334 | private function render_section( $section_slug, $heading, $downloads_data, $wp_plugins = array() ) { |
||
359 | |||
360 | /** |
||
361 | * Outputs the HTML of a single download |
||
362 | * |
||
363 | * @param array $download Download data, as returned from EDD API |
||
364 | * @param array $wp_plugins |
||
365 | * |
||
366 | * @return void |
||
367 | */ |
||
368 | protected function render_download( $download, $wp_plugins ) { |
||
410 | |||
411 | /** |
||
412 | * Generates details array for the download to keep the render_download() method a bit tidier |
||
413 | * |
||
414 | * @param array $download Single download, as returned by {@see get_downloads_data} |
||
415 | * @param array $wp_plugins All active plugins, as returned by {@see get_plugins()} |
||
416 | * |
||
417 | * @return array { |
||
418 | * @type array $download_info |
||
419 | * @type string $plugin_path |
||
420 | * @type string $status License status returned by Easy Digital Downloads ("active", "inactive", "expired", "revoked", etc) |
||
421 | * @type string $status_label |
||
422 | * @type string $button_title Title attribute to show when hovering over the download's button |
||
423 | * @type string $button_class CSS class to use for the button |
||
424 | * @type string $button_label Text to use for the download's anchor link |
||
425 | * @type string $href URL for the download's button |
||
426 | * @type bool $spinner Whether to show the spinner icon |
||
427 | * @type string $item_class CSS class for the download container |
||
428 | * @type string $required_license The name of the required license for the download ("All Access" or "Core + Extensions") |
||
429 | * @type bool $is_active Is the current GravityView license (as entered in Settings) active? |
||
430 | * } |
||
431 | */ |
||
432 | private function get_download_display_details( $download, $wp_plugins ) { |
||
532 | |||
533 | /** |
||
534 | * Returns the base price for an extension |
||
535 | * |
||
536 | * @param array $download |
||
537 | * |
||
538 | * @return float Base price for an extension. If not for sale separately, returns 0 |
||
539 | */ |
||
540 | private function get_download_base_price( $download ) { |
||
554 | |||
555 | /** |
||
556 | * Handle AJAX request to activate extension |
||
557 | * |
||
558 | * @return void Exits with JSON response |
||
559 | */ |
||
560 | public function activate_download() { |
||
577 | |||
578 | /** |
||
579 | * Handle AJAX request to deactivate extension |
||
580 | * |
||
581 | * @return void Send JSON response status and error message |
||
582 | */ |
||
583 | public function deactivate_download() { |
||
600 | |||
601 | /** |
||
602 | * Register and enqueue assets; localize script |
||
603 | * |
||
604 | * @return void |
||
605 | */ |
||
606 | public function maybe_enqueue_scripts_and_styles() { |
||
627 | } |
||
628 | |||
630 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.