|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* @file class-gravityview-field-workflow_step.php |
|
4
|
|
|
* @since 1.17.2 |
|
5
|
|
|
* @package GravityView |
|
6
|
|
|
* @subpackage includes\fields |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
class GravityView_Field_Workflow_Step extends GravityView_Field { |
|
10
|
|
|
|
|
11
|
|
|
var $name = 'workflow_step'; |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
var $group = 'meta'; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
public function __construct() { |
|
16
|
|
|
$this->label = esc_html__( 'Workflow Step', 'gravityview' ); |
|
17
|
|
|
$this->default_search_label = $this->label; |
|
18
|
|
|
|
|
19
|
|
|
$this->add_hooks(); |
|
20
|
|
|
|
|
21
|
|
|
parent::__construct(); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
function add_hooks() { |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
add_filter( 'gravityview_search_field_label', array( $this, 'modify_gravityview_search_field_step_label' ), 10, 3 ); |
|
27
|
|
|
|
|
28
|
|
|
add_filter( 'gravityview_widget_search_filters', array( $this, 'modify_frontend_search_fields' ), 10, 3 ); |
|
29
|
|
|
|
|
30
|
|
|
add_filter( 'gravityview_field_entry_value_workflow_step', array( $this, 'modify_entry_value_workflow_step' ), 10, 4 ); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get the value of the Workflow Step based on the `workflow_step` entry meta int value |
|
35
|
|
|
* |
|
36
|
|
|
* @uses Gravity_Flow_API::get_current_step |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $output HTML value output |
|
39
|
|
|
* @param array $entry The GF entry array |
|
40
|
|
|
* @param array $field_settings Settings for the particular GV field |
|
41
|
|
|
* @param array $field Current field being displayed |
|
42
|
|
|
* |
|
43
|
|
|
* @since 1.17 |
|
44
|
|
|
* |
|
45
|
|
|
* @return string If Gravity Flow not found, or entry not processed yet, returns initial value. Otherwise, returns name of workflow step. |
|
46
|
|
|
*/ |
|
47
|
|
|
function modify_entry_value_workflow_step( $output, $entry, $field_settings, $field ) { |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
// If not set, the entry hasn't started a workflow |
|
50
|
|
|
$has_workflow_step = isset( $entry['workflow_step'] ); |
|
51
|
|
|
|
|
52
|
|
|
if( $has_workflow_step ) { |
|
53
|
|
|
|
|
54
|
|
|
$GFlow = new Gravity_Flow_API( $entry['form_id'] ); |
|
55
|
|
|
|
|
56
|
|
|
if ( $current_step = $GFlow->get_current_step( $entry ) ) { |
|
57
|
|
|
$output = esc_html( $current_step->get_name() ); |
|
58
|
|
|
} else { |
|
59
|
|
|
$output = esc_html__( 'Workflow Complete', 'gravityview' ); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
unset( $GFlow ); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $output; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get the Workflow Step ID from a search field key |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $key Search field key, in the following format: `workflow_step_status_[number]` |
|
72
|
|
|
* |
|
73
|
|
|
* @return bool|int The ID of the workflow step. False if not a workflow step field key. |
|
74
|
|
|
*/ |
|
75
|
|
|
private function get_step_id_from_key( $key ) { |
|
76
|
|
|
|
|
77
|
|
|
$workflow_step_id = false; |
|
78
|
|
|
|
|
79
|
|
|
preg_match( '/workflow_step_status_(\d+)/', $key, $matches ); |
|
80
|
|
|
|
|
81
|
|
|
if ( ! empty( $matches ) ) { |
|
82
|
|
|
$workflow_step_id = intval( $matches[1] ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $workflow_step_id; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @since 1.17.3 |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $label Existing label text, sanitized. |
|
92
|
|
|
* @param null|GF_Field $gf_field If search field is connected to a Gravity Forms field, the field object. |
|
93
|
|
|
* @param array $field Array with the following keys: `field` ID of the meta key or field ID to be searched, `input` the type of search input to be shown, `label` the existing label. Same as $label parameter. |
|
94
|
|
|
* |
|
95
|
|
|
* @return string If showing a search field for a Step, show the step label. |
|
96
|
|
|
*/ |
|
97
|
|
|
function modify_gravityview_search_field_step_label( $label = '', $gf_field = null, $field = array() ) { |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
$return = $label; |
|
100
|
|
|
|
|
101
|
|
|
if ( '' === $label && $workflow_step_id = $this->get_step_id_from_key( $field['field'] ) ) { |
|
102
|
|
|
|
|
103
|
|
|
$step = $this->get_workflow_step( $workflow_step_id ); |
|
104
|
|
|
|
|
105
|
|
|
$return = esc_html( $step->get_label() ); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $return; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get a Gravity_Flow_Step object from the step ID |
|
113
|
|
|
* |
|
114
|
|
|
* @since 1.17.3 |
|
115
|
|
|
* |
|
116
|
|
|
* @uses GravityView_View::getFormId() to get the current form being searched |
|
117
|
|
|
* @uses Gravity_Flow_API::get_step() |
|
118
|
|
|
* |
|
119
|
|
|
* @param int $workflow_step_id ID of the step |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool|Gravity_Flow_Step |
|
122
|
|
|
*/ |
|
123
|
|
|
function get_workflow_step( $workflow_step_id = 0 ) { |
|
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
$form_id = GravityView_View::getInstance()->getFormId(); |
|
126
|
|
|
|
|
127
|
|
|
$GFlow = new Gravity_Flow_API( $form_id ); |
|
128
|
|
|
|
|
129
|
|
|
$workflow_step = $GFlow->get_step( $workflow_step_id ); |
|
130
|
|
|
|
|
131
|
|
|
if( ! $GFlow || ! $workflow_step ) { |
|
132
|
|
|
return false; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $workflow_step; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Set the search field choices to the Steps available for the current form |
|
140
|
|
|
* |
|
141
|
|
|
* @since 1.17.3 |
|
142
|
|
|
* |
|
143
|
|
|
* @param array $search_fields |
|
144
|
|
|
* @param GravityView_Widget_Search $widget |
|
145
|
|
|
* @param array $widget_args |
|
146
|
|
|
* |
|
147
|
|
|
* @return array |
|
148
|
|
|
*/ |
|
149
|
|
|
function modify_frontend_search_fields( $search_fields = array(), GravityView_Widget_Search $widget, $widget_args = array() ) { |
|
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
foreach ( $search_fields as & $search_field ) { |
|
152
|
|
|
|
|
153
|
|
|
if ( $this->name === $search_field['key'] ) { |
|
154
|
|
|
|
|
155
|
|
|
$form_id = GravityView_View::getInstance()->getFormId(); |
|
156
|
|
|
|
|
157
|
|
|
$workflow_steps = gravity_flow()->get_steps( $form_id ); |
|
158
|
|
|
|
|
159
|
|
|
$choices = array(); |
|
160
|
|
|
|
|
161
|
|
|
foreach ( $workflow_steps as $step ) { |
|
162
|
|
|
$choices[] = array( |
|
163
|
|
|
'text' => $step->get_name(), |
|
164
|
|
|
'value' => $step->get_id(), |
|
165
|
|
|
); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$search_field['choices'] = $choices; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
// Workflow Step Statuses |
|
172
|
|
|
else if ( $workflow_step_id = $this->get_step_id_from_key( $search_field['key'] ) ) { |
|
173
|
|
|
|
|
174
|
|
|
$status_key = sprintf( 'workflow_step_status_%d', $workflow_step_id ); |
|
175
|
|
|
|
|
176
|
|
|
$search_field['choices'] = GravityView_Plugin_Hooks_Gravity_Flow::get_status_options( null, $status_key ); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
return $search_fields; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
new GravityView_Field_Workflow_Step; |
|
186
|
|
|
|
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.