Tribe__Context
Class Tribe__Context
Source
File: src/Tribe/Context.php
class Tribe__Context { /** * Whether the context of the current HTTP request is an AJAX one or not. * * @var bool */ protected $doing_ajax; /** * Whether the context of the current HTTP request is a Cron one or not. * * @var bool */ protected $doing_cron; /** * Whether we are currently creating a new post, a post of post type(s) or not. * * @since 4.7.7 * * @param null $post_type The optional post type to check. * * @return bool Whether we are currently creating a new post, a post of post type(s) or not. */ public function is_new_post( $post_type = null ) { global $pagenow; $is_new = 'post-new.php' === $pagenow; return $is_new && $this->is_editing_post( $post_type ); } /** * Whether we are currently editing a post(s), post type(s) or not. * * @since 4.7.7 * * @param null|array|string|int $post_or_type A post ID, post type, an array of post types or post IDs, `null` * to just make sure we are currently editing a post. * * @return bool */ public function is_editing_post( $post_or_type = null ) { global $pagenow; $is_new = 'post-new.php' === $pagenow; $is_post = 'post.php' === $pagenow; if ( ! $is_new && ! $is_post ) { return false; } if ( null !== $post_or_type ) { $lookup = array( $_GET, $_POST, $_REQUEST ); $current_post = Tribe__Utils__Array::get_in_any( $lookup, 'post', get_post() ); if ( is_numeric( $post_or_type ) ) { $post = $is_post ? get_post( $post_or_type ) : null; return ! empty( $post ) && $post == $current_post; } $post_types = is_array( $post_or_type ) ? $post_or_type : array( $post_or_type ); $post = $is_post ? $current_post : null; if ( count( array_filter( $post_types, 'is_numeric' ) ) === count( $post_types ) ) { return ! empty( $post ) && in_array( $post->ID, $post_types ); } if ( $is_post && $post instanceof WP_Post ) { $post_type = $post->post_type; } else { $post_type = Tribe__Utils__Array::get_in_any( $lookup, 'post_type', 'post' ); } return (bool) count( array_intersect( $post_types, array( $post_type ) ) ); } return $is_new || $is_post; } /** * Helper function to indicate whether the current execution context is AJAX. * * This method exists to allow us test code that behaves differently depending on the execution * context; passing a value to this argument will set it to that value in future checks, a test-related usage. * * @since 4.7.12 * * @param bool $doing_ajax An injectable status to override the `DOING_AJAX` check. * * @return boolean */ public function doing_ajax( $doing_ajax = null ) { if ( null !== $doing_ajax ) { $this->doing_ajax = (bool) $doing_ajax; } else { $this->doing_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX; } return $this->doing_ajax; } /** * Checks whether the context of the current HTTP request is a Cron one or not. * * @since 4.7.23 * * @param bool|null $doing_cron If set then this method will act as a setter; the current * method call, and the following ones, will return this value. * * @return bool whether the context of the current HTTP request is a Cron one or not. */ public function doing_cron( $doing_cron = null ) { if ( null !== $doing_cron ) { $this->doing_cron = (bool) $doing_cron; } else { $this->doing_cron = defined( 'DOING_CRON' ) && DOING_CRON; } return $this->doing_cron; } }
Changelog
Version | Description |
---|---|
4.9.5 | Made the context immutable. |
4.7.7 | Introduced. |
Methods
- add_locations — Adds/replaces read and write locations to a context.
- alter — Alters the context.
- dangerously_repopulate_locations — Just dont.
- dangerously_set_global_context — Modifies the global context using the defined write locations to persist the altered values.
- doing_ajax — Helper function to indicate whether the current execution context is AJAX.
- doing_cron — Checks whether the context of the current HTTP request is a Cron one or not.
- doing_php_initial_state — Whether the current request is for a PHP-rendered initial state or not.
- doing_rest — Checks whether the current request is a REST API one or not.
- filter — Reads (gets) the value applying one or more filters.
- get — Gets a value reading it from the location(s) defined in the `Tribe__Context::$props
- get_locations — Returns the read and write locations set on the context.
- get_orm_args — Returns an array of ORM arguments generated from the current context values.
- get_read_key_for — Returns the first key, if there are many, that will be used to read a location.
- get_state — Returns the current context state in a format suitable to hydrate a Redux-like store on the front-end.
- is — Convenience method to get and check if a location has a truthy value or not.
- is_editing_post — Whether we are currently editing a post(s), post type(s) or not.
- is_new_post — Whether we are currently creating a new post, a post of post type(s) or not.
- location_func — Reads the value from one callback, passing it the value of another Context location.
- map_to_read — Maps an input array to the corresponding read locations.
- query_method — Reads the value from one or more global WP_Query object methods.
- refresh — Clears the context cache forcing a re-fetch of the variables from the context.
- safe_set — Safely set the value of a group of locations.
- set_locations — Sets, replacing them, the locations used by this context.
- to_array — Returns an array representation of the context.
- translate_sub_locations — Translates sub-locations to their respective location key.
- wp_matched_query — Reads (gets) the value reading it from a query var parsed from the query matched by the global `$wp` object.
- wp_parsed — Reads (gets) the value reading it from a query var parsed from the global `$wp` object.