1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace GV\Shortcodes; |
3
|
|
|
|
4
|
|
|
/** If this file is called directly, abort. */ |
5
|
|
|
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) { |
6
|
|
|
die(); |
7
|
|
|
} |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The [gvfield] shortcode. |
11
|
|
|
*/ |
12
|
|
|
class gvfield extends \GV\Shortcode { |
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* {@inheritDoc} |
15
|
|
|
*/ |
16
|
|
|
public $name = 'gvfield'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Process and output the [gvfield] shortcode. |
20
|
|
|
* |
21
|
|
|
* @param array $atts The attributes passed. |
22
|
|
|
* @param string $content The content inside the shortcode. |
23
|
|
|
* |
24
|
|
|
* @return string The output. |
25
|
|
|
*/ |
26
|
3 |
|
public function callback( $atts, $content = null ) { |
27
|
3 |
|
$request = gravityview()->request; |
28
|
|
|
|
29
|
3 |
|
if ( $request->is_admin() ) { |
30
|
1 |
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', null, null, $atts ); |
31
|
|
|
} |
32
|
|
|
|
33
|
3 |
|
$atts = wp_parse_args( $atts, array( |
34
|
3 |
|
'view_id' => null, |
35
|
|
|
'entry_id' => null, |
36
|
|
|
'field_id' => null, |
37
|
|
|
) ); |
38
|
|
|
|
39
|
3 |
|
$atts = gv_map_deep( $atts, array( 'GravityView_Merge_Tags', 'replace_get_variables' ) ); |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @filter `gravityview/shortcodes/gvfield/atts` Filter the [gvfield] shortcode attributes. |
43
|
|
|
* @param array $atts The initial attributes. |
44
|
|
|
* @since 2.0 |
45
|
|
|
*/ |
46
|
3 |
|
$atts = apply_filters( 'gravityview/shortcodes/gvfield/atts', $atts ); |
47
|
|
|
|
48
|
3 |
|
if ( ! $view = \GV\View::by_id( $atts['view_id'] ) ) { |
49
|
1 |
|
gravityview()->log->error( 'View #{view_id} not found', array( 'view_id' => $atts['view_id'] ) ); |
50
|
1 |
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, null, null, $atts ); |
51
|
|
|
} |
52
|
|
|
|
53
|
3 |
|
switch( $atts['entry_id'] ): |
54
|
3 |
|
case 'last': |
55
|
3 |
|
if ( gravityview()->plugin->supports( \GV\Plugin::FEATURE_GFQUERY ) ) { |
56
|
|
|
/** |
57
|
|
|
* @todo Remove once we refactor the use of get_view_entries_parameters. |
58
|
|
|
* |
59
|
|
|
* Since we're using \GF_Query shorthand initialization we have to reverse the order parameters here. |
60
|
|
|
*/ |
61
|
3 |
|
add_filter( 'gravityview_get_entries', $filter = function( $parameters, $args, $form_id ) { |
|
|
|
|
62
|
3 |
|
if ( ! empty( $parameters['sorting'] ) ) { |
63
|
|
|
/** |
64
|
|
|
* Reverse existing sorts. |
65
|
|
|
*/ |
66
|
1 |
|
$sort = &$parameters['sorting']; |
67
|
1 |
|
$sort['direction'] = $sort['direction'] == 'RAND' ? : ( $sort['direction'] == 'ASC' ? 'DESC' : 'ASC' ); |
68
|
|
|
} else { |
69
|
|
|
/** |
70
|
|
|
* Otherwise, sort by date_created. |
71
|
|
|
*/ |
72
|
2 |
|
$parameters['sorting'] = array( |
73
|
|
|
'key' => 'id', |
74
|
|
|
'direction' => 'ASC', |
75
|
|
|
'is_numeric' => true |
|
|
|
|
76
|
|
|
); |
77
|
|
|
} |
78
|
3 |
|
return $parameters; |
79
|
3 |
|
}, 10, 3 ); |
80
|
|
|
$entries = $view->get_entries( null ); |
81
|
|
|
remove_filter( 'gravityview_get_entries', $filter ); |
82
|
|
|
} else { |
83
|
|
|
$entries = $view->get_entries( null ); |
84
|
|
|
|
85
|
|
|
/** If a sort already exists, reverse it. */ |
86
|
|
|
if ( $sort = end( $entries->sorts ) ) { |
87
|
|
|
$entries = $entries->sort( new \GV\Entry_Sort( $sort->field, $sort->direction == \GV\Entry_Sort::RAND ? : ( $sort->direction == \GV\Entry_Sort::ASC ? \GV\Entry_Sort::DESC : \GV\Entry_Sort::ASC ) ), $sort->mode ); |
|
|
|
|
88
|
|
|
} else { |
89
|
|
|
/** Otherwise, sort by date_created */ |
90
|
|
|
$entries = $entries->sort( new \GV\Entry_Sort( \GV\Internal_Field::by_id( 'id' ), \GV\Entry_Sort::ASC ), \GV\Entry_Sort::NUMERIC ); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ( ! $entry = $entries->first() ) { |
95
|
|
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, null, $atts ); |
96
|
|
|
} |
97
|
|
|
break; |
98
|
|
|
case 'first': |
99
|
|
|
if ( ! $entry = $view->get_entries( null )->first() ) { |
100
|
|
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, null, $atts ); |
101
|
|
|
} |
102
|
|
|
break; |
103
|
|
|
default: |
104
|
|
|
if ( ! $entry = \GV\GF_Entry::by_id( $atts['entry_id'] ) ) { |
105
|
|
|
gravityview()->log->error( 'Entry #{entry_id} not found', array( 'view_id' => $atts['view_id'] ) ); |
106
|
|
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, null, $atts ); |
107
|
|
|
} |
108
|
|
|
endswitch; |
109
|
|
|
|
110
|
|
|
$field = is_numeric( $atts['field_id'] ) ? \GV\GF_Field::by_id( $view->form, $atts['field_id'] ) : \GV\Internal_Field::by_id( $atts['field_id'] ); |
111
|
|
|
|
112
|
|
|
if ( ! $field ) { |
113
|
|
|
gravityview()->log->error( 'Field #{field_id} not found', array( 'view_id' => $atts['field_id'] ) ); |
114
|
|
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $field, $atts ); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ( $view->form->ID != $entry['form_id'] ) { |
118
|
|
|
gravityview()->log->error( 'Entry does not belong to view (form mismatch)' ); |
119
|
|
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ( post_password_required( $view->ID ) ) { |
|
|
|
|
123
|
|
|
gravityview()->log->notice( 'Post password is required for View #{view_id}', array( 'view_id' => $view->ID ) ); |
|
|
|
|
124
|
|
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', get_the_password_form( $view->ID ), $view, $entry, $atts ); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if ( ! $view->form ) { |
128
|
|
|
gravityview()->log->notice( 'View #{id} has no form attached to it.', array( 'id' => $view->ID ) ); |
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* This View has no data source. There's nothing to show really. |
132
|
|
|
* ...apart from a nice message if the user can do anything about it. |
133
|
|
|
*/ |
134
|
|
|
if ( \GVCommon::has_cap( array( 'edit_gravityviews', 'edit_gravityview' ), $view->ID ) ) { |
|
|
|
|
135
|
|
|
$return = __( sprintf( 'This View is not configured properly. Start by <a href="%s">selecting a form</a>.', esc_url( get_edit_post_link( $view->ID, false ) ) ), 'gravityview' ); |
|
|
|
|
136
|
|
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', $return, $view, $entry, $atts ); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** Private, pending, draft, etc. */ |
143
|
|
|
$public_states = get_post_stati( array( 'public' => true ) ); |
144
|
|
|
if ( ! in_array( $view->post_status, $public_states ) && ! \GVCommon::has_cap( 'read_gravityview', $view->ID ) ) { |
|
|
|
|
145
|
|
|
gravityview()->log->notice( 'The current user cannot access this View #{view_id}', array( 'view_id' => $view->ID ) ); |
|
|
|
|
146
|
|
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts ); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** Unapproved entries. */ |
150
|
|
|
if ( $entry['status'] != 'active' ) { |
|
|
|
|
151
|
|
|
gravityview()->log->notice( 'Entry ID #{entry_id} is not active', array( 'entry_id' => $entry->ID ) ); |
152
|
|
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts ); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if ( $view->settings->get( 'show_only_approved' ) ) { |
156
|
|
|
if ( ! \GravityView_Entry_Approval_Status::is_approved( gform_get_meta( $entry->ID, \GravityView_Entry_Approval::meta_key ) ) ) { |
157
|
|
|
gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing', array( 'entry_id' => $entry->ID ) ); |
158
|
|
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts ); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$field->update_configuration( $atts ); |
163
|
|
|
|
164
|
|
|
$renderer = new \GV\Field_Renderer(); |
165
|
|
|
$output = $renderer->render( $field, $view, is_numeric( $field->ID ) ? $view->form : new \GV\Internal_Source(), $entry, gravityview()->request ); |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @filter `gravityview/shortcodes/gvfield/output` Filter the [gvfield] output. |
169
|
|
|
* @param string $output The output. |
170
|
|
|
* @param \GV\View|null $view The View detected or null. |
171
|
|
|
* @param \GV\Entry|null $entry The Entry or null. |
172
|
|
|
* @param \GV\Field|null $field The Field or null. |
173
|
|
|
* |
174
|
|
|
* @since 2.0 |
175
|
|
|
*/ |
176
|
|
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', $output, $view, $entry, $field, $atts ); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
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.