|
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 [gventry] shortcode. |
|
11
|
|
|
*/ |
|
12
|
|
|
class gventry extends \GV\Shortcode { |
|
13
|
|
|
/** |
|
14
|
|
|
* {@inheritDoc} |
|
15
|
|
|
*/ |
|
16
|
|
|
public $name = 'gventry'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Process and output the [gventry] 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
|
2 |
|
public function callback( $atts, $content = '', $tag = '' ) { |
|
28
|
2 |
|
$request = gravityview()->request; |
|
29
|
|
|
|
|
30
|
2 |
|
if ( $request->is_admin() ) { |
|
31
|
1 |
|
return apply_filters( 'gravityview/shortcodes/gventry/output', '', null, null, $atts ); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
2 |
|
$atts = wp_parse_args( $atts, array( |
|
35
|
2 |
|
'id' => 0, |
|
36
|
|
|
'entry_id' => 0, |
|
37
|
|
|
'view_id' => 0, |
|
38
|
|
|
'edit' => 0, |
|
39
|
|
|
) ); |
|
40
|
|
|
|
|
41
|
2 |
|
$atts = gv_map_deep( $atts, array( 'GravityView_Merge_Tags', 'replace_get_variables' ) ); |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @filter `gravityview/shortcodes/gventry/atts` Filter the [gventry] shortcode attributes. |
|
45
|
|
|
* @param array $atts The initial attributes. |
|
46
|
|
|
* @since 2.0 |
|
47
|
|
|
*/ |
|
48
|
2 |
|
$atts = apply_filters( 'gravityview/shortcodes/gventry/atts', $atts ); |
|
49
|
|
|
|
|
50
|
2 |
|
$view = \GV\View::by_id( $atts['view_id'] ); |
|
51
|
|
|
|
|
52
|
2 |
|
if ( ! $view ) { |
|
53
|
1 |
|
gravityview()->log->error( 'View does not exist #{view_id}', array( 'view_id' => $atts['view_id'] ) ); |
|
54
|
1 |
|
return apply_filters( 'gravityview/shortcodes/gventry/output', '', null, null, $atts ); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
$entry_id = ! empty( $atts['entry_id'] ) ? $atts['entry_id'] : $atts['id']; |
|
58
|
|
|
|
|
59
|
2 |
|
switch( $entry_id ): |
|
60
|
2 |
|
case 'last': |
|
61
|
2 |
|
$entries = $view->get_entries( null ); |
|
62
|
|
|
|
|
63
|
|
|
/** If a sort already exists, reverse it. */ |
|
64
|
2 |
|
if ( $sort = end( $entries->sorts ) ) { |
|
65
|
|
|
$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 ); |
|
|
|
|
|
|
66
|
|
|
} else { |
|
67
|
|
|
/** Otherwise, sort by date_created */ |
|
68
|
2 |
|
$entries = $entries->sort( new \GV\Entry_Sort( \GV\Internal_Field::by_id( 'id' ), \GV\Entry_Sort::ASC ), \GV\Entry_Sort::NUMERIC ); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
if ( ! $entry = $entries->first() ) { |
|
72
|
1 |
|
return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, null, $atts ); |
|
73
|
|
|
} |
|
74
|
1 |
|
break; |
|
75
|
2 |
|
case 'first': |
|
76
|
2 |
|
if ( ! $entry = $view->get_entries( null )->first() ) { |
|
77
|
1 |
|
return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, null, $atts ); |
|
78
|
|
|
} |
|
79
|
1 |
|
break; |
|
80
|
|
|
default: |
|
81
|
2 |
|
if ( ! $entry = \GV\GF_Entry::by_id( $entry_id ) ) { |
|
82
|
1 |
|
gravityview()->log->error( 'Entry #{entry_id} not found', array( 'view_id' => $atts['view_id'] ) ); |
|
83
|
1 |
|
return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, null, $atts ); |
|
84
|
|
|
} |
|
85
|
|
|
endswitch; |
|
86
|
|
|
|
|
87
|
2 |
|
if ( $view->form->ID != $entry['form_id'] ) { |
|
88
|
1 |
|
gravityview()->log->error( 'Entry does not belong to view (form mismatch)' ); |
|
89
|
1 |
|
return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, $entry, $atts ); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
2 |
|
if ( post_password_required( $view->ID ) ) { |
|
|
|
|
|
|
93
|
1 |
|
gravityview()->log->notice( 'Post password is required for View #{view_id}', array( 'view_id' => $view->ID ) ); |
|
|
|
|
|
|
94
|
1 |
|
return apply_filters( 'gravityview/shortcodes/gventry/output', get_the_password_form( $view->ID ), $view, $entry, $atts ); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
if ( ! $view->form ) { |
|
98
|
|
|
gravityview()->log->notice( 'View #{id} has no form attached to it.', array( 'id' => $view->ID ) ); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* This View has no data source. There's nothing to show really. |
|
102
|
|
|
* ...apart from a nice message if the user can do anything about it. |
|
103
|
|
|
*/ |
|
104
|
|
|
if ( \GVCommon::has_cap( array( 'edit_gravityviews', 'edit_gravityview' ), $view->ID ) ) { |
|
|
|
|
|
|
105
|
|
|
$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' ); |
|
|
|
|
|
|
106
|
|
|
return apply_filters( 'gravityview/shortcodes/gventry/output', $return, $view, $entry, $atts ); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, $entry, $atts ); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** Private, pending, draft, etc. */ |
|
113
|
1 |
|
$public_states = get_post_stati( array( 'public' => true ) ); |
|
114
|
1 |
|
if ( ! in_array( $view->post_status, $public_states ) && ! \GVCommon::has_cap( 'read_gravityview', $view->ID ) ) { |
|
|
|
|
|
|
115
|
|
|
gravityview()->log->notice( 'The current user cannot access this View #{view_id}', array( 'view_id' => $view->ID ) ); |
|
|
|
|
|
|
116
|
|
|
return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, $entry, $atts ); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** Unapproved entries. */ |
|
120
|
1 |
|
if ( $entry['status'] != 'active' ) { |
|
121
|
|
|
gravityview()->log->notice( 'Entry ID #{entry_id} is not active', array( 'entry_id' => $entry->ID ) ); |
|
122
|
|
|
return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, $entry, $atts ); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
1 |
|
$is_admin_and_can_view = $view->settings->get( 'admin_show_all_statuses' ) && \GVCommon::has_cap('gravityview_moderate_entries', $view->ID ); |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
1 |
|
if ( $view->settings->get( 'show_only_approved' ) && ! $is_admin_and_can_view ) { |
|
128
|
|
|
if ( ! \GravityView_Entry_Approval_Status::is_approved( gform_get_meta( $entry->ID, \GravityView_Entry_Approval::meta_key ) ) ) { |
|
129
|
|
|
gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing', array( 'entry_id' => $entry->ID ) ); |
|
130
|
|
|
return apply_filters( 'gravityview/shortcodes/gventry/output', '', $view, $entry, $atts ); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
if ( $atts['edit'] ) { |
|
135
|
|
|
/** |
|
136
|
|
|
* Based on code in our unit-tests. |
|
137
|
|
|
* Mocks old context, etc. |
|
138
|
|
|
*/ |
|
139
|
|
|
$loader = \GravityView_Edit_Entry::getInstance(); |
|
140
|
|
|
$render = $loader->instances['render']; |
|
141
|
|
|
|
|
142
|
|
|
add_filter( 'gravityview/is_single_entry', '__return_true' ); |
|
143
|
|
|
|
|
144
|
|
|
$form = \GFAPI::get_form( $entry['form_id'] ); |
|
145
|
|
|
|
|
146
|
|
|
$data = \GravityView_View_Data::getInstance( $view ); |
|
147
|
|
|
$template = \GravityView_View::getInstance( array( |
|
148
|
|
|
'form' => $form, |
|
149
|
|
|
'form_id' => $form['id'], |
|
150
|
|
|
'view_id' => $view->ID, |
|
|
|
|
|
|
151
|
|
|
'entries' => array( $entry ), |
|
152
|
|
|
'atts' => \GVCommon::get_template_settings( $view->ID ), |
|
|
|
|
|
|
153
|
|
|
) ); |
|
154
|
|
|
|
|
155
|
|
|
$_GET['edit'] = wp_create_nonce( |
|
156
|
|
|
\GravityView_Edit_Entry::get_nonce_key( $view->ID, $form['id'], $entry['id'] ) |
|
|
|
|
|
|
157
|
|
|
); |
|
158
|
|
|
|
|
159
|
|
|
add_filter( 'gravityview/edit_entry/success', $callback = function( $message ) use ( $view, $entry, $atts ) { |
|
|
|
|
|
|
160
|
|
|
$message = __( 'Entry Updated', 'gravityview' ); |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @filter `gravityview/shortcodes/gventry/edit/success` Modify the edit entry success message in [gventry]. |
|
164
|
|
|
* @since develop |
|
165
|
|
|
* @param[in,out] string $message The message. |
|
166
|
|
|
* @param \GV\View $view The View. |
|
167
|
|
|
* @param \GV\Entry $entry The entry. |
|
168
|
|
|
* @param array $atts The attributes. |
|
169
|
|
|
*/ |
|
170
|
|
|
return apply_filters( 'gravityview/shortcodes/gventry/edit/success', $message, $view, $entry, $atts ); |
|
171
|
|
|
} ); |
|
172
|
|
|
|
|
173
|
|
|
ob_start() && $render->init( $data, \GV\Entry::by_id( $entry['id'] ), $view ); |
|
174
|
|
|
$output = ob_get_clean(); // Render :) |
|
175
|
|
|
|
|
176
|
|
|
remove_filter( 'gravityview/is_single_entry', '__return_true' ); |
|
177
|
|
|
remove_filter( 'gravityview/edit_entry/success', $callback ); |
|
178
|
|
|
} else { |
|
179
|
|
|
/** Remove the back link. */ |
|
180
|
1 |
|
add_filter( 'gravityview/template/links/back/url', '__return_false' ); |
|
181
|
|
|
|
|
182
|
1 |
|
$renderer = new \GV\Entry_Renderer(); |
|
183
|
|
|
|
|
184
|
1 |
|
$request = new \GV\Mock_Request(); |
|
185
|
1 |
|
$request->returns['is_entry'] = $entry; |
|
186
|
|
|
|
|
187
|
1 |
|
$output = $renderer->render( $entry, $view, $request ); |
|
188
|
|
|
|
|
189
|
1 |
|
remove_filter( 'gravityview/template/links/back/url', '__return_false' ); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @filter `gravityview/shortcodes/gventry/output` Filter the [gventry] output. |
|
194
|
|
|
* @param string $output The output. |
|
195
|
|
|
* @param \GV\View|null $view The View detected or null. |
|
196
|
|
|
* @param \GV\Entry|null $entry The Entry or null. |
|
197
|
|
|
* |
|
198
|
|
|
* @since 2.0 |
|
199
|
|
|
*/ |
|
200
|
1 |
|
return apply_filters( 'gravityview/shortcodes/gventry/output', $output, $view, $entry, $atts ); |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
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
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.