class-acf-location-page-template.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'ACF_Location_Page_Template' ) ) :
  6. class ACF_Location_Page_Template extends ACF_Location {
  7. /**
  8. * Initializes props.
  9. *
  10. * @date 5/03/2014
  11. * @since 5.0.0
  12. *
  13. * @param void
  14. * @return void
  15. */
  16. public function initialize() {
  17. $this->name = 'page_template';
  18. $this->label = __( 'Page Template', 'acf' );
  19. $this->category = 'page';
  20. $this->object_type = 'post';
  21. $this->object_subtype = 'page';
  22. }
  23. /**
  24. * Matches the provided rule against the screen args returning a bool result.
  25. *
  26. * @date 9/4/20
  27. * @since 5.9.0
  28. *
  29. * @param array $rule The location rule.
  30. * @param array $screen The screen args.
  31. * @param array $field_group The field group settings.
  32. * @return bool
  33. */
  34. public function match( $rule, $screen, $field_group ) {
  35. // Check screen args.
  36. if ( isset( $screen['post_type'] ) ) {
  37. $post_type = $screen['post_type'];
  38. } elseif ( isset( $screen['post_id'] ) ) {
  39. $post_type = get_post_type( $screen['post_id'] );
  40. } else {
  41. return false;
  42. }
  43. // Page templates were extended in WordPress version 4.7 for all post types.
  44. // Prevent this rule (which is scoped to the "page" post type) appearing on all post types without a template selected (default template).
  45. if ( $rule['value'] === 'default' && $post_type !== 'page' ) {
  46. return false;
  47. }
  48. // Match rule using Post Template logic.
  49. return acf_get_location_type( 'post_template' )->match( $rule, $screen, $field_group );
  50. }
  51. /**
  52. * Returns an array of possible values for this rule type.
  53. *
  54. * @date 9/4/20
  55. * @since 5.9.0
  56. *
  57. * @param array $rule A location rule.
  58. * @return array
  59. */
  60. public function get_values( $rule ) {
  61. $post_templates = acf_get_post_templates();
  62. return array_merge(
  63. array(
  64. 'default' => apply_filters( 'default_page_template_title', __( 'Default Template', 'acf' ) ),
  65. ),
  66. $post_templates['page']
  67. );
  68. }
  69. }
  70. // Register.
  71. acf_register_location_type( 'ACF_Location_Page_Template' );
  72. endif; // class_exists check.