This repository has been archived by the owner on Oct 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helpers.php
530 lines (429 loc) · 13.5 KB
/
Helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
<?php
/**
* Name: Pod Helpers
*
* Description: A holdover from Pods 1.x for backwards compatibility purposes, you most likely don't need these and we recommend you use our WP filters and actions instead.
*
* Version: 2.3
*
* Category: Advanced
*
* Menu Page: edit.php?post_type=_pods_helper
* Menu Add Page: post-new.php?post_type=_pods_helper
*
* @package Pods\Components
* @subpackage Helpers
*/
if ( class_exists( 'Pods_Helpers' ) ) {
return;
}
class Pods_Helpers extends PodsComponent {
/**
* Pods object
*
* @var object
*
* @since 2.0.0
*/
static $obj = null;
/**
* Object type
*
* @var string
*
* @since 2.0.0
*/
private $object_type = '_pods_helper';
/**
* {@inheritdoc}
*/
public function init() {
$args = array(
'label' => 'Pod Helpers',
'labels' => array( 'singular_name' => 'Pod Helper' ),
'public' => false,
'can_export' => false,
'show_ui' => true,
'show_in_menu' => false,
'query_var' => false,
'rewrite' => false,
'has_archive' => false,
'hierarchical' => false,
'supports' => array( 'title', 'author', 'revisions' ),
'menu_icon' => 'dashicons-pods',
);
if ( ! pods_is_admin() ) {
$args['capability_type'] = 'pods_helper';
}
$args = PodsInit::object_label_fix( $args, 'post_type' );
register_post_type( $this->object_type, apply_filters( 'pods_internal_register_post_type_object_helper', $args ) );
if ( is_admin() ) {
add_filter( 'post_updated_messages', array( $this, 'setup_updated_messages' ), 10, 1 );
add_action( 'add_meta_boxes_' . $this->object_type, array( $this, 'edit_page_form' ) );
add_action( 'pods_meta_groups', array( $this, 'add_meta_boxes' ) );
add_filter( 'get_post_metadata', array( $this, 'get_meta' ), 10, 4 );
add_filter( 'update_post_metadata', array( $this, 'save_meta' ), 10, 4 );
add_action( 'pods_meta_save_pre_post__pods_helper', array( $this, 'fix_filters' ), 10, 5 );
add_action( 'post_updated', array( $this, 'clear_cache' ), 10, 3 );
add_action( 'delete_post', array( $this, 'clear_cache' ), 10, 1 );
add_filter( 'post_row_actions', array( $this, 'remove_row_actions' ), 10, 2 );
add_filter( 'bulk_actions-edit-' . $this->object_type, array( $this, 'remove_bulk_actions' ) );
add_filter( 'builder_layout_filter_non_layout_post_types', array( $this, 'disable_builder_layout' ) );
}
}
public function disable_builder_layout( $post_types ) {
$post_types[] = $this->object_type;
return $post_types;
}
/**
* Update Post Type messages
*
* @param array $messages
*
* @return array
* @since 2.0.2
*/
public function setup_updated_messages( $messages ) {
global $post, $post_ID;
$post_type = get_post_type_object( $this->object_type );
$labels = $post_type->labels;
$messages[ $post_type->name ] = array(
1 => sprintf( __( '%1$s updated. <a href="%2$s">%3$s</a>', 'pods' ), $labels->singular_name, esc_url( get_permalink( $post_ID ) ), $labels->view_item ),
2 => __( 'Custom field updated.', 'pods' ),
3 => __( 'Custom field deleted.', 'pods' ),
4 => sprintf( __( '%s updated.', 'pods' ), $labels->singular_name ),
/* translators: %s: date and time of the revision */
5 => isset( $_GET['revision'] ) ? sprintf( __( '%1$s restored to revision from %2$s', 'pods' ), $labels->singular_name, wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
6 => sprintf( __( '%1$s published. <a href="%2$s">%3$s</a>', 'pods' ), $labels->singular_name, esc_url( get_permalink( $post_ID ) ), $labels->view_item ),
7 => sprintf( __( '%s saved.', 'pods' ), $labels->singular_name ),
8 => sprintf( __( '%1$s submitted. <a target="_blank" rel="noopener noreferrer" href="%2$s">Preview %3$s</a>', 'pods' ), $labels->singular_name, esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ), $labels->singular_name ),
9 => sprintf(
__( '%1$s scheduled for: <strong>%2$s</strong>. <a target="_blank" rel="noopener noreferrer" href="%3$s">Preview %4$s</a>', 'pods' ), $labels->singular_name,
// translators: Publish box date format, see http://php.net/date
date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink( $post_ID ) ), $labels->singular_name
),
10 => sprintf( __( '%1$s draft updated. <a target="_blank" rel="noopener noreferrer" href="%2$s">Preview %3$s</a>', 'pods' ), $labels->singular_name, esc_url( add_query_arg( 'preview', 'true', get_permalink( $post_ID ) ) ), $labels->singular_name ),
);
if ( false === (boolean) $post_type->public ) {
$messages[ $post_type->name ][1] = sprintf( __( '%s updated.', 'pods' ), $labels->singular_name );
$messages[ $post_type->name ][6] = sprintf( __( '%s published.', 'pods' ), $labels->singular_name );
$messages[ $post_type->name ][8] = sprintf( __( '%s submitted.', 'pods' ), $labels->singular_name );
$messages[ $post_type->name ][9] = sprintf(
__( '%1$s scheduled for: <strong>%2$s</strong>.', 'pods' ), $labels->singular_name,
// translators: Publish box date format, see http://php.net/date
date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) )
);
$messages[ $post_type->name ][10] = sprintf( __( '%s draft updated.', 'pods' ), $labels->singular_name );
}
return $messages;
}
/**
* Enqueue styles
*
* @since 2.0.0
*/
public function admin_assets() {
wp_enqueue_style( 'pods-styles' );
}
/**
* Fix filters, specifically removing balanceTags
*
* @since 2.0.1
*/
public function fix_filters( $data, $pod = null, $id = null, $groups = null, $post = null ) {
remove_filter( 'content_save_pre', 'balanceTags', 50 );
}
/**
* Remove unused row actions
*
* @since 2.0.5
*/
public function remove_row_actions( $actions, $post ) {
global $current_screen;
if ( ! is_object( $current_screen ) || $this->object_type != $current_screen->post_type ) {
return $actions;
}
if ( isset( $actions['view'] ) ) {
unset( $actions['view'] );
}
if ( isset( $actions['inline hide-if-no-js'] ) ) {
unset( $actions['inline hide-if-no-js'] );
}
// W3 Total Cache
if ( isset( $actions['pgcache_purge'] ) ) {
unset( $actions['pgcache_purge'] );
}
return $actions;
}
/**
* Remove unused bulk actions
*
* @since 2.0.5
*/
public function remove_bulk_actions( $actions ) {
if ( isset( $actions['edit'] ) ) {
unset( $actions['edit'] );
}
return $actions;
}
/**
* Clear cache on save
*
* @since 2.0.0
*/
public function clear_cache( $data, $pod = null, $id = null, $groups = null, $post = null ) {
$old_post = $id;
if ( ! is_object( $id ) ) {
$old_post = null;
}
if ( is_object( $post ) && $this->object_type != $post->post_type ) {
return;
}
if ( ! is_array( $data ) && 0 < $data ) {
$post = $data;
$post = get_post( $post );
}
if ( $this->object_type == $post->object_type ) {
pods_transient_clear( 'pods_object_helpers' );
}
}
/**
* Change post title placeholder text
*
* @since 2.0.0
*/
public function set_title_text( $text, $post ) {
return __( 'Enter helper name here', 'pods' );
}
/**
* Edit page form
*
* @since 2.0.0
*/
public function edit_page_form() {
global $post_type;
if ( $this->object_type != $post_type ) {
return;
}
add_filter( 'enter_title_here', array( $this, 'set_title_text' ), 10, 2 );
}
/**
* Add meta boxes to the page
*
* @since 2.0.0
*/
public function add_meta_boxes() {
$pod = array(
'name' => $this->object_type,
'type' => 'post_type',
);
if ( isset( PodsMeta::$post_types[ $pod['name'] ] ) ) {
return;
}
$fields = array(
array(
'name' => 'helper_type',
'label' => __( 'Helper Type', 'pods' ),
'type' => 'pick',
'default' => 'display',
'data' => array(
'input' => 'Input (change form fields)',
'display' => 'Display (change field output when using magic tags)',
'pre_save' => 'Pre-Save (change form fields before saving)',
'post_save' => 'Post-Save',
'pre_delete' => 'Pre-Delete',
'post_delete' => 'Post-Delete',
),
),
array(
'name' => 'code',
'label' => __( 'Code', 'pods' ),
'type' => 'code',
),
);
pods_group_add( $pod, __( 'Helper', 'pods' ), $fields, 'normal', 'high' );
}
/**
* Get the fields
*
* @param null $_null
* @param null $post_ID
* @param null $meta_key
* @param bool $single
*
* @return array|bool|int|mixed|null|string|void
*/
public function get_meta( $_null, $post_ID = null, $meta_key = null, $single = false ) {
if ( 'code' == $meta_key ) {
$post = get_post( $post_ID );
if ( is_object( $post ) && $this->object_type == $post->post_type ) {
return $post->post_content;
}
}
return $_null;
}
/**
* Save the fields
*
* @param $_null
* @param int $post_ID
* @param string $meta_key
* @param string $meta_value
*
* @return bool|int|null
*/
public function save_meta( $_null, $post_ID = null, $meta_key = null, $meta_value = null ) {
if ( 'code' == $meta_key ) {
$post = get_post( $post_ID );
if ( is_object( $post ) && $this->object_type == $post->post_type ) {
$postdata = array(
'ID' => $post_ID,
'post_content' => $meta_value,
);
remove_filter( current_filter(), array( $this, __FUNCTION__ ) );
$revisions = false;
if ( has_action( 'pre_post_update', 'wp_save_post_revision' ) ) {
remove_action( 'pre_post_update', 'wp_save_post_revision' );
$revisions = true;
}
wp_update_post( (object) $postdata );
// objects will be automatically sanitized
if ( $revisions ) {
add_action( 'pre_post_update', 'wp_save_post_revision' );
}
return true;
}//end if
}//end if
return $_null;
}
/**
* @static
*
* Run a helper within a Pod Page or WP Template
*
* $params['helper'] string Helper name
* $params['value'] string Value to run Helper on
* $params['name'] string Field name
*
* @param array $params An associative array of parameters
* @param null $obj
*
* @return mixed Anything returned by the helper
* @since 2.0.0
*/
public static function helper( $params, $obj = null ) {
/**
* @var $obj Pods
*/
if ( ! empty( $obj ) ) {
self::$obj =& $obj;
} else {
$obj =& self::$obj;
}
if ( empty( $obj ) || ! is_object( $obj ) ) {
return '';
}
$defaults = array(
'helper' => '',
'value' => '',
'name' => '',
'deprecated' => false,
);
if ( is_array( $params ) ) {
$params = array_merge( $defaults, $params );
} else {
$params = $defaults;
}
$params = (object) $params;
if ( empty( $params->helper ) ) {
return pods_error( __( 'Helper name required', 'pods' ), $obj );
} elseif ( ! is_array( $params->helper ) ) {
$params->helper = trim( $params->helper );
}
if ( ! isset( $params->value ) ) {
$params->value = null;
}
if ( true === $params->deprecated && is_array( $params->value ) && ! empty( $params->value ) && ! isset( $params->value[0] ) ) {
$params->value = array( $params->value );
}
if ( ! isset( $params->name ) ) {
$params->name = null;
}
$helper = get_page_by_title( $params->helper, OBJECT, '_pods_helper' );
if ( $helper ) {
$helper = [
'id' => $helper->ID,
'name' => $helper->post_title,
'code' => $helper->post_content,
'type' => str_replace( '_pods_', '', $helper->post_type ),
'slug' => $helper->post_name,
'options' => [],
];
}
ob_start();
if ( ! empty( $helper ) && ! empty( $helper['code'] ) ) {
$code = $helper['code'];
$code = str_replace( '$this->', '$obj->', $code );
$value =& $params->value;
$name =& $params->name;
$_safe_params = $params;
if ( ! defined( 'PODS_DISABLE_EVAL' ) || ! PODS_DISABLE_EVAL ) {
eval( "?>{$code}" );
} else {
echo $code;
}
$params = $_safe_params;
} elseif ( is_callable( (string) $params->helper ) ) {
$params->helper = (string) $params->helper;
$disallowed = array(
'system',
'exec',
'popen',
'eval',
'preg_replace',
'create_function',
'include',
'include_once',
'require',
'require_once',
);
$allowed = array();
/**
* Allows adjusting the disallowed callbacks as needed.
*
* @param array $disallowed List of callbacks not allowed.
* @param array $params Parameters used by Pods::helper() method.
*
* @since 2.7.0
*/
$disallowed = apply_filters( 'pods_helper_disallowed_callbacks', $disallowed, get_object_vars( $params ) );
/**
* Allows adjusting the allowed allowed callbacks as needed.
*
* @param array $allowed List of callbacks explicitly allowed.
* @param array $params Parameters used by Pods::helper() method.
*
* @since 2.7.0
*/
$allowed = apply_filters( 'pods_helper_allowed_callbacks', $allowed, get_object_vars( $params ) );
// Clean up helper callback (if string)
$params->helper = strip_tags( str_replace( array( '`', chr( 96 ) ), "'", $params->helper ) );
$is_allowed = false;
if ( ! empty( $allowed ) ) {
if ( in_array( $params->helper, $allowed, true ) ) {
$is_allowed = true;
}
} elseif ( ! in_array( $params->helper, $disallowed, true ) ) {
$is_allowed = true;
}
if ( $is_allowed ) {
echo call_user_func( $params->helper, $params->value, $params->name, $params, $obj );
}
}//end if
$slug = $helper['slug'];
$out = ob_get_clean();
$out = apply_filters( 'pods_helpers_post_helper', $out, $params, $helper );
$out = apply_filters( "pods_helpers_post_helper_{$slug}", $out, $params, $helper );
return $out;
}
}