GravityView  1.19.4
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 Katz Web Services, Inc.
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/data/parse/meta_keys` hook
44  * @see GravityView_View_Data::parse_post_meta
45  * @since 1.15.2
46  * @type array
47  */
48  protected $content_meta_keys = array();
49 
50  /**
51  * Define script handles used by the theme or plugin to be parsed by the `gravityview/data/parse/meta_keys` hook
52  * @see GravityView_Admin::remove_conflicts
53  * @since 1.15.2
54  * @type array
55  */
56  protected $script_handles = array();
57 
58  /**
59  * Define style handles used by the theme or plugin to be parsed by the `gravityview/data/parse/meta_keys` hook
60  * @see GravityView_Admin::remove_conflicts
61  * @since 1.15.2
62  * @type array
63  */
64  protected $style_handles = array();
65 
66  /**
67  * Define features in the admin editor used by the theme or plugin to be used when registering the GravityView post type
68  * @see \GV\Entry::get_endpoint_name
69  * @since 1.15.2
70  * @type array
71  */
72  protected $post_type_support = array();
73 
74  /**
75  * GravityView_Theme_Support constructor.
76  * @return void
77  */
78  public function __construct() {
79  $this->maybe_add_hooks();
80  }
81 
82  /**
83  * Check whether plugin or theme exists. If so, add hooks.
84  * This is to reduce load time, since `apply_filters()` isn't free.
85  * If the class name or function name or constant exists for a plugin or theme, add hooks
86  * If the class/function/definition aren't speicifed add the hooks
87  *
88  * @since 1.15.2
89  * @return void
90  */
91  private function maybe_add_hooks() {
92  $class_exists = $this->class_name && class_exists( $this->class_name );
93  $function_exists = $this->function_name && function_exists( $this->function_name );
94  $constant_defined = $this->constant_name && defined("{$this->constant_name}");
95 
96  if( $class_exists || $function_exists || $constant_defined ) {
97  $this->add_hooks();
98  }
99  }
100 
101  /**
102  * Add filters for meta key and script/style handles, if defined.
103  * @since 1.15.2
104  * @return void
105  */
106  protected function add_hooks() {
107  if( $this->content_meta_keys ) {
108  add_filter( 'gravityview/data/parse/meta_keys', array( $this, 'merge_content_meta_keys' ), 10, 2 );
109  }
110 
111  if( $this->script_handles ) {
112  add_filter( 'gravityview_noconflict_scripts', array( $this, 'merge_noconflict_scripts' ) );
113  }
114 
115  if( $this->style_handles ) {
116  add_filter( 'gravityview_noconflict_styles', array( $this, 'merge_noconflict_styles' ) );
117  }
118 
119  if( $this->post_type_support ) {
120  add_filter( 'gravityview_post_type_support', array( $this, 'merge_post_type_support' ), 10, 2 );
121  }
122  }
123 
124  /**
125  * Merge plugin or theme post type support definitions with existing support values
126  *
127  * @since 1.15.2
128  *
129  * @param array $supports Array of features associated with a functional area of the edit screen.
130  * @param boolean $is_hierarchical Do Views support parent/child relationships? See `gravityview_is_hierarchical` filter.
131  *
132  * @return array Array of features associated with a functional area of the edit screen, merged with existing values
133  */
134  public function merge_post_type_support( $supports = array(), $is_hierarchical = false ) {
135  $supports = array_merge( $this->post_type_support, $supports );
136  return $supports;
137  }
138 
139  /**
140  * Merge plugin or theme styles with existing no-conflict styles
141  *
142  * @since 1.15.2
143  *
144  * @param array $handles Array of style handles, as registered with WordPress
145  *
146  * @return array Handles, merged with existing styles
147  */
148  public function merge_noconflict_styles( $handles ) {
149  $handles = array_merge( $this->style_handles, $handles );
150  return $handles;
151  }
152 
153  /**
154  * Merge plugin or theme scripts with existing no-conflict scripts
155  *
156  * @since 1.15.2
157  *
158  * @param array $handles Array of script handles, as registered with WordPress
159  *
160  * @return array Handles, merged with existing scripts
161  */
162  public function merge_noconflict_scripts( $handles ) {
163  $handles = array_merge( $this->script_handles, $handles );
164  return $handles;
165  }
166 
167  /**
168  * Merge plugin or theme meta keys that store shortcode data with existing keys to check
169  *
170  * @since 1.15.2
171  *
172  * @param array $handles Array of meta keys to check for existence of shortcodes
173  * @param int $post_id The ID being checked by GravityView
174  *
175  * @return array Meta key array, merged with existing meta keys
176  */
177  public function merge_content_meta_keys( $meta_keys = array(), $post_id = 0 ) {
178  return array_merge( $this->content_meta_keys, $meta_keys );
179  }
180 
181 }
$script_handles
Define script handles used by the theme or plugin to be parsed by the gravityview/data/parse/meta_key...
$content_meta_keys
Define the keys to be parsed by the gravityview/data/parse/meta_keys hook.
merge_post_type_support($supports=array(), $is_hierarchical=false)
Merge plugin or theme post type support definitions with existing support values. ...
__construct()
GravityView_Theme_Support constructor.
$style_handles
Define style handles used by the theme or plugin to be parsed by the gravityview/data/parse/meta_keys...
$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.
merge_noconflict_scripts($handles)
Merge plugin or theme scripts with existing no-conflict scripts.
merge_noconflict_styles($handles)
Merge plugin or theme styles with existing no-conflict styles.
merge_content_meta_keys($meta_keys=array(), $post_id=0)
Merge plugin or theme meta keys that store shortcode data with existing keys to check.
Abstract class that makes it easy for plugins and themes to register no-conflict scripts and styles...
maybe_add_hooks()
Check whether plugin or theme exists.