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