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