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 |
|
$entries = $view->get_entries(); |
57
|
|
|
|
58
|
|
|
/** If a sort already exists, reverse it. */ |
59
|
3 |
|
if ( $sort = end( $entries->sorts ) ) { |
60
|
|
|
// @todo For multisorts we need to do this for each sort |
61
|
1 |
|
$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 ); |
|
|
|
|
62
|
|
|
} else { |
63
|
|
|
/** Otherwise, sort by date_created */ |
64
|
2 |
|
$entries = $entries->sort( new \GV\Entry_Sort( \GV\Internal_Field::by_id( 'id' ), \GV\Entry_Sort::ASC ), \GV\Entry_Sort::NUMERIC ); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
if ( ! $entry = $entries->first() ) { |
68
|
1 |
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, null, $atts ); |
69
|
|
|
} |
70
|
2 |
|
break; |
71
|
2 |
|
case 'first': |
72
|
2 |
|
if ( ! $entry = $view->get_entries( null )->first() ) { |
73
|
1 |
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, null, $atts ); |
74
|
|
|
} |
75
|
2 |
|
break; |
76
|
|
|
default: |
77
|
2 |
|
if ( ! $entry = \GV\GF_Entry::by_id( $atts['entry_id'] ) ) { |
78
|
1 |
|
gravityview()->log->error( 'Entry #{entry_id} not found', array( 'view_id' => $atts['view_id'] ) ); |
79
|
1 |
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, null, $atts ); |
80
|
|
|
} |
81
|
|
|
endswitch; |
82
|
|
|
|
83
|
3 |
|
$field = is_numeric( $atts['field_id'] ) ? \GV\GF_Field::by_id( $view->form, $atts['field_id'] ) : \GV\Internal_Field::by_id( $atts['field_id'] ); |
84
|
|
|
|
85
|
3 |
|
if ( ! $field ) { |
86
|
1 |
|
gravityview()->log->error( 'Field #{field_id} not found', array( 'view_id' => $atts['field_id'] ) ); |
87
|
1 |
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $field, $atts ); |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
if ( $view->form->ID != $entry['form_id'] ) { |
91
|
|
|
gravityview()->log->error( 'Entry does not belong to view (form mismatch)' ); |
92
|
|
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts ); |
93
|
|
|
} |
94
|
|
|
|
95
|
2 |
|
if ( post_password_required( $view->ID ) ) { |
|
|
|
|
96
|
|
|
gravityview()->log->notice( 'Post password is required for View #{view_id}', array( 'view_id' => $view->ID ) ); |
|
|
|
|
97
|
|
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', get_the_password_form( $view->ID ), $view, $entry, $atts ); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
if ( ! $view->form ) { |
101
|
|
|
gravityview()->log->notice( 'View #{id} has no form attached to it.', array( 'id' => $view->ID ) ); |
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* This View has no data source. There's nothing to show really. |
105
|
|
|
* ...apart from a nice message if the user can do anything about it. |
106
|
|
|
*/ |
107
|
|
|
if ( \GVCommon::has_cap( array( 'edit_gravityviews', 'edit_gravityview' ), $view->ID ) ) { |
|
|
|
|
108
|
|
|
$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' ); |
|
|
|
|
109
|
|
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', $return, $view, $entry, $atts ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** Private, pending, draft, etc. */ |
116
|
2 |
|
$public_states = get_post_stati( array( 'public' => true ) ); |
117
|
2 |
|
if ( ! in_array( $view->post_status, $public_states ) && ! \GVCommon::has_cap( 'read_gravityview', $view->ID ) ) { |
|
|
|
|
118
|
|
|
gravityview()->log->notice( 'The current user cannot access this View #{view_id}', array( 'view_id' => $view->ID ) ); |
|
|
|
|
119
|
|
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** Unapproved entries. */ |
123
|
2 |
|
if ( $entry['status'] != 'active' ) { |
124
|
|
|
gravityview()->log->notice( 'Entry ID #{entry_id} is not active', array( 'entry_id' => $entry->ID ) ); |
125
|
|
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts ); |
126
|
|
|
} |
127
|
|
|
|
128
|
2 |
|
if ( $view->settings->get( 'show_only_approved' ) ) { |
129
|
|
|
if ( ! \GravityView_Entry_Approval_Status::is_approved( gform_get_meta( $entry->ID, \GravityView_Entry_Approval::meta_key ) ) ) { |
130
|
|
|
gravityview()->log->error( 'Entry ID #{entry_id} is not approved for viewing', array( 'entry_id' => $entry->ID ) ); |
131
|
|
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', '', $view, $entry, $atts ); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
2 |
|
$field->update_configuration( $atts ); |
136
|
|
|
|
137
|
2 |
|
$renderer = new \GV\Field_Renderer(); |
138
|
2 |
|
$output = $renderer->render( $field, $view, is_numeric( $field->ID ) ? $view->form : new \GV\Internal_Source(), $entry, gravityview()->request ); |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @filter `gravityview/shortcodes/gvfield/output` Filter the [gvfield] output. |
142
|
|
|
* @param string $output The output. |
143
|
|
|
* @param \GV\View|null $view The View detected or null. |
144
|
|
|
* @param \GV\Entry|null $entry The Entry or null. |
145
|
|
|
* @param \GV\Field|null $field The Field or null. |
146
|
|
|
* |
147
|
|
|
* @since 2.0 |
148
|
|
|
*/ |
149
|
2 |
|
return apply_filters( 'gravityview/shortcodes/gvfield/output', $output, $view, $entry, $field, $atts ); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
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.