1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* @file class-gravityview-field-custom.php |
4
|
|
|
* @package GravityView |
5
|
|
|
* @subpackage includes\fields |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Add custom options for Code field |
10
|
|
|
* @since 1.2 |
11
|
|
|
*/ |
12
|
|
|
class GravityView_Field_Custom extends GravityView_Field { |
13
|
|
|
|
14
|
|
|
var $name = 'custom'; |
|
|
|
|
15
|
|
|
|
16
|
|
|
var $contexts = array( 'single', 'multiple', 'edit' ); |
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var bool |
20
|
|
|
* @since 1.15.3 |
21
|
|
|
*/ |
22
|
|
|
var $is_sortable = false; |
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var bool |
26
|
|
|
* @since 1.15.3 |
27
|
|
|
*/ |
28
|
|
|
var $is_searchable = false; |
|
|
|
|
29
|
|
|
|
30
|
|
|
var $group = 'gravityview'; |
|
|
|
|
31
|
|
|
|
32
|
|
|
public function __construct() { |
33
|
|
|
|
34
|
|
|
$this->label = esc_html__( 'Custom Content', 'gravityview' ); |
35
|
|
|
|
36
|
|
|
add_filter( 'gravityview/edit_entry/form_fields', array( $this, 'show_field_in_edit_entry' ), 10, 4 ); |
37
|
|
|
|
38
|
|
|
parent::__construct(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
function field_options( $field_options, $template_id, $field_id, $context, $input_type ) { |
|
|
|
|
42
|
|
|
|
43
|
|
|
unset ( $field_options['search_filter'], $field_options['show_as_link'] ); |
|
|
|
|
44
|
|
|
|
45
|
|
|
$new_fields = array( |
46
|
|
|
'content' => array( |
47
|
|
|
'type' => 'textarea', |
48
|
|
|
'label' => __( 'Custom Content', 'gravityview' ), |
49
|
|
|
'desc' => sprintf( __( 'Enter text or HTML. Also supports shortcodes. You can show or hide data using the %s shortcode (%slearn more%s).', 'gravityview' ), '<code>[gvlogic]</code>', '<a href="http://docs.gravityview.co/article/252-gvlogic-shortcode">', '</a>' ), |
50
|
|
|
'value' => '', |
51
|
|
|
'class' => 'code', |
52
|
|
|
'merge_tags' => 'force', |
53
|
|
|
'show_all_fields' => true, // Show the `{all_fields}` and `{pricing_fields}` merge tags |
54
|
|
|
), |
55
|
|
|
'wpautop' => array( |
56
|
|
|
'type' => 'checkbox', |
57
|
|
|
'label' => __( 'Automatically add paragraphs to content', 'gravityview' ), |
58
|
|
|
'tooltip' => __( 'Wrap each block of text in an HTML paragraph tag (recommended for text).', 'gravityview' ), |
59
|
|
|
'value' => '', |
60
|
|
|
), |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
if( 'edit' === $context ) { |
64
|
|
|
unset( $field_options['custom_label'], $field_options['show_label'], $field_options['allow_edit_cap'], $new_fields['wpautop'] ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $new_fields + $field_options; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Adds the GravityView Custom Content field to the Edit Entry form |
72
|
|
|
* |
73
|
|
|
* It does this by pretending to be a HTML field so that Gravity Forms displays it |
74
|
|
|
* |
75
|
|
|
* @since 1.19.2 |
76
|
|
|
* |
77
|
|
|
* @param GF_Field[] $fields Gravity Forms form fields |
78
|
|
|
* @param array|null $edit_fields Fields for the Edit Entry tab configured in the View Configuration |
79
|
|
|
* @param array $form GF Form array (`fields` key modified to have only fields configured to show in Edit Entry) |
80
|
|
|
* @param int $view_id View ID |
81
|
|
|
* |
82
|
|
|
* @return GF_Field[] If Custom Content field exists, returns fields array with the fields inserted. Otherwise, returns unmodified fields array. |
83
|
|
|
*/ |
84
|
|
|
public function show_field_in_edit_entry( $fields, $edit_fields = null, $form, $view_id ) { |
|
|
|
|
85
|
|
|
|
86
|
|
|
// Not configured; show all fields. |
87
|
|
|
if ( is_null( $edit_fields ) ) { |
88
|
|
|
return $fields; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$new_fields = array(); |
92
|
|
|
$i = 0; |
93
|
|
|
|
94
|
|
|
$entry = GravityView_View::getInstance()->getCurrentEntry(); |
95
|
|
|
|
96
|
|
|
// Loop through the configured Edit Entry fields and add Custom Content fields if there are any |
97
|
|
|
// TODO: Make this available to other custom GV field types |
98
|
|
|
foreach ( (array) $edit_fields as $edit_field ) { |
99
|
|
|
|
100
|
|
|
if( 'custom' === rgar( $edit_field, 'id') ) { |
|
|
|
|
101
|
|
|
|
102
|
|
|
$field_data = array( |
103
|
|
|
'label' => rgar( $edit_field, 'custom_label' ), |
104
|
|
|
'customLabel' => rgar( $edit_field, 'custom_label' ), |
105
|
|
|
'content' => rgar( $edit_field, 'content' ), |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
// Replace merge tags in the content |
109
|
|
|
foreach ( $field_data as $key => $field_datum ) { |
110
|
|
|
$field_data[ $key ] = GravityView_Merge_Tags::replace_variables( $field_datum, $form, $entry, false, false ); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$field_data['cssClass'] = rgar( $edit_field, 'custom_class' ); |
114
|
|
|
|
115
|
|
|
$new_fields[] = new GF_Field_HTML( $field_data ); |
116
|
|
|
|
117
|
|
|
} else { |
118
|
|
|
$new_fields[] = $fields[ $i ]; |
119
|
|
|
$i++; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $new_fields; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
new GravityView_Field_Custom; |
130
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.