GravityView  2.17
The best, easiest way to display Gravity Forms entries on your website.
abstract-gravityview-plugin-and-theme-hooks.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Abstract class that makes it easy for plugins and themes to register no-conflict scripts and styles, as well as
4  * add post meta keys for GravityView to parse when checking for the existence of shortcodes in content.
5  *
6  * @file abstract-gravityview-plugin-and-theme-hooks.php
7  * @package GravityView
8  * @license GPL2+
9  * @author GravityView <[email protected]>
10  * @link http://gravityview.co
11  * @copyright Copyright 2015, Katz Web Services, Inc.
12  *
13  * @since 1.15.2
14  */
15 
16 /**
17  * Abstract class that makes it easy for plugins and themes to register no-conflict scripts and styles, as well as
18  * add post meta keys for GravityView to parse when checking for the existence of shortcodes in content.
19  *
20  * @since 1.15.2
21  */
23 
24  /**
25  * @type string Optional. Class that should be exist in a plugin or theme. Used to check whether plugin is active.
26  * @since 1.15.2
27  */
28  protected $class_name = false;
29 
30  /**
31  * @type string Optional. Function that should be exist in a plugin or theme. Used to check whether plugin is active.
32  * @since 1.15.2
33  */
34  protected $function_name = false;
35 
36  /**
37  * @type string Optional. Constant that should be defined by plugin or theme. Used to check whether plugin is active.
38  * @since 1.15.2
39  */
40  protected $constant_name = false;
41 
42  /**
43  * Define the keys to be parsed by the `gravityview/view_collection/from_post/meta_keys` hook
44  * @see View_Collection::from_post
45  * @since 2.0
46  * @type array
47  */
48  protected $content_meta_keys = array();
49 
50  /**
51  * Define the keys to be parsed by the `gravityview/data/parse/meta_keys` hook
52  * @see GravityView_View_Data::parse_post_meta
53  * @deprecated 2.0
54  * @since 1.15.2
55  * @type array
56  */
57  protected $meta_keys = array();
58 
59  /**
60  * Define script handles used by the theme or plugin to be added to allowed no-conflict scripts
61  * @see GravityView_Admin::remove_conflicts
62  * @since 1.15.2
63  * @type array
64  */
65  protected $script_handles = array();
66 
67  /**
68  * Define style handles used by the theme or plugin to be added to allowed no-conflict styles
69  * @see GravityView_Admin::remove_conflicts
70  * @since 1.15.2
71  * @type array
72  */
73  protected $style_handles = array();
74 
75  /**
76  * Define features in the admin editor used by the theme or plugin to be used when registering the GravityView post type
77  * @see \GV\Entry::get_endpoint_name
78  * @since 1.15.2
79  * @type array
80  */
81  protected $post_type_support = array();
82 
83  /**
84  * GravityView_Theme_Support constructor.
85  * @return void
86  */
87  public function __construct() {
88  add_action( 'wp_loaded', array( $this, '_wp_loaded' ) );
89  }
90 
91  /**
92  * Fired when all themes and plugins have been loaded.
93  * This makes sure we can reliably detect functions and classes.
94  * @internal
95  * @return void
96  */
97  public function _wp_loaded() {
98  $this->maybe_add_hooks();
99  }
100 
101  /**
102  * Check whether plugin or theme exists. If so, add hooks.
103  * This is to reduce load time, since `apply_filters()` isn't free.
104  * If the class name or function name or constant exists for a plugin or theme, add hooks
105  * If the class/function/definition aren't speicifed add the hooks
106  *
107  * @since 1.15.2
108  * @return void
109  */
110  private function maybe_add_hooks() {
111  $class_exists = $this->class_name && class_exists( $this->class_name );
112  $function_exists = $this->function_name && function_exists( $this->function_name );
113  $constant_defined = $this->constant_name && defined("{$this->constant_name}");
114 
115  if( $class_exists || $function_exists || $constant_defined ) {
116  $this->add_hooks();
117  }
118  }
119 
120  /**
121  * Add filters for meta key and script/style handles, if defined.
122  * @since 1.15.2
123  * @return void
124  */
125  protected function add_hooks() {
126 
127  if( $this->meta_keys ) {
128  add_filter( 'gravityview/data/parse/meta_keys', array( $this, 'merge_meta_keys' ), 10, 2 );
129  }
130 
131  if( $this->content_meta_keys ) {
132  add_filter( 'gravityview/view_collection/from_post/meta_keys', array( $this, 'merge_content_meta_keys' ), 10, 3 );
133  }
134 
135  if( $this->script_handles ) {
136  add_filter( 'gravityview_noconflict_scripts', array( $this, 'merge_noconflict_scripts' ) );
137  }
138 
139  if( $this->style_handles ) {
140  add_filter( 'gravityview_noconflict_styles', array( $this, 'merge_noconflict_styles' ) );
141  }
142 
143  if( $this->post_type_support ) {
144  add_filter( 'gravityview_post_type_support', array( $this, 'merge_post_type_support' ), 10, 2 );
145  }
146  }
147 
148  /**
149  * Merge plugin or theme post type support definitions with existing support values
150  *
151  * @since 1.15.2
152  *
153  * @param array $supports Array of features associated with a functional area of the edit screen.
154  * @param boolean $is_hierarchical Do Views support parent/child relationships? See `gravityview_is_hierarchical` filter.
155  *
156  * @return array Array of features associated with a functional area of the edit screen, merged with existing values
157  */
158  public function merge_post_type_support( $supports = array(), $is_hierarchical = false ) {
159  $supports = array_merge( $this->post_type_support, $supports );
160  return $supports;
161  }
162 
163  /**
164  * Merge plugin or theme styles with existing no-conflict styles
165  *
166  * @since 1.15.2
167  *
168  * @param array $handles Array of style handles, as registered with WordPress
169  *
170  * @return array Handles, merged with existing styles
171  */
172  public function merge_noconflict_styles( $handles ) {
173  $handles = array_merge( $this->style_handles, $handles );
174  return $handles;
175  }
176 
177  /**
178  * Merge plugin or theme scripts with existing no-conflict scripts
179  *
180  * @since 1.15.2
181  *
182  * @param array $handles Array of script handles, as registered with WordPress
183  *
184  * @return array Handles, merged with existing scripts
185  */
186  public function merge_noconflict_scripts( $handles ) {
187  $handles = array_merge( $this->script_handles, $handles );
188  return $handles;
189  }
190 
191  /**
192  * Merge plugin or theme meta keys that store shortcode data with existing keys to check
193  *
194  * @since 1.15.2
195  *
196  * @deprecated 2.0.7
197  *
198  * @param array $handles Array of meta keys to check for existence of shortcodes
199  * @param int $post_id The ID being checked by GravityView
200  *
201  * @return array Meta key array, merged with existing meta keys
202  */
203  public function merge_meta_keys( $meta_keys = array(), $post_id = 0 ) {
204  return array_merge( $this->meta_keys, $meta_keys );
205  }
206 
207  /**
208  * Merge plugin or theme meta keys that store shortcode data with existing keys to check
209  *
210  * @since 2.0.7
211  *
212  * @param array $handles Array of meta keys to check for existence of shortcodes
213  * @param \WP_Post $post The ID being checked by GravityView
214  *
215  * @return array Meta key array, merged with existing meta keys
216  */
217  public function merge_content_meta_keys( $meta_keys = array(), $post = null, & $views = null ) {
218  return array_merge( $this->content_meta_keys, $meta_keys );
219  }
220 
221 }
$script_handles
Define script handles used by the theme or plugin to be added to allowed no-conflict scripts...
merge_noconflict_styles( $handles)
Merge plugin or theme styles with existing no-conflict styles.
merge_meta_keys( $meta_keys=array(), $post_id=0)
Merge plugin or theme meta keys that store shortcode data with existing keys to check.
$content_meta_keys
Define the keys to be parsed by the gravityview/view_collection/from_post/meta_keys hook...
global $post
Definition: delete-entry.php:7
merge_noconflict_scripts( $handles)
Merge plugin or theme scripts with existing no-conflict scripts.
__construct()
GravityView_Theme_Support constructor.
$style_handles
Define style handles used by the theme or plugin to be added to allowed no-conflict styles...
$post_type_support
Define features in the admin editor used by the theme or plugin to be used when registering the Gravi...
add_hooks()
Add filters for meta key and script/style handles, if defined.
$meta_keys
Define the keys to be parsed by the gravityview/data/parse/meta_keys hook.
merge_content_meta_keys( $meta_keys=array(), $post=null, & $views=null)
Merge plugin or theme meta keys that store shortcode data with existing keys to check.
merge_post_type_support( $supports=array(), $is_hierarchical=false)
Merge plugin or theme post type support definitions with existing support values. ...
Abstract class that makes it easy for plugins and themes to register no-conflict scripts and styles...
_wp_loaded()
Fired when all themes and plugins have been loaded.