cli
2 weeks ago
fields
2 weeks ago
widgets
2 weeks ago
Pods.php
2 weeks ago
PodsAPI.php
2 weeks ago
PodsAdmin.php
2 weeks ago
PodsArray.php
2 weeks ago
PodsComponent.php
2 weeks ago
PodsComponents.php
2 weeks ago
PodsData.php
2 weeks ago
PodsField.php
2 weeks ago
PodsForm.php
2 weeks ago
PodsI18n.php
2 weeks ago
PodsInit.php
2 weeks ago
PodsMeta.php
2 weeks ago
PodsMigrate.php
2 weeks ago
PodsRESTFields.php
2 weeks ago
PodsRESTHandlers.php
2 weeks ago
PodsTermSplitting.php
2 weeks ago
PodsUI.php
2 weeks ago
PodsView.php
2 weeks ago
PodsView.php
739 lines
| 1 | <?php |
| 2 | |
| 3 | use Pods\Static_Cache; |
| 4 | |
| 5 | /** |
| 6 | * @package Pods |
| 7 | */ |
| 8 | class PodsView { |
| 9 | |
| 10 | /** |
| 11 | * @var array $cache_modes Array of available cache modes |
| 12 | */ |
| 13 | public static $cache_modes = array( 'none', 'transient', 'site-transient', 'cache', 'static-cache', 'option-cache' ); |
| 14 | |
| 15 | /** |
| 16 | * @return \PodsView |
| 17 | */ |
| 18 | private function __construct() { |
| 19 | |
| 20 | // !nope |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * @static |
| 25 | * |
| 26 | * @param string $view Path of the view file |
| 27 | * @param array|null $data (optional) Data to pass on to the template |
| 28 | * @param bool|int|array $expires (optional) Time in seconds for the cache to expire, if 0 no expiration. |
| 29 | * @param string $cache_mode (optional) Decides the caching method to use for the view. |
| 30 | * @param bool $limited (optional) Whether to limit the view to only the theme directory, defaults to false |
| 31 | * |
| 32 | * @return bool|mixed|null|string|void |
| 33 | * |
| 34 | * @since 2.0.0 |
| 35 | */ |
| 36 | public static function view( $view, $data = null, $expires = false, $cache_mode = 'cache', $limited = false ) { |
| 37 | |
| 38 | /** |
| 39 | * Override the value of $view. For example, using Pods AJAX View. |
| 40 | * |
| 41 | * To use, set first param to true. If that param in not null, this method returns its value. |
| 42 | * |
| 43 | * @param null|bool If not set to null, this filter overrides the rest of the method. |
| 44 | * @param string $view Path of the view file |
| 45 | * @param array|null $data (optional) Data to pass on to the template |
| 46 | * @param bool|int|array $expires (optional) Time in seconds for the cache to expire, if 0 no expiration. |
| 47 | * @param string $cache_mode (optional) Decides the caching method to use for the view. |
| 48 | * @param bool $limited (optional) Whether to limit the view to only the theme directory, defaults to false |
| 49 | * |
| 50 | * @since 2.4.1 |
| 51 | */ |
| 52 | $filter_check = apply_filters( 'pods_view_alt_view', null, $view, $data, $expires, $cache_mode ); |
| 53 | |
| 54 | if ( null !== $filter_check ) { |
| 55 | return $filter_check; |
| 56 | } |
| 57 | |
| 58 | // Advanced $expires handling |
| 59 | $expires = self::expires( $expires, $cache_mode ); |
| 60 | |
| 61 | if ( ! in_array( $cache_mode, self::$cache_modes ) ) { |
| 62 | $cache_mode = 'cache'; |
| 63 | } |
| 64 | |
| 65 | // Support my-view.php?custom-key=X#hash keying for cache |
| 66 | $view_id = ''; |
| 67 | |
| 68 | if ( ! is_array( $view ) ) { |
| 69 | $view_q = explode( '?', $view ); |
| 70 | |
| 71 | if ( 1 < count( $view_q ) ) { |
| 72 | $view_id = '?' . $view_q[1]; |
| 73 | |
| 74 | $view = $view_q[0]; |
| 75 | } |
| 76 | |
| 77 | $view_h = explode( '#', $view ); |
| 78 | |
| 79 | if ( 1 < count( $view_h ) ) { |
| 80 | $view_id .= '#' . $view_h[1]; |
| 81 | |
| 82 | $view = $view_h[0]; |
| 83 | } |
| 84 | |
| 85 | // Support dynamic tags! |
| 86 | $view_id = pods_evaluate_tags( $view_id ); |
| 87 | } |
| 88 | |
| 89 | $view = apply_filters( 'pods_view_inc', $view, $data, $expires, $cache_mode, $limited ); |
| 90 | |
| 91 | $view_key = $view; |
| 92 | |
| 93 | if ( is_array( $view_key ) ) { |
| 94 | $view_key = implode( '-', $view_key ) . '.php'; |
| 95 | } |
| 96 | |
| 97 | if ( false !== realpath( $view_key ) ) { |
| 98 | $view_key = realpath( $view_key ); |
| 99 | } |
| 100 | |
| 101 | $pods_ui_dir = realpath( PODS_DIR . 'ui/' ); |
| 102 | $pods_components_dir = realpath( PODS_DIR . 'components/' ); |
| 103 | $abspath_dir = realpath( ABSPATH ); |
| 104 | |
| 105 | $cache_key = pods_str_replace( $abspath_dir, '/', $view_key, 1 ); |
| 106 | |
| 107 | $output = false; |
| 108 | |
| 109 | $caching = false; |
| 110 | |
| 111 | if ( false !== $expires && false === strpos( $view_key, $pods_ui_dir ) && false === strpos( $view_key, $pods_components_dir ) ) { |
| 112 | $caching = true; |
| 113 | } |
| 114 | |
| 115 | if ( $caching ) { |
| 116 | $output = self::get( 'pods-view-' . $cache_key . $view_id, $cache_mode, 'pods_view' ); |
| 117 | } |
| 118 | |
| 119 | if ( false === $output || null === $output ) { |
| 120 | $output = self::get_template_part( $view, $data ); |
| 121 | } |
| 122 | |
| 123 | if ( false !== $output && $caching ) { |
| 124 | self::set( 'pods-view-' . $cache_key . $view_id, $output, $expires, $cache_mode, 'pods_view' ); |
| 125 | } |
| 126 | |
| 127 | $output = apply_filters( "pods_view_output_{$cache_key}", $output, $view, $data, $expires, $cache_mode ); |
| 128 | $output = apply_filters( 'pods_view_output', $output, $view, $data, $expires, $cache_mode ); |
| 129 | |
| 130 | return $output; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Get the full path of the view if it exists. |
| 135 | * |
| 136 | * @since 3.1.0 |
| 137 | * |
| 138 | * @param string $view Path of the view file |
| 139 | * @param bool $limited (optional) Whether to limit the view to only the theme directory, defaults to false |
| 140 | * |
| 141 | * @return string|false The full path of the view if it exists. |
| 142 | */ |
| 143 | public static function view_get_path( $view, $limited = false ) { |
| 144 | // Support my-view.php?custom-key=X#hash keying for cache |
| 145 | if ( ! is_array( $view ) ) { |
| 146 | $view_q = explode( '?', $view ); |
| 147 | |
| 148 | if ( 1 < count( $view_q ) ) { |
| 149 | $view = $view_q[0]; |
| 150 | } |
| 151 | |
| 152 | $view_h = explode( '#', $view ); |
| 153 | |
| 154 | if ( 1 < count( $view_h ) ) { |
| 155 | $view = $view_h[0]; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | $view = apply_filters( 'pods_view_inc', $view, null, false, 'cache', $limited ); |
| 160 | |
| 161 | $view_key = $view; |
| 162 | |
| 163 | if ( is_array( $view_key ) ) { |
| 164 | $view_key = implode( '-', $view_key ) . '.php'; |
| 165 | } |
| 166 | |
| 167 | if ( false !== realpath( $view_key ) ) { |
| 168 | $view_key = realpath( $view_key ); |
| 169 | } |
| 170 | |
| 171 | $view_path = self::locate_template( $view_key, $limited ); |
| 172 | |
| 173 | if ( empty( $view_path ) ) { |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | return $view_path; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Get the cache key, salted with current Pods version, peppered with md5 if too long |
| 182 | * |
| 183 | * @param string $key |
| 184 | * @param string $group_key |
| 185 | * |
| 186 | * @return string |
| 187 | * |
| 188 | * @since 2.6.2 |
| 189 | */ |
| 190 | public static function get_key( $key, $group_key = '' ) { |
| 191 | |
| 192 | // Add some salt |
| 193 | $key .= '-' . sanitize_key( PODS_VERSION ); |
| 194 | |
| 195 | // Patch for limitations in DB |
| 196 | if ( 44 < strlen( $group_key . $key ) ) { |
| 197 | $key = md5( $key ); |
| 198 | } |
| 199 | |
| 200 | return $key; |
| 201 | |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * @static |
| 206 | * |
| 207 | * @param string $key Key for the cache |
| 208 | * @param string $cache_mode (optional) Decides the caching method to use for the view. |
| 209 | * @param string $group (optional) Set the group of the value. |
| 210 | * @param string $callback (optional) Callback function to run to set the value if not cached. |
| 211 | * |
| 212 | * @return bool|mixed|null|void |
| 213 | * |
| 214 | * @since 2.0.0 |
| 215 | */ |
| 216 | public static function get( $key, $cache_mode = 'cache', $group = '', $callback = null ) { |
| 217 | |
| 218 | $object_cache = false; |
| 219 | |
| 220 | if ( isset( $GLOBALS['wp_object_cache'] ) && is_object( $GLOBALS['wp_object_cache'] ) ) { |
| 221 | $object_cache = true; |
| 222 | } |
| 223 | |
| 224 | if ( ! in_array( $cache_mode, self::$cache_modes ) ) { |
| 225 | $cache_mode = 'cache'; |
| 226 | } |
| 227 | |
| 228 | $group_key = 'pods_'; |
| 229 | |
| 230 | if ( ! empty( $group ) ) { |
| 231 | $group_key = $group . '_'; |
| 232 | } |
| 233 | |
| 234 | $original_key = $key; |
| 235 | |
| 236 | // Get proper cache key |
| 237 | $key = self::get_key( $key, $group_key ); |
| 238 | |
| 239 | $value = null; |
| 240 | |
| 241 | $called = false; |
| 242 | |
| 243 | $pods_nocache = pods_v( 'pods_nocache' ); |
| 244 | $nocache = array(); |
| 245 | |
| 246 | if ( null !== $pods_nocache && pods_is_admin() ) { |
| 247 | if ( 1 < strlen( $pods_nocache ) ) { |
| 248 | $nocache = explode( ',', $pods_nocache ); |
| 249 | } else { |
| 250 | $nocache = self::$cache_modes; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | $cache_enabled = ! in_array( $cache_mode, $nocache, true ); |
| 255 | |
| 256 | if ( apply_filters( 'pods_view_cache_alt_get', false, $cache_mode, $group_key . $key, $original_key, $group ) ) { |
| 257 | $value = apply_filters( 'pods_view_cache_alt_get_value', $value, $cache_mode, $group_key . $key, $original_key, $group ); |
| 258 | } elseif ( $cache_enabled ) { |
| 259 | if ( 'transient' === $cache_mode ) { |
| 260 | $value = get_transient( $group_key . $key ); |
| 261 | } elseif ( 'site-transient' === $cache_mode ) { |
| 262 | $value = get_site_transient( $group_key . $key ); |
| 263 | } elseif ( 'cache' === $cache_mode && $object_cache ) { |
| 264 | $value = wp_cache_get( $key, ( empty( $group ) ? 'pods_view' : $group ) ); |
| 265 | } elseif ( 'option-cache' === $cache_mode ) { |
| 266 | global $_wp_using_ext_object_cache; |
| 267 | |
| 268 | $pre = apply_filters( "pre_transient_{$key}", false ); |
| 269 | |
| 270 | if ( false !== $pre ) { |
| 271 | $value = $pre; |
| 272 | } elseif ( $_wp_using_ext_object_cache ) { |
| 273 | $cache_found = false; |
| 274 | |
| 275 | $value = wp_cache_get( $key, ( empty( $group ) ? 'pods_option_cache' : $group ), false, $cache_found ); |
| 276 | |
| 277 | if ( false === $value || ! $cache_found ) { |
| 278 | if ( is_callable( $callback ) ) { |
| 279 | // Callback function should do it's own set/update for cache |
| 280 | $callback_value = call_user_func( $callback, $original_key, $group, $cache_mode ); |
| 281 | |
| 282 | if ( null !== $callback_value && false !== $callback_value ) { |
| 283 | $value = $callback_value; |
| 284 | } |
| 285 | |
| 286 | $called = true; |
| 287 | } |
| 288 | } |
| 289 | } else { |
| 290 | $transient_option = '_pods_option_' . $key; |
| 291 | $transient_timeout = '_pods_option_timeout_' . $key; |
| 292 | |
| 293 | $value = get_option( $transient_option ); |
| 294 | $timeout = get_option( $transient_timeout ); |
| 295 | |
| 296 | if ( ! empty( $timeout ) && $timeout < time() ) { |
| 297 | if ( is_callable( $callback ) ) { |
| 298 | // Callback function should do it's own set/update for cache |
| 299 | $callback_value = call_user_func( $callback, $original_key, $group, $cache_mode ); |
| 300 | |
| 301 | if ( null !== $callback_value && false !== $callback_value ) { |
| 302 | $value = $callback_value; |
| 303 | } |
| 304 | |
| 305 | $called = true; |
| 306 | } else { |
| 307 | $value = false; |
| 308 | |
| 309 | delete_option( $transient_option ); |
| 310 | delete_option( $transient_timeout ); |
| 311 | } |
| 312 | } |
| 313 | }//end if |
| 314 | |
| 315 | if ( false !== $value ) { |
| 316 | $value = apply_filters( "transient_{$key}", $value ); |
| 317 | } |
| 318 | } elseif ( 'static-cache' === $cache_mode ) { |
| 319 | $static_cache = pods_container( Static_Cache::class ); |
| 320 | |
| 321 | if ( $static_cache ) { |
| 322 | $value = $static_cache->get( $key, ( empty( $group ) ? 'pods_view' : $group ) ); |
| 323 | } else { |
| 324 | $value = false; |
| 325 | } |
| 326 | } else { |
| 327 | $value = false; |
| 328 | }//end if |
| 329 | } else { |
| 330 | $value = false; |
| 331 | }//end if |
| 332 | |
| 333 | if ( false === $value && is_callable( $callback ) && ! $called ) { |
| 334 | // Callback function should do it's own set/update for cache |
| 335 | $callback_value = call_user_func( $callback, $original_key, $group, $cache_mode ); |
| 336 | |
| 337 | if ( null !== $callback_value && false !== $callback_value ) { |
| 338 | $value = $callback_value; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | $value = apply_filters( "pods_view_get_{$cache_mode}", $value, $original_key, $group ); |
| 343 | |
| 344 | return $value; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * @static |
| 349 | * |
| 350 | * Set a cached value |
| 351 | * |
| 352 | * @param string $key Key for the cache |
| 353 | * @param mixed $value Value to add to the cache |
| 354 | * @param int $expires (optional) Time in seconds for the cache to expire, if 0 no expiration. |
| 355 | * @param string $cache_mode (optional) Decides the caching method to use for the view. |
| 356 | * @param string $group (optional) Set the group of the value. |
| 357 | * |
| 358 | * @return bool|mixed|null|string|void |
| 359 | * |
| 360 | * @since 2.0.0 |
| 361 | */ |
| 362 | public static function set( $key, $value, $expires = 0, $cache_mode = null, $group = '' ) { |
| 363 | |
| 364 | $object_cache = false; |
| 365 | |
| 366 | if ( isset( $GLOBALS['wp_object_cache'] ) && is_object( $GLOBALS['wp_object_cache'] ) ) { |
| 367 | $object_cache = true; |
| 368 | } |
| 369 | |
| 370 | // Advanced $expires handling |
| 371 | $expires = self::expires( $expires, $cache_mode ); |
| 372 | |
| 373 | if ( ! in_array( $cache_mode, self::$cache_modes ) ) { |
| 374 | $cache_mode = 'cache'; |
| 375 | } |
| 376 | |
| 377 | $group_key = 'pods_'; |
| 378 | |
| 379 | if ( ! empty( $group ) ) { |
| 380 | $group_key = $group . '_'; |
| 381 | } |
| 382 | |
| 383 | $original_key = $key; |
| 384 | |
| 385 | // Get proper cache key |
| 386 | $key = self::get_key( $key, $group_key ); |
| 387 | |
| 388 | if ( apply_filters( 'pods_view_cache_alt_set', false, $cache_mode, $group_key . $key, $original_key, $value, $expires, $group ) ) { |
| 389 | return $value; |
| 390 | } elseif ( 'transient' === $cache_mode ) { |
| 391 | set_transient( $group_key . $key, $value, $expires ); |
| 392 | } elseif ( 'site-transient' === $cache_mode ) { |
| 393 | set_site_transient( $group_key . $key, $value, $expires ); |
| 394 | } elseif ( 'cache' === $cache_mode && $object_cache ) { |
| 395 | wp_cache_set( $key, $value, ( empty( $group ) ? 'pods_view' : $group ), $expires ); |
| 396 | } elseif ( 'option-cache' === $cache_mode ) { |
| 397 | global $_wp_using_ext_object_cache; |
| 398 | |
| 399 | $value = apply_filters( "pre_set_transient_{$key}", $value ); |
| 400 | |
| 401 | if ( $_wp_using_ext_object_cache ) { |
| 402 | $result = wp_cache_set( $key, $value, ( empty( $group ) ? 'pods_option_cache' : $group ), $expires ); |
| 403 | } else { |
| 404 | $transient_timeout = '_pods_option_timeout_' . $key; |
| 405 | $key = '_pods_option_' . $key; |
| 406 | |
| 407 | if ( false === get_option( $key ) ) { |
| 408 | if ( $expires ) { |
| 409 | add_option( $transient_timeout, time() + $expires, '', 'no' ); |
| 410 | } |
| 411 | |
| 412 | $result = add_option( $key, $value, '', 'no' ); |
| 413 | } else { |
| 414 | if ( $expires ) { |
| 415 | update_option( $transient_timeout, time() + $expires ); |
| 416 | } |
| 417 | |
| 418 | $result = update_option( $key, $value ); |
| 419 | } |
| 420 | }//end if |
| 421 | |
| 422 | if ( $result ) { |
| 423 | do_action( "set_transient_{$key}" ); |
| 424 | do_action( 'setted_transient', $key ); |
| 425 | } |
| 426 | } elseif ( 'static-cache' === $cache_mode ) { |
| 427 | $static_cache = pods_container( Static_Cache::class ); |
| 428 | |
| 429 | if ( $static_cache ) { |
| 430 | $static_cache->set( $key, $value, ( empty( $group ) ? __CLASS__ : $group ) ); |
| 431 | } |
| 432 | }//end if |
| 433 | |
| 434 | do_action( "pods_view_set_{$cache_mode}", $original_key, $value, $expires, $group ); |
| 435 | |
| 436 | return $value; |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * @static |
| 441 | * |
| 442 | * Clear a cached value |
| 443 | * |
| 444 | * @param string|bool $key Key for the cache |
| 445 | * @param string $cache_mode (optional) Decides the caching method to use for the view. |
| 446 | * @param string $group (optional) Set the group. |
| 447 | * |
| 448 | * @return bool |
| 449 | * |
| 450 | * @since 2.0.0 |
| 451 | */ |
| 452 | public static function clear( $key = true, $cache_mode = null, $group = '' ) { |
| 453 | |
| 454 | $object_cache = false; |
| 455 | |
| 456 | if ( isset( $GLOBALS['wp_object_cache'] ) && is_object( $GLOBALS['wp_object_cache'] ) ) { |
| 457 | $object_cache = true; |
| 458 | } |
| 459 | |
| 460 | global $wpdb; |
| 461 | |
| 462 | if ( ! in_array( $cache_mode, self::$cache_modes ) ) { |
| 463 | $cache_mode = 'cache'; |
| 464 | } |
| 465 | |
| 466 | $group_key = 'pods_'; |
| 467 | |
| 468 | if ( ! empty( $group ) ) { |
| 469 | $group_key = $group . '_'; |
| 470 | } |
| 471 | |
| 472 | $full_key = $key; |
| 473 | $original_key = $key; |
| 474 | |
| 475 | if ( true !== $key ) { |
| 476 | // Get proper cache key |
| 477 | $key = self::get_key( $key, $group_key ); |
| 478 | |
| 479 | $full_key = $group_key . $key; |
| 480 | } |
| 481 | |
| 482 | if ( apply_filters( 'pods_view_cache_alt_set', false, $cache_mode, $full_key, $original_key, '', 0, $group ) ) { |
| 483 | return true; |
| 484 | } elseif ( 'transient' === $cache_mode ) { |
| 485 | if ( true === $key ) { |
| 486 | $group_key = pods_sanitize_like( $group_key ); |
| 487 | |
| 488 | $wpdb->query( "DELETE FROM `{$wpdb->options}` WHERE option_name LIKE '_transient_{$group_key}%'" ); |
| 489 | |
| 490 | if ( $object_cache ) { |
| 491 | wp_cache_flush(); |
| 492 | } |
| 493 | } else { |
| 494 | delete_transient( $group_key . $key ); |
| 495 | } |
| 496 | } elseif ( 'site-transient' === $cache_mode ) { |
| 497 | if ( true === $key ) { |
| 498 | $group_key = pods_sanitize_like( $group_key ); |
| 499 | |
| 500 | $wpdb->query( "DELETE FROM `{$wpdb->options}` WHERE option_name LIKE '_site_transient_{$group_key}%'" ); |
| 501 | |
| 502 | if ( $object_cache ) { |
| 503 | wp_cache_flush(); |
| 504 | } |
| 505 | } else { |
| 506 | delete_site_transient( $group_key . $key ); |
| 507 | } |
| 508 | } elseif ( 'cache' === $cache_mode && $object_cache ) { |
| 509 | if ( true === $key ) { |
| 510 | wp_cache_flush(); |
| 511 | } else { |
| 512 | wp_cache_delete( ( empty( $key ) ? 'pods_view' : $key ), ( empty( $group ) ? 'pods_view' : $group ) ); |
| 513 | } |
| 514 | } elseif ( 'option-cache' === $cache_mode ) { |
| 515 | global $_wp_using_ext_object_cache; |
| 516 | |
| 517 | do_action( "delete_transient_{$key}", $key ); |
| 518 | |
| 519 | if ( $_wp_using_ext_object_cache ) { |
| 520 | $result = wp_cache_delete( $key, ( empty( $group ) ? 'pods_option_cache' : $group ) ); |
| 521 | |
| 522 | wp_cache_delete( '_timeout_' . $key, ( empty( $group ) ? 'pods_option_cache' : $group ) ); |
| 523 | } else { |
| 524 | $option_timeout = '_pods_option_timeout_' . $key; |
| 525 | $option = '_pods_option_' . $key; |
| 526 | |
| 527 | $result = delete_option( $option ); |
| 528 | |
| 529 | if ( $result ) { |
| 530 | delete_option( $option_timeout ); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | if ( $result ) { |
| 535 | do_action( 'deleted_transient', $key ); |
| 536 | } |
| 537 | } elseif ( 'static-cache' === $cache_mode ) { |
| 538 | $static_cache = pods_container( Static_Cache::class ); |
| 539 | |
| 540 | if ( $static_cache ) { |
| 541 | if ( true === $key ) { |
| 542 | $static_cache->flush( ( empty( $group ) ? 'pods_view' : $group ) ); |
| 543 | } else { |
| 544 | $static_cache->delete( ( empty( $key ) ? 'pods_view' : $key ), ( empty( $group ) ? 'pods_view' : $group ) ); |
| 545 | } |
| 546 | } |
| 547 | }//end if |
| 548 | |
| 549 | do_action( "pods_view_clear_{$cache_mode}", $original_key, $group ); |
| 550 | |
| 551 | return true; |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * @static |
| 556 | * |
| 557 | * @param $_view |
| 558 | * @param null|array $_data |
| 559 | * @param bool $limited (optional) Whether to limit the view to only the theme directory, defaults to false |
| 560 | * |
| 561 | * @return bool|mixed|string|void |
| 562 | */ |
| 563 | public static function get_template_part( $_view, $_data = null, $limited = false ) { |
| 564 | |
| 565 | /* |
| 566 | To be reviewed later, should have more checks and restrictions like a whitelist etc. |
| 567 | |
| 568 | if ( 0 === strpos( $_view, 'http://' ) || 0 === strpos( $_view, 'https://' ) ) { |
| 569 | $_view = apply_filters( 'pods_view_url_include', $_view ); |
| 570 | |
| 571 | if ( empty( $_view ) || ( defined( 'PODS_REMOTE_VIEWS' ) && PODS_REMOTE_VIEWS ) ) |
| 572 | return ''; |
| 573 | |
| 574 | $response = wp_remote_get( $_view ); |
| 575 | |
| 576 | return wp_remote_retrieve_body( $response ); |
| 577 | } |
| 578 | */ |
| 579 | |
| 580 | $_view = self::locate_template( $_view, $limited ); |
| 581 | |
| 582 | if ( empty( $_view ) ) { |
| 583 | return $_view; |
| 584 | } |
| 585 | |
| 586 | if ( ! empty( $_data ) && is_array( $_data ) ) { |
| 587 | // Only expose array entries whose keys are valid PHP variable names as template variables (EXTR_SKIP prevents overwriting existing scope), to help prevent security issues. |
| 588 | $_safe_data = array_filter( |
| 589 | $_data, |
| 590 | static function ( $key ) { |
| 591 | return is_string( $key ) && preg_match( '/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$/', $key ); |
| 592 | }, |
| 593 | ARRAY_FILTER_USE_KEY |
| 594 | ); |
| 595 | |
| 596 | extract( $_safe_data, EXTR_SKIP ); |
| 597 | } |
| 598 | |
| 599 | ob_start(); |
| 600 | require $_view; |
| 601 | $output = ob_get_clean(); |
| 602 | |
| 603 | return $output; |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * @static |
| 608 | * |
| 609 | * @param array|string $_view |
| 610 | * @param bool $limited (optional) Whether to limit the view to only the theme directory, defaults to false |
| 611 | * |
| 612 | * @return bool|mixed|string|void |
| 613 | */ |
| 614 | private static function locate_template( $_view, $limited = false ) { |
| 615 | if ( is_array( $_view ) ) { |
| 616 | $_views = []; |
| 617 | |
| 618 | if ( isset( $_view[0] ) && false === strpos( $_view[0], '.php' ) ) { |
| 619 | $_view_count = count( $_view ); |
| 620 | |
| 621 | for ( $_view_x = $_view_count; 0 < $_view_x; $_view_x -- ) { |
| 622 | $_view_v = array_slice( $_view, 0, $_view_x ); |
| 623 | |
| 624 | $_views[] = implode( '-', $_view_v ) . '.php'; |
| 625 | } |
| 626 | } else { |
| 627 | $_views = $_view; |
| 628 | } |
| 629 | |
| 630 | $_view = false; |
| 631 | |
| 632 | foreach ( $_views as $_view_check ) { |
| 633 | $_view = self::locate_template( $_view_check ); |
| 634 | |
| 635 | if ( ! empty( $_view ) ) { |
| 636 | break; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | return $_view; |
| 641 | }//end if |
| 642 | |
| 643 | $paths_to_check = [ 'plugins', 'pods', 'theme' ]; |
| 644 | |
| 645 | if ( $limited ) { |
| 646 | $paths_to_check = [ 'theme' ]; |
| 647 | } |
| 648 | |
| 649 | // Is the view's file somewhere within the plugin directory tree? |
| 650 | // Note: we include PODS_DIR for the case of symlinks (see issue #2945). |
| 651 | $located = pods_validate_safe_path( $_view, $paths_to_check ); |
| 652 | |
| 653 | /** |
| 654 | * Allow filtering the validated view file path to use. |
| 655 | * |
| 656 | * @since unknown |
| 657 | * |
| 658 | * @param string|false $located The validated view file path to use, or false if it was not valid. |
| 659 | * @param string $_view The original view file path to use. |
| 660 | */ |
| 661 | $located = apply_filters( 'pods_view_locate_template', $located, $_view ); |
| 662 | |
| 663 | if ( ! $located ) { |
| 664 | return false; |
| 665 | } |
| 666 | |
| 667 | return $located; |
| 668 | |
| 669 | } |
| 670 | |
| 671 | /** |
| 672 | * Advanced $expires handling |
| 673 | * |
| 674 | * @param array|bool|int $expires |
| 675 | * @param string $cache_mode |
| 676 | * |
| 677 | * @return bool|int |
| 678 | * |
| 679 | * @since 2.7.0 |
| 680 | * @static |
| 681 | */ |
| 682 | public static function expires( $expires, $cache_mode = 'cache' ) { |
| 683 | |
| 684 | // Different $expires if user is anonymous or logged in or specific capability |
| 685 | if ( is_array( $expires ) ) { |
| 686 | if ( ( isset( $expires['anonymous'] ) || isset( $expires['user_with_access'] ) ) && isset( $expires['user'] ) ) { |
| 687 | if ( isset( $expires['user_with_access'] ) ) { |
| 688 | $expires = array( |
| 689 | pods_v( 'anonymous', $expires, false ), |
| 690 | pods_v( 'user', $expires, false ), |
| 691 | pods_v( 'user_with_access', $expires, false ), |
| 692 | pods_v( 'capability', $expires, null, null, true ), |
| 693 | ); |
| 694 | } elseif ( isset( $expires['anonymous'] ) ) { |
| 695 | $expires = array( |
| 696 | pods_v( 'anonymous', $expires, false ), |
| 697 | pods_v( 'user', $expires, false ), |
| 698 | pods_v( 'capability', $expires, null, null, true ), |
| 699 | ); |
| 700 | } |
| 701 | } else { |
| 702 | $expires = array_values( $expires ); |
| 703 | } |
| 704 | |
| 705 | if ( 4 === count( $expires ) ) { |
| 706 | if ( ! is_user_logged_in() ) { |
| 707 | $expires = pods_v( 0, $expires, false ); |
| 708 | } else { |
| 709 | $user_no_access = pods_v( 1, $expires, false ); |
| 710 | $user_with_access = pods_v( 2, $expires, false ); |
| 711 | $capability = pods_v( 3, $expires, null, true ); |
| 712 | |
| 713 | $expires = pods_var_user( $user_no_access, $user_with_access, $capability ); |
| 714 | } |
| 715 | } else { |
| 716 | $anon = pods_v( 0, $expires, false ); |
| 717 | $user = pods_v( 1, $expires, false ); |
| 718 | $capability = pods_v( 2, $expires, null, true ); |
| 719 | |
| 720 | $expires = pods_var_user( $anon, $user, $capability ); |
| 721 | } |
| 722 | }//end if |
| 723 | |
| 724 | if ( 'none' === $cache_mode ) { |
| 725 | $expires = false; |
| 726 | } elseif ( false !== $expires ) { |
| 727 | $expires = (int) $expires; |
| 728 | |
| 729 | if ( $expires < 1 ) { |
| 730 | $expires = 0; |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | return $expires; |
| 735 | |
| 736 | } |
| 737 | |
| 738 | } |
| 739 |