class-acf-location-attachment.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'ACF_Location_Attachment' ) ) :
  6. class ACF_Location_Attachment 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 = 'attachment';
  18. $this->label = __( 'Attachment', 'acf' );
  19. $this->category = 'forms';
  20. $this->object_type = 'attachment';
  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['attachment'] ) ) {
  36. $attachment = $screen['attachment'];
  37. } else {
  38. return false;
  39. }
  40. // Get attachment mime type
  41. $mime_type = get_post_mime_type( $attachment );
  42. // Allow for unspecific mim_type matching such as "image" or "video".
  43. if ( ! strpos( $rule['value'], '/' ) ) {
  44. // Explode mime_type into bits ([0] => type, [1] => subtype) and match type.
  45. $bits = explode( '/', $mime_type );
  46. if ( $bits[0] === $rule['value'] ) {
  47. $mime_type = $rule['value'];
  48. }
  49. }
  50. return $this->compare_to_rule( $mime_type, $rule );
  51. }
  52. /**
  53. * Returns an array of possible values for this rule type.
  54. *
  55. * @date 9/4/20
  56. * @since 5.9.0
  57. *
  58. * @param array $rule A location rule.
  59. * @return array
  60. */
  61. public function get_values( $rule ) {
  62. $choices = array(
  63. 'all' => __( 'All', 'acf' ),
  64. );
  65. // Get mime types and append into optgroups.
  66. $mime_types = get_allowed_mime_types();
  67. foreach ( $mime_types as $regex => $mime_type ) {
  68. // Get type "image" from mime_type "image/jpeg".
  69. $type = current( explode( '/', $mime_type ) );
  70. // Append group and mimetype.
  71. $choices[ $type ][ $type ] = sprintf( __( 'All %s formats', 'acf' ), $type );
  72. $choices[ $type ][ $mime_type ] = "$regex ($mime_type)";
  73. }
  74. // return
  75. return $choices;
  76. }
  77. }
  78. // Register.
  79. acf_register_location_type( 'ACF_Location_Attachment' );
  80. endif; // class_exists check