From 674bec56be5b3b15daf5aa73bbc1225c05e1c29d Mon Sep 17 00:00:00 2001 From: Karen Attfield Date: Thu, 24 Oct 2024 14:22:44 +0100 Subject: [PATCH 1/4] Rearranging how the Contact Info block register process is required - move back to two files --- .../class-jetpack-contact-info-block.php | 161 ++++++++++++++++++ .../blocks/contact-info/contact-info.php | 149 +--------------- 2 files changed, 164 insertions(+), 146 deletions(-) create mode 100644 projects/plugins/jetpack/extensions/blocks/contact-info/class-jetpack-contact-info-block.php diff --git a/projects/plugins/jetpack/extensions/blocks/contact-info/class-jetpack-contact-info-block.php b/projects/plugins/jetpack/extensions/blocks/contact-info/class-jetpack-contact-info-block.php new file mode 100644 index 0000000000000..3e3647953ce39 --- /dev/null +++ b/projects/plugins/jetpack/extensions/blocks/contact-info/class-jetpack-contact-info-block.php @@ -0,0 +1,161 @@ + __NAMESPACE__ . '\render', + ) + ); + + Blocks::jetpack_register_block( + 'jetpack/address', + array( + 'parent' => array( 'jetpack/contact-info' ), + 'render_callback' => __NAMESPACE__ . '\render_adress', + ) + ); + + Blocks::jetpack_register_block( + 'jetpack/email', + array( + 'parent' => array( 'jetpack/contact-info' ), + 'render_callback' => __NAMESPACE__ . '\render_email', + ) + ); + + Blocks::jetpack_register_block( + 'jetpack/phone', + array( + 'parent' => array( 'jetpack/contact-info' ), + 'render_callback' => __NAMESPACE__ . '\render_phone', + ) + ); + } + + /** + * Adds contact info schema attributes. + * + * @param array $attr Array containing the contact info block attributes. + * @param string $content String containing the contact info block content. + * + * @return string + */ + public static function render( $attr, $content ) { + Jetpack_Gutenberg::load_assets_as_required( __DIR__ ); + return str_replace( + 'class="wp-block-jetpack-contact-info', // Closing " intentionally ommited to that the user can also add the className as expected. + 'itemprop="location" itemscope itemtype="http://schema.org/Organization" class="wp-block-jetpack-contact-info', + $content + ); + } + + /** + * Adds address schema attributes. + * + * @param array $attr Array containing the address block attributes. + * @param string $content String containing the address block content. + * + * @return string + */ + public static function render_address( $attr, $content ) { + // Returns empty content if the only attribute set is linkToGoogleMaps. + if ( ! self::has_attributes( $attr, array( 'linkToGoogleMaps', 'className' ) ) ) { + return ''; + } + $find = array( + 'class="wp-block-jetpack-address"', + 'class="jetpack-address__address', + // Closing " left out on purpose - there are multiple address fields and they all need to be updated with the same itemprop. + 'class="jetpack-address__region"', + 'class="jetpack-address__city"', + 'class="jetpack-address__postal"', + 'class="jetpack-address__country"', + ); + $replace = array( + 'itemprop="address" itemscope itemtype="http://schema.org/PostalAddress" class="wp-block-jetpack-address" ', + 'itemprop="streetAddress" class="jetpack-address__address', // Closing " left out on purpose. + 'itemprop="addressRegion" class="jetpack-address__region"', + 'itemprop="addressLocality" class="jetpack-address__city"', + 'itemprop="postalCode" class="jetpack-address__postal"', + 'itemprop="addressCountry" class="jetpack-address__country"', + ); + + return str_replace( $find, $replace, $content ); + } + + /** + * Helper function that lets us determine if a block has any valid attributes. + * + * @param array $attr Array containing the block attributes. + * @param array $omit Array containing the block attributes that we ignore. + * + * @return string + */ + public static function has_attributes( $attr, $omit = array() ) { + foreach ( $attr as $attribute => $value ) { + if ( ! in_array( $attribute, $omit, true ) && ! empty( $value ) ) { + return true; + } + } + + return false; + } + + /** + * Adds email schema attributes. + * + * @param array $attr Array containing the email block attributes. + * @param string $content String containing the email block content. + * + * @return string + */ + public static function render_email( $attr, $content ) { + $content = self::has_attributes( $attr, array( 'className' ) ) ? + str_replace( 'href="mailto:', 'itemprop="email" href="mailto:', $content ) : + ''; + return $content; + } + + /** + * Adds phone schema attributes. Also wraps the tel link in a span so that + * it's recognized as a telephone number in Google's Structured Data. + * + * @param array $attr Array containing the phone block attributes. + * @param string $content String containing the phone block content. + * + * @return string + */ + public static function render_phone( $attr, $content ) { + if ( self::has_attributes( $attr, array( 'className' ) ) ) { + return str_replace( + array( ' $value ) { - if ( ! in_array( $attribute, $omit, true ) && ! empty( $value ) ) { - return true; - } - } - - return false; -} - -/** - * Adds email schema attributes. - * - * @param array $attr Array containing the email block attributes. - * @param string $content String containing the email block content. - * - * @return string - */ -function render_email( $attr, $content ) { - $content = has_attributes( $attr, array( 'className' ) ) ? - str_replace( 'href="mailto:', 'itemprop="email" href="mailto:', $content ) : - ''; - return $content; -} - -/** - * Adds phone schema attributes. Also wraps the tel link in a span so that - * it's recognized as a telephone number in Google's Structured Data. - * - * @param array $attr Array containing the phone block attributes. - * @param string $content String containing the phone block content. - * - * @return string - */ -function render_phone( $attr, $content ) { - if ( has_attributes( $attr, array( 'className' ) ) ) { - return str_replace( - array( '