-
Notifications
You must be signed in to change notification settings - Fork 800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Contact Info: Rearranging how the Contact Info block register process is required. #39892
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
674bec5
Rearranging how the Contact Info block register process is required -…
coder-karen 901d04e
changelog
coder-karen 5b20bef
Updating phan file with re-added class file name
coder-karen 01c2d3c
Fix Phan baseline reported issue by changing return type on function
coder-karen 878de40
Merge branch 'trunk' into update/contact-info-block-loading
coder-karen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
projects/plugins/jetpack/changelog/update-contact-info-block-loading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: other | ||
|
||
Contact Info: Change block registration code - move back to two files. |
161 changes: 161 additions & 0 deletions
161
projects/plugins/jetpack/extensions/blocks/contact-info/class-jetpack-contact-info-block.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
<?php | ||
/** | ||
* Class Jetpack_Contact_Info_Block | ||
* | ||
* @package automattic/jetpack | ||
*/ | ||
|
||
use Automattic\Jetpack\Blocks; | ||
|
||
/** | ||
* Helper class that lets us add schema attributes dynamically because they are not something that is store with the content. | ||
* Due to the limitations of wp_kses. | ||
* | ||
* @since 7.1.0 | ||
*/ | ||
class Jetpack_Contact_Info_Block { | ||
|
||
/** | ||
* Registers the block for use in Gutenberg | ||
* This is done via an action so that we can disable | ||
* registration if we need to. | ||
*/ | ||
public static function register_block() { | ||
|
||
Blocks::jetpack_register_block( | ||
__DIR__, | ||
array( | ||
'render_callback' => __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( '<a href="tel:', '</a>' ), | ||
array( '<span itemprop="telephone"><a href="tel:', '</a></span>' ), | ||
$content | ||
); | ||
} | ||
|
||
return ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we just fix the Phan issue instead of continuing to ignore it? 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, no rush to get this deployed now. Added in 01c2d3c