Skip to content

Commit

Permalink
Hooks: Ensure translation loading is not triggered too early. (#39841)
Browse files Browse the repository at this point in the history
  • Loading branch information
coder-karen authored Oct 24, 2024
1 parent 37925d9 commit be3804c
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 173 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Hooks: Hook init_sync_cron_jobs into init to ensure translation loading within the function is not triggered too early.
2 changes: 1 addition & 1 deletion projects/packages/sync/src/class-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static function init() {
}

if ( self::sync_via_cron_allowed() ) {
self::init_sync_cron_jobs();
add_action( 'init', array( __CLASS__, 'init_sync_cron_jobs' ), 1 );
} elseif ( wp_next_scheduled( 'jetpack_sync_cron' ) ) {
self::clear_sync_cron_jobs();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: changed

Admin: Changed initialization load order for menu items to prevent translation error.
24 changes: 17 additions & 7 deletions projects/packages/videopress/src/class-admin-ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ class Admin_UI {
* @return void
*/
public static function init() {

add_action( 'admin_menu', array( __CLASS__, 'enable_menu' ) );

add_action( 'admin_footer-upload.php', array( __CLASS__, 'attachment_details_two_column_template' ) );
add_action( 'admin_footer-post.php', array( __CLASS__, 'attachment_details_template' ), 20 );

add_filter( 'get_edit_post_link', array( __CLASS__, 'edit_video_link' ), 10, 3 );

add_action( 'admin_init', array( __CLASS__, 'remove_jetpack_hooks' ) );
}

/**
* Enable the menu, separately to init due to translations needing to run early for the page suffix.
*
* @return void
*/
public static function enable_menu() {
$page_suffix = Admin_Menu::add_menu(
__( 'Jetpack VideoPress', 'jetpack-videopress-pkg' ),
_x( 'VideoPress', 'The Jetpack VideoPress product name, without the Jetpack prefix', 'jetpack-videopress-pkg' ),
Expand All @@ -43,13 +60,6 @@ public static function init() {
3
);
add_action( 'load-' . $page_suffix, array( __CLASS__, 'admin_init' ) );

add_action( 'admin_footer-upload.php', array( __CLASS__, 'attachment_details_two_column_template' ) );
add_action( 'admin_footer-post.php', array( __CLASS__, 'attachment_details_template' ), 20 );

add_filter( 'get_edit_post_link', array( __CLASS__, 'edit_video_link' ), 10, 3 );

add_action( 'admin_init', array( __CLASS__, 'remove_jetpack_hooks' ) );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion projects/plugins/jetpack/.phan/baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
'extensions/blocks/blog-stats/blog-stats.php' => ['PhanTypeMismatchReturnProbablyReal'],
'extensions/blocks/blogroll/blogroll-item/blogroll-item.php' => ['PhanPluginDuplicateConditionalNullCoalescing'],
'extensions/blocks/calendly/calendly.php' => ['PhanTypeMismatchArgumentProbablyReal', 'PhanTypeMismatchReturnProbablyReal'],
'extensions/blocks/contact-info/class-jetpack-contact-info-block.php' => ['PhanTypeMismatchReturn'],
'extensions/blocks/contact-info/contact-info.php' => ['PhanTypeMismatchReturn'],
'extensions/blocks/cookie-consent/cookie-consent.php' => ['PhanParamTooMany'],
'extensions/blocks/donations/donations.php' => ['PhanTypeMismatchArgument'],
'extensions/blocks/gif/gif.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanTypeMismatchReturnProbablyReal'],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: other

Hooks: Change several action priorities to ensure translation loading is not triggered too early.
3 changes: 1 addition & 2 deletions projects/plugins/jetpack/class.jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -867,8 +867,7 @@ function ( $methods ) {
* @action plugins_loaded
*/
public function late_initialization() {
add_action( 'plugins_loaded', array( 'Jetpack', 'load_modules' ), 100 );

add_action( 'after_setup_theme', array( 'Jetpack', 'load_modules' ), 1 );
My_Jetpack_Initializer::init();

// Initialize Boost Speed Score
Expand Down

This file was deleted.

179 changes: 148 additions & 31 deletions projects/plugins/jetpack/extensions/blocks/contact-info/contact-info.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,153 @@
* @package automattic/jetpack
*/

namespace Automattic\Jetpack\Extensions\Contact_Info;

use Automattic\Jetpack\Blocks;
use Jetpack_Gutenberg;

/**
* Registers the block for use in Gutenberg
* This is done via an action so that we can disable
* registration if we need to.
*/
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',
)
);
}

add_action( 'init', __NAMESPACE__ . '\register_block' );

/**
* 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
*/
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
*/
function render_address( $attr, $content ) {
// Returns empty content if the only attribute set is linkToGoogleMaps.
if ( ! 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
*/
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
*/
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( '<a href="tel:', '</a>' ),
array( '<span itemprop="telephone"><a href="tel:', '</a></span>' ),
$content
);
}

Blocks::jetpack_register_block(
__DIR__,
array(
'render_callback' => array( 'Jetpack_Contact_Info_Block', 'render' ),
)
);

Blocks::jetpack_register_block(
'jetpack/address',
array(
'parent' => array( 'jetpack/contact-info' ),
'render_callback' => array( 'Jetpack_Contact_Info_Block', 'render_address' ),
)
);

Blocks::jetpack_register_block(
'jetpack/email',
array(
'parent' => array( 'jetpack/contact-info' ),
'render_callback' => array( 'Jetpack_Contact_Info_Block', 'render_email' ),
)
);

Blocks::jetpack_register_block(
'jetpack/phone',
array(
'parent' => array( 'jetpack/contact-info' ),
'render_callback' => array( 'Jetpack_Contact_Info_Block', 'render_phone' ),
)
);
require_once __DIR__ . '/class-jetpack-contact-info-block.php';
return '';
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,4 @@ private static function is_squareish_layout( $attr ) {
}
}

Tiled_Gallery::register();
add_action( 'init', array( Tiled_Gallery::class, 'register' ) );
Loading

0 comments on commit be3804c

Please sign in to comment.