GravityView  1.19.4
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-template.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Class used to register a new template to be shown in GravityView presets
5  */
6 abstract class GravityView_Template {
7 
8  /**
9  * @var string template unique id
10  */
11  public $template_id;
12 
13  /**
14  * @var array $settings {
15  * @type string $slug - template slug (frontend)
16  * @type string $css_source - url path to CSS file, to be enqueued (frontend)
17  * @type string $type - 'custom' or 'preset' (admin)
18  * @type string $label - template nicename (admin)
19  * @type string $description - short about text (admin)
20  * @type string $logo - template icon (admin)
21  * @type string $preview - template image for previewing (admin)
22  * @type string $buy_source - url source for buying this template
23  * @type string $preset_form - path to Gravity Form form XML file
24  * @type string $preset_config - path to View config (XML)
25  * }
26  */
27  public $settings;
28 
29  /**
30  * @var array form fields extra options
31  */
33 
34  /**
35  * @var array define the active areas
36  */
37  public $active_areas;
38 
39 
40  function __construct( $id, $settings = array(), $field_options = array(), $areas = array() ) {
41 
42  if ( empty( $id ) ) {
43  return;
44  }
45 
46  $this->template_id = $id;
47 
48  $this->merge_defaults( $settings );
49 
50  $this->field_options = $field_options;
51  $this->active_areas = $areas;
52 
53  $this->add_hooks();
54  }
55 
56  /**
57  * Add filters and actions for the templates
58  *
59  * @since 1.15
60  */
61  private function add_hooks() {
62 
63  add_filter( 'gravityview_register_directory_template', array( $this, 'register_template' ) );
64 
65  // presets hooks:
66  // form xml
67  add_filter( 'gravityview_template_formxml', array( $this, 'assign_form_xml' ), 10, 2 );
68  // fields config xml
69  add_filter( 'gravityview_template_fieldsxml', array( $this, 'assign_fields_xml' ), 10, 2 );
70 
71  // assign active areas
72  add_filter( 'gravityview_template_active_areas', array( $this, 'assign_active_areas' ), 10, 3 );
73 
74  // field options
75  add_filter( 'gravityview_template_field_options', array( $this, 'assign_field_options' ), 10, 4 );
76 
77  // template slug
78  add_filter( "gravityview_template_slug_{$this->template_id}", array( $this, 'assign_view_slug' ), 10, 2 );
79 
80  // register template CSS
81  add_action( 'wp_enqueue_scripts', array( $this, 'register_styles' ) );
82  }
83 
84  /**
85  * Merge the template settings with the default settings
86  *
87  * Sets the `settings` object var.
88  *
89  * @param array $settings Defined template settings
90  *
91  * @return array Merged template settings.
92  */
93  private function merge_defaults( $settings = array() ) {
94 
95  $defaults = array(
96  'slug' => '',
97  'css_source' => '',
98  'type' => '',
99  'label' => '',
100  'description' => '',
101  'logo' => '',
102  'preview' => '',
103  'buy_source' => '',
104  'preset_form' => '',
105  'preset_fields' => ''
106  );
107 
108  $this->settings = wp_parse_args( $settings, $defaults );
109 
110  return $this->settings;
111  }
112 
113  /**
114  * Register the template to display in the admin
115  *
116  * @access private
117  *
118  * @param mixed $templates
119  *
120  * @return array Array of templates available for GV
121  */
122  public function register_template( $templates ) {
124 
125  return $templates;
126  }
127 
128 
129  /**
130  * Assign active areas (for admin configuration)
131  *
132  * @access protected
133  *
134  * @param array $areas
135  * @param string $template (default: '')
136  *
137  * @return array Array of active areas
138  */
139  public function assign_active_areas( $areas, $template = '', $context = 'directory' ) {
140  if ( $this->template_id === $template ) {
141  $areas = $this->get_active_areas( $context );
142  }
143 
144  return $areas;
145  }
146 
147  public function get_active_areas( $context ) {
148  if ( isset( $this->active_areas[ $context ] ) ) {
149  return $this->active_areas[ $context ];
150  } else {
151  return $this->active_areas;
152  }
153  }
154 
155 
156  /**
157  * Assign template specific field options
158  *
159  * @param array $options (default: array())
160  * @param string $template (default: '')
161  * @param string $field_id key for the field
162  * @param string|array $context Context for the field; `directory` or `single` for example.
163  *
164  * @return array Array of field options
165  */
166  public function assign_field_options( $field_options, $template_id, $field_id = NULL, $context = 'directory' ) {
167 
168  if ( $this->template_id === $template_id ) {
169 
170  foreach ( $this->field_options as $key => $field_option ) {
171 
172  $field_context = rgar( $field_option, 'context' );
173 
174  // Does the field option only apply to a certain context?
175  // You can define multiple contexts as an array: `context => array("directory", "single")`
176  $context_matches = is_array( $field_context ) ? in_array( $context, $field_context ) : $context === $field_context;
177 
178  // If the context matches (or isn't defined), add the field options.
179  if ( $context_matches ) {
180  $field_options[ $key ] = $field_option;
181  }
182  }
183  }
184 
185  return $field_options;
186  }
187 
188  /**
189  * Set the Gravity Forms import form information by using the `preset_form` field defined in the template.
190  *
191  * @see GravityView_Admin_Views::pre_get_form_fields()
192  * @see GravityView_Admin_Views::create_preset_form()
193  * @return string Path to XML file
194  */
195  public function assign_form_xml( $xml = '', $template = '' ) {
196  if ( $this->settings['type'] === 'preset' && ! empty( $this->settings['preset_form'] ) && $this->template_id === $template ) {
197  return $this->settings['preset_form'];
198  }
199 
200  return $xml;
201  }
202 
203  /**
204  * Set the Gravity Forms import form by using the `preset_fields` field defined in the template.
205  *
206  * @see GravityView_Admin_Views::pre_get_form_fields()
207  * @return string Path to XML file
208  */
209  public function assign_fields_xml( $xml = '', $template = '' ) {
210  if ( $this->settings['type'] === 'preset' && ! empty( $this->settings['preset_fields'] ) && $this->template_id === $template ) {
211  return $this->settings['preset_fields'];
212  }
213 
214  return $xml;
215  }
216 
217 
218  /**
219  * Assign the template slug when loading the presentation template (frontend)
220  *
221  * @access protected
222  *
223  * @param mixed $default
224  *
225  * @return string
226  */
227  public function assign_view_slug( $default, $context ) {
228 
229  if ( ! empty( $this->settings['slug'] ) ) {
230  return $this->settings['slug'];
231  }
232  if ( ! empty( $default ) ) {
233  return $default;
234  }
235 
236  // last resort, template_id
237  return $this->template_id;
238  }
239 
240  /**
241  * If the template has a CSS file defined in the `css_source` setting, register it
242  * It will be registered using `gravityview_style_{template_id}` format
243  *
244  * @return void
245  */
246  public function register_styles() {
247  if ( ! empty( $this->settings['css_source'] ) ) {
248  wp_register_style( 'gravityview_style_' . $this->template_id, $this->settings['css_source'], array(), GravityView_Plugin::version, 'all' );
249  }
250  }
251 
252 }
register_styles()
If the template has a CSS file defined in the css_source setting, register it It will be registered u...
assign_view_slug($default, $context)
Assign the template slug when loading the presentation template (frontend)
Class used to register a new template to be shown in GravityView presets.
__construct($id, $settings=array(), $field_options=array(), $areas=array())
$templates
assign_form_xml($xml= '', $template= '')
Set the Gravity Forms import form information by using the preset_form field defined in the template...
$field_id
Definition: time.php:17
register_template($templates)
Register the template to display in the admin.
assign_fields_xml($xml= '', $template= '')
Set the Gravity Forms import form by using the preset_fields field defined in the template...
add_hooks()
Add filters and actions for the templates.
assign_field_options($field_options, $template_id, $field_id=NULL, $context= 'directory')
Assign template specific field options.
assign_active_areas($areas, $template= '', $context= 'directory')
Assign active areas (for admin configuration)
merge_defaults($settings=array())
Merge the template settings with the default settings.