|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* @file class-gravityview-field-post-custom-field.php |
|
4
|
|
|
* @package GravityView |
|
5
|
|
|
* @subpackage includes\fields |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
class GravityView_Field_Post_Custom_Field extends GravityView_Field { |
|
9
|
|
|
|
|
10
|
|
|
var $name = 'post_custom_field'; |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
var $is_searchable = true; |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
var $_gf_field_class_name = 'GF_Field_Post_Custom_Field'; |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
var $group = 'post'; |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
public function __construct() { |
|
19
|
|
|
$this->label = esc_html__( 'Post Custom Field', 'gravityview' ); |
|
20
|
|
|
parent::__construct(); |
|
21
|
|
|
|
|
22
|
|
|
$this->add_hooks(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Add hooks for the field |
|
27
|
|
|
* |
|
28
|
|
|
* @since 1.17 |
|
29
|
|
|
* |
|
30
|
|
|
* @return void |
|
31
|
|
|
*/ |
|
32
|
|
|
private function add_hooks() { |
|
33
|
|
|
add_filter( 'gravityview/edit_entry/field_value_post_custom_field', array( $this, 'edit_entry_field_value'), 10, 2 ); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Fix "List" Field Type pre-population of content in Edit Entry mode |
|
38
|
|
|
* |
|
39
|
|
|
* @since 1.17 |
|
40
|
|
|
* |
|
41
|
|
|
* @param mixed $field_value field value used to populate the input |
|
42
|
|
|
* @param GF_Field $field Gravity Forms field object |
|
43
|
|
|
* |
|
44
|
|
|
* @return mixed If a List input for Custom Field, returns JSON-decoded value. Otherwise, original value. |
|
45
|
|
|
*/ |
|
46
|
|
|
public function edit_entry_field_value( $field_value, $field ) { |
|
47
|
|
|
|
|
48
|
|
|
if( 'list' === $field->inputType ) { |
|
49
|
|
|
$field_value = is_string( $field_value ) ? json_decode( $field_value, true ) : $field_value; |
|
50
|
|
|
|
|
51
|
|
|
if ( ! is_array( $field_value ) ) { |
|
52
|
|
|
do_action( 'gravityview_log_error', __METHOD__ . ': "List" Custom Field value not an array or string.', compact( 'field_value', 'field' ) ); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $field_value; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
new GravityView_Field_Post_Custom_Field; |
|
62
|
|
|
|
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.