@@ -14,520 +14,520 @@ |
||
14 | 14 | |
15 | 15 | /** If this file is called directly, abort. */ |
16 | 16 | if (!defined('GRAVITYVIEW_DIR')) { |
17 | - exit(); |
|
17 | + exit(); |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | class Views_Route extends Route |
21 | 21 | { |
22 | - /** |
|
23 | - * Route Name. |
|
24 | - * |
|
25 | - * @since 2.0 |
|
26 | - * |
|
27 | - * @string |
|
28 | - */ |
|
29 | - protected $route_name = 'views'; |
|
30 | - |
|
31 | - /** |
|
32 | - * Sub type, forms {$namespace}/route_name/{id}/sub_type type endpoints. |
|
33 | - * |
|
34 | - * @since 2.0 |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - protected $sub_type = 'entries'; |
|
39 | - |
|
40 | - /** |
|
41 | - * Get a collection of views. |
|
42 | - * |
|
43 | - * Callback for GET /v1/views/ |
|
44 | - * |
|
45 | - * @param \WP_REST_Request $request Full data about the request. |
|
46 | - * |
|
47 | - * @return \WP_Error|\WP_REST_Response |
|
48 | - */ |
|
49 | - public function get_items($request) |
|
50 | - { |
|
51 | - $page = $request->get_param('page'); |
|
52 | - $limit = $request->get_param('limit'); |
|
53 | - |
|
54 | - $items = \GVCommon::get_all_views([ |
|
55 | - 'posts_per_page' => $limit, |
|
56 | - 'paged' => $page, |
|
57 | - ]); |
|
58 | - |
|
59 | - if (empty($items)) { |
|
60 | - return new \WP_Error('gravityview-no-views', __('No Views found.', 'gravityview')); //@todo message |
|
61 | - } |
|
62 | - |
|
63 | - $data = [ |
|
64 | - 'views' => [], |
|
65 | - 'total' => wp_count_posts('gravityview')->publish, |
|
66 | - ]; |
|
67 | - foreach ($items as $item) { |
|
68 | - $data['views'][] = $this->prepare_view_for_response($item, $request); |
|
69 | - } |
|
70 | - |
|
71 | - return new \WP_REST_Response($data, 200); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Get one view. |
|
76 | - * |
|
77 | - * Callback for /v1/views/{id}/ |
|
78 | - * |
|
79 | - * @since 2.0 |
|
80 | - * |
|
81 | - * @param \WP_REST_Request $request Full data about the request. |
|
82 | - * |
|
83 | - * @return \WP_Error|\WP_REST_Response |
|
84 | - */ |
|
85 | - public function get_item($request) |
|
86 | - { |
|
87 | - $url = $request->get_url_params(); |
|
88 | - |
|
89 | - $view_id = intval($url['id']); |
|
90 | - |
|
91 | - $item = get_post($view_id); |
|
92 | - |
|
93 | - //return a response or error based on some conditional |
|
94 | - if ($item && !is_wp_error($item)) { |
|
95 | - $data = $this->prepare_view_for_response($item, $request); |
|
96 | - |
|
97 | - return new \WP_REST_Response($data, 200); |
|
98 | - } |
|
99 | - |
|
100 | - return new \WP_Error('code', sprintf('A View with ID #%d was not found.', $view_id)); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Prepare the item for the REST response. |
|
105 | - * |
|
106 | - * @since 2.0 |
|
107 | - * |
|
108 | - * @param \GV\View $view The view. |
|
109 | - * @param \GV\Entry $entry WordPress representation of the item. |
|
110 | - * @param \WP_REST_Request $request Request object. |
|
111 | - * @param string $context The context (directory, single) |
|
112 | - * @param string $class The value renderer. Default: null (raw value) |
|
113 | - * |
|
114 | - * @since 2.1 Add value renderer override $class parameter. |
|
115 | - * |
|
116 | - * @return mixed The data that is sent. |
|
117 | - */ |
|
118 | - public function prepare_entry_for_response($view, $entry, \WP_REST_Request $request, $context, $class = null) |
|
119 | - { |
|
120 | - |
|
121 | - // Only output the fields that should be displayed. |
|
122 | - $allowed = []; |
|
123 | - foreach ($view->fields->by_position("{$context}_*")->by_visible($view)->all() as $field) { |
|
124 | - $allowed[] = $field; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * @filter `gravityview/rest/entry/fields` Allow more entry fields that are output in regular REST requests. |
|
129 | - * |
|
130 | - * @param array $allowed The allowed ones, default by_visible, by_position( "context_*" ), i.e. as set in the view. |
|
131 | - * @param \GV\View $view The view. |
|
132 | - * @param \GV\Entry $entry The entry. |
|
133 | - * @param \WP_REST_Request $request Request object. |
|
134 | - * @param string $context The context (directory, single) |
|
135 | - */ |
|
136 | - $allowed_field_ids = apply_filters('gravityview/rest/entry/fields', wp_list_pluck($allowed, 'ID'), $view, $entry, $request, $context); |
|
137 | - |
|
138 | - $allowed = array_filter($allowed, function ($field) use ($allowed_field_ids) { |
|
139 | - return in_array($field->ID, $allowed_field_ids, true); |
|
140 | - }); |
|
141 | - |
|
142 | - // Tack on additional fields if needed |
|
143 | - foreach (array_diff($allowed_field_ids, wp_list_pluck($allowed, 'ID')) as $field_id) { |
|
144 | - $allowed[] = is_numeric($field_id) ? \GV\GF_Field::by_id($view->form, $field_id) : \GV\Internal_Field::by_id($field_id); |
|
145 | - } |
|
146 | - |
|
147 | - $r = new Request($request); |
|
148 | - $return = []; |
|
149 | - |
|
150 | - $renderer = new \GV\Field_Renderer(); |
|
151 | - |
|
152 | - $used_ids = []; |
|
153 | - |
|
154 | - foreach ($allowed as $field) { |
|
155 | - $source = is_numeric($field->ID) ? $view->form : new \GV\Internal_Source(); |
|
156 | - |
|
157 | - $field_id = $field->ID; |
|
158 | - $index = null; |
|
159 | - |
|
160 | - if (!isset($used_ids[$field_id])) { |
|
161 | - $used_ids[$field_id] = 0; |
|
162 | - } else { |
|
163 | - $index = ++$used_ids[$field_id]; |
|
164 | - } |
|
165 | - |
|
166 | - if ($index) { |
|
167 | - /** |
|
168 | - * Modify non-unique IDs (custom, id, etc.) to be unique and not gobbled up. |
|
169 | - */ |
|
170 | - $field_id = sprintf('%s(%d)', $field_id, $index + 1); |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @filter `gravityview/api/field/key` Filter the key name in the results for JSON output. |
|
175 | - * |
|
176 | - * @param string $field_id The ID. Should be unique or keys will be gobbled up. |
|
177 | - * @param \GV\View $view The view. |
|
178 | - * @param \GV\Entry $entry The entry. |
|
179 | - * @param \WP_REST_Request $request Request object. |
|
180 | - * @param string $context The context (directory, single) |
|
181 | - */ |
|
182 | - $field_id = apply_filters('gravityview/api/field/key', $field_id, $view, $entry, $request, $context); |
|
183 | - |
|
184 | - if (!$class && in_array($field->ID, ['custom'])) { |
|
185 | - /** |
|
186 | - * Custom fields (and perhaps some others) will require rendering as they don't |
|
187 | - * contain an intrinsic value (for custom their value is stored in the view and requires a renderer). |
|
188 | - * We force the CSV template to take over in such cases, it's good enough for most cases. |
|
189 | - */ |
|
190 | - $return[$field_id] = $renderer->render($field, $view, $source, $entry, $r, '\GV\Field_CSV_Template'); |
|
191 | - } elseif ($class) { |
|
192 | - $return[$field_id] = $renderer->render($field, $view, $source, $entry, $r, $class); |
|
193 | - } else { |
|
194 | - switch ($field->type) { |
|
195 | - case 'list': |
|
196 | - $return[$field_id] = unserialize($field->get_value($view, $source, $entry, $r)); |
|
197 | - break; |
|
198 | - case 'fileupload': |
|
199 | - case 'business_hours': |
|
200 | - $return[$field_id] = json_decode($field->get_value($view, $source, $entry, $r)); |
|
201 | - break; |
|
202 | - default: |
|
203 | - $return[$field_id] = $field->get_value($view, $source, $entry, $r); |
|
204 | - } |
|
205 | - } |
|
206 | - } |
|
207 | - |
|
208 | - return $return; |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Get entries from a view. |
|
213 | - * |
|
214 | - * Callback for /v1/views/{id}/entries/ |
|
215 | - * |
|
216 | - * @since 2.0 |
|
217 | - * |
|
218 | - * @param \WP_REST_Request $request Full data about the request. |
|
219 | - * |
|
220 | - * @return \WP_Error|\WP_REST_Response |
|
221 | - */ |
|
222 | - public function get_sub_items($request) |
|
223 | - { |
|
224 | - $url = $request->get_url_params(); |
|
225 | - $view_id = intval($url['id']); |
|
226 | - $format = \GV\Utils::get($url, 'format', 'json'); |
|
227 | - |
|
228 | - if ($post_id = $request->get_param('post_id')) { |
|
229 | - global $post; |
|
230 | - |
|
231 | - $post = get_post($post_id); |
|
232 | - |
|
233 | - if (!$post || is_wp_error($post)) { |
|
234 | - return new \WP_Error('gravityview-post-not-found', sprintf('A post with ID #%d was not found.', $post_id)); |
|
235 | - } |
|
236 | - |
|
237 | - $collection = \GV\View_Collection::from_post($post); |
|
238 | - |
|
239 | - if (!$collection->contains($view_id)) { |
|
240 | - return new \WP_Error('gravityview-post-not-contains', sprintf('The post with ID #%d does not contain a View with ID #%d', $post_id, $view_id)); |
|
241 | - } |
|
242 | - } |
|
243 | - |
|
244 | - $view = \GV\View::by_id($view_id); |
|
245 | - |
|
246 | - if ('html' === $format) { |
|
247 | - $renderer = new \GV\View_Renderer(); |
|
248 | - $count = $total = 0; |
|
249 | - |
|
250 | - /** @var \GV\Template_Context $context */ |
|
251 | - add_action('gravityview/template/view/render', function ($context) use (&$count, &$total) { |
|
252 | - $count = $context->entries->count(); |
|
253 | - $total = $context->entries->total(); |
|
254 | - }); |
|
255 | - |
|
256 | - $output = $renderer->render($view, new Request($request)); |
|
257 | - |
|
258 | - /** |
|
259 | - * @filter `gravityview/rest/entries/html/insert_meta` Whether to include `http-equiv` meta tags in the HTML output describing the data |
|
260 | - * |
|
261 | - * @since 2.0 |
|
262 | - * |
|
263 | - * @param bool $insert_meta Add <meta> tags? [Default: true] |
|
264 | - * @param int $count The number of entries being rendered |
|
265 | - * @param \GV\View $view The view. |
|
266 | - * @param \WP_REST_Request $request Request object. |
|
267 | - * @param int $total The number of total entries for the request |
|
268 | - */ |
|
269 | - $insert_meta = apply_filters('gravityview/rest/entries/html/insert_meta', true, $count, $view, $request, $total); |
|
270 | - |
|
271 | - if ($insert_meta) { |
|
272 | - $output = '<meta http-equiv="X-Item-Count" content="'.$count.'" />'.$output; |
|
273 | - $output = '<meta http-equiv="X-Item-Total" content="'.$total.'" />'.$output; |
|
274 | - } |
|
275 | - |
|
276 | - $response = new \WP_REST_Response($output, 200); |
|
277 | - $response->header('X-Item-Count', $count); |
|
278 | - $response->header('X-Item-Total', $total); |
|
279 | - |
|
280 | - return $response; |
|
281 | - } |
|
282 | - |
|
283 | - $entries = $view->get_entries(new Request($request)); |
|
284 | - |
|
285 | - if (!$entries->all()) { |
|
286 | - return new \WP_Error('gravityview-no-entries', __('No Entries found.', 'gravityview')); |
|
287 | - } |
|
288 | - |
|
289 | - if (in_array($format, ['csv', 'tsv'], true)) { |
|
290 | - ob_start(); |
|
291 | - |
|
292 | - $csv_or_tsv = fopen('php://output', 'w'); |
|
293 | - |
|
294 | - /** Da' BOM :) */ |
|
295 | - if (apply_filters('gform_include_bom_export_entries', true, $view->form ? $view->form->form : null)) { |
|
296 | - fputs($csv_or_tsv, "\xef\xbb\xbf"); |
|
297 | - } |
|
298 | - |
|
299 | - $headers_done = false; |
|
300 | - |
|
301 | - // If not "tsv" then use comma |
|
302 | - $delimiter = ('tsv' === $format) ? "\t" : ','; |
|
303 | - |
|
304 | - foreach ($entries->all() as $entry) { |
|
305 | - $entry = $this->prepare_entry_for_response($view, $entry, $request, 'directory', '\GV\Field_CSV_Template'); |
|
306 | - |
|
307 | - if (!$headers_done) { |
|
308 | - $headers_done = fputcsv($csv_or_tsv, array_map(['\GV\Utils', 'strip_excel_formulas'], array_keys($entry)), $delimiter); |
|
309 | - } |
|
310 | - |
|
311 | - fputcsv($csv_or_tsv, array_map(['\GV\Utils', 'strip_excel_formulas'], $entry), $delimiter); |
|
312 | - } |
|
313 | - |
|
314 | - $response = new \WP_REST_Response('', 200); |
|
315 | - $response->header('X-Item-Count', $entries->count()); |
|
316 | - $response->header('X-Item-Total', $entries->total()); |
|
317 | - $response->header('Content-Type', 'text/'.$format); |
|
318 | - |
|
319 | - fflush($csv_or_tsv); |
|
320 | - |
|
321 | - $data = rtrim(ob_get_clean()); |
|
322 | - |
|
323 | - add_filter('rest_pre_serve_request', function () use ($data) { |
|
324 | - echo $data; |
|
22 | + /** |
|
23 | + * Route Name. |
|
24 | + * |
|
25 | + * @since 2.0 |
|
26 | + * |
|
27 | + * @string |
|
28 | + */ |
|
29 | + protected $route_name = 'views'; |
|
30 | + |
|
31 | + /** |
|
32 | + * Sub type, forms {$namespace}/route_name/{id}/sub_type type endpoints. |
|
33 | + * |
|
34 | + * @since 2.0 |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + protected $sub_type = 'entries'; |
|
39 | + |
|
40 | + /** |
|
41 | + * Get a collection of views. |
|
42 | + * |
|
43 | + * Callback for GET /v1/views/ |
|
44 | + * |
|
45 | + * @param \WP_REST_Request $request Full data about the request. |
|
46 | + * |
|
47 | + * @return \WP_Error|\WP_REST_Response |
|
48 | + */ |
|
49 | + public function get_items($request) |
|
50 | + { |
|
51 | + $page = $request->get_param('page'); |
|
52 | + $limit = $request->get_param('limit'); |
|
53 | + |
|
54 | + $items = \GVCommon::get_all_views([ |
|
55 | + 'posts_per_page' => $limit, |
|
56 | + 'paged' => $page, |
|
57 | + ]); |
|
58 | + |
|
59 | + if (empty($items)) { |
|
60 | + return new \WP_Error('gravityview-no-views', __('No Views found.', 'gravityview')); //@todo message |
|
61 | + } |
|
62 | + |
|
63 | + $data = [ |
|
64 | + 'views' => [], |
|
65 | + 'total' => wp_count_posts('gravityview')->publish, |
|
66 | + ]; |
|
67 | + foreach ($items as $item) { |
|
68 | + $data['views'][] = $this->prepare_view_for_response($item, $request); |
|
69 | + } |
|
70 | + |
|
71 | + return new \WP_REST_Response($data, 200); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Get one view. |
|
76 | + * |
|
77 | + * Callback for /v1/views/{id}/ |
|
78 | + * |
|
79 | + * @since 2.0 |
|
80 | + * |
|
81 | + * @param \WP_REST_Request $request Full data about the request. |
|
82 | + * |
|
83 | + * @return \WP_Error|\WP_REST_Response |
|
84 | + */ |
|
85 | + public function get_item($request) |
|
86 | + { |
|
87 | + $url = $request->get_url_params(); |
|
88 | + |
|
89 | + $view_id = intval($url['id']); |
|
90 | + |
|
91 | + $item = get_post($view_id); |
|
92 | + |
|
93 | + //return a response or error based on some conditional |
|
94 | + if ($item && !is_wp_error($item)) { |
|
95 | + $data = $this->prepare_view_for_response($item, $request); |
|
96 | + |
|
97 | + return new \WP_REST_Response($data, 200); |
|
98 | + } |
|
99 | + |
|
100 | + return new \WP_Error('code', sprintf('A View with ID #%d was not found.', $view_id)); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Prepare the item for the REST response. |
|
105 | + * |
|
106 | + * @since 2.0 |
|
107 | + * |
|
108 | + * @param \GV\View $view The view. |
|
109 | + * @param \GV\Entry $entry WordPress representation of the item. |
|
110 | + * @param \WP_REST_Request $request Request object. |
|
111 | + * @param string $context The context (directory, single) |
|
112 | + * @param string $class The value renderer. Default: null (raw value) |
|
113 | + * |
|
114 | + * @since 2.1 Add value renderer override $class parameter. |
|
115 | + * |
|
116 | + * @return mixed The data that is sent. |
|
117 | + */ |
|
118 | + public function prepare_entry_for_response($view, $entry, \WP_REST_Request $request, $context, $class = null) |
|
119 | + { |
|
120 | + |
|
121 | + // Only output the fields that should be displayed. |
|
122 | + $allowed = []; |
|
123 | + foreach ($view->fields->by_position("{$context}_*")->by_visible($view)->all() as $field) { |
|
124 | + $allowed[] = $field; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * @filter `gravityview/rest/entry/fields` Allow more entry fields that are output in regular REST requests. |
|
129 | + * |
|
130 | + * @param array $allowed The allowed ones, default by_visible, by_position( "context_*" ), i.e. as set in the view. |
|
131 | + * @param \GV\View $view The view. |
|
132 | + * @param \GV\Entry $entry The entry. |
|
133 | + * @param \WP_REST_Request $request Request object. |
|
134 | + * @param string $context The context (directory, single) |
|
135 | + */ |
|
136 | + $allowed_field_ids = apply_filters('gravityview/rest/entry/fields', wp_list_pluck($allowed, 'ID'), $view, $entry, $request, $context); |
|
137 | + |
|
138 | + $allowed = array_filter($allowed, function ($field) use ($allowed_field_ids) { |
|
139 | + return in_array($field->ID, $allowed_field_ids, true); |
|
140 | + }); |
|
141 | + |
|
142 | + // Tack on additional fields if needed |
|
143 | + foreach (array_diff($allowed_field_ids, wp_list_pluck($allowed, 'ID')) as $field_id) { |
|
144 | + $allowed[] = is_numeric($field_id) ? \GV\GF_Field::by_id($view->form, $field_id) : \GV\Internal_Field::by_id($field_id); |
|
145 | + } |
|
146 | + |
|
147 | + $r = new Request($request); |
|
148 | + $return = []; |
|
149 | + |
|
150 | + $renderer = new \GV\Field_Renderer(); |
|
151 | + |
|
152 | + $used_ids = []; |
|
153 | + |
|
154 | + foreach ($allowed as $field) { |
|
155 | + $source = is_numeric($field->ID) ? $view->form : new \GV\Internal_Source(); |
|
156 | + |
|
157 | + $field_id = $field->ID; |
|
158 | + $index = null; |
|
159 | + |
|
160 | + if (!isset($used_ids[$field_id])) { |
|
161 | + $used_ids[$field_id] = 0; |
|
162 | + } else { |
|
163 | + $index = ++$used_ids[$field_id]; |
|
164 | + } |
|
165 | + |
|
166 | + if ($index) { |
|
167 | + /** |
|
168 | + * Modify non-unique IDs (custom, id, etc.) to be unique and not gobbled up. |
|
169 | + */ |
|
170 | + $field_id = sprintf('%s(%d)', $field_id, $index + 1); |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @filter `gravityview/api/field/key` Filter the key name in the results for JSON output. |
|
175 | + * |
|
176 | + * @param string $field_id The ID. Should be unique or keys will be gobbled up. |
|
177 | + * @param \GV\View $view The view. |
|
178 | + * @param \GV\Entry $entry The entry. |
|
179 | + * @param \WP_REST_Request $request Request object. |
|
180 | + * @param string $context The context (directory, single) |
|
181 | + */ |
|
182 | + $field_id = apply_filters('gravityview/api/field/key', $field_id, $view, $entry, $request, $context); |
|
183 | + |
|
184 | + if (!$class && in_array($field->ID, ['custom'])) { |
|
185 | + /** |
|
186 | + * Custom fields (and perhaps some others) will require rendering as they don't |
|
187 | + * contain an intrinsic value (for custom their value is stored in the view and requires a renderer). |
|
188 | + * We force the CSV template to take over in such cases, it's good enough for most cases. |
|
189 | + */ |
|
190 | + $return[$field_id] = $renderer->render($field, $view, $source, $entry, $r, '\GV\Field_CSV_Template'); |
|
191 | + } elseif ($class) { |
|
192 | + $return[$field_id] = $renderer->render($field, $view, $source, $entry, $r, $class); |
|
193 | + } else { |
|
194 | + switch ($field->type) { |
|
195 | + case 'list': |
|
196 | + $return[$field_id] = unserialize($field->get_value($view, $source, $entry, $r)); |
|
197 | + break; |
|
198 | + case 'fileupload': |
|
199 | + case 'business_hours': |
|
200 | + $return[$field_id] = json_decode($field->get_value($view, $source, $entry, $r)); |
|
201 | + break; |
|
202 | + default: |
|
203 | + $return[$field_id] = $field->get_value($view, $source, $entry, $r); |
|
204 | + } |
|
205 | + } |
|
206 | + } |
|
207 | + |
|
208 | + return $return; |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Get entries from a view. |
|
213 | + * |
|
214 | + * Callback for /v1/views/{id}/entries/ |
|
215 | + * |
|
216 | + * @since 2.0 |
|
217 | + * |
|
218 | + * @param \WP_REST_Request $request Full data about the request. |
|
219 | + * |
|
220 | + * @return \WP_Error|\WP_REST_Response |
|
221 | + */ |
|
222 | + public function get_sub_items($request) |
|
223 | + { |
|
224 | + $url = $request->get_url_params(); |
|
225 | + $view_id = intval($url['id']); |
|
226 | + $format = \GV\Utils::get($url, 'format', 'json'); |
|
227 | + |
|
228 | + if ($post_id = $request->get_param('post_id')) { |
|
229 | + global $post; |
|
230 | + |
|
231 | + $post = get_post($post_id); |
|
232 | + |
|
233 | + if (!$post || is_wp_error($post)) { |
|
234 | + return new \WP_Error('gravityview-post-not-found', sprintf('A post with ID #%d was not found.', $post_id)); |
|
235 | + } |
|
236 | + |
|
237 | + $collection = \GV\View_Collection::from_post($post); |
|
238 | + |
|
239 | + if (!$collection->contains($view_id)) { |
|
240 | + return new \WP_Error('gravityview-post-not-contains', sprintf('The post with ID #%d does not contain a View with ID #%d', $post_id, $view_id)); |
|
241 | + } |
|
242 | + } |
|
243 | + |
|
244 | + $view = \GV\View::by_id($view_id); |
|
245 | + |
|
246 | + if ('html' === $format) { |
|
247 | + $renderer = new \GV\View_Renderer(); |
|
248 | + $count = $total = 0; |
|
249 | + |
|
250 | + /** @var \GV\Template_Context $context */ |
|
251 | + add_action('gravityview/template/view/render', function ($context) use (&$count, &$total) { |
|
252 | + $count = $context->entries->count(); |
|
253 | + $total = $context->entries->total(); |
|
254 | + }); |
|
255 | + |
|
256 | + $output = $renderer->render($view, new Request($request)); |
|
257 | + |
|
258 | + /** |
|
259 | + * @filter `gravityview/rest/entries/html/insert_meta` Whether to include `http-equiv` meta tags in the HTML output describing the data |
|
260 | + * |
|
261 | + * @since 2.0 |
|
262 | + * |
|
263 | + * @param bool $insert_meta Add <meta> tags? [Default: true] |
|
264 | + * @param int $count The number of entries being rendered |
|
265 | + * @param \GV\View $view The view. |
|
266 | + * @param \WP_REST_Request $request Request object. |
|
267 | + * @param int $total The number of total entries for the request |
|
268 | + */ |
|
269 | + $insert_meta = apply_filters('gravityview/rest/entries/html/insert_meta', true, $count, $view, $request, $total); |
|
270 | + |
|
271 | + if ($insert_meta) { |
|
272 | + $output = '<meta http-equiv="X-Item-Count" content="'.$count.'" />'.$output; |
|
273 | + $output = '<meta http-equiv="X-Item-Total" content="'.$total.'" />'.$output; |
|
274 | + } |
|
275 | + |
|
276 | + $response = new \WP_REST_Response($output, 200); |
|
277 | + $response->header('X-Item-Count', $count); |
|
278 | + $response->header('X-Item-Total', $total); |
|
279 | + |
|
280 | + return $response; |
|
281 | + } |
|
282 | + |
|
283 | + $entries = $view->get_entries(new Request($request)); |
|
284 | + |
|
285 | + if (!$entries->all()) { |
|
286 | + return new \WP_Error('gravityview-no-entries', __('No Entries found.', 'gravityview')); |
|
287 | + } |
|
288 | + |
|
289 | + if (in_array($format, ['csv', 'tsv'], true)) { |
|
290 | + ob_start(); |
|
291 | + |
|
292 | + $csv_or_tsv = fopen('php://output', 'w'); |
|
293 | + |
|
294 | + /** Da' BOM :) */ |
|
295 | + if (apply_filters('gform_include_bom_export_entries', true, $view->form ? $view->form->form : null)) { |
|
296 | + fputs($csv_or_tsv, "\xef\xbb\xbf"); |
|
297 | + } |
|
298 | + |
|
299 | + $headers_done = false; |
|
300 | + |
|
301 | + // If not "tsv" then use comma |
|
302 | + $delimiter = ('tsv' === $format) ? "\t" : ','; |
|
303 | + |
|
304 | + foreach ($entries->all() as $entry) { |
|
305 | + $entry = $this->prepare_entry_for_response($view, $entry, $request, 'directory', '\GV\Field_CSV_Template'); |
|
306 | + |
|
307 | + if (!$headers_done) { |
|
308 | + $headers_done = fputcsv($csv_or_tsv, array_map(['\GV\Utils', 'strip_excel_formulas'], array_keys($entry)), $delimiter); |
|
309 | + } |
|
310 | + |
|
311 | + fputcsv($csv_or_tsv, array_map(['\GV\Utils', 'strip_excel_formulas'], $entry), $delimiter); |
|
312 | + } |
|
313 | + |
|
314 | + $response = new \WP_REST_Response('', 200); |
|
315 | + $response->header('X-Item-Count', $entries->count()); |
|
316 | + $response->header('X-Item-Total', $entries->total()); |
|
317 | + $response->header('Content-Type', 'text/'.$format); |
|
318 | + |
|
319 | + fflush($csv_or_tsv); |
|
320 | + |
|
321 | + $data = rtrim(ob_get_clean()); |
|
322 | + |
|
323 | + add_filter('rest_pre_serve_request', function () use ($data) { |
|
324 | + echo $data; |
|
325 | 325 | |
326 | - return true; |
|
327 | - }); |
|
326 | + return true; |
|
327 | + }); |
|
328 | 328 | |
329 | - if (defined('DOING_GRAVITYVIEW_TESTS') && DOING_GRAVITYVIEW_TESTS) { |
|
330 | - echo $data; // rest_pre_serve_request is not called in tests |
|
331 | - } |
|
329 | + if (defined('DOING_GRAVITYVIEW_TESTS') && DOING_GRAVITYVIEW_TESTS) { |
|
330 | + echo $data; // rest_pre_serve_request is not called in tests |
|
331 | + } |
|
332 | 332 | |
333 | - return $response; |
|
334 | - } |
|
335 | - |
|
336 | - $data = ['entries' => $entries->all(), 'total' => $entries->total()]; |
|
337 | - |
|
338 | - foreach ($data['entries'] as &$entry) { |
|
339 | - $entry = $this->prepare_entry_for_response($view, $entry, $request, 'directory'); |
|
340 | - } |
|
341 | - |
|
342 | - return new \WP_REST_Response($data, 200); |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * Get one entry from view. |
|
347 | - * |
|
348 | - * Callback for /v1/views/{id}/entries/{id}/ |
|
349 | - * |
|
350 | - * @uses GVCommon::get_entry |
|
351 | - * |
|
352 | - * @since 2.0 |
|
353 | - * |
|
354 | - * @param \WP_REST_Request $request Full data about the request. |
|
355 | - * |
|
356 | - * @return \WP_Error|\WP_REST_Response |
|
357 | - */ |
|
358 | - public function get_sub_item($request) |
|
359 | - { |
|
360 | - $url = $request->get_url_params(); |
|
361 | - $view_id = intval($url['id']); |
|
362 | - $entry_id = intval($url['s_id']); |
|
363 | - $format = \GV\Utils::get($url, 'format', 'json'); |
|
364 | - |
|
365 | - $view = \GV\View::by_id($view_id); |
|
366 | - $entry = \GV\GF_Entry::by_id($entry_id); |
|
367 | - |
|
368 | - if ($format === 'html') { |
|
369 | - $renderer = new \GV\Entry_Renderer(); |
|
370 | - |
|
371 | - return $renderer->render($entry, $view, new Request($request)); |
|
372 | - } |
|
373 | - |
|
374 | - return $this->prepare_entry_for_response($view, $entry, $request, 'single'); |
|
375 | - } |
|
376 | - |
|
377 | - /** |
|
378 | - * Prepare the item for the REST response. |
|
379 | - * |
|
380 | - * @since 2.0 |
|
381 | - * |
|
382 | - * @param \WP_Post $view_post WordPress representation of the item. |
|
383 | - * @param \WP_REST_Request $request Request object. |
|
384 | - * |
|
385 | - * @return mixed |
|
386 | - */ |
|
387 | - public function prepare_view_for_response($view_post, \WP_REST_Request $request) |
|
388 | - { |
|
389 | - if (is_wp_error($this->get_item_permissions_check($request, $view_post->ID))) { |
|
390 | - // Redacted out view. |
|
391 | - return ['ID' => $view_post->ID, 'post_content' => __('You are not allowed to access this content.', 'gravityview')]; |
|
392 | - } |
|
393 | - |
|
394 | - $view = \GV\View::from_post($view_post); |
|
395 | - |
|
396 | - $item = $view->as_data(); |
|
397 | - |
|
398 | - // Add all the WP_Post data |
|
399 | - $view_post = $view_post->to_array(); |
|
400 | - |
|
401 | - unset($view_post['to_ping'], $view_post['ping_status'], $view_post['pinged'], $view_post['post_type'], $view_post['filter'], $view_post['post_category'], $view_post['tags_input'], $view_post['post_content'], $view_post['post_content_filtered']); |
|
402 | - |
|
403 | - $return = wp_parse_args($item, $view_post); |
|
404 | - |
|
405 | - $return['title'] = $return['post_title']; |
|
406 | - |
|
407 | - $return['settings'] = isset($return['atts']) ? $return['atts'] : []; |
|
408 | - unset($return['atts'], $return['view_id']); |
|
409 | - |
|
410 | - $return['search_criteria'] = [ |
|
411 | - 'page_size' => rgars($return, 'settings/page_size'), |
|
412 | - 'sort_field' => rgars($return, 'settings/sort_field'), |
|
413 | - 'sort_direction' => rgars($return, 'settings/sort_direction'), |
|
414 | - 'offset' => rgars($return, 'settings/offset'), |
|
415 | - ]; |
|
416 | - |
|
417 | - unset($return['settings']['page_size'], $return['settings']['sort_field'], $return['settings']['sort_direction']); |
|
418 | - |
|
419 | - // Redact for non-logged ins |
|
420 | - if (!\GVCommon::has_cap('edit_others_gravityviews')) { |
|
421 | - unset($return['settings']); |
|
422 | - unset($return['search_criteria']); |
|
423 | - } |
|
424 | - |
|
425 | - if (!\GFCommon::current_user_can_any('gravityforms_edit_forms')) { |
|
426 | - unset($return['form']); |
|
427 | - } |
|
428 | - |
|
429 | - return $return; |
|
430 | - } |
|
431 | - |
|
432 | - /** |
|
433 | - * @param \WP_REST_Request $request |
|
434 | - * |
|
435 | - * @return bool|\WP_Error |
|
436 | - */ |
|
437 | - public function get_item_permissions_check($request) |
|
438 | - { |
|
439 | - if (func_num_args() === 2) { |
|
440 | - $view_id = func_get_arg(1); // $view_id override |
|
441 | - } else { |
|
442 | - $url = $request->get_url_params(); |
|
443 | - $view_id = intval($url['id']); |
|
444 | - } |
|
445 | - |
|
446 | - if (!$view = \GV\View::by_id($view_id)) { |
|
447 | - return new \WP_Error('rest_forbidden', __('You are not allowed to access this content.', 'gravityview')); |
|
448 | - } |
|
449 | - |
|
450 | - while ($error = $view->can_render(['rest'], $request)) { |
|
451 | - if (!is_wp_error($error)) { |
|
452 | - break; |
|
453 | - } |
|
454 | - |
|
455 | - switch (str_replace('gravityview/', '', $error->get_error_code())) { |
|
456 | - case 'rest_disabled': |
|
457 | - case 'post_password_required': |
|
458 | - case 'not_public': |
|
459 | - case 'embed_only': |
|
460 | - case 'no_direct_access': |
|
461 | - return new \WP_Error('rest_forbidden_access_denied', __('You are not allowed to access this content.', 'gravityview')); |
|
462 | - case 'no_form_attached': |
|
463 | - return new \WP_Error('rest_forbidden_no_form_attached', __('This View is not configured properly.', 'gravityview')); |
|
464 | - default: |
|
465 | - return new \WP_Error('rest_forbidden', __('You are not allowed to access this content.', 'gravityview')); |
|
466 | - } |
|
467 | - } |
|
468 | - |
|
469 | - /** |
|
470 | - * @filter `gravityview/view/output/rest` Disable rest output. Final chance. |
|
471 | - * |
|
472 | - * @param bool Enable or not. |
|
473 | - * @param \GV\View $view The view. |
|
474 | - */ |
|
475 | - if (!apply_filters('gravityview/view/output/rest', true, $view)) { |
|
476 | - return new \WP_Error('rest_forbidden', __('You are not allowed to access this content.', 'gravityview')); |
|
477 | - } |
|
478 | - |
|
479 | - return true; |
|
480 | - } |
|
481 | - |
|
482 | - public function get_sub_item_permissions_check($request) |
|
483 | - { |
|
484 | - // Accessing a single entry needs the View access permissions. |
|
485 | - if (is_wp_error($error = $this->get_items_permissions_check($request))) { |
|
486 | - return $error; |
|
487 | - } |
|
488 | - |
|
489 | - $url = $request->get_url_params(); |
|
490 | - $view_id = intval($url['id']); |
|
491 | - $entry_id = intval($url['s_id']); |
|
492 | - |
|
493 | - $view = \GV\View::by_id($view_id); |
|
494 | - |
|
495 | - if (!$entry = \GV\GF_Entry::by_id($entry_id)) { |
|
496 | - return new \WP_Error('rest_forbidden', 'You are not allowed to view this content.', 'gravityview'); |
|
497 | - } |
|
498 | - |
|
499 | - if ($entry['form_id'] != $view->form->ID) { |
|
500 | - return new \WP_Error('rest_forbidden', 'You are not allowed to view this content.', 'gravityview'); |
|
501 | - } |
|
502 | - |
|
503 | - if ($entry['status'] != 'active') { |
|
504 | - return new \WP_Error('rest_forbidden', 'You are not allowed to view this content.', 'gravityview'); |
|
505 | - } |
|
506 | - |
|
507 | - if (apply_filters('gravityview_custom_entry_slug', false) && $entry->slug != get_query_var(\GV\Entry::get_endpoint_name())) { |
|
508 | - return new \WP_Error('rest_forbidden', 'You are not allowed to view this content.', 'gravityview'); |
|
509 | - } |
|
510 | - |
|
511 | - $is_admin_and_can_view = $view->settings->get('admin_show_all_statuses') && \GVCommon::has_cap('gravityview_moderate_entries', $view->ID); |
|
512 | - |
|
513 | - if ($view->settings->get('show_only_approved') && !$is_admin_and_can_view) { |
|
514 | - if (!\GravityView_Entry_Approval_Status::is_approved(gform_get_meta($entry->ID, \GravityView_Entry_Approval::meta_key))) { |
|
515 | - return new \WP_Error('rest_forbidden', 'You are not allowed to view this content.', 'gravityview'); |
|
516 | - } |
|
517 | - } |
|
518 | - |
|
519 | - return true; |
|
520 | - } |
|
521 | - |
|
522 | - public function get_items_permissions_check($request) |
|
523 | - { |
|
524 | - // Getting a list of all Views is always possible. |
|
525 | - return true; |
|
526 | - } |
|
527 | - |
|
528 | - public function get_sub_items_permissions_check($request) |
|
529 | - { |
|
530 | - // Accessing all entries of a View needs the same permissions as accessing the View. |
|
531 | - return $this->get_item_permissions_check($request); |
|
532 | - } |
|
333 | + return $response; |
|
334 | + } |
|
335 | + |
|
336 | + $data = ['entries' => $entries->all(), 'total' => $entries->total()]; |
|
337 | + |
|
338 | + foreach ($data['entries'] as &$entry) { |
|
339 | + $entry = $this->prepare_entry_for_response($view, $entry, $request, 'directory'); |
|
340 | + } |
|
341 | + |
|
342 | + return new \WP_REST_Response($data, 200); |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * Get one entry from view. |
|
347 | + * |
|
348 | + * Callback for /v1/views/{id}/entries/{id}/ |
|
349 | + * |
|
350 | + * @uses GVCommon::get_entry |
|
351 | + * |
|
352 | + * @since 2.0 |
|
353 | + * |
|
354 | + * @param \WP_REST_Request $request Full data about the request. |
|
355 | + * |
|
356 | + * @return \WP_Error|\WP_REST_Response |
|
357 | + */ |
|
358 | + public function get_sub_item($request) |
|
359 | + { |
|
360 | + $url = $request->get_url_params(); |
|
361 | + $view_id = intval($url['id']); |
|
362 | + $entry_id = intval($url['s_id']); |
|
363 | + $format = \GV\Utils::get($url, 'format', 'json'); |
|
364 | + |
|
365 | + $view = \GV\View::by_id($view_id); |
|
366 | + $entry = \GV\GF_Entry::by_id($entry_id); |
|
367 | + |
|
368 | + if ($format === 'html') { |
|
369 | + $renderer = new \GV\Entry_Renderer(); |
|
370 | + |
|
371 | + return $renderer->render($entry, $view, new Request($request)); |
|
372 | + } |
|
373 | + |
|
374 | + return $this->prepare_entry_for_response($view, $entry, $request, 'single'); |
|
375 | + } |
|
376 | + |
|
377 | + /** |
|
378 | + * Prepare the item for the REST response. |
|
379 | + * |
|
380 | + * @since 2.0 |
|
381 | + * |
|
382 | + * @param \WP_Post $view_post WordPress representation of the item. |
|
383 | + * @param \WP_REST_Request $request Request object. |
|
384 | + * |
|
385 | + * @return mixed |
|
386 | + */ |
|
387 | + public function prepare_view_for_response($view_post, \WP_REST_Request $request) |
|
388 | + { |
|
389 | + if (is_wp_error($this->get_item_permissions_check($request, $view_post->ID))) { |
|
390 | + // Redacted out view. |
|
391 | + return ['ID' => $view_post->ID, 'post_content' => __('You are not allowed to access this content.', 'gravityview')]; |
|
392 | + } |
|
393 | + |
|
394 | + $view = \GV\View::from_post($view_post); |
|
395 | + |
|
396 | + $item = $view->as_data(); |
|
397 | + |
|
398 | + // Add all the WP_Post data |
|
399 | + $view_post = $view_post->to_array(); |
|
400 | + |
|
401 | + unset($view_post['to_ping'], $view_post['ping_status'], $view_post['pinged'], $view_post['post_type'], $view_post['filter'], $view_post['post_category'], $view_post['tags_input'], $view_post['post_content'], $view_post['post_content_filtered']); |
|
402 | + |
|
403 | + $return = wp_parse_args($item, $view_post); |
|
404 | + |
|
405 | + $return['title'] = $return['post_title']; |
|
406 | + |
|
407 | + $return['settings'] = isset($return['atts']) ? $return['atts'] : []; |
|
408 | + unset($return['atts'], $return['view_id']); |
|
409 | + |
|
410 | + $return['search_criteria'] = [ |
|
411 | + 'page_size' => rgars($return, 'settings/page_size'), |
|
412 | + 'sort_field' => rgars($return, 'settings/sort_field'), |
|
413 | + 'sort_direction' => rgars($return, 'settings/sort_direction'), |
|
414 | + 'offset' => rgars($return, 'settings/offset'), |
|
415 | + ]; |
|
416 | + |
|
417 | + unset($return['settings']['page_size'], $return['settings']['sort_field'], $return['settings']['sort_direction']); |
|
418 | + |
|
419 | + // Redact for non-logged ins |
|
420 | + if (!\GVCommon::has_cap('edit_others_gravityviews')) { |
|
421 | + unset($return['settings']); |
|
422 | + unset($return['search_criteria']); |
|
423 | + } |
|
424 | + |
|
425 | + if (!\GFCommon::current_user_can_any('gravityforms_edit_forms')) { |
|
426 | + unset($return['form']); |
|
427 | + } |
|
428 | + |
|
429 | + return $return; |
|
430 | + } |
|
431 | + |
|
432 | + /** |
|
433 | + * @param \WP_REST_Request $request |
|
434 | + * |
|
435 | + * @return bool|\WP_Error |
|
436 | + */ |
|
437 | + public function get_item_permissions_check($request) |
|
438 | + { |
|
439 | + if (func_num_args() === 2) { |
|
440 | + $view_id = func_get_arg(1); // $view_id override |
|
441 | + } else { |
|
442 | + $url = $request->get_url_params(); |
|
443 | + $view_id = intval($url['id']); |
|
444 | + } |
|
445 | + |
|
446 | + if (!$view = \GV\View::by_id($view_id)) { |
|
447 | + return new \WP_Error('rest_forbidden', __('You are not allowed to access this content.', 'gravityview')); |
|
448 | + } |
|
449 | + |
|
450 | + while ($error = $view->can_render(['rest'], $request)) { |
|
451 | + if (!is_wp_error($error)) { |
|
452 | + break; |
|
453 | + } |
|
454 | + |
|
455 | + switch (str_replace('gravityview/', '', $error->get_error_code())) { |
|
456 | + case 'rest_disabled': |
|
457 | + case 'post_password_required': |
|
458 | + case 'not_public': |
|
459 | + case 'embed_only': |
|
460 | + case 'no_direct_access': |
|
461 | + return new \WP_Error('rest_forbidden_access_denied', __('You are not allowed to access this content.', 'gravityview')); |
|
462 | + case 'no_form_attached': |
|
463 | + return new \WP_Error('rest_forbidden_no_form_attached', __('This View is not configured properly.', 'gravityview')); |
|
464 | + default: |
|
465 | + return new \WP_Error('rest_forbidden', __('You are not allowed to access this content.', 'gravityview')); |
|
466 | + } |
|
467 | + } |
|
468 | + |
|
469 | + /** |
|
470 | + * @filter `gravityview/view/output/rest` Disable rest output. Final chance. |
|
471 | + * |
|
472 | + * @param bool Enable or not. |
|
473 | + * @param \GV\View $view The view. |
|
474 | + */ |
|
475 | + if (!apply_filters('gravityview/view/output/rest', true, $view)) { |
|
476 | + return new \WP_Error('rest_forbidden', __('You are not allowed to access this content.', 'gravityview')); |
|
477 | + } |
|
478 | + |
|
479 | + return true; |
|
480 | + } |
|
481 | + |
|
482 | + public function get_sub_item_permissions_check($request) |
|
483 | + { |
|
484 | + // Accessing a single entry needs the View access permissions. |
|
485 | + if (is_wp_error($error = $this->get_items_permissions_check($request))) { |
|
486 | + return $error; |
|
487 | + } |
|
488 | + |
|
489 | + $url = $request->get_url_params(); |
|
490 | + $view_id = intval($url['id']); |
|
491 | + $entry_id = intval($url['s_id']); |
|
492 | + |
|
493 | + $view = \GV\View::by_id($view_id); |
|
494 | + |
|
495 | + if (!$entry = \GV\GF_Entry::by_id($entry_id)) { |
|
496 | + return new \WP_Error('rest_forbidden', 'You are not allowed to view this content.', 'gravityview'); |
|
497 | + } |
|
498 | + |
|
499 | + if ($entry['form_id'] != $view->form->ID) { |
|
500 | + return new \WP_Error('rest_forbidden', 'You are not allowed to view this content.', 'gravityview'); |
|
501 | + } |
|
502 | + |
|
503 | + if ($entry['status'] != 'active') { |
|
504 | + return new \WP_Error('rest_forbidden', 'You are not allowed to view this content.', 'gravityview'); |
|
505 | + } |
|
506 | + |
|
507 | + if (apply_filters('gravityview_custom_entry_slug', false) && $entry->slug != get_query_var(\GV\Entry::get_endpoint_name())) { |
|
508 | + return new \WP_Error('rest_forbidden', 'You are not allowed to view this content.', 'gravityview'); |
|
509 | + } |
|
510 | + |
|
511 | + $is_admin_and_can_view = $view->settings->get('admin_show_all_statuses') && \GVCommon::has_cap('gravityview_moderate_entries', $view->ID); |
|
512 | + |
|
513 | + if ($view->settings->get('show_only_approved') && !$is_admin_and_can_view) { |
|
514 | + if (!\GravityView_Entry_Approval_Status::is_approved(gform_get_meta($entry->ID, \GravityView_Entry_Approval::meta_key))) { |
|
515 | + return new \WP_Error('rest_forbidden', 'You are not allowed to view this content.', 'gravityview'); |
|
516 | + } |
|
517 | + } |
|
518 | + |
|
519 | + return true; |
|
520 | + } |
|
521 | + |
|
522 | + public function get_items_permissions_check($request) |
|
523 | + { |
|
524 | + // Getting a list of all Views is always possible. |
|
525 | + return true; |
|
526 | + } |
|
527 | + |
|
528 | + public function get_sub_items_permissions_check($request) |
|
529 | + { |
|
530 | + // Accessing all entries of a View needs the same permissions as accessing the View. |
|
531 | + return $this->get_item_permissions_check($request); |
|
532 | + } |
|
533 | 533 | } |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | namespace GV\REST; |
14 | 14 | |
15 | 15 | /** If this file is called directly, abort. */ |
16 | -if (!defined('GRAVITYVIEW_DIR')) { |
|
16 | +if ( ! defined( 'GRAVITYVIEW_DIR' ) ) { |
|
17 | 17 | exit(); |
18 | 18 | } |
19 | 19 | |
@@ -46,29 +46,29 @@ discard block |
||
46 | 46 | * |
47 | 47 | * @return \WP_Error|\WP_REST_Response |
48 | 48 | */ |
49 | - public function get_items($request) |
|
49 | + public function get_items( $request ) |
|
50 | 50 | { |
51 | - $page = $request->get_param('page'); |
|
52 | - $limit = $request->get_param('limit'); |
|
51 | + $page = $request->get_param( 'page' ); |
|
52 | + $limit = $request->get_param( 'limit' ); |
|
53 | 53 | |
54 | - $items = \GVCommon::get_all_views([ |
|
54 | + $items = \GVCommon::get_all_views( [ |
|
55 | 55 | 'posts_per_page' => $limit, |
56 | 56 | 'paged' => $page, |
57 | - ]); |
|
57 | + ] ); |
|
58 | 58 | |
59 | - if (empty($items)) { |
|
60 | - return new \WP_Error('gravityview-no-views', __('No Views found.', 'gravityview')); //@todo message |
|
59 | + if ( empty( $items ) ) { |
|
60 | + return new \WP_Error( 'gravityview-no-views', __( 'No Views found.', 'gravityview' ) ); //@todo message |
|
61 | 61 | } |
62 | 62 | |
63 | 63 | $data = [ |
64 | - 'views' => [], |
|
65 | - 'total' => wp_count_posts('gravityview')->publish, |
|
64 | + 'views' => [ ], |
|
65 | + 'total' => wp_count_posts( 'gravityview' )->publish, |
|
66 | 66 | ]; |
67 | - foreach ($items as $item) { |
|
68 | - $data['views'][] = $this->prepare_view_for_response($item, $request); |
|
67 | + foreach ( $items as $item ) { |
|
68 | + $data[ 'views' ][ ] = $this->prepare_view_for_response( $item, $request ); |
|
69 | 69 | } |
70 | 70 | |
71 | - return new \WP_REST_Response($data, 200); |
|
71 | + return new \WP_REST_Response( $data, 200 ); |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | /** |
@@ -82,22 +82,22 @@ discard block |
||
82 | 82 | * |
83 | 83 | * @return \WP_Error|\WP_REST_Response |
84 | 84 | */ |
85 | - public function get_item($request) |
|
85 | + public function get_item( $request ) |
|
86 | 86 | { |
87 | 87 | $url = $request->get_url_params(); |
88 | 88 | |
89 | - $view_id = intval($url['id']); |
|
89 | + $view_id = intval( $url[ 'id' ] ); |
|
90 | 90 | |
91 | - $item = get_post($view_id); |
|
91 | + $item = get_post( $view_id ); |
|
92 | 92 | |
93 | 93 | //return a response or error based on some conditional |
94 | - if ($item && !is_wp_error($item)) { |
|
95 | - $data = $this->prepare_view_for_response($item, $request); |
|
94 | + if ( $item && ! is_wp_error( $item ) ) { |
|
95 | + $data = $this->prepare_view_for_response( $item, $request ); |
|
96 | 96 | |
97 | - return new \WP_REST_Response($data, 200); |
|
97 | + return new \WP_REST_Response( $data, 200 ); |
|
98 | 98 | } |
99 | 99 | |
100 | - return new \WP_Error('code', sprintf('A View with ID #%d was not found.', $view_id)); |
|
100 | + return new \WP_Error( 'code', sprintf( 'A View with ID #%d was not found.', $view_id ) ); |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | /** |
@@ -115,13 +115,13 @@ discard block |
||
115 | 115 | * |
116 | 116 | * @return mixed The data that is sent. |
117 | 117 | */ |
118 | - public function prepare_entry_for_response($view, $entry, \WP_REST_Request $request, $context, $class = null) |
|
118 | + public function prepare_entry_for_response( $view, $entry, \WP_REST_Request $request, $context, $class = null ) |
|
119 | 119 | { |
120 | 120 | |
121 | 121 | // Only output the fields that should be displayed. |
122 | - $allowed = []; |
|
123 | - foreach ($view->fields->by_position("{$context}_*")->by_visible($view)->all() as $field) { |
|
124 | - $allowed[] = $field; |
|
122 | + $allowed = [ ]; |
|
123 | + foreach ( $view->fields->by_position( "{$context}_*" )->by_visible( $view )->all() as $field ) { |
|
124 | + $allowed[ ] = $field; |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | /** |
@@ -133,41 +133,41 @@ discard block |
||
133 | 133 | * @param \WP_REST_Request $request Request object. |
134 | 134 | * @param string $context The context (directory, single) |
135 | 135 | */ |
136 | - $allowed_field_ids = apply_filters('gravityview/rest/entry/fields', wp_list_pluck($allowed, 'ID'), $view, $entry, $request, $context); |
|
136 | + $allowed_field_ids = apply_filters( 'gravityview/rest/entry/fields', wp_list_pluck( $allowed, 'ID' ), $view, $entry, $request, $context ); |
|
137 | 137 | |
138 | - $allowed = array_filter($allowed, function ($field) use ($allowed_field_ids) { |
|
139 | - return in_array($field->ID, $allowed_field_ids, true); |
|
138 | + $allowed = array_filter( $allowed, function( $field ) use ( $allowed_field_ids ) { |
|
139 | + return in_array( $field->ID, $allowed_field_ids, true ); |
|
140 | 140 | }); |
141 | 141 | |
142 | 142 | // Tack on additional fields if needed |
143 | - foreach (array_diff($allowed_field_ids, wp_list_pluck($allowed, 'ID')) as $field_id) { |
|
144 | - $allowed[] = is_numeric($field_id) ? \GV\GF_Field::by_id($view->form, $field_id) : \GV\Internal_Field::by_id($field_id); |
|
143 | + foreach ( array_diff( $allowed_field_ids, wp_list_pluck( $allowed, 'ID' ) ) as $field_id ) { |
|
144 | + $allowed[ ] = is_numeric( $field_id ) ? \GV\GF_Field::by_id( $view->form, $field_id ) : \GV\Internal_Field::by_id( $field_id ); |
|
145 | 145 | } |
146 | 146 | |
147 | - $r = new Request($request); |
|
148 | - $return = []; |
|
147 | + $r = new Request( $request ); |
|
148 | + $return = [ ]; |
|
149 | 149 | |
150 | 150 | $renderer = new \GV\Field_Renderer(); |
151 | 151 | |
152 | - $used_ids = []; |
|
152 | + $used_ids = [ ]; |
|
153 | 153 | |
154 | - foreach ($allowed as $field) { |
|
155 | - $source = is_numeric($field->ID) ? $view->form : new \GV\Internal_Source(); |
|
154 | + foreach ( $allowed as $field ) { |
|
155 | + $source = is_numeric( $field->ID ) ? $view->form : new \GV\Internal_Source(); |
|
156 | 156 | |
157 | 157 | $field_id = $field->ID; |
158 | 158 | $index = null; |
159 | 159 | |
160 | - if (!isset($used_ids[$field_id])) { |
|
161 | - $used_ids[$field_id] = 0; |
|
160 | + if ( ! isset( $used_ids[ $field_id ] ) ) { |
|
161 | + $used_ids[ $field_id ] = 0; |
|
162 | 162 | } else { |
163 | - $index = ++$used_ids[$field_id]; |
|
163 | + $index = ++$used_ids[ $field_id ]; |
|
164 | 164 | } |
165 | 165 | |
166 | - if ($index) { |
|
166 | + if ( $index ) { |
|
167 | 167 | /** |
168 | 168 | * Modify non-unique IDs (custom, id, etc.) to be unique and not gobbled up. |
169 | 169 | */ |
170 | - $field_id = sprintf('%s(%d)', $field_id, $index + 1); |
|
170 | + $field_id = sprintf( '%s(%d)', $field_id, $index + 1 ); |
|
171 | 171 | } |
172 | 172 | |
173 | 173 | /** |
@@ -179,28 +179,28 @@ discard block |
||
179 | 179 | * @param \WP_REST_Request $request Request object. |
180 | 180 | * @param string $context The context (directory, single) |
181 | 181 | */ |
182 | - $field_id = apply_filters('gravityview/api/field/key', $field_id, $view, $entry, $request, $context); |
|
182 | + $field_id = apply_filters( 'gravityview/api/field/key', $field_id, $view, $entry, $request, $context ); |
|
183 | 183 | |
184 | - if (!$class && in_array($field->ID, ['custom'])) { |
|
184 | + if ( ! $class && in_array( $field->ID, [ 'custom' ] ) ) { |
|
185 | 185 | /** |
186 | 186 | * Custom fields (and perhaps some others) will require rendering as they don't |
187 | 187 | * contain an intrinsic value (for custom their value is stored in the view and requires a renderer). |
188 | 188 | * We force the CSV template to take over in such cases, it's good enough for most cases. |
189 | 189 | */ |
190 | - $return[$field_id] = $renderer->render($field, $view, $source, $entry, $r, '\GV\Field_CSV_Template'); |
|
191 | - } elseif ($class) { |
|
192 | - $return[$field_id] = $renderer->render($field, $view, $source, $entry, $r, $class); |
|
190 | + $return[ $field_id ] = $renderer->render( $field, $view, $source, $entry, $r, '\GV\Field_CSV_Template' ); |
|
191 | + } elseif ( $class ) { |
|
192 | + $return[ $field_id ] = $renderer->render( $field, $view, $source, $entry, $r, $class ); |
|
193 | 193 | } else { |
194 | - switch ($field->type) { |
|
194 | + switch ( $field->type ) { |
|
195 | 195 | case 'list': |
196 | - $return[$field_id] = unserialize($field->get_value($view, $source, $entry, $r)); |
|
196 | + $return[ $field_id ] = unserialize( $field->get_value( $view, $source, $entry, $r ) ); |
|
197 | 197 | break; |
198 | 198 | case 'fileupload': |
199 | 199 | case 'business_hours': |
200 | - $return[$field_id] = json_decode($field->get_value($view, $source, $entry, $r)); |
|
200 | + $return[ $field_id ] = json_decode( $field->get_value( $view, $source, $entry, $r ) ); |
|
201 | 201 | break; |
202 | 202 | default: |
203 | - $return[$field_id] = $field->get_value($view, $source, $entry, $r); |
|
203 | + $return[ $field_id ] = $field->get_value( $view, $source, $entry, $r ); |
|
204 | 204 | } |
205 | 205 | } |
206 | 206 | } |
@@ -219,41 +219,41 @@ discard block |
||
219 | 219 | * |
220 | 220 | * @return \WP_Error|\WP_REST_Response |
221 | 221 | */ |
222 | - public function get_sub_items($request) |
|
222 | + public function get_sub_items( $request ) |
|
223 | 223 | { |
224 | 224 | $url = $request->get_url_params(); |
225 | - $view_id = intval($url['id']); |
|
226 | - $format = \GV\Utils::get($url, 'format', 'json'); |
|
225 | + $view_id = intval( $url[ 'id' ] ); |
|
226 | + $format = \GV\Utils::get( $url, 'format', 'json' ); |
|
227 | 227 | |
228 | - if ($post_id = $request->get_param('post_id')) { |
|
228 | + if ( $post_id = $request->get_param( 'post_id' ) ) { |
|
229 | 229 | global $post; |
230 | 230 | |
231 | - $post = get_post($post_id); |
|
231 | + $post = get_post( $post_id ); |
|
232 | 232 | |
233 | - if (!$post || is_wp_error($post)) { |
|
234 | - return new \WP_Error('gravityview-post-not-found', sprintf('A post with ID #%d was not found.', $post_id)); |
|
233 | + if ( ! $post || is_wp_error( $post ) ) { |
|
234 | + return new \WP_Error( 'gravityview-post-not-found', sprintf( 'A post with ID #%d was not found.', $post_id ) ); |
|
235 | 235 | } |
236 | 236 | |
237 | - $collection = \GV\View_Collection::from_post($post); |
|
237 | + $collection = \GV\View_Collection::from_post( $post ); |
|
238 | 238 | |
239 | - if (!$collection->contains($view_id)) { |
|
240 | - return new \WP_Error('gravityview-post-not-contains', sprintf('The post with ID #%d does not contain a View with ID #%d', $post_id, $view_id)); |
|
239 | + if ( ! $collection->contains( $view_id ) ) { |
|
240 | + return new \WP_Error( 'gravityview-post-not-contains', sprintf( 'The post with ID #%d does not contain a View with ID #%d', $post_id, $view_id ) ); |
|
241 | 241 | } |
242 | 242 | } |
243 | 243 | |
244 | - $view = \GV\View::by_id($view_id); |
|
244 | + $view = \GV\View::by_id( $view_id ); |
|
245 | 245 | |
246 | - if ('html' === $format) { |
|
246 | + if ( 'html' === $format ) { |
|
247 | 247 | $renderer = new \GV\View_Renderer(); |
248 | 248 | $count = $total = 0; |
249 | 249 | |
250 | 250 | /** @var \GV\Template_Context $context */ |
251 | - add_action('gravityview/template/view/render', function ($context) use (&$count, &$total) { |
|
251 | + add_action( 'gravityview/template/view/render', function( $context ) use ( &$count, &$total ) { |
|
252 | 252 | $count = $context->entries->count(); |
253 | 253 | $total = $context->entries->total(); |
254 | 254 | }); |
255 | 255 | |
256 | - $output = $renderer->render($view, new Request($request)); |
|
256 | + $output = $renderer->render( $view, new Request( $request ) ); |
|
257 | 257 | |
258 | 258 | /** |
259 | 259 | * @filter `gravityview/rest/entries/html/insert_meta` Whether to include `http-equiv` meta tags in the HTML output describing the data |
@@ -266,80 +266,80 @@ discard block |
||
266 | 266 | * @param \WP_REST_Request $request Request object. |
267 | 267 | * @param int $total The number of total entries for the request |
268 | 268 | */ |
269 | - $insert_meta = apply_filters('gravityview/rest/entries/html/insert_meta', true, $count, $view, $request, $total); |
|
269 | + $insert_meta = apply_filters( 'gravityview/rest/entries/html/insert_meta', true, $count, $view, $request, $total ); |
|
270 | 270 | |
271 | - if ($insert_meta) { |
|
272 | - $output = '<meta http-equiv="X-Item-Count" content="'.$count.'" />'.$output; |
|
273 | - $output = '<meta http-equiv="X-Item-Total" content="'.$total.'" />'.$output; |
|
271 | + if ( $insert_meta ) { |
|
272 | + $output = '<meta http-equiv="X-Item-Count" content="' . $count . '" />' . $output; |
|
273 | + $output = '<meta http-equiv="X-Item-Total" content="' . $total . '" />' . $output; |
|
274 | 274 | } |
275 | 275 | |
276 | - $response = new \WP_REST_Response($output, 200); |
|
277 | - $response->header('X-Item-Count', $count); |
|
278 | - $response->header('X-Item-Total', $total); |
|
276 | + $response = new \WP_REST_Response( $output, 200 ); |
|
277 | + $response->header( 'X-Item-Count', $count ); |
|
278 | + $response->header( 'X-Item-Total', $total ); |
|
279 | 279 | |
280 | 280 | return $response; |
281 | 281 | } |
282 | 282 | |
283 | - $entries = $view->get_entries(new Request($request)); |
|
283 | + $entries = $view->get_entries( new Request( $request ) ); |
|
284 | 284 | |
285 | - if (!$entries->all()) { |
|
286 | - return new \WP_Error('gravityview-no-entries', __('No Entries found.', 'gravityview')); |
|
285 | + if ( ! $entries->all() ) { |
|
286 | + return new \WP_Error( 'gravityview-no-entries', __( 'No Entries found.', 'gravityview' ) ); |
|
287 | 287 | } |
288 | 288 | |
289 | - if (in_array($format, ['csv', 'tsv'], true)) { |
|
289 | + if ( in_array( $format, [ 'csv', 'tsv' ], true ) ) { |
|
290 | 290 | ob_start(); |
291 | 291 | |
292 | - $csv_or_tsv = fopen('php://output', 'w'); |
|
292 | + $csv_or_tsv = fopen( 'php://output', 'w' ); |
|
293 | 293 | |
294 | 294 | /** Da' BOM :) */ |
295 | - if (apply_filters('gform_include_bom_export_entries', true, $view->form ? $view->form->form : null)) { |
|
296 | - fputs($csv_or_tsv, "\xef\xbb\xbf"); |
|
295 | + if ( apply_filters( 'gform_include_bom_export_entries', true, $view->form ? $view->form->form : null ) ) { |
|
296 | + fputs( $csv_or_tsv, "\xef\xbb\xbf" ); |
|
297 | 297 | } |
298 | 298 | |
299 | 299 | $headers_done = false; |
300 | 300 | |
301 | 301 | // If not "tsv" then use comma |
302 | - $delimiter = ('tsv' === $format) ? "\t" : ','; |
|
302 | + $delimiter = ( 'tsv' === $format ) ? "\t" : ','; |
|
303 | 303 | |
304 | - foreach ($entries->all() as $entry) { |
|
305 | - $entry = $this->prepare_entry_for_response($view, $entry, $request, 'directory', '\GV\Field_CSV_Template'); |
|
304 | + foreach ( $entries->all() as $entry ) { |
|
305 | + $entry = $this->prepare_entry_for_response( $view, $entry, $request, 'directory', '\GV\Field_CSV_Template' ); |
|
306 | 306 | |
307 | - if (!$headers_done) { |
|
308 | - $headers_done = fputcsv($csv_or_tsv, array_map(['\GV\Utils', 'strip_excel_formulas'], array_keys($entry)), $delimiter); |
|
307 | + if ( ! $headers_done ) { |
|
308 | + $headers_done = fputcsv( $csv_or_tsv, array_map( [ '\GV\Utils', 'strip_excel_formulas' ], array_keys( $entry ) ), $delimiter ); |
|
309 | 309 | } |
310 | 310 | |
311 | - fputcsv($csv_or_tsv, array_map(['\GV\Utils', 'strip_excel_formulas'], $entry), $delimiter); |
|
311 | + fputcsv( $csv_or_tsv, array_map( [ '\GV\Utils', 'strip_excel_formulas' ], $entry ), $delimiter ); |
|
312 | 312 | } |
313 | 313 | |
314 | - $response = new \WP_REST_Response('', 200); |
|
315 | - $response->header('X-Item-Count', $entries->count()); |
|
316 | - $response->header('X-Item-Total', $entries->total()); |
|
317 | - $response->header('Content-Type', 'text/'.$format); |
|
314 | + $response = new \WP_REST_Response( '', 200 ); |
|
315 | + $response->header( 'X-Item-Count', $entries->count() ); |
|
316 | + $response->header( 'X-Item-Total', $entries->total() ); |
|
317 | + $response->header( 'Content-Type', 'text/' . $format ); |
|
318 | 318 | |
319 | - fflush($csv_or_tsv); |
|
319 | + fflush( $csv_or_tsv ); |
|
320 | 320 | |
321 | - $data = rtrim(ob_get_clean()); |
|
321 | + $data = rtrim( ob_get_clean() ); |
|
322 | 322 | |
323 | - add_filter('rest_pre_serve_request', function () use ($data) { |
|
323 | + add_filter( 'rest_pre_serve_request', function() use ( $data ) { |
|
324 | 324 | echo $data; |
325 | 325 | |
326 | 326 | return true; |
327 | 327 | }); |
328 | 328 | |
329 | - if (defined('DOING_GRAVITYVIEW_TESTS') && DOING_GRAVITYVIEW_TESTS) { |
|
329 | + if ( defined( 'DOING_GRAVITYVIEW_TESTS' ) && DOING_GRAVITYVIEW_TESTS ) { |
|
330 | 330 | echo $data; // rest_pre_serve_request is not called in tests |
331 | 331 | } |
332 | 332 | |
333 | 333 | return $response; |
334 | 334 | } |
335 | 335 | |
336 | - $data = ['entries' => $entries->all(), 'total' => $entries->total()]; |
|
336 | + $data = [ 'entries' => $entries->all(), 'total' => $entries->total() ]; |
|
337 | 337 | |
338 | - foreach ($data['entries'] as &$entry) { |
|
339 | - $entry = $this->prepare_entry_for_response($view, $entry, $request, 'directory'); |
|
338 | + foreach ( $data[ 'entries' ] as &$entry ) { |
|
339 | + $entry = $this->prepare_entry_for_response( $view, $entry, $request, 'directory' ); |
|
340 | 340 | } |
341 | 341 | |
342 | - return new \WP_REST_Response($data, 200); |
|
342 | + return new \WP_REST_Response( $data, 200 ); |
|
343 | 343 | } |
344 | 344 | |
345 | 345 | /** |
@@ -355,23 +355,23 @@ discard block |
||
355 | 355 | * |
356 | 356 | * @return \WP_Error|\WP_REST_Response |
357 | 357 | */ |
358 | - public function get_sub_item($request) |
|
358 | + public function get_sub_item( $request ) |
|
359 | 359 | { |
360 | 360 | $url = $request->get_url_params(); |
361 | - $view_id = intval($url['id']); |
|
362 | - $entry_id = intval($url['s_id']); |
|
363 | - $format = \GV\Utils::get($url, 'format', 'json'); |
|
361 | + $view_id = intval( $url[ 'id' ] ); |
|
362 | + $entry_id = intval( $url[ 's_id' ] ); |
|
363 | + $format = \GV\Utils::get( $url, 'format', 'json' ); |
|
364 | 364 | |
365 | - $view = \GV\View::by_id($view_id); |
|
366 | - $entry = \GV\GF_Entry::by_id($entry_id); |
|
365 | + $view = \GV\View::by_id( $view_id ); |
|
366 | + $entry = \GV\GF_Entry::by_id( $entry_id ); |
|
367 | 367 | |
368 | - if ($format === 'html') { |
|
368 | + if ( $format === 'html' ) { |
|
369 | 369 | $renderer = new \GV\Entry_Renderer(); |
370 | 370 | |
371 | - return $renderer->render($entry, $view, new Request($request)); |
|
371 | + return $renderer->render( $entry, $view, new Request( $request ) ); |
|
372 | 372 | } |
373 | 373 | |
374 | - return $this->prepare_entry_for_response($view, $entry, $request, 'single'); |
|
374 | + return $this->prepare_entry_for_response( $view, $entry, $request, 'single' ); |
|
375 | 375 | } |
376 | 376 | |
377 | 377 | /** |
@@ -384,46 +384,46 @@ discard block |
||
384 | 384 | * |
385 | 385 | * @return mixed |
386 | 386 | */ |
387 | - public function prepare_view_for_response($view_post, \WP_REST_Request $request) |
|
387 | + public function prepare_view_for_response( $view_post, \WP_REST_Request $request ) |
|
388 | 388 | { |
389 | - if (is_wp_error($this->get_item_permissions_check($request, $view_post->ID))) { |
|
389 | + if ( is_wp_error( $this->get_item_permissions_check( $request, $view_post->ID ) ) ) { |
|
390 | 390 | // Redacted out view. |
391 | - return ['ID' => $view_post->ID, 'post_content' => __('You are not allowed to access this content.', 'gravityview')]; |
|
391 | + return [ 'ID' => $view_post->ID, 'post_content' => __( 'You are not allowed to access this content.', 'gravityview' ) ]; |
|
392 | 392 | } |
393 | 393 | |
394 | - $view = \GV\View::from_post($view_post); |
|
394 | + $view = \GV\View::from_post( $view_post ); |
|
395 | 395 | |
396 | 396 | $item = $view->as_data(); |
397 | 397 | |
398 | 398 | // Add all the WP_Post data |
399 | 399 | $view_post = $view_post->to_array(); |
400 | 400 | |
401 | - unset($view_post['to_ping'], $view_post['ping_status'], $view_post['pinged'], $view_post['post_type'], $view_post['filter'], $view_post['post_category'], $view_post['tags_input'], $view_post['post_content'], $view_post['post_content_filtered']); |
|
401 | + unset( $view_post[ 'to_ping' ], $view_post[ 'ping_status' ], $view_post[ 'pinged' ], $view_post[ 'post_type' ], $view_post[ 'filter' ], $view_post[ 'post_category' ], $view_post[ 'tags_input' ], $view_post[ 'post_content' ], $view_post[ 'post_content_filtered' ] ); |
|
402 | 402 | |
403 | - $return = wp_parse_args($item, $view_post); |
|
403 | + $return = wp_parse_args( $item, $view_post ); |
|
404 | 404 | |
405 | - $return['title'] = $return['post_title']; |
|
405 | + $return[ 'title' ] = $return[ 'post_title' ]; |
|
406 | 406 | |
407 | - $return['settings'] = isset($return['atts']) ? $return['atts'] : []; |
|
408 | - unset($return['atts'], $return['view_id']); |
|
407 | + $return[ 'settings' ] = isset( $return[ 'atts' ] ) ? $return[ 'atts' ] : [ ]; |
|
408 | + unset( $return[ 'atts' ], $return[ 'view_id' ] ); |
|
409 | 409 | |
410 | - $return['search_criteria'] = [ |
|
411 | - 'page_size' => rgars($return, 'settings/page_size'), |
|
412 | - 'sort_field' => rgars($return, 'settings/sort_field'), |
|
413 | - 'sort_direction' => rgars($return, 'settings/sort_direction'), |
|
414 | - 'offset' => rgars($return, 'settings/offset'), |
|
410 | + $return[ 'search_criteria' ] = [ |
|
411 | + 'page_size' => rgars( $return, 'settings/page_size' ), |
|
412 | + 'sort_field' => rgars( $return, 'settings/sort_field' ), |
|
413 | + 'sort_direction' => rgars( $return, 'settings/sort_direction' ), |
|
414 | + 'offset' => rgars( $return, 'settings/offset' ), |
|
415 | 415 | ]; |
416 | 416 | |
417 | - unset($return['settings']['page_size'], $return['settings']['sort_field'], $return['settings']['sort_direction']); |
|
417 | + unset( $return[ 'settings' ][ 'page_size' ], $return[ 'settings' ][ 'sort_field' ], $return[ 'settings' ][ 'sort_direction' ] ); |
|
418 | 418 | |
419 | 419 | // Redact for non-logged ins |
420 | - if (!\GVCommon::has_cap('edit_others_gravityviews')) { |
|
421 | - unset($return['settings']); |
|
422 | - unset($return['search_criteria']); |
|
420 | + if ( ! \GVCommon::has_cap( 'edit_others_gravityviews' ) ) { |
|
421 | + unset( $return[ 'settings' ] ); |
|
422 | + unset( $return[ 'search_criteria' ] ); |
|
423 | 423 | } |
424 | 424 | |
425 | - if (!\GFCommon::current_user_can_any('gravityforms_edit_forms')) { |
|
426 | - unset($return['form']); |
|
425 | + if ( ! \GFCommon::current_user_can_any( 'gravityforms_edit_forms' ) ) { |
|
426 | + unset( $return[ 'form' ] ); |
|
427 | 427 | } |
428 | 428 | |
429 | 429 | return $return; |
@@ -434,35 +434,35 @@ discard block |
||
434 | 434 | * |
435 | 435 | * @return bool|\WP_Error |
436 | 436 | */ |
437 | - public function get_item_permissions_check($request) |
|
437 | + public function get_item_permissions_check( $request ) |
|
438 | 438 | { |
439 | - if (func_num_args() === 2) { |
|
440 | - $view_id = func_get_arg(1); // $view_id override |
|
439 | + if ( func_num_args() === 2 ) { |
|
440 | + $view_id = func_get_arg( 1 ); // $view_id override |
|
441 | 441 | } else { |
442 | 442 | $url = $request->get_url_params(); |
443 | - $view_id = intval($url['id']); |
|
443 | + $view_id = intval( $url[ 'id' ] ); |
|
444 | 444 | } |
445 | 445 | |
446 | - if (!$view = \GV\View::by_id($view_id)) { |
|
447 | - return new \WP_Error('rest_forbidden', __('You are not allowed to access this content.', 'gravityview')); |
|
446 | + if ( ! $view = \GV\View::by_id( $view_id ) ) { |
|
447 | + return new \WP_Error( 'rest_forbidden', __( 'You are not allowed to access this content.', 'gravityview' ) ); |
|
448 | 448 | } |
449 | 449 | |
450 | - while ($error = $view->can_render(['rest'], $request)) { |
|
451 | - if (!is_wp_error($error)) { |
|
450 | + while ( $error = $view->can_render( [ 'rest' ], $request ) ) { |
|
451 | + if ( ! is_wp_error( $error ) ) { |
|
452 | 452 | break; |
453 | 453 | } |
454 | 454 | |
455 | - switch (str_replace('gravityview/', '', $error->get_error_code())) { |
|
455 | + switch ( str_replace( 'gravityview/', '', $error->get_error_code() ) ) { |
|
456 | 456 | case 'rest_disabled': |
457 | 457 | case 'post_password_required': |
458 | 458 | case 'not_public': |
459 | 459 | case 'embed_only': |
460 | 460 | case 'no_direct_access': |
461 | - return new \WP_Error('rest_forbidden_access_denied', __('You are not allowed to access this content.', 'gravityview')); |
|
461 | + return new \WP_Error( 'rest_forbidden_access_denied', __( 'You are not allowed to access this content.', 'gravityview' ) ); |
|
462 | 462 | case 'no_form_attached': |
463 | - return new \WP_Error('rest_forbidden_no_form_attached', __('This View is not configured properly.', 'gravityview')); |
|
463 | + return new \WP_Error( 'rest_forbidden_no_form_attached', __( 'This View is not configured properly.', 'gravityview' ) ); |
|
464 | 464 | default: |
465 | - return new \WP_Error('rest_forbidden', __('You are not allowed to access this content.', 'gravityview')); |
|
465 | + return new \WP_Error( 'rest_forbidden', __( 'You are not allowed to access this content.', 'gravityview' ) ); |
|
466 | 466 | } |
467 | 467 | } |
468 | 468 | |
@@ -472,62 +472,62 @@ discard block |
||
472 | 472 | * @param bool Enable or not. |
473 | 473 | * @param \GV\View $view The view. |
474 | 474 | */ |
475 | - if (!apply_filters('gravityview/view/output/rest', true, $view)) { |
|
476 | - return new \WP_Error('rest_forbidden', __('You are not allowed to access this content.', 'gravityview')); |
|
475 | + if ( ! apply_filters( 'gravityview/view/output/rest', true, $view ) ) { |
|
476 | + return new \WP_Error( 'rest_forbidden', __( 'You are not allowed to access this content.', 'gravityview' ) ); |
|
477 | 477 | } |
478 | 478 | |
479 | 479 | return true; |
480 | 480 | } |
481 | 481 | |
482 | - public function get_sub_item_permissions_check($request) |
|
482 | + public function get_sub_item_permissions_check( $request ) |
|
483 | 483 | { |
484 | 484 | // Accessing a single entry needs the View access permissions. |
485 | - if (is_wp_error($error = $this->get_items_permissions_check($request))) { |
|
485 | + if ( is_wp_error( $error = $this->get_items_permissions_check( $request ) ) ) { |
|
486 | 486 | return $error; |
487 | 487 | } |
488 | 488 | |
489 | 489 | $url = $request->get_url_params(); |
490 | - $view_id = intval($url['id']); |
|
491 | - $entry_id = intval($url['s_id']); |
|
490 | + $view_id = intval( $url[ 'id' ] ); |
|
491 | + $entry_id = intval( $url[ 's_id' ] ); |
|
492 | 492 | |
493 | - $view = \GV\View::by_id($view_id); |
|
493 | + $view = \GV\View::by_id( $view_id ); |
|
494 | 494 | |
495 | - if (!$entry = \GV\GF_Entry::by_id($entry_id)) { |
|
496 | - return new \WP_Error('rest_forbidden', 'You are not allowed to view this content.', 'gravityview'); |
|
495 | + if ( ! $entry = \GV\GF_Entry::by_id( $entry_id ) ) { |
|
496 | + return new \WP_Error( 'rest_forbidden', 'You are not allowed to view this content.', 'gravityview' ); |
|
497 | 497 | } |
498 | 498 | |
499 | - if ($entry['form_id'] != $view->form->ID) { |
|
500 | - return new \WP_Error('rest_forbidden', 'You are not allowed to view this content.', 'gravityview'); |
|
499 | + if ( $entry[ 'form_id' ] != $view->form->ID ) { |
|
500 | + return new \WP_Error( 'rest_forbidden', 'You are not allowed to view this content.', 'gravityview' ); |
|
501 | 501 | } |
502 | 502 | |
503 | - if ($entry['status'] != 'active') { |
|
504 | - return new \WP_Error('rest_forbidden', 'You are not allowed to view this content.', 'gravityview'); |
|
503 | + if ( $entry[ 'status' ] != 'active' ) { |
|
504 | + return new \WP_Error( 'rest_forbidden', 'You are not allowed to view this content.', 'gravityview' ); |
|
505 | 505 | } |
506 | 506 | |
507 | - if (apply_filters('gravityview_custom_entry_slug', false) && $entry->slug != get_query_var(\GV\Entry::get_endpoint_name())) { |
|
508 | - return new \WP_Error('rest_forbidden', 'You are not allowed to view this content.', 'gravityview'); |
|
507 | + if ( apply_filters( 'gravityview_custom_entry_slug', false ) && $entry->slug != get_query_var( \GV\Entry::get_endpoint_name() ) ) { |
|
508 | + return new \WP_Error( 'rest_forbidden', 'You are not allowed to view this content.', 'gravityview' ); |
|
509 | 509 | } |
510 | 510 | |
511 | - $is_admin_and_can_view = $view->settings->get('admin_show_all_statuses') && \GVCommon::has_cap('gravityview_moderate_entries', $view->ID); |
|
511 | + $is_admin_and_can_view = $view->settings->get( 'admin_show_all_statuses' ) && \GVCommon::has_cap( 'gravityview_moderate_entries', $view->ID ); |
|
512 | 512 | |
513 | - if ($view->settings->get('show_only_approved') && !$is_admin_and_can_view) { |
|
514 | - if (!\GravityView_Entry_Approval_Status::is_approved(gform_get_meta($entry->ID, \GravityView_Entry_Approval::meta_key))) { |
|
515 | - return new \WP_Error('rest_forbidden', 'You are not allowed to view this content.', 'gravityview'); |
|
513 | + if ( $view->settings->get( 'show_only_approved' ) && ! $is_admin_and_can_view ) { |
|
514 | + if ( ! \GravityView_Entry_Approval_Status::is_approved( gform_get_meta( $entry->ID, \GravityView_Entry_Approval::meta_key ) ) ) { |
|
515 | + return new \WP_Error( 'rest_forbidden', 'You are not allowed to view this content.', 'gravityview' ); |
|
516 | 516 | } |
517 | 517 | } |
518 | 518 | |
519 | 519 | return true; |
520 | 520 | } |
521 | 521 | |
522 | - public function get_items_permissions_check($request) |
|
522 | + public function get_items_permissions_check( $request ) |
|
523 | 523 | { |
524 | 524 | // Getting a list of all Views is always possible. |
525 | 525 | return true; |
526 | 526 | } |
527 | 527 | |
528 | - public function get_sub_items_permissions_check($request) |
|
528 | + public function get_sub_items_permissions_check( $request ) |
|
529 | 529 | { |
530 | 530 | // Accessing all entries of a View needs the same permissions as accessing the View. |
531 | - return $this->get_item_permissions_check($request); |
|
531 | + return $this->get_item_permissions_check( $request ); |
|
532 | 532 | } |
533 | 533 | } |
@@ -17,8 +17,7 @@ discard block |
||
17 | 17 | exit(); |
18 | 18 | } |
19 | 19 | |
20 | -class Views_Route extends Route |
|
21 | -{ |
|
20 | +class Views_Route extends Route { |
|
22 | 21 | /** |
23 | 22 | * Route Name. |
24 | 23 | * |
@@ -46,8 +45,7 @@ discard block |
||
46 | 45 | * |
47 | 46 | * @return \WP_Error|\WP_REST_Response |
48 | 47 | */ |
49 | - public function get_items($request) |
|
50 | - { |
|
48 | + public function get_items($request) { |
|
51 | 49 | $page = $request->get_param('page'); |
52 | 50 | $limit = $request->get_param('limit'); |
53 | 51 | |
@@ -82,8 +80,7 @@ discard block |
||
82 | 80 | * |
83 | 81 | * @return \WP_Error|\WP_REST_Response |
84 | 82 | */ |
85 | - public function get_item($request) |
|
86 | - { |
|
83 | + public function get_item($request) { |
|
87 | 84 | $url = $request->get_url_params(); |
88 | 85 | |
89 | 86 | $view_id = intval($url['id']); |
@@ -115,8 +112,7 @@ discard block |
||
115 | 112 | * |
116 | 113 | * @return mixed The data that is sent. |
117 | 114 | */ |
118 | - public function prepare_entry_for_response($view, $entry, \WP_REST_Request $request, $context, $class = null) |
|
119 | - { |
|
115 | + public function prepare_entry_for_response($view, $entry, \WP_REST_Request $request, $context, $class = null) { |
|
120 | 116 | |
121 | 117 | // Only output the fields that should be displayed. |
122 | 118 | $allowed = []; |
@@ -219,8 +215,7 @@ discard block |
||
219 | 215 | * |
220 | 216 | * @return \WP_Error|\WP_REST_Response |
221 | 217 | */ |
222 | - public function get_sub_items($request) |
|
223 | - { |
|
218 | + public function get_sub_items($request) { |
|
224 | 219 | $url = $request->get_url_params(); |
225 | 220 | $view_id = intval($url['id']); |
226 | 221 | $format = \GV\Utils::get($url, 'format', 'json'); |
@@ -355,8 +350,7 @@ discard block |
||
355 | 350 | * |
356 | 351 | * @return \WP_Error|\WP_REST_Response |
357 | 352 | */ |
358 | - public function get_sub_item($request) |
|
359 | - { |
|
353 | + public function get_sub_item($request) { |
|
360 | 354 | $url = $request->get_url_params(); |
361 | 355 | $view_id = intval($url['id']); |
362 | 356 | $entry_id = intval($url['s_id']); |
@@ -384,8 +378,7 @@ discard block |
||
384 | 378 | * |
385 | 379 | * @return mixed |
386 | 380 | */ |
387 | - public function prepare_view_for_response($view_post, \WP_REST_Request $request) |
|
388 | - { |
|
381 | + public function prepare_view_for_response($view_post, \WP_REST_Request $request) { |
|
389 | 382 | if (is_wp_error($this->get_item_permissions_check($request, $view_post->ID))) { |
390 | 383 | // Redacted out view. |
391 | 384 | return ['ID' => $view_post->ID, 'post_content' => __('You are not allowed to access this content.', 'gravityview')]; |
@@ -434,8 +427,7 @@ discard block |
||
434 | 427 | * |
435 | 428 | * @return bool|\WP_Error |
436 | 429 | */ |
437 | - public function get_item_permissions_check($request) |
|
438 | - { |
|
430 | + public function get_item_permissions_check($request) { |
|
439 | 431 | if (func_num_args() === 2) { |
440 | 432 | $view_id = func_get_arg(1); // $view_id override |
441 | 433 | } else { |
@@ -479,8 +471,7 @@ discard block |
||
479 | 471 | return true; |
480 | 472 | } |
481 | 473 | |
482 | - public function get_sub_item_permissions_check($request) |
|
483 | - { |
|
474 | + public function get_sub_item_permissions_check($request) { |
|
484 | 475 | // Accessing a single entry needs the View access permissions. |
485 | 476 | if (is_wp_error($error = $this->get_items_permissions_check($request))) { |
486 | 477 | return $error; |
@@ -519,14 +510,12 @@ discard block |
||
519 | 510 | return true; |
520 | 511 | } |
521 | 512 | |
522 | - public function get_items_permissions_check($request) |
|
523 | - { |
|
513 | + public function get_items_permissions_check($request) { |
|
524 | 514 | // Getting a list of all Views is always possible. |
525 | 515 | return true; |
526 | 516 | } |
527 | 517 | |
528 | - public function get_sub_items_permissions_check($request) |
|
529 | - { |
|
518 | + public function get_sub_items_permissions_check($request) { |
|
530 | 519 | // Accessing all entries of a View needs the same permissions as accessing the View. |
531 | 520 | return $this->get_item_permissions_check($request); |
532 | 521 | } |
@@ -14,443 +14,443 @@ |
||
14 | 14 | |
15 | 15 | /** If this file is called directly, abort. */ |
16 | 16 | if (!defined('GRAVITYVIEW_DIR')) { |
17 | - exit(); |
|
17 | + exit(); |
|
18 | 18 | } |
19 | 19 | |
20 | 20 | abstract class Route extends \WP_REST_Controller |
21 | 21 | { |
22 | - /** |
|
23 | - * Route Name. |
|
24 | - * |
|
25 | - * @since 2.0 |
|
26 | - * |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - protected $route_name; |
|
30 | - |
|
31 | - /** |
|
32 | - * Sub type, forms {$namespace}/route_name/{id}/sub_type type endpoints. |
|
33 | - * |
|
34 | - * @since 2.0 |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - protected $sub_type; |
|
39 | - |
|
40 | - /** |
|
41 | - * Register the routes for the objects of the controller. |
|
42 | - */ |
|
43 | - public function register_routes() |
|
44 | - { |
|
45 | - $namespace = \GV\REST\Core::get_namespace(); |
|
46 | - $base = $this->get_route_name(); |
|
47 | - |
|
48 | - register_rest_route($namespace, '/'.$base, [ |
|
49 | - [ |
|
50 | - 'methods' => \WP_REST_Server::READABLE, |
|
51 | - 'callback' => [$this, 'get_items'], |
|
52 | - 'permission_callback' => [$this, 'get_items_permissions_check'], |
|
53 | - 'args' => [ |
|
54 | - 'page' => [ |
|
55 | - 'default' => 1, |
|
56 | - 'sanitize_callback' => 'absint', |
|
57 | - ], |
|
58 | - 'limit' => [ |
|
59 | - 'default' => 10, |
|
60 | - 'sanitize_callback' => 'absint', |
|
61 | - ], |
|
62 | - 'post_id' => [ |
|
63 | - 'default' => null, |
|
64 | - 'sanitize_callback' => 'absint', |
|
65 | - ], |
|
66 | - ], |
|
67 | - ], |
|
68 | - [ |
|
69 | - 'methods' => \WP_REST_Server::CREATABLE, |
|
70 | - 'callback' => [$this, 'create_item'], |
|
71 | - 'permission_callback' => [$this, 'create_item_permissions_check'], |
|
72 | - 'args' => $this->create_item_args(), |
|
73 | - |
|
74 | - ], |
|
75 | - ]); |
|
76 | - register_rest_route($namespace, '/'.$base.'/(?P<id>[\d]+)', [ |
|
77 | - [ |
|
78 | - 'methods' => \WP_REST_Server::READABLE, |
|
79 | - 'callback' => [$this, 'get_item'], |
|
80 | - 'permission_callback' => [$this, 'get_item_permissions_check'], |
|
81 | - 'args' => [ |
|
82 | - 'context' => [ |
|
83 | - 'default' => 'view', |
|
84 | - ], |
|
85 | - ], |
|
86 | - ], |
|
87 | - [ |
|
88 | - 'methods' => \WP_REST_Server::EDITABLE, |
|
89 | - 'callback' => [$this, 'update_item'], |
|
90 | - 'permission_callback' => [$this, 'update_item_permissions_check'], |
|
91 | - 'args' => $this->update_item_args(), |
|
92 | - ], |
|
93 | - [ |
|
94 | - 'methods' => \WP_REST_Server::DELETABLE, |
|
95 | - 'callback' => [$this, 'delete_item'], |
|
96 | - 'permission_callback' => [$this, 'delete_item_permissions_check'], |
|
97 | - 'args' => [ |
|
98 | - 'force' => [ |
|
99 | - 'default' => false, |
|
100 | - ], |
|
101 | - ], |
|
102 | - ], |
|
103 | - ]); |
|
104 | - |
|
105 | - $sub_type = $this->get_sub_type(); |
|
106 | - |
|
107 | - $format = '(?:\.(?P<format>html|json|csv|tsv))?'; |
|
108 | - |
|
109 | - register_rest_route($namespace, '/'.$base.'/(?P<id>[\d]+)'.'/'.$sub_type.$format, [ |
|
110 | - [ |
|
111 | - 'methods' => \WP_REST_Server::READABLE, |
|
112 | - 'callback' => [$this, 'get_sub_items'], |
|
113 | - 'permission_callback' => [$this, 'get_sub_items_permissions_check'], |
|
114 | - 'args' => [ |
|
115 | - 'page' => [ |
|
116 | - 'default' => 1, |
|
117 | - 'sanitize_callback' => 'absint', |
|
118 | - ], |
|
119 | - 'limit' => [ |
|
120 | - 'default' => 10, |
|
121 | - 'sanitize_callback' => 'absint', |
|
122 | - ], |
|
123 | - 'post_id' => [ |
|
124 | - 'default' => null, |
|
125 | - 'sanitize_callback' => 'absint', |
|
126 | - ], |
|
127 | - ], |
|
128 | - ], |
|
129 | - [ |
|
130 | - 'methods' => \WP_REST_Server::CREATABLE, |
|
131 | - 'callback' => [$this, 'create_sub_item'], |
|
132 | - 'permission_callback' => [$this, 'create_sub_item_permissions_check'], |
|
133 | - 'args' => $this->create_sub_item_args(), |
|
134 | - ], |
|
135 | - ]); |
|
136 | - |
|
137 | - $format = '(?:\.(?P<format>html|json))?'; |
|
138 | - |
|
139 | - register_rest_route($namespace, sprintf('/%s/(?P<id>[\d]+)/%s/(?P<s_id>[\w-]+)%s', $base, $sub_type, $format), [ |
|
140 | - [ |
|
141 | - 'methods' => \WP_REST_Server::READABLE, |
|
142 | - 'callback' => [$this, 'get_sub_item'], |
|
143 | - 'permission_callback' => [$this, 'get_sub_item_permissions_check'], |
|
144 | - 'args' => [ |
|
145 | - 'context' => [ |
|
146 | - 'default' => 'view', |
|
147 | - ], |
|
148 | - ], |
|
149 | - ], |
|
150 | - [ |
|
151 | - 'methods' => \WP_REST_Server::EDITABLE, |
|
152 | - 'callback' => [$this, 'update_sub_item'], |
|
153 | - 'permission_callback' => [$this, 'update_sub_item_permissions_check'], |
|
154 | - 'args' => $this->update_sub_item_args(), |
|
155 | - ], |
|
156 | - [ |
|
157 | - 'methods' => \WP_REST_Server::DELETABLE, |
|
158 | - 'callback' => [$this, 'delete_sub_item'], |
|
159 | - 'permission_callback' => [$this, 'delete_sub_item_permissions_check'], |
|
160 | - 'args' => [ |
|
161 | - 'force' => [ |
|
162 | - 'default' => false, |
|
163 | - ], |
|
164 | - ], |
|
165 | - ], |
|
166 | - ]); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Get route name. |
|
171 | - * |
|
172 | - * MUST SET route_name property in subclass! |
|
173 | - * |
|
174 | - * @since 2.0 |
|
175 | - * |
|
176 | - * @return string |
|
177 | - */ |
|
178 | - protected function get_route_name() |
|
179 | - { |
|
180 | - if (is_string($this->route_name)) { |
|
181 | - return $this->route_name; |
|
182 | - } else { |
|
183 | - _doing_it_wrong(__METHOD__, __('Must set route name in subclass.', 'gravityview'), '2.0'); |
|
184 | - |
|
185 | - return ''; |
|
186 | - } |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Get sub_type. |
|
191 | - * |
|
192 | - * MUST SET sub_type property in subclass! |
|
193 | - * |
|
194 | - * @since 2.0 |
|
195 | - * |
|
196 | - * @return string |
|
197 | - */ |
|
198 | - protected function get_sub_type() |
|
199 | - { |
|
200 | - if (is_string($this->sub_type)) { |
|
201 | - return $this->sub_type; |
|
202 | - } else { |
|
203 | - _doing_it_wrong(__METHOD__, __('Must set route sub type in subclass.', 'gravityview'), '2.0'); |
|
204 | - |
|
205 | - return ''; |
|
206 | - } |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Get a collection of items. |
|
211 | - * |
|
212 | - * @param \WP_REST_Request $request Full data about the request. |
|
213 | - * |
|
214 | - * @return \WP_Error|\WP_REST_Response |
|
215 | - */ |
|
216 | - public function get_items($request) |
|
217 | - { |
|
218 | - return $this->not_implemented(); |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Get one item from the collection. |
|
223 | - * |
|
224 | - * @param \WP_REST_Request $request Full data about the request. |
|
225 | - * |
|
226 | - * @return \WP_Error|\WP_REST_Response |
|
227 | - */ |
|
228 | - public function get_item($request) |
|
229 | - { |
|
230 | - return $this->not_implemented(); |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Create one item from the collection. |
|
235 | - * |
|
236 | - * @param \WP_REST_Request $request Full data about the request. |
|
237 | - * |
|
238 | - * @return \WP_REST_Response |
|
239 | - */ |
|
240 | - public function create_item($request) |
|
241 | - { |
|
242 | - return $this->not_implemented(); |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Update one item from the collection. |
|
247 | - * |
|
248 | - * @param \WP_REST_Request $request Full data about the request. |
|
249 | - * |
|
250 | - * @return \WP_REST_Response |
|
251 | - */ |
|
252 | - public function update_item($request) |
|
253 | - { |
|
254 | - return $this->not_implemented(); |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * Delete one item from the collection. |
|
259 | - * |
|
260 | - * @param \WP_REST_Request $request Full data about the request. |
|
261 | - * |
|
262 | - * @return \WP_REST_Response |
|
263 | - */ |
|
264 | - public function delete_item($request) |
|
265 | - { |
|
266 | - return $this->not_implemented(); |
|
267 | - } |
|
268 | - |
|
269 | - /** |
|
270 | - * Get a collection of items. |
|
271 | - * |
|
272 | - * @param \WP_REST_Request $request Full data about the request. |
|
273 | - * |
|
274 | - * @return \WP_Error|\WP_REST_Response |
|
275 | - */ |
|
276 | - public function get_sub_items($request) |
|
277 | - { |
|
278 | - return $this->not_implemented(); |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * Get one item from the collection. |
|
283 | - * |
|
284 | - * @param \WP_REST_Request $request Full data about the request. |
|
285 | - * |
|
286 | - * @return \WP_Error|\WP_REST_Response |
|
287 | - */ |
|
288 | - public function get_sub_item($request) |
|
289 | - { |
|
290 | - return $this->not_implemented(); |
|
291 | - } |
|
292 | - |
|
293 | - /** |
|
294 | - * Create one item from the collection. |
|
295 | - * |
|
296 | - * @param \WP_REST_Request $request Full data about the request. |
|
297 | - * |
|
298 | - * @return \WP_REST_Response |
|
299 | - */ |
|
300 | - public function create_sub_item($request) |
|
301 | - { |
|
302 | - return $this->not_implemented(); |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * Update one item from the collection for sub items. |
|
307 | - * |
|
308 | - * @param \WP_REST_Request $request Full data about the request. |
|
309 | - * |
|
310 | - * @return \WP_REST_Response |
|
311 | - */ |
|
312 | - public function update_sub_item($request) |
|
313 | - { |
|
314 | - return $this->not_implemented(); |
|
315 | - } |
|
316 | - |
|
317 | - /** |
|
318 | - * Delete one item from the collection for sub items. |
|
319 | - * |
|
320 | - * @param \WP_REST_Request $request Full data about the request. |
|
321 | - * |
|
322 | - * @return \WP_REST_Response |
|
323 | - */ |
|
324 | - public function delete_sub_item($request) |
|
325 | - { |
|
326 | - return $this->not_implemented(); |
|
327 | - } |
|
328 | - |
|
329 | - /** |
|
330 | - * Check if a given request has access to get items. |
|
331 | - * |
|
332 | - * @param \WP_REST_Request $request Full data about the request. |
|
333 | - * |
|
334 | - * @return \WP_REST_Response |
|
335 | - */ |
|
336 | - public function get_items_permissions_check($request) |
|
337 | - { |
|
338 | - return $this->not_implemented(); |
|
339 | - } |
|
340 | - |
|
341 | - /** |
|
342 | - * Check if a given request has access to get a specific item. |
|
343 | - * |
|
344 | - * @param \WP_REST_Request $request Full data about the request. |
|
345 | - * |
|
346 | - * @return \WP_REST_Response |
|
347 | - */ |
|
348 | - public function get_item_permissions_check($request) |
|
349 | - { |
|
350 | - return $this->not_implemented(); |
|
351 | - } |
|
352 | - |
|
353 | - /** |
|
354 | - * Check if a given request has access to create items. |
|
355 | - * |
|
356 | - * @param \WP_REST_Request $request Full data about the request. |
|
357 | - * |
|
358 | - * @return \WP_REST_Response |
|
359 | - */ |
|
360 | - public function create_item_permissions_check($request) |
|
361 | - { |
|
362 | - return $this->not_implemented(); |
|
363 | - } |
|
364 | - |
|
365 | - /** |
|
366 | - * Check if a given request has access to update a specific item. |
|
367 | - * |
|
368 | - * @param \WP_REST_Request $request Full data about the request. |
|
369 | - * |
|
370 | - * @return \WP_REST_Response |
|
371 | - */ |
|
372 | - public function update_item_permissions_check($request) |
|
373 | - { |
|
374 | - return $this->not_implemented(); |
|
375 | - } |
|
376 | - |
|
377 | - /** |
|
378 | - * Check if a given request has access to delete a specific item. |
|
379 | - * |
|
380 | - * @param \WP_REST_Request $request Full data about the request. |
|
381 | - * |
|
382 | - * @return \WP_REST_Response |
|
383 | - */ |
|
384 | - public function delete_item_permissions_check($request) |
|
385 | - { |
|
386 | - return $this->not_implemented(); |
|
387 | - } |
|
388 | - |
|
389 | - /** |
|
390 | - * Prepare the item for create or update operation. |
|
391 | - * |
|
392 | - * @todo ZACK - Use this as generic prepare to save or remove from usage. |
|
393 | - * |
|
394 | - * @param \WP_REST_Request $request Request object |
|
395 | - * |
|
396 | - * @return \WP_REST_Response |
|
397 | - */ |
|
398 | - protected function prepare_item_for_database($request) |
|
399 | - { |
|
400 | - return $this->not_implemented(); |
|
401 | - } |
|
402 | - |
|
403 | - /** |
|
404 | - * Prepare the item for the REST response. |
|
405 | - * |
|
406 | - * @todo ZACK - Use this as generic prepare for response or remove from usage |
|
407 | - * |
|
408 | - * @since 2.0 |
|
409 | - * |
|
410 | - * @param mixed $item WordPress representation of the item. |
|
411 | - * @param \WP_REST_Request $request Request object. |
|
412 | - * |
|
413 | - * @return \WP_REST_Response |
|
414 | - */ |
|
415 | - public function prepare_item_for_response($item, $request) |
|
416 | - { |
|
417 | - return $this->not_implemented(); |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * Generic response for routes not yet implemented. |
|
422 | - * |
|
423 | - * @since 2.0 |
|
424 | - * |
|
425 | - * @return \WP_REST_Response |
|
426 | - */ |
|
427 | - protected function not_implemented() |
|
428 | - { |
|
429 | - $error = new \WP_Error('not-implemented-yet', __('Endpoint Not Yet Implemented.', 'gravityview')); |
|
430 | - |
|
431 | - return new \WP_REST_Response($error, 501); |
|
432 | - } |
|
433 | - |
|
434 | - /** |
|
435 | - * Fallback if subclass doesn't define routes. |
|
436 | - * |
|
437 | - * Returns empty array for args instead of making an error. |
|
438 | - * |
|
439 | - * @since 2.0 |
|
440 | - * |
|
441 | - * @param $method |
|
442 | - * |
|
443 | - * @return array |
|
444 | - */ |
|
445 | - public function __call($method, $args) |
|
446 | - { |
|
447 | - if (in_array($method, [ |
|
448 | - 'create_item_args', |
|
449 | - 'update_item_args', |
|
450 | - 'create_sub_item_args', |
|
451 | - 'update_sub_item_args', |
|
452 | - ])) { |
|
453 | - return []; |
|
454 | - } |
|
455 | - } |
|
22 | + /** |
|
23 | + * Route Name. |
|
24 | + * |
|
25 | + * @since 2.0 |
|
26 | + * |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + protected $route_name; |
|
30 | + |
|
31 | + /** |
|
32 | + * Sub type, forms {$namespace}/route_name/{id}/sub_type type endpoints. |
|
33 | + * |
|
34 | + * @since 2.0 |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + protected $sub_type; |
|
39 | + |
|
40 | + /** |
|
41 | + * Register the routes for the objects of the controller. |
|
42 | + */ |
|
43 | + public function register_routes() |
|
44 | + { |
|
45 | + $namespace = \GV\REST\Core::get_namespace(); |
|
46 | + $base = $this->get_route_name(); |
|
47 | + |
|
48 | + register_rest_route($namespace, '/'.$base, [ |
|
49 | + [ |
|
50 | + 'methods' => \WP_REST_Server::READABLE, |
|
51 | + 'callback' => [$this, 'get_items'], |
|
52 | + 'permission_callback' => [$this, 'get_items_permissions_check'], |
|
53 | + 'args' => [ |
|
54 | + 'page' => [ |
|
55 | + 'default' => 1, |
|
56 | + 'sanitize_callback' => 'absint', |
|
57 | + ], |
|
58 | + 'limit' => [ |
|
59 | + 'default' => 10, |
|
60 | + 'sanitize_callback' => 'absint', |
|
61 | + ], |
|
62 | + 'post_id' => [ |
|
63 | + 'default' => null, |
|
64 | + 'sanitize_callback' => 'absint', |
|
65 | + ], |
|
66 | + ], |
|
67 | + ], |
|
68 | + [ |
|
69 | + 'methods' => \WP_REST_Server::CREATABLE, |
|
70 | + 'callback' => [$this, 'create_item'], |
|
71 | + 'permission_callback' => [$this, 'create_item_permissions_check'], |
|
72 | + 'args' => $this->create_item_args(), |
|
73 | + |
|
74 | + ], |
|
75 | + ]); |
|
76 | + register_rest_route($namespace, '/'.$base.'/(?P<id>[\d]+)', [ |
|
77 | + [ |
|
78 | + 'methods' => \WP_REST_Server::READABLE, |
|
79 | + 'callback' => [$this, 'get_item'], |
|
80 | + 'permission_callback' => [$this, 'get_item_permissions_check'], |
|
81 | + 'args' => [ |
|
82 | + 'context' => [ |
|
83 | + 'default' => 'view', |
|
84 | + ], |
|
85 | + ], |
|
86 | + ], |
|
87 | + [ |
|
88 | + 'methods' => \WP_REST_Server::EDITABLE, |
|
89 | + 'callback' => [$this, 'update_item'], |
|
90 | + 'permission_callback' => [$this, 'update_item_permissions_check'], |
|
91 | + 'args' => $this->update_item_args(), |
|
92 | + ], |
|
93 | + [ |
|
94 | + 'methods' => \WP_REST_Server::DELETABLE, |
|
95 | + 'callback' => [$this, 'delete_item'], |
|
96 | + 'permission_callback' => [$this, 'delete_item_permissions_check'], |
|
97 | + 'args' => [ |
|
98 | + 'force' => [ |
|
99 | + 'default' => false, |
|
100 | + ], |
|
101 | + ], |
|
102 | + ], |
|
103 | + ]); |
|
104 | + |
|
105 | + $sub_type = $this->get_sub_type(); |
|
106 | + |
|
107 | + $format = '(?:\.(?P<format>html|json|csv|tsv))?'; |
|
108 | + |
|
109 | + register_rest_route($namespace, '/'.$base.'/(?P<id>[\d]+)'.'/'.$sub_type.$format, [ |
|
110 | + [ |
|
111 | + 'methods' => \WP_REST_Server::READABLE, |
|
112 | + 'callback' => [$this, 'get_sub_items'], |
|
113 | + 'permission_callback' => [$this, 'get_sub_items_permissions_check'], |
|
114 | + 'args' => [ |
|
115 | + 'page' => [ |
|
116 | + 'default' => 1, |
|
117 | + 'sanitize_callback' => 'absint', |
|
118 | + ], |
|
119 | + 'limit' => [ |
|
120 | + 'default' => 10, |
|
121 | + 'sanitize_callback' => 'absint', |
|
122 | + ], |
|
123 | + 'post_id' => [ |
|
124 | + 'default' => null, |
|
125 | + 'sanitize_callback' => 'absint', |
|
126 | + ], |
|
127 | + ], |
|
128 | + ], |
|
129 | + [ |
|
130 | + 'methods' => \WP_REST_Server::CREATABLE, |
|
131 | + 'callback' => [$this, 'create_sub_item'], |
|
132 | + 'permission_callback' => [$this, 'create_sub_item_permissions_check'], |
|
133 | + 'args' => $this->create_sub_item_args(), |
|
134 | + ], |
|
135 | + ]); |
|
136 | + |
|
137 | + $format = '(?:\.(?P<format>html|json))?'; |
|
138 | + |
|
139 | + register_rest_route($namespace, sprintf('/%s/(?P<id>[\d]+)/%s/(?P<s_id>[\w-]+)%s', $base, $sub_type, $format), [ |
|
140 | + [ |
|
141 | + 'methods' => \WP_REST_Server::READABLE, |
|
142 | + 'callback' => [$this, 'get_sub_item'], |
|
143 | + 'permission_callback' => [$this, 'get_sub_item_permissions_check'], |
|
144 | + 'args' => [ |
|
145 | + 'context' => [ |
|
146 | + 'default' => 'view', |
|
147 | + ], |
|
148 | + ], |
|
149 | + ], |
|
150 | + [ |
|
151 | + 'methods' => \WP_REST_Server::EDITABLE, |
|
152 | + 'callback' => [$this, 'update_sub_item'], |
|
153 | + 'permission_callback' => [$this, 'update_sub_item_permissions_check'], |
|
154 | + 'args' => $this->update_sub_item_args(), |
|
155 | + ], |
|
156 | + [ |
|
157 | + 'methods' => \WP_REST_Server::DELETABLE, |
|
158 | + 'callback' => [$this, 'delete_sub_item'], |
|
159 | + 'permission_callback' => [$this, 'delete_sub_item_permissions_check'], |
|
160 | + 'args' => [ |
|
161 | + 'force' => [ |
|
162 | + 'default' => false, |
|
163 | + ], |
|
164 | + ], |
|
165 | + ], |
|
166 | + ]); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Get route name. |
|
171 | + * |
|
172 | + * MUST SET route_name property in subclass! |
|
173 | + * |
|
174 | + * @since 2.0 |
|
175 | + * |
|
176 | + * @return string |
|
177 | + */ |
|
178 | + protected function get_route_name() |
|
179 | + { |
|
180 | + if (is_string($this->route_name)) { |
|
181 | + return $this->route_name; |
|
182 | + } else { |
|
183 | + _doing_it_wrong(__METHOD__, __('Must set route name in subclass.', 'gravityview'), '2.0'); |
|
184 | + |
|
185 | + return ''; |
|
186 | + } |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Get sub_type. |
|
191 | + * |
|
192 | + * MUST SET sub_type property in subclass! |
|
193 | + * |
|
194 | + * @since 2.0 |
|
195 | + * |
|
196 | + * @return string |
|
197 | + */ |
|
198 | + protected function get_sub_type() |
|
199 | + { |
|
200 | + if (is_string($this->sub_type)) { |
|
201 | + return $this->sub_type; |
|
202 | + } else { |
|
203 | + _doing_it_wrong(__METHOD__, __('Must set route sub type in subclass.', 'gravityview'), '2.0'); |
|
204 | + |
|
205 | + return ''; |
|
206 | + } |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Get a collection of items. |
|
211 | + * |
|
212 | + * @param \WP_REST_Request $request Full data about the request. |
|
213 | + * |
|
214 | + * @return \WP_Error|\WP_REST_Response |
|
215 | + */ |
|
216 | + public function get_items($request) |
|
217 | + { |
|
218 | + return $this->not_implemented(); |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Get one item from the collection. |
|
223 | + * |
|
224 | + * @param \WP_REST_Request $request Full data about the request. |
|
225 | + * |
|
226 | + * @return \WP_Error|\WP_REST_Response |
|
227 | + */ |
|
228 | + public function get_item($request) |
|
229 | + { |
|
230 | + return $this->not_implemented(); |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Create one item from the collection. |
|
235 | + * |
|
236 | + * @param \WP_REST_Request $request Full data about the request. |
|
237 | + * |
|
238 | + * @return \WP_REST_Response |
|
239 | + */ |
|
240 | + public function create_item($request) |
|
241 | + { |
|
242 | + return $this->not_implemented(); |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Update one item from the collection. |
|
247 | + * |
|
248 | + * @param \WP_REST_Request $request Full data about the request. |
|
249 | + * |
|
250 | + * @return \WP_REST_Response |
|
251 | + */ |
|
252 | + public function update_item($request) |
|
253 | + { |
|
254 | + return $this->not_implemented(); |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * Delete one item from the collection. |
|
259 | + * |
|
260 | + * @param \WP_REST_Request $request Full data about the request. |
|
261 | + * |
|
262 | + * @return \WP_REST_Response |
|
263 | + */ |
|
264 | + public function delete_item($request) |
|
265 | + { |
|
266 | + return $this->not_implemented(); |
|
267 | + } |
|
268 | + |
|
269 | + /** |
|
270 | + * Get a collection of items. |
|
271 | + * |
|
272 | + * @param \WP_REST_Request $request Full data about the request. |
|
273 | + * |
|
274 | + * @return \WP_Error|\WP_REST_Response |
|
275 | + */ |
|
276 | + public function get_sub_items($request) |
|
277 | + { |
|
278 | + return $this->not_implemented(); |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * Get one item from the collection. |
|
283 | + * |
|
284 | + * @param \WP_REST_Request $request Full data about the request. |
|
285 | + * |
|
286 | + * @return \WP_Error|\WP_REST_Response |
|
287 | + */ |
|
288 | + public function get_sub_item($request) |
|
289 | + { |
|
290 | + return $this->not_implemented(); |
|
291 | + } |
|
292 | + |
|
293 | + /** |
|
294 | + * Create one item from the collection. |
|
295 | + * |
|
296 | + * @param \WP_REST_Request $request Full data about the request. |
|
297 | + * |
|
298 | + * @return \WP_REST_Response |
|
299 | + */ |
|
300 | + public function create_sub_item($request) |
|
301 | + { |
|
302 | + return $this->not_implemented(); |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * Update one item from the collection for sub items. |
|
307 | + * |
|
308 | + * @param \WP_REST_Request $request Full data about the request. |
|
309 | + * |
|
310 | + * @return \WP_REST_Response |
|
311 | + */ |
|
312 | + public function update_sub_item($request) |
|
313 | + { |
|
314 | + return $this->not_implemented(); |
|
315 | + } |
|
316 | + |
|
317 | + /** |
|
318 | + * Delete one item from the collection for sub items. |
|
319 | + * |
|
320 | + * @param \WP_REST_Request $request Full data about the request. |
|
321 | + * |
|
322 | + * @return \WP_REST_Response |
|
323 | + */ |
|
324 | + public function delete_sub_item($request) |
|
325 | + { |
|
326 | + return $this->not_implemented(); |
|
327 | + } |
|
328 | + |
|
329 | + /** |
|
330 | + * Check if a given request has access to get items. |
|
331 | + * |
|
332 | + * @param \WP_REST_Request $request Full data about the request. |
|
333 | + * |
|
334 | + * @return \WP_REST_Response |
|
335 | + */ |
|
336 | + public function get_items_permissions_check($request) |
|
337 | + { |
|
338 | + return $this->not_implemented(); |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * Check if a given request has access to get a specific item. |
|
343 | + * |
|
344 | + * @param \WP_REST_Request $request Full data about the request. |
|
345 | + * |
|
346 | + * @return \WP_REST_Response |
|
347 | + */ |
|
348 | + public function get_item_permissions_check($request) |
|
349 | + { |
|
350 | + return $this->not_implemented(); |
|
351 | + } |
|
352 | + |
|
353 | + /** |
|
354 | + * Check if a given request has access to create items. |
|
355 | + * |
|
356 | + * @param \WP_REST_Request $request Full data about the request. |
|
357 | + * |
|
358 | + * @return \WP_REST_Response |
|
359 | + */ |
|
360 | + public function create_item_permissions_check($request) |
|
361 | + { |
|
362 | + return $this->not_implemented(); |
|
363 | + } |
|
364 | + |
|
365 | + /** |
|
366 | + * Check if a given request has access to update a specific item. |
|
367 | + * |
|
368 | + * @param \WP_REST_Request $request Full data about the request. |
|
369 | + * |
|
370 | + * @return \WP_REST_Response |
|
371 | + */ |
|
372 | + public function update_item_permissions_check($request) |
|
373 | + { |
|
374 | + return $this->not_implemented(); |
|
375 | + } |
|
376 | + |
|
377 | + /** |
|
378 | + * Check if a given request has access to delete a specific item. |
|
379 | + * |
|
380 | + * @param \WP_REST_Request $request Full data about the request. |
|
381 | + * |
|
382 | + * @return \WP_REST_Response |
|
383 | + */ |
|
384 | + public function delete_item_permissions_check($request) |
|
385 | + { |
|
386 | + return $this->not_implemented(); |
|
387 | + } |
|
388 | + |
|
389 | + /** |
|
390 | + * Prepare the item for create or update operation. |
|
391 | + * |
|
392 | + * @todo ZACK - Use this as generic prepare to save or remove from usage. |
|
393 | + * |
|
394 | + * @param \WP_REST_Request $request Request object |
|
395 | + * |
|
396 | + * @return \WP_REST_Response |
|
397 | + */ |
|
398 | + protected function prepare_item_for_database($request) |
|
399 | + { |
|
400 | + return $this->not_implemented(); |
|
401 | + } |
|
402 | + |
|
403 | + /** |
|
404 | + * Prepare the item for the REST response. |
|
405 | + * |
|
406 | + * @todo ZACK - Use this as generic prepare for response or remove from usage |
|
407 | + * |
|
408 | + * @since 2.0 |
|
409 | + * |
|
410 | + * @param mixed $item WordPress representation of the item. |
|
411 | + * @param \WP_REST_Request $request Request object. |
|
412 | + * |
|
413 | + * @return \WP_REST_Response |
|
414 | + */ |
|
415 | + public function prepare_item_for_response($item, $request) |
|
416 | + { |
|
417 | + return $this->not_implemented(); |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * Generic response for routes not yet implemented. |
|
422 | + * |
|
423 | + * @since 2.0 |
|
424 | + * |
|
425 | + * @return \WP_REST_Response |
|
426 | + */ |
|
427 | + protected function not_implemented() |
|
428 | + { |
|
429 | + $error = new \WP_Error('not-implemented-yet', __('Endpoint Not Yet Implemented.', 'gravityview')); |
|
430 | + |
|
431 | + return new \WP_REST_Response($error, 501); |
|
432 | + } |
|
433 | + |
|
434 | + /** |
|
435 | + * Fallback if subclass doesn't define routes. |
|
436 | + * |
|
437 | + * Returns empty array for args instead of making an error. |
|
438 | + * |
|
439 | + * @since 2.0 |
|
440 | + * |
|
441 | + * @param $method |
|
442 | + * |
|
443 | + * @return array |
|
444 | + */ |
|
445 | + public function __call($method, $args) |
|
446 | + { |
|
447 | + if (in_array($method, [ |
|
448 | + 'create_item_args', |
|
449 | + 'update_item_args', |
|
450 | + 'create_sub_item_args', |
|
451 | + 'update_sub_item_args', |
|
452 | + ])) { |
|
453 | + return []; |
|
454 | + } |
|
455 | + } |
|
456 | 456 | } |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | namespace GV\REST; |
14 | 14 | |
15 | 15 | /** If this file is called directly, abort. */ |
16 | -if (!defined('GRAVITYVIEW_DIR')) { |
|
16 | +if ( ! defined( 'GRAVITYVIEW_DIR' ) ) { |
|
17 | 17 | exit(); |
18 | 18 | } |
19 | 19 | |
@@ -45,11 +45,11 @@ discard block |
||
45 | 45 | $namespace = \GV\REST\Core::get_namespace(); |
46 | 46 | $base = $this->get_route_name(); |
47 | 47 | |
48 | - register_rest_route($namespace, '/'.$base, [ |
|
48 | + register_rest_route( $namespace, '/' . $base, [ |
|
49 | 49 | [ |
50 | 50 | 'methods' => \WP_REST_Server::READABLE, |
51 | - 'callback' => [$this, 'get_items'], |
|
52 | - 'permission_callback' => [$this, 'get_items_permissions_check'], |
|
51 | + 'callback' => [ $this, 'get_items' ], |
|
52 | + 'permission_callback' => [ $this, 'get_items_permissions_check' ], |
|
53 | 53 | 'args' => [ |
54 | 54 | 'page' => [ |
55 | 55 | 'default' => 1, |
@@ -67,17 +67,17 @@ discard block |
||
67 | 67 | ], |
68 | 68 | [ |
69 | 69 | 'methods' => \WP_REST_Server::CREATABLE, |
70 | - 'callback' => [$this, 'create_item'], |
|
71 | - 'permission_callback' => [$this, 'create_item_permissions_check'], |
|
70 | + 'callback' => [ $this, 'create_item' ], |
|
71 | + 'permission_callback' => [ $this, 'create_item_permissions_check' ], |
|
72 | 72 | 'args' => $this->create_item_args(), |
73 | 73 | |
74 | 74 | ], |
75 | - ]); |
|
76 | - register_rest_route($namespace, '/'.$base.'/(?P<id>[\d]+)', [ |
|
75 | + ] ); |
|
76 | + register_rest_route( $namespace, '/' . $base . '/(?P<id>[\d]+)', [ |
|
77 | 77 | [ |
78 | 78 | 'methods' => \WP_REST_Server::READABLE, |
79 | - 'callback' => [$this, 'get_item'], |
|
80 | - 'permission_callback' => [$this, 'get_item_permissions_check'], |
|
79 | + 'callback' => [ $this, 'get_item' ], |
|
80 | + 'permission_callback' => [ $this, 'get_item_permissions_check' ], |
|
81 | 81 | 'args' => [ |
82 | 82 | 'context' => [ |
83 | 83 | 'default' => 'view', |
@@ -86,31 +86,31 @@ discard block |
||
86 | 86 | ], |
87 | 87 | [ |
88 | 88 | 'methods' => \WP_REST_Server::EDITABLE, |
89 | - 'callback' => [$this, 'update_item'], |
|
90 | - 'permission_callback' => [$this, 'update_item_permissions_check'], |
|
89 | + 'callback' => [ $this, 'update_item' ], |
|
90 | + 'permission_callback' => [ $this, 'update_item_permissions_check' ], |
|
91 | 91 | 'args' => $this->update_item_args(), |
92 | 92 | ], |
93 | 93 | [ |
94 | 94 | 'methods' => \WP_REST_Server::DELETABLE, |
95 | - 'callback' => [$this, 'delete_item'], |
|
96 | - 'permission_callback' => [$this, 'delete_item_permissions_check'], |
|
95 | + 'callback' => [ $this, 'delete_item' ], |
|
96 | + 'permission_callback' => [ $this, 'delete_item_permissions_check' ], |
|
97 | 97 | 'args' => [ |
98 | 98 | 'force' => [ |
99 | 99 | 'default' => false, |
100 | 100 | ], |
101 | 101 | ], |
102 | 102 | ], |
103 | - ]); |
|
103 | + ] ); |
|
104 | 104 | |
105 | 105 | $sub_type = $this->get_sub_type(); |
106 | 106 | |
107 | 107 | $format = '(?:\.(?P<format>html|json|csv|tsv))?'; |
108 | 108 | |
109 | - register_rest_route($namespace, '/'.$base.'/(?P<id>[\d]+)'.'/'.$sub_type.$format, [ |
|
109 | + register_rest_route( $namespace, '/' . $base . '/(?P<id>[\d]+)' . '/' . $sub_type . $format, [ |
|
110 | 110 | [ |
111 | 111 | 'methods' => \WP_REST_Server::READABLE, |
112 | - 'callback' => [$this, 'get_sub_items'], |
|
113 | - 'permission_callback' => [$this, 'get_sub_items_permissions_check'], |
|
112 | + 'callback' => [ $this, 'get_sub_items' ], |
|
113 | + 'permission_callback' => [ $this, 'get_sub_items_permissions_check' ], |
|
114 | 114 | 'args' => [ |
115 | 115 | 'page' => [ |
116 | 116 | 'default' => 1, |
@@ -128,19 +128,19 @@ discard block |
||
128 | 128 | ], |
129 | 129 | [ |
130 | 130 | 'methods' => \WP_REST_Server::CREATABLE, |
131 | - 'callback' => [$this, 'create_sub_item'], |
|
132 | - 'permission_callback' => [$this, 'create_sub_item_permissions_check'], |
|
131 | + 'callback' => [ $this, 'create_sub_item' ], |
|
132 | + 'permission_callback' => [ $this, 'create_sub_item_permissions_check' ], |
|
133 | 133 | 'args' => $this->create_sub_item_args(), |
134 | 134 | ], |
135 | - ]); |
|
135 | + ] ); |
|
136 | 136 | |
137 | 137 | $format = '(?:\.(?P<format>html|json))?'; |
138 | 138 | |
139 | - register_rest_route($namespace, sprintf('/%s/(?P<id>[\d]+)/%s/(?P<s_id>[\w-]+)%s', $base, $sub_type, $format), [ |
|
139 | + register_rest_route( $namespace, sprintf( '/%s/(?P<id>[\d]+)/%s/(?P<s_id>[\w-]+)%s', $base, $sub_type, $format ), [ |
|
140 | 140 | [ |
141 | 141 | 'methods' => \WP_REST_Server::READABLE, |
142 | - 'callback' => [$this, 'get_sub_item'], |
|
143 | - 'permission_callback' => [$this, 'get_sub_item_permissions_check'], |
|
142 | + 'callback' => [ $this, 'get_sub_item' ], |
|
143 | + 'permission_callback' => [ $this, 'get_sub_item_permissions_check' ], |
|
144 | 144 | 'args' => [ |
145 | 145 | 'context' => [ |
146 | 146 | 'default' => 'view', |
@@ -149,21 +149,21 @@ discard block |
||
149 | 149 | ], |
150 | 150 | [ |
151 | 151 | 'methods' => \WP_REST_Server::EDITABLE, |
152 | - 'callback' => [$this, 'update_sub_item'], |
|
153 | - 'permission_callback' => [$this, 'update_sub_item_permissions_check'], |
|
152 | + 'callback' => [ $this, 'update_sub_item' ], |
|
153 | + 'permission_callback' => [ $this, 'update_sub_item_permissions_check' ], |
|
154 | 154 | 'args' => $this->update_sub_item_args(), |
155 | 155 | ], |
156 | 156 | [ |
157 | 157 | 'methods' => \WP_REST_Server::DELETABLE, |
158 | - 'callback' => [$this, 'delete_sub_item'], |
|
159 | - 'permission_callback' => [$this, 'delete_sub_item_permissions_check'], |
|
158 | + 'callback' => [ $this, 'delete_sub_item' ], |
|
159 | + 'permission_callback' => [ $this, 'delete_sub_item_permissions_check' ], |
|
160 | 160 | 'args' => [ |
161 | 161 | 'force' => [ |
162 | 162 | 'default' => false, |
163 | 163 | ], |
164 | 164 | ], |
165 | 165 | ], |
166 | - ]); |
|
166 | + ] ); |
|
167 | 167 | } |
168 | 168 | |
169 | 169 | /** |
@@ -177,10 +177,10 @@ discard block |
||
177 | 177 | */ |
178 | 178 | protected function get_route_name() |
179 | 179 | { |
180 | - if (is_string($this->route_name)) { |
|
180 | + if ( is_string( $this->route_name ) ) { |
|
181 | 181 | return $this->route_name; |
182 | 182 | } else { |
183 | - _doing_it_wrong(__METHOD__, __('Must set route name in subclass.', 'gravityview'), '2.0'); |
|
183 | + _doing_it_wrong( __METHOD__, __( 'Must set route name in subclass.', 'gravityview' ), '2.0' ); |
|
184 | 184 | |
185 | 185 | return ''; |
186 | 186 | } |
@@ -197,10 +197,10 @@ discard block |
||
197 | 197 | */ |
198 | 198 | protected function get_sub_type() |
199 | 199 | { |
200 | - if (is_string($this->sub_type)) { |
|
200 | + if ( is_string( $this->sub_type ) ) { |
|
201 | 201 | return $this->sub_type; |
202 | 202 | } else { |
203 | - _doing_it_wrong(__METHOD__, __('Must set route sub type in subclass.', 'gravityview'), '2.0'); |
|
203 | + _doing_it_wrong( __METHOD__, __( 'Must set route sub type in subclass.', 'gravityview' ), '2.0' ); |
|
204 | 204 | |
205 | 205 | return ''; |
206 | 206 | } |
@@ -213,7 +213,7 @@ discard block |
||
213 | 213 | * |
214 | 214 | * @return \WP_Error|\WP_REST_Response |
215 | 215 | */ |
216 | - public function get_items($request) |
|
216 | + public function get_items( $request ) |
|
217 | 217 | { |
218 | 218 | return $this->not_implemented(); |
219 | 219 | } |
@@ -225,7 +225,7 @@ discard block |
||
225 | 225 | * |
226 | 226 | * @return \WP_Error|\WP_REST_Response |
227 | 227 | */ |
228 | - public function get_item($request) |
|
228 | + public function get_item( $request ) |
|
229 | 229 | { |
230 | 230 | return $this->not_implemented(); |
231 | 231 | } |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | * |
238 | 238 | * @return \WP_REST_Response |
239 | 239 | */ |
240 | - public function create_item($request) |
|
240 | + public function create_item( $request ) |
|
241 | 241 | { |
242 | 242 | return $this->not_implemented(); |
243 | 243 | } |
@@ -249,7 +249,7 @@ discard block |
||
249 | 249 | * |
250 | 250 | * @return \WP_REST_Response |
251 | 251 | */ |
252 | - public function update_item($request) |
|
252 | + public function update_item( $request ) |
|
253 | 253 | { |
254 | 254 | return $this->not_implemented(); |
255 | 255 | } |
@@ -261,7 +261,7 @@ discard block |
||
261 | 261 | * |
262 | 262 | * @return \WP_REST_Response |
263 | 263 | */ |
264 | - public function delete_item($request) |
|
264 | + public function delete_item( $request ) |
|
265 | 265 | { |
266 | 266 | return $this->not_implemented(); |
267 | 267 | } |
@@ -273,7 +273,7 @@ discard block |
||
273 | 273 | * |
274 | 274 | * @return \WP_Error|\WP_REST_Response |
275 | 275 | */ |
276 | - public function get_sub_items($request) |
|
276 | + public function get_sub_items( $request ) |
|
277 | 277 | { |
278 | 278 | return $this->not_implemented(); |
279 | 279 | } |
@@ -285,7 +285,7 @@ discard block |
||
285 | 285 | * |
286 | 286 | * @return \WP_Error|\WP_REST_Response |
287 | 287 | */ |
288 | - public function get_sub_item($request) |
|
288 | + public function get_sub_item( $request ) |
|
289 | 289 | { |
290 | 290 | return $this->not_implemented(); |
291 | 291 | } |
@@ -297,7 +297,7 @@ discard block |
||
297 | 297 | * |
298 | 298 | * @return \WP_REST_Response |
299 | 299 | */ |
300 | - public function create_sub_item($request) |
|
300 | + public function create_sub_item( $request ) |
|
301 | 301 | { |
302 | 302 | return $this->not_implemented(); |
303 | 303 | } |
@@ -309,7 +309,7 @@ discard block |
||
309 | 309 | * |
310 | 310 | * @return \WP_REST_Response |
311 | 311 | */ |
312 | - public function update_sub_item($request) |
|
312 | + public function update_sub_item( $request ) |
|
313 | 313 | { |
314 | 314 | return $this->not_implemented(); |
315 | 315 | } |
@@ -321,7 +321,7 @@ discard block |
||
321 | 321 | * |
322 | 322 | * @return \WP_REST_Response |
323 | 323 | */ |
324 | - public function delete_sub_item($request) |
|
324 | + public function delete_sub_item( $request ) |
|
325 | 325 | { |
326 | 326 | return $this->not_implemented(); |
327 | 327 | } |
@@ -333,7 +333,7 @@ discard block |
||
333 | 333 | * |
334 | 334 | * @return \WP_REST_Response |
335 | 335 | */ |
336 | - public function get_items_permissions_check($request) |
|
336 | + public function get_items_permissions_check( $request ) |
|
337 | 337 | { |
338 | 338 | return $this->not_implemented(); |
339 | 339 | } |
@@ -345,7 +345,7 @@ discard block |
||
345 | 345 | * |
346 | 346 | * @return \WP_REST_Response |
347 | 347 | */ |
348 | - public function get_item_permissions_check($request) |
|
348 | + public function get_item_permissions_check( $request ) |
|
349 | 349 | { |
350 | 350 | return $this->not_implemented(); |
351 | 351 | } |
@@ -357,7 +357,7 @@ discard block |
||
357 | 357 | * |
358 | 358 | * @return \WP_REST_Response |
359 | 359 | */ |
360 | - public function create_item_permissions_check($request) |
|
360 | + public function create_item_permissions_check( $request ) |
|
361 | 361 | { |
362 | 362 | return $this->not_implemented(); |
363 | 363 | } |
@@ -369,7 +369,7 @@ discard block |
||
369 | 369 | * |
370 | 370 | * @return \WP_REST_Response |
371 | 371 | */ |
372 | - public function update_item_permissions_check($request) |
|
372 | + public function update_item_permissions_check( $request ) |
|
373 | 373 | { |
374 | 374 | return $this->not_implemented(); |
375 | 375 | } |
@@ -381,7 +381,7 @@ discard block |
||
381 | 381 | * |
382 | 382 | * @return \WP_REST_Response |
383 | 383 | */ |
384 | - public function delete_item_permissions_check($request) |
|
384 | + public function delete_item_permissions_check( $request ) |
|
385 | 385 | { |
386 | 386 | return $this->not_implemented(); |
387 | 387 | } |
@@ -395,7 +395,7 @@ discard block |
||
395 | 395 | * |
396 | 396 | * @return \WP_REST_Response |
397 | 397 | */ |
398 | - protected function prepare_item_for_database($request) |
|
398 | + protected function prepare_item_for_database( $request ) |
|
399 | 399 | { |
400 | 400 | return $this->not_implemented(); |
401 | 401 | } |
@@ -412,7 +412,7 @@ discard block |
||
412 | 412 | * |
413 | 413 | * @return \WP_REST_Response |
414 | 414 | */ |
415 | - public function prepare_item_for_response($item, $request) |
|
415 | + public function prepare_item_for_response( $item, $request ) |
|
416 | 416 | { |
417 | 417 | return $this->not_implemented(); |
418 | 418 | } |
@@ -426,9 +426,9 @@ discard block |
||
426 | 426 | */ |
427 | 427 | protected function not_implemented() |
428 | 428 | { |
429 | - $error = new \WP_Error('not-implemented-yet', __('Endpoint Not Yet Implemented.', 'gravityview')); |
|
429 | + $error = new \WP_Error( 'not-implemented-yet', __( 'Endpoint Not Yet Implemented.', 'gravityview' ) ); |
|
430 | 430 | |
431 | - return new \WP_REST_Response($error, 501); |
|
431 | + return new \WP_REST_Response( $error, 501 ); |
|
432 | 432 | } |
433 | 433 | |
434 | 434 | /** |
@@ -442,15 +442,15 @@ discard block |
||
442 | 442 | * |
443 | 443 | * @return array |
444 | 444 | */ |
445 | - public function __call($method, $args) |
|
445 | + public function __call( $method, $args ) |
|
446 | 446 | { |
447 | - if (in_array($method, [ |
|
447 | + if ( in_array( $method, [ |
|
448 | 448 | 'create_item_args', |
449 | 449 | 'update_item_args', |
450 | 450 | 'create_sub_item_args', |
451 | 451 | 'update_sub_item_args', |
452 | - ])) { |
|
453 | - return []; |
|
452 | + ] ) ) { |
|
453 | + return [ ]; |
|
454 | 454 | } |
455 | 455 | } |
456 | 456 | } |
@@ -40,8 +40,7 @@ discard block |
||
40 | 40 | /** |
41 | 41 | * Register the routes for the objects of the controller. |
42 | 42 | */ |
43 | - public function register_routes() |
|
44 | - { |
|
43 | + public function register_routes() { |
|
45 | 44 | $namespace = \GV\REST\Core::get_namespace(); |
46 | 45 | $base = $this->get_route_name(); |
47 | 46 | |
@@ -175,8 +174,7 @@ discard block |
||
175 | 174 | * |
176 | 175 | * @return string |
177 | 176 | */ |
178 | - protected function get_route_name() |
|
179 | - { |
|
177 | + protected function get_route_name() { |
|
180 | 178 | if (is_string($this->route_name)) { |
181 | 179 | return $this->route_name; |
182 | 180 | } else { |
@@ -195,8 +193,7 @@ discard block |
||
195 | 193 | * |
196 | 194 | * @return string |
197 | 195 | */ |
198 | - protected function get_sub_type() |
|
199 | - { |
|
196 | + protected function get_sub_type() { |
|
200 | 197 | if (is_string($this->sub_type)) { |
201 | 198 | return $this->sub_type; |
202 | 199 | } else { |
@@ -213,8 +210,7 @@ discard block |
||
213 | 210 | * |
214 | 211 | * @return \WP_Error|\WP_REST_Response |
215 | 212 | */ |
216 | - public function get_items($request) |
|
217 | - { |
|
213 | + public function get_items($request) { |
|
218 | 214 | return $this->not_implemented(); |
219 | 215 | } |
220 | 216 | |
@@ -225,8 +221,7 @@ discard block |
||
225 | 221 | * |
226 | 222 | * @return \WP_Error|\WP_REST_Response |
227 | 223 | */ |
228 | - public function get_item($request) |
|
229 | - { |
|
224 | + public function get_item($request) { |
|
230 | 225 | return $this->not_implemented(); |
231 | 226 | } |
232 | 227 | |
@@ -237,8 +232,7 @@ discard block |
||
237 | 232 | * |
238 | 233 | * @return \WP_REST_Response |
239 | 234 | */ |
240 | - public function create_item($request) |
|
241 | - { |
|
235 | + public function create_item($request) { |
|
242 | 236 | return $this->not_implemented(); |
243 | 237 | } |
244 | 238 | |
@@ -249,8 +243,7 @@ discard block |
||
249 | 243 | * |
250 | 244 | * @return \WP_REST_Response |
251 | 245 | */ |
252 | - public function update_item($request) |
|
253 | - { |
|
246 | + public function update_item($request) { |
|
254 | 247 | return $this->not_implemented(); |
255 | 248 | } |
256 | 249 | |
@@ -261,8 +254,7 @@ discard block |
||
261 | 254 | * |
262 | 255 | * @return \WP_REST_Response |
263 | 256 | */ |
264 | - public function delete_item($request) |
|
265 | - { |
|
257 | + public function delete_item($request) { |
|
266 | 258 | return $this->not_implemented(); |
267 | 259 | } |
268 | 260 | |
@@ -273,8 +265,7 @@ discard block |
||
273 | 265 | * |
274 | 266 | * @return \WP_Error|\WP_REST_Response |
275 | 267 | */ |
276 | - public function get_sub_items($request) |
|
277 | - { |
|
268 | + public function get_sub_items($request) { |
|
278 | 269 | return $this->not_implemented(); |
279 | 270 | } |
280 | 271 | |
@@ -285,8 +276,7 @@ discard block |
||
285 | 276 | * |
286 | 277 | * @return \WP_Error|\WP_REST_Response |
287 | 278 | */ |
288 | - public function get_sub_item($request) |
|
289 | - { |
|
279 | + public function get_sub_item($request) { |
|
290 | 280 | return $this->not_implemented(); |
291 | 281 | } |
292 | 282 | |
@@ -297,8 +287,7 @@ discard block |
||
297 | 287 | * |
298 | 288 | * @return \WP_REST_Response |
299 | 289 | */ |
300 | - public function create_sub_item($request) |
|
301 | - { |
|
290 | + public function create_sub_item($request) { |
|
302 | 291 | return $this->not_implemented(); |
303 | 292 | } |
304 | 293 | |
@@ -309,8 +298,7 @@ discard block |
||
309 | 298 | * |
310 | 299 | * @return \WP_REST_Response |
311 | 300 | */ |
312 | - public function update_sub_item($request) |
|
313 | - { |
|
301 | + public function update_sub_item($request) { |
|
314 | 302 | return $this->not_implemented(); |
315 | 303 | } |
316 | 304 | |
@@ -321,8 +309,7 @@ discard block |
||
321 | 309 | * |
322 | 310 | * @return \WP_REST_Response |
323 | 311 | */ |
324 | - public function delete_sub_item($request) |
|
325 | - { |
|
312 | + public function delete_sub_item($request) { |
|
326 | 313 | return $this->not_implemented(); |
327 | 314 | } |
328 | 315 | |
@@ -333,8 +320,7 @@ discard block |
||
333 | 320 | * |
334 | 321 | * @return \WP_REST_Response |
335 | 322 | */ |
336 | - public function get_items_permissions_check($request) |
|
337 | - { |
|
323 | + public function get_items_permissions_check($request) { |
|
338 | 324 | return $this->not_implemented(); |
339 | 325 | } |
340 | 326 | |
@@ -345,8 +331,7 @@ discard block |
||
345 | 331 | * |
346 | 332 | * @return \WP_REST_Response |
347 | 333 | */ |
348 | - public function get_item_permissions_check($request) |
|
349 | - { |
|
334 | + public function get_item_permissions_check($request) { |
|
350 | 335 | return $this->not_implemented(); |
351 | 336 | } |
352 | 337 | |
@@ -357,8 +342,7 @@ discard block |
||
357 | 342 | * |
358 | 343 | * @return \WP_REST_Response |
359 | 344 | */ |
360 | - public function create_item_permissions_check($request) |
|
361 | - { |
|
345 | + public function create_item_permissions_check($request) { |
|
362 | 346 | return $this->not_implemented(); |
363 | 347 | } |
364 | 348 | |
@@ -369,8 +353,7 @@ discard block |
||
369 | 353 | * |
370 | 354 | * @return \WP_REST_Response |
371 | 355 | */ |
372 | - public function update_item_permissions_check($request) |
|
373 | - { |
|
356 | + public function update_item_permissions_check($request) { |
|
374 | 357 | return $this->not_implemented(); |
375 | 358 | } |
376 | 359 | |
@@ -381,8 +364,7 @@ discard block |
||
381 | 364 | * |
382 | 365 | * @return \WP_REST_Response |
383 | 366 | */ |
384 | - public function delete_item_permissions_check($request) |
|
385 | - { |
|
367 | + public function delete_item_permissions_check($request) { |
|
386 | 368 | return $this->not_implemented(); |
387 | 369 | } |
388 | 370 | |
@@ -395,8 +377,7 @@ discard block |
||
395 | 377 | * |
396 | 378 | * @return \WP_REST_Response |
397 | 379 | */ |
398 | - protected function prepare_item_for_database($request) |
|
399 | - { |
|
380 | + protected function prepare_item_for_database($request) { |
|
400 | 381 | return $this->not_implemented(); |
401 | 382 | } |
402 | 383 | |
@@ -412,8 +393,7 @@ discard block |
||
412 | 393 | * |
413 | 394 | * @return \WP_REST_Response |
414 | 395 | */ |
415 | - public function prepare_item_for_response($item, $request) |
|
416 | - { |
|
396 | + public function prepare_item_for_response($item, $request) { |
|
417 | 397 | return $this->not_implemented(); |
418 | 398 | } |
419 | 399 | |
@@ -424,8 +404,7 @@ discard block |
||
424 | 404 | * |
425 | 405 | * @return \WP_REST_Response |
426 | 406 | */ |
427 | - protected function not_implemented() |
|
428 | - { |
|
407 | + protected function not_implemented() { |
|
429 | 408 | $error = new \WP_Error('not-implemented-yet', __('Endpoint Not Yet Implemented.', 'gravityview')); |
430 | 409 | |
431 | 410 | return new \WP_REST_Response($error, 501); |
@@ -442,8 +421,7 @@ discard block |
||
442 | 421 | * |
443 | 422 | * @return array |
444 | 423 | */ |
445 | - public function __call($method, $args) |
|
446 | - { |
|
424 | + public function __call($method, $args) { |
|
447 | 425 | if (in_array($method, [ |
448 | 426 | 'create_item_args', |
449 | 427 | 'update_item_args', |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | |
5 | 5 | /** If this file is called directly, abort. */ |
6 | 6 | if (!defined('GRAVITYVIEW_DIR')) { |
7 | - exit(); |
|
7 | + exit(); |
|
8 | 8 | } |
9 | 9 | |
10 | 10 | /** |
@@ -12,28 +12,28 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class Request extends \GV\Request |
14 | 14 | { |
15 | - private $request; |
|
15 | + private $request; |
|
16 | 16 | |
17 | - /** |
|
18 | - * @param \WP_REST_Request $request The WordPress REST request object. |
|
19 | - */ |
|
20 | - public function __construct(\WP_REST_Request $request) |
|
21 | - { |
|
22 | - $this->request = $request; |
|
23 | - } |
|
17 | + /** |
|
18 | + * @param \WP_REST_Request $request The WordPress REST request object. |
|
19 | + */ |
|
20 | + public function __construct(\WP_REST_Request $request) |
|
21 | + { |
|
22 | + $this->request = $request; |
|
23 | + } |
|
24 | 24 | |
25 | - /** |
|
26 | - * Retrieve paging parameters if any. |
|
27 | - * |
|
28 | - * @return array |
|
29 | - */ |
|
30 | - public function get_paging() |
|
31 | - { |
|
32 | - return [ |
|
33 | - 'paging' => [ |
|
34 | - 'page_size' => $this->request->get_param('limit'), |
|
35 | - 'current_page' => $this->request->get_param('page'), |
|
36 | - ], |
|
37 | - ]; |
|
38 | - } |
|
25 | + /** |
|
26 | + * Retrieve paging parameters if any. |
|
27 | + * |
|
28 | + * @return array |
|
29 | + */ |
|
30 | + public function get_paging() |
|
31 | + { |
|
32 | + return [ |
|
33 | + 'paging' => [ |
|
34 | + 'page_size' => $this->request->get_param('limit'), |
|
35 | + 'current_page' => $this->request->get_param('page'), |
|
36 | + ], |
|
37 | + ]; |
|
38 | + } |
|
39 | 39 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | namespace GV\REST; |
4 | 4 | |
5 | 5 | /** If this file is called directly, abort. */ |
6 | -if (!defined('GRAVITYVIEW_DIR')) { |
|
6 | +if ( ! defined( 'GRAVITYVIEW_DIR' ) ) { |
|
7 | 7 | exit(); |
8 | 8 | } |
9 | 9 | |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | /** |
18 | 18 | * @param \WP_REST_Request $request The WordPress REST request object. |
19 | 19 | */ |
20 | - public function __construct(\WP_REST_Request $request) |
|
20 | + public function __construct( \WP_REST_Request $request ) |
|
21 | 21 | { |
22 | 22 | $this->request = $request; |
23 | 23 | } |
@@ -31,8 +31,8 @@ discard block |
||
31 | 31 | { |
32 | 32 | return [ |
33 | 33 | 'paging' => [ |
34 | - 'page_size' => $this->request->get_param('limit'), |
|
35 | - 'current_page' => $this->request->get_param('page'), |
|
34 | + 'page_size' => $this->request->get_param( 'limit' ), |
|
35 | + 'current_page' => $this->request->get_param( 'page' ), |
|
36 | 36 | ], |
37 | 37 | ]; |
38 | 38 | } |
@@ -17,8 +17,7 @@ discard block |
||
17 | 17 | /** |
18 | 18 | * @param \WP_REST_Request $request The WordPress REST request object. |
19 | 19 | */ |
20 | - public function __construct(\WP_REST_Request $request) |
|
21 | - { |
|
20 | + public function __construct(\WP_REST_Request $request) { |
|
22 | 21 | $this->request = $request; |
23 | 22 | } |
24 | 23 | |
@@ -27,8 +26,7 @@ discard block |
||
27 | 26 | * |
28 | 27 | * @return array |
29 | 28 | */ |
30 | - public function get_paging() |
|
31 | - { |
|
29 | + public function get_paging() { |
|
32 | 30 | return [ |
33 | 31 | 'paging' => [ |
34 | 32 | 'page_size' => $this->request->get_param('limit'), |
@@ -4,51 +4,51 @@ |
||
4 | 4 | |
5 | 5 | /** If this file is called directly, abort. */ |
6 | 6 | if (!defined('GRAVITYVIEW_DIR')) { |
7 | - exit(); |
|
7 | + exit(); |
|
8 | 8 | } |
9 | 9 | |
10 | 10 | class Core |
11 | 11 | { |
12 | - public static $routes; |
|
13 | - |
|
14 | - /** |
|
15 | - * Initialization. |
|
16 | - */ |
|
17 | - public static function init() |
|
18 | - { |
|
19 | - if (!gravityview()->plugin->supports(\GV\Plugin::FEATURE_REST)) { |
|
20 | - return; |
|
21 | - } |
|
22 | - |
|
23 | - /** Load routes. */ |
|
24 | - require_once gravityview()->plugin->dir('future/includes/rest/class-gv-rest-route.php'); |
|
25 | - require_once gravityview()->plugin->dir('future/includes/rest/class-gv-rest-views-route.php'); |
|
26 | - |
|
27 | - self::$routes['views'] = $views = new Views_Route(); |
|
28 | - $views->register_routes(); |
|
29 | - } |
|
30 | - |
|
31 | - /** |
|
32 | - * Get namespace for GravityView REST API endpoints. |
|
33 | - * |
|
34 | - * @since 2.0 |
|
35 | - * |
|
36 | - * @return string |
|
37 | - */ |
|
38 | - public static function get_namespace() |
|
39 | - { |
|
40 | - return 'gravityview/v1'; |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * Get root URL for GravityView REST API. |
|
45 | - * |
|
46 | - * @since 2.0 |
|
47 | - * |
|
48 | - * @return string |
|
49 | - */ |
|
50 | - public static function get_url() |
|
51 | - { |
|
52 | - return rest_url(self::get_namespace()); |
|
53 | - } |
|
12 | + public static $routes; |
|
13 | + |
|
14 | + /** |
|
15 | + * Initialization. |
|
16 | + */ |
|
17 | + public static function init() |
|
18 | + { |
|
19 | + if (!gravityview()->plugin->supports(\GV\Plugin::FEATURE_REST)) { |
|
20 | + return; |
|
21 | + } |
|
22 | + |
|
23 | + /** Load routes. */ |
|
24 | + require_once gravityview()->plugin->dir('future/includes/rest/class-gv-rest-route.php'); |
|
25 | + require_once gravityview()->plugin->dir('future/includes/rest/class-gv-rest-views-route.php'); |
|
26 | + |
|
27 | + self::$routes['views'] = $views = new Views_Route(); |
|
28 | + $views->register_routes(); |
|
29 | + } |
|
30 | + |
|
31 | + /** |
|
32 | + * Get namespace for GravityView REST API endpoints. |
|
33 | + * |
|
34 | + * @since 2.0 |
|
35 | + * |
|
36 | + * @return string |
|
37 | + */ |
|
38 | + public static function get_namespace() |
|
39 | + { |
|
40 | + return 'gravityview/v1'; |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * Get root URL for GravityView REST API. |
|
45 | + * |
|
46 | + * @since 2.0 |
|
47 | + * |
|
48 | + * @return string |
|
49 | + */ |
|
50 | + public static function get_url() |
|
51 | + { |
|
52 | + return rest_url(self::get_namespace()); |
|
53 | + } |
|
54 | 54 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | namespace GV\REST; |
4 | 4 | |
5 | 5 | /** If this file is called directly, abort. */ |
6 | -if (!defined('GRAVITYVIEW_DIR')) { |
|
6 | +if ( ! defined( 'GRAVITYVIEW_DIR' ) ) { |
|
7 | 7 | exit(); |
8 | 8 | } |
9 | 9 | |
@@ -16,15 +16,15 @@ discard block |
||
16 | 16 | */ |
17 | 17 | public static function init() |
18 | 18 | { |
19 | - if (!gravityview()->plugin->supports(\GV\Plugin::FEATURE_REST)) { |
|
19 | + if ( ! gravityview()->plugin->supports( \GV\Plugin::FEATURE_REST ) ) { |
|
20 | 20 | return; |
21 | 21 | } |
22 | 22 | |
23 | 23 | /** Load routes. */ |
24 | - require_once gravityview()->plugin->dir('future/includes/rest/class-gv-rest-route.php'); |
|
25 | - require_once gravityview()->plugin->dir('future/includes/rest/class-gv-rest-views-route.php'); |
|
24 | + require_once gravityview()->plugin->dir( 'future/includes/rest/class-gv-rest-route.php' ); |
|
25 | + require_once gravityview()->plugin->dir( 'future/includes/rest/class-gv-rest-views-route.php' ); |
|
26 | 26 | |
27 | - self::$routes['views'] = $views = new Views_Route(); |
|
27 | + self::$routes[ 'views' ] = $views = new Views_Route(); |
|
28 | 28 | $views->register_routes(); |
29 | 29 | } |
30 | 30 | |
@@ -49,6 +49,6 @@ discard block |
||
49 | 49 | */ |
50 | 50 | public static function get_url() |
51 | 51 | { |
52 | - return rest_url(self::get_namespace()); |
|
52 | + return rest_url( self::get_namespace() ); |
|
53 | 53 | } |
54 | 54 | } |
@@ -7,15 +7,13 @@ discard block |
||
7 | 7 | exit(); |
8 | 8 | } |
9 | 9 | |
10 | -class Core |
|
11 | -{ |
|
10 | +class Core { |
|
12 | 11 | public static $routes; |
13 | 12 | |
14 | 13 | /** |
15 | 14 | * Initialization. |
16 | 15 | */ |
17 | - public static function init() |
|
18 | - { |
|
16 | + public static function init() { |
|
19 | 17 | if (!gravityview()->plugin->supports(\GV\Plugin::FEATURE_REST)) { |
20 | 18 | return; |
21 | 19 | } |
@@ -35,8 +33,7 @@ discard block |
||
35 | 33 | * |
36 | 34 | * @return string |
37 | 35 | */ |
38 | - public static function get_namespace() |
|
39 | - { |
|
36 | + public static function get_namespace() { |
|
40 | 37 | return 'gravityview/v1'; |
41 | 38 | } |
42 | 39 | |
@@ -47,8 +44,7 @@ discard block |
||
47 | 44 | * |
48 | 45 | * @return string |
49 | 46 | */ |
50 | - public static function get_url() |
|
51 | - { |
|
47 | + public static function get_url() { |
|
52 | 48 | return rest_url(self::get_namespace()); |
53 | 49 | } |
54 | 50 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | |
5 | 5 | /** If this file is called directly, abort. */ |
6 | 6 | if (!defined('GRAVITYVIEW_DIR')) { |
7 | - exit(); |
|
7 | + exit(); |
|
8 | 8 | } |
9 | 9 | |
10 | 10 | /** |
@@ -14,146 +14,146 @@ discard block |
||
14 | 14 | */ |
15 | 15 | class View_Renderer extends Renderer |
16 | 16 | { |
17 | - /** |
|
18 | - * Renders a \GV\View instance. |
|
19 | - * |
|
20 | - * @param \GV\View $view The View instance to render. |
|
21 | - * @param \GV\Request $request The request context we're currently in. Default: `gravityview()->request` |
|
22 | - * |
|
23 | - * @api |
|
24 | - * |
|
25 | - * @since 2.0 |
|
26 | - * |
|
27 | - * @return string The rendered View. |
|
28 | - */ |
|
29 | - public function render(View $view, Request $request = null) |
|
30 | - { |
|
31 | - if (is_null($request)) { |
|
32 | - $request = &gravityview()->request; |
|
33 | - } |
|
34 | - |
|
35 | - if (!$request->is_renderable()) { |
|
36 | - gravityview()->log->error('Renderer unable to render View in {request_class} context', ['request_class' => get_class($request)]); |
|
37 | - |
|
38 | - return null; |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * @filter `gravityview_template_slug_{$template_id}` Modify the template slug about to be loaded in directory views. |
|
43 | - * |
|
44 | - * @since 1.6 |
|
45 | - * @deprecated |
|
46 | - * @see The `gravityview_get_template_id` filter |
|
47 | - * |
|
48 | - * @param string $slug Default: 'table' |
|
49 | - * @param string $view The current view context: directory. |
|
50 | - */ |
|
51 | - $template_slug = apply_filters('gravityview_template_slug_'.$view->settings->get('template'), 'table', 'directory'); |
|
52 | - |
|
53 | - /** |
|
54 | - * Figure out whether to get the entries or not. |
|
55 | - * |
|
56 | - * Some contexts don't need initial entries, like the DataTables directory type. |
|
57 | - * |
|
58 | - * @filter `gravityview_get_view_entries_{$template_slug}` Whether to get the entries or not. |
|
59 | - * |
|
60 | - * @param bool $get_entries Get entries or not, default: true. |
|
61 | - */ |
|
62 | - $get_entries = apply_filters('gravityview_get_view_entries_'.$template_slug, true); |
|
63 | - |
|
64 | - $hide_until_searched = $view->settings->get('hide_until_searched'); |
|
65 | - |
|
66 | - /** |
|
67 | - * Hide View data until search is performed. |
|
68 | - */ |
|
69 | - $get_entries = ($hide_until_searched && !$request->is_search()) ? false : $get_entries; |
|
70 | - |
|
71 | - /** |
|
72 | - * Fetch entries for this View. |
|
73 | - */ |
|
74 | - if ($get_entries) { |
|
75 | - $entries = $view->get_entries($request); |
|
76 | - } else { |
|
77 | - $entries = new \GV\Entry_Collection(); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Load a legacy override template if exists. |
|
82 | - */ |
|
83 | - $override = new \GV\Legacy_Override_Template($view, null, null, $request); |
|
84 | - foreach (['header', 'body', 'footer'] as $part) { |
|
85 | - if (($path = $override->get_template_part($template_slug, $part)) && strpos($path, '/deprecated') === false) { |
|
86 | - /** |
|
87 | - * We have to bail and call the legacy renderer. Crap! |
|
88 | - */ |
|
89 | - gravityview()->log->notice('Legacy templates detected in theme {path}', ['path' => $path]); |
|
90 | - |
|
91 | - /** |
|
92 | - * Show a warning at the top, if View is editable by the user. |
|
93 | - */ |
|
94 | - add_action('gravityview_before', $this->legacy_template_warning($view, $path)); |
|
95 | - |
|
96 | - return $override->render($template_slug); |
|
97 | - } |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * @filter `gravityview/template/view/class` Filter the template class that is about to be used to render the view. |
|
102 | - * |
|
103 | - * @since 2.0 |
|
104 | - * |
|
105 | - * @param string $class The chosen class - Default: \GV\View_Table_Template. |
|
106 | - * @param \GV\View $view The view about to be rendered. |
|
107 | - * @param \GV\Request $request The associated request. |
|
108 | - */ |
|
109 | - $class = apply_filters('gravityview/template/view/class', sprintf('\GV\View_%s_Template', ucfirst($template_slug)), $view, $request); |
|
110 | - if (!$class || !class_exists($class)) { |
|
111 | - gravityview()->log->notice('{template_class} not found, falling back to legacy', ['template_class' => $class]); |
|
112 | - $class = '\GV\View_Legacy_Template'; |
|
113 | - } |
|
114 | - $template = new $class($view, $entries, $request); |
|
115 | - |
|
116 | - /** |
|
117 | - * Remove multiple sorting before calling legacy filters. |
|
118 | - * This allows us to fake it till we make it. |
|
119 | - */ |
|
120 | - $parameters = $view->settings->as_atts(); |
|
121 | - if (!empty($parameters['sort_field']) && is_array($parameters['sort_field'])) { |
|
122 | - $has_multisort = true; |
|
123 | - $parameters['sort_field'] = reset($parameters['sort_field']); |
|
124 | - if (!empty($parameters['sort_direction']) && is_array($parameters['sort_direction'])) { |
|
125 | - $parameters['sort_direction'] = reset($parameters['sort_direction']); |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - /** @todo Deprecate this! */ |
|
130 | - $parameters = \GravityView_frontend::get_view_entries_parameters($parameters, $view->form->ID); |
|
131 | - |
|
132 | - global $post; |
|
133 | - |
|
134 | - /** Mock the legacy state for the widgets and whatnot */ |
|
135 | - \GV\Mocks\Legacy_Context::push(array_merge([ |
|
136 | - 'view' => $view, |
|
137 | - 'entries' => $entries, |
|
138 | - 'request' => $request, |
|
139 | - ], empty($parameters) ? [] : [ |
|
140 | - 'paging' => $parameters['paging'], |
|
141 | - 'sorting' => $parameters['sorting'], |
|
142 | - ], empty($post) ? [] : [ |
|
143 | - 'post' => $post, |
|
144 | - ])); |
|
145 | - |
|
146 | - add_action('gravityview/template/after', $view_id_output = function ($context) { |
|
147 | - printf('<input type="hidden" class="gravityview-view-id" value="%d">', $context->view->ID); |
|
148 | - }); |
|
149 | - |
|
150 | - ob_start(); |
|
151 | - $template->render(); |
|
152 | - |
|
153 | - remove_action('gravityview/template/after', $view_id_output); |
|
154 | - |
|
155 | - \GV\Mocks\Legacy_Context::pop(); |
|
156 | - |
|
157 | - return ob_get_clean(); |
|
158 | - } |
|
17 | + /** |
|
18 | + * Renders a \GV\View instance. |
|
19 | + * |
|
20 | + * @param \GV\View $view The View instance to render. |
|
21 | + * @param \GV\Request $request The request context we're currently in. Default: `gravityview()->request` |
|
22 | + * |
|
23 | + * @api |
|
24 | + * |
|
25 | + * @since 2.0 |
|
26 | + * |
|
27 | + * @return string The rendered View. |
|
28 | + */ |
|
29 | + public function render(View $view, Request $request = null) |
|
30 | + { |
|
31 | + if (is_null($request)) { |
|
32 | + $request = &gravityview()->request; |
|
33 | + } |
|
34 | + |
|
35 | + if (!$request->is_renderable()) { |
|
36 | + gravityview()->log->error('Renderer unable to render View in {request_class} context', ['request_class' => get_class($request)]); |
|
37 | + |
|
38 | + return null; |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * @filter `gravityview_template_slug_{$template_id}` Modify the template slug about to be loaded in directory views. |
|
43 | + * |
|
44 | + * @since 1.6 |
|
45 | + * @deprecated |
|
46 | + * @see The `gravityview_get_template_id` filter |
|
47 | + * |
|
48 | + * @param string $slug Default: 'table' |
|
49 | + * @param string $view The current view context: directory. |
|
50 | + */ |
|
51 | + $template_slug = apply_filters('gravityview_template_slug_'.$view->settings->get('template'), 'table', 'directory'); |
|
52 | + |
|
53 | + /** |
|
54 | + * Figure out whether to get the entries or not. |
|
55 | + * |
|
56 | + * Some contexts don't need initial entries, like the DataTables directory type. |
|
57 | + * |
|
58 | + * @filter `gravityview_get_view_entries_{$template_slug}` Whether to get the entries or not. |
|
59 | + * |
|
60 | + * @param bool $get_entries Get entries or not, default: true. |
|
61 | + */ |
|
62 | + $get_entries = apply_filters('gravityview_get_view_entries_'.$template_slug, true); |
|
63 | + |
|
64 | + $hide_until_searched = $view->settings->get('hide_until_searched'); |
|
65 | + |
|
66 | + /** |
|
67 | + * Hide View data until search is performed. |
|
68 | + */ |
|
69 | + $get_entries = ($hide_until_searched && !$request->is_search()) ? false : $get_entries; |
|
70 | + |
|
71 | + /** |
|
72 | + * Fetch entries for this View. |
|
73 | + */ |
|
74 | + if ($get_entries) { |
|
75 | + $entries = $view->get_entries($request); |
|
76 | + } else { |
|
77 | + $entries = new \GV\Entry_Collection(); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Load a legacy override template if exists. |
|
82 | + */ |
|
83 | + $override = new \GV\Legacy_Override_Template($view, null, null, $request); |
|
84 | + foreach (['header', 'body', 'footer'] as $part) { |
|
85 | + if (($path = $override->get_template_part($template_slug, $part)) && strpos($path, '/deprecated') === false) { |
|
86 | + /** |
|
87 | + * We have to bail and call the legacy renderer. Crap! |
|
88 | + */ |
|
89 | + gravityview()->log->notice('Legacy templates detected in theme {path}', ['path' => $path]); |
|
90 | + |
|
91 | + /** |
|
92 | + * Show a warning at the top, if View is editable by the user. |
|
93 | + */ |
|
94 | + add_action('gravityview_before', $this->legacy_template_warning($view, $path)); |
|
95 | + |
|
96 | + return $override->render($template_slug); |
|
97 | + } |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * @filter `gravityview/template/view/class` Filter the template class that is about to be used to render the view. |
|
102 | + * |
|
103 | + * @since 2.0 |
|
104 | + * |
|
105 | + * @param string $class The chosen class - Default: \GV\View_Table_Template. |
|
106 | + * @param \GV\View $view The view about to be rendered. |
|
107 | + * @param \GV\Request $request The associated request. |
|
108 | + */ |
|
109 | + $class = apply_filters('gravityview/template/view/class', sprintf('\GV\View_%s_Template', ucfirst($template_slug)), $view, $request); |
|
110 | + if (!$class || !class_exists($class)) { |
|
111 | + gravityview()->log->notice('{template_class} not found, falling back to legacy', ['template_class' => $class]); |
|
112 | + $class = '\GV\View_Legacy_Template'; |
|
113 | + } |
|
114 | + $template = new $class($view, $entries, $request); |
|
115 | + |
|
116 | + /** |
|
117 | + * Remove multiple sorting before calling legacy filters. |
|
118 | + * This allows us to fake it till we make it. |
|
119 | + */ |
|
120 | + $parameters = $view->settings->as_atts(); |
|
121 | + if (!empty($parameters['sort_field']) && is_array($parameters['sort_field'])) { |
|
122 | + $has_multisort = true; |
|
123 | + $parameters['sort_field'] = reset($parameters['sort_field']); |
|
124 | + if (!empty($parameters['sort_direction']) && is_array($parameters['sort_direction'])) { |
|
125 | + $parameters['sort_direction'] = reset($parameters['sort_direction']); |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + /** @todo Deprecate this! */ |
|
130 | + $parameters = \GravityView_frontend::get_view_entries_parameters($parameters, $view->form->ID); |
|
131 | + |
|
132 | + global $post; |
|
133 | + |
|
134 | + /** Mock the legacy state for the widgets and whatnot */ |
|
135 | + \GV\Mocks\Legacy_Context::push(array_merge([ |
|
136 | + 'view' => $view, |
|
137 | + 'entries' => $entries, |
|
138 | + 'request' => $request, |
|
139 | + ], empty($parameters) ? [] : [ |
|
140 | + 'paging' => $parameters['paging'], |
|
141 | + 'sorting' => $parameters['sorting'], |
|
142 | + ], empty($post) ? [] : [ |
|
143 | + 'post' => $post, |
|
144 | + ])); |
|
145 | + |
|
146 | + add_action('gravityview/template/after', $view_id_output = function ($context) { |
|
147 | + printf('<input type="hidden" class="gravityview-view-id" value="%d">', $context->view->ID); |
|
148 | + }); |
|
149 | + |
|
150 | + ob_start(); |
|
151 | + $template->render(); |
|
152 | + |
|
153 | + remove_action('gravityview/template/after', $view_id_output); |
|
154 | + |
|
155 | + \GV\Mocks\Legacy_Context::pop(); |
|
156 | + |
|
157 | + return ob_get_clean(); |
|
158 | + } |
|
159 | 159 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | namespace GV; |
4 | 4 | |
5 | 5 | /** If this file is called directly, abort. */ |
6 | -if (!defined('GRAVITYVIEW_DIR')) { |
|
6 | +if ( ! defined( 'GRAVITYVIEW_DIR' ) ) { |
|
7 | 7 | exit(); |
8 | 8 | } |
9 | 9 | |
@@ -26,14 +26,14 @@ discard block |
||
26 | 26 | * |
27 | 27 | * @return string The rendered View. |
28 | 28 | */ |
29 | - public function render(View $view, Request $request = null) |
|
29 | + public function render( View $view, Request $request = null ) |
|
30 | 30 | { |
31 | - if (is_null($request)) { |
|
31 | + if ( is_null( $request ) ) { |
|
32 | 32 | $request = &gravityview()->request; |
33 | 33 | } |
34 | 34 | |
35 | - if (!$request->is_renderable()) { |
|
36 | - gravityview()->log->error('Renderer unable to render View in {request_class} context', ['request_class' => get_class($request)]); |
|
35 | + if ( ! $request->is_renderable() ) { |
|
36 | + gravityview()->log->error( 'Renderer unable to render View in {request_class} context', [ 'request_class' => get_class( $request ) ] ); |
|
37 | 37 | |
38 | 38 | return null; |
39 | 39 | } |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | * @param string $slug Default: 'table' |
49 | 49 | * @param string $view The current view context: directory. |
50 | 50 | */ |
51 | - $template_slug = apply_filters('gravityview_template_slug_'.$view->settings->get('template'), 'table', 'directory'); |
|
51 | + $template_slug = apply_filters( 'gravityview_template_slug_' . $view->settings->get( 'template' ), 'table', 'directory' ); |
|
52 | 52 | |
53 | 53 | /** |
54 | 54 | * Figure out whether to get the entries or not. |
@@ -59,20 +59,20 @@ discard block |
||
59 | 59 | * |
60 | 60 | * @param bool $get_entries Get entries or not, default: true. |
61 | 61 | */ |
62 | - $get_entries = apply_filters('gravityview_get_view_entries_'.$template_slug, true); |
|
62 | + $get_entries = apply_filters( 'gravityview_get_view_entries_' . $template_slug, true ); |
|
63 | 63 | |
64 | - $hide_until_searched = $view->settings->get('hide_until_searched'); |
|
64 | + $hide_until_searched = $view->settings->get( 'hide_until_searched' ); |
|
65 | 65 | |
66 | 66 | /** |
67 | 67 | * Hide View data until search is performed. |
68 | 68 | */ |
69 | - $get_entries = ($hide_until_searched && !$request->is_search()) ? false : $get_entries; |
|
69 | + $get_entries = ( $hide_until_searched && ! $request->is_search() ) ? false : $get_entries; |
|
70 | 70 | |
71 | 71 | /** |
72 | 72 | * Fetch entries for this View. |
73 | 73 | */ |
74 | - if ($get_entries) { |
|
75 | - $entries = $view->get_entries($request); |
|
74 | + if ( $get_entries ) { |
|
75 | + $entries = $view->get_entries( $request ); |
|
76 | 76 | } else { |
77 | 77 | $entries = new \GV\Entry_Collection(); |
78 | 78 | } |
@@ -80,20 +80,20 @@ discard block |
||
80 | 80 | /** |
81 | 81 | * Load a legacy override template if exists. |
82 | 82 | */ |
83 | - $override = new \GV\Legacy_Override_Template($view, null, null, $request); |
|
84 | - foreach (['header', 'body', 'footer'] as $part) { |
|
85 | - if (($path = $override->get_template_part($template_slug, $part)) && strpos($path, '/deprecated') === false) { |
|
83 | + $override = new \GV\Legacy_Override_Template( $view, null, null, $request ); |
|
84 | + foreach ( [ 'header', 'body', 'footer' ] as $part ) { |
|
85 | + if ( ( $path = $override->get_template_part( $template_slug, $part ) ) && strpos( $path, '/deprecated' ) === false ) { |
|
86 | 86 | /** |
87 | 87 | * We have to bail and call the legacy renderer. Crap! |
88 | 88 | */ |
89 | - gravityview()->log->notice('Legacy templates detected in theme {path}', ['path' => $path]); |
|
89 | + gravityview()->log->notice( 'Legacy templates detected in theme {path}', [ 'path' => $path ] ); |
|
90 | 90 | |
91 | 91 | /** |
92 | 92 | * Show a warning at the top, if View is editable by the user. |
93 | 93 | */ |
94 | - add_action('gravityview_before', $this->legacy_template_warning($view, $path)); |
|
94 | + add_action( 'gravityview_before', $this->legacy_template_warning( $view, $path ) ); |
|
95 | 95 | |
96 | - return $override->render($template_slug); |
|
96 | + return $override->render( $template_slug ); |
|
97 | 97 | } |
98 | 98 | } |
99 | 99 | |
@@ -106,51 +106,51 @@ discard block |
||
106 | 106 | * @param \GV\View $view The view about to be rendered. |
107 | 107 | * @param \GV\Request $request The associated request. |
108 | 108 | */ |
109 | - $class = apply_filters('gravityview/template/view/class', sprintf('\GV\View_%s_Template', ucfirst($template_slug)), $view, $request); |
|
110 | - if (!$class || !class_exists($class)) { |
|
111 | - gravityview()->log->notice('{template_class} not found, falling back to legacy', ['template_class' => $class]); |
|
109 | + $class = apply_filters( 'gravityview/template/view/class', sprintf( '\GV\View_%s_Template', ucfirst( $template_slug ) ), $view, $request ); |
|
110 | + if ( ! $class || ! class_exists( $class ) ) { |
|
111 | + gravityview()->log->notice( '{template_class} not found, falling back to legacy', [ 'template_class' => $class ] ); |
|
112 | 112 | $class = '\GV\View_Legacy_Template'; |
113 | 113 | } |
114 | - $template = new $class($view, $entries, $request); |
|
114 | + $template = new $class( $view, $entries, $request ); |
|
115 | 115 | |
116 | 116 | /** |
117 | 117 | * Remove multiple sorting before calling legacy filters. |
118 | 118 | * This allows us to fake it till we make it. |
119 | 119 | */ |
120 | 120 | $parameters = $view->settings->as_atts(); |
121 | - if (!empty($parameters['sort_field']) && is_array($parameters['sort_field'])) { |
|
121 | + if ( ! empty( $parameters[ 'sort_field' ] ) && is_array( $parameters[ 'sort_field' ] ) ) { |
|
122 | 122 | $has_multisort = true; |
123 | - $parameters['sort_field'] = reset($parameters['sort_field']); |
|
124 | - if (!empty($parameters['sort_direction']) && is_array($parameters['sort_direction'])) { |
|
125 | - $parameters['sort_direction'] = reset($parameters['sort_direction']); |
|
123 | + $parameters[ 'sort_field' ] = reset( $parameters[ 'sort_field' ] ); |
|
124 | + if ( ! empty( $parameters[ 'sort_direction' ] ) && is_array( $parameters[ 'sort_direction' ] ) ) { |
|
125 | + $parameters[ 'sort_direction' ] = reset( $parameters[ 'sort_direction' ] ); |
|
126 | 126 | } |
127 | 127 | } |
128 | 128 | |
129 | 129 | /** @todo Deprecate this! */ |
130 | - $parameters = \GravityView_frontend::get_view_entries_parameters($parameters, $view->form->ID); |
|
130 | + $parameters = \GravityView_frontend::get_view_entries_parameters( $parameters, $view->form->ID ); |
|
131 | 131 | |
132 | 132 | global $post; |
133 | 133 | |
134 | 134 | /** Mock the legacy state for the widgets and whatnot */ |
135 | - \GV\Mocks\Legacy_Context::push(array_merge([ |
|
135 | + \GV\Mocks\Legacy_Context::push( array_merge( [ |
|
136 | 136 | 'view' => $view, |
137 | 137 | 'entries' => $entries, |
138 | 138 | 'request' => $request, |
139 | - ], empty($parameters) ? [] : [ |
|
140 | - 'paging' => $parameters['paging'], |
|
141 | - 'sorting' => $parameters['sorting'], |
|
142 | - ], empty($post) ? [] : [ |
|
139 | + ], empty( $parameters ) ? [ ] : [ |
|
140 | + 'paging' => $parameters[ 'paging' ], |
|
141 | + 'sorting' => $parameters[ 'sorting' ], |
|
142 | + ], empty( $post ) ? [ ] : [ |
|
143 | 143 | 'post' => $post, |
144 | - ])); |
|
144 | + ] ) ); |
|
145 | 145 | |
146 | - add_action('gravityview/template/after', $view_id_output = function ($context) { |
|
147 | - printf('<input type="hidden" class="gravityview-view-id" value="%d">', $context->view->ID); |
|
146 | + add_action( 'gravityview/template/after', $view_id_output = function( $context ) { |
|
147 | + printf( '<input type="hidden" class="gravityview-view-id" value="%d">', $context->view->ID ); |
|
148 | 148 | }); |
149 | 149 | |
150 | 150 | ob_start(); |
151 | 151 | $template->render(); |
152 | 152 | |
153 | - remove_action('gravityview/template/after', $view_id_output); |
|
153 | + remove_action( 'gravityview/template/after', $view_id_output ); |
|
154 | 154 | |
155 | 155 | \GV\Mocks\Legacy_Context::pop(); |
156 | 156 |
@@ -12,8 +12,7 @@ discard block |
||
12 | 12 | * |
13 | 13 | * Houses some preliminary \GV\View rendering functionality. |
14 | 14 | */ |
15 | -class View_Renderer extends Renderer |
|
16 | -{ |
|
15 | +class View_Renderer extends Renderer { |
|
17 | 16 | /** |
18 | 17 | * Renders a \GV\View instance. |
19 | 18 | * |
@@ -26,8 +25,7 @@ discard block |
||
26 | 25 | * |
27 | 26 | * @return string The rendered View. |
28 | 27 | */ |
29 | - public function render(View $view, Request $request = null) |
|
30 | - { |
|
28 | + public function render(View $view, Request $request = null) { |
|
31 | 29 | if (is_null($request)) { |
32 | 30 | $request = &gravityview()->request; |
33 | 31 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | |
5 | 5 | /** If this file is called directly, abort. */ |
6 | 6 | if (!defined('GRAVITYVIEW_DIR')) { |
7 | - exit(); |
|
7 | + exit(); |
|
8 | 8 | } |
9 | 9 | |
10 | 10 | /** |
@@ -14,568 +14,568 @@ discard block |
||
14 | 14 | */ |
15 | 15 | abstract class Widget |
16 | 16 | { |
17 | - /** |
|
18 | - * Widget admin label. |
|
19 | - * |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - protected $widget_label = ''; |
|
23 | - |
|
24 | - /** |
|
25 | - * Widget description, shown on the "+ Add Widget" picker. |
|
26 | - * |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - protected $widget_description = ''; |
|
30 | - |
|
31 | - /** |
|
32 | - * Widget details, shown in the widget modal. |
|
33 | - * |
|
34 | - * @since 1.8 |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - protected $widget_subtitle = ''; |
|
39 | - |
|
40 | - /** |
|
41 | - * Widget admin ID. |
|
42 | - * |
|
43 | - * @var string |
|
44 | - */ |
|
45 | - protected $widget_id = ''; |
|
46 | - |
|
47 | - /** |
|
48 | - * Default configuration for header and footer. |
|
49 | - * |
|
50 | - * @var array |
|
51 | - */ |
|
52 | - protected $defaults = []; |
|
53 | - |
|
54 | - /** |
|
55 | - * Widget admin advanced settings. |
|
56 | - * |
|
57 | - * @var array |
|
58 | - */ |
|
59 | - protected $settings = []; |
|
60 | - |
|
61 | - /** |
|
62 | - * Allow class to automatically add widget_text filter for you in shortcode. |
|
63 | - * |
|
64 | - * @var string |
|
65 | - */ |
|
66 | - protected $shortcode_name; |
|
67 | - |
|
68 | - /** |
|
69 | - * Hold the widget options. |
|
70 | - * |
|
71 | - * @var array() |
|
72 | - */ |
|
73 | - private $widget_options = []; |
|
74 | - |
|
75 | - /** |
|
76 | - * The position of the widget. |
|
77 | - * |
|
78 | - * @api |
|
79 | - * |
|
80 | - * @since 2.0 |
|
81 | - * |
|
82 | - * @var string |
|
83 | - */ |
|
84 | - public $position = ''; |
|
85 | - |
|
86 | - /** |
|
87 | - * A unique ID for this widget. |
|
88 | - * |
|
89 | - * @api |
|
90 | - * |
|
91 | - * @since 2.0 |
|
92 | - * |
|
93 | - * @var string |
|
94 | - */ |
|
95 | - public $UID = ''; |
|
96 | - |
|
97 | - /** |
|
98 | - * The actual configuration for this widget instance. |
|
99 | - * |
|
100 | - * @api |
|
101 | - * |
|
102 | - * @since 2.0 |
|
103 | - * |
|
104 | - * @var \GV\Settings |
|
105 | - */ |
|
106 | - public $configuration; |
|
107 | - |
|
108 | - /** |
|
109 | - * @var string An icon that represents the widget type in the widget picker. |
|
110 | - * |
|
111 | - * Supports these icon formats: |
|
112 | - * - Gravity Forms icon class: The string starts with "gform-icon". Note: the site must be running GF 2.5+. No need to also pass "gform-icon". |
|
113 | - * - Dashicons: The string starts with "dashicons". No need to also pass "dashicons". |
|
114 | - * - Inline SVG: Starts with "data:" |
|
115 | - * - If not matching those formats, the value will be used as a CSS class in a `<i>` element. |
|
116 | - * |
|
117 | - * @see GravityView_Admin_View_Item::getOutput |
|
118 | - */ |
|
119 | - public $icon; |
|
120 | - |
|
121 | - /** |
|
122 | - * Constructor. |
|
123 | - * |
|
124 | - * @param string $label The Widget label as shown in the admin. |
|
125 | - * @param string $id The Widget ID, make this something unique. |
|
126 | - * @param array $defaults Default footer/header Widget configuration. |
|
127 | - * @param array $settings Advanced Widget settings. |
|
128 | - * |
|
129 | - * @return \GV\Widget |
|
130 | - */ |
|
131 | - public function __construct($label, $id, $defaults = [], $settings = []) |
|
132 | - { |
|
133 | - /** |
|
134 | - * The shortcode name is set to the lowercase name of the widget class, unless overridden by the class specifying a different value for $shortcode_name. |
|
135 | - * |
|
136 | - * @var string |
|
137 | - */ |
|
138 | - $this->shortcode_name = empty($this->shortcode_name) ? strtolower(get_called_class()) : $this->shortcode_name; |
|
139 | - |
|
140 | - if ($id) { |
|
141 | - $this->widget_id = $id; |
|
142 | - } |
|
143 | - |
|
144 | - $this->widget_label = $label; |
|
145 | - $this->defaults = array_merge(['header' => 0, 'footer' => 0], $defaults); |
|
146 | - |
|
147 | - // Make sure every widget has a title, even if empty |
|
148 | - $this->settings = wp_parse_args($settings, $this->get_default_settings()); |
|
149 | - |
|
150 | - // Hook once per unique ID |
|
151 | - if ($this->is_registered()) { |
|
152 | - return; |
|
153 | - } |
|
154 | - |
|
155 | - // widget options |
|
156 | - add_filter('gravityview_template_widget_options', [$this, 'assign_widget_options'], 10, 3); |
|
157 | - |
|
158 | - // frontend logic |
|
159 | - add_action(sprintf('gravityview/widgets/%s/render', $this->get_widget_id()), [$this, 'render_frontend'], 10, 3); |
|
160 | - |
|
161 | - // register shortcodes |
|
162 | - add_action('wp', [$this, 'add_shortcode']); |
|
163 | - |
|
164 | - // Use shortcodes in text widgets. |
|
165 | - add_filter('widget_text', [$this, 'maybe_do_shortcode']); |
|
166 | - |
|
167 | - // register widgets to be listed in the View Configuration |
|
168 | - // Important: this has to be the last filter/action added in the constructor. |
|
169 | - add_filter('gravityview/widgets/register', [$this, 'register_widget']); |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * Define general widget settings. |
|
174 | - * |
|
175 | - * @since 1.5.4 |
|
176 | - * |
|
177 | - * @return array $settings Default settings |
|
178 | - */ |
|
179 | - protected function get_default_settings() |
|
180 | - { |
|
181 | - $settings = []; |
|
182 | - |
|
183 | - /** |
|
184 | - * @filter `gravityview/widget/enable_custom_class` Enable custom CSS class settings for widgets |
|
185 | - * |
|
186 | - * @param bool $enable_custom_class False by default. Return true if you want to enable. |
|
187 | - * @param \GV\Widget $this Current instance of \GV\Widget. |
|
188 | - */ |
|
189 | - $enable_custom_class = apply_filters('gravityview/widget/enable_custom_class', false, $this); |
|
190 | - |
|
191 | - if ($enable_custom_class) { |
|
192 | - $settings['custom_class'] = [ |
|
193 | - 'type' => 'text', |
|
194 | - 'label' => __('Custom CSS Class:', 'gravityview'), |
|
195 | - 'desc' => __('This class will be added to the widget container', 'gravityview'), |
|
196 | - 'value' => '', |
|
197 | - 'merge_tags' => true, |
|
198 | - 'class' => 'widefat code', |
|
199 | - ]; |
|
200 | - } |
|
201 | - |
|
202 | - return $settings; |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * Get the Widget ID. |
|
207 | - * |
|
208 | - * @return string The Widget ID. |
|
209 | - */ |
|
210 | - public function get_widget_id() |
|
211 | - { |
|
212 | - return $this->widget_id; |
|
213 | - } |
|
214 | - |
|
215 | - /** |
|
216 | - * Get the widget settings. |
|
217 | - * |
|
218 | - * @return array|null Settings array; NULL if not set for some reason. |
|
219 | - */ |
|
220 | - public function get_settings() |
|
221 | - { |
|
222 | - return empty($this->settings) ? null : $this->settings; |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * Get a setting by the setting key. |
|
227 | - * |
|
228 | - * @param string $key Key for the setting |
|
229 | - * |
|
230 | - * @todo Use the \GV\Settings class later. For now subclasses may still expect and array instead. |
|
231 | - * |
|
232 | - * @return mixed|null Value of the setting; NULL if not set |
|
233 | - */ |
|
234 | - public function get_setting($key) |
|
235 | - { |
|
236 | - return Utils::get($this->settings, $key, null); |
|
237 | - } |
|
238 | - |
|
239 | - /** |
|
240 | - * Default widget areas. |
|
241 | - * |
|
242 | - * Usually overridden by the selected template. |
|
243 | - * |
|
244 | - * @return array The default areas where widgets can be rendered. |
|
245 | - */ |
|
246 | - public static function get_default_widget_areas() |
|
247 | - { |
|
248 | - $default_areas = [ |
|
249 | - [ |
|
250 | - '1-1' => [ |
|
251 | - [ |
|
252 | - 'areaid' => 'top', |
|
253 | - 'title' => __('Top', 'gravityview'), |
|
254 | - 'subtitle' => '', |
|
255 | - ], |
|
256 | - ], |
|
257 | - ], |
|
258 | - [ |
|
259 | - '1-2' => [ |
|
260 | - [ |
|
261 | - 'areaid' => 'left', |
|
262 | - 'title' => __('Left', 'gravityview'), |
|
263 | - 'subtitle' => '', |
|
264 | - ], |
|
265 | - ], |
|
266 | - '2-2' => [ |
|
267 | - [ |
|
268 | - 'areaid' => 'right', |
|
269 | - 'title' => __('Right', 'gravityview'), |
|
270 | - 'subtitle' => '', |
|
271 | - ], |
|
272 | - ], |
|
273 | - ], |
|
274 | - ]; |
|
275 | - |
|
276 | - /** |
|
277 | - * @filter `gravityview_widget_active_areas` Array of zones available for widgets to be dropped into |
|
278 | - * |
|
279 | - * @deprecated 2.0: Use gravityview/widget/active_areas instead |
|
280 | - * |
|
281 | - * @param array $default_areas Definition for default widget areas |
|
282 | - */ |
|
283 | - $default_areas = apply_filters('gravityview_widget_active_areas', $default_areas); |
|
284 | - |
|
285 | - /** |
|
286 | - * @filter `gravityview/widget/active_areas` Array of zones available for widgets to be dropped into |
|
287 | - * |
|
288 | - * @since 2.0 |
|
289 | - * |
|
290 | - * @param array $default_areas Definition for default widget areas |
|
291 | - */ |
|
292 | - return apply_filters('gravityview/widget/active_areas', $default_areas); |
|
293 | - } |
|
294 | - |
|
295 | - /** |
|
296 | - * Register widget to become available in admin. And for lookup. |
|
297 | - * |
|
298 | - * @param array $widgets Usually just empty. Used to gather them all up. |
|
299 | - * |
|
300 | - * @return array $widgets |
|
301 | - */ |
|
302 | - public function register_widget($widgets) |
|
303 | - { |
|
304 | - if (!is_array($widgets)) { |
|
305 | - $widgets = []; |
|
306 | - } |
|
307 | - |
|
308 | - $widgets[$this->get_widget_id()] = [ |
|
309 | - 'label' => $this->widget_label, |
|
310 | - 'description' => $this->widget_description, |
|
311 | - 'subtitle' => $this->widget_subtitle, |
|
312 | - 'icon' => $this->icon, |
|
313 | - 'class' => get_called_class(), |
|
314 | - ]; |
|
315 | - |
|
316 | - return $widgets; |
|
317 | - } |
|
318 | - |
|
319 | - /** |
|
320 | - * Assign template specific widget options. |
|
321 | - * |
|
322 | - * |
|
323 | - * @param array $options (default: array()) |
|
324 | - * @param string $template (default: '') |
|
325 | - * |
|
326 | - * @return array |
|
327 | - */ |
|
328 | - public function assign_widget_options($options = [], $template = '', $widget = '') |
|
329 | - { |
|
330 | - if ($this->get_widget_id() === $widget) { |
|
331 | - if ($settings = $this->get_settings()) { |
|
332 | - $options = array_merge($options, $settings); |
|
333 | - } |
|
334 | - } |
|
335 | - |
|
336 | - return $options; |
|
337 | - } |
|
338 | - |
|
339 | - /** |
|
340 | - * Do shortcode if the Widget's shortcode exists. |
|
341 | - * |
|
342 | - * @param string $text Widget text to check |
|
343 | - * @param null|\WP_Widget Empty if not called by WP_Widget, or a WP_Widget instance |
|
344 | - * |
|
345 | - * @return string Widget text |
|
346 | - */ |
|
347 | - public function maybe_do_shortcode($text, $widget = null) |
|
348 | - { |
|
349 | - if (!empty($this->shortcode_name) && has_shortcode($text, $this->shortcode_name)) { |
|
350 | - return do_shortcode($text); |
|
351 | - } |
|
352 | - |
|
353 | - return $text; |
|
354 | - } |
|
355 | - |
|
356 | - /** |
|
357 | - * Add $this->shortcode_name shortcode to output self::render_frontend(). |
|
358 | - * |
|
359 | - * @return void |
|
360 | - */ |
|
361 | - public function add_shortcode() |
|
362 | - { |
|
363 | - if (empty($this->shortcode_name)) { |
|
364 | - return; |
|
365 | - } |
|
366 | - |
|
367 | - if (!gravityview()->plugin->is_compatible()) { |
|
368 | - return; |
|
369 | - } |
|
370 | - |
|
371 | - if (gravityview()->request->is_admin()) { |
|
372 | - return; |
|
373 | - } |
|
374 | - |
|
375 | - // If the widget shouldn't output on single entries, don't show it |
|
376 | - if (empty($this->show_on_single) && gravityview()->request->is_entry()) { |
|
377 | - gravityview()->log->debug('Skipping; set to not run on single entry.'); |
|
378 | - add_shortcode($this->shortcode_name, '__return_null'); |
|
379 | - |
|
380 | - return; |
|
381 | - } |
|
382 | - |
|
383 | - global $post; |
|
384 | - |
|
385 | - if (!is_object($post) || empty($post->post_content) || !Shortcode::parse($post->post_content)) { |
|
386 | - add_shortcode($this->shortcode_name, '__return_null'); |
|
387 | - |
|
388 | - return; |
|
389 | - } |
|
390 | - |
|
391 | - add_shortcode($this->shortcode_name, [$this, 'render_shortcode']); |
|
392 | - } |
|
393 | - |
|
394 | - /** |
|
395 | - * Frontend logic. |
|
396 | - * |
|
397 | - * Override in child class. |
|
398 | - * |
|
399 | - * @param array $widget_args The Widget shortcode args. |
|
400 | - * @param string $content The content. |
|
401 | - * @param string|\GV\Template_Context $context The context, if available. |
|
402 | - * |
|
403 | - * @return void |
|
404 | - */ |
|
405 | - public function render_frontend($widget_args, $content = '', $context = '') |
|
406 | - { |
|
407 | - } |
|
408 | - |
|
409 | - /** |
|
410 | - * General validations when rendering the widget. |
|
411 | - * |
|
412 | - * Always call this from your `render_frontend()` override! |
|
413 | - * |
|
414 | - * @return bool True: render frontend; False: don't render frontend |
|
415 | - */ |
|
416 | - public function pre_render_frontend() |
|
417 | - { |
|
418 | - |
|
419 | - /** |
|
420 | - * Assume shown regardless of hide_until_search setting. |
|
421 | - */ |
|
422 | - $allowlist = [ |
|
423 | - 'custom_content', |
|
424 | - ]; |
|
425 | - |
|
426 | - /** |
|
427 | - * @deprecated 2.14 In favor of allowlist. |
|
428 | - */ |
|
429 | - $allowlist = apply_filters_deprecated('gravityview/widget/hide_until_searched/whitelist', [$allowlist], '2.14', 'gravityview/widget/hide_until_searched/allowlist'); |
|
430 | - |
|
431 | - /** |
|
432 | - * @filter `gravityview/widget/hide_until_searched/allowlist` Some widgets have got to stay shown. |
|
433 | - * |
|
434 | - * @since 2.14 |
|
435 | - * |
|
436 | - * @param string[] $allowlist The widget IDs that have to be shown by default. |
|
437 | - */ |
|
438 | - $allowlist = apply_filters('gravityview/widget/hide_until_searched/allowlist', $allowlist); |
|
439 | - |
|
440 | - if (($view = gravityview()->views->get()) && !in_array($this->get_widget_id(), $allowlist)) { |
|
441 | - $hide_until_searched = $view->settings->get('hide_until_searched'); |
|
442 | - } else { |
|
443 | - $hide_until_searched = false; |
|
444 | - } |
|
445 | - |
|
446 | - /** |
|
447 | - * @filter `gravityview/widget/hide_until_searched` Modify whether to hide content until search |
|
448 | - * |
|
449 | - * @param bool $hide_until_searched Hide until search? |
|
450 | - * @param \GV\Widget $this Widget instance |
|
451 | - */ |
|
452 | - $hide_until_searched = apply_filters('gravityview/widget/hide_until_searched', $hide_until_searched, $this); |
|
453 | - |
|
454 | - if ($hide_until_searched && !gravityview()->request->is_search()) { |
|
455 | - gravityview()->log->debug('Hide View data until search is performed'); |
|
456 | - |
|
457 | - return false; |
|
458 | - } |
|
459 | - |
|
460 | - return true; |
|
461 | - } |
|
462 | - |
|
463 | - /** |
|
464 | - * Shortcode. |
|
465 | - * |
|
466 | - * @param array $atts The Widget shortcode args. |
|
467 | - * @param string $content The content. |
|
468 | - * @param string|\GV\Template_Context $context The context, if available. |
|
469 | - * |
|
470 | - * @return string Whatever the widget echoed. |
|
471 | - */ |
|
472 | - public function render_shortcode($atts, $content = '', $context = '') |
|
473 | - { |
|
474 | - ob_start(); |
|
475 | - $this->render_frontend($atts, $content, $context); |
|
476 | - |
|
477 | - return ob_get_clean(); |
|
478 | - } |
|
479 | - |
|
480 | - /** |
|
481 | - * Create the needed widget from a configuration array. |
|
482 | - * |
|
483 | - * @param array $configuration The configuration array. |
|
484 | - * |
|
485 | - * @see \GV\Widget::as_configuration() |
|
486 | - * |
|
487 | - * @internal |
|
488 | - * |
|
489 | - * @since 2.0 |
|
490 | - * |
|
491 | - * @return \GV\Widget|null The widget implementation from configuration or none. |
|
492 | - */ |
|
493 | - public static function from_configuration($configuration) |
|
494 | - { |
|
495 | - $registered_widgets = self::registered(); |
|
496 | - |
|
497 | - if (!$id = Utils::get($configuration, 'id')) { |
|
498 | - return null; |
|
499 | - } |
|
500 | - |
|
501 | - if (!$widget = Utils::get($registered_widgets, $id)) { |
|
502 | - return null; |
|
503 | - } |
|
504 | - |
|
505 | - if (!class_exists($class = Utils::get($widget, 'class'))) { |
|
506 | - return null; |
|
507 | - } |
|
508 | - |
|
509 | - $w = new $class(Utils::get($widget, 'label'), $id); |
|
510 | - $w->configuration = new Settings($configuration); |
|
511 | - |
|
512 | - return $w; |
|
513 | - } |
|
514 | - |
|
515 | - /** |
|
516 | - * Return an array of the old format. |
|
517 | - * |
|
518 | - * 'id' => string |
|
519 | - * + whatever else specific fields may have |
|
520 | - * |
|
521 | - * @internal |
|
522 | - * |
|
523 | - * @since 2.0 |
|
524 | - * |
|
525 | - * @return array |
|
526 | - */ |
|
527 | - public function as_configuration() |
|
528 | - { |
|
529 | - return array_merge([ |
|
530 | - 'id' => $this->get_widget_id(), |
|
531 | - ], $this->configuration->all()); |
|
532 | - } |
|
533 | - |
|
534 | - /** |
|
535 | - * Return all registered widgets. |
|
536 | - * |
|
537 | - * @api |
|
538 | - * |
|
539 | - * @since 2.0 |
|
540 | - * |
|
541 | - * @return array |
|
542 | - */ |
|
543 | - public static function registered() |
|
544 | - { |
|
545 | - /** |
|
546 | - * @filter `gravityview_register_directory_widgets` Get the list of registered widgets. Each item is used to instantiate a GravityView_Admin_View_Widget object |
|
547 | - * |
|
548 | - * @deprecated Use `gravityview/widgets/register` |
|
549 | - * |
|
550 | - * @param array $registered_widgets Empty array |
|
551 | - */ |
|
552 | - $registered_widgets = apply_filters('gravityview_register_directory_widgets', []); |
|
553 | - |
|
554 | - /** |
|
555 | - * @filter `gravityview/widgets/register` Each item is used to instantiate a GravityView_Admin_View_Widget object |
|
556 | - * |
|
557 | - * @param array $registered_widgets Empty array |
|
558 | - */ |
|
559 | - return apply_filters('gravityview/widgets/register', $registered_widgets); |
|
560 | - } |
|
561 | - |
|
562 | - /** |
|
563 | - * Whether this Widget's been registered already or not. |
|
564 | - * |
|
565 | - * @api |
|
566 | - * |
|
567 | - * @since 2.0 |
|
568 | - * |
|
569 | - * @return bool |
|
570 | - */ |
|
571 | - public function is_registered() |
|
572 | - { |
|
573 | - if (!$widget_id = $this->get_widget_id()) { |
|
574 | - gravityview()->log->warning('Widget ID not set before calling Widget::is_registered', ['data' => $this]); |
|
575 | - |
|
576 | - return false; |
|
577 | - } |
|
578 | - |
|
579 | - return in_array($widget_id, array_keys(self::registered()), true); |
|
580 | - } |
|
17 | + /** |
|
18 | + * Widget admin label. |
|
19 | + * |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + protected $widget_label = ''; |
|
23 | + |
|
24 | + /** |
|
25 | + * Widget description, shown on the "+ Add Widget" picker. |
|
26 | + * |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + protected $widget_description = ''; |
|
30 | + |
|
31 | + /** |
|
32 | + * Widget details, shown in the widget modal. |
|
33 | + * |
|
34 | + * @since 1.8 |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + protected $widget_subtitle = ''; |
|
39 | + |
|
40 | + /** |
|
41 | + * Widget admin ID. |
|
42 | + * |
|
43 | + * @var string |
|
44 | + */ |
|
45 | + protected $widget_id = ''; |
|
46 | + |
|
47 | + /** |
|
48 | + * Default configuration for header and footer. |
|
49 | + * |
|
50 | + * @var array |
|
51 | + */ |
|
52 | + protected $defaults = []; |
|
53 | + |
|
54 | + /** |
|
55 | + * Widget admin advanced settings. |
|
56 | + * |
|
57 | + * @var array |
|
58 | + */ |
|
59 | + protected $settings = []; |
|
60 | + |
|
61 | + /** |
|
62 | + * Allow class to automatically add widget_text filter for you in shortcode. |
|
63 | + * |
|
64 | + * @var string |
|
65 | + */ |
|
66 | + protected $shortcode_name; |
|
67 | + |
|
68 | + /** |
|
69 | + * Hold the widget options. |
|
70 | + * |
|
71 | + * @var array() |
|
72 | + */ |
|
73 | + private $widget_options = []; |
|
74 | + |
|
75 | + /** |
|
76 | + * The position of the widget. |
|
77 | + * |
|
78 | + * @api |
|
79 | + * |
|
80 | + * @since 2.0 |
|
81 | + * |
|
82 | + * @var string |
|
83 | + */ |
|
84 | + public $position = ''; |
|
85 | + |
|
86 | + /** |
|
87 | + * A unique ID for this widget. |
|
88 | + * |
|
89 | + * @api |
|
90 | + * |
|
91 | + * @since 2.0 |
|
92 | + * |
|
93 | + * @var string |
|
94 | + */ |
|
95 | + public $UID = ''; |
|
96 | + |
|
97 | + /** |
|
98 | + * The actual configuration for this widget instance. |
|
99 | + * |
|
100 | + * @api |
|
101 | + * |
|
102 | + * @since 2.0 |
|
103 | + * |
|
104 | + * @var \GV\Settings |
|
105 | + */ |
|
106 | + public $configuration; |
|
107 | + |
|
108 | + /** |
|
109 | + * @var string An icon that represents the widget type in the widget picker. |
|
110 | + * |
|
111 | + * Supports these icon formats: |
|
112 | + * - Gravity Forms icon class: The string starts with "gform-icon". Note: the site must be running GF 2.5+. No need to also pass "gform-icon". |
|
113 | + * - Dashicons: The string starts with "dashicons". No need to also pass "dashicons". |
|
114 | + * - Inline SVG: Starts with "data:" |
|
115 | + * - If not matching those formats, the value will be used as a CSS class in a `<i>` element. |
|
116 | + * |
|
117 | + * @see GravityView_Admin_View_Item::getOutput |
|
118 | + */ |
|
119 | + public $icon; |
|
120 | + |
|
121 | + /** |
|
122 | + * Constructor. |
|
123 | + * |
|
124 | + * @param string $label The Widget label as shown in the admin. |
|
125 | + * @param string $id The Widget ID, make this something unique. |
|
126 | + * @param array $defaults Default footer/header Widget configuration. |
|
127 | + * @param array $settings Advanced Widget settings. |
|
128 | + * |
|
129 | + * @return \GV\Widget |
|
130 | + */ |
|
131 | + public function __construct($label, $id, $defaults = [], $settings = []) |
|
132 | + { |
|
133 | + /** |
|
134 | + * The shortcode name is set to the lowercase name of the widget class, unless overridden by the class specifying a different value for $shortcode_name. |
|
135 | + * |
|
136 | + * @var string |
|
137 | + */ |
|
138 | + $this->shortcode_name = empty($this->shortcode_name) ? strtolower(get_called_class()) : $this->shortcode_name; |
|
139 | + |
|
140 | + if ($id) { |
|
141 | + $this->widget_id = $id; |
|
142 | + } |
|
143 | + |
|
144 | + $this->widget_label = $label; |
|
145 | + $this->defaults = array_merge(['header' => 0, 'footer' => 0], $defaults); |
|
146 | + |
|
147 | + // Make sure every widget has a title, even if empty |
|
148 | + $this->settings = wp_parse_args($settings, $this->get_default_settings()); |
|
149 | + |
|
150 | + // Hook once per unique ID |
|
151 | + if ($this->is_registered()) { |
|
152 | + return; |
|
153 | + } |
|
154 | + |
|
155 | + // widget options |
|
156 | + add_filter('gravityview_template_widget_options', [$this, 'assign_widget_options'], 10, 3); |
|
157 | + |
|
158 | + // frontend logic |
|
159 | + add_action(sprintf('gravityview/widgets/%s/render', $this->get_widget_id()), [$this, 'render_frontend'], 10, 3); |
|
160 | + |
|
161 | + // register shortcodes |
|
162 | + add_action('wp', [$this, 'add_shortcode']); |
|
163 | + |
|
164 | + // Use shortcodes in text widgets. |
|
165 | + add_filter('widget_text', [$this, 'maybe_do_shortcode']); |
|
166 | + |
|
167 | + // register widgets to be listed in the View Configuration |
|
168 | + // Important: this has to be the last filter/action added in the constructor. |
|
169 | + add_filter('gravityview/widgets/register', [$this, 'register_widget']); |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * Define general widget settings. |
|
174 | + * |
|
175 | + * @since 1.5.4 |
|
176 | + * |
|
177 | + * @return array $settings Default settings |
|
178 | + */ |
|
179 | + protected function get_default_settings() |
|
180 | + { |
|
181 | + $settings = []; |
|
182 | + |
|
183 | + /** |
|
184 | + * @filter `gravityview/widget/enable_custom_class` Enable custom CSS class settings for widgets |
|
185 | + * |
|
186 | + * @param bool $enable_custom_class False by default. Return true if you want to enable. |
|
187 | + * @param \GV\Widget $this Current instance of \GV\Widget. |
|
188 | + */ |
|
189 | + $enable_custom_class = apply_filters('gravityview/widget/enable_custom_class', false, $this); |
|
190 | + |
|
191 | + if ($enable_custom_class) { |
|
192 | + $settings['custom_class'] = [ |
|
193 | + 'type' => 'text', |
|
194 | + 'label' => __('Custom CSS Class:', 'gravityview'), |
|
195 | + 'desc' => __('This class will be added to the widget container', 'gravityview'), |
|
196 | + 'value' => '', |
|
197 | + 'merge_tags' => true, |
|
198 | + 'class' => 'widefat code', |
|
199 | + ]; |
|
200 | + } |
|
201 | + |
|
202 | + return $settings; |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * Get the Widget ID. |
|
207 | + * |
|
208 | + * @return string The Widget ID. |
|
209 | + */ |
|
210 | + public function get_widget_id() |
|
211 | + { |
|
212 | + return $this->widget_id; |
|
213 | + } |
|
214 | + |
|
215 | + /** |
|
216 | + * Get the widget settings. |
|
217 | + * |
|
218 | + * @return array|null Settings array; NULL if not set for some reason. |
|
219 | + */ |
|
220 | + public function get_settings() |
|
221 | + { |
|
222 | + return empty($this->settings) ? null : $this->settings; |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Get a setting by the setting key. |
|
227 | + * |
|
228 | + * @param string $key Key for the setting |
|
229 | + * |
|
230 | + * @todo Use the \GV\Settings class later. For now subclasses may still expect and array instead. |
|
231 | + * |
|
232 | + * @return mixed|null Value of the setting; NULL if not set |
|
233 | + */ |
|
234 | + public function get_setting($key) |
|
235 | + { |
|
236 | + return Utils::get($this->settings, $key, null); |
|
237 | + } |
|
238 | + |
|
239 | + /** |
|
240 | + * Default widget areas. |
|
241 | + * |
|
242 | + * Usually overridden by the selected template. |
|
243 | + * |
|
244 | + * @return array The default areas where widgets can be rendered. |
|
245 | + */ |
|
246 | + public static function get_default_widget_areas() |
|
247 | + { |
|
248 | + $default_areas = [ |
|
249 | + [ |
|
250 | + '1-1' => [ |
|
251 | + [ |
|
252 | + 'areaid' => 'top', |
|
253 | + 'title' => __('Top', 'gravityview'), |
|
254 | + 'subtitle' => '', |
|
255 | + ], |
|
256 | + ], |
|
257 | + ], |
|
258 | + [ |
|
259 | + '1-2' => [ |
|
260 | + [ |
|
261 | + 'areaid' => 'left', |
|
262 | + 'title' => __('Left', 'gravityview'), |
|
263 | + 'subtitle' => '', |
|
264 | + ], |
|
265 | + ], |
|
266 | + '2-2' => [ |
|
267 | + [ |
|
268 | + 'areaid' => 'right', |
|
269 | + 'title' => __('Right', 'gravityview'), |
|
270 | + 'subtitle' => '', |
|
271 | + ], |
|
272 | + ], |
|
273 | + ], |
|
274 | + ]; |
|
275 | + |
|
276 | + /** |
|
277 | + * @filter `gravityview_widget_active_areas` Array of zones available for widgets to be dropped into |
|
278 | + * |
|
279 | + * @deprecated 2.0: Use gravityview/widget/active_areas instead |
|
280 | + * |
|
281 | + * @param array $default_areas Definition for default widget areas |
|
282 | + */ |
|
283 | + $default_areas = apply_filters('gravityview_widget_active_areas', $default_areas); |
|
284 | + |
|
285 | + /** |
|
286 | + * @filter `gravityview/widget/active_areas` Array of zones available for widgets to be dropped into |
|
287 | + * |
|
288 | + * @since 2.0 |
|
289 | + * |
|
290 | + * @param array $default_areas Definition for default widget areas |
|
291 | + */ |
|
292 | + return apply_filters('gravityview/widget/active_areas', $default_areas); |
|
293 | + } |
|
294 | + |
|
295 | + /** |
|
296 | + * Register widget to become available in admin. And for lookup. |
|
297 | + * |
|
298 | + * @param array $widgets Usually just empty. Used to gather them all up. |
|
299 | + * |
|
300 | + * @return array $widgets |
|
301 | + */ |
|
302 | + public function register_widget($widgets) |
|
303 | + { |
|
304 | + if (!is_array($widgets)) { |
|
305 | + $widgets = []; |
|
306 | + } |
|
307 | + |
|
308 | + $widgets[$this->get_widget_id()] = [ |
|
309 | + 'label' => $this->widget_label, |
|
310 | + 'description' => $this->widget_description, |
|
311 | + 'subtitle' => $this->widget_subtitle, |
|
312 | + 'icon' => $this->icon, |
|
313 | + 'class' => get_called_class(), |
|
314 | + ]; |
|
315 | + |
|
316 | + return $widgets; |
|
317 | + } |
|
318 | + |
|
319 | + /** |
|
320 | + * Assign template specific widget options. |
|
321 | + * |
|
322 | + * |
|
323 | + * @param array $options (default: array()) |
|
324 | + * @param string $template (default: '') |
|
325 | + * |
|
326 | + * @return array |
|
327 | + */ |
|
328 | + public function assign_widget_options($options = [], $template = '', $widget = '') |
|
329 | + { |
|
330 | + if ($this->get_widget_id() === $widget) { |
|
331 | + if ($settings = $this->get_settings()) { |
|
332 | + $options = array_merge($options, $settings); |
|
333 | + } |
|
334 | + } |
|
335 | + |
|
336 | + return $options; |
|
337 | + } |
|
338 | + |
|
339 | + /** |
|
340 | + * Do shortcode if the Widget's shortcode exists. |
|
341 | + * |
|
342 | + * @param string $text Widget text to check |
|
343 | + * @param null|\WP_Widget Empty if not called by WP_Widget, or a WP_Widget instance |
|
344 | + * |
|
345 | + * @return string Widget text |
|
346 | + */ |
|
347 | + public function maybe_do_shortcode($text, $widget = null) |
|
348 | + { |
|
349 | + if (!empty($this->shortcode_name) && has_shortcode($text, $this->shortcode_name)) { |
|
350 | + return do_shortcode($text); |
|
351 | + } |
|
352 | + |
|
353 | + return $text; |
|
354 | + } |
|
355 | + |
|
356 | + /** |
|
357 | + * Add $this->shortcode_name shortcode to output self::render_frontend(). |
|
358 | + * |
|
359 | + * @return void |
|
360 | + */ |
|
361 | + public function add_shortcode() |
|
362 | + { |
|
363 | + if (empty($this->shortcode_name)) { |
|
364 | + return; |
|
365 | + } |
|
366 | + |
|
367 | + if (!gravityview()->plugin->is_compatible()) { |
|
368 | + return; |
|
369 | + } |
|
370 | + |
|
371 | + if (gravityview()->request->is_admin()) { |
|
372 | + return; |
|
373 | + } |
|
374 | + |
|
375 | + // If the widget shouldn't output on single entries, don't show it |
|
376 | + if (empty($this->show_on_single) && gravityview()->request->is_entry()) { |
|
377 | + gravityview()->log->debug('Skipping; set to not run on single entry.'); |
|
378 | + add_shortcode($this->shortcode_name, '__return_null'); |
|
379 | + |
|
380 | + return; |
|
381 | + } |
|
382 | + |
|
383 | + global $post; |
|
384 | + |
|
385 | + if (!is_object($post) || empty($post->post_content) || !Shortcode::parse($post->post_content)) { |
|
386 | + add_shortcode($this->shortcode_name, '__return_null'); |
|
387 | + |
|
388 | + return; |
|
389 | + } |
|
390 | + |
|
391 | + add_shortcode($this->shortcode_name, [$this, 'render_shortcode']); |
|
392 | + } |
|
393 | + |
|
394 | + /** |
|
395 | + * Frontend logic. |
|
396 | + * |
|
397 | + * Override in child class. |
|
398 | + * |
|
399 | + * @param array $widget_args The Widget shortcode args. |
|
400 | + * @param string $content The content. |
|
401 | + * @param string|\GV\Template_Context $context The context, if available. |
|
402 | + * |
|
403 | + * @return void |
|
404 | + */ |
|
405 | + public function render_frontend($widget_args, $content = '', $context = '') |
|
406 | + { |
|
407 | + } |
|
408 | + |
|
409 | + /** |
|
410 | + * General validations when rendering the widget. |
|
411 | + * |
|
412 | + * Always call this from your `render_frontend()` override! |
|
413 | + * |
|
414 | + * @return bool True: render frontend; False: don't render frontend |
|
415 | + */ |
|
416 | + public function pre_render_frontend() |
|
417 | + { |
|
418 | + |
|
419 | + /** |
|
420 | + * Assume shown regardless of hide_until_search setting. |
|
421 | + */ |
|
422 | + $allowlist = [ |
|
423 | + 'custom_content', |
|
424 | + ]; |
|
425 | + |
|
426 | + /** |
|
427 | + * @deprecated 2.14 In favor of allowlist. |
|
428 | + */ |
|
429 | + $allowlist = apply_filters_deprecated('gravityview/widget/hide_until_searched/whitelist', [$allowlist], '2.14', 'gravityview/widget/hide_until_searched/allowlist'); |
|
430 | + |
|
431 | + /** |
|
432 | + * @filter `gravityview/widget/hide_until_searched/allowlist` Some widgets have got to stay shown. |
|
433 | + * |
|
434 | + * @since 2.14 |
|
435 | + * |
|
436 | + * @param string[] $allowlist The widget IDs that have to be shown by default. |
|
437 | + */ |
|
438 | + $allowlist = apply_filters('gravityview/widget/hide_until_searched/allowlist', $allowlist); |
|
439 | + |
|
440 | + if (($view = gravityview()->views->get()) && !in_array($this->get_widget_id(), $allowlist)) { |
|
441 | + $hide_until_searched = $view->settings->get('hide_until_searched'); |
|
442 | + } else { |
|
443 | + $hide_until_searched = false; |
|
444 | + } |
|
445 | + |
|
446 | + /** |
|
447 | + * @filter `gravityview/widget/hide_until_searched` Modify whether to hide content until search |
|
448 | + * |
|
449 | + * @param bool $hide_until_searched Hide until search? |
|
450 | + * @param \GV\Widget $this Widget instance |
|
451 | + */ |
|
452 | + $hide_until_searched = apply_filters('gravityview/widget/hide_until_searched', $hide_until_searched, $this); |
|
453 | + |
|
454 | + if ($hide_until_searched && !gravityview()->request->is_search()) { |
|
455 | + gravityview()->log->debug('Hide View data until search is performed'); |
|
456 | + |
|
457 | + return false; |
|
458 | + } |
|
459 | + |
|
460 | + return true; |
|
461 | + } |
|
462 | + |
|
463 | + /** |
|
464 | + * Shortcode. |
|
465 | + * |
|
466 | + * @param array $atts The Widget shortcode args. |
|
467 | + * @param string $content The content. |
|
468 | + * @param string|\GV\Template_Context $context The context, if available. |
|
469 | + * |
|
470 | + * @return string Whatever the widget echoed. |
|
471 | + */ |
|
472 | + public function render_shortcode($atts, $content = '', $context = '') |
|
473 | + { |
|
474 | + ob_start(); |
|
475 | + $this->render_frontend($atts, $content, $context); |
|
476 | + |
|
477 | + return ob_get_clean(); |
|
478 | + } |
|
479 | + |
|
480 | + /** |
|
481 | + * Create the needed widget from a configuration array. |
|
482 | + * |
|
483 | + * @param array $configuration The configuration array. |
|
484 | + * |
|
485 | + * @see \GV\Widget::as_configuration() |
|
486 | + * |
|
487 | + * @internal |
|
488 | + * |
|
489 | + * @since 2.0 |
|
490 | + * |
|
491 | + * @return \GV\Widget|null The widget implementation from configuration or none. |
|
492 | + */ |
|
493 | + public static function from_configuration($configuration) |
|
494 | + { |
|
495 | + $registered_widgets = self::registered(); |
|
496 | + |
|
497 | + if (!$id = Utils::get($configuration, 'id')) { |
|
498 | + return null; |
|
499 | + } |
|
500 | + |
|
501 | + if (!$widget = Utils::get($registered_widgets, $id)) { |
|
502 | + return null; |
|
503 | + } |
|
504 | + |
|
505 | + if (!class_exists($class = Utils::get($widget, 'class'))) { |
|
506 | + return null; |
|
507 | + } |
|
508 | + |
|
509 | + $w = new $class(Utils::get($widget, 'label'), $id); |
|
510 | + $w->configuration = new Settings($configuration); |
|
511 | + |
|
512 | + return $w; |
|
513 | + } |
|
514 | + |
|
515 | + /** |
|
516 | + * Return an array of the old format. |
|
517 | + * |
|
518 | + * 'id' => string |
|
519 | + * + whatever else specific fields may have |
|
520 | + * |
|
521 | + * @internal |
|
522 | + * |
|
523 | + * @since 2.0 |
|
524 | + * |
|
525 | + * @return array |
|
526 | + */ |
|
527 | + public function as_configuration() |
|
528 | + { |
|
529 | + return array_merge([ |
|
530 | + 'id' => $this->get_widget_id(), |
|
531 | + ], $this->configuration->all()); |
|
532 | + } |
|
533 | + |
|
534 | + /** |
|
535 | + * Return all registered widgets. |
|
536 | + * |
|
537 | + * @api |
|
538 | + * |
|
539 | + * @since 2.0 |
|
540 | + * |
|
541 | + * @return array |
|
542 | + */ |
|
543 | + public static function registered() |
|
544 | + { |
|
545 | + /** |
|
546 | + * @filter `gravityview_register_directory_widgets` Get the list of registered widgets. Each item is used to instantiate a GravityView_Admin_View_Widget object |
|
547 | + * |
|
548 | + * @deprecated Use `gravityview/widgets/register` |
|
549 | + * |
|
550 | + * @param array $registered_widgets Empty array |
|
551 | + */ |
|
552 | + $registered_widgets = apply_filters('gravityview_register_directory_widgets', []); |
|
553 | + |
|
554 | + /** |
|
555 | + * @filter `gravityview/widgets/register` Each item is used to instantiate a GravityView_Admin_View_Widget object |
|
556 | + * |
|
557 | + * @param array $registered_widgets Empty array |
|
558 | + */ |
|
559 | + return apply_filters('gravityview/widgets/register', $registered_widgets); |
|
560 | + } |
|
561 | + |
|
562 | + /** |
|
563 | + * Whether this Widget's been registered already or not. |
|
564 | + * |
|
565 | + * @api |
|
566 | + * |
|
567 | + * @since 2.0 |
|
568 | + * |
|
569 | + * @return bool |
|
570 | + */ |
|
571 | + public function is_registered() |
|
572 | + { |
|
573 | + if (!$widget_id = $this->get_widget_id()) { |
|
574 | + gravityview()->log->warning('Widget ID not set before calling Widget::is_registered', ['data' => $this]); |
|
575 | + |
|
576 | + return false; |
|
577 | + } |
|
578 | + |
|
579 | + return in_array($widget_id, array_keys(self::registered()), true); |
|
580 | + } |
|
581 | 581 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | namespace GV; |
4 | 4 | |
5 | 5 | /** If this file is called directly, abort. */ |
6 | -if (!defined('GRAVITYVIEW_DIR')) { |
|
6 | +if ( ! defined( 'GRAVITYVIEW_DIR' ) ) { |
|
7 | 7 | exit(); |
8 | 8 | } |
9 | 9 | |
@@ -49,14 +49,14 @@ discard block |
||
49 | 49 | * |
50 | 50 | * @var array |
51 | 51 | */ |
52 | - protected $defaults = []; |
|
52 | + protected $defaults = [ ]; |
|
53 | 53 | |
54 | 54 | /** |
55 | 55 | * Widget admin advanced settings. |
56 | 56 | * |
57 | 57 | * @var array |
58 | 58 | */ |
59 | - protected $settings = []; |
|
59 | + protected $settings = [ ]; |
|
60 | 60 | |
61 | 61 | /** |
62 | 62 | * Allow class to automatically add widget_text filter for you in shortcode. |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | * |
71 | 71 | * @var array() |
72 | 72 | */ |
73 | - private $widget_options = []; |
|
73 | + private $widget_options = [ ]; |
|
74 | 74 | |
75 | 75 | /** |
76 | 76 | * The position of the widget. |
@@ -128,45 +128,45 @@ discard block |
||
128 | 128 | * |
129 | 129 | * @return \GV\Widget |
130 | 130 | */ |
131 | - public function __construct($label, $id, $defaults = [], $settings = []) |
|
131 | + public function __construct( $label, $id, $defaults = [ ], $settings = [ ] ) |
|
132 | 132 | { |
133 | 133 | /** |
134 | 134 | * The shortcode name is set to the lowercase name of the widget class, unless overridden by the class specifying a different value for $shortcode_name. |
135 | 135 | * |
136 | 136 | * @var string |
137 | 137 | */ |
138 | - $this->shortcode_name = empty($this->shortcode_name) ? strtolower(get_called_class()) : $this->shortcode_name; |
|
138 | + $this->shortcode_name = empty( $this->shortcode_name ) ? strtolower( get_called_class() ) : $this->shortcode_name; |
|
139 | 139 | |
140 | - if ($id) { |
|
140 | + if ( $id ) { |
|
141 | 141 | $this->widget_id = $id; |
142 | 142 | } |
143 | 143 | |
144 | 144 | $this->widget_label = $label; |
145 | - $this->defaults = array_merge(['header' => 0, 'footer' => 0], $defaults); |
|
145 | + $this->defaults = array_merge( [ 'header' => 0, 'footer' => 0 ], $defaults ); |
|
146 | 146 | |
147 | 147 | // Make sure every widget has a title, even if empty |
148 | - $this->settings = wp_parse_args($settings, $this->get_default_settings()); |
|
148 | + $this->settings = wp_parse_args( $settings, $this->get_default_settings() ); |
|
149 | 149 | |
150 | 150 | // Hook once per unique ID |
151 | - if ($this->is_registered()) { |
|
151 | + if ( $this->is_registered() ) { |
|
152 | 152 | return; |
153 | 153 | } |
154 | 154 | |
155 | 155 | // widget options |
156 | - add_filter('gravityview_template_widget_options', [$this, 'assign_widget_options'], 10, 3); |
|
156 | + add_filter( 'gravityview_template_widget_options', [ $this, 'assign_widget_options' ], 10, 3 ); |
|
157 | 157 | |
158 | 158 | // frontend logic |
159 | - add_action(sprintf('gravityview/widgets/%s/render', $this->get_widget_id()), [$this, 'render_frontend'], 10, 3); |
|
159 | + add_action( sprintf( 'gravityview/widgets/%s/render', $this->get_widget_id() ), [ $this, 'render_frontend' ], 10, 3 ); |
|
160 | 160 | |
161 | 161 | // register shortcodes |
162 | - add_action('wp', [$this, 'add_shortcode']); |
|
162 | + add_action( 'wp', [ $this, 'add_shortcode' ] ); |
|
163 | 163 | |
164 | 164 | // Use shortcodes in text widgets. |
165 | - add_filter('widget_text', [$this, 'maybe_do_shortcode']); |
|
165 | + add_filter( 'widget_text', [ $this, 'maybe_do_shortcode' ] ); |
|
166 | 166 | |
167 | 167 | // register widgets to be listed in the View Configuration |
168 | 168 | // Important: this has to be the last filter/action added in the constructor. |
169 | - add_filter('gravityview/widgets/register', [$this, 'register_widget']); |
|
169 | + add_filter( 'gravityview/widgets/register', [ $this, 'register_widget' ] ); |
|
170 | 170 | } |
171 | 171 | |
172 | 172 | /** |
@@ -178,7 +178,7 @@ discard block |
||
178 | 178 | */ |
179 | 179 | protected function get_default_settings() |
180 | 180 | { |
181 | - $settings = []; |
|
181 | + $settings = [ ]; |
|
182 | 182 | |
183 | 183 | /** |
184 | 184 | * @filter `gravityview/widget/enable_custom_class` Enable custom CSS class settings for widgets |
@@ -186,13 +186,13 @@ discard block |
||
186 | 186 | * @param bool $enable_custom_class False by default. Return true if you want to enable. |
187 | 187 | * @param \GV\Widget $this Current instance of \GV\Widget. |
188 | 188 | */ |
189 | - $enable_custom_class = apply_filters('gravityview/widget/enable_custom_class', false, $this); |
|
189 | + $enable_custom_class = apply_filters( 'gravityview/widget/enable_custom_class', false, $this ); |
|
190 | 190 | |
191 | - if ($enable_custom_class) { |
|
192 | - $settings['custom_class'] = [ |
|
191 | + if ( $enable_custom_class ) { |
|
192 | + $settings[ 'custom_class' ] = [ |
|
193 | 193 | 'type' => 'text', |
194 | - 'label' => __('Custom CSS Class:', 'gravityview'), |
|
195 | - 'desc' => __('This class will be added to the widget container', 'gravityview'), |
|
194 | + 'label' => __( 'Custom CSS Class:', 'gravityview' ), |
|
195 | + 'desc' => __( 'This class will be added to the widget container', 'gravityview' ), |
|
196 | 196 | 'value' => '', |
197 | 197 | 'merge_tags' => true, |
198 | 198 | 'class' => 'widefat code', |
@@ -219,7 +219,7 @@ discard block |
||
219 | 219 | */ |
220 | 220 | public function get_settings() |
221 | 221 | { |
222 | - return empty($this->settings) ? null : $this->settings; |
|
222 | + return empty( $this->settings ) ? null : $this->settings; |
|
223 | 223 | } |
224 | 224 | |
225 | 225 | /** |
@@ -231,9 +231,9 @@ discard block |
||
231 | 231 | * |
232 | 232 | * @return mixed|null Value of the setting; NULL if not set |
233 | 233 | */ |
234 | - public function get_setting($key) |
|
234 | + public function get_setting( $key ) |
|
235 | 235 | { |
236 | - return Utils::get($this->settings, $key, null); |
|
236 | + return Utils::get( $this->settings, $key, null ); |
|
237 | 237 | } |
238 | 238 | |
239 | 239 | /** |
@@ -250,7 +250,7 @@ discard block |
||
250 | 250 | '1-1' => [ |
251 | 251 | [ |
252 | 252 | 'areaid' => 'top', |
253 | - 'title' => __('Top', 'gravityview'), |
|
253 | + 'title' => __( 'Top', 'gravityview' ), |
|
254 | 254 | 'subtitle' => '', |
255 | 255 | ], |
256 | 256 | ], |
@@ -259,14 +259,14 @@ discard block |
||
259 | 259 | '1-2' => [ |
260 | 260 | [ |
261 | 261 | 'areaid' => 'left', |
262 | - 'title' => __('Left', 'gravityview'), |
|
262 | + 'title' => __( 'Left', 'gravityview' ), |
|
263 | 263 | 'subtitle' => '', |
264 | 264 | ], |
265 | 265 | ], |
266 | 266 | '2-2' => [ |
267 | 267 | [ |
268 | 268 | 'areaid' => 'right', |
269 | - 'title' => __('Right', 'gravityview'), |
|
269 | + 'title' => __( 'Right', 'gravityview' ), |
|
270 | 270 | 'subtitle' => '', |
271 | 271 | ], |
272 | 272 | ], |
@@ -280,7 +280,7 @@ discard block |
||
280 | 280 | * |
281 | 281 | * @param array $default_areas Definition for default widget areas |
282 | 282 | */ |
283 | - $default_areas = apply_filters('gravityview_widget_active_areas', $default_areas); |
|
283 | + $default_areas = apply_filters( 'gravityview_widget_active_areas', $default_areas ); |
|
284 | 284 | |
285 | 285 | /** |
286 | 286 | * @filter `gravityview/widget/active_areas` Array of zones available for widgets to be dropped into |
@@ -289,7 +289,7 @@ discard block |
||
289 | 289 | * |
290 | 290 | * @param array $default_areas Definition for default widget areas |
291 | 291 | */ |
292 | - return apply_filters('gravityview/widget/active_areas', $default_areas); |
|
292 | + return apply_filters( 'gravityview/widget/active_areas', $default_areas ); |
|
293 | 293 | } |
294 | 294 | |
295 | 295 | /** |
@@ -299,13 +299,13 @@ discard block |
||
299 | 299 | * |
300 | 300 | * @return array $widgets |
301 | 301 | */ |
302 | - public function register_widget($widgets) |
|
302 | + public function register_widget( $widgets ) |
|
303 | 303 | { |
304 | - if (!is_array($widgets)) { |
|
305 | - $widgets = []; |
|
304 | + if ( ! is_array( $widgets ) ) { |
|
305 | + $widgets = [ ]; |
|
306 | 306 | } |
307 | 307 | |
308 | - $widgets[$this->get_widget_id()] = [ |
|
308 | + $widgets[ $this->get_widget_id() ] = [ |
|
309 | 309 | 'label' => $this->widget_label, |
310 | 310 | 'description' => $this->widget_description, |
311 | 311 | 'subtitle' => $this->widget_subtitle, |
@@ -325,11 +325,11 @@ discard block |
||
325 | 325 | * |
326 | 326 | * @return array |
327 | 327 | */ |
328 | - public function assign_widget_options($options = [], $template = '', $widget = '') |
|
328 | + public function assign_widget_options( $options = [ ], $template = '', $widget = '' ) |
|
329 | 329 | { |
330 | - if ($this->get_widget_id() === $widget) { |
|
331 | - if ($settings = $this->get_settings()) { |
|
332 | - $options = array_merge($options, $settings); |
|
330 | + if ( $this->get_widget_id() === $widget ) { |
|
331 | + if ( $settings = $this->get_settings() ) { |
|
332 | + $options = array_merge( $options, $settings ); |
|
333 | 333 | } |
334 | 334 | } |
335 | 335 | |
@@ -344,10 +344,10 @@ discard block |
||
344 | 344 | * |
345 | 345 | * @return string Widget text |
346 | 346 | */ |
347 | - public function maybe_do_shortcode($text, $widget = null) |
|
347 | + public function maybe_do_shortcode( $text, $widget = null ) |
|
348 | 348 | { |
349 | - if (!empty($this->shortcode_name) && has_shortcode($text, $this->shortcode_name)) { |
|
350 | - return do_shortcode($text); |
|
349 | + if ( ! empty( $this->shortcode_name ) && has_shortcode( $text, $this->shortcode_name ) ) { |
|
350 | + return do_shortcode( $text ); |
|
351 | 351 | } |
352 | 352 | |
353 | 353 | return $text; |
@@ -360,35 +360,35 @@ discard block |
||
360 | 360 | */ |
361 | 361 | public function add_shortcode() |
362 | 362 | { |
363 | - if (empty($this->shortcode_name)) { |
|
363 | + if ( empty( $this->shortcode_name ) ) { |
|
364 | 364 | return; |
365 | 365 | } |
366 | 366 | |
367 | - if (!gravityview()->plugin->is_compatible()) { |
|
367 | + if ( ! gravityview()->plugin->is_compatible() ) { |
|
368 | 368 | return; |
369 | 369 | } |
370 | 370 | |
371 | - if (gravityview()->request->is_admin()) { |
|
371 | + if ( gravityview()->request->is_admin() ) { |
|
372 | 372 | return; |
373 | 373 | } |
374 | 374 | |
375 | 375 | // If the widget shouldn't output on single entries, don't show it |
376 | - if (empty($this->show_on_single) && gravityview()->request->is_entry()) { |
|
377 | - gravityview()->log->debug('Skipping; set to not run on single entry.'); |
|
378 | - add_shortcode($this->shortcode_name, '__return_null'); |
|
376 | + if ( empty( $this->show_on_single ) && gravityview()->request->is_entry() ) { |
|
377 | + gravityview()->log->debug( 'Skipping; set to not run on single entry.' ); |
|
378 | + add_shortcode( $this->shortcode_name, '__return_null' ); |
|
379 | 379 | |
380 | 380 | return; |
381 | 381 | } |
382 | 382 | |
383 | 383 | global $post; |
384 | 384 | |
385 | - if (!is_object($post) || empty($post->post_content) || !Shortcode::parse($post->post_content)) { |
|
386 | - add_shortcode($this->shortcode_name, '__return_null'); |
|
385 | + if ( ! is_object( $post ) || empty( $post->post_content ) || ! Shortcode::parse( $post->post_content ) ) { |
|
386 | + add_shortcode( $this->shortcode_name, '__return_null' ); |
|
387 | 387 | |
388 | 388 | return; |
389 | 389 | } |
390 | 390 | |
391 | - add_shortcode($this->shortcode_name, [$this, 'render_shortcode']); |
|
391 | + add_shortcode( $this->shortcode_name, [ $this, 'render_shortcode' ] ); |
|
392 | 392 | } |
393 | 393 | |
394 | 394 | /** |
@@ -402,7 +402,7 @@ discard block |
||
402 | 402 | * |
403 | 403 | * @return void |
404 | 404 | */ |
405 | - public function render_frontend($widget_args, $content = '', $context = '') |
|
405 | + public function render_frontend( $widget_args, $content = '', $context = '' ) |
|
406 | 406 | { |
407 | 407 | } |
408 | 408 | |
@@ -426,7 +426,7 @@ discard block |
||
426 | 426 | /** |
427 | 427 | * @deprecated 2.14 In favor of allowlist. |
428 | 428 | */ |
429 | - $allowlist = apply_filters_deprecated('gravityview/widget/hide_until_searched/whitelist', [$allowlist], '2.14', 'gravityview/widget/hide_until_searched/allowlist'); |
|
429 | + $allowlist = apply_filters_deprecated( 'gravityview/widget/hide_until_searched/whitelist', [ $allowlist ], '2.14', 'gravityview/widget/hide_until_searched/allowlist' ); |
|
430 | 430 | |
431 | 431 | /** |
432 | 432 | * @filter `gravityview/widget/hide_until_searched/allowlist` Some widgets have got to stay shown. |
@@ -435,10 +435,10 @@ discard block |
||
435 | 435 | * |
436 | 436 | * @param string[] $allowlist The widget IDs that have to be shown by default. |
437 | 437 | */ |
438 | - $allowlist = apply_filters('gravityview/widget/hide_until_searched/allowlist', $allowlist); |
|
438 | + $allowlist = apply_filters( 'gravityview/widget/hide_until_searched/allowlist', $allowlist ); |
|
439 | 439 | |
440 | - if (($view = gravityview()->views->get()) && !in_array($this->get_widget_id(), $allowlist)) { |
|
441 | - $hide_until_searched = $view->settings->get('hide_until_searched'); |
|
440 | + if ( ( $view = gravityview()->views->get() ) && ! in_array( $this->get_widget_id(), $allowlist ) ) { |
|
441 | + $hide_until_searched = $view->settings->get( 'hide_until_searched' ); |
|
442 | 442 | } else { |
443 | 443 | $hide_until_searched = false; |
444 | 444 | } |
@@ -449,10 +449,10 @@ discard block |
||
449 | 449 | * @param bool $hide_until_searched Hide until search? |
450 | 450 | * @param \GV\Widget $this Widget instance |
451 | 451 | */ |
452 | - $hide_until_searched = apply_filters('gravityview/widget/hide_until_searched', $hide_until_searched, $this); |
|
452 | + $hide_until_searched = apply_filters( 'gravityview/widget/hide_until_searched', $hide_until_searched, $this ); |
|
453 | 453 | |
454 | - if ($hide_until_searched && !gravityview()->request->is_search()) { |
|
455 | - gravityview()->log->debug('Hide View data until search is performed'); |
|
454 | + if ( $hide_until_searched && ! gravityview()->request->is_search() ) { |
|
455 | + gravityview()->log->debug( 'Hide View data until search is performed' ); |
|
456 | 456 | |
457 | 457 | return false; |
458 | 458 | } |
@@ -469,10 +469,10 @@ discard block |
||
469 | 469 | * |
470 | 470 | * @return string Whatever the widget echoed. |
471 | 471 | */ |
472 | - public function render_shortcode($atts, $content = '', $context = '') |
|
472 | + public function render_shortcode( $atts, $content = '', $context = '' ) |
|
473 | 473 | { |
474 | 474 | ob_start(); |
475 | - $this->render_frontend($atts, $content, $context); |
|
475 | + $this->render_frontend( $atts, $content, $context ); |
|
476 | 476 | |
477 | 477 | return ob_get_clean(); |
478 | 478 | } |
@@ -490,24 +490,24 @@ discard block |
||
490 | 490 | * |
491 | 491 | * @return \GV\Widget|null The widget implementation from configuration or none. |
492 | 492 | */ |
493 | - public static function from_configuration($configuration) |
|
493 | + public static function from_configuration( $configuration ) |
|
494 | 494 | { |
495 | 495 | $registered_widgets = self::registered(); |
496 | 496 | |
497 | - if (!$id = Utils::get($configuration, 'id')) { |
|
497 | + if ( ! $id = Utils::get( $configuration, 'id' ) ) { |
|
498 | 498 | return null; |
499 | 499 | } |
500 | 500 | |
501 | - if (!$widget = Utils::get($registered_widgets, $id)) { |
|
501 | + if ( ! $widget = Utils::get( $registered_widgets, $id ) ) { |
|
502 | 502 | return null; |
503 | 503 | } |
504 | 504 | |
505 | - if (!class_exists($class = Utils::get($widget, 'class'))) { |
|
505 | + if ( ! class_exists( $class = Utils::get( $widget, 'class' ) ) ) { |
|
506 | 506 | return null; |
507 | 507 | } |
508 | 508 | |
509 | - $w = new $class(Utils::get($widget, 'label'), $id); |
|
510 | - $w->configuration = new Settings($configuration); |
|
509 | + $w = new $class( Utils::get( $widget, 'label' ), $id ); |
|
510 | + $w->configuration = new Settings( $configuration ); |
|
511 | 511 | |
512 | 512 | return $w; |
513 | 513 | } |
@@ -526,9 +526,9 @@ discard block |
||
526 | 526 | */ |
527 | 527 | public function as_configuration() |
528 | 528 | { |
529 | - return array_merge([ |
|
529 | + return array_merge( [ |
|
530 | 530 | 'id' => $this->get_widget_id(), |
531 | - ], $this->configuration->all()); |
|
531 | + ], $this->configuration->all() ); |
|
532 | 532 | } |
533 | 533 | |
534 | 534 | /** |
@@ -549,14 +549,14 @@ discard block |
||
549 | 549 | * |
550 | 550 | * @param array $registered_widgets Empty array |
551 | 551 | */ |
552 | - $registered_widgets = apply_filters('gravityview_register_directory_widgets', []); |
|
552 | + $registered_widgets = apply_filters( 'gravityview_register_directory_widgets', [ ] ); |
|
553 | 553 | |
554 | 554 | /** |
555 | 555 | * @filter `gravityview/widgets/register` Each item is used to instantiate a GravityView_Admin_View_Widget object |
556 | 556 | * |
557 | 557 | * @param array $registered_widgets Empty array |
558 | 558 | */ |
559 | - return apply_filters('gravityview/widgets/register', $registered_widgets); |
|
559 | + return apply_filters( 'gravityview/widgets/register', $registered_widgets ); |
|
560 | 560 | } |
561 | 561 | |
562 | 562 | /** |
@@ -570,12 +570,12 @@ discard block |
||
570 | 570 | */ |
571 | 571 | public function is_registered() |
572 | 572 | { |
573 | - if (!$widget_id = $this->get_widget_id()) { |
|
574 | - gravityview()->log->warning('Widget ID not set before calling Widget::is_registered', ['data' => $this]); |
|
573 | + if ( ! $widget_id = $this->get_widget_id() ) { |
|
574 | + gravityview()->log->warning( 'Widget ID not set before calling Widget::is_registered', [ 'data' => $this ] ); |
|
575 | 575 | |
576 | 576 | return false; |
577 | 577 | } |
578 | 578 | |
579 | - return in_array($widget_id, array_keys(self::registered()), true); |
|
579 | + return in_array( $widget_id, array_keys( self::registered() ), true ); |
|
580 | 580 | } |
581 | 581 | } |
@@ -12,8 +12,7 @@ discard block |
||
12 | 12 | * |
13 | 13 | * An interface that most GravityView widgets would want to adhere to and inherit from. |
14 | 14 | */ |
15 | -abstract class Widget |
|
16 | -{ |
|
15 | +abstract class Widget { |
|
17 | 16 | /** |
18 | 17 | * Widget admin label. |
19 | 18 | * |
@@ -128,8 +127,7 @@ discard block |
||
128 | 127 | * |
129 | 128 | * @return \GV\Widget |
130 | 129 | */ |
131 | - public function __construct($label, $id, $defaults = [], $settings = []) |
|
132 | - { |
|
130 | + public function __construct($label, $id, $defaults = [], $settings = []) { |
|
133 | 131 | /** |
134 | 132 | * The shortcode name is set to the lowercase name of the widget class, unless overridden by the class specifying a different value for $shortcode_name. |
135 | 133 | * |
@@ -176,8 +174,7 @@ discard block |
||
176 | 174 | * |
177 | 175 | * @return array $settings Default settings |
178 | 176 | */ |
179 | - protected function get_default_settings() |
|
180 | - { |
|
177 | + protected function get_default_settings() { |
|
181 | 178 | $settings = []; |
182 | 179 | |
183 | 180 | /** |
@@ -207,8 +204,7 @@ discard block |
||
207 | 204 | * |
208 | 205 | * @return string The Widget ID. |
209 | 206 | */ |
210 | - public function get_widget_id() |
|
211 | - { |
|
207 | + public function get_widget_id() { |
|
212 | 208 | return $this->widget_id; |
213 | 209 | } |
214 | 210 | |
@@ -217,8 +213,7 @@ discard block |
||
217 | 213 | * |
218 | 214 | * @return array|null Settings array; NULL if not set for some reason. |
219 | 215 | */ |
220 | - public function get_settings() |
|
221 | - { |
|
216 | + public function get_settings() { |
|
222 | 217 | return empty($this->settings) ? null : $this->settings; |
223 | 218 | } |
224 | 219 | |
@@ -231,8 +226,7 @@ discard block |
||
231 | 226 | * |
232 | 227 | * @return mixed|null Value of the setting; NULL if not set |
233 | 228 | */ |
234 | - public function get_setting($key) |
|
235 | - { |
|
229 | + public function get_setting($key) { |
|
236 | 230 | return Utils::get($this->settings, $key, null); |
237 | 231 | } |
238 | 232 | |
@@ -243,8 +237,7 @@ discard block |
||
243 | 237 | * |
244 | 238 | * @return array The default areas where widgets can be rendered. |
245 | 239 | */ |
246 | - public static function get_default_widget_areas() |
|
247 | - { |
|
240 | + public static function get_default_widget_areas() { |
|
248 | 241 | $default_areas = [ |
249 | 242 | [ |
250 | 243 | '1-1' => [ |
@@ -299,8 +292,7 @@ discard block |
||
299 | 292 | * |
300 | 293 | * @return array $widgets |
301 | 294 | */ |
302 | - public function register_widget($widgets) |
|
303 | - { |
|
295 | + public function register_widget($widgets) { |
|
304 | 296 | if (!is_array($widgets)) { |
305 | 297 | $widgets = []; |
306 | 298 | } |
@@ -325,8 +317,7 @@ discard block |
||
325 | 317 | * |
326 | 318 | * @return array |
327 | 319 | */ |
328 | - public function assign_widget_options($options = [], $template = '', $widget = '') |
|
329 | - { |
|
320 | + public function assign_widget_options($options = [], $template = '', $widget = '') { |
|
330 | 321 | if ($this->get_widget_id() === $widget) { |
331 | 322 | if ($settings = $this->get_settings()) { |
332 | 323 | $options = array_merge($options, $settings); |
@@ -344,8 +335,7 @@ discard block |
||
344 | 335 | * |
345 | 336 | * @return string Widget text |
346 | 337 | */ |
347 | - public function maybe_do_shortcode($text, $widget = null) |
|
348 | - { |
|
338 | + public function maybe_do_shortcode($text, $widget = null) { |
|
349 | 339 | if (!empty($this->shortcode_name) && has_shortcode($text, $this->shortcode_name)) { |
350 | 340 | return do_shortcode($text); |
351 | 341 | } |
@@ -358,8 +348,7 @@ discard block |
||
358 | 348 | * |
359 | 349 | * @return void |
360 | 350 | */ |
361 | - public function add_shortcode() |
|
362 | - { |
|
351 | + public function add_shortcode() { |
|
363 | 352 | if (empty($this->shortcode_name)) { |
364 | 353 | return; |
365 | 354 | } |
@@ -402,8 +391,7 @@ discard block |
||
402 | 391 | * |
403 | 392 | * @return void |
404 | 393 | */ |
405 | - public function render_frontend($widget_args, $content = '', $context = '') |
|
406 | - { |
|
394 | + public function render_frontend($widget_args, $content = '', $context = '') { |
|
407 | 395 | } |
408 | 396 | |
409 | 397 | /** |
@@ -413,8 +401,7 @@ discard block |
||
413 | 401 | * |
414 | 402 | * @return bool True: render frontend; False: don't render frontend |
415 | 403 | */ |
416 | - public function pre_render_frontend() |
|
417 | - { |
|
404 | + public function pre_render_frontend() { |
|
418 | 405 | |
419 | 406 | /** |
420 | 407 | * Assume shown regardless of hide_until_search setting. |
@@ -469,8 +456,7 @@ discard block |
||
469 | 456 | * |
470 | 457 | * @return string Whatever the widget echoed. |
471 | 458 | */ |
472 | - public function render_shortcode($atts, $content = '', $context = '') |
|
473 | - { |
|
459 | + public function render_shortcode($atts, $content = '', $context = '') { |
|
474 | 460 | ob_start(); |
475 | 461 | $this->render_frontend($atts, $content, $context); |
476 | 462 | |
@@ -490,8 +476,7 @@ discard block |
||
490 | 476 | * |
491 | 477 | * @return \GV\Widget|null The widget implementation from configuration or none. |
492 | 478 | */ |
493 | - public static function from_configuration($configuration) |
|
494 | - { |
|
479 | + public static function from_configuration($configuration) { |
|
495 | 480 | $registered_widgets = self::registered(); |
496 | 481 | |
497 | 482 | if (!$id = Utils::get($configuration, 'id')) { |
@@ -524,8 +509,7 @@ discard block |
||
524 | 509 | * |
525 | 510 | * @return array |
526 | 511 | */ |
527 | - public function as_configuration() |
|
528 | - { |
|
512 | + public function as_configuration() { |
|
529 | 513 | return array_merge([ |
530 | 514 | 'id' => $this->get_widget_id(), |
531 | 515 | ], $this->configuration->all()); |
@@ -540,8 +524,7 @@ discard block |
||
540 | 524 | * |
541 | 525 | * @return array |
542 | 526 | */ |
543 | - public static function registered() |
|
544 | - { |
|
527 | + public static function registered() { |
|
545 | 528 | /** |
546 | 529 | * @filter `gravityview_register_directory_widgets` Get the list of registered widgets. Each item is used to instantiate a GravityView_Admin_View_Widget object |
547 | 530 | * |
@@ -568,8 +551,7 @@ discard block |
||
568 | 551 | * |
569 | 552 | * @return bool |
570 | 553 | */ |
571 | - public function is_registered() |
|
572 | - { |
|
554 | + public function is_registered() { |
|
573 | 555 | if (!$widget_id = $this->get_widget_id()) { |
574 | 556 | gravityview()->log->warning('Widget ID not set before calling Widget::is_registered', ['data' => $this]); |
575 | 557 |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | |
5 | 5 | /** If this file is called directly, abort. */ |
6 | 6 | if (!defined('GRAVITYVIEW_DIR')) { |
7 | - exit(); |
|
7 | + exit(); |
|
8 | 8 | } |
9 | 9 | |
10 | 10 | /** |
@@ -12,410 +12,410 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class Entry_Collection extends Collection |
14 | 14 | { |
15 | - /** |
|
16 | - * Lazy fetching and counting of data defers |
|
17 | - * all processing of entries and entry data until |
|
18 | - * it is really requested. |
|
19 | - * |
|
20 | - * @see \GV\Entry_Collection::add_fetch_callback |
|
21 | - * @see \GV\Entry_Collection::add_count_callback |
|
22 | - * |
|
23 | - * @var array Lazy data loading callbacks. |
|
24 | - */ |
|
25 | - private $callbacks = []; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var \GV\Entry_Filter[] Filtering criteria. |
|
29 | - */ |
|
30 | - public $filters = []; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var \GV\Entry_Sort[] Sorting criteria. |
|
34 | - */ |
|
35 | - public $sorts = []; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var int The offset. |
|
39 | - */ |
|
40 | - public $offset = 0; |
|
41 | - |
|
42 | - /** |
|
43 | - * @var int The limit. |
|
44 | - */ |
|
45 | - public $limit = 20; |
|
46 | - |
|
47 | - /** |
|
48 | - * @var int The current page. |
|
49 | - */ |
|
50 | - public $current_page = 1; |
|
51 | - |
|
52 | - /** |
|
53 | - * @var int The number of entries fetched. |
|
54 | - */ |
|
55 | - private $fetched = -1; |
|
56 | - |
|
57 | - /** |
|
58 | - * Add an \GV\Entry to this collection. |
|
59 | - * |
|
60 | - * @param \GV\Entry $entry The entry to add to the internal array. |
|
61 | - * |
|
62 | - * @api |
|
63 | - * |
|
64 | - * @since 2.0 |
|
65 | - * |
|
66 | - * @return void |
|
67 | - */ |
|
68 | - public function add($entry) |
|
69 | - { |
|
70 | - if (!$entry instanceof Entry) { |
|
71 | - $this->fetched = max(0, $this->fetched); |
|
72 | - gravityview()->log->error('Entry_Collections can only contain objects of type \GV\Entry.'); |
|
73 | - |
|
74 | - return; |
|
75 | - } |
|
76 | - parent::add($entry); |
|
77 | - $this->fetched = max(1, $this->fetched + 1); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Get a \GV\Entry from this list. |
|
82 | - * |
|
83 | - * @param int $entry_id The ID of the entry to get. |
|
84 | - * @param string $backend The form backend identifier, allows for multiple form backends in the future. Unused until then. |
|
85 | - * |
|
86 | - * @api |
|
87 | - * |
|
88 | - * @since 2.0 |
|
89 | - * |
|
90 | - * @return \GV\Entry|null The \GV\entry with the $entry_id as the ID, or null if not found. |
|
91 | - */ |
|
92 | - public function get($entry_id, $backend = 'gravityforms') |
|
93 | - { |
|
94 | - foreach ($this->all() as $entry) { |
|
95 | - if ($entry->ID == $entry_id) { |
|
96 | - return $entry; |
|
97 | - } |
|
98 | - } |
|
99 | - |
|
100 | - return null; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Count the total number of \GV\Entry objects that are possible to get. |
|
105 | - * |
|
106 | - * @api |
|
107 | - * |
|
108 | - * @since 2.0 |
|
109 | - * |
|
110 | - * @return int The total number of entries that are fetchable. |
|
111 | - */ |
|
112 | - public function total() |
|
113 | - { |
|
114 | - $total = 0; |
|
115 | - |
|
116 | - /** Call all lazy callbacks. */ |
|
117 | - foreach ($this->callbacks as $callback) { |
|
118 | - if ($callback[0] != 'count') { |
|
119 | - continue; |
|
120 | - } |
|
121 | - |
|
122 | - $total += $callback[1]($this->filters); |
|
123 | - } |
|
124 | - |
|
125 | - if (!$total) { |
|
126 | - $total = parent::count(); |
|
127 | - } |
|
128 | - |
|
129 | - return $total - $this->offset; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Get the entries as an array. |
|
134 | - * |
|
135 | - * @api |
|
136 | - * |
|
137 | - * @since 2.0 |
|
138 | - * |
|
139 | - * @return \GV\Entry[] The entries as an array. |
|
140 | - */ |
|
141 | - public function all() |
|
142 | - { |
|
143 | - if ($this->fetched >= 0 || parent::count()) { |
|
144 | - return parent::all(); |
|
145 | - } |
|
146 | - |
|
147 | - return $this->fetch()->all(); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Pluck by key. |
|
152 | - * |
|
153 | - * @api |
|
154 | - * |
|
155 | - * @since develop |
|
156 | - * |
|
157 | - * @param string $key The key to pluck by. |
|
158 | - * |
|
159 | - * @return array The plucked values. |
|
160 | - */ |
|
161 | - public function pluck($key) |
|
162 | - { |
|
163 | - $result = []; |
|
164 | - |
|
165 | - foreach ($this->all() as $entry) { |
|
166 | - $entry = $entry->as_entry(); |
|
167 | - $result[] = Utils::get($entry, $key, null); |
|
168 | - } |
|
169 | - |
|
170 | - return $result; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Get the last \GV\Entry in this collection. |
|
175 | - * |
|
176 | - * @api |
|
177 | - * |
|
178 | - * @since 2.0 |
|
179 | - * |
|
180 | - * @return \GV\Entry|null The last entry or null. |
|
181 | - */ |
|
182 | - public function last() |
|
183 | - { |
|
184 | - if ($this->fetched >= 0 || parent::count()) { |
|
185 | - return parent::last(); |
|
186 | - } |
|
187 | - |
|
188 | - return $this->fetch()->last(); |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Get the first \GV\Entry in this collection. |
|
193 | - * |
|
194 | - * @api |
|
195 | - * |
|
196 | - * @since 2.0 |
|
197 | - * |
|
198 | - * @return \GV\Entry|null The first entry or null. |
|
199 | - */ |
|
200 | - public function first() |
|
201 | - { |
|
202 | - if ($this->fetched >= 0 || parent::count()) { |
|
203 | - return parent::first(); |
|
204 | - } |
|
205 | - |
|
206 | - return $this->fetch()->first(); |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Hydrate this collection now. |
|
211 | - * |
|
212 | - * @api |
|
213 | - * |
|
214 | - * @since 2.0 |
|
215 | - * |
|
216 | - * @return \GV\Entry_Collection This collection, now hydrated. |
|
217 | - */ |
|
218 | - public function fetch() |
|
219 | - { |
|
220 | - if ($this->fetched >= 0) { |
|
221 | - return $this; |
|
222 | - } |
|
223 | - |
|
224 | - $this->clear(); |
|
225 | - |
|
226 | - /** Calculate the offsets. */ |
|
227 | - $offset = new \GV\Entry_Offset(); |
|
228 | - $offset->limit = $this->limit; |
|
229 | - $offset->offset = ($this->limit * ($this->current_page - 1)) + $this->offset; |
|
230 | - |
|
231 | - /** Call all lazy callbacks. */ |
|
232 | - foreach ($this->callbacks as $i => $callback) { |
|
233 | - if ($callback[0] != 'fetch') { |
|
234 | - continue; |
|
235 | - } |
|
236 | - |
|
237 | - $this->merge($callback[1]($this->filters, $this->sorts, $offset)); |
|
238 | - } |
|
239 | - |
|
240 | - $this->fetched = parent::count(); |
|
241 | - |
|
242 | - return $this; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Apply a filter to the current collection. |
|
247 | - * |
|
248 | - * This operation is non-destructive as a copy of the collection is returned. |
|
249 | - * |
|
250 | - * @param \GV\Entry_Filter $filter The filter to be applied. |
|
251 | - * |
|
252 | - * @api |
|
253 | - * |
|
254 | - * @since 2.0 |
|
255 | - * |
|
256 | - * @return \GV\Entry_Collection A copy of the this collection with the filter applied. |
|
257 | - */ |
|
258 | - public function filter(\GV\Entry_Filter $filter) |
|
259 | - { |
|
260 | - $collection = clone $this; |
|
261 | - $collection->clear(); |
|
262 | - |
|
263 | - array_push($collection->filters, $filter); |
|
264 | - |
|
265 | - return $collection; |
|
266 | - } |
|
267 | - |
|
268 | - /** |
|
269 | - * Sort. |
|
270 | - * |
|
271 | - * @param \GV\Entry_Sort $sort The sort to apply to this collection. |
|
272 | - * |
|
273 | - * @api |
|
274 | - * |
|
275 | - * @since 2.0 |
|
276 | - * |
|
277 | - * @return \GV\Entry_Collection A copy of the this collection with the sort applied. |
|
278 | - */ |
|
279 | - public function sort($sort) |
|
280 | - { |
|
281 | - $collection = clone $this; |
|
282 | - $collection->clear(); |
|
283 | - |
|
284 | - array_push($collection->sorts, $sort); |
|
285 | - |
|
286 | - return $collection; |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * Limit the fetch to a specified window. |
|
291 | - * |
|
292 | - * @param int $limit The limit. |
|
293 | - * |
|
294 | - * @api |
|
295 | - * |
|
296 | - * @since 2.0 |
|
297 | - * |
|
298 | - * @return \GV\Entry_Collection A copy of the this collection with the limit applied. |
|
299 | - */ |
|
300 | - public function limit($limit) |
|
301 | - { |
|
302 | - $collection = clone $this; |
|
303 | - $collection->clear(); |
|
304 | - $collection->limit = $limit; |
|
305 | - |
|
306 | - return $collection; |
|
307 | - } |
|
308 | - |
|
309 | - /** |
|
310 | - * Add an $offset to these entries. |
|
311 | - * |
|
312 | - * Useful, you know, for pagination and stuff. Not too useful directly. |
|
313 | - * |
|
314 | - * @see \GV\Entry_Collection::page() |
|
315 | - * |
|
316 | - * @param int $offset The number of entries to skip in the database. |
|
317 | - * |
|
318 | - * @api |
|
319 | - * |
|
320 | - * @since 2.0 |
|
321 | - * |
|
322 | - * @return \GV\Entry_Collection A copy of the this collection with the offset applied. |
|
323 | - */ |
|
324 | - public function offset($offset) |
|
325 | - { |
|
326 | - $collection = clone $this; |
|
327 | - $collection->clear(); |
|
328 | - $collection->offset = $offset; |
|
329 | - |
|
330 | - return $collection; |
|
331 | - } |
|
332 | - |
|
333 | - /** |
|
334 | - * Set the current page. |
|
335 | - * |
|
336 | - * @param int $page Set the current page to this page. Ends up agumenting the $offset in \GV\Entry_Offset |
|
337 | - * |
|
338 | - * @return \GV\Entry_Collection A copy of the this collection with the offset applied. |
|
339 | - */ |
|
340 | - public function page($page) |
|
341 | - { |
|
342 | - $collection = clone $this; |
|
343 | - $collection->clear(); |
|
344 | - $collection->current_page = $page; |
|
345 | - |
|
346 | - return $collection; |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * Defer fetching of data to the provided callable. |
|
351 | - * |
|
352 | - * The callback signature should be as follows: |
|
353 | - * \GV\Entry_Collection callback( \GV\Entry_Filter $filter, \GV\Entry_Sort $sort, \GV\Entry_Offset $offset ); |
|
354 | - * |
|
355 | - * The methods that trigger the callback are: |
|
356 | - * - \GV\Entry_Collection::fetch |
|
357 | - * |
|
358 | - * ::fetch is triggered via: |
|
359 | - * - \GV\Entry_Collection::all |
|
360 | - * - \GV\Entry_Collection::last |
|
361 | - * |
|
362 | - * @param callable $callback The callback to call when needed. |
|
363 | - * |
|
364 | - * @internal |
|
365 | - * |
|
366 | - * @since 2.0 |
|
367 | - * |
|
368 | - * @return void |
|
369 | - */ |
|
370 | - public function add_fetch_callback($callback) |
|
371 | - { |
|
372 | - $this->add_callback('fetch', $callback); |
|
373 | - } |
|
374 | - |
|
375 | - /** |
|
376 | - * Defer counting of data to the provided callable. |
|
377 | - * |
|
378 | - * The callback signature should be as follows: |
|
379 | - * int callback( \GV\Entry_Filter $filter ); |
|
380 | - * |
|
381 | - * The methods that trigger the callback are: |
|
382 | - * - \GV\Entry_Collection::count |
|
383 | - * |
|
384 | - * @param callable $callback The callback to call when needed. |
|
385 | - * |
|
386 | - * @internal |
|
387 | - * |
|
388 | - * @since 2.0 |
|
389 | - * |
|
390 | - * @return void |
|
391 | - */ |
|
392 | - public function add_count_callback($callback) |
|
393 | - { |
|
394 | - $this->add_callback('count', $callback); |
|
395 | - } |
|
396 | - |
|
397 | - /** |
|
398 | - * Add a callback for lazy loading/counting. |
|
399 | - * |
|
400 | - * @param callable $callback The callback to call when needed. |
|
401 | - * |
|
402 | - * @return void |
|
403 | - */ |
|
404 | - private function add_callback($type, $callback) |
|
405 | - { |
|
406 | - if (!is_callable($callback)) { |
|
407 | - return; |
|
408 | - } |
|
409 | - |
|
410 | - $this->callbacks[] = [$type, $callback]; |
|
411 | - } |
|
412 | - |
|
413 | - /** |
|
414 | - * @inheritdoc |
|
415 | - */ |
|
416 | - public function clear() |
|
417 | - { |
|
418 | - $this->fetched = -1; |
|
419 | - parent::clear(); |
|
420 | - } |
|
15 | + /** |
|
16 | + * Lazy fetching and counting of data defers |
|
17 | + * all processing of entries and entry data until |
|
18 | + * it is really requested. |
|
19 | + * |
|
20 | + * @see \GV\Entry_Collection::add_fetch_callback |
|
21 | + * @see \GV\Entry_Collection::add_count_callback |
|
22 | + * |
|
23 | + * @var array Lazy data loading callbacks. |
|
24 | + */ |
|
25 | + private $callbacks = []; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var \GV\Entry_Filter[] Filtering criteria. |
|
29 | + */ |
|
30 | + public $filters = []; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var \GV\Entry_Sort[] Sorting criteria. |
|
34 | + */ |
|
35 | + public $sorts = []; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var int The offset. |
|
39 | + */ |
|
40 | + public $offset = 0; |
|
41 | + |
|
42 | + /** |
|
43 | + * @var int The limit. |
|
44 | + */ |
|
45 | + public $limit = 20; |
|
46 | + |
|
47 | + /** |
|
48 | + * @var int The current page. |
|
49 | + */ |
|
50 | + public $current_page = 1; |
|
51 | + |
|
52 | + /** |
|
53 | + * @var int The number of entries fetched. |
|
54 | + */ |
|
55 | + private $fetched = -1; |
|
56 | + |
|
57 | + /** |
|
58 | + * Add an \GV\Entry to this collection. |
|
59 | + * |
|
60 | + * @param \GV\Entry $entry The entry to add to the internal array. |
|
61 | + * |
|
62 | + * @api |
|
63 | + * |
|
64 | + * @since 2.0 |
|
65 | + * |
|
66 | + * @return void |
|
67 | + */ |
|
68 | + public function add($entry) |
|
69 | + { |
|
70 | + if (!$entry instanceof Entry) { |
|
71 | + $this->fetched = max(0, $this->fetched); |
|
72 | + gravityview()->log->error('Entry_Collections can only contain objects of type \GV\Entry.'); |
|
73 | + |
|
74 | + return; |
|
75 | + } |
|
76 | + parent::add($entry); |
|
77 | + $this->fetched = max(1, $this->fetched + 1); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Get a \GV\Entry from this list. |
|
82 | + * |
|
83 | + * @param int $entry_id The ID of the entry to get. |
|
84 | + * @param string $backend The form backend identifier, allows for multiple form backends in the future. Unused until then. |
|
85 | + * |
|
86 | + * @api |
|
87 | + * |
|
88 | + * @since 2.0 |
|
89 | + * |
|
90 | + * @return \GV\Entry|null The \GV\entry with the $entry_id as the ID, or null if not found. |
|
91 | + */ |
|
92 | + public function get($entry_id, $backend = 'gravityforms') |
|
93 | + { |
|
94 | + foreach ($this->all() as $entry) { |
|
95 | + if ($entry->ID == $entry_id) { |
|
96 | + return $entry; |
|
97 | + } |
|
98 | + } |
|
99 | + |
|
100 | + return null; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Count the total number of \GV\Entry objects that are possible to get. |
|
105 | + * |
|
106 | + * @api |
|
107 | + * |
|
108 | + * @since 2.0 |
|
109 | + * |
|
110 | + * @return int The total number of entries that are fetchable. |
|
111 | + */ |
|
112 | + public function total() |
|
113 | + { |
|
114 | + $total = 0; |
|
115 | + |
|
116 | + /** Call all lazy callbacks. */ |
|
117 | + foreach ($this->callbacks as $callback) { |
|
118 | + if ($callback[0] != 'count') { |
|
119 | + continue; |
|
120 | + } |
|
121 | + |
|
122 | + $total += $callback[1]($this->filters); |
|
123 | + } |
|
124 | + |
|
125 | + if (!$total) { |
|
126 | + $total = parent::count(); |
|
127 | + } |
|
128 | + |
|
129 | + return $total - $this->offset; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Get the entries as an array. |
|
134 | + * |
|
135 | + * @api |
|
136 | + * |
|
137 | + * @since 2.0 |
|
138 | + * |
|
139 | + * @return \GV\Entry[] The entries as an array. |
|
140 | + */ |
|
141 | + public function all() |
|
142 | + { |
|
143 | + if ($this->fetched >= 0 || parent::count()) { |
|
144 | + return parent::all(); |
|
145 | + } |
|
146 | + |
|
147 | + return $this->fetch()->all(); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Pluck by key. |
|
152 | + * |
|
153 | + * @api |
|
154 | + * |
|
155 | + * @since develop |
|
156 | + * |
|
157 | + * @param string $key The key to pluck by. |
|
158 | + * |
|
159 | + * @return array The plucked values. |
|
160 | + */ |
|
161 | + public function pluck($key) |
|
162 | + { |
|
163 | + $result = []; |
|
164 | + |
|
165 | + foreach ($this->all() as $entry) { |
|
166 | + $entry = $entry->as_entry(); |
|
167 | + $result[] = Utils::get($entry, $key, null); |
|
168 | + } |
|
169 | + |
|
170 | + return $result; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Get the last \GV\Entry in this collection. |
|
175 | + * |
|
176 | + * @api |
|
177 | + * |
|
178 | + * @since 2.0 |
|
179 | + * |
|
180 | + * @return \GV\Entry|null The last entry or null. |
|
181 | + */ |
|
182 | + public function last() |
|
183 | + { |
|
184 | + if ($this->fetched >= 0 || parent::count()) { |
|
185 | + return parent::last(); |
|
186 | + } |
|
187 | + |
|
188 | + return $this->fetch()->last(); |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Get the first \GV\Entry in this collection. |
|
193 | + * |
|
194 | + * @api |
|
195 | + * |
|
196 | + * @since 2.0 |
|
197 | + * |
|
198 | + * @return \GV\Entry|null The first entry or null. |
|
199 | + */ |
|
200 | + public function first() |
|
201 | + { |
|
202 | + if ($this->fetched >= 0 || parent::count()) { |
|
203 | + return parent::first(); |
|
204 | + } |
|
205 | + |
|
206 | + return $this->fetch()->first(); |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Hydrate this collection now. |
|
211 | + * |
|
212 | + * @api |
|
213 | + * |
|
214 | + * @since 2.0 |
|
215 | + * |
|
216 | + * @return \GV\Entry_Collection This collection, now hydrated. |
|
217 | + */ |
|
218 | + public function fetch() |
|
219 | + { |
|
220 | + if ($this->fetched >= 0) { |
|
221 | + return $this; |
|
222 | + } |
|
223 | + |
|
224 | + $this->clear(); |
|
225 | + |
|
226 | + /** Calculate the offsets. */ |
|
227 | + $offset = new \GV\Entry_Offset(); |
|
228 | + $offset->limit = $this->limit; |
|
229 | + $offset->offset = ($this->limit * ($this->current_page - 1)) + $this->offset; |
|
230 | + |
|
231 | + /** Call all lazy callbacks. */ |
|
232 | + foreach ($this->callbacks as $i => $callback) { |
|
233 | + if ($callback[0] != 'fetch') { |
|
234 | + continue; |
|
235 | + } |
|
236 | + |
|
237 | + $this->merge($callback[1]($this->filters, $this->sorts, $offset)); |
|
238 | + } |
|
239 | + |
|
240 | + $this->fetched = parent::count(); |
|
241 | + |
|
242 | + return $this; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Apply a filter to the current collection. |
|
247 | + * |
|
248 | + * This operation is non-destructive as a copy of the collection is returned. |
|
249 | + * |
|
250 | + * @param \GV\Entry_Filter $filter The filter to be applied. |
|
251 | + * |
|
252 | + * @api |
|
253 | + * |
|
254 | + * @since 2.0 |
|
255 | + * |
|
256 | + * @return \GV\Entry_Collection A copy of the this collection with the filter applied. |
|
257 | + */ |
|
258 | + public function filter(\GV\Entry_Filter $filter) |
|
259 | + { |
|
260 | + $collection = clone $this; |
|
261 | + $collection->clear(); |
|
262 | + |
|
263 | + array_push($collection->filters, $filter); |
|
264 | + |
|
265 | + return $collection; |
|
266 | + } |
|
267 | + |
|
268 | + /** |
|
269 | + * Sort. |
|
270 | + * |
|
271 | + * @param \GV\Entry_Sort $sort The sort to apply to this collection. |
|
272 | + * |
|
273 | + * @api |
|
274 | + * |
|
275 | + * @since 2.0 |
|
276 | + * |
|
277 | + * @return \GV\Entry_Collection A copy of the this collection with the sort applied. |
|
278 | + */ |
|
279 | + public function sort($sort) |
|
280 | + { |
|
281 | + $collection = clone $this; |
|
282 | + $collection->clear(); |
|
283 | + |
|
284 | + array_push($collection->sorts, $sort); |
|
285 | + |
|
286 | + return $collection; |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * Limit the fetch to a specified window. |
|
291 | + * |
|
292 | + * @param int $limit The limit. |
|
293 | + * |
|
294 | + * @api |
|
295 | + * |
|
296 | + * @since 2.0 |
|
297 | + * |
|
298 | + * @return \GV\Entry_Collection A copy of the this collection with the limit applied. |
|
299 | + */ |
|
300 | + public function limit($limit) |
|
301 | + { |
|
302 | + $collection = clone $this; |
|
303 | + $collection->clear(); |
|
304 | + $collection->limit = $limit; |
|
305 | + |
|
306 | + return $collection; |
|
307 | + } |
|
308 | + |
|
309 | + /** |
|
310 | + * Add an $offset to these entries. |
|
311 | + * |
|
312 | + * Useful, you know, for pagination and stuff. Not too useful directly. |
|
313 | + * |
|
314 | + * @see \GV\Entry_Collection::page() |
|
315 | + * |
|
316 | + * @param int $offset The number of entries to skip in the database. |
|
317 | + * |
|
318 | + * @api |
|
319 | + * |
|
320 | + * @since 2.0 |
|
321 | + * |
|
322 | + * @return \GV\Entry_Collection A copy of the this collection with the offset applied. |
|
323 | + */ |
|
324 | + public function offset($offset) |
|
325 | + { |
|
326 | + $collection = clone $this; |
|
327 | + $collection->clear(); |
|
328 | + $collection->offset = $offset; |
|
329 | + |
|
330 | + return $collection; |
|
331 | + } |
|
332 | + |
|
333 | + /** |
|
334 | + * Set the current page. |
|
335 | + * |
|
336 | + * @param int $page Set the current page to this page. Ends up agumenting the $offset in \GV\Entry_Offset |
|
337 | + * |
|
338 | + * @return \GV\Entry_Collection A copy of the this collection with the offset applied. |
|
339 | + */ |
|
340 | + public function page($page) |
|
341 | + { |
|
342 | + $collection = clone $this; |
|
343 | + $collection->clear(); |
|
344 | + $collection->current_page = $page; |
|
345 | + |
|
346 | + return $collection; |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * Defer fetching of data to the provided callable. |
|
351 | + * |
|
352 | + * The callback signature should be as follows: |
|
353 | + * \GV\Entry_Collection callback( \GV\Entry_Filter $filter, \GV\Entry_Sort $sort, \GV\Entry_Offset $offset ); |
|
354 | + * |
|
355 | + * The methods that trigger the callback are: |
|
356 | + * - \GV\Entry_Collection::fetch |
|
357 | + * |
|
358 | + * ::fetch is triggered via: |
|
359 | + * - \GV\Entry_Collection::all |
|
360 | + * - \GV\Entry_Collection::last |
|
361 | + * |
|
362 | + * @param callable $callback The callback to call when needed. |
|
363 | + * |
|
364 | + * @internal |
|
365 | + * |
|
366 | + * @since 2.0 |
|
367 | + * |
|
368 | + * @return void |
|
369 | + */ |
|
370 | + public function add_fetch_callback($callback) |
|
371 | + { |
|
372 | + $this->add_callback('fetch', $callback); |
|
373 | + } |
|
374 | + |
|
375 | + /** |
|
376 | + * Defer counting of data to the provided callable. |
|
377 | + * |
|
378 | + * The callback signature should be as follows: |
|
379 | + * int callback( \GV\Entry_Filter $filter ); |
|
380 | + * |
|
381 | + * The methods that trigger the callback are: |
|
382 | + * - \GV\Entry_Collection::count |
|
383 | + * |
|
384 | + * @param callable $callback The callback to call when needed. |
|
385 | + * |
|
386 | + * @internal |
|
387 | + * |
|
388 | + * @since 2.0 |
|
389 | + * |
|
390 | + * @return void |
|
391 | + */ |
|
392 | + public function add_count_callback($callback) |
|
393 | + { |
|
394 | + $this->add_callback('count', $callback); |
|
395 | + } |
|
396 | + |
|
397 | + /** |
|
398 | + * Add a callback for lazy loading/counting. |
|
399 | + * |
|
400 | + * @param callable $callback The callback to call when needed. |
|
401 | + * |
|
402 | + * @return void |
|
403 | + */ |
|
404 | + private function add_callback($type, $callback) |
|
405 | + { |
|
406 | + if (!is_callable($callback)) { |
|
407 | + return; |
|
408 | + } |
|
409 | + |
|
410 | + $this->callbacks[] = [$type, $callback]; |
|
411 | + } |
|
412 | + |
|
413 | + /** |
|
414 | + * @inheritdoc |
|
415 | + */ |
|
416 | + public function clear() |
|
417 | + { |
|
418 | + $this->fetched = -1; |
|
419 | + parent::clear(); |
|
420 | + } |
|
421 | 421 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | namespace GV; |
4 | 4 | |
5 | 5 | /** If this file is called directly, abort. */ |
6 | -if (!defined('GRAVITYVIEW_DIR')) { |
|
6 | +if ( ! defined( 'GRAVITYVIEW_DIR' ) ) { |
|
7 | 7 | exit(); |
8 | 8 | } |
9 | 9 | |
@@ -22,17 +22,17 @@ discard block |
||
22 | 22 | * |
23 | 23 | * @var array Lazy data loading callbacks. |
24 | 24 | */ |
25 | - private $callbacks = []; |
|
25 | + private $callbacks = [ ]; |
|
26 | 26 | |
27 | 27 | /** |
28 | 28 | * @var \GV\Entry_Filter[] Filtering criteria. |
29 | 29 | */ |
30 | - public $filters = []; |
|
30 | + public $filters = [ ]; |
|
31 | 31 | |
32 | 32 | /** |
33 | 33 | * @var \GV\Entry_Sort[] Sorting criteria. |
34 | 34 | */ |
35 | - public $sorts = []; |
|
35 | + public $sorts = [ ]; |
|
36 | 36 | |
37 | 37 | /** |
38 | 38 | * @var int The offset. |
@@ -65,16 +65,16 @@ discard block |
||
65 | 65 | * |
66 | 66 | * @return void |
67 | 67 | */ |
68 | - public function add($entry) |
|
68 | + public function add( $entry ) |
|
69 | 69 | { |
70 | - if (!$entry instanceof Entry) { |
|
71 | - $this->fetched = max(0, $this->fetched); |
|
72 | - gravityview()->log->error('Entry_Collections can only contain objects of type \GV\Entry.'); |
|
70 | + if ( ! $entry instanceof Entry ) { |
|
71 | + $this->fetched = max( 0, $this->fetched ); |
|
72 | + gravityview()->log->error( 'Entry_Collections can only contain objects of type \GV\Entry.' ); |
|
73 | 73 | |
74 | 74 | return; |
75 | 75 | } |
76 | - parent::add($entry); |
|
77 | - $this->fetched = max(1, $this->fetched + 1); |
|
76 | + parent::add( $entry ); |
|
77 | + $this->fetched = max( 1, $this->fetched + 1 ); |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | /** |
@@ -89,10 +89,10 @@ discard block |
||
89 | 89 | * |
90 | 90 | * @return \GV\Entry|null The \GV\entry with the $entry_id as the ID, or null if not found. |
91 | 91 | */ |
92 | - public function get($entry_id, $backend = 'gravityforms') |
|
92 | + public function get( $entry_id, $backend = 'gravityforms' ) |
|
93 | 93 | { |
94 | - foreach ($this->all() as $entry) { |
|
95 | - if ($entry->ID == $entry_id) { |
|
94 | + foreach ( $this->all() as $entry ) { |
|
95 | + if ( $entry->ID == $entry_id ) { |
|
96 | 96 | return $entry; |
97 | 97 | } |
98 | 98 | } |
@@ -114,15 +114,15 @@ discard block |
||
114 | 114 | $total = 0; |
115 | 115 | |
116 | 116 | /** Call all lazy callbacks. */ |
117 | - foreach ($this->callbacks as $callback) { |
|
118 | - if ($callback[0] != 'count') { |
|
117 | + foreach ( $this->callbacks as $callback ) { |
|
118 | + if ( $callback[ 0 ] != 'count' ) { |
|
119 | 119 | continue; |
120 | 120 | } |
121 | 121 | |
122 | - $total += $callback[1]($this->filters); |
|
122 | + $total += $callback[ 1 ]( $this->filters ); |
|
123 | 123 | } |
124 | 124 | |
125 | - if (!$total) { |
|
125 | + if ( ! $total ) { |
|
126 | 126 | $total = parent::count(); |
127 | 127 | } |
128 | 128 | |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | */ |
141 | 141 | public function all() |
142 | 142 | { |
143 | - if ($this->fetched >= 0 || parent::count()) { |
|
143 | + if ( $this->fetched >= 0 || parent::count() ) { |
|
144 | 144 | return parent::all(); |
145 | 145 | } |
146 | 146 | |
@@ -158,13 +158,13 @@ discard block |
||
158 | 158 | * |
159 | 159 | * @return array The plucked values. |
160 | 160 | */ |
161 | - public function pluck($key) |
|
161 | + public function pluck( $key ) |
|
162 | 162 | { |
163 | - $result = []; |
|
163 | + $result = [ ]; |
|
164 | 164 | |
165 | - foreach ($this->all() as $entry) { |
|
165 | + foreach ( $this->all() as $entry ) { |
|
166 | 166 | $entry = $entry->as_entry(); |
167 | - $result[] = Utils::get($entry, $key, null); |
|
167 | + $result[ ] = Utils::get( $entry, $key, null ); |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | return $result; |
@@ -181,7 +181,7 @@ discard block |
||
181 | 181 | */ |
182 | 182 | public function last() |
183 | 183 | { |
184 | - if ($this->fetched >= 0 || parent::count()) { |
|
184 | + if ( $this->fetched >= 0 || parent::count() ) { |
|
185 | 185 | return parent::last(); |
186 | 186 | } |
187 | 187 | |
@@ -199,7 +199,7 @@ discard block |
||
199 | 199 | */ |
200 | 200 | public function first() |
201 | 201 | { |
202 | - if ($this->fetched >= 0 || parent::count()) { |
|
202 | + if ( $this->fetched >= 0 || parent::count() ) { |
|
203 | 203 | return parent::first(); |
204 | 204 | } |
205 | 205 | |
@@ -217,7 +217,7 @@ discard block |
||
217 | 217 | */ |
218 | 218 | public function fetch() |
219 | 219 | { |
220 | - if ($this->fetched >= 0) { |
|
220 | + if ( $this->fetched >= 0 ) { |
|
221 | 221 | return $this; |
222 | 222 | } |
223 | 223 | |
@@ -226,15 +226,15 @@ discard block |
||
226 | 226 | /** Calculate the offsets. */ |
227 | 227 | $offset = new \GV\Entry_Offset(); |
228 | 228 | $offset->limit = $this->limit; |
229 | - $offset->offset = ($this->limit * ($this->current_page - 1)) + $this->offset; |
|
229 | + $offset->offset = ( $this->limit * ( $this->current_page - 1 ) ) + $this->offset; |
|
230 | 230 | |
231 | 231 | /** Call all lazy callbacks. */ |
232 | - foreach ($this->callbacks as $i => $callback) { |
|
233 | - if ($callback[0] != 'fetch') { |
|
232 | + foreach ( $this->callbacks as $i => $callback ) { |
|
233 | + if ( $callback[ 0 ] != 'fetch' ) { |
|
234 | 234 | continue; |
235 | 235 | } |
236 | 236 | |
237 | - $this->merge($callback[1]($this->filters, $this->sorts, $offset)); |
|
237 | + $this->merge( $callback[ 1 ]( $this->filters, $this->sorts, $offset ) ); |
|
238 | 238 | } |
239 | 239 | |
240 | 240 | $this->fetched = parent::count(); |
@@ -255,12 +255,12 @@ discard block |
||
255 | 255 | * |
256 | 256 | * @return \GV\Entry_Collection A copy of the this collection with the filter applied. |
257 | 257 | */ |
258 | - public function filter(\GV\Entry_Filter $filter) |
|
258 | + public function filter( \GV\Entry_Filter $filter ) |
|
259 | 259 | { |
260 | 260 | $collection = clone $this; |
261 | 261 | $collection->clear(); |
262 | 262 | |
263 | - array_push($collection->filters, $filter); |
|
263 | + array_push( $collection->filters, $filter ); |
|
264 | 264 | |
265 | 265 | return $collection; |
266 | 266 | } |
@@ -276,12 +276,12 @@ discard block |
||
276 | 276 | * |
277 | 277 | * @return \GV\Entry_Collection A copy of the this collection with the sort applied. |
278 | 278 | */ |
279 | - public function sort($sort) |
|
279 | + public function sort( $sort ) |
|
280 | 280 | { |
281 | 281 | $collection = clone $this; |
282 | 282 | $collection->clear(); |
283 | 283 | |
284 | - array_push($collection->sorts, $sort); |
|
284 | + array_push( $collection->sorts, $sort ); |
|
285 | 285 | |
286 | 286 | return $collection; |
287 | 287 | } |
@@ -297,7 +297,7 @@ discard block |
||
297 | 297 | * |
298 | 298 | * @return \GV\Entry_Collection A copy of the this collection with the limit applied. |
299 | 299 | */ |
300 | - public function limit($limit) |
|
300 | + public function limit( $limit ) |
|
301 | 301 | { |
302 | 302 | $collection = clone $this; |
303 | 303 | $collection->clear(); |
@@ -321,7 +321,7 @@ discard block |
||
321 | 321 | * |
322 | 322 | * @return \GV\Entry_Collection A copy of the this collection with the offset applied. |
323 | 323 | */ |
324 | - public function offset($offset) |
|
324 | + public function offset( $offset ) |
|
325 | 325 | { |
326 | 326 | $collection = clone $this; |
327 | 327 | $collection->clear(); |
@@ -337,7 +337,7 @@ discard block |
||
337 | 337 | * |
338 | 338 | * @return \GV\Entry_Collection A copy of the this collection with the offset applied. |
339 | 339 | */ |
340 | - public function page($page) |
|
340 | + public function page( $page ) |
|
341 | 341 | { |
342 | 342 | $collection = clone $this; |
343 | 343 | $collection->clear(); |
@@ -367,9 +367,9 @@ discard block |
||
367 | 367 | * |
368 | 368 | * @return void |
369 | 369 | */ |
370 | - public function add_fetch_callback($callback) |
|
370 | + public function add_fetch_callback( $callback ) |
|
371 | 371 | { |
372 | - $this->add_callback('fetch', $callback); |
|
372 | + $this->add_callback( 'fetch', $callback ); |
|
373 | 373 | } |
374 | 374 | |
375 | 375 | /** |
@@ -389,9 +389,9 @@ discard block |
||
389 | 389 | * |
390 | 390 | * @return void |
391 | 391 | */ |
392 | - public function add_count_callback($callback) |
|
392 | + public function add_count_callback( $callback ) |
|
393 | 393 | { |
394 | - $this->add_callback('count', $callback); |
|
394 | + $this->add_callback( 'count', $callback ); |
|
395 | 395 | } |
396 | 396 | |
397 | 397 | /** |
@@ -401,13 +401,13 @@ discard block |
||
401 | 401 | * |
402 | 402 | * @return void |
403 | 403 | */ |
404 | - private function add_callback($type, $callback) |
|
404 | + private function add_callback( $type, $callback ) |
|
405 | 405 | { |
406 | - if (!is_callable($callback)) { |
|
406 | + if ( ! is_callable( $callback ) ) { |
|
407 | 407 | return; |
408 | 408 | } |
409 | 409 | |
410 | - $this->callbacks[] = [$type, $callback]; |
|
410 | + $this->callbacks[ ] = [ $type, $callback ]; |
|
411 | 411 | } |
412 | 412 | |
413 | 413 | /** |
@@ -10,8 +10,7 @@ discard block |
||
10 | 10 | /** |
11 | 11 | * A collection of \GV\Entry objects. |
12 | 12 | */ |
13 | -class Entry_Collection extends Collection |
|
14 | -{ |
|
13 | +class Entry_Collection extends Collection { |
|
15 | 14 | /** |
16 | 15 | * Lazy fetching and counting of data defers |
17 | 16 | * all processing of entries and entry data until |
@@ -65,8 +64,7 @@ discard block |
||
65 | 64 | * |
66 | 65 | * @return void |
67 | 66 | */ |
68 | - public function add($entry) |
|
69 | - { |
|
67 | + public function add($entry) { |
|
70 | 68 | if (!$entry instanceof Entry) { |
71 | 69 | $this->fetched = max(0, $this->fetched); |
72 | 70 | gravityview()->log->error('Entry_Collections can only contain objects of type \GV\Entry.'); |
@@ -89,8 +87,7 @@ discard block |
||
89 | 87 | * |
90 | 88 | * @return \GV\Entry|null The \GV\entry with the $entry_id as the ID, or null if not found. |
91 | 89 | */ |
92 | - public function get($entry_id, $backend = 'gravityforms') |
|
93 | - { |
|
90 | + public function get($entry_id, $backend = 'gravityforms') { |
|
94 | 91 | foreach ($this->all() as $entry) { |
95 | 92 | if ($entry->ID == $entry_id) { |
96 | 93 | return $entry; |
@@ -109,8 +106,7 @@ discard block |
||
109 | 106 | * |
110 | 107 | * @return int The total number of entries that are fetchable. |
111 | 108 | */ |
112 | - public function total() |
|
113 | - { |
|
109 | + public function total() { |
|
114 | 110 | $total = 0; |
115 | 111 | |
116 | 112 | /** Call all lazy callbacks. */ |
@@ -138,8 +134,7 @@ discard block |
||
138 | 134 | * |
139 | 135 | * @return \GV\Entry[] The entries as an array. |
140 | 136 | */ |
141 | - public function all() |
|
142 | - { |
|
137 | + public function all() { |
|
143 | 138 | if ($this->fetched >= 0 || parent::count()) { |
144 | 139 | return parent::all(); |
145 | 140 | } |
@@ -158,8 +153,7 @@ discard block |
||
158 | 153 | * |
159 | 154 | * @return array The plucked values. |
160 | 155 | */ |
161 | - public function pluck($key) |
|
162 | - { |
|
156 | + public function pluck($key) { |
|
163 | 157 | $result = []; |
164 | 158 | |
165 | 159 | foreach ($this->all() as $entry) { |
@@ -179,8 +173,7 @@ discard block |
||
179 | 173 | * |
180 | 174 | * @return \GV\Entry|null The last entry or null. |
181 | 175 | */ |
182 | - public function last() |
|
183 | - { |
|
176 | + public function last() { |
|
184 | 177 | if ($this->fetched >= 0 || parent::count()) { |
185 | 178 | return parent::last(); |
186 | 179 | } |
@@ -197,8 +190,7 @@ discard block |
||
197 | 190 | * |
198 | 191 | * @return \GV\Entry|null The first entry or null. |
199 | 192 | */ |
200 | - public function first() |
|
201 | - { |
|
193 | + public function first() { |
|
202 | 194 | if ($this->fetched >= 0 || parent::count()) { |
203 | 195 | return parent::first(); |
204 | 196 | } |
@@ -215,8 +207,7 @@ discard block |
||
215 | 207 | * |
216 | 208 | * @return \GV\Entry_Collection This collection, now hydrated. |
217 | 209 | */ |
218 | - public function fetch() |
|
219 | - { |
|
210 | + public function fetch() { |
|
220 | 211 | if ($this->fetched >= 0) { |
221 | 212 | return $this; |
222 | 213 | } |
@@ -255,8 +246,7 @@ discard block |
||
255 | 246 | * |
256 | 247 | * @return \GV\Entry_Collection A copy of the this collection with the filter applied. |
257 | 248 | */ |
258 | - public function filter(\GV\Entry_Filter $filter) |
|
259 | - { |
|
249 | + public function filter(\GV\Entry_Filter $filter) { |
|
260 | 250 | $collection = clone $this; |
261 | 251 | $collection->clear(); |
262 | 252 | |
@@ -276,8 +266,7 @@ discard block |
||
276 | 266 | * |
277 | 267 | * @return \GV\Entry_Collection A copy of the this collection with the sort applied. |
278 | 268 | */ |
279 | - public function sort($sort) |
|
280 | - { |
|
269 | + public function sort($sort) { |
|
281 | 270 | $collection = clone $this; |
282 | 271 | $collection->clear(); |
283 | 272 | |
@@ -297,8 +286,7 @@ discard block |
||
297 | 286 | * |
298 | 287 | * @return \GV\Entry_Collection A copy of the this collection with the limit applied. |
299 | 288 | */ |
300 | - public function limit($limit) |
|
301 | - { |
|
289 | + public function limit($limit) { |
|
302 | 290 | $collection = clone $this; |
303 | 291 | $collection->clear(); |
304 | 292 | $collection->limit = $limit; |
@@ -321,8 +309,7 @@ discard block |
||
321 | 309 | * |
322 | 310 | * @return \GV\Entry_Collection A copy of the this collection with the offset applied. |
323 | 311 | */ |
324 | - public function offset($offset) |
|
325 | - { |
|
312 | + public function offset($offset) { |
|
326 | 313 | $collection = clone $this; |
327 | 314 | $collection->clear(); |
328 | 315 | $collection->offset = $offset; |
@@ -337,8 +324,7 @@ discard block |
||
337 | 324 | * |
338 | 325 | * @return \GV\Entry_Collection A copy of the this collection with the offset applied. |
339 | 326 | */ |
340 | - public function page($page) |
|
341 | - { |
|
327 | + public function page($page) { |
|
342 | 328 | $collection = clone $this; |
343 | 329 | $collection->clear(); |
344 | 330 | $collection->current_page = $page; |
@@ -367,8 +353,7 @@ discard block |
||
367 | 353 | * |
368 | 354 | * @return void |
369 | 355 | */ |
370 | - public function add_fetch_callback($callback) |
|
371 | - { |
|
356 | + public function add_fetch_callback($callback) { |
|
372 | 357 | $this->add_callback('fetch', $callback); |
373 | 358 | } |
374 | 359 | |
@@ -389,8 +374,7 @@ discard block |
||
389 | 374 | * |
390 | 375 | * @return void |
391 | 376 | */ |
392 | - public function add_count_callback($callback) |
|
393 | - { |
|
377 | + public function add_count_callback($callback) { |
|
394 | 378 | $this->add_callback('count', $callback); |
395 | 379 | } |
396 | 380 | |
@@ -401,8 +385,7 @@ discard block |
||
401 | 385 | * |
402 | 386 | * @return void |
403 | 387 | */ |
404 | - private function add_callback($type, $callback) |
|
405 | - { |
|
388 | + private function add_callback($type, $callback) { |
|
406 | 389 | if (!is_callable($callback)) { |
407 | 390 | return; |
408 | 391 | } |
@@ -413,8 +396,7 @@ discard block |
||
413 | 396 | /** |
414 | 397 | * @inheritdoc |
415 | 398 | */ |
416 | - public function clear() |
|
417 | - { |
|
399 | + public function clear() { |
|
418 | 400 | $this->fetched = -1; |
419 | 401 | parent::clear(); |
420 | 402 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | |
5 | 5 | /** If this file is called directly, abort. */ |
6 | 6 | if (!defined('GRAVITYVIEW_DIR')) { |
7 | - exit(); |
|
7 | + exit(); |
|
8 | 8 | } |
9 | 9 | |
10 | 10 | /** |
@@ -14,564 +14,564 @@ discard block |
||
14 | 14 | */ |
15 | 15 | class View_Table_Template extends View_Template |
16 | 16 | { |
17 | - /** |
|
18 | - * @var string The template slug to be loaded (like "table", "list") |
|
19 | - */ |
|
20 | - public static $slug = 'table'; |
|
21 | - |
|
22 | - /** |
|
23 | - * Constructor. Add filters to modify output. |
|
24 | - * |
|
25 | - * @since 2.0.4 |
|
26 | - * |
|
27 | - * @param View $view |
|
28 | - * @param Entry_Collection $entries |
|
29 | - * @param Request $request |
|
30 | - */ |
|
31 | - public function __construct(View $view, Entry_Collection $entries, Request $request) |
|
32 | - { |
|
33 | - add_filter('gravityview/template/field/label', [__CLASS__, 'add_columns_sort_links'], 100, 2); |
|
34 | - |
|
35 | - parent::__construct($view, $entries, $request); |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * Add sorting links to HTML columns that support sorting. |
|
40 | - * |
|
41 | - * @since 2.0.4 |
|
42 | - * @since 2.0.5 Made static |
|
43 | - * |
|
44 | - * @static |
|
45 | - * |
|
46 | - * @param string $column_label Label for the table column |
|
47 | - * @param \GV\Template_Context $context |
|
48 | - * |
|
49 | - * @return string |
|
50 | - */ |
|
51 | - public static function add_columns_sort_links($column_label, $context = null) |
|
52 | - { |
|
53 | - $sort_columns = $context->view->settings->get('sort_columns'); |
|
54 | - |
|
55 | - if (empty($sort_columns)) { |
|
56 | - return $column_label; |
|
57 | - } |
|
58 | - |
|
59 | - if (!\GravityView_frontend::getInstance()->is_field_sortable($context->field->ID, $context->view->form->form)) { |
|
60 | - return $column_label; |
|
61 | - } |
|
62 | - |
|
63 | - $sorting = []; |
|
64 | - |
|
65 | - $directions = $context->view->settings->get('sort_direction'); |
|
66 | - |
|
67 | - $sorts = Utils::_GET('sort'); |
|
68 | - |
|
69 | - if ($sorts) { |
|
70 | - if (is_array($sorts)) { |
|
71 | - foreach ((array) $sorts as $key => $direction) { |
|
72 | - if ($key == $context->field->ID) { |
|
73 | - $sorting['key'] = $context->field->ID; |
|
74 | - $sorting['direction'] = strtolower($direction); |
|
75 | - break; |
|
76 | - } |
|
77 | - } |
|
78 | - } else { |
|
79 | - if ($sorts == $context->field->ID) { |
|
80 | - $sorting['key'] = $context->field->ID; |
|
81 | - $sorting['direction'] = strtolower(Utils::_GET('dir', '')); |
|
82 | - } |
|
83 | - } |
|
84 | - } else { |
|
85 | - foreach ((array) $context->view->settings->get('sort_field', []) as $i => $sort_field) { |
|
86 | - if ($sort_field == $context->field->ID) { |
|
87 | - $sorting['key'] = $sort_field; |
|
88 | - $sorting['direction'] = strtolower(Utils::get($directions, $i, '')); |
|
89 | - break; // Only get the first sort |
|
90 | - } |
|
91 | - } |
|
92 | - } |
|
93 | - |
|
94 | - $class = 'gv-sort'; |
|
95 | - |
|
96 | - $sort_field_id = \GravityView_frontend::_override_sorting_id_by_field_type($context->field->ID, $context->view->form->ID); |
|
97 | - |
|
98 | - $sort_args = [ |
|
99 | - sprintf('sort[%s]', $context->field->ID), |
|
100 | - 'asc', |
|
101 | - ]; |
|
102 | - |
|
103 | - // If we are already sorting by the current field... |
|
104 | - if (!empty($sorting['key']) && (string) $sort_field_id === (string) $sorting['key']) { |
|
105 | - switch ($sorting['direction']) { |
|
106 | - // No sort |
|
107 | - case '': |
|
108 | - $sort_args[1] = 'asc'; |
|
109 | - $class .= ' gv-icon-caret-up-down'; |
|
110 | - break; |
|
111 | - case 'desc': |
|
112 | - $sort_args[1] = ''; |
|
113 | - $class .= ' gv-icon-sort-asc'; |
|
114 | - break; |
|
115 | - case 'asc': |
|
116 | - default: |
|
117 | - $sort_args[1] = 'desc'; |
|
118 | - $class .= ' gv-icon-sort-desc'; |
|
119 | - break; |
|
120 | - } |
|
121 | - } else { |
|
122 | - $class .= ' gv-icon-caret-up-down'; |
|
123 | - } |
|
124 | - |
|
125 | - $url = remove_query_arg(['pagenum']); |
|
126 | - $url = remove_query_arg('sort', $url); |
|
127 | - $multisort_url = self::_get_multisort_url($url, $sort_args, $context->field->ID); |
|
128 | - |
|
129 | - $url = add_query_arg($sort_args[0], $sort_args[1], $url); |
|
130 | - |
|
131 | - $return = '<a href="'.esc_url_raw($url).'"'; |
|
132 | - |
|
133 | - if (!empty($multisort_url)) { |
|
134 | - $return .= ' data-multisort-href="'.esc_url_raw($multisort_url).'"'; |
|
135 | - } |
|
136 | - |
|
137 | - $return .= ' class="'.$class.'" ></a> '.$column_label; |
|
138 | - |
|
139 | - return $return; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Get the multi-sort URL used in the sorting links. |
|
144 | - * |
|
145 | - * @todo Consider moving to Utils? |
|
146 | - * |
|
147 | - * @since 2.3 |
|
148 | - * @see add_columns_sort_links |
|
149 | - * |
|
150 | - * @param string $url Single-sort URL |
|
151 | - * @param array $sort_args Single sorting for rules, in [ field_id, dir ] format |
|
152 | - * @param string|int $field_id ID of the current field being displayed |
|
153 | - * |
|
154 | - * @return string Multisort URL, if there are multiple sorts. Otherwise, existing $url |
|
155 | - */ |
|
156 | - public static function _get_multisort_url($url, $sort_args, $field_id) |
|
157 | - { |
|
158 | - $sorts = Utils::_GET('sort'); |
|
159 | - |
|
160 | - if (!is_array($sorts)) { |
|
161 | - return $url; |
|
162 | - } |
|
163 | - |
|
164 | - $multisort_url = $url; |
|
165 | - |
|
166 | - // If the field has already been sorted by, add the field to the URL |
|
167 | - if (!in_array($field_id, $keys = array_keys($sorts))) { |
|
168 | - if (count($keys)) { |
|
169 | - $multisort_url = add_query_arg(sprintf('sort[%s]', end($keys)), $sorts[end($keys)], $multisort_url); |
|
170 | - $multisort_url = add_query_arg($sort_args[0], $sort_args[1], $multisort_url); |
|
171 | - } else { |
|
172 | - $multisort_url = add_query_arg($sort_args[0], $sort_args[1], $multisort_url); |
|
173 | - } |
|
174 | - } |
|
175 | - // Otherwise, we are just updating the sort order |
|
176 | - else { |
|
177 | - |
|
178 | - // Pass empty value to unset |
|
179 | - if ('' === $sort_args[1]) { |
|
180 | - unset($sorts[$field_id]); |
|
181 | - } else { |
|
182 | - $sorts[$field_id] = $sort_args[1]; |
|
183 | - } |
|
184 | - |
|
185 | - $multisort_url = add_query_arg(['sort' => $sorts], $multisort_url); |
|
186 | - } |
|
187 | - |
|
188 | - return $multisort_url; |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Output the table column names. |
|
193 | - * |
|
194 | - * @return void |
|
195 | - */ |
|
196 | - public function the_columns() |
|
197 | - { |
|
198 | - $fields = $this->view->fields->by_position('directory_table-columns'); |
|
199 | - |
|
200 | - foreach ($fields->by_visible($this->view)->all() as $field) { |
|
201 | - $context = Template_Context::from_template($this, compact('field')); |
|
202 | - |
|
203 | - $args = [ |
|
204 | - 'field' => is_numeric($field->ID) ? $field->as_configuration() : null, |
|
205 | - 'hide_empty' => false, |
|
206 | - 'zone_id' => 'directory_table-columns', |
|
207 | - 'markup' => '<th id="{{ field_id }}" class="{{ class }}" style="{{width:style}}" data-label="{{label_value:data-label}}">{{label}}</th>', |
|
208 | - 'label_markup' => '<span class="gv-field-label">{{ label }}</span>', |
|
209 | - 'label' => self::get_field_column_label($field, $context), |
|
210 | - ]; |
|
211 | - |
|
212 | - echo \gravityview_field_output($args, $context); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - /** |
|
217 | - * Returns the label for a column, with support for all deprecated filters. |
|
218 | - * |
|
219 | - * @since 2.1 |
|
220 | - * |
|
221 | - * @param \GV\Field $field |
|
222 | - * @param \GV\Template_Context $context |
|
223 | - */ |
|
224 | - protected static function get_field_column_label($field, $context = null) |
|
225 | - { |
|
226 | - $form = $field->form_id ? GF_Form::by_id($field->form_id) : $context->view->form; |
|
227 | - |
|
228 | - /** |
|
229 | - * @deprecated Here for back-compatibility. |
|
230 | - */ |
|
231 | - $column_label = apply_filters('gravityview_render_after_label', $field->get_label($context->view, $form), $field->as_configuration()); |
|
232 | - $column_label = apply_filters('gravityview/template/field_label', $column_label, $field->as_configuration(), ($form && $form->form) ? $form->form : null, null); |
|
233 | - |
|
234 | - /** |
|
235 | - * @filter `gravityview/template/field/label` Override the field label. |
|
236 | - * |
|
237 | - * @since 2.0 |
|
238 | - * |
|
239 | - * @param string $column_label The label to override. |
|
240 | - * @param \GV\Template_Context $context The context. Does not have entry set here. |
|
241 | - */ |
|
242 | - $column_label = apply_filters('gravityview/template/field/label', $column_label, $context); |
|
243 | - |
|
244 | - return $column_label; |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * Output the entry row. |
|
249 | - * |
|
250 | - * @param \GV\Entry $entry The entry to be rendered. |
|
251 | - * @param array $attributes The attributes for the <tr> tag |
|
252 | - * |
|
253 | - * @return void |
|
254 | - */ |
|
255 | - public function the_entry(\GV\Entry $entry, $attributes) |
|
256 | - { |
|
257 | - $fields = $this->view->fields->by_position('directory_table-columns')->by_visible($this->view); |
|
258 | - |
|
259 | - $context = Template_Context::from_template($this, compact('entry', 'fields')); |
|
260 | - |
|
261 | - /** |
|
262 | - * Push legacy entry context. |
|
263 | - */ |
|
264 | - \GV\Mocks\Legacy_Context::load([ |
|
265 | - 'entry' => $entry, |
|
266 | - ]); |
|
267 | - |
|
268 | - /** |
|
269 | - * @filter `gravityview_table_cells` Modify the fields displayed in a table |
|
270 | - * |
|
271 | - * @param array $fields |
|
272 | - * @param \GravityView_View $this |
|
273 | - * |
|
274 | - * @deprecated Use `gravityview/template/table/fields` |
|
275 | - */ |
|
276 | - $fields = apply_filters('gravityview_table_cells', $fields->as_configuration(), \GravityView_View::getInstance()); |
|
277 | - $fields = Field_Collection::from_configuration($fields); |
|
278 | - |
|
279 | - /** |
|
280 | - * @filter `gravityview/template/table/fields` Modify the fields displayed in this tables. |
|
281 | - * |
|
282 | - * @param \GV\Field_Collection $fields The fields. |
|
283 | - * @param \GV\Template_Context $context The context. |
|
284 | - * |
|
285 | - * @since 2.0 |
|
286 | - */ |
|
287 | - $fields = apply_filters('gravityview/template/table/fields', $fields, $context); |
|
288 | - |
|
289 | - $context = Template_Context::from_template($this, compact('entry', 'fields')); |
|
290 | - |
|
291 | - /** |
|
292 | - * @filter `gravityview/template/table/entry/row/attributes` Filter the row attributes for the row in table view. |
|
293 | - * |
|
294 | - * @param array $attributes The HTML attributes. |
|
295 | - * @param \GV\Template_Context The context. |
|
296 | - * |
|
297 | - * @since 2.0 |
|
298 | - */ |
|
299 | - $attributes = apply_filters('gravityview/template/table/entry/row/attributes', $attributes, $context); |
|
300 | - |
|
301 | - /** Glue the attributes together. */ |
|
302 | - foreach ($attributes as $attribute => $value) { |
|
303 | - $attributes[$attribute] = sprintf("$attribute=\"%s\"", esc_attr($value)); |
|
304 | - } |
|
305 | - $attributes = implode(' ', $attributes); ?> |
|
17 | + /** |
|
18 | + * @var string The template slug to be loaded (like "table", "list") |
|
19 | + */ |
|
20 | + public static $slug = 'table'; |
|
21 | + |
|
22 | + /** |
|
23 | + * Constructor. Add filters to modify output. |
|
24 | + * |
|
25 | + * @since 2.0.4 |
|
26 | + * |
|
27 | + * @param View $view |
|
28 | + * @param Entry_Collection $entries |
|
29 | + * @param Request $request |
|
30 | + */ |
|
31 | + public function __construct(View $view, Entry_Collection $entries, Request $request) |
|
32 | + { |
|
33 | + add_filter('gravityview/template/field/label', [__CLASS__, 'add_columns_sort_links'], 100, 2); |
|
34 | + |
|
35 | + parent::__construct($view, $entries, $request); |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * Add sorting links to HTML columns that support sorting. |
|
40 | + * |
|
41 | + * @since 2.0.4 |
|
42 | + * @since 2.0.5 Made static |
|
43 | + * |
|
44 | + * @static |
|
45 | + * |
|
46 | + * @param string $column_label Label for the table column |
|
47 | + * @param \GV\Template_Context $context |
|
48 | + * |
|
49 | + * @return string |
|
50 | + */ |
|
51 | + public static function add_columns_sort_links($column_label, $context = null) |
|
52 | + { |
|
53 | + $sort_columns = $context->view->settings->get('sort_columns'); |
|
54 | + |
|
55 | + if (empty($sort_columns)) { |
|
56 | + return $column_label; |
|
57 | + } |
|
58 | + |
|
59 | + if (!\GravityView_frontend::getInstance()->is_field_sortable($context->field->ID, $context->view->form->form)) { |
|
60 | + return $column_label; |
|
61 | + } |
|
62 | + |
|
63 | + $sorting = []; |
|
64 | + |
|
65 | + $directions = $context->view->settings->get('sort_direction'); |
|
66 | + |
|
67 | + $sorts = Utils::_GET('sort'); |
|
68 | + |
|
69 | + if ($sorts) { |
|
70 | + if (is_array($sorts)) { |
|
71 | + foreach ((array) $sorts as $key => $direction) { |
|
72 | + if ($key == $context->field->ID) { |
|
73 | + $sorting['key'] = $context->field->ID; |
|
74 | + $sorting['direction'] = strtolower($direction); |
|
75 | + break; |
|
76 | + } |
|
77 | + } |
|
78 | + } else { |
|
79 | + if ($sorts == $context->field->ID) { |
|
80 | + $sorting['key'] = $context->field->ID; |
|
81 | + $sorting['direction'] = strtolower(Utils::_GET('dir', '')); |
|
82 | + } |
|
83 | + } |
|
84 | + } else { |
|
85 | + foreach ((array) $context->view->settings->get('sort_field', []) as $i => $sort_field) { |
|
86 | + if ($sort_field == $context->field->ID) { |
|
87 | + $sorting['key'] = $sort_field; |
|
88 | + $sorting['direction'] = strtolower(Utils::get($directions, $i, '')); |
|
89 | + break; // Only get the first sort |
|
90 | + } |
|
91 | + } |
|
92 | + } |
|
93 | + |
|
94 | + $class = 'gv-sort'; |
|
95 | + |
|
96 | + $sort_field_id = \GravityView_frontend::_override_sorting_id_by_field_type($context->field->ID, $context->view->form->ID); |
|
97 | + |
|
98 | + $sort_args = [ |
|
99 | + sprintf('sort[%s]', $context->field->ID), |
|
100 | + 'asc', |
|
101 | + ]; |
|
102 | + |
|
103 | + // If we are already sorting by the current field... |
|
104 | + if (!empty($sorting['key']) && (string) $sort_field_id === (string) $sorting['key']) { |
|
105 | + switch ($sorting['direction']) { |
|
106 | + // No sort |
|
107 | + case '': |
|
108 | + $sort_args[1] = 'asc'; |
|
109 | + $class .= ' gv-icon-caret-up-down'; |
|
110 | + break; |
|
111 | + case 'desc': |
|
112 | + $sort_args[1] = ''; |
|
113 | + $class .= ' gv-icon-sort-asc'; |
|
114 | + break; |
|
115 | + case 'asc': |
|
116 | + default: |
|
117 | + $sort_args[1] = 'desc'; |
|
118 | + $class .= ' gv-icon-sort-desc'; |
|
119 | + break; |
|
120 | + } |
|
121 | + } else { |
|
122 | + $class .= ' gv-icon-caret-up-down'; |
|
123 | + } |
|
124 | + |
|
125 | + $url = remove_query_arg(['pagenum']); |
|
126 | + $url = remove_query_arg('sort', $url); |
|
127 | + $multisort_url = self::_get_multisort_url($url, $sort_args, $context->field->ID); |
|
128 | + |
|
129 | + $url = add_query_arg($sort_args[0], $sort_args[1], $url); |
|
130 | + |
|
131 | + $return = '<a href="'.esc_url_raw($url).'"'; |
|
132 | + |
|
133 | + if (!empty($multisort_url)) { |
|
134 | + $return .= ' data-multisort-href="'.esc_url_raw($multisort_url).'"'; |
|
135 | + } |
|
136 | + |
|
137 | + $return .= ' class="'.$class.'" ></a> '.$column_label; |
|
138 | + |
|
139 | + return $return; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Get the multi-sort URL used in the sorting links. |
|
144 | + * |
|
145 | + * @todo Consider moving to Utils? |
|
146 | + * |
|
147 | + * @since 2.3 |
|
148 | + * @see add_columns_sort_links |
|
149 | + * |
|
150 | + * @param string $url Single-sort URL |
|
151 | + * @param array $sort_args Single sorting for rules, in [ field_id, dir ] format |
|
152 | + * @param string|int $field_id ID of the current field being displayed |
|
153 | + * |
|
154 | + * @return string Multisort URL, if there are multiple sorts. Otherwise, existing $url |
|
155 | + */ |
|
156 | + public static function _get_multisort_url($url, $sort_args, $field_id) |
|
157 | + { |
|
158 | + $sorts = Utils::_GET('sort'); |
|
159 | + |
|
160 | + if (!is_array($sorts)) { |
|
161 | + return $url; |
|
162 | + } |
|
163 | + |
|
164 | + $multisort_url = $url; |
|
165 | + |
|
166 | + // If the field has already been sorted by, add the field to the URL |
|
167 | + if (!in_array($field_id, $keys = array_keys($sorts))) { |
|
168 | + if (count($keys)) { |
|
169 | + $multisort_url = add_query_arg(sprintf('sort[%s]', end($keys)), $sorts[end($keys)], $multisort_url); |
|
170 | + $multisort_url = add_query_arg($sort_args[0], $sort_args[1], $multisort_url); |
|
171 | + } else { |
|
172 | + $multisort_url = add_query_arg($sort_args[0], $sort_args[1], $multisort_url); |
|
173 | + } |
|
174 | + } |
|
175 | + // Otherwise, we are just updating the sort order |
|
176 | + else { |
|
177 | + |
|
178 | + // Pass empty value to unset |
|
179 | + if ('' === $sort_args[1]) { |
|
180 | + unset($sorts[$field_id]); |
|
181 | + } else { |
|
182 | + $sorts[$field_id] = $sort_args[1]; |
|
183 | + } |
|
184 | + |
|
185 | + $multisort_url = add_query_arg(['sort' => $sorts], $multisort_url); |
|
186 | + } |
|
187 | + |
|
188 | + return $multisort_url; |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Output the table column names. |
|
193 | + * |
|
194 | + * @return void |
|
195 | + */ |
|
196 | + public function the_columns() |
|
197 | + { |
|
198 | + $fields = $this->view->fields->by_position('directory_table-columns'); |
|
199 | + |
|
200 | + foreach ($fields->by_visible($this->view)->all() as $field) { |
|
201 | + $context = Template_Context::from_template($this, compact('field')); |
|
202 | + |
|
203 | + $args = [ |
|
204 | + 'field' => is_numeric($field->ID) ? $field->as_configuration() : null, |
|
205 | + 'hide_empty' => false, |
|
206 | + 'zone_id' => 'directory_table-columns', |
|
207 | + 'markup' => '<th id="{{ field_id }}" class="{{ class }}" style="{{width:style}}" data-label="{{label_value:data-label}}">{{label}}</th>', |
|
208 | + 'label_markup' => '<span class="gv-field-label">{{ label }}</span>', |
|
209 | + 'label' => self::get_field_column_label($field, $context), |
|
210 | + ]; |
|
211 | + |
|
212 | + echo \gravityview_field_output($args, $context); |
|
213 | + } |
|
214 | + } |
|
215 | + |
|
216 | + /** |
|
217 | + * Returns the label for a column, with support for all deprecated filters. |
|
218 | + * |
|
219 | + * @since 2.1 |
|
220 | + * |
|
221 | + * @param \GV\Field $field |
|
222 | + * @param \GV\Template_Context $context |
|
223 | + */ |
|
224 | + protected static function get_field_column_label($field, $context = null) |
|
225 | + { |
|
226 | + $form = $field->form_id ? GF_Form::by_id($field->form_id) : $context->view->form; |
|
227 | + |
|
228 | + /** |
|
229 | + * @deprecated Here for back-compatibility. |
|
230 | + */ |
|
231 | + $column_label = apply_filters('gravityview_render_after_label', $field->get_label($context->view, $form), $field->as_configuration()); |
|
232 | + $column_label = apply_filters('gravityview/template/field_label', $column_label, $field->as_configuration(), ($form && $form->form) ? $form->form : null, null); |
|
233 | + |
|
234 | + /** |
|
235 | + * @filter `gravityview/template/field/label` Override the field label. |
|
236 | + * |
|
237 | + * @since 2.0 |
|
238 | + * |
|
239 | + * @param string $column_label The label to override. |
|
240 | + * @param \GV\Template_Context $context The context. Does not have entry set here. |
|
241 | + */ |
|
242 | + $column_label = apply_filters('gravityview/template/field/label', $column_label, $context); |
|
243 | + |
|
244 | + return $column_label; |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * Output the entry row. |
|
249 | + * |
|
250 | + * @param \GV\Entry $entry The entry to be rendered. |
|
251 | + * @param array $attributes The attributes for the <tr> tag |
|
252 | + * |
|
253 | + * @return void |
|
254 | + */ |
|
255 | + public function the_entry(\GV\Entry $entry, $attributes) |
|
256 | + { |
|
257 | + $fields = $this->view->fields->by_position('directory_table-columns')->by_visible($this->view); |
|
258 | + |
|
259 | + $context = Template_Context::from_template($this, compact('entry', 'fields')); |
|
260 | + |
|
261 | + /** |
|
262 | + * Push legacy entry context. |
|
263 | + */ |
|
264 | + \GV\Mocks\Legacy_Context::load([ |
|
265 | + 'entry' => $entry, |
|
266 | + ]); |
|
267 | + |
|
268 | + /** |
|
269 | + * @filter `gravityview_table_cells` Modify the fields displayed in a table |
|
270 | + * |
|
271 | + * @param array $fields |
|
272 | + * @param \GravityView_View $this |
|
273 | + * |
|
274 | + * @deprecated Use `gravityview/template/table/fields` |
|
275 | + */ |
|
276 | + $fields = apply_filters('gravityview_table_cells', $fields->as_configuration(), \GravityView_View::getInstance()); |
|
277 | + $fields = Field_Collection::from_configuration($fields); |
|
278 | + |
|
279 | + /** |
|
280 | + * @filter `gravityview/template/table/fields` Modify the fields displayed in this tables. |
|
281 | + * |
|
282 | + * @param \GV\Field_Collection $fields The fields. |
|
283 | + * @param \GV\Template_Context $context The context. |
|
284 | + * |
|
285 | + * @since 2.0 |
|
286 | + */ |
|
287 | + $fields = apply_filters('gravityview/template/table/fields', $fields, $context); |
|
288 | + |
|
289 | + $context = Template_Context::from_template($this, compact('entry', 'fields')); |
|
290 | + |
|
291 | + /** |
|
292 | + * @filter `gravityview/template/table/entry/row/attributes` Filter the row attributes for the row in table view. |
|
293 | + * |
|
294 | + * @param array $attributes The HTML attributes. |
|
295 | + * @param \GV\Template_Context The context. |
|
296 | + * |
|
297 | + * @since 2.0 |
|
298 | + */ |
|
299 | + $attributes = apply_filters('gravityview/template/table/entry/row/attributes', $attributes, $context); |
|
300 | + |
|
301 | + /** Glue the attributes together. */ |
|
302 | + foreach ($attributes as $attribute => $value) { |
|
303 | + $attributes[$attribute] = sprintf("$attribute=\"%s\"", esc_attr($value)); |
|
304 | + } |
|
305 | + $attributes = implode(' ', $attributes); ?> |
|
306 | 306 | <tr<?php echo $attributes ? " $attributes" : ''; ?>> |
307 | 307 | <?php |
308 | 308 | |
309 | - /** |
|
310 | - * @action `gravityview/template/table/cells/before` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells. |
|
311 | - * |
|
312 | - * @since 2.0 |
|
313 | - * |
|
314 | - * @param \GV\Template_Context The context. |
|
315 | - */ |
|
316 | - do_action('gravityview/template/table/cells/before', $context); |
|
317 | - |
|
318 | - /** |
|
319 | - * @action `gravityview_table_cells_before` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells. |
|
320 | - * |
|
321 | - * @since 1.0.7 |
|
322 | - * |
|
323 | - * @param \GravityView_View $this Current GravityView_View object |
|
324 | - * |
|
325 | - * @deprecated Use `gravityview/template/table/cells/before` |
|
326 | - */ |
|
327 | - do_action('gravityview_table_cells_before', \GravityView_View::getInstance()); |
|
328 | - |
|
329 | - foreach ($fields->all() as $field) { |
|
330 | - if (isset($this->view->unions[$entry['form_id']])) { |
|
331 | - if (isset($this->view->unions[$entry['form_id']][$field->ID])) { |
|
332 | - $field = $this->view->unions[$entry['form_id']][$field->ID]; |
|
333 | - } else { |
|
334 | - if (!$field instanceof Internal_Field) { |
|
335 | - $field = Internal_Field::from_configuration(['id' => 'custom']); |
|
336 | - } |
|
337 | - } |
|
338 | - } |
|
339 | - $this->the_field($field, $entry); |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * @action `gravityview/template/table/cells/after` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells. |
|
344 | - * |
|
345 | - * @since 2.0 |
|
346 | - * |
|
347 | - * @param \GV\Template_Context The context. |
|
348 | - */ |
|
349 | - do_action('gravityview/template/table/cells/after', $context); |
|
350 | - |
|
351 | - /** |
|
352 | - * @action `gravityview_table_cells_after` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells. |
|
353 | - * |
|
354 | - * @since 1.0.7 |
|
355 | - * |
|
356 | - * @param \GravityView_View $this Current GravityView_View object |
|
357 | - * |
|
358 | - * @deprecated Use `gravityview/template/table/cells/after` |
|
359 | - */ |
|
360 | - do_action('gravityview_table_cells_after', \GravityView_View::getInstance()); ?> |
|
309 | + /** |
|
310 | + * @action `gravityview/template/table/cells/before` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells. |
|
311 | + * |
|
312 | + * @since 2.0 |
|
313 | + * |
|
314 | + * @param \GV\Template_Context The context. |
|
315 | + */ |
|
316 | + do_action('gravityview/template/table/cells/before', $context); |
|
317 | + |
|
318 | + /** |
|
319 | + * @action `gravityview_table_cells_before` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells. |
|
320 | + * |
|
321 | + * @since 1.0.7 |
|
322 | + * |
|
323 | + * @param \GravityView_View $this Current GravityView_View object |
|
324 | + * |
|
325 | + * @deprecated Use `gravityview/template/table/cells/before` |
|
326 | + */ |
|
327 | + do_action('gravityview_table_cells_before', \GravityView_View::getInstance()); |
|
328 | + |
|
329 | + foreach ($fields->all() as $field) { |
|
330 | + if (isset($this->view->unions[$entry['form_id']])) { |
|
331 | + if (isset($this->view->unions[$entry['form_id']][$field->ID])) { |
|
332 | + $field = $this->view->unions[$entry['form_id']][$field->ID]; |
|
333 | + } else { |
|
334 | + if (!$field instanceof Internal_Field) { |
|
335 | + $field = Internal_Field::from_configuration(['id' => 'custom']); |
|
336 | + } |
|
337 | + } |
|
338 | + } |
|
339 | + $this->the_field($field, $entry); |
|
340 | + } |
|
341 | + |
|
342 | + /** |
|
343 | + * @action `gravityview/template/table/cells/after` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells. |
|
344 | + * |
|
345 | + * @since 2.0 |
|
346 | + * |
|
347 | + * @param \GV\Template_Context The context. |
|
348 | + */ |
|
349 | + do_action('gravityview/template/table/cells/after', $context); |
|
350 | + |
|
351 | + /** |
|
352 | + * @action `gravityview_table_cells_after` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells. |
|
353 | + * |
|
354 | + * @since 1.0.7 |
|
355 | + * |
|
356 | + * @param \GravityView_View $this Current GravityView_View object |
|
357 | + * |
|
358 | + * @deprecated Use `gravityview/template/table/cells/after` |
|
359 | + */ |
|
360 | + do_action('gravityview_table_cells_after', \GravityView_View::getInstance()); ?> |
|
361 | 361 | </tr> |
362 | 362 | <?php |
363 | - } |
|
364 | - |
|
365 | - /** |
|
366 | - * Output a field cell. |
|
367 | - * |
|
368 | - * @param \GV\Field $field The field to be ouput. |
|
369 | - * @param \GV\Field $entry The entry this field is for. |
|
370 | - * |
|
371 | - * @return void |
|
372 | - */ |
|
373 | - public function the_field(\GV\Field $field, \GV\Entry $entry) |
|
374 | - { |
|
375 | - $form = $this->view->form; |
|
376 | - $single_entry = $entry; |
|
377 | - |
|
378 | - /** |
|
379 | - * Push legacy entry context. |
|
380 | - */ |
|
381 | - \GV\Mocks\Legacy_Context::load([ |
|
382 | - 'field' => $field, |
|
383 | - ]); |
|
384 | - |
|
385 | - if ($entry->is_multi()) { |
|
386 | - if (!$single_entry = $entry->from_field($field)) { |
|
387 | - echo '<td></td>'; |
|
388 | - |
|
389 | - return; |
|
390 | - } |
|
391 | - $form = GF_Form::by_id($field->form_id); |
|
392 | - } |
|
393 | - |
|
394 | - $renderer = new Field_Renderer(); |
|
395 | - $source = is_numeric($field->ID) ? $form : new Internal_Source(); |
|
396 | - |
|
397 | - $value = $renderer->render($field, $this->view, $source, $entry, $this->request); |
|
398 | - |
|
399 | - $context = Template_Context::from_template($this, compact('field')); |
|
400 | - $context->entry = $single_entry; |
|
401 | - |
|
402 | - $args = [ |
|
403 | - 'entry' => $entry->as_entry(), |
|
404 | - 'field' => is_numeric($field->ID) ? $field->as_configuration() : null, |
|
405 | - 'value' => $value, |
|
406 | - 'hide_empty' => false, |
|
407 | - 'zone_id' => 'directory_table-columns', |
|
408 | - 'label' => self::get_field_column_label($field, $context), |
|
409 | - 'markup' => '<td id="{{ field_id }}" class="{{ class }}" data-label="{{label_value:data-label}}">{{ value }}</td>', |
|
410 | - 'form' => $form, |
|
411 | - ]; |
|
412 | - |
|
413 | - /** Output. */ |
|
414 | - echo \gravityview_field_output($args, $context); |
|
415 | - } |
|
416 | - |
|
417 | - /** |
|
418 | - * `gravityview_table_body_before` and `gravityview/template/table/body/before` actions. |
|
419 | - * |
|
420 | - * Output inside the `tbody` of the table. |
|
421 | - * |
|
422 | - * @param $context \GV\Template_Context The 2.0 context. |
|
423 | - * |
|
424 | - * @return void |
|
425 | - */ |
|
426 | - public static function body_before($context) |
|
427 | - { |
|
428 | - /** |
|
429 | - * @action `gravityview/template/table/body/before` Output inside the `tbody` of the table. |
|
430 | - * |
|
431 | - * @since 2.0 |
|
432 | - * |
|
433 | - * @param \GV\Template_Context $context The template context. |
|
434 | - */ |
|
435 | - do_action('gravityview/template/table/body/before', $context); |
|
436 | - |
|
437 | - /** |
|
438 | - * @action `gravityview_table_body_before` Inside the `tbody`, before any rows are rendered. Can be used to insert additional rows. |
|
439 | - * |
|
440 | - * @deprecated Use `gravityview/template/table/body/before` |
|
441 | - * @since 1.0.7 |
|
442 | - * |
|
443 | - * @param \GravityView_View $gravityview_view Current GravityView_View object. |
|
444 | - */ |
|
445 | - do_action('gravityview_table_body_before', \GravityView_View::getInstance() /** ugh! */); |
|
446 | - } |
|
447 | - |
|
448 | - /** |
|
449 | - * `gravityview_table_body_after` and `gravityview/template/table/body/after` actions. |
|
450 | - * |
|
451 | - * Output inside the `tbody` of the table. |
|
452 | - * |
|
453 | - * @param $context \GV\Template_Context The 2.0 context. |
|
454 | - * |
|
455 | - * @return void |
|
456 | - */ |
|
457 | - public static function body_after($context) |
|
458 | - { |
|
459 | - /** |
|
460 | - * @action `gravityview/template/table/body/after` Output inside the `tbody` of the table at the end. |
|
461 | - * |
|
462 | - * @since 2.0 |
|
463 | - * |
|
464 | - * @param \GV\Template_Context $context The template context. |
|
465 | - */ |
|
466 | - do_action('gravityview/template/table/body/after', $context); |
|
467 | - |
|
468 | - /** |
|
469 | - * @action `gravityview_table_body_after` Inside the `tbody`, after any rows are rendered. Can be used to insert additional rows. |
|
470 | - * |
|
471 | - * @deprecated Use `gravityview/template/table/body/after` |
|
472 | - * @since 1.0.7 |
|
473 | - * |
|
474 | - * @param \GravityView_View $gravityview_view Current GravityView_View object. |
|
475 | - */ |
|
476 | - do_action('gravityview_table_body_after', \GravityView_View::getInstance() /** ugh! */); |
|
477 | - } |
|
478 | - |
|
479 | - /** |
|
480 | - * `gravityview_table_tr_before` and `gravityview/template/table/tr/after` actions. |
|
481 | - * |
|
482 | - * Output inside the `tr` of the table. |
|
483 | - * |
|
484 | - * @param $context \GV\Template_Context The 2.0 context. |
|
485 | - * |
|
486 | - * @return void |
|
487 | - */ |
|
488 | - public static function tr_before($context) |
|
489 | - { |
|
490 | - /** |
|
491 | - * @action `gravityview/template/table/tr/before` Output inside the `tr` of the table when there are no results. |
|
492 | - * |
|
493 | - * @since 2.0 |
|
494 | - * |
|
495 | - * @param \GV\Template_Context $context The template context. |
|
496 | - */ |
|
497 | - do_action('gravityview/template/table/tr/before', $context); |
|
498 | - |
|
499 | - /** |
|
500 | - * @action `gravityview_table_tr_before` Before the `tr` while rendering each entry in the loop. Can be used to insert additional table rows. |
|
501 | - * |
|
502 | - * @since 1.0.7 |
|
503 | - * @deprecated USe `gravityview/template/table/tr/before` |
|
504 | - * |
|
505 | - * @param \GravityView_View $gravityview_view Current GraivtyView_View object. |
|
506 | - */ |
|
507 | - do_action('gravityview_table_tr_before', \GravityView_View::getInstance() /** ugh! */); |
|
508 | - } |
|
509 | - |
|
510 | - /** |
|
511 | - * `gravityview_table_tr_after` and `gravityview/template/table/tr/after` actions. |
|
512 | - * |
|
513 | - * Output inside the `tr` of the table. |
|
514 | - * |
|
515 | - * @param $context \GV\Template_Context The 2.0 context. |
|
516 | - * |
|
517 | - * @return void |
|
518 | - */ |
|
519 | - public static function tr_after($context) |
|
520 | - { |
|
521 | - /** |
|
522 | - * @action `gravityview/template/table/tr/after` Output inside the `tr` of the table when there are no results. |
|
523 | - * |
|
524 | - * @since 2.0 |
|
525 | - * |
|
526 | - * @param \GV\Template_Context $context The template context. |
|
527 | - */ |
|
528 | - do_action('gravityview/template/table/tr/after', $context); |
|
529 | - |
|
530 | - /** |
|
531 | - * @action `gravityview_table_tr_after` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells. |
|
532 | - * |
|
533 | - * @since 1.0.7 |
|
534 | - * @deprecated USe `gravityview/template/table/tr/after` |
|
535 | - * |
|
536 | - * @param \GravityView_View $gravityview_view Current GravityView_View object. |
|
537 | - */ |
|
538 | - do_action('gravityview_table_tr_after', \GravityView_View::getInstance() /** ugh! */); |
|
539 | - } |
|
540 | - |
|
541 | - /** |
|
542 | - * `gravityview_entry_class` and `gravityview/template/table/entry/class` filters. |
|
543 | - * |
|
544 | - * Modify of the class of a row. |
|
545 | - * |
|
546 | - * @param string $class The class. |
|
547 | - * @param \GV\Entry $entry The entry. |
|
548 | - * @param \GV\Template_Context The context. |
|
549 | - * |
|
550 | - * @return string The classes. |
|
551 | - */ |
|
552 | - public static function entry_class($class, $entry, $context) |
|
553 | - { |
|
554 | - /** |
|
555 | - * @filter `gravityview_entry_class` Modify the class applied to the entry row. |
|
556 | - * |
|
557 | - * @param string $class Existing class. |
|
558 | - * @param array $entry Current entry being displayed |
|
559 | - * @param \GravityView_View $this Current GravityView_View object |
|
560 | - * |
|
561 | - * @deprecated Use `gravityview/template/table/entry/class` |
|
562 | - * |
|
563 | - * @return string The modified class. |
|
564 | - */ |
|
565 | - $class = apply_filters('gravityview_entry_class', $class, $entry->as_entry(), \GravityView_View::getInstance()); |
|
566 | - |
|
567 | - /** |
|
568 | - * @filter `gravityview/template/table/entry/class` Modify the class aplied to the entry row. |
|
569 | - * |
|
570 | - * @param string $class The existing class. |
|
571 | - * @param \GV\Template_Context The context. |
|
572 | - * |
|
573 | - * @return string The modified class. |
|
574 | - */ |
|
575 | - return apply_filters('gravityview/template/table/entry/class', $class, Template_Context::from_template($context->template, compact('entry'))); |
|
576 | - } |
|
363 | + } |
|
364 | + |
|
365 | + /** |
|
366 | + * Output a field cell. |
|
367 | + * |
|
368 | + * @param \GV\Field $field The field to be ouput. |
|
369 | + * @param \GV\Field $entry The entry this field is for. |
|
370 | + * |
|
371 | + * @return void |
|
372 | + */ |
|
373 | + public function the_field(\GV\Field $field, \GV\Entry $entry) |
|
374 | + { |
|
375 | + $form = $this->view->form; |
|
376 | + $single_entry = $entry; |
|
377 | + |
|
378 | + /** |
|
379 | + * Push legacy entry context. |
|
380 | + */ |
|
381 | + \GV\Mocks\Legacy_Context::load([ |
|
382 | + 'field' => $field, |
|
383 | + ]); |
|
384 | + |
|
385 | + if ($entry->is_multi()) { |
|
386 | + if (!$single_entry = $entry->from_field($field)) { |
|
387 | + echo '<td></td>'; |
|
388 | + |
|
389 | + return; |
|
390 | + } |
|
391 | + $form = GF_Form::by_id($field->form_id); |
|
392 | + } |
|
393 | + |
|
394 | + $renderer = new Field_Renderer(); |
|
395 | + $source = is_numeric($field->ID) ? $form : new Internal_Source(); |
|
396 | + |
|
397 | + $value = $renderer->render($field, $this->view, $source, $entry, $this->request); |
|
398 | + |
|
399 | + $context = Template_Context::from_template($this, compact('field')); |
|
400 | + $context->entry = $single_entry; |
|
401 | + |
|
402 | + $args = [ |
|
403 | + 'entry' => $entry->as_entry(), |
|
404 | + 'field' => is_numeric($field->ID) ? $field->as_configuration() : null, |
|
405 | + 'value' => $value, |
|
406 | + 'hide_empty' => false, |
|
407 | + 'zone_id' => 'directory_table-columns', |
|
408 | + 'label' => self::get_field_column_label($field, $context), |
|
409 | + 'markup' => '<td id="{{ field_id }}" class="{{ class }}" data-label="{{label_value:data-label}}">{{ value }}</td>', |
|
410 | + 'form' => $form, |
|
411 | + ]; |
|
412 | + |
|
413 | + /** Output. */ |
|
414 | + echo \gravityview_field_output($args, $context); |
|
415 | + } |
|
416 | + |
|
417 | + /** |
|
418 | + * `gravityview_table_body_before` and `gravityview/template/table/body/before` actions. |
|
419 | + * |
|
420 | + * Output inside the `tbody` of the table. |
|
421 | + * |
|
422 | + * @param $context \GV\Template_Context The 2.0 context. |
|
423 | + * |
|
424 | + * @return void |
|
425 | + */ |
|
426 | + public static function body_before($context) |
|
427 | + { |
|
428 | + /** |
|
429 | + * @action `gravityview/template/table/body/before` Output inside the `tbody` of the table. |
|
430 | + * |
|
431 | + * @since 2.0 |
|
432 | + * |
|
433 | + * @param \GV\Template_Context $context The template context. |
|
434 | + */ |
|
435 | + do_action('gravityview/template/table/body/before', $context); |
|
436 | + |
|
437 | + /** |
|
438 | + * @action `gravityview_table_body_before` Inside the `tbody`, before any rows are rendered. Can be used to insert additional rows. |
|
439 | + * |
|
440 | + * @deprecated Use `gravityview/template/table/body/before` |
|
441 | + * @since 1.0.7 |
|
442 | + * |
|
443 | + * @param \GravityView_View $gravityview_view Current GravityView_View object. |
|
444 | + */ |
|
445 | + do_action('gravityview_table_body_before', \GravityView_View::getInstance() /** ugh! */); |
|
446 | + } |
|
447 | + |
|
448 | + /** |
|
449 | + * `gravityview_table_body_after` and `gravityview/template/table/body/after` actions. |
|
450 | + * |
|
451 | + * Output inside the `tbody` of the table. |
|
452 | + * |
|
453 | + * @param $context \GV\Template_Context The 2.0 context. |
|
454 | + * |
|
455 | + * @return void |
|
456 | + */ |
|
457 | + public static function body_after($context) |
|
458 | + { |
|
459 | + /** |
|
460 | + * @action `gravityview/template/table/body/after` Output inside the `tbody` of the table at the end. |
|
461 | + * |
|
462 | + * @since 2.0 |
|
463 | + * |
|
464 | + * @param \GV\Template_Context $context The template context. |
|
465 | + */ |
|
466 | + do_action('gravityview/template/table/body/after', $context); |
|
467 | + |
|
468 | + /** |
|
469 | + * @action `gravityview_table_body_after` Inside the `tbody`, after any rows are rendered. Can be used to insert additional rows. |
|
470 | + * |
|
471 | + * @deprecated Use `gravityview/template/table/body/after` |
|
472 | + * @since 1.0.7 |
|
473 | + * |
|
474 | + * @param \GravityView_View $gravityview_view Current GravityView_View object. |
|
475 | + */ |
|
476 | + do_action('gravityview_table_body_after', \GravityView_View::getInstance() /** ugh! */); |
|
477 | + } |
|
478 | + |
|
479 | + /** |
|
480 | + * `gravityview_table_tr_before` and `gravityview/template/table/tr/after` actions. |
|
481 | + * |
|
482 | + * Output inside the `tr` of the table. |
|
483 | + * |
|
484 | + * @param $context \GV\Template_Context The 2.0 context. |
|
485 | + * |
|
486 | + * @return void |
|
487 | + */ |
|
488 | + public static function tr_before($context) |
|
489 | + { |
|
490 | + /** |
|
491 | + * @action `gravityview/template/table/tr/before` Output inside the `tr` of the table when there are no results. |
|
492 | + * |
|
493 | + * @since 2.0 |
|
494 | + * |
|
495 | + * @param \GV\Template_Context $context The template context. |
|
496 | + */ |
|
497 | + do_action('gravityview/template/table/tr/before', $context); |
|
498 | + |
|
499 | + /** |
|
500 | + * @action `gravityview_table_tr_before` Before the `tr` while rendering each entry in the loop. Can be used to insert additional table rows. |
|
501 | + * |
|
502 | + * @since 1.0.7 |
|
503 | + * @deprecated USe `gravityview/template/table/tr/before` |
|
504 | + * |
|
505 | + * @param \GravityView_View $gravityview_view Current GraivtyView_View object. |
|
506 | + */ |
|
507 | + do_action('gravityview_table_tr_before', \GravityView_View::getInstance() /** ugh! */); |
|
508 | + } |
|
509 | + |
|
510 | + /** |
|
511 | + * `gravityview_table_tr_after` and `gravityview/template/table/tr/after` actions. |
|
512 | + * |
|
513 | + * Output inside the `tr` of the table. |
|
514 | + * |
|
515 | + * @param $context \GV\Template_Context The 2.0 context. |
|
516 | + * |
|
517 | + * @return void |
|
518 | + */ |
|
519 | + public static function tr_after($context) |
|
520 | + { |
|
521 | + /** |
|
522 | + * @action `gravityview/template/table/tr/after` Output inside the `tr` of the table when there are no results. |
|
523 | + * |
|
524 | + * @since 2.0 |
|
525 | + * |
|
526 | + * @param \GV\Template_Context $context The template context. |
|
527 | + */ |
|
528 | + do_action('gravityview/template/table/tr/after', $context); |
|
529 | + |
|
530 | + /** |
|
531 | + * @action `gravityview_table_tr_after` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells. |
|
532 | + * |
|
533 | + * @since 1.0.7 |
|
534 | + * @deprecated USe `gravityview/template/table/tr/after` |
|
535 | + * |
|
536 | + * @param \GravityView_View $gravityview_view Current GravityView_View object. |
|
537 | + */ |
|
538 | + do_action('gravityview_table_tr_after', \GravityView_View::getInstance() /** ugh! */); |
|
539 | + } |
|
540 | + |
|
541 | + /** |
|
542 | + * `gravityview_entry_class` and `gravityview/template/table/entry/class` filters. |
|
543 | + * |
|
544 | + * Modify of the class of a row. |
|
545 | + * |
|
546 | + * @param string $class The class. |
|
547 | + * @param \GV\Entry $entry The entry. |
|
548 | + * @param \GV\Template_Context The context. |
|
549 | + * |
|
550 | + * @return string The classes. |
|
551 | + */ |
|
552 | + public static function entry_class($class, $entry, $context) |
|
553 | + { |
|
554 | + /** |
|
555 | + * @filter `gravityview_entry_class` Modify the class applied to the entry row. |
|
556 | + * |
|
557 | + * @param string $class Existing class. |
|
558 | + * @param array $entry Current entry being displayed |
|
559 | + * @param \GravityView_View $this Current GravityView_View object |
|
560 | + * |
|
561 | + * @deprecated Use `gravityview/template/table/entry/class` |
|
562 | + * |
|
563 | + * @return string The modified class. |
|
564 | + */ |
|
565 | + $class = apply_filters('gravityview_entry_class', $class, $entry->as_entry(), \GravityView_View::getInstance()); |
|
566 | + |
|
567 | + /** |
|
568 | + * @filter `gravityview/template/table/entry/class` Modify the class aplied to the entry row. |
|
569 | + * |
|
570 | + * @param string $class The existing class. |
|
571 | + * @param \GV\Template_Context The context. |
|
572 | + * |
|
573 | + * @return string The modified class. |
|
574 | + */ |
|
575 | + return apply_filters('gravityview/template/table/entry/class', $class, Template_Context::from_template($context->template, compact('entry'))); |
|
576 | + } |
|
577 | 577 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | namespace GV; |
4 | 4 | |
5 | 5 | /** If this file is called directly, abort. */ |
6 | -if (!defined('GRAVITYVIEW_DIR')) { |
|
6 | +if ( ! defined( 'GRAVITYVIEW_DIR' ) ) { |
|
7 | 7 | exit(); |
8 | 8 | } |
9 | 9 | |
@@ -28,11 +28,11 @@ discard block |
||
28 | 28 | * @param Entry_Collection $entries |
29 | 29 | * @param Request $request |
30 | 30 | */ |
31 | - public function __construct(View $view, Entry_Collection $entries, Request $request) |
|
31 | + public function __construct( View $view, Entry_Collection $entries, Request $request ) |
|
32 | 32 | { |
33 | - add_filter('gravityview/template/field/label', [__CLASS__, 'add_columns_sort_links'], 100, 2); |
|
33 | + add_filter( 'gravityview/template/field/label', [ __CLASS__, 'add_columns_sort_links' ], 100, 2 ); |
|
34 | 34 | |
35 | - parent::__construct($view, $entries, $request); |
|
35 | + parent::__construct( $view, $entries, $request ); |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | /** |
@@ -48,44 +48,44 @@ discard block |
||
48 | 48 | * |
49 | 49 | * @return string |
50 | 50 | */ |
51 | - public static function add_columns_sort_links($column_label, $context = null) |
|
51 | + public static function add_columns_sort_links( $column_label, $context = null ) |
|
52 | 52 | { |
53 | - $sort_columns = $context->view->settings->get('sort_columns'); |
|
53 | + $sort_columns = $context->view->settings->get( 'sort_columns' ); |
|
54 | 54 | |
55 | - if (empty($sort_columns)) { |
|
55 | + if ( empty( $sort_columns ) ) { |
|
56 | 56 | return $column_label; |
57 | 57 | } |
58 | 58 | |
59 | - if (!\GravityView_frontend::getInstance()->is_field_sortable($context->field->ID, $context->view->form->form)) { |
|
59 | + if ( ! \GravityView_frontend::getInstance()->is_field_sortable( $context->field->ID, $context->view->form->form ) ) { |
|
60 | 60 | return $column_label; |
61 | 61 | } |
62 | 62 | |
63 | - $sorting = []; |
|
63 | + $sorting = [ ]; |
|
64 | 64 | |
65 | - $directions = $context->view->settings->get('sort_direction'); |
|
65 | + $directions = $context->view->settings->get( 'sort_direction' ); |
|
66 | 66 | |
67 | - $sorts = Utils::_GET('sort'); |
|
67 | + $sorts = Utils::_GET( 'sort' ); |
|
68 | 68 | |
69 | - if ($sorts) { |
|
70 | - if (is_array($sorts)) { |
|
71 | - foreach ((array) $sorts as $key => $direction) { |
|
72 | - if ($key == $context->field->ID) { |
|
73 | - $sorting['key'] = $context->field->ID; |
|
74 | - $sorting['direction'] = strtolower($direction); |
|
69 | + if ( $sorts ) { |
|
70 | + if ( is_array( $sorts ) ) { |
|
71 | + foreach ( (array)$sorts as $key => $direction ) { |
|
72 | + if ( $key == $context->field->ID ) { |
|
73 | + $sorting[ 'key' ] = $context->field->ID; |
|
74 | + $sorting[ 'direction' ] = strtolower( $direction ); |
|
75 | 75 | break; |
76 | 76 | } |
77 | 77 | } |
78 | 78 | } else { |
79 | - if ($sorts == $context->field->ID) { |
|
80 | - $sorting['key'] = $context->field->ID; |
|
81 | - $sorting['direction'] = strtolower(Utils::_GET('dir', '')); |
|
79 | + if ( $sorts == $context->field->ID ) { |
|
80 | + $sorting[ 'key' ] = $context->field->ID; |
|
81 | + $sorting[ 'direction' ] = strtolower( Utils::_GET( 'dir', '' ) ); |
|
82 | 82 | } |
83 | 83 | } |
84 | 84 | } else { |
85 | - foreach ((array) $context->view->settings->get('sort_field', []) as $i => $sort_field) { |
|
86 | - if ($sort_field == $context->field->ID) { |
|
87 | - $sorting['key'] = $sort_field; |
|
88 | - $sorting['direction'] = strtolower(Utils::get($directions, $i, '')); |
|
85 | + foreach ( (array)$context->view->settings->get( 'sort_field', [ ] ) as $i => $sort_field ) { |
|
86 | + if ( $sort_field == $context->field->ID ) { |
|
87 | + $sorting[ 'key' ] = $sort_field; |
|
88 | + $sorting[ 'direction' ] = strtolower( Utils::get( $directions, $i, '' ) ); |
|
89 | 89 | break; // Only get the first sort |
90 | 90 | } |
91 | 91 | } |
@@ -93,28 +93,28 @@ discard block |
||
93 | 93 | |
94 | 94 | $class = 'gv-sort'; |
95 | 95 | |
96 | - $sort_field_id = \GravityView_frontend::_override_sorting_id_by_field_type($context->field->ID, $context->view->form->ID); |
|
96 | + $sort_field_id = \GravityView_frontend::_override_sorting_id_by_field_type( $context->field->ID, $context->view->form->ID ); |
|
97 | 97 | |
98 | 98 | $sort_args = [ |
99 | - sprintf('sort[%s]', $context->field->ID), |
|
99 | + sprintf( 'sort[%s]', $context->field->ID ), |
|
100 | 100 | 'asc', |
101 | 101 | ]; |
102 | 102 | |
103 | 103 | // If we are already sorting by the current field... |
104 | - if (!empty($sorting['key']) && (string) $sort_field_id === (string) $sorting['key']) { |
|
105 | - switch ($sorting['direction']) { |
|
104 | + if ( ! empty( $sorting[ 'key' ] ) && (string)$sort_field_id === (string)$sorting[ 'key' ] ) { |
|
105 | + switch ( $sorting[ 'direction' ] ) { |
|
106 | 106 | // No sort |
107 | 107 | case '': |
108 | - $sort_args[1] = 'asc'; |
|
108 | + $sort_args[ 1 ] = 'asc'; |
|
109 | 109 | $class .= ' gv-icon-caret-up-down'; |
110 | 110 | break; |
111 | 111 | case 'desc': |
112 | - $sort_args[1] = ''; |
|
112 | + $sort_args[ 1 ] = ''; |
|
113 | 113 | $class .= ' gv-icon-sort-asc'; |
114 | 114 | break; |
115 | 115 | case 'asc': |
116 | 116 | default: |
117 | - $sort_args[1] = 'desc'; |
|
117 | + $sort_args[ 1 ] = 'desc'; |
|
118 | 118 | $class .= ' gv-icon-sort-desc'; |
119 | 119 | break; |
120 | 120 | } |
@@ -122,19 +122,19 @@ discard block |
||
122 | 122 | $class .= ' gv-icon-caret-up-down'; |
123 | 123 | } |
124 | 124 | |
125 | - $url = remove_query_arg(['pagenum']); |
|
126 | - $url = remove_query_arg('sort', $url); |
|
127 | - $multisort_url = self::_get_multisort_url($url, $sort_args, $context->field->ID); |
|
125 | + $url = remove_query_arg( [ 'pagenum' ] ); |
|
126 | + $url = remove_query_arg( 'sort', $url ); |
|
127 | + $multisort_url = self::_get_multisort_url( $url, $sort_args, $context->field->ID ); |
|
128 | 128 | |
129 | - $url = add_query_arg($sort_args[0], $sort_args[1], $url); |
|
129 | + $url = add_query_arg( $sort_args[ 0 ], $sort_args[ 1 ], $url ); |
|
130 | 130 | |
131 | - $return = '<a href="'.esc_url_raw($url).'"'; |
|
131 | + $return = '<a href="' . esc_url_raw( $url ) . '"'; |
|
132 | 132 | |
133 | - if (!empty($multisort_url)) { |
|
134 | - $return .= ' data-multisort-href="'.esc_url_raw($multisort_url).'"'; |
|
133 | + if ( ! empty( $multisort_url ) ) { |
|
134 | + $return .= ' data-multisort-href="' . esc_url_raw( $multisort_url ) . '"'; |
|
135 | 135 | } |
136 | 136 | |
137 | - $return .= ' class="'.$class.'" ></a> '.$column_label; |
|
137 | + $return .= ' class="' . $class . '" ></a> ' . $column_label; |
|
138 | 138 | |
139 | 139 | return $return; |
140 | 140 | } |
@@ -153,36 +153,36 @@ discard block |
||
153 | 153 | * |
154 | 154 | * @return string Multisort URL, if there are multiple sorts. Otherwise, existing $url |
155 | 155 | */ |
156 | - public static function _get_multisort_url($url, $sort_args, $field_id) |
|
156 | + public static function _get_multisort_url( $url, $sort_args, $field_id ) |
|
157 | 157 | { |
158 | - $sorts = Utils::_GET('sort'); |
|
158 | + $sorts = Utils::_GET( 'sort' ); |
|
159 | 159 | |
160 | - if (!is_array($sorts)) { |
|
160 | + if ( ! is_array( $sorts ) ) { |
|
161 | 161 | return $url; |
162 | 162 | } |
163 | 163 | |
164 | 164 | $multisort_url = $url; |
165 | 165 | |
166 | 166 | // If the field has already been sorted by, add the field to the URL |
167 | - if (!in_array($field_id, $keys = array_keys($sorts))) { |
|
168 | - if (count($keys)) { |
|
169 | - $multisort_url = add_query_arg(sprintf('sort[%s]', end($keys)), $sorts[end($keys)], $multisort_url); |
|
170 | - $multisort_url = add_query_arg($sort_args[0], $sort_args[1], $multisort_url); |
|
167 | + if ( ! in_array( $field_id, $keys = array_keys( $sorts ) ) ) { |
|
168 | + if ( count( $keys ) ) { |
|
169 | + $multisort_url = add_query_arg( sprintf( 'sort[%s]', end( $keys ) ), $sorts[ end( $keys ) ], $multisort_url ); |
|
170 | + $multisort_url = add_query_arg( $sort_args[ 0 ], $sort_args[ 1 ], $multisort_url ); |
|
171 | 171 | } else { |
172 | - $multisort_url = add_query_arg($sort_args[0], $sort_args[1], $multisort_url); |
|
172 | + $multisort_url = add_query_arg( $sort_args[ 0 ], $sort_args[ 1 ], $multisort_url ); |
|
173 | 173 | } |
174 | 174 | } |
175 | 175 | // Otherwise, we are just updating the sort order |
176 | 176 | else { |
177 | 177 | |
178 | 178 | // Pass empty value to unset |
179 | - if ('' === $sort_args[1]) { |
|
180 | - unset($sorts[$field_id]); |
|
179 | + if ( '' === $sort_args[ 1 ] ) { |
|
180 | + unset( $sorts[ $field_id ] ); |
|
181 | 181 | } else { |
182 | - $sorts[$field_id] = $sort_args[1]; |
|
182 | + $sorts[ $field_id ] = $sort_args[ 1 ]; |
|
183 | 183 | } |
184 | 184 | |
185 | - $multisort_url = add_query_arg(['sort' => $sorts], $multisort_url); |
|
185 | + $multisort_url = add_query_arg( [ 'sort' => $sorts ], $multisort_url ); |
|
186 | 186 | } |
187 | 187 | |
188 | 188 | return $multisort_url; |
@@ -195,21 +195,21 @@ discard block |
||
195 | 195 | */ |
196 | 196 | public function the_columns() |
197 | 197 | { |
198 | - $fields = $this->view->fields->by_position('directory_table-columns'); |
|
198 | + $fields = $this->view->fields->by_position( 'directory_table-columns' ); |
|
199 | 199 | |
200 | - foreach ($fields->by_visible($this->view)->all() as $field) { |
|
201 | - $context = Template_Context::from_template($this, compact('field')); |
|
200 | + foreach ( $fields->by_visible( $this->view )->all() as $field ) { |
|
201 | + $context = Template_Context::from_template( $this, compact( 'field' ) ); |
|
202 | 202 | |
203 | 203 | $args = [ |
204 | - 'field' => is_numeric($field->ID) ? $field->as_configuration() : null, |
|
204 | + 'field' => is_numeric( $field->ID ) ? $field->as_configuration() : null, |
|
205 | 205 | 'hide_empty' => false, |
206 | 206 | 'zone_id' => 'directory_table-columns', |
207 | 207 | 'markup' => '<th id="{{ field_id }}" class="{{ class }}" style="{{width:style}}" data-label="{{label_value:data-label}}">{{label}}</th>', |
208 | 208 | 'label_markup' => '<span class="gv-field-label">{{ label }}</span>', |
209 | - 'label' => self::get_field_column_label($field, $context), |
|
209 | + 'label' => self::get_field_column_label( $field, $context ), |
|
210 | 210 | ]; |
211 | 211 | |
212 | - echo \gravityview_field_output($args, $context); |
|
212 | + echo \gravityview_field_output( $args, $context ); |
|
213 | 213 | } |
214 | 214 | } |
215 | 215 | |
@@ -221,15 +221,15 @@ discard block |
||
221 | 221 | * @param \GV\Field $field |
222 | 222 | * @param \GV\Template_Context $context |
223 | 223 | */ |
224 | - protected static function get_field_column_label($field, $context = null) |
|
224 | + protected static function get_field_column_label( $field, $context = null ) |
|
225 | 225 | { |
226 | - $form = $field->form_id ? GF_Form::by_id($field->form_id) : $context->view->form; |
|
226 | + $form = $field->form_id ? GF_Form::by_id( $field->form_id ) : $context->view->form; |
|
227 | 227 | |
228 | 228 | /** |
229 | 229 | * @deprecated Here for back-compatibility. |
230 | 230 | */ |
231 | - $column_label = apply_filters('gravityview_render_after_label', $field->get_label($context->view, $form), $field->as_configuration()); |
|
232 | - $column_label = apply_filters('gravityview/template/field_label', $column_label, $field->as_configuration(), ($form && $form->form) ? $form->form : null, null); |
|
231 | + $column_label = apply_filters( 'gravityview_render_after_label', $field->get_label( $context->view, $form ), $field->as_configuration() ); |
|
232 | + $column_label = apply_filters( 'gravityview/template/field_label', $column_label, $field->as_configuration(), ( $form && $form->form ) ? $form->form : null, null ); |
|
233 | 233 | |
234 | 234 | /** |
235 | 235 | * @filter `gravityview/template/field/label` Override the field label. |
@@ -239,7 +239,7 @@ discard block |
||
239 | 239 | * @param string $column_label The label to override. |
240 | 240 | * @param \GV\Template_Context $context The context. Does not have entry set here. |
241 | 241 | */ |
242 | - $column_label = apply_filters('gravityview/template/field/label', $column_label, $context); |
|
242 | + $column_label = apply_filters( 'gravityview/template/field/label', $column_label, $context ); |
|
243 | 243 | |
244 | 244 | return $column_label; |
245 | 245 | } |
@@ -252,18 +252,18 @@ discard block |
||
252 | 252 | * |
253 | 253 | * @return void |
254 | 254 | */ |
255 | - public function the_entry(\GV\Entry $entry, $attributes) |
|
255 | + public function the_entry( \GV\Entry $entry, $attributes ) |
|
256 | 256 | { |
257 | - $fields = $this->view->fields->by_position('directory_table-columns')->by_visible($this->view); |
|
257 | + $fields = $this->view->fields->by_position( 'directory_table-columns' )->by_visible( $this->view ); |
|
258 | 258 | |
259 | - $context = Template_Context::from_template($this, compact('entry', 'fields')); |
|
259 | + $context = Template_Context::from_template( $this, compact( 'entry', 'fields' ) ); |
|
260 | 260 | |
261 | 261 | /** |
262 | 262 | * Push legacy entry context. |
263 | 263 | */ |
264 | - \GV\Mocks\Legacy_Context::load([ |
|
264 | + \GV\Mocks\Legacy_Context::load( [ |
|
265 | 265 | 'entry' => $entry, |
266 | - ]); |
|
266 | + ] ); |
|
267 | 267 | |
268 | 268 | /** |
269 | 269 | * @filter `gravityview_table_cells` Modify the fields displayed in a table |
@@ -273,8 +273,8 @@ discard block |
||
273 | 273 | * |
274 | 274 | * @deprecated Use `gravityview/template/table/fields` |
275 | 275 | */ |
276 | - $fields = apply_filters('gravityview_table_cells', $fields->as_configuration(), \GravityView_View::getInstance()); |
|
277 | - $fields = Field_Collection::from_configuration($fields); |
|
276 | + $fields = apply_filters( 'gravityview_table_cells', $fields->as_configuration(), \GravityView_View::getInstance() ); |
|
277 | + $fields = Field_Collection::from_configuration( $fields ); |
|
278 | 278 | |
279 | 279 | /** |
280 | 280 | * @filter `gravityview/template/table/fields` Modify the fields displayed in this tables. |
@@ -284,9 +284,9 @@ discard block |
||
284 | 284 | * |
285 | 285 | * @since 2.0 |
286 | 286 | */ |
287 | - $fields = apply_filters('gravityview/template/table/fields', $fields, $context); |
|
287 | + $fields = apply_filters( 'gravityview/template/table/fields', $fields, $context ); |
|
288 | 288 | |
289 | - $context = Template_Context::from_template($this, compact('entry', 'fields')); |
|
289 | + $context = Template_Context::from_template( $this, compact( 'entry', 'fields' ) ); |
|
290 | 290 | |
291 | 291 | /** |
292 | 292 | * @filter `gravityview/template/table/entry/row/attributes` Filter the row attributes for the row in table view. |
@@ -296,13 +296,13 @@ discard block |
||
296 | 296 | * |
297 | 297 | * @since 2.0 |
298 | 298 | */ |
299 | - $attributes = apply_filters('gravityview/template/table/entry/row/attributes', $attributes, $context); |
|
299 | + $attributes = apply_filters( 'gravityview/template/table/entry/row/attributes', $attributes, $context ); |
|
300 | 300 | |
301 | 301 | /** Glue the attributes together. */ |
302 | - foreach ($attributes as $attribute => $value) { |
|
303 | - $attributes[$attribute] = sprintf("$attribute=\"%s\"", esc_attr($value)); |
|
302 | + foreach ( $attributes as $attribute => $value ) { |
|
303 | + $attributes[ $attribute ] = sprintf( "$attribute=\"%s\"", esc_attr( $value ) ); |
|
304 | 304 | } |
305 | - $attributes = implode(' ', $attributes); ?> |
|
305 | + $attributes = implode( ' ', $attributes ); ?> |
|
306 | 306 | <tr<?php echo $attributes ? " $attributes" : ''; ?>> |
307 | 307 | <?php |
308 | 308 | |
@@ -313,7 +313,7 @@ discard block |
||
313 | 313 | * |
314 | 314 | * @param \GV\Template_Context The context. |
315 | 315 | */ |
316 | - do_action('gravityview/template/table/cells/before', $context); |
|
316 | + do_action( 'gravityview/template/table/cells/before', $context ); |
|
317 | 317 | |
318 | 318 | /** |
319 | 319 | * @action `gravityview_table_cells_before` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells. |
@@ -324,19 +324,19 @@ discard block |
||
324 | 324 | * |
325 | 325 | * @deprecated Use `gravityview/template/table/cells/before` |
326 | 326 | */ |
327 | - do_action('gravityview_table_cells_before', \GravityView_View::getInstance()); |
|
327 | + do_action( 'gravityview_table_cells_before', \GravityView_View::getInstance() ); |
|
328 | 328 | |
329 | - foreach ($fields->all() as $field) { |
|
330 | - if (isset($this->view->unions[$entry['form_id']])) { |
|
331 | - if (isset($this->view->unions[$entry['form_id']][$field->ID])) { |
|
332 | - $field = $this->view->unions[$entry['form_id']][$field->ID]; |
|
329 | + foreach ( $fields->all() as $field ) { |
|
330 | + if ( isset( $this->view->unions[ $entry[ 'form_id' ] ] ) ) { |
|
331 | + if ( isset( $this->view->unions[ $entry[ 'form_id' ] ][ $field->ID ] ) ) { |
|
332 | + $field = $this->view->unions[ $entry[ 'form_id' ] ][ $field->ID ]; |
|
333 | 333 | } else { |
334 | - if (!$field instanceof Internal_Field) { |
|
335 | - $field = Internal_Field::from_configuration(['id' => 'custom']); |
|
334 | + if ( ! $field instanceof Internal_Field ) { |
|
335 | + $field = Internal_Field::from_configuration( [ 'id' => 'custom' ] ); |
|
336 | 336 | } |
337 | 337 | } |
338 | 338 | } |
339 | - $this->the_field($field, $entry); |
|
339 | + $this->the_field( $field, $entry ); |
|
340 | 340 | } |
341 | 341 | |
342 | 342 | /** |
@@ -346,7 +346,7 @@ discard block |
||
346 | 346 | * |
347 | 347 | * @param \GV\Template_Context The context. |
348 | 348 | */ |
349 | - do_action('gravityview/template/table/cells/after', $context); |
|
349 | + do_action( 'gravityview/template/table/cells/after', $context ); |
|
350 | 350 | |
351 | 351 | /** |
352 | 352 | * @action `gravityview_table_cells_after` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells. |
@@ -357,7 +357,7 @@ discard block |
||
357 | 357 | * |
358 | 358 | * @deprecated Use `gravityview/template/table/cells/after` |
359 | 359 | */ |
360 | - do_action('gravityview_table_cells_after', \GravityView_View::getInstance()); ?> |
|
360 | + do_action( 'gravityview_table_cells_after', \GravityView_View::getInstance() ); ?> |
|
361 | 361 | </tr> |
362 | 362 | <?php |
363 | 363 | } |
@@ -370,7 +370,7 @@ discard block |
||
370 | 370 | * |
371 | 371 | * @return void |
372 | 372 | */ |
373 | - public function the_field(\GV\Field $field, \GV\Entry $entry) |
|
373 | + public function the_field( \GV\Field $field, \GV\Entry $entry ) |
|
374 | 374 | { |
375 | 375 | $form = $this->view->form; |
376 | 376 | $single_entry = $entry; |
@@ -378,40 +378,40 @@ discard block |
||
378 | 378 | /** |
379 | 379 | * Push legacy entry context. |
380 | 380 | */ |
381 | - \GV\Mocks\Legacy_Context::load([ |
|
381 | + \GV\Mocks\Legacy_Context::load( [ |
|
382 | 382 | 'field' => $field, |
383 | - ]); |
|
383 | + ] ); |
|
384 | 384 | |
385 | - if ($entry->is_multi()) { |
|
386 | - if (!$single_entry = $entry->from_field($field)) { |
|
385 | + if ( $entry->is_multi() ) { |
|
386 | + if ( ! $single_entry = $entry->from_field( $field ) ) { |
|
387 | 387 | echo '<td></td>'; |
388 | 388 | |
389 | 389 | return; |
390 | 390 | } |
391 | - $form = GF_Form::by_id($field->form_id); |
|
391 | + $form = GF_Form::by_id( $field->form_id ); |
|
392 | 392 | } |
393 | 393 | |
394 | 394 | $renderer = new Field_Renderer(); |
395 | - $source = is_numeric($field->ID) ? $form : new Internal_Source(); |
|
395 | + $source = is_numeric( $field->ID ) ? $form : new Internal_Source(); |
|
396 | 396 | |
397 | - $value = $renderer->render($field, $this->view, $source, $entry, $this->request); |
|
397 | + $value = $renderer->render( $field, $this->view, $source, $entry, $this->request ); |
|
398 | 398 | |
399 | - $context = Template_Context::from_template($this, compact('field')); |
|
399 | + $context = Template_Context::from_template( $this, compact( 'field' ) ); |
|
400 | 400 | $context->entry = $single_entry; |
401 | 401 | |
402 | 402 | $args = [ |
403 | 403 | 'entry' => $entry->as_entry(), |
404 | - 'field' => is_numeric($field->ID) ? $field->as_configuration() : null, |
|
404 | + 'field' => is_numeric( $field->ID ) ? $field->as_configuration() : null, |
|
405 | 405 | 'value' => $value, |
406 | 406 | 'hide_empty' => false, |
407 | 407 | 'zone_id' => 'directory_table-columns', |
408 | - 'label' => self::get_field_column_label($field, $context), |
|
408 | + 'label' => self::get_field_column_label( $field, $context ), |
|
409 | 409 | 'markup' => '<td id="{{ field_id }}" class="{{ class }}" data-label="{{label_value:data-label}}">{{ value }}</td>', |
410 | 410 | 'form' => $form, |
411 | 411 | ]; |
412 | 412 | |
413 | 413 | /** Output. */ |
414 | - echo \gravityview_field_output($args, $context); |
|
414 | + echo \gravityview_field_output( $args, $context ); |
|
415 | 415 | } |
416 | 416 | |
417 | 417 | /** |
@@ -423,7 +423,7 @@ discard block |
||
423 | 423 | * |
424 | 424 | * @return void |
425 | 425 | */ |
426 | - public static function body_before($context) |
|
426 | + public static function body_before( $context ) |
|
427 | 427 | { |
428 | 428 | /** |
429 | 429 | * @action `gravityview/template/table/body/before` Output inside the `tbody` of the table. |
@@ -432,7 +432,7 @@ discard block |
||
432 | 432 | * |
433 | 433 | * @param \GV\Template_Context $context The template context. |
434 | 434 | */ |
435 | - do_action('gravityview/template/table/body/before', $context); |
|
435 | + do_action( 'gravityview/template/table/body/before', $context ); |
|
436 | 436 | |
437 | 437 | /** |
438 | 438 | * @action `gravityview_table_body_before` Inside the `tbody`, before any rows are rendered. Can be used to insert additional rows. |
@@ -442,7 +442,7 @@ discard block |
||
442 | 442 | * |
443 | 443 | * @param \GravityView_View $gravityview_view Current GravityView_View object. |
444 | 444 | */ |
445 | - do_action('gravityview_table_body_before', \GravityView_View::getInstance() /** ugh! */); |
|
445 | + do_action( 'gravityview_table_body_before', \GravityView_View::getInstance() /** ugh! */ ); |
|
446 | 446 | } |
447 | 447 | |
448 | 448 | /** |
@@ -454,7 +454,7 @@ discard block |
||
454 | 454 | * |
455 | 455 | * @return void |
456 | 456 | */ |
457 | - public static function body_after($context) |
|
457 | + public static function body_after( $context ) |
|
458 | 458 | { |
459 | 459 | /** |
460 | 460 | * @action `gravityview/template/table/body/after` Output inside the `tbody` of the table at the end. |
@@ -463,7 +463,7 @@ discard block |
||
463 | 463 | * |
464 | 464 | * @param \GV\Template_Context $context The template context. |
465 | 465 | */ |
466 | - do_action('gravityview/template/table/body/after', $context); |
|
466 | + do_action( 'gravityview/template/table/body/after', $context ); |
|
467 | 467 | |
468 | 468 | /** |
469 | 469 | * @action `gravityview_table_body_after` Inside the `tbody`, after any rows are rendered. Can be used to insert additional rows. |
@@ -473,7 +473,7 @@ discard block |
||
473 | 473 | * |
474 | 474 | * @param \GravityView_View $gravityview_view Current GravityView_View object. |
475 | 475 | */ |
476 | - do_action('gravityview_table_body_after', \GravityView_View::getInstance() /** ugh! */); |
|
476 | + do_action( 'gravityview_table_body_after', \GravityView_View::getInstance() /** ugh! */ ); |
|
477 | 477 | } |
478 | 478 | |
479 | 479 | /** |
@@ -485,7 +485,7 @@ discard block |
||
485 | 485 | * |
486 | 486 | * @return void |
487 | 487 | */ |
488 | - public static function tr_before($context) |
|
488 | + public static function tr_before( $context ) |
|
489 | 489 | { |
490 | 490 | /** |
491 | 491 | * @action `gravityview/template/table/tr/before` Output inside the `tr` of the table when there are no results. |
@@ -494,7 +494,7 @@ discard block |
||
494 | 494 | * |
495 | 495 | * @param \GV\Template_Context $context The template context. |
496 | 496 | */ |
497 | - do_action('gravityview/template/table/tr/before', $context); |
|
497 | + do_action( 'gravityview/template/table/tr/before', $context ); |
|
498 | 498 | |
499 | 499 | /** |
500 | 500 | * @action `gravityview_table_tr_before` Before the `tr` while rendering each entry in the loop. Can be used to insert additional table rows. |
@@ -504,7 +504,7 @@ discard block |
||
504 | 504 | * |
505 | 505 | * @param \GravityView_View $gravityview_view Current GraivtyView_View object. |
506 | 506 | */ |
507 | - do_action('gravityview_table_tr_before', \GravityView_View::getInstance() /** ugh! */); |
|
507 | + do_action( 'gravityview_table_tr_before', \GravityView_View::getInstance() /** ugh! */ ); |
|
508 | 508 | } |
509 | 509 | |
510 | 510 | /** |
@@ -516,7 +516,7 @@ discard block |
||
516 | 516 | * |
517 | 517 | * @return void |
518 | 518 | */ |
519 | - public static function tr_after($context) |
|
519 | + public static function tr_after( $context ) |
|
520 | 520 | { |
521 | 521 | /** |
522 | 522 | * @action `gravityview/template/table/tr/after` Output inside the `tr` of the table when there are no results. |
@@ -525,7 +525,7 @@ discard block |
||
525 | 525 | * |
526 | 526 | * @param \GV\Template_Context $context The template context. |
527 | 527 | */ |
528 | - do_action('gravityview/template/table/tr/after', $context); |
|
528 | + do_action( 'gravityview/template/table/tr/after', $context ); |
|
529 | 529 | |
530 | 530 | /** |
531 | 531 | * @action `gravityview_table_tr_after` Inside the `tr` while rendering each entry in the loop. Can be used to insert additional table cells. |
@@ -535,7 +535,7 @@ discard block |
||
535 | 535 | * |
536 | 536 | * @param \GravityView_View $gravityview_view Current GravityView_View object. |
537 | 537 | */ |
538 | - do_action('gravityview_table_tr_after', \GravityView_View::getInstance() /** ugh! */); |
|
538 | + do_action( 'gravityview_table_tr_after', \GravityView_View::getInstance() /** ugh! */ ); |
|
539 | 539 | } |
540 | 540 | |
541 | 541 | /** |
@@ -549,7 +549,7 @@ discard block |
||
549 | 549 | * |
550 | 550 | * @return string The classes. |
551 | 551 | */ |
552 | - public static function entry_class($class, $entry, $context) |
|
552 | + public static function entry_class( $class, $entry, $context ) |
|
553 | 553 | { |
554 | 554 | /** |
555 | 555 | * @filter `gravityview_entry_class` Modify the class applied to the entry row. |
@@ -562,7 +562,7 @@ discard block |
||
562 | 562 | * |
563 | 563 | * @return string The modified class. |
564 | 564 | */ |
565 | - $class = apply_filters('gravityview_entry_class', $class, $entry->as_entry(), \GravityView_View::getInstance()); |
|
565 | + $class = apply_filters( 'gravityview_entry_class', $class, $entry->as_entry(), \GravityView_View::getInstance() ); |
|
566 | 566 | |
567 | 567 | /** |
568 | 568 | * @filter `gravityview/template/table/entry/class` Modify the class aplied to the entry row. |
@@ -572,6 +572,6 @@ discard block |
||
572 | 572 | * |
573 | 573 | * @return string The modified class. |
574 | 574 | */ |
575 | - return apply_filters('gravityview/template/table/entry/class', $class, Template_Context::from_template($context->template, compact('entry'))); |
|
575 | + return apply_filters( 'gravityview/template/table/entry/class', $class, Template_Context::from_template( $context->template, compact( 'entry' ) ) ); |
|
576 | 576 | } |
577 | 577 | } |
@@ -12,8 +12,7 @@ discard block |
||
12 | 12 | * |
13 | 13 | * Renders a \GV\View and a \GV\Entry_Collection via a \GV\View_Renderer. |
14 | 14 | */ |
15 | -class View_Table_Template extends View_Template |
|
16 | -{ |
|
15 | +class View_Table_Template extends View_Template { |
|
17 | 16 | /** |
18 | 17 | * @var string The template slug to be loaded (like "table", "list") |
19 | 18 | */ |
@@ -28,8 +27,7 @@ discard block |
||
28 | 27 | * @param Entry_Collection $entries |
29 | 28 | * @param Request $request |
30 | 29 | */ |
31 | - public function __construct(View $view, Entry_Collection $entries, Request $request) |
|
32 | - { |
|
30 | + public function __construct(View $view, Entry_Collection $entries, Request $request) { |
|
33 | 31 | add_filter('gravityview/template/field/label', [__CLASS__, 'add_columns_sort_links'], 100, 2); |
34 | 32 | |
35 | 33 | parent::__construct($view, $entries, $request); |
@@ -48,8 +46,7 @@ discard block |
||
48 | 46 | * |
49 | 47 | * @return string |
50 | 48 | */ |
51 | - public static function add_columns_sort_links($column_label, $context = null) |
|
52 | - { |
|
49 | + public static function add_columns_sort_links($column_label, $context = null) { |
|
53 | 50 | $sort_columns = $context->view->settings->get('sort_columns'); |
54 | 51 | |
55 | 52 | if (empty($sort_columns)) { |
@@ -153,8 +150,7 @@ discard block |
||
153 | 150 | * |
154 | 151 | * @return string Multisort URL, if there are multiple sorts. Otherwise, existing $url |
155 | 152 | */ |
156 | - public static function _get_multisort_url($url, $sort_args, $field_id) |
|
157 | - { |
|
153 | + public static function _get_multisort_url($url, $sort_args, $field_id) { |
|
158 | 154 | $sorts = Utils::_GET('sort'); |
159 | 155 | |
160 | 156 | if (!is_array($sorts)) { |
@@ -193,8 +189,7 @@ discard block |
||
193 | 189 | * |
194 | 190 | * @return void |
195 | 191 | */ |
196 | - public function the_columns() |
|
197 | - { |
|
192 | + public function the_columns() { |
|
198 | 193 | $fields = $this->view->fields->by_position('directory_table-columns'); |
199 | 194 | |
200 | 195 | foreach ($fields->by_visible($this->view)->all() as $field) { |
@@ -221,8 +216,7 @@ discard block |
||
221 | 216 | * @param \GV\Field $field |
222 | 217 | * @param \GV\Template_Context $context |
223 | 218 | */ |
224 | - protected static function get_field_column_label($field, $context = null) |
|
225 | - { |
|
219 | + protected static function get_field_column_label($field, $context = null) { |
|
226 | 220 | $form = $field->form_id ? GF_Form::by_id($field->form_id) : $context->view->form; |
227 | 221 | |
228 | 222 | /** |
@@ -252,8 +246,7 @@ discard block |
||
252 | 246 | * |
253 | 247 | * @return void |
254 | 248 | */ |
255 | - public function the_entry(\GV\Entry $entry, $attributes) |
|
256 | - { |
|
249 | + public function the_entry(\GV\Entry $entry, $attributes) { |
|
257 | 250 | $fields = $this->view->fields->by_position('directory_table-columns')->by_visible($this->view); |
258 | 251 | |
259 | 252 | $context = Template_Context::from_template($this, compact('entry', 'fields')); |
@@ -370,8 +363,7 @@ discard block |
||
370 | 363 | * |
371 | 364 | * @return void |
372 | 365 | */ |
373 | - public function the_field(\GV\Field $field, \GV\Entry $entry) |
|
374 | - { |
|
366 | + public function the_field(\GV\Field $field, \GV\Entry $entry) { |
|
375 | 367 | $form = $this->view->form; |
376 | 368 | $single_entry = $entry; |
377 | 369 | |
@@ -423,8 +415,7 @@ discard block |
||
423 | 415 | * |
424 | 416 | * @return void |
425 | 417 | */ |
426 | - public static function body_before($context) |
|
427 | - { |
|
418 | + public static function body_before($context) { |
|
428 | 419 | /** |
429 | 420 | * @action `gravityview/template/table/body/before` Output inside the `tbody` of the table. |
430 | 421 | * |
@@ -454,8 +445,7 @@ discard block |
||
454 | 445 | * |
455 | 446 | * @return void |
456 | 447 | */ |
457 | - public static function body_after($context) |
|
458 | - { |
|
448 | + public static function body_after($context) { |
|
459 | 449 | /** |
460 | 450 | * @action `gravityview/template/table/body/after` Output inside the `tbody` of the table at the end. |
461 | 451 | * |
@@ -485,8 +475,7 @@ discard block |
||
485 | 475 | * |
486 | 476 | * @return void |
487 | 477 | */ |
488 | - public static function tr_before($context) |
|
489 | - { |
|
478 | + public static function tr_before($context) { |
|
490 | 479 | /** |
491 | 480 | * @action `gravityview/template/table/tr/before` Output inside the `tr` of the table when there are no results. |
492 | 481 | * |
@@ -516,8 +505,7 @@ discard block |
||
516 | 505 | * |
517 | 506 | * @return void |
518 | 507 | */ |
519 | - public static function tr_after($context) |
|
520 | - { |
|
508 | + public static function tr_after($context) { |
|
521 | 509 | /** |
522 | 510 | * @action `gravityview/template/table/tr/after` Output inside the `tr` of the table when there are no results. |
523 | 511 | * |
@@ -549,8 +537,7 @@ discard block |
||
549 | 537 | * |
550 | 538 | * @return string The classes. |
551 | 539 | */ |
552 | - public static function entry_class($class, $entry, $context) |
|
553 | - { |
|
540 | + public static function entry_class($class, $entry, $context) { |
|
554 | 541 | /** |
555 | 542 | * @filter `gravityview_entry_class` Modify the class applied to the entry row. |
556 | 543 | * |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | |
5 | 5 | /** If this file is called directly, abort. */ |
6 | 6 | if (!defined('GRAVITYVIEW_DIR')) { |
7 | - exit(); |
|
7 | + exit(); |
|
8 | 8 | } |
9 | 9 | |
10 | 10 | /** |
@@ -14,180 +14,180 @@ discard block |
||
14 | 14 | */ |
15 | 15 | class GF_Entry extends Entry implements \ArrayAccess |
16 | 16 | { |
17 | - /** |
|
18 | - * @var string The identifier of the backend used for this entry. |
|
19 | - * |
|
20 | - * @api |
|
21 | - * |
|
22 | - * @since 2.0 |
|
23 | - */ |
|
24 | - public static $backend = 'gravityforms'; |
|
25 | - |
|
26 | - /** |
|
27 | - * Initialization. |
|
28 | - */ |
|
29 | - private function __construct() |
|
30 | - { |
|
31 | - if (!class_exists('GFAPI')) { |
|
32 | - gravityview()->log->error('Gravity Forms plugin not active.'); |
|
33 | - } |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Construct a \GV\Entry instance by ID. |
|
38 | - * |
|
39 | - * @param int|string $entry_id The internal entry ID. |
|
40 | - * @param int $form_id The form ID, since slugs can be non-unique. Default: 0. |
|
41 | - * |
|
42 | - * @api |
|
43 | - * |
|
44 | - * @since 2.0 |
|
45 | - * |
|
46 | - * @return \GV\GF_Entry|null An instance of this entry or null if not found. |
|
47 | - */ |
|
48 | - public static function by_id($entry_id, $form_id = 0) |
|
49 | - { |
|
50 | - $entry = null; |
|
51 | - |
|
52 | - /** Always try to grab by numeric ID first. */ |
|
53 | - if (is_numeric($entry_id)) { |
|
54 | - $entry = \GFAPI::get_entry($entry_id); |
|
55 | - } |
|
56 | - |
|
57 | - if (!$entry || is_wp_error($entry)) { |
|
58 | - /** Hmm, slugs? Must be. */ |
|
59 | - if (apply_filters('gravityview_custom_entry_slug', false)) { |
|
60 | - return self::by_slug($entry_id, $form_id); |
|
61 | - } |
|
62 | - |
|
63 | - return null; |
|
64 | - } |
|
65 | - |
|
66 | - return self::from_entry($entry); |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Construct a \GV\Entry instance by slug name. |
|
71 | - * |
|
72 | - * @param int|string $entry_slug The registered slug for the entry. |
|
73 | - * @param int $form_id The form ID, since slugs can be non-unique. Default: 0. |
|
74 | - * |
|
75 | - * @api |
|
76 | - * |
|
77 | - * @since 2.0 |
|
78 | - * |
|
79 | - * @return \GV\GF_Entry|null An instance of this entry or null if not found. |
|
80 | - */ |
|
81 | - public static function by_slug($entry_slug, $form_id = 0) |
|
82 | - { |
|
83 | - global $wpdb; |
|
84 | - |
|
85 | - if (version_compare(\GFFormsModel::get_database_version(), '2.3-dev-1', '>=')) { |
|
86 | - $entry_meta = \GFFormsModel::get_entry_meta_table_name(); |
|
87 | - $sql = "SELECT entry_id FROM $entry_meta"; |
|
88 | - } else { |
|
89 | - $lead_meta = \GFFormsModel::get_lead_meta_table_name(); |
|
90 | - $sql = "SELECT lead_id FROM $lead_meta"; |
|
91 | - } |
|
92 | - |
|
93 | - $sql = "$sql WHERE meta_key = 'gravityview_unique_id' AND"; |
|
94 | - |
|
95 | - if ($form_id = apply_filters('gravityview/common/get_entry_id_from_slug/form_id', $form_id)) { |
|
96 | - $sql = $wpdb->prepare("$sql meta_value = %s AND form_id = %s", $entry_slug, $form_id); |
|
97 | - } else { |
|
98 | - $sql = $wpdb->prepare("$sql meta_value = %s", $entry_slug); |
|
99 | - } |
|
100 | - |
|
101 | - $entry_id = $wpdb->get_var($sql); |
|
102 | - |
|
103 | - if (!is_numeric($entry_id)) { |
|
104 | - return null; |
|
105 | - } |
|
106 | - |
|
107 | - return self::by_id($entry_id); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Construct a \GV\Entry instance from a Gravity Forms entry array. |
|
112 | - * |
|
113 | - * @param array $entry The entry array |
|
114 | - * |
|
115 | - * @return \GV\GF_Entry|null An instance of this entry or null if not found. |
|
116 | - */ |
|
117 | - public static function from_entry($entry) |
|
118 | - { |
|
119 | - if (empty($entry['id'])) { |
|
120 | - return null; |
|
121 | - } |
|
122 | - |
|
123 | - $self = new self(); |
|
124 | - $self->entry = $entry; |
|
125 | - |
|
126 | - $self->ID = $self->entry['id']; |
|
127 | - $self->slug = $self->get_slug(); |
|
128 | - |
|
129 | - return $self; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * ArrayAccess compatibility layer with a Gravity Forms entry array. |
|
134 | - * |
|
135 | - * @internal |
|
136 | - * |
|
137 | - * @deprecated |
|
138 | - * @since 2.0 |
|
139 | - * |
|
140 | - * @return bool Whether the offset exists or not. |
|
141 | - */ |
|
142 | - public function offsetExists($offset) |
|
143 | - { |
|
144 | - return isset($this->entry[$offset]); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * ArrayAccess compatibility layer with a Gravity Forms entry array. |
|
149 | - * |
|
150 | - * Maps the old keys to the new data; |
|
151 | - * |
|
152 | - * @internal |
|
153 | - * |
|
154 | - * @deprecated |
|
155 | - * @since 2.0 |
|
156 | - * |
|
157 | - * @return mixed The value of the requested entry data. |
|
158 | - */ |
|
159 | - public function offsetGet($offset) |
|
160 | - { |
|
161 | - return $this->entry[$offset]; |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * ArrayAccess compatibility layer with a Gravity Forms entry array. |
|
166 | - * |
|
167 | - * @internal |
|
168 | - * |
|
169 | - * @deprecated |
|
170 | - * @since 2.0 |
|
171 | - * |
|
172 | - * @return void |
|
173 | - */ |
|
174 | - public function offsetSet($offset, $value) |
|
175 | - { |
|
176 | - gravityview()->log->error('The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.'); |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * ArrayAccess compatibility layer with a Gravity Forms entry array. |
|
181 | - * |
|
182 | - * @internal |
|
183 | - * |
|
184 | - * @deprecated |
|
185 | - * @since 2.0 |
|
186 | - * |
|
187 | - * @return void |
|
188 | - */ |
|
189 | - public function offsetUnset($offset) |
|
190 | - { |
|
191 | - gravityview()->log->error('The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.'); |
|
192 | - } |
|
17 | + /** |
|
18 | + * @var string The identifier of the backend used for this entry. |
|
19 | + * |
|
20 | + * @api |
|
21 | + * |
|
22 | + * @since 2.0 |
|
23 | + */ |
|
24 | + public static $backend = 'gravityforms'; |
|
25 | + |
|
26 | + /** |
|
27 | + * Initialization. |
|
28 | + */ |
|
29 | + private function __construct() |
|
30 | + { |
|
31 | + if (!class_exists('GFAPI')) { |
|
32 | + gravityview()->log->error('Gravity Forms plugin not active.'); |
|
33 | + } |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Construct a \GV\Entry instance by ID. |
|
38 | + * |
|
39 | + * @param int|string $entry_id The internal entry ID. |
|
40 | + * @param int $form_id The form ID, since slugs can be non-unique. Default: 0. |
|
41 | + * |
|
42 | + * @api |
|
43 | + * |
|
44 | + * @since 2.0 |
|
45 | + * |
|
46 | + * @return \GV\GF_Entry|null An instance of this entry or null if not found. |
|
47 | + */ |
|
48 | + public static function by_id($entry_id, $form_id = 0) |
|
49 | + { |
|
50 | + $entry = null; |
|
51 | + |
|
52 | + /** Always try to grab by numeric ID first. */ |
|
53 | + if (is_numeric($entry_id)) { |
|
54 | + $entry = \GFAPI::get_entry($entry_id); |
|
55 | + } |
|
56 | + |
|
57 | + if (!$entry || is_wp_error($entry)) { |
|
58 | + /** Hmm, slugs? Must be. */ |
|
59 | + if (apply_filters('gravityview_custom_entry_slug', false)) { |
|
60 | + return self::by_slug($entry_id, $form_id); |
|
61 | + } |
|
62 | + |
|
63 | + return null; |
|
64 | + } |
|
65 | + |
|
66 | + return self::from_entry($entry); |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Construct a \GV\Entry instance by slug name. |
|
71 | + * |
|
72 | + * @param int|string $entry_slug The registered slug for the entry. |
|
73 | + * @param int $form_id The form ID, since slugs can be non-unique. Default: 0. |
|
74 | + * |
|
75 | + * @api |
|
76 | + * |
|
77 | + * @since 2.0 |
|
78 | + * |
|
79 | + * @return \GV\GF_Entry|null An instance of this entry or null if not found. |
|
80 | + */ |
|
81 | + public static function by_slug($entry_slug, $form_id = 0) |
|
82 | + { |
|
83 | + global $wpdb; |
|
84 | + |
|
85 | + if (version_compare(\GFFormsModel::get_database_version(), '2.3-dev-1', '>=')) { |
|
86 | + $entry_meta = \GFFormsModel::get_entry_meta_table_name(); |
|
87 | + $sql = "SELECT entry_id FROM $entry_meta"; |
|
88 | + } else { |
|
89 | + $lead_meta = \GFFormsModel::get_lead_meta_table_name(); |
|
90 | + $sql = "SELECT lead_id FROM $lead_meta"; |
|
91 | + } |
|
92 | + |
|
93 | + $sql = "$sql WHERE meta_key = 'gravityview_unique_id' AND"; |
|
94 | + |
|
95 | + if ($form_id = apply_filters('gravityview/common/get_entry_id_from_slug/form_id', $form_id)) { |
|
96 | + $sql = $wpdb->prepare("$sql meta_value = %s AND form_id = %s", $entry_slug, $form_id); |
|
97 | + } else { |
|
98 | + $sql = $wpdb->prepare("$sql meta_value = %s", $entry_slug); |
|
99 | + } |
|
100 | + |
|
101 | + $entry_id = $wpdb->get_var($sql); |
|
102 | + |
|
103 | + if (!is_numeric($entry_id)) { |
|
104 | + return null; |
|
105 | + } |
|
106 | + |
|
107 | + return self::by_id($entry_id); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Construct a \GV\Entry instance from a Gravity Forms entry array. |
|
112 | + * |
|
113 | + * @param array $entry The entry array |
|
114 | + * |
|
115 | + * @return \GV\GF_Entry|null An instance of this entry or null if not found. |
|
116 | + */ |
|
117 | + public static function from_entry($entry) |
|
118 | + { |
|
119 | + if (empty($entry['id'])) { |
|
120 | + return null; |
|
121 | + } |
|
122 | + |
|
123 | + $self = new self(); |
|
124 | + $self->entry = $entry; |
|
125 | + |
|
126 | + $self->ID = $self->entry['id']; |
|
127 | + $self->slug = $self->get_slug(); |
|
128 | + |
|
129 | + return $self; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * ArrayAccess compatibility layer with a Gravity Forms entry array. |
|
134 | + * |
|
135 | + * @internal |
|
136 | + * |
|
137 | + * @deprecated |
|
138 | + * @since 2.0 |
|
139 | + * |
|
140 | + * @return bool Whether the offset exists or not. |
|
141 | + */ |
|
142 | + public function offsetExists($offset) |
|
143 | + { |
|
144 | + return isset($this->entry[$offset]); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * ArrayAccess compatibility layer with a Gravity Forms entry array. |
|
149 | + * |
|
150 | + * Maps the old keys to the new data; |
|
151 | + * |
|
152 | + * @internal |
|
153 | + * |
|
154 | + * @deprecated |
|
155 | + * @since 2.0 |
|
156 | + * |
|
157 | + * @return mixed The value of the requested entry data. |
|
158 | + */ |
|
159 | + public function offsetGet($offset) |
|
160 | + { |
|
161 | + return $this->entry[$offset]; |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * ArrayAccess compatibility layer with a Gravity Forms entry array. |
|
166 | + * |
|
167 | + * @internal |
|
168 | + * |
|
169 | + * @deprecated |
|
170 | + * @since 2.0 |
|
171 | + * |
|
172 | + * @return void |
|
173 | + */ |
|
174 | + public function offsetSet($offset, $value) |
|
175 | + { |
|
176 | + gravityview()->log->error('The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.'); |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * ArrayAccess compatibility layer with a Gravity Forms entry array. |
|
181 | + * |
|
182 | + * @internal |
|
183 | + * |
|
184 | + * @deprecated |
|
185 | + * @since 2.0 |
|
186 | + * |
|
187 | + * @return void |
|
188 | + */ |
|
189 | + public function offsetUnset($offset) |
|
190 | + { |
|
191 | + gravityview()->log->error('The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.'); |
|
192 | + } |
|
193 | 193 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | namespace GV; |
4 | 4 | |
5 | 5 | /** If this file is called directly, abort. */ |
6 | -if (!defined('GRAVITYVIEW_DIR')) { |
|
6 | +if ( ! defined( 'GRAVITYVIEW_DIR' ) ) { |
|
7 | 7 | exit(); |
8 | 8 | } |
9 | 9 | |
@@ -28,8 +28,8 @@ discard block |
||
28 | 28 | */ |
29 | 29 | private function __construct() |
30 | 30 | { |
31 | - if (!class_exists('GFAPI')) { |
|
32 | - gravityview()->log->error('Gravity Forms plugin not active.'); |
|
31 | + if ( ! class_exists( 'GFAPI' ) ) { |
|
32 | + gravityview()->log->error( 'Gravity Forms plugin not active.' ); |
|
33 | 33 | } |
34 | 34 | } |
35 | 35 | |
@@ -45,25 +45,25 @@ discard block |
||
45 | 45 | * |
46 | 46 | * @return \GV\GF_Entry|null An instance of this entry or null if not found. |
47 | 47 | */ |
48 | - public static function by_id($entry_id, $form_id = 0) |
|
48 | + public static function by_id( $entry_id, $form_id = 0 ) |
|
49 | 49 | { |
50 | 50 | $entry = null; |
51 | 51 | |
52 | 52 | /** Always try to grab by numeric ID first. */ |
53 | - if (is_numeric($entry_id)) { |
|
54 | - $entry = \GFAPI::get_entry($entry_id); |
|
53 | + if ( is_numeric( $entry_id ) ) { |
|
54 | + $entry = \GFAPI::get_entry( $entry_id ); |
|
55 | 55 | } |
56 | 56 | |
57 | - if (!$entry || is_wp_error($entry)) { |
|
57 | + if ( ! $entry || is_wp_error( $entry ) ) { |
|
58 | 58 | /** Hmm, slugs? Must be. */ |
59 | - if (apply_filters('gravityview_custom_entry_slug', false)) { |
|
60 | - return self::by_slug($entry_id, $form_id); |
|
59 | + if ( apply_filters( 'gravityview_custom_entry_slug', false ) ) { |
|
60 | + return self::by_slug( $entry_id, $form_id ); |
|
61 | 61 | } |
62 | 62 | |
63 | 63 | return null; |
64 | 64 | } |
65 | 65 | |
66 | - return self::from_entry($entry); |
|
66 | + return self::from_entry( $entry ); |
|
67 | 67 | } |
68 | 68 | |
69 | 69 | /** |
@@ -78,11 +78,11 @@ discard block |
||
78 | 78 | * |
79 | 79 | * @return \GV\GF_Entry|null An instance of this entry or null if not found. |
80 | 80 | */ |
81 | - public static function by_slug($entry_slug, $form_id = 0) |
|
81 | + public static function by_slug( $entry_slug, $form_id = 0 ) |
|
82 | 82 | { |
83 | 83 | global $wpdb; |
84 | 84 | |
85 | - if (version_compare(\GFFormsModel::get_database_version(), '2.3-dev-1', '>=')) { |
|
85 | + if ( version_compare( \GFFormsModel::get_database_version(), '2.3-dev-1', '>=' ) ) { |
|
86 | 86 | $entry_meta = \GFFormsModel::get_entry_meta_table_name(); |
87 | 87 | $sql = "SELECT entry_id FROM $entry_meta"; |
88 | 88 | } else { |
@@ -92,19 +92,19 @@ discard block |
||
92 | 92 | |
93 | 93 | $sql = "$sql WHERE meta_key = 'gravityview_unique_id' AND"; |
94 | 94 | |
95 | - if ($form_id = apply_filters('gravityview/common/get_entry_id_from_slug/form_id', $form_id)) { |
|
96 | - $sql = $wpdb->prepare("$sql meta_value = %s AND form_id = %s", $entry_slug, $form_id); |
|
95 | + if ( $form_id = apply_filters( 'gravityview/common/get_entry_id_from_slug/form_id', $form_id ) ) { |
|
96 | + $sql = $wpdb->prepare( "$sql meta_value = %s AND form_id = %s", $entry_slug, $form_id ); |
|
97 | 97 | } else { |
98 | - $sql = $wpdb->prepare("$sql meta_value = %s", $entry_slug); |
|
98 | + $sql = $wpdb->prepare( "$sql meta_value = %s", $entry_slug ); |
|
99 | 99 | } |
100 | 100 | |
101 | - $entry_id = $wpdb->get_var($sql); |
|
101 | + $entry_id = $wpdb->get_var( $sql ); |
|
102 | 102 | |
103 | - if (!is_numeric($entry_id)) { |
|
103 | + if ( ! is_numeric( $entry_id ) ) { |
|
104 | 104 | return null; |
105 | 105 | } |
106 | 106 | |
107 | - return self::by_id($entry_id); |
|
107 | + return self::by_id( $entry_id ); |
|
108 | 108 | } |
109 | 109 | |
110 | 110 | /** |
@@ -114,16 +114,16 @@ discard block |
||
114 | 114 | * |
115 | 115 | * @return \GV\GF_Entry|null An instance of this entry or null if not found. |
116 | 116 | */ |
117 | - public static function from_entry($entry) |
|
117 | + public static function from_entry( $entry ) |
|
118 | 118 | { |
119 | - if (empty($entry['id'])) { |
|
119 | + if ( empty( $entry[ 'id' ] ) ) { |
|
120 | 120 | return null; |
121 | 121 | } |
122 | 122 | |
123 | 123 | $self = new self(); |
124 | 124 | $self->entry = $entry; |
125 | 125 | |
126 | - $self->ID = $self->entry['id']; |
|
126 | + $self->ID = $self->entry[ 'id' ]; |
|
127 | 127 | $self->slug = $self->get_slug(); |
128 | 128 | |
129 | 129 | return $self; |
@@ -139,9 +139,9 @@ discard block |
||
139 | 139 | * |
140 | 140 | * @return bool Whether the offset exists or not. |
141 | 141 | */ |
142 | - public function offsetExists($offset) |
|
142 | + public function offsetExists( $offset ) |
|
143 | 143 | { |
144 | - return isset($this->entry[$offset]); |
|
144 | + return isset( $this->entry[ $offset ] ); |
|
145 | 145 | } |
146 | 146 | |
147 | 147 | /** |
@@ -156,9 +156,9 @@ discard block |
||
156 | 156 | * |
157 | 157 | * @return mixed The value of the requested entry data. |
158 | 158 | */ |
159 | - public function offsetGet($offset) |
|
159 | + public function offsetGet( $offset ) |
|
160 | 160 | { |
161 | - return $this->entry[$offset]; |
|
161 | + return $this->entry[ $offset ]; |
|
162 | 162 | } |
163 | 163 | |
164 | 164 | /** |
@@ -171,9 +171,9 @@ discard block |
||
171 | 171 | * |
172 | 172 | * @return void |
173 | 173 | */ |
174 | - public function offsetSet($offset, $value) |
|
174 | + public function offsetSet( $offset, $value ) |
|
175 | 175 | { |
176 | - gravityview()->log->error('The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.'); |
|
176 | + gravityview()->log->error( 'The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.' ); |
|
177 | 177 | } |
178 | 178 | |
179 | 179 | /** |
@@ -186,8 +186,8 @@ discard block |
||
186 | 186 | * |
187 | 187 | * @return void |
188 | 188 | */ |
189 | - public function offsetUnset($offset) |
|
189 | + public function offsetUnset( $offset ) |
|
190 | 190 | { |
191 | - gravityview()->log->error('The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.'); |
|
191 | + gravityview()->log->error( 'The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.' ); |
|
192 | 192 | } |
193 | 193 | } |
@@ -26,8 +26,7 @@ discard block |
||
26 | 26 | /** |
27 | 27 | * Initialization. |
28 | 28 | */ |
29 | - private function __construct() |
|
30 | - { |
|
29 | + private function __construct() { |
|
31 | 30 | if (!class_exists('GFAPI')) { |
32 | 31 | gravityview()->log->error('Gravity Forms plugin not active.'); |
33 | 32 | } |
@@ -45,8 +44,7 @@ discard block |
||
45 | 44 | * |
46 | 45 | * @return \GV\GF_Entry|null An instance of this entry or null if not found. |
47 | 46 | */ |
48 | - public static function by_id($entry_id, $form_id = 0) |
|
49 | - { |
|
47 | + public static function by_id($entry_id, $form_id = 0) { |
|
50 | 48 | $entry = null; |
51 | 49 | |
52 | 50 | /** Always try to grab by numeric ID first. */ |
@@ -78,8 +76,7 @@ discard block |
||
78 | 76 | * |
79 | 77 | * @return \GV\GF_Entry|null An instance of this entry or null if not found. |
80 | 78 | */ |
81 | - public static function by_slug($entry_slug, $form_id = 0) |
|
82 | - { |
|
79 | + public static function by_slug($entry_slug, $form_id = 0) { |
|
83 | 80 | global $wpdb; |
84 | 81 | |
85 | 82 | if (version_compare(\GFFormsModel::get_database_version(), '2.3-dev-1', '>=')) { |
@@ -114,8 +111,7 @@ discard block |
||
114 | 111 | * |
115 | 112 | * @return \GV\GF_Entry|null An instance of this entry or null if not found. |
116 | 113 | */ |
117 | - public static function from_entry($entry) |
|
118 | - { |
|
114 | + public static function from_entry($entry) { |
|
119 | 115 | if (empty($entry['id'])) { |
120 | 116 | return null; |
121 | 117 | } |
@@ -139,8 +135,7 @@ discard block |
||
139 | 135 | * |
140 | 136 | * @return bool Whether the offset exists or not. |
141 | 137 | */ |
142 | - public function offsetExists($offset) |
|
143 | - { |
|
138 | + public function offsetExists($offset) { |
|
144 | 139 | return isset($this->entry[$offset]); |
145 | 140 | } |
146 | 141 | |
@@ -156,8 +151,7 @@ discard block |
||
156 | 151 | * |
157 | 152 | * @return mixed The value of the requested entry data. |
158 | 153 | */ |
159 | - public function offsetGet($offset) |
|
160 | - { |
|
154 | + public function offsetGet($offset) { |
|
161 | 155 | return $this->entry[$offset]; |
162 | 156 | } |
163 | 157 | |
@@ -171,8 +165,7 @@ discard block |
||
171 | 165 | * |
172 | 166 | * @return void |
173 | 167 | */ |
174 | - public function offsetSet($offset, $value) |
|
175 | - { |
|
168 | + public function offsetSet($offset, $value) { |
|
176 | 169 | gravityview()->log->error('The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.'); |
177 | 170 | } |
178 | 171 | |
@@ -186,8 +179,7 @@ discard block |
||
186 | 179 | * |
187 | 180 | * @return void |
188 | 181 | */ |
189 | - public function offsetUnset($offset) |
|
190 | - { |
|
182 | + public function offsetUnset($offset) { |
|
191 | 183 | gravityview()->log->error('The underlying Gravity Forms entry is immutable. This is a \GV\Entry object and should not be accessed as an array.'); |
192 | 184 | } |
193 | 185 | } |