Bez popisu

class-jetpack-contact-info-block.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Class Jetpack_Contact_Info_Block
  4. *
  5. * @package automattic/jetpack
  6. */
  7. /**
  8. * Helper class that lets us add schema attributes dynamically because they are not something that is store with the content.
  9. * Due to the limitations of wp_kses.
  10. *
  11. * @since 7.1.0
  12. */
  13. class Jetpack_Contact_Info_Block {
  14. /**
  15. * Adds contact info schema attributes.
  16. *
  17. * @param array $attr Array containing the contact info block attributes.
  18. * @param string $content String containing the contact info block content.
  19. *
  20. * @return string
  21. */
  22. public static function render( $attr, $content ) {
  23. Jetpack_Gutenberg::load_styles_as_required( 'contact-info' );
  24. return str_replace(
  25. 'class="wp-block-jetpack-contact-info', // Closing " intentionally ommited to that the user can also add the className as expected.
  26. 'itemprop="location" itemscope itemtype="http://schema.org/Organization" class="wp-block-jetpack-contact-info',
  27. $content
  28. );
  29. }
  30. /**
  31. * Adds address schema attributes.
  32. *
  33. * @param array $attr Array containing the address block attributes.
  34. * @param string $content String containing the address block content.
  35. *
  36. * @return string
  37. */
  38. public static function render_address( $attr, $content ) {
  39. // Returns empty content if the only attribute set is linkToGoogleMaps.
  40. if ( ! self::has_attributes( $attr, array( 'linkToGoogleMaps', 'className' ) ) ) {
  41. return '';
  42. }
  43. $find = array(
  44. 'class="wp-block-jetpack-address"',
  45. 'class="jetpack-address__address',
  46. // Closing " left out on purpose - there are multiple address fields and they all need to be updated with the same itemprop.
  47. 'class="jetpack-address__region"',
  48. 'class="jetpack-address__city"',
  49. 'class="jetpack-address__postal"',
  50. 'class="jetpack-address__country"',
  51. );
  52. $replace = array(
  53. 'itemprop="address" itemscope itemtype="http://schema.org/PostalAddress" class="wp-block-jetpack-address" ',
  54. 'itemprop="streetAddress" class="jetpack-address__address', // Closing " left out on purpose.
  55. 'itemprop="addressRegion" class="jetpack-address__region"',
  56. 'itemprop="addressLocality" class="jetpack-address__city"',
  57. 'itemprop="postalCode" class="jetpack-address__postal"',
  58. 'itemprop="addressCountry" class="jetpack-address__country"',
  59. );
  60. return str_replace( $find, $replace, $content );
  61. }
  62. /**
  63. * Helper function that lets us determine if a block has any valid attributes.
  64. *
  65. * @param array $attr Array containing the block attributes.
  66. * @param array $omit Array containing the block attributes that we ignore.
  67. *
  68. * @return string
  69. */
  70. public static function has_attributes( $attr, $omit = array() ) {
  71. foreach ( $attr as $attribute => $value ) {
  72. if ( ! in_array( $attribute, $omit, true ) && ! empty( $value ) ) {
  73. return true;
  74. }
  75. }
  76. return false;
  77. }
  78. /**
  79. * Adds email schema attributes.
  80. *
  81. * @param array $attr Array containing the email block attributes.
  82. * @param string $content String containing the email block content.
  83. *
  84. * @return string
  85. */
  86. public static function render_email( $attr, $content ) {
  87. $content = self::has_attributes( $attr, array( 'className' ) ) ?
  88. str_replace( 'href="mailto:', 'itemprop="email" href="mailto:', $content ) :
  89. '';
  90. return $content;
  91. }
  92. /**
  93. * Adds phone schema attributes. Also wraps the tel link in a span so that
  94. * it's recognized as a telephone number in Google's Structured Data.
  95. *
  96. * @param array $attr Array containing the phone block attributes.
  97. * @param string $content String containing the phone block content.
  98. *
  99. * @return string
  100. */
  101. public static function render_phone( $attr, $content ) {
  102. if ( self::has_attributes( $attr, array( 'className' ) ) ) {
  103. return str_replace(
  104. array( '<a href="tel:', '</a>' ),
  105. array( '<span itemprop="telephone"><a href="tel:', '</a></span>' ),
  106. $content
  107. );
  108. }
  109. return '';
  110. }
  111. }