Complex classes like View 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 View, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 17 | class View implements \ArrayAccess { | 
            ||
| 18 | |||
| 19 | /**  | 
            ||
| 20 | * @var \WP_Post The backing post instance.  | 
            ||
| 21 | */  | 
            ||
| 22 | private $post;  | 
            ||
| 23 | |||
| 24 | /**  | 
            ||
| 25 | * @var \GV\View_Settings The settings.  | 
            ||
| 26 | *  | 
            ||
| 27 | * @api  | 
            ||
| 28 | * @since 2.0  | 
            ||
| 29 | */  | 
            ||
| 30 | public $settings;  | 
            ||
| 31 | |||
| 32 | /**  | 
            ||
| 33 | * @var \GV\Widget_Collection The widets attached here.  | 
            ||
| 34 | *  | 
            ||
| 35 | * @api  | 
            ||
| 36 | * @since 2.0  | 
            ||
| 37 | */  | 
            ||
| 38 | public $widgets;  | 
            ||
| 39 | |||
| 40 | /**  | 
            ||
| 41 | * @var \GV\GF_Form|\GV\Form The backing form for this view.  | 
            ||
| 42 | *  | 
            ||
| 43 | * Contains the form that is sourced for entries in this view.  | 
            ||
| 44 | *  | 
            ||
| 45 | * @api  | 
            ||
| 46 | * @since 2.0  | 
            ||
| 47 | */  | 
            ||
| 48 | public $form;  | 
            ||
| 49 | |||
| 50 | /**  | 
            ||
| 51 | * @var \GV\Field_Collection The fields for this view.  | 
            ||
| 52 | *  | 
            ||
| 53 | * Contains all the fields that are attached to this view.  | 
            ||
| 54 | *  | 
            ||
| 55 | * @api  | 
            ||
| 56 | * @since 2.0  | 
            ||
| 57 | */  | 
            ||
| 58 | public $fields;  | 
            ||
| 59 | |||
| 60 | /**  | 
            ||
| 61 | * @var array  | 
            ||
| 62 | *  | 
            ||
| 63 | * Internal static cache for gets, and whatnot.  | 
            ||
| 64 | * This is not persistent, resets across requests.  | 
            ||
| 65 | |||
| 66 | * @internal  | 
            ||
| 67 | */  | 
            ||
| 68 | private static $cache = array();  | 
            ||
| 69 | |||
| 70 | /**  | 
            ||
| 71 | * @var \GV\Join[] The joins for all sources in this view.  | 
            ||
| 72 | *  | 
            ||
| 73 | * @api  | 
            ||
| 74 | * @since future  | 
            ||
| 75 | */  | 
            ||
| 76 | public $joins = array();  | 
            ||
| 77 | |||
| 78 | /**  | 
            ||
| 79 | * The constructor.  | 
            ||
| 80 | */  | 
            ||
| 81 | 71 | 	public function __construct() { | 
            |
| 86 | |||
| 87 | /**  | 
            ||
| 88 | * Register the gravityview WordPress Custom Post Type.  | 
            ||
| 89 | *  | 
            ||
| 90 | * @internal  | 
            ||
| 91 | * @return void  | 
            ||
| 92 | */  | 
            ||
| 93 | 	public static function register_post_type() { | 
            ||
| 197 | |||
| 198 | /**  | 
            ||
| 199 | * A renderer filter for the View post type content.  | 
            ||
| 200 | *  | 
            ||
| 201 | * @param string $content Should be empty, as we don't store anything there.  | 
            ||
| 202 | *  | 
            ||
| 203 | * @return string $content The view content as output by the renderers.  | 
            ||
| 204 | */  | 
            ||
| 205 | 8 | 	public static function content( $content ) { | 
            |
| 206 | 8 | $request = gravityview()->request;  | 
            |
| 207 | |||
| 208 | // Plugins may run through the content in the header. WP SEO does this for its OpenGraph functionality.  | 
            ||
| 209 | 8 | 		if ( ! defined( 'DOING_GRAVITYVIEW_TESTS' ) ) { | 
            |
| 210 | 			if ( ! did_action( 'loop_start' ) ) { | 
            ||
| 211 | 				gravityview()->log->debug( 'Not processing yet: loop_start hasn\'t run yet. Current action: {action}', array( 'action' => current_filter() ) ); | 
            ||
| 212 | return $content;  | 
            ||
| 213 | }  | 
            ||
| 214 | |||
| 215 | // We don't want this filter to run infinite loop on any post content fields  | 
            ||
| 216 | remove_filter( 'the_content', array( __CLASS__, __METHOD__ ) );  | 
            ||
| 217 | }  | 
            ||
| 218 | |||
| 219 | /**  | 
            ||
| 220 | * This is not a View. Bail.  | 
            ||
| 221 | *  | 
            ||
| 222 | * Shortcodes and oEmbeds and whatnot will be handled  | 
            ||
| 223 | * elsewhere.  | 
            ||
| 224 | */  | 
            ||
| 225 | 8 | 		if ( ! $view = $request->is_view() ) { | 
            |
| 226 | 5 | return $content;  | 
            |
| 227 | }  | 
            ||
| 228 | |||
| 229 | /**  | 
            ||
| 230 | * This View is password protected. Nothing to do here.  | 
            ||
| 231 | */  | 
            ||
| 232 | 3 | 		if ( post_password_required( $view->ID ) ) { | 
            |
| 233 | 1 | 			gravityview()->log->notice( 'Post password is required for View #{view_id}', array( 'view_id' => $view->ID ) ); | 
            |
| 234 | 1 | return get_the_password_form( $view->ID );  | 
            |
| 235 | }  | 
            ||
| 236 | |||
| 237 | 3 | 		if ( ! $view->form ) { | 
            |
| 238 | 			gravityview()->log->notice( 'View #{id} has no form attached to it.', array( 'id' => $view->ID ) ); | 
            ||
| 239 | |||
| 240 | /**  | 
            ||
| 241 | * This View has no data source. There's nothing to show really.  | 
            ||
| 242 | * ...apart from a nice message if the user can do anything about it.  | 
            ||
| 243 | */  | 
            ||
| 244 | 			if ( \GVCommon::has_cap( array( 'edit_gravityviews', 'edit_gravityview' ), $view->ID ) ) { | 
            ||
| 245 | return __( sprintf( 'This View is not configured properly. Start by <a href="%s">selecting a form</a>.', esc_url( get_edit_post_link( $view->ID, false ) ) ), 'gravityview' );  | 
            ||
| 246 | }  | 
            ||
| 247 | |||
| 248 | return $content;  | 
            ||
| 249 | }  | 
            ||
| 250 | |||
| 251 | /**  | 
            ||
| 252 | * Is this View directly accessible via a post URL?  | 
            ||
| 253 | *  | 
            ||
| 254 | * @see https://codex.wordpress.org/Function_Reference/register_post_type#public  | 
            ||
| 255 | */  | 
            ||
| 256 | |||
| 257 | /**  | 
            ||
| 258 | * @filter `gravityview_direct_access` Should Views be directly accessible, or only visible using the shortcode?  | 
            ||
| 259 | * @deprecated  | 
            ||
| 260 | * @param[in,out] boolean `true`: allow Views to be accessible directly. `false`: Only allow Views to be embedded. Default: `true`  | 
            ||
| 261 | * @param int $view_id The ID of the View currently being requested. `0` for general setting  | 
            ||
| 262 | */  | 
            ||
| 263 | 3 | $direct_access = apply_filters( 'gravityview_direct_access', true, $view->ID );  | 
            |
| 264 | |||
| 265 | /**  | 
            ||
| 266 | * @filter `gravityview/request/output/direct` Should this View be directly accessbile?  | 
            ||
| 267 | * @since 2.0  | 
            ||
| 268 | * @param[in,out] boolean Accessible or not. Default: accessbile.  | 
            ||
| 269 | * @param \GV\View $view The View we're trying to directly render here.  | 
            ||
| 270 | * @param \GV\Request $request The current request.  | 
            ||
| 271 | */  | 
            ||
| 272 | 3 | 		if ( ! apply_filters( 'gravityview/view/output/direct', $direct_access, $view, $request ) ) { | 
            |
| 273 | return __( 'You are not allowed to view this content.', 'gravityview' );  | 
            ||
| 274 | }  | 
            ||
| 275 | |||
| 276 | /**  | 
            ||
| 277 | * Is this View an embed-only View? If so, don't allow rendering here,  | 
            ||
| 278 | * as this is a direct request.  | 
            ||
| 279 | */  | 
            ||
| 280 | 3 | 		if ( $view->settings->get( 'embed_only' ) && ! \GVCommon::has_cap( 'read_private_gravityviews' ) ) { | 
            |
| 281 | return __( 'You are not allowed to view this content.', 'gravityview' );  | 
            ||
| 282 | }  | 
            ||
| 283 | |||
| 284 | /** Private, pending, draft, etc. */  | 
            ||
| 285 | 3 | $public_states = get_post_stati( array( 'public' => true ) );  | 
            |
| 286 | 3 | 		if ( ! in_array( $view->post_status, $public_states ) && ! \GVCommon::has_cap( 'read_gravityview', $view->ID ) ) { | 
            |
| 287 | 			gravityview()->log->notice( 'The current user cannot access this View #{view_id}', array( 'view_id' => $view->ID ) ); | 
            ||
| 288 | return __( 'You are not allowed to view this content.', 'gravityview' );  | 
            ||
| 289 | }  | 
            ||
| 290 | |||
| 291 | 3 | 		$is_admin_and_can_view = $view->settings->get( 'admin_show_all_statuses' ) && \GVCommon::has_cap('gravityview_moderate_entries', $view->ID ); | 
            |
| 292 | |||
| 293 | /**  | 
            ||
| 294 | * Editing a single entry.  | 
            ||
| 295 | */  | 
            ||
| 296 | 3 | 		if ( $entry = $request->is_edit_entry() ) { | 
            |
| 297 | 			if ( $entry['status'] != 'active' ) { | 
            ||
| 298 | 				gravityview()->log->notice( 'Entry ID #{entry_id} is not active', array( 'entry_id' => $entry->ID ) ); | 
            ||
| 299 | return __( 'You are not allowed to view this content.', 'gravityview' );  | 
            ||
| 300 | }  | 
            ||
| 301 | |||
| 302 | 			if ( apply_filters( 'gravityview_custom_entry_slug', false ) && $entry->slug != get_query_var( \GV\Entry::get_endpoint_name() ) ) { | 
            ||
| 303 | 				gravityview()->log->error( 'Entry ID #{entry_id} was accessed by a bad slug', array( 'entry_id' => $entry->ID ) ); | 
            ||
| 304 | return __( 'You are not allowed to view this content.', 'gravityview' );  | 
            ||
| 305 | }  | 
            ||
| 306 | |||
| 307 | 			if ( $view->settings->get( 'show_only_approved' ) && ! $is_admin_and_can_view ) { | 
            ||
| 308 | 				if ( ! \GravityView_Entry_Approval_Status::is_approved( gform_get_meta( $entry->ID, \GravityView_Entry_Approval::meta_key ) )  ) { | 
            ||
| 309 | 					gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing', array( 'entry_id' => $entry->ID ) ); | 
            ||
| 310 | return __( 'You are not allowed to view this content.', 'gravityview' );  | 
            ||
| 311 | }  | 
            ||
| 312 | }  | 
            ||
| 313 | |||
| 314 | $renderer = new Edit_Entry_Renderer();  | 
            ||
| 315 | return $renderer->render( $entry, $view, $request );  | 
            ||
| 316 | |||
| 317 | /**  | 
            ||
| 318 | * Viewing a single entry.  | 
            ||
| 319 | */  | 
            ||
| 320 | 3 | 		} else if ( $entry = $request->is_entry() ) { | 
            |
| 321 | 1 | 			if ( $entry['status'] != 'active' ) { | 
            |
| 322 | 1 | 				gravityview()->log->notice( 'Entry ID #{entry_id} is not active', array( 'entry_id' => $entry->ID ) ); | 
            |
| 323 | 1 | return __( 'You are not allowed to view this content.', 'gravityview' );  | 
            |
| 324 | }  | 
            ||
| 325 | |||
| 326 | 1 | 			if ( apply_filters( 'gravityview_custom_entry_slug', false ) && $entry->slug != get_query_var( \GV\Entry::get_endpoint_name() ) ) { | 
            |
| 327 | 1 | 				gravityview()->log->error( 'Entry ID #{entry_id} was accessed by a bad slug', array( 'entry_id' => $entry->ID ) ); | 
            |
| 328 | 1 | return __( 'You are not allowed to view this content.', 'gravityview' );  | 
            |
| 329 | }  | 
            ||
| 330 | |||
| 331 | 1 | 			if ( $view->settings->get( 'show_only_approved' ) && ! $is_admin_and_can_view ) { | 
            |
| 332 | 1 | 				if ( ! \GravityView_Entry_Approval_Status::is_approved( gform_get_meta( $entry->ID, \GravityView_Entry_Approval::meta_key ) )  ) { | 
            |
| 333 | 1 | 					gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing', array( 'entry_id' => $entry->ID ) ); | 
            |
| 334 | 1 | return __( 'You are not allowed to view this content.', 'gravityview' );  | 
            |
| 335 | }  | 
            ||
| 336 | }  | 
            ||
| 337 | |||
| 338 | 1 | $error = \GVCommon::check_entry_display( $entry->as_entry() );  | 
            |
| 339 | |||
| 340 | 1 | 			if( is_wp_error( $error ) ) { | 
            |
| 341 | 				gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing: {message}', array( 'entry_id' => $entry->ID, 'message' => $error->get_error_message() ) ); | 
            ||
| 342 | return __( 'You are not allowed to view this content.', 'gravityview' );  | 
            ||
| 343 | }  | 
            ||
| 344 | |||
| 345 | 1 | $renderer = new Entry_Renderer();  | 
            |
| 346 | 1 | return $renderer->render( $entry, $view, $request );  | 
            |
| 347 | |||
| 348 | /**  | 
            ||
| 349 | * Plain old View.  | 
            ||
| 350 | */  | 
            ||
| 351 | 		} else { | 
            ||
| 352 | 2 | $renderer = new View_Renderer();  | 
            |
| 353 | 2 | return $renderer->render( $view, $request );  | 
            |
| 354 | }  | 
            ||
| 355 | |||
| 356 | return $content;  | 
            ||
| 357 | }  | 
            ||
| 358 | |||
| 359 | /**  | 
            ||
| 360 | * Get joins associated with a view  | 
            ||
| 361 | *  | 
            ||
| 362 | * @param \WP_Post $post  | 
            ||
| 363 | *  | 
            ||
| 364 | * @api  | 
            ||
| 365 | * @since 2.0  | 
            ||
| 366 | * @return \GV\Join[]|null Array of \GV\Join instances  | 
            ||
| 367 | */  | 
            ||
| 368 | 71 | 	public static function get_joins( $post ) { | 
            |
| 404 | |||
| 405 | /**  | 
            ||
| 406 | * Get joined forms associated with a view  | 
            ||
| 407 | *  | 
            ||
| 408 | * @param $post_id  | 
            ||
| 409 | *  | 
            ||
| 410 | * @api  | 
            ||
| 411 | * @since 2.0  | 
            ||
| 412 | * @return \GV\GF_Form[]|null Array of \GV\GF_Form instances  | 
            ||
| 413 | */  | 
            ||
| 414 | 	public static function get_joined_forms( $post_id ) { | 
            ||
| 415 | 		if ( ! $post_id || ! gravityview()->plugin->supports( Plugin::FEATURE_JOINS ) ) { | 
            ||
| 416 | return null;  | 
            ||
| 417 | }  | 
            ||
| 418 | |||
| 419 | $forms_ids = array();  | 
            ||
| 420 | |||
| 421 | $joins_meta = get_post_meta( $post_id, '_gravityview_form_joins', true );  | 
            ||
| 422 | |||
| 423 | 		if ( empty( $joins_meta ) ) { | 
            ||
| 424 | return null;  | 
            ||
| 425 | }  | 
            ||
| 426 | |||
| 427 | 		foreach ( $joins_meta  as $meta ) { | 
            ||
| 428 | 			if ( ! is_array( $meta ) || count( $meta ) != 4 ) { | 
            ||
| 429 | continue;  | 
            ||
| 430 | }  | 
            ||
| 431 | |||
| 432 | list( $join, $join_column, $join_on, $join_on_column ) = $meta;  | 
            ||
| 433 | |||
| 434 | $forms_ids [] = GF_Form::by_id( $join_on );  | 
            ||
| 435 | }  | 
            ||
| 436 | |||
| 437 | return ( !empty( $forms_ids) ) ? $forms_ids : null;  | 
            ||
| 438 | }  | 
            ||
| 439 | |||
| 440 | /**  | 
            ||
| 441 | * Construct a \GV\View instance from a \WP_Post.  | 
            ||
| 442 | *  | 
            ||
| 443 | * @param \WP_Post $post The \WP_Post instance to wrap.  | 
            ||
| 444 | *  | 
            ||
| 445 | * @api  | 
            ||
| 446 | * @since 2.0  | 
            ||
| 447 | * @return \GV\View|null An instance around this \WP_Post if valid, null otherwise.  | 
            ||
| 448 | */  | 
            ||
| 449 | 72 | 	public static function from_post( $post ) { | 
            |
| 450 | 72 | 		if ( ! $post || get_post_type( $post ) != 'gravityview' ) { | 
            |
| 451 | 1 | gravityview()->log->error( 'Only gravityview post types can be \GV\View instances.' );  | 
            |
| 452 | 1 | return null;  | 
            |
| 453 | }  | 
            ||
| 454 | |||
| 455 | 72 | 		if ( $view = Utils::get( self::$cache, "View::from_post:{$post->ID}" ) ) { | 
            |
| 456 | 31 | return $view;  | 
            |
| 457 | }  | 
            ||
| 458 | |||
| 459 | 72 | $view = new self();  | 
            |
| 460 | 72 | $view->post = $post;  | 
            |
| 461 | |||
| 462 | /** Get connected form. */  | 
            ||
| 463 | 72 | $view->form = GF_Form::by_id( $view->_gravityview_form_id );  | 
            |
| 464 | 72 | 		if ( ! $view->form ) { | 
            |
| 465 | 			gravityview()->log->error( 'View #{view_id} tried attaching non-existent Form #{form_id} to it.', array( | 
            ||
| 466 | 'view_id' => $view->ID,  | 
            ||
| 467 | 'form_id' => $view->_gravityview_form_id ? : 0,  | 
            ||
| 468 | ) );  | 
            ||
| 469 | }  | 
            ||
| 470 | |||
| 471 | 72 | $view->joins = $view->get_joins( $post );  | 
            |
| 472 | |||
| 473 | /**  | 
            ||
| 474 | * @filter `gravityview/configuration/fields` Filter the View fields' configuration array.  | 
            ||
| 475 | * @since 1.6.5  | 
            ||
| 476 | *  | 
            ||
| 477 | * @deprecated Use `gravityview/view/configuration/fields` or `gravityview/view/fields` filters.  | 
            ||
| 478 | *  | 
            ||
| 479 | * @param $fields array Multi-array of fields with first level being the field zones.  | 
            ||
| 480 | * @param $view_id int The View the fields are being pulled for.  | 
            ||
| 481 | */  | 
            ||
| 482 | 72 | $configuration = apply_filters( 'gravityview/configuration/fields', (array)$view->_gravityview_directory_fields, $view->ID );  | 
            |
| 483 | |||
| 484 | /**  | 
            ||
| 485 | * @filter `gravityview/view/configuration/fields` Filter the View fields' configuration array.  | 
            ||
| 486 | * @since 2.0  | 
            ||
| 487 | *  | 
            ||
| 488 | * @param array $fields Multi-array of fields with first level being the field zones.  | 
            ||
| 489 | * @param \GV\View $view The View the fields are being pulled for.  | 
            ||
| 490 | */  | 
            ||
| 491 | 72 | $configuration = apply_filters( 'gravityview/view/configuration/fields', $configuration, $view );  | 
            |
| 492 | |||
| 493 | /**  | 
            ||
| 494 | * @filter `gravityview/view/fields` Filter the Field Collection for this View.  | 
            ||
| 495 | * @since 2.0  | 
            ||
| 496 | *  | 
            ||
| 497 | * @param \GV\Field_Collection $fields A collection of fields.  | 
            ||
| 498 | * @param \GV\View $view The View the fields are being pulled for.  | 
            ||
| 499 | */  | 
            ||
| 500 | 72 | $view->fields = apply_filters( 'gravityview/view/fields', Field_Collection::from_configuration( $configuration ), $view );  | 
            |
| 501 | |||
| 502 | /**  | 
            ||
| 503 | * @filter `gravityview/view/configuration/widgets` Filter the View widgets' configuration array.  | 
            ||
| 504 | * @since 2.0  | 
            ||
| 505 | *  | 
            ||
| 506 | * @param array $fields Multi-array of widgets with first level being the field zones.  | 
            ||
| 507 | * @param \GV\View $view The View the widgets are being pulled for.  | 
            ||
| 508 | */  | 
            ||
| 509 | 72 | $configuration = apply_filters( 'gravityview/view/configuration/widgets', (array)$view->_gravityview_directory_widgets, $view );  | 
            |
| 510 | |||
| 511 | /**  | 
            ||
| 512 | * @filter `gravityview/view/widgets` Filter the Widget Collection for this View.  | 
            ||
| 513 | * @since 2.0  | 
            ||
| 514 | *  | 
            ||
| 515 | * @param \GV\Widget_Collection $widgets A collection of widgets.  | 
            ||
| 516 | * @param \GV\View $view The View the widgets are being pulled for.  | 
            ||
| 517 | */  | 
            ||
| 518 | 72 | $view->widgets = apply_filters( 'gravityview/view/widgets', Widget_Collection::from_configuration( $configuration ), $view );  | 
            |
| 519 | |||
| 520 | /** View configuration. */  | 
            ||
| 521 | 72 | $view->settings->update( gravityview_get_template_settings( $view->ID ) );  | 
            |
| 522 | |||
| 523 | /** Add the template name into the settings. */  | 
            ||
| 524 | 72 | $view->settings->update( array( 'template' => gravityview_get_template_id( $view->ID ) ) );  | 
            |
| 525 | |||
| 526 | /** View basics. */  | 
            ||
| 527 | 72 | $view->settings->update( array(  | 
            |
| 528 | 72 | 'id' => $view->ID,  | 
            |
| 529 | ) );  | 
            ||
| 530 | |||
| 531 | 72 | 		self::$cache[ "View::from_post:{$post->ID}" ] = &$view; | 
            |
| 532 | |||
| 533 | 72 | return $view;  | 
            |
| 534 | }  | 
            ||
| 535 | |||
| 536 | /**  | 
            ||
| 537 | * Flush the view cache.  | 
            ||
| 538 | *  | 
            ||
| 539 | * @param int $view_id The View to reset cache for. Optional. Default: resets everything.  | 
            ||
| 540 | *  | 
            ||
| 541 | * @internal  | 
            ||
| 542 | */  | 
            ||
| 543 | 83 | 	public static function _flush_cache( $view_id = null ) { | 
            |
| 544 | 83 | 		if ( $view_id ) { | 
            |
| 545 | 76 | unset( self::$cache[ "View::from_post:$view_id" ] );  | 
            |
| 546 | 76 | return;  | 
            |
| 547 | }  | 
            ||
| 548 | 65 | self::$cache = array();  | 
            |
| 549 | 65 | }  | 
            |
| 550 | |||
| 551 | /**  | 
            ||
| 552 | * Construct a \GV\View instance from a post ID.  | 
            ||
| 553 | *  | 
            ||
| 554 | * @param int|string $post_id The post ID.  | 
            ||
| 555 | *  | 
            ||
| 556 | * @api  | 
            ||
| 557 | * @since 2.0  | 
            ||
| 558 | * @return \GV\View|null An instance around this \WP_Post or null if not found.  | 
            ||
| 559 | */  | 
            ||
| 560 | 32 | 	public static function by_id( $post_id ) { | 
            |
| 561 | 32 | 		if ( ! $post_id || ! $post = get_post( $post_id ) ) { | 
            |
| 562 | 3 | return null;  | 
            |
| 563 | }  | 
            ||
| 564 | 32 | return self::from_post( $post );  | 
            |
| 565 | }  | 
            ||
| 566 | |||
| 567 | /**  | 
            ||
| 568 | * Determines if a view exists to begin with.  | 
            ||
| 569 | *  | 
            ||
| 570 | * @param int|\WP_Post|null $view The WordPress post ID, a \WP_Post object or null for global $post;  | 
            ||
| 571 | *  | 
            ||
| 572 | * @api  | 
            ||
| 573 | * @since 2.0  | 
            ||
| 574 | * @return bool Whether the post exists or not.  | 
            ||
| 575 | */  | 
            ||
| 576 | 4 | 	public static function exists( $view ) { | 
            |
| 577 | 4 | return get_post_type( $view ) == 'gravityview';  | 
            |
| 578 | }  | 
            ||
| 579 | |||
| 580 | /**  | 
            ||
| 581 | * ArrayAccess compatibility layer with GravityView_View_Data::$views  | 
            ||
| 582 | *  | 
            ||
| 583 | * @internal  | 
            ||
| 584 | * @deprecated  | 
            ||
| 585 | * @since 2.0  | 
            ||
| 586 | * @return bool Whether the offset exists or not, limited to GravityView_View_Data::$views element keys.  | 
            ||
| 587 | */  | 
            ||
| 588 | 4 | 	public function offsetExists( $offset ) { | 
            |
| 589 | 4 | $data_keys = array( 'id', 'view_id', 'form_id', 'template_id', 'atts', 'fields', 'widgets', 'form' );  | 
            |
| 590 | 4 | return in_array( $offset, $data_keys );  | 
            |
| 591 | }  | 
            ||
| 592 | |||
| 593 | /**  | 
            ||
| 594 | * ArrayAccess compatibility layer with GravityView_View_Data::$views  | 
            ||
| 595 | *  | 
            ||
| 596 | * Maps the old keys to the new data;  | 
            ||
| 597 | *  | 
            ||
| 598 | * @internal  | 
            ||
| 599 | * @deprecated  | 
            ||
| 600 | * @since 2.0  | 
            ||
| 601 | *  | 
            ||
| 602 | * @return mixed The value of the requested view data key limited to GravityView_View_Data::$views element keys.  | 
            ||
| 603 | */  | 
            ||
| 604 | 4 | 	public function offsetGet( $offset ) { | 
            |
| 605 | |||
| 606 | 4 | gravityview()->log->notice( 'This is a \GV\View object should not be accessed as an array.' );  | 
            |
| 607 | |||
| 608 | 4 | 		if ( ! isset( $this[ $offset ] ) ) { | 
            |
| 609 | return null;  | 
            ||
| 610 | }  | 
            ||
| 611 | |||
| 612 | 4 | 		switch ( $offset ) { | 
            |
| 613 | 4 | case 'id':  | 
            |
| 614 | 4 | case 'view_id':  | 
            |
| 615 | 1 | return $this->ID;  | 
            |
| 616 | 4 | case 'form':  | 
            |
| 617 | 4 | return $this->form;  | 
            |
| 618 | 1 | case 'form_id':  | 
            |
| 619 | 1 | return $this->form ? $this->form->ID : null;  | 
            |
| 620 | 1 | case 'atts':  | 
            |
| 621 | return $this->settings->as_atts();  | 
            ||
| 622 | 1 | case 'template_id':  | 
            |
| 623 | 1 | return $this->settings->get( 'template' );  | 
            |
| 624 | case 'widgets':  | 
            ||
| 625 | return $this->widgets->as_configuration();  | 
            ||
| 626 | }  | 
            ||
| 627 | }  | 
            ||
| 628 | |||
| 629 | /**  | 
            ||
| 630 | * ArrayAccess compatibility layer with GravityView_View_Data::$views  | 
            ||
| 631 | *  | 
            ||
| 632 | * @internal  | 
            ||
| 633 | * @deprecated  | 
            ||
| 634 | * @since 2.0  | 
            ||
| 635 | *  | 
            ||
| 636 | * @return void  | 
            ||
| 637 | */  | 
            ||
| 638 | 1 | 	public function offsetSet( $offset, $value ) { | 
            |
| 639 | 1 | gravityview()->log->error( 'The old view data is no longer mutable. This is a \GV\View object should not be accessed as an array.' );  | 
            |
| 640 | 1 | }  | 
            |
| 641 | |||
| 642 | /**  | 
            ||
| 643 | * ArrayAccess compatibility layer with GravityView_View_Data::$views  | 
            ||
| 644 | *  | 
            ||
| 645 | * @internal  | 
            ||
| 646 | * @deprecated  | 
            ||
| 647 | * @since 2.0  | 
            ||
| 648 | * @return void  | 
            ||
| 649 | */  | 
            ||
| 650 | 1 | 	public function offsetUnset( $offset ) { | 
            |
| 653 | |||
| 654 | /**  | 
            ||
| 655 | * Be compatible with the old data object.  | 
            ||
| 656 | *  | 
            ||
| 657 | * Some external code expects an array (doing things like foreach on this, or array_keys)  | 
            ||
| 658 | * so let's return an array in the old format for such cases. Do not use unless using  | 
            ||
| 659 | * for back-compatibility.  | 
            ||
| 660 | *  | 
            ||
| 661 | * @internal  | 
            ||
| 662 | * @deprecated  | 
            ||
| 663 | * @since 2.0  | 
            ||
| 664 | * @return array  | 
            ||
| 665 | */  | 
            ||
| 666 | 4 | 	public function as_data() { | 
            |
| 678 | |||
| 679 | /**  | 
            ||
| 680 | * Retrieve the entries for the current view and request.  | 
            ||
| 681 | *  | 
            ||
| 682 | * @param \GV\Request The request. Usued for now.  | 
            ||
| 683 | *  | 
            ||
| 684 | * @return \GV\Entry_Collection The entries.  | 
            ||
| 685 | */  | 
            ||
| 686 | 21 | 	public function get_entries( $request ) { | 
            |
| 776 | |||
| 777 | 71 | 	public function __get( $key ) { | 
            |
| 784 | }  | 
            ||
| 785 | 
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.