Skip to content

Commit

Permalink
Blocks: add AMP and CSS Classes methods to the new package (#17101)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeherve authored Sep 22, 2020
1 parent 9f90589 commit 92de5f8
Show file tree
Hide file tree
Showing 19 changed files with 117 additions and 99 deletions.
33 changes: 4 additions & 29 deletions class.jetpack-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @package Jetpack
*/

use Automattic\Jetpack\Blocks;
use Automattic\Jetpack\Constants;
use Automattic\Jetpack\Status;

Expand Down Expand Up @@ -649,7 +650,7 @@ public static function load_scripts_as_required( $type, $script_dependencies = a
$script_dependencies = array_unique( array_merge( $script_dependencies, $asset_manifest['dependencies'] ) );
}

if ( ( ! class_exists( 'Jetpack_AMP_Support' ) || ! Jetpack_AMP_Support::is_amp_request() ) && self::block_has_asset( $script_relative_path ) ) {
if ( ! Blocks::is_amp_request() && self::block_has_asset( $script_relative_path ) ) {
$script_version = self::get_asset_version( $script_relative_path );
$view_script = plugins_url( $script_relative_path, JETPACK__PLUGIN_FILE );
wp_enqueue_script( 'jetpack-block-' . $type, $view_script, $script_dependencies, $script_version, false );
Expand Down Expand Up @@ -841,34 +842,8 @@ public static function load_extended_blocks() {
* @return string $classes List of CSS classes for a block.
*/
public static function block_classes( $slug = '', $attr, $extra = array() ) {
if ( empty( $slug ) ) {
return '';
}

// Basic block name class.
$classes = array(
'wp-block-jetpack-' . $slug,
);

// Add alignment if provided.
if (
! empty( $attr['align'] )
&& in_array( $attr['align'], array( 'left', 'center', 'right', 'wide', 'full' ), true )
) {
array_push( $classes, 'align' . $attr['align'] );
}

// Add custom classes if provided in the block editor.
if ( ! empty( $attr['className'] ) ) {
array_push( $classes, $attr['className'] );
}

// Add any extra classes.
if ( is_array( $extra ) && ! empty( $extra ) ) {
$classes = array_merge( $classes, array_filter( $extra ) );
}

return implode( ' ', $classes );
_deprecated_function( __METHOD__, '9.0.0', 'Automattic\\Jetpack\\Blocks::classes' );
return Blocks::classes( $slug, $attr, $extra );
}

/**
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"automattic/jetpack-assets": "@dev",
"automattic/jetpack-autoloader": "@dev",
"automattic/jetpack-backup": "@dev",
"automattic/jetpack-blocks": "@dev",
"automattic/jetpack-compat": "@dev",
"automattic/jetpack-config": "@dev",
"automattic/jetpack-connection": "@dev",
Expand Down
101 changes: 69 additions & 32 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions extensions/blocks/button/button.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Automattic\Jetpack\Extensions\Button;

use Automattic\Jetpack\Blocks;
use Jetpack_Gutenberg;

const FEATURE_NAME = 'button';
Expand Down Expand Up @@ -38,7 +39,7 @@ function register_block() {
function render_block( $attributes, $content ) {
$save_in_post_content = get_attribute( $attributes, 'saveInPostContent' );

if ( class_exists( 'Jetpack_AMP_Support' ) && \Jetpack_AMP_Support::is_amp_request() ) {
if ( Blocks::is_amp_request() ) {
Jetpack_Gutenberg::load_styles_as_required( FEATURE_NAME );
}

Expand All @@ -50,7 +51,7 @@ function render_block( $attributes, $content ) {
$text = get_attribute( $attributes, 'text' );
$unique_id = get_attribute( $attributes, 'uniqueId' );
$url = get_attribute( $attributes, 'url' );
$classes = Jetpack_Gutenberg::block_classes( FEATURE_NAME, $attributes );
$classes = Blocks::classes( FEATURE_NAME, $attributes );

$button_classes = get_button_classes( $attributes );
$button_styles = get_button_styles( $attributes );
Expand Down
Loading

0 comments on commit 92de5f8

Please sign in to comment.