class-acf-location-post-template.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'ACF_Location_Post_Template' ) ) :
  6. class ACF_Location_Post_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 = 'post_template';
  18. $this->label = __( 'Post Template', 'acf' );
  19. $this->category = 'post';
  20. $this->object_type = 'post';
  21. }
  22. /**
  23. * Matches the provided rule against the screen args returning a bool result.
  24. *
  25. * @date 9/4/20
  26. * @since 5.9.0
  27. *
  28. * @param array $rule The location rule.
  29. * @param array $screen The screen args.
  30. * @param array $field_group The field group settings.
  31. * @return bool
  32. */
  33. public function match( $rule, $screen, $field_group ) {
  34. // Check screen args.
  35. if ( isset( $screen['post_type'] ) ) {
  36. $post_type = $screen['post_type'];
  37. } elseif ( isset( $screen['post_id'] ) ) {
  38. $post_type = get_post_type( $screen['post_id'] );
  39. } else {
  40. return false;
  41. }
  42. // Check if this post type has templates.
  43. $post_templates = acf_get_post_templates();
  44. if ( ! isset( $post_templates[ $post_type ] ) ) {
  45. return false;
  46. }
  47. // Get page template allowing for screen or database value.
  48. if ( isset( $screen['page_template'] ) ) {
  49. $page_template = $screen['page_template'];
  50. } elseif ( isset( $screen['post_id'] ) ) {
  51. $page_template = get_post_meta( $screen['post_id'], '_wp_page_template', true );
  52. } else {
  53. $page_template = '';
  54. }
  55. // Treat empty value as default template.
  56. if ( $page_template === '' ) {
  57. $page_template = 'default';
  58. }
  59. // Compare rule against $page_template.
  60. return $this->compare_to_rule( $page_template, $rule );
  61. }
  62. /**
  63. * Returns an array of possible values for this rule type.
  64. *
  65. * @date 9/4/20
  66. * @since 5.9.0
  67. *
  68. * @param array $rule A location rule.
  69. * @return array
  70. */
  71. public function get_values( $rule ) {
  72. return array_merge(
  73. array(
  74. 'default' => apply_filters( 'default_page_template_title', __( 'Default Template', 'acf' ) ),
  75. ),
  76. acf_get_post_templates()
  77. );
  78. }
  79. /**
  80. * Returns the object_subtype connected to this location.
  81. *
  82. * @date 1/4/20
  83. * @since 5.9.0
  84. *
  85. * @param array $rule A location rule.
  86. * @return string|array
  87. */
  88. public function get_object_subtype( $rule ) {
  89. if ( $rule['operator'] === '==' ) {
  90. $post_templates = acf_get_post_templates();
  91. // If "default", return array of all post types which have templates.
  92. if ( $rule['value'] === 'default' ) {
  93. return array_keys( $post_templates );
  94. // Otherwise, generate list of post types that have the selected template.
  95. } else {
  96. $post_types = array();
  97. foreach ( $post_templates as $post_type => $templates ) {
  98. if ( isset( $templates[ $rule['value'] ] ) ) {
  99. $post_types[] = $post_type;
  100. }
  101. }
  102. return $post_types;
  103. }
  104. }
  105. return '';
  106. }
  107. }
  108. // initialize
  109. acf_register_location_type( 'ACF_Location_Post_Template' );
  110. endif; // class_exists check