Skip to main content

GravityKit\GravityView\View\View

Source: src/View/View.php:58

Details​

  • Kind: class

  • Namespace: GravityKit\GravityView\View

  • Implements: ArrayAccess

Properties​

PropertyTypeDescription
$settings\GV\View_SettingsThe settings.
$widgets\GV\Widget_CollectionThe widgets attached here.
$form\GV\GF_Form | \GV\FormThe backing form for this view. Contains the form that is sourced for entries in this view.
$fields\GV\Field_CollectionThe fields for this view. Contains all the fields that are attached to this view.
$styles\GravityKit\GravityView\Settings\ViewStylesThe design-token registry adapter for this View. Provides token discovery, sanitisation, and CSS emission for the View's saved palette / typography / spacing / effects overrides. Backed by static methods; the property is a thin namespace handle so callers can write $view->styles->emit_css_declarations( ... ) (see also scoped_selector(), sticky_header_css(), and prefers_contrast_css() for the per-View emission helpers).
$joins\GV\Join[]The joins for all sources in this view.
$unions\GV\Field[][]The unions for all sources in this view. An array of fields grouped by form_id keyed by main field_id: array( $form_id => array( $field_id => $field, $field_id => $field, ) )

Methods​

MethodDescription
__construct()The constructor.
add_rewrite_endpoint()Add extra rewrite endpoints.
content()A renderer filter for the View post type content.
can_render()Checks whether this view can be accessed or not.
lists_entries_from()Whether the View lists entries of a form as its own rows: its form or a unioned form.
get_entry()The entry, when this View lists it as a row of its own form or a unioned form.
lists_entry()Whether this View lists the entry for the request, with all its filters applied.
can_render_entry()Whether this View shows the entry on its Single Entry page.
get_form_ids()The IDs of every form this View spans: its own form plus any joined or unioned forms.
get_joins()Get joins associated with a view
get_joined_forms()Get joined forms associated with a view
get_unions()Get unions associated with a view
maybe_union_field()Resolves a configured field to its unioned counterpart, if the form has a union mapping.
apply_embed_settings()Applies the attributes of the embed being rendered, such as a shortcode's, over the View's own settings.
restore_embed_settings()Undoes what {@see View::apply_embed_settings()} changed for a render, where nothing changed it since.
add_embed_settings()Adds an embed's attributes, read from a page without rendering it, to the View's settings.
apply_embed()Applies the attributes of one embed read from a page, over the View's own settings.
has_embed()Whether a page read for this View holds the embed with this key.
embed_count()Returns how many different embeds of this View were read from a post.
entry_link_args()Returns the query arguments an entry link needs to name the embed it came from.
with_embed()Runs a callback with one embed's settings applied, then puts the settings back.
embed_key()Returns a key naming an embed by its View, page, and attributes as written, the same for its render and for reading its page.
from_post()Construct a \GV\View instance from a \WP_Post.
by_id()Construct a \GV\View instance from a post ID.
get_source()Gets the source of the field.
exists()Determines if a view exists to begin with.
is_hidden_until_searched()Whether the View withholds its entries from this request because nothing was searched yet.
get_entries()Retrieve the entries for the current view and request.
template_redirect()Last chance to configure the output.
get_query_class()Return the query class for this View.
restrict()Restrict View access to specific capabilities.
set_anchor_id()Sets the anchor ID of a View, without the prefix.
get_anchor_id()Returns the anchor ID to be used in the View container HTML id attribute.
__get()Magic getter forwarded to the backing \WP_Post.
get_post()Return associated WP post
get_validation_secret()Calculates and returns the View's validation secret.
get_validation_secret_by_id()Calculates and returns a View's validation secret without hydrating the View.
maybe_establish_secret_key_boundary()Establishes the per-site secret-key boundary on this site's first run.
validate_secret()Returns whether the provided secret validates for this View.
start_post_content()Notes which post's content is being filtered, so a secret in it can be traced to who wrote it.
start_acf_field()Notes the post an ACF WYSIWYG field belongs to, so a secret in it is traced to that post's writers.
end_post_content()Ends what start_post_content() or start_acf_field() started.
start_rest_callback()Notes the route of the REST request whose callback is about to run.
end_rest_callback()Ends what start_rest_callback() started.
remember_new_post_saver()Remembers who wrote a new post's content.
remember_post_saver()Remembers who changed a post's content, from any editor.
remember_meta_saver()Remembers who changed a layout a post shows from its meta, such as an Elementor or View layout.
maybe_record_legacy_secret_last_post_id()Saves, once, the highest post ID on this site, so posts created later cannot pass as unchanged since the key.
get_shortcode()Returns the shortcode for this View.

Method Reference​

__construct()​

public function __construct()

The constructor.

Source: src/View/View.php:314

add_rewrite_endpoint()​

public static function add_rewrite_endpoint()

Add extra rewrite endpoints.

Returns​

  • void

Source: src/View/View.php:456

content()​

public static function content( $content )

A renderer filter for the View post type content.

Parameters​

NameTypeDefaultDescription
$contentstringShould be empty, as we don't store anything there.

Returns​

  • string — $content The view content as output by the renderers.

Source: src/View/View.php:489

can_render()​

public function can_render( $context=null, $request=null )

Checks whether this view can be accessed or not.

Parameters​

NameTypeDefaultDescription
$contextstring[]nullThe context we're asking for access from. Can any and as many of one of: edit An edit context. single A single context. cpt The custom post type single page accessed. shortcode Embedded as a shortcode. lightbox Rendered inside a GravityView entry lightbox. oembed Embedded as an oEmbed. rest A REST call. csv A CSV export.
$requestRequest | \WP_REST_Request | nullnullThe request.

Returns​

  • bool | \WP_Error — An error if this View shouldn't be rendered here.

See Also​

  • \View::can_render_entry() Whether the View shows a given entry on its Single Entry page.

Source: src/View/View.php:603

lists_entries_from()​

public function lists_entries_from( $form_id ): bool

Whether the View lists entries of a form as its own rows: its form or a unioned form.

Parameters​

NameTypeDefaultDescription
$form_idint | stringThe form ID.

Returns​

  • bool

Since: 3.7.0

Source: src/View/View.php:707

get_entry()​

public function get_entry( $entry_id ): ?Entry

The entry, when this View lists it as a row of its own form or a unioned form.

Like the View's Single Entry page, the page's search, sort and paging do not narrow the check.

Parameters​

NameTypeDefaultDescription
$entry_idint | stringThe entry ID or slug. On a joined View, the IDs or slugs of one row, joined with commas.

Returns​

  • Entry | null — Null when the entry does not exist or the View leaves it out. On a joined View, the first row in the View's sort order that holds every entry named.

Since:

  • 3.7.0
  • 3.8.0 (Returns the joined row on a joined View, and takes the View's own entry slugs for joined rows, such as 12,34.)

Source: src/View/View.php:731

lists_entry()​

public function lists_entry( Entry $entry, ?Request $request=null ): bool

Whether this View lists the entry for the request, with all its filters applied.

Parameters​

NameTypeDefaultDescription
$entryEntryThe entry.
$requestRequest | nullnullThe request. Default: the current request.

Returns​

  • bool

See Also​

  • \View::can_render_entry() The Single Entry answer, which also checks the entry's status, approval and slug.

Since: 3.7.0

Source: src/View/View.php:760

can_render_entry()​

public function can_render_entry( Entry $entry )

Whether this View shows the entry on its Single Entry page.

Checks the entry, or each entry of a joined row, the way the View's Single Entry page does: the entry's status, approval and slug (\Entry::check_access()), Embed Only on the View's own page, ?gvid= naming another View, and the View's filters (\View::lists_entry()). All of them judge the current request. Users who may bypass Embed Only (\DirectAccess::may_bypass()) pass it here, as they do on the View's list. Whether the View itself renders is \View::can_render(); call that first.

Parameters​

NameTypeDefaultDescription
$entryEntryThe entry, or a joined entry.

Returns​

  • true | \WP_Error — True when the View shows the entry, or the first reason it does not.

See Also​

  • \View::can_render() Whether the View itself renders; call it first.
  • \View::lists_entry() Whether the View's filters list the entry.
  • \Entry::check_access() The entry's status, approval and slug.
  • \DirectAccess::may_bypass() Who may open an Embed Only View at its own address.
  • \GVCommon::check_entry_display() The legacy check add-ons call: the View's rules only.
  • \GravityKit\GravityView\Permissions\Permissions Capability checks for managing Views, not whether a visitor sees an entry.

Since: 3.10.0

Source: src/View/View.php:786

get_form_ids()​

public function get_form_ids(): array

The IDs of every form this View spans: its own form plus any joined or unioned forms.

Returns​

  • int[]

Since:

  • 3.0.1
  • 3.2.0 (Includes unioned forms.)

Source: src/View/View.php:972

get_joins()​

public static function get_joins( $post )

Get joins associated with a view

Parameters​

NameTypeDefaultDescription
$post\WP_PostGravityView CPT to get joins for.

Returns​

  • \GV\Join[] — Array of \GV\Join instances

Since: 2.0.11

Source: src/View/View.php:1030

get_joined_forms()​

public static function get_joined_forms( $post_id )

Get joined forms associated with a view

In no particular order.

Parameters​

NameTypeDefaultDescription
$post_idintID of the View.

Returns​

  • \GV\GF_Form[] — Array of \GV\GF_Form instances

Since:

  • 2.0.11
  • 2.0

Source: src/View/View.php:1082

get_unions()​

public static function get_unions( $post )

Get unions associated with a view

Parameters​

NameTypeDefaultDescription
$post\WP_PostGravityView CPT to get unions for.

Returns​

Since: 2.2.2

Source: src/View/View.php:1140

maybe_union_field()​

public function maybe_union_field( $field, $form_id )

Resolves a configured field to its unioned counterpart, if the form has a union mapping.

The slot's display configuration (position, label, visibility, class, and link settings) is copied so the unioned field is presented exactly as the slot it replaces; only the value comes from the unioned form. A unioned form field of the same type also takes the slot's other settings.

Parameters​

NameTypeDefaultDescription
$field\GV\FieldThe configured field from the primary form.
$form_idint | stringThe form ID of the entry being rendered.

Returns​

  • \GV\Field — The unioned field for the form, or the original field when there is no mapping.

Since: 3.2.0

Source: src/View/View.php:1204

apply_embed_settings()​

public function apply_embed_settings( array $atts ): array

Applies the attributes of the embed being rendered, such as a shortcode's, over the View's own settings.

Every embed of a View on a page shares this object. A setting another embed gave a value this embed does not set goes back to its value from before any embed, until the render ends and \View::restore_embed_settings() puts it back. A value something else set since is kept. An embed inside a render of this same View keeps the settings around it, such as a detail="page_size" shortcode in the View's own widget.

Parameters​

NameTypeDefaultDescription
$attsarrayThe embed's attributes, in shortcode form.

Returns​

  • array — What the render changed, for {@see View::restore_embed_settings()}.

Since: 3.7.0

Source: src/View/View.php:1330

restore_embed_settings()​

public function restore_embed_settings( array $state, bool $everything=false )

Undoes what {@see View::apply_embed_settings()} changed for a render, where nothing changed it since.

The other embeds' values set aside always come back. A nested render, such as a View counting its own entries in one of its fields, also puts back the settings it applied, so the render around it goes on with its own.

Parameters​

NameTypeDefaultDescription
$statearrayWhat the render changed.
$everythingboolfalseWhether to put back the render's own attributes too.

Returns​

  • void

Since: 3.7.0

Source: src/View/View.php:1382

add_embed_settings()​

public function add_embed_settings( array $atts, string $key='', int $post_id=0 )

Adds an embed's attributes, read from a page without rendering it, to the View's settings.

Settings from the page's other embeds of this View stay. Code that reads the page without knowing which embed it serves (the lightbox, REST, DataTables) then keeps every embed's search. A secured View skips an embed without its secret, since that embed never renders. While the View renders, as when an entry link reads the page, only the embed is recorded so the render keeps its own settings.

Parameters​

NameTypeDefaultDescription
$attsarrayThe embed's attributes, in shortcode form.
$keystring''Optional. The embed's \View::embed_key(), so a request can name it later.
$post_idint0Optional. The ID of the post the embed was read from.

Returns​

  • void

Since: 3.7.0

Source: src/View/View.php:1426

apply_embed()​

public function apply_embed( string $key ): bool

Applies the attributes of one embed read from a page, over the View's own settings.

Requests that come from a rendered embed (the lightbox, a live refresh) name it, so the page's other embeds of this View do not narrow or widen the result.

Parameters​

NameTypeDefaultDescription
$keystringThe embed's \View::embed_key().

Returns​

  • bool — Whether an embed with that key was read from the page and applied.

Since: 3.7.0

Source: src/View/View.php:1464

has_embed()​

public function has_embed( string $key ): bool

Whether a page read for this View holds the embed with this key.

Parameters​

NameTypeDefaultDescription
$keystringThe embed's \View::embed_key().

Returns​

  • bool

Since: 3.7.0

Source: src/View/View.php:1483

embed_count()​

public function embed_count( int $post_id ): int

Returns how many different embeds of this View were read from a post.

Parameters​

NameTypeDefaultDescription
$post_idintThe ID of the post.

Returns​

  • int

Since: 3.7.0

Source: src/View/View.php:1496

entry_link_args()​

public function entry_link_args( int $post_id ): array

Returns the query arguments an entry link needs to name the embed it came from.

Only a post that holds this View more than once needs them.

Parameters​

NameTypeDefaultDescription
$post_idintThe ID of the post the link points into.

Returns​

  • array

Since: 3.7.0

Source: src/View/View.php:1511

with_embed()​

public function with_embed( string $key, callable $callback )

Runs a callback with one embed's settings applied, then puts the settings back.

Parameters​

NameTypeDefaultDescription
$keystringThe embed's \View::embed_key(). An embed not read from the page is ignored.
$callbackcallableThe callback.

Returns​

  • mixed — What the callback returns.

Since: 3.7.0

Source: src/View/View.php:1531

embed_key()​

public static function embed_key( array $atts, int $post_id=0 ): string

Returns a key naming an embed by its View, page, and attributes as written, the same for its render and for reading its page.

The key is signed with the site's salt, so a visitor learns only the keys of embeds rendered for them.

Parameters​

NameTypeDefaultDescription
$attsarrayThe embed's attributes as written, in shortcode form, including the View id.
$post_idint0The ID of the post holding the embed, or 0 if unknown.

Returns​

  • string

Since: 3.7.0

Source: src/View/View.php:1557

from_post()​

public static function from_post( $post )

Construct a \GV\View instance from a \WP_Post.

Parameters​

NameTypeDefaultDescription
$post\WP_PostThe \WP_Post instance to wrap.

Returns​

  • \GV\View | null — An instance around this \WP_Post if valid, null otherwise.

Since: 2.0

Source: src/View/View.php:1609

by_id()​

public static function by_id( $post_id )

Construct a \GV\View instance from a post ID.

Parameters​

NameTypeDefaultDescription
$post_idint | stringThe post ID.

Returns​

  • \GV\View | null — An instance around this \WP_Post or null if not found.

Since: 2.0

Source: src/View/View.php:1740

get_source()​

public static function get_source( $field, $view )

Gets the source of the field.

Parameters​

NameTypeDefaultDescription
$field\GV\FieldThe field.
$view\GV\ViewThe view.

Returns​

  • \GV\GF_Form | \GV\Internal_Source

Since: 2.33

Source: src/View/View.php:1816

exists()​

public static function exists( $view )

Determines if a view exists to begin with.

Parameters​

NameTypeDefaultDescription
$viewint | \WP_Post | nullThe WordPress post ID, a \WP_Post object or null for global $post.

Returns​

  • bool — Whether the post exists or not.

Since: 2.0

Source: src/View/View.php:1848

is_hidden_until_searched()​

public function is_hidden_until_searched( $request=null ): bool

Whether the View withholds its entries from this request because nothing was searched yet.

Parameters​

NameTypeDefaultDescription
$requestRequest | nullnullThe request. Default: the current request.

Returns​

  • bool — True when "Hide View data until search is performed" is on and the request does not search this View.

Since: 3.6.0

Source: src/View/View.php:2191

get_entries()​

public function get_entries( $request=null )

Retrieve the entries for the current view and request.

Parameters​

NameTypeDefaultDescription
$requestRequest | nullnullThe request.

Returns​

Source: src/View/View.php:2252

template_redirect()​

public static function template_redirect()

Last chance to configure the output.

Used for CSV output, for example.

Returns​

  • void

Source: src/View/View.php:2987

get_query_class()​

public function get_query_class()

Return the query class for this View.

Returns​

  • string — The class name.

Source: src/View/View.php:3122

restrict()​

public static function restrict( $caps, $cap, $user_id, $args )

Restrict View access to specific capabilities.

Hooked into map_meta_cap WordPress filter.

Parameters​

NameTypeDefaultDescription
$capsarrayThe output capabilities.
$capstringThe cap that is being checked.
$user_idintThe User ID.
$argsarrayAdditional arguments to the capability.

Returns​

  • array — The resulting capabilities.

Since: 3.0.0

Source: src/View/View.php:3146

set_anchor_id()​

public function set_anchor_id( $counter=1 )

Sets the anchor ID of a View, without the prefix.

Parameters​

NameTypeDefaultDescription
$counterint1An incremental counter reflecting how many times this View has been rendered.

Returns​

  • void

Since: 2.15

Source: src/View/View.php:3191

get_anchor_id()​

public function get_anchor_id()

Returns the anchor ID to be used in the View container HTML id attribute.

Returns​

  • string — Unsanitized anchor ID.

Since: 2.15

Source: src/View/View.php:3202

__get()​

public function __get( $key )

Magic getter forwarded to the backing \WP_Post.

Parameters​

NameTypeDefaultDescription
$keystringThe property name.

Returns​

  • mixed — The property value, or null if not set.

Source: src/View/View.php:3220

get_post()​

public function get_post()

Return associated WP post

Returns​

  • \WP_Post | null

Since: 2.13.2

Source: src/View/View.php:3235

get_validation_secret()​

final public function get_validation_secret( bool $is_forced=false ): ?string

Calculates and returns the View's validation secret.

Parameters​

NameTypeDefaultDescription
$is_forcedboolfalseWhether to compute the secret for non-secure Views too.

Returns​

  • string | null — The View's secret.

Since: 2.21

Source: src/View/View.php:3261

get_validation_secret_by_id()​

public static function get_validation_secret_by_id( int $view_id, bool $is_forced=false ): ?string

Calculates and returns a View's validation secret without hydrating the View.

Bulk callers (e.g. the block editor View picker) use this so listing thousands of Views does not pay full View hydration per row.

Parameters​

NameTypeDefaultDescription
$view_idintThe View ID.
$is_forcedboolfalseWhether to compute the secret for non-secure Views too.

Returns​

  • string | null — The View's secret.

Since:

  • 3.0.0
  • 3.1.0 (The secret is derived from a per-site random key stored in the gravityview_view_secret_key option. Embeds of Views that predate the per-site key keep working because validate_secret() still accepts the previous secret for them.)

Source: src/View/View.php:3282

maybe_establish_secret_key_boundary()​

public static function maybe_establish_secret_key_boundary(): int

Establishes the per-site secret-key boundary on this site's first run.

Hooked to gravityview/loaded (an early, every-request action), so the stored GMT timestamp is the moment this site first ran the per-site-key code: activation time on a fresh install, first request after the update on an upgrade. Writes once and returns the same boundary thereafter.

Returns​

  • int — The boundary as a GMT Unix timestamp, or 0 if it cannot be persisted.

Since: 3.1.0

Source: src/View/View.php:3417

validate_secret()​

final public function validate_secret( #[\SensitiveParameter] string $secret, ?\WP_Post $embed_post=null ): bool

Returns whether the provided secret validates for this View.

Parameters​

NameTypeDefaultDescription
$secretstringThe provided secret.
$embed_post\WP_Post | nullnullOptional. The post the embed was read from, when it is not being rendered.

Returns​

  • bool

Since:

  • 2.21
  • 3.1.0 (The current secret is derived from a per-site key. The previous secret is accepted only for Views that predate the per-site key, so existing embeds keep working while newly created Views use only the per-site secret. Override with the gk/gravityview/view/secret/allow-legacy filter.)
  • 3.9.0 (The previous secret is accepted only where someone who can edit the View put it.)

Source: src/View/View.php:3483

start_post_content()​

public static function start_post_content( $content )

Notes which post's content is being filtered, so a secret in it can be traced to who wrote it.

Parameters​

NameTypeDefaultDescription
$contentmixedThe post content.

Returns​

  • mixed — The content, unchanged.

Since: 3.9.0

Source: src/View/View.php:3547

start_acf_field()​

public static function start_acf_field( $value, $post_id )

Notes the post an ACF WYSIWYG field belongs to, so a secret in it is traced to that post's writers.

Parameters​

NameTypeDefaultDescription
$valuemixedThe field value.
$post_idint | stringWhat ACF loaded the value from: a post ID, or another ID such as options or user_2.

Returns​

  • mixed — The value, unchanged.

Since: 3.9.0

Source: src/View/View.php:3563

end_post_content()​

public static function end_post_content( $content )

Ends what start_post_content() or start_acf_field() started.

Parameters​

NameTypeDefaultDescription
$contentmixedThe post content.

Returns​

  • mixed — The content, unchanged.

Since: 3.9.0

Source: src/View/View.php:3606

start_rest_callback()​

public static function start_rest_callback( $response, $handler, $request )

Notes the route of the REST request whose callback is about to run.

Parameters​

NameTypeDefaultDescription
$responsemixedThe response so far.
$handlerarrayThe route's handler.
$request\WP_REST_RequestThe request.

Returns​

  • mixed — The response, unchanged.

Since: 3.9.0

Source: src/View/View.php:3653

end_rest_callback()​

public static function end_rest_callback( $response )

Ends what start_rest_callback() started.

Parameters​

NameTypeDefaultDescription
$responsemixedThe response.

Returns​

  • mixed — The response, unchanged.

Since: 3.9.0

Source: src/View/View.php:3668

remember_new_post_saver()​

public static function remember_new_post_saver( $post_id, $post, $update )

Remembers who wrote a new post's content.

Parameters​

NameTypeDefaultDescription
$post_idintPost ID.
$post\WP_PostThe post.
$updateboolWhether this is an update, which remember_post_saver() handles.

Returns​

  • void

Since: 3.9.0

Source: src/View/View.php:3795

remember_post_saver()​

public static function remember_post_saver( $post_id, $post_after, $post_before )

Remembers who changed a post's content, from any editor.

Revisions can be turned off and _edit_last is not kept up to date, so neither is a reliable record.

Parameters​

NameTypeDefaultDescription
$post_idintPost ID.
$post_after\WP_PostThe post after the save.
$post_before\WP_PostThe post before the save.

Returns​

  • void

Since: 3.9.0

Source: src/View/View.php:3814

remember_meta_saver()​

public static function remember_meta_saver( $meta_id, $post_id, $meta_key )

Remembers who changed a layout a post shows from its meta, such as an Elementor or View layout.

Parameters​

NameTypeDefaultDescription
$meta_idintMeta ID.
$post_idintPost ID.
$meta_keystringMeta key.

Returns​

  • void

Since: 3.9.0

Source: src/View/View.php:3833

maybe_record_legacy_secret_last_post_id()​

public static function maybe_record_legacy_secret_last_post_id(): void

Saves, once, the highest post ID on this site, so posts created later cannot pass as unchanged since the key.

Returns​

  • void

Since: 3.9.0

Source: src/View/View.php:3920

get_shortcode()​

final public function get_shortcode( array $atts=[] ): string

Returns the shortcode for this View.

Parameters​

NameTypeDefaultDescription
$attsarray[]Additional attributes for the shortcode.

Returns​

  • string

Since:

  • 2.21
  • 2.22 (Added $atts parameter.)

Source: src/View/View.php:3947