Complex classes like GravityView_View_Data 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_View_Data, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class GravityView_View_Data { |
||
|
|
|||
| 9 | |||
| 10 | static $instance = NULL; |
||
| 11 | |||
| 12 | public $views = array(); |
||
| 13 | |||
| 14 | /** |
||
| 15 | * |
||
| 16 | * @param null $passed_post |
||
| 17 | */ |
||
| 18 | 60 | private function __construct( $passed_post = NULL ) { |
|
| 19 | 60 | $this->views = new \GV\View_Collection(); |
|
| 20 | |||
| 21 | 60 | if ( ! empty( $passed_post ) ) { |
|
| 22 | 24 | $id_or_id_array = $this->maybe_get_view_id( $passed_post ); |
|
| 23 | 24 | foreach( is_array( $id_or_id_array ) ? $id_or_id_array : array( $id_or_id_array ) as $view_id ) { |
|
| 24 | 24 | if ( \GV\View::exists( $view_id ) && ! $this->views->contains( $view_id ) ) { |
|
| 25 | $this->views->add( \GV\View::by_id( $view_id ) ); |
||
| 26 | } |
||
| 27 | } |
||
| 28 | } |
||
| 29 | |||
| 30 | 60 | } |
|
| 31 | |||
| 32 | /** |
||
| 33 | * @deprecated |
||
| 34 | * @see \GV\View_Collection::count |
||
| 35 | * @return boolean |
||
| 36 | */ |
||
| 37 | 25 | public function has_multiple_views() { |
|
| 40 | |||
| 41 | |||
| 42 | /** |
||
| 43 | * Figure out what the View ID is for a variable, if any. |
||
| 44 | * |
||
| 45 | * Can be: |
||
| 46 | * - WP_Post (Either a `gravityview` post type or not) |
||
| 47 | * - Multi-dimensional array of WP_Post objects |
||
| 48 | * - Array with `view_id` or `id` key(s) set |
||
| 49 | * - String of content that may include GravityView shortcode |
||
| 50 | * - Number representing the Post ID or View ID |
||
| 51 | * |
||
| 52 | * @param mixed $passed_post See method description |
||
| 53 | * |
||
| 54 | * @deprecated |
||
| 55 | * @see \GV\View_Collection::from_post and \GV\Shortcode::parse |
||
| 56 | * |
||
| 57 | * @return int|null|array ID of the View. If there are multiple views in the content, array of IDs parsed. |
||
| 58 | */ |
||
| 59 | 25 | public function maybe_get_view_id( $passed_post ) { |
|
| 60 | 25 | $ids = array(); |
|
| 61 | |||
| 62 | 25 | if ( ! empty( $passed_post ) ) { |
|
| 63 | |||
| 64 | 25 | if ( is_numeric( $passed_post ) ) { |
|
| 65 | $passed_post = get_post( $passed_post ); |
||
| 66 | } |
||
| 67 | |||
| 68 | // Convert WP_Posts into WP_Posts[] array |
||
| 69 | 25 | if ( $passed_post instanceof WP_Post ) { |
|
| 70 | 25 | $passed_post = array( $passed_post ); |
|
| 71 | } |
||
| 72 | |||
| 73 | 25 | if ( is_array( $passed_post ) ) { |
|
| 74 | |||
| 75 | 25 | foreach ( $passed_post as &$post ) { |
|
| 76 | 25 | $views = \GV\View_Collection::from_post( $post ); |
|
| 77 | 25 | foreach ( $views->all() as $view ) { |
|
| 78 | 25 | $ids []= $view->ID; |
|
| 79 | |||
| 80 | /** And as a side-effect... add each view to the global scope. */ |
||
| 81 | 25 | if ( ! $this->views->contains( $view->ID ) ) { |
|
| 82 | 25 | $this->views->add( $view ); |
|
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | } else { |
||
| 88 | |||
| 89 | 1 | if ( is_string( $passed_post ) ) { |
|
| 90 | 1 | $shortcodes = \GV\Shortcode::parse( $passed_post ); |
|
| 91 | 1 | foreach ( $shortcodes as $shortcode ) { |
|
| 92 | 1 | if ( $shortcode->name == 'gravityview' && !empty( $shortcode->atts['id'] ) ) { |
|
| 93 | 1 | $ids []= $shortcode->atts['id']; |
|
| 94 | |||
| 95 | /** And as a side-effect... add each view to the global scope. */ |
||
| 96 | 1 | if ( ! $this->views->contains( $shortcode->atts['id'] ) && \GV\View::exists( $shortcode->atts['id'] ) ) { |
|
| 97 | $this->views->add( $shortcode->atts['id'] ); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } else { |
||
| 102 | $id = $this->get_id_from_atts( $passed_post ); |
||
| 103 | $ids[] = intval( $id ); |
||
| 104 | } |
||
| 105 | } |
||
| 106 | } |
||
| 107 | |||
| 108 | 25 | if( empty($ids) ) { |
|
| 109 | return NULL; |
||
| 110 | } |
||
| 111 | |||
| 112 | // If it's just one ID, return that. |
||
| 113 | // Otherwise, return array of IDs |
||
| 114 | 25 | return ( sizeof( $ids ) === 1 ) ? $ids[0] : $ids; |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return GravityView_View_Data |
||
| 119 | */ |
||
| 120 | 63 | public static function getInstance( $passed_post = NULL ) { |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @deprecated |
||
| 131 | * @see \GV\View_Collection::all() |
||
| 132 | */ |
||
| 133 | 23 | function get_views() { |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @deprecated |
||
| 145 | * @see \GV\View_Collection::get() |
||
| 146 | */ |
||
| 147 | function get_view( $view_id, $atts = NULL ) { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Determines if a post, identified by the specified ID, exist |
||
| 172 | * within the WordPress database. |
||
| 173 | * |
||
| 174 | * @see http://tommcfarlin.com/wordpress-post-exists-by-id/ Fastest check available |
||
| 175 | * @param int $view_id The ID of the post to check |
||
| 176 | * |
||
| 177 | * @deprecated |
||
| 178 | 1 | * @see \GV\View::exists() |
|
| 179 | 1 | * |
|
| 180 | * @return bool True if the post exists; otherwise, false. |
||
| 181 | * @since 1.0.0 |
||
| 182 | */ |
||
| 183 | function view_exists( $view_id ) { |
||
| 186 | |||
| 187 | /** |
||
| 188 | * |
||
| 189 | * Add a view to the views array |
||
| 190 | * |
||
| 191 | * @param int|array $view_id View ID or array of View IDs |
||
| 192 | * @param array|string $atts Combine other attributes (eg. from shortcode) with the view settings (optional) |
||
| 193 | * |
||
| 194 | * @deprecated |
||
| 195 | * @see \GV\View_Collection::append |
||
| 196 | * |
||
| 197 | * @return array|false All views if $view_id is array, a view data array if $view_id is an int, false on errors. |
||
| 198 | */ |
||
| 199 | function add_view( $view_id, $atts = NULL ) { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Get the visible fields for a View |
||
| 205 | * @uses gravityview_get_directory_fields() Fetch the configured fields for a View |
||
| 206 | * @uses GravityView_View_Data::filter_fields() Only show visible fields |
||
| 207 | * @param int $view_id View ID |
||
| 208 | * |
||
| 209 | 1 | * @deprecated |
|
| 210 | 1 | * @see \GV\View::$fields |
|
| 211 | 1 | * |
|
| 212 | 1 | * @return array|null Array of fields as passed by `gravityview_get_directory_fields()` |
|
| 213 | */ |
||
| 214 | function get_fields( $view_id ) { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Retrieves view ID from an array. |
||
| 223 | * |
||
| 224 | 1 | * @param array $atts |
|
| 225 | 1 | * @deprecated Dead code, was probably superceded by GravityView_View_Data::parse_post_content |
|
| 226 | 1 | * |
|
| 227 | 1 | * @return int|null A view ID cast to int, or null. |
|
| 228 | 1 | */ |
|
| 229 | 1 | function get_id_from_atts( $atts ) { |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Parse content to determine if there is a GV shortcode to allow for enqueing necessary files in the head. |
||
| 239 | * |
||
| 240 | * @uses gravityview_has_shortcode_r() Check whether shortcode exists (recursively) |
||
| 241 | * @uses shortcode_parse_atts() Parse each GV shortcode |
||
| 242 | * @uses gravityview_get_template_settings() Get the settings for the View ID |
||
| 243 | * @param string $content $post->post_content content |
||
| 244 | * |
||
| 245 | 1 | * @deprecated |
|
| 246 | 1 | * @see \GV\View_Collection::from_content |
|
| 247 | 1 | * |
|
| 248 | 1 | * @return int|null|array If a single View is found, the ID of the View. If there are multiple views in the content, array of IDs parsed. If not found, NULL |
|
| 249 | 1 | */ |
|
| 250 | public function parse_post_content( $content ) { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Checks if the passed post id has the passed View id embedded. |
||
| 273 | * |
||
| 274 | * Returns |
||
| 275 | * |
||
| 276 | * @since 1.6.1 |
||
| 277 | * |
||
| 278 | * @param string $post_id Post ID where the View is embedded |
||
| 279 | 1 | * @param string $view_id View ID |
|
| 280 | * @param string $empty_is_valid If either $post_id or $view_id is empty consider valid. Default: false. |
||
| 281 | 1 | * |
|
| 282 | * @return bool|WP_Error If valid, returns true. If invalid, returns WP_Error containing error message. |
||
| 283 | */ |
||
| 284 | 1 | public static function is_valid_embed_id( $post_id = '', $view_id = '', $empty_is_valid = false ) { |
|
| 342 | |||
| 343 | /** |
||
| 344 | 1 | * Get a specific default setting |
|
| 345 | * @param string $key The key of the setting array item |
||
| 346 | 1 | * @param boolean $with_details Include details |
|
| 347 | * @return mixed|array If using $with_details, return array. Otherwise, mixed. |
||
| 348 | 1 | */ |
|
| 349 | public static function get_default_arg( $key, $with_details = false ) { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Retrieve the default args for shortcode and theme function |
||
| 362 | * |
||
| 363 | * @param boolean $with_details True: Return array with full default settings information, including description, name, etc. False: Return an array with only key => value pairs. |
||
| 364 | * @param string $group Only fetch |
||
| 365 | * |
||
| 366 | * @return array $args Associative array of default settings for a View |
||
| 367 | * @param[out] string $label Setting label shown in admin |
||
| 368 | * @param[out] string $type Gravity Forms field type |
||
| 369 | * @param[out] string $group The field group the setting is associated with. Default: "default" |
||
| 370 | * @param[out] mixed $value The default value for the setting |
||
| 371 | * @param[out] string $tooltip Tooltip displayed for the setting |
||
| 372 | * @param[out] boolean $show_in_shortcode Whether to show the setting in the shortcode configuration modal |
||
| 373 | * @param[out] array $options Array of values to use when generating select, multiselect, radio, or checkboxes fields |
||
| 374 | 102 | * @param[out] boolean $full_width True: Display the input and label together when rendering. False: Display label and input in separate columns when rendering. |
|
| 375 | 102 | * |
|
| 376 | * @deprecated |
||
| 377 | * @see \GV\View_Settings::defaults() |
||
| 378 | */ |
||
| 379 | public static function get_default_args( $with_details = false, $group = NULL ) { |
||
| 382 | } |
||
| 383 |