Complex classes like GravityView_Entry_Link_Shortcode 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_Entry_Link_Shortcode, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class GravityView_Entry_Link_Shortcode { |
||
11 | |||
12 | /** |
||
13 | * @type array Entry fetched using the $atts['entry_id'] shortcode setting. |
||
14 | * @since 1.15 |
||
15 | */ |
||
16 | private $entry = array(); |
||
17 | |||
18 | /** |
||
19 | * @type int If set, generate a link to the entry for this View ID. Required when used outside a View. Otherwise, current View ID is used. |
||
20 | * @since 1.15 |
||
21 | */ |
||
22 | private $view_id = 0; |
||
23 | |||
24 | /** |
||
25 | * @type array The accepted shortcode attribute pairs, with defaults set |
||
26 | * @since 1.15 |
||
27 | */ |
||
28 | static private $defaults = array( |
||
29 | 'action' => 'read', |
||
30 | 'view_id' => 0, |
||
31 | 'entry_id' => 0, |
||
32 | 'post_id' => 0, |
||
33 | 'link_atts' => '', |
||
34 | 'return' => 'html', |
||
35 | 'field_values' => '', |
||
36 | ); |
||
37 | |||
38 | /** |
||
39 | * @type array The final settings for the shortcode, after merging passed $atts with self::$defaults |
||
40 | * @since 1.15 |
||
41 | */ |
||
42 | private $settings = array(); |
||
43 | |||
44 | 2 | function __construct() { |
|
47 | |||
48 | /** |
||
49 | * Add shortcodes |
||
50 | * |
||
51 | * @since 1.15 |
||
52 | */ |
||
53 | 2 | private function add_hooks() { |
|
58 | |||
59 | /** |
||
60 | * @since 1.15 |
||
61 | * @copydoc GravityView_Entry_Link_Shortcode::shortcode |
||
62 | */ |
||
63 | 1 | public function read_shortcode( $atts, $content = null, $context = 'gv_entry_link' ) { |
|
66 | |||
67 | /** |
||
68 | * Backward compatibility for existing `gv_edit_entry_link` shortcode |
||
69 | * Forces $atts['action'] to "edit" |
||
70 | * |
||
71 | * @since 1.15 |
||
72 | * @copydoc GravityView_Entry_Link_Shortcode::shortcode |
||
73 | */ |
||
74 | public function edit_shortcode( $atts = array(), $content = null, $context = 'gv_edit_entry_link' ) { |
||
82 | |||
83 | /** |
||
84 | * Backward compatibility for existing `gv_delete_entry_link` shortcodes |
||
85 | * Forces $atts['action'] to "delete" |
||
86 | * |
||
87 | * @since 1.15 |
||
88 | * @copydoc GravityView_Entry_Link_Shortcode::shortcode |
||
89 | */ |
||
90 | public function delete_shortcode( $atts = array(), $content = null, $context = 'gv_delete_entry_link' ) { |
||
98 | |||
99 | /** |
||
100 | * Generate a link to an entry. The link can be an edit, delete, or standard link. |
||
101 | * |
||
102 | * @since 1.15 |
||
103 | * |
||
104 | * @param array $atts { |
||
105 | * @type string $action What type of link to generate. Options: `read`, `edit`, and `delete`. Default: `read` |
||
106 | * @type string $view_id Define the ID for the View. If not set, use current View ID, if exists. |
||
107 | * @type string $entry_id ID of the entry to edit. If undefined, uses the current entry ID, if exists. |
||
108 | * @type string $post_id ID of the base post or page to use for an embedded View |
||
109 | * @type string $link_atts Pass anchor tag attributes (`target=_blank` to open Edit Entry link in a new window, for example) |
||
110 | * @type string $return What should the shortcode return: link HTML (`html`) or the URL (`url`). Default: `html` |
||
111 | * @type string $field_values Only used for `action="edit"`. Parameters to pass in to the prefill data in Edit Entry form. Uses the same format as Gravity Forms "Allow field to be populated dynamically" {@see https://www.gravityhelp.com/documentation/article/allow-field-to-be-populated-dynamically/ } |
||
112 | * } |
||
113 | * |
||
114 | * @param string|null $content Used as link anchor text, if specified. |
||
115 | * @param string $context Current shortcode being called. Not used. |
||
116 | * |
||
117 | * @return null|string If admin or an error occurred, returns null. Otherwise, returns entry link output. If `$atts['return']` is 'url', the entry link URL. Otherwise, entry link `<a>` HTML tag. |
||
118 | */ |
||
119 | 2 | private function shortcode( $atts, $content = null, $context = 'gv_entry_link' ) { |
|
194 | |||
195 | /** |
||
196 | * Parse shortcode atts to fetch `link_atts`, which will be added to the output of the HTML anchor tag generated by shortcode |
||
197 | * Only used when `return` value of shortcode is not "url" |
||
198 | * |
||
199 | * @since 1.15 |
||
200 | * @see gravityview_get_link() See acceptable attributes here |
||
201 | * @return array Array of attributes to be added |
||
202 | */ |
||
203 | 1 | private function get_link_atts() { |
|
213 | |||
214 | /** |
||
215 | * Get the anchor text for the link. If content inside shortcode is defined, use that as the text. Otherwise, use default values. |
||
216 | * |
||
217 | * Only used when `return` value of shortcode is not "url" |
||
218 | * |
||
219 | * @since 1.15 |
||
220 | * |
||
221 | * @param string|null $content Content inside shortcode, if defined |
||
222 | * |
||
223 | * @return string Text to use for HTML anchor |
||
224 | */ |
||
225 | 1 | private function get_anchor_text( $content = null ) { |
|
244 | |||
245 | /** |
||
246 | * Get the URL for the entry. |
||
247 | * |
||
248 | * Uses the `post_id`, `view_id` params as defined in the shortcode attributes. |
||
249 | * |
||
250 | * @since 1.15 |
||
251 | * |
||
252 | * @param string|null $content Content inside shortcode, if defined |
||
253 | * |
||
254 | * @return string|boolean If URL is fetched, the URL to the entry link. If not found, returns false. |
||
255 | */ |
||
256 | 1 | private function get_url() { |
|
277 | |||
278 | /** |
||
279 | * Check whether the user has the capability to see the shortcode output, depending on the action ('read', 'edit', 'delete') |
||
280 | * |
||
281 | * @since 1.15 |
||
282 | * @return bool True: has cap. |
||
283 | */ |
||
284 | 1 | private function has_cap() { |
|
300 | |||
301 | /** |
||
302 | * Get entry array from `entry_id` parameter. If no $entry_id |
||
303 | * |
||
304 | * @since 1.15 |
||
305 | * @uses GVCommon::get_entry |
||
306 | * @uses GravityView_frontend::getSingleEntry |
||
307 | * |
||
308 | * @param int $entry_id Gravity Forms Entry ID. If not passed, current View's current entry ID will be used, if found. |
||
309 | * |
||
310 | * @return array|bool Gravity Forms array, if found. Otherwise, false. |
||
311 | */ |
||
312 | 1 | private function get_entry( $entry_id = 0 ) { |
|
340 | |||
341 | /** |
||
342 | * Allow passing URL params to dynamically populate the Edit Entry form |
||
343 | * If `field_values` key is set, run it through `parse_str()` and add the values to $url |
||
344 | * |
||
345 | * @since 1.15 |
||
346 | * |
||
347 | * @param string $href URL |
||
348 | */ |
||
349 | 1 | private function maybe_add_field_values_query_args( $url ) { |
|
360 | } |
||
361 | |||
362 | new GravityView_Entry_Link_Shortcode; |
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.