Add upstream

This commit is contained in:
root
2019-10-24 00:12:05 +02:00
parent 85d41e4216
commit ac980f592c
3504 changed files with 1049983 additions and 29971 deletions

View File

@@ -0,0 +1,148 @@
<?php
/**
* Business Hours Block.
*
* @since 7.1.0
*
* @package Jetpack
*/
jetpack_register_block(
'jetpack/business-hours',
array( 'render_callback' => 'jetpack_business_hours_render' )
);
/**
* Get's default days / hours to render a business hour block with no data provided.
*
* @return array
*/
function jetpack_business_hours_get_default_days() {
return array(
array(
'name' => 'Sun',
'hours' => array(),
),
array(
'name' => 'Mon',
'hours' => array(
array(
'opening' => '09:00',
'closing' => '17:00',
),
),
),
array(
'name' => 'Tue',
'hours' => array(
array(
'opening' => '09:00',
'closing' => '17:00',
),
),
),
array(
'name' => 'Wed',
'hours' => array(
array(
'opening' => '09:00',
'closing' => '17:00',
),
),
),
array(
'name' => 'Thu',
'hours' => array(
array(
'opening' => '09:00',
'closing' => '17:00',
),
),
),
array(
'name' => 'Fri',
'hours' => array(
array(
'opening' => '09:00',
'closing' => '17:00',
),
),
),
array(
'name' => 'Sat',
'hours' => array(),
),
);
}
/**
* Dynamic rendering of the block.
*
* @param array $attributes Array containing the business hours block attributes.
*
* @return string
*/
function jetpack_business_hours_render( $attributes ) {
global $wp_locale;
if ( empty( $attributes['days'] ) || ! is_array( $attributes['days'] ) ) {
$attributes['days'] = jetpack_business_hours_get_default_days();
}
$start_of_week = (int) get_option( 'start_of_week', 0 );
$time_format = get_option( 'time_format' );
$content = sprintf(
'<dl class="jetpack-business-hours %s">',
! empty( $attributes['className'] ) ? esc_attr( $attributes['className'] ) : ''
);
$days = array( 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' );
if ( $start_of_week ) {
$chunk1 = array_slice( $attributes['days'], 0, $start_of_week );
$chunk2 = array_slice( $attributes['days'], $start_of_week );
$attributes['days'] = array_merge( $chunk2, $chunk1 );
}
foreach ( $attributes['days'] as $day ) {
$content .= '<dt class="' . esc_attr( $day['name'] ) . '">' .
ucfirst( $wp_locale->get_weekday( array_search( $day['name'], $days, true ) ) ) .
'</dt>';
$content .= '<dd class="' . esc_attr( $day['name'] ) . '">';
$days_hours = '';
foreach ( $day['hours'] as $hour ) {
$opening = strtotime( $hour['opening'] );
$closing = strtotime( $hour['closing'] );
if ( ! $opening || ! $closing ) {
continue;
}
$days_hours .= sprintf(
'%1$s - %2$s',
date( $time_format, $opening ),
date( $time_format, $closing )
);
$days_hours .= '<br />';
}
if ( empty( $days_hours ) ) {
$days_hours = esc_html__( 'Closed', 'jetpack' );
}
$content .= $days_hours;
$content .= '</dd>';
}
$content .= '</dl>';
Jetpack_Gutenberg::load_assets_as_required( 'business-hours' );
/**
* Allows folks to filter the HTML content for the Business Hours block
*
* @since 7.1.0
*
* @param string $content The default HTML content set by `jetpack_business_hours_render`
* @param array $attributes Attributes generated in the block editor for the Business Hours block
*/
return apply_filters( 'jetpack_business_hours_content', $content, $attributes );
}

View File

@@ -0,0 +1,114 @@
<?php
/**
* Class Jetpack_Contact_Info_Block
*
* @package Jetpack
*/
/**
* 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 {
/**
* 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_styles_as_required( 'contact-info' );
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.
*
* @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 ) {
$content = self::has_attributes( $attr, array( 'className' ) ) ?
str_replace( 'href="tel:', 'itemprop="telephone" href="tel:', $content ) :
'';
return $content;
}
}

View File

@@ -0,0 +1,40 @@
<?php
/**
* Contact Info block and its child blocks.
*
* @since 7.1.0
*
* @package Jetpack
*/
jetpack_register_block(
'jetpack/contact-info',
array(
'render_callback' => array( 'Jetpack_Contact_Info_Block', 'render' ),
)
);
jetpack_register_block(
'jetpack/address',
array(
'parent' => array( 'jetpack/contact-info' ),
'render_callback' => array( 'Jetpack_Contact_Info_Block', 'render_address' ),
)
);
jetpack_register_block(
'jetpack/email',
array(
'parent' => array( 'jetpack/contact-info' ),
'render_callback' => array( 'Jetpack_Contact_Info_Block', 'render_email' ),
)
);
jetpack_register_block(
'jetpack/phone',
array(
'parent' => array( 'jetpack/contact-info' ),
'render_callback' => array( 'Jetpack_Contact_Info_Block', 'render_phone' ),
)
);
require_once dirname( __FILE__ ) . '/class-jetpack-contact-info-block.php';

View File

@@ -0,0 +1,74 @@
<?php
/**
* GIF Block.
*
* @since 7.0.0
*
* @package Jetpack
*/
jetpack_register_block(
'jetpack/gif',
array(
'render_callback' => 'jetpack_gif_block_render',
)
);
/**
* Gif block registration/dependency declaration.
*
* @param array $attr - Array containing the gif block attributes.
*
* @return string
*/
function jetpack_gif_block_render( $attr ) {
$padding_top = isset( $attr['paddingTop'] ) ? $attr['paddingTop'] : 0;
$style = 'padding-top:' . $padding_top;
$giphy_url = isset( $attr['giphyUrl'] ) ? $attr['giphyUrl'] : null;
$search_text = isset( $attr['searchText'] ) ? $attr['searchText'] : '';
$caption = isset( $attr['caption'] ) ? $attr['caption'] : null;
if ( ! $giphy_url ) {
return null;
}
/* TODO: replace with centralized block_class function */
$align = isset( $attr['align'] ) ? $attr['align'] : 'center';
$type = 'gif';
$classes = array(
'wp-block-jetpack-' . $type,
'align' . $align,
);
if ( isset( $attr['className'] ) ) {
array_push( $classes, $attr['className'] );
}
$classes = implode( $classes, ' ' );
$placeholder = sprintf( '<a href="%s">%s</a>', esc_url( $giphy_url ), esc_attr( $search_text ) );
ob_start();
?>
<div class="<?php echo esc_attr( $classes ); ?>">
<figure>
<?php if ( class_exists( 'Jetpack_AMP_Support' ) && Jetpack_AMP_Support::is_amp_request() ) : ?>
<amp-iframe src="<?php echo esc_url( $giphy_url ); ?>" width="100" height="<?php echo absint( $padding_top ); ?>" sandbox="allow-scripts allow-same-origin" layout="responsive">
<div placeholder>
<?php echo wp_kses_post( $placeholder ); ?>
</div>
</amp-iframe>
<?php else : ?>
<div class="wp-block-jetpack-gif-wrapper" style="<?php echo esc_attr( $style ); ?>">
<iframe src="<?php echo esc_url( $giphy_url ); ?>" title="<?php echo esc_attr( $search_text ); ?>"></iframe>
</div>
<?php endif; ?>
<?php if ( $caption ) : ?>
<figcaption class="wp-block-jetpack-gif-caption gallery-caption"><?php echo wp_kses_post( $caption ); ?></figcaption>
<?php endif; ?>
</figure>
</div>
<?php
$html = ob_get_clean();
Jetpack_Gutenberg::load_assets_as_required( 'gif' );
return $html;
}

View File

@@ -0,0 +1,137 @@
<?php
/**
* Mailchimp Block.
*
* @since 7.1.0
*
* @package Jetpack
*/
if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || Jetpack::is_active() ) {
jetpack_register_block(
'jetpack/mailchimp',
array(
'render_callback' => 'jetpack_mailchimp_block_load_assets',
)
);
}
/**
* Mailchimp block registration/dependency declaration.
*
* @param array $attr - Array containing the map block attributes.
*
* @return string
*/
function jetpack_mailchimp_block_load_assets( $attr ) {
if ( ! jetpack_mailchimp_verify_connection() ) {
return null;
}
$values = array();
$blog_id = ( defined( 'IS_WPCOM' ) && IS_WPCOM )
? get_current_blog_id()
: Jetpack_Options::get_option( 'id' );
Jetpack_Gutenberg::load_assets_as_required( 'mailchimp' );
$defaults = array(
'emailPlaceholder' => esc_html__( 'Enter your email', 'jetpack' ),
'submitButtonText' => esc_html__( 'Join my email list', 'jetpack' ),
'consentText' => esc_html__( 'By clicking submit, you agree to share your email address with the site owner and Mailchimp to receive marketing, updates, and other emails from the site owner. Use the unsubscribe link in those emails to opt out at any time.', 'jetpack' ),
'processingLabel' => esc_html__( 'Processing…', 'jetpack' ),
'successLabel' => esc_html__( 'Success! You\'re on the list.', 'jetpack' ),
'errorLabel' => esc_html__( 'Whoops! There was an error and we couldn\'t process your subscription. Please reload the page and try again.', 'jetpack' ),
);
foreach ( $defaults as $id => $default ) {
$values[ $id ] = isset( $attr[ $id ] ) ? $attr[ $id ] : $default;
}
$values['submitButtonText'] = empty( $values['submitButtonText'] ) ? $defaults['submitButtonText'] : $values['submitButtonText'];
/* TODO: replace with centralized block_class function */
$align = isset( $attr['align'] ) ? $attr['align'] : 'center';
$type = 'mailchimp';
$classes = array(
'wp-block-jetpack-' . $type,
'align' . $align,
);
if ( isset( $attr['className'] ) ) {
array_push( $classes, $attr['className'] );
}
$classes = implode( $classes, ' ' );
$button_styles = array();
if ( ! empty( $attr['customBackgroundButtonColor'] ) ) {
array_push(
$button_styles,
sprintf(
'background-color: %s',
sanitize_hex_color( $attr['customBackgroundButtonColor'] )
)
);
}
if ( ! empty( $attr['customTextButtonColor'] ) ) {
array_push(
$button_styles,
sprintf(
'color: %s',
sanitize_hex_color( $attr['customTextButtonColor'] )
)
);
}
$button_styles = implode( $button_styles, ';' );
ob_start();
?>
<div class="<?php echo esc_attr( $classes ); ?>" data-blog-id="<?php echo esc_attr( $blog_id ); ?>">
<div class="components-placeholder">
<form aria-describedby="wp-block-jetpack-mailchimp_consent-text">
<p>
<input
aria-label="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
placeholder="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
required
title="<?php echo esc_attr( $values['emailPlaceholder'] ); ?>"
type="email"
/>
</p>
<p>
<button type="submit" class="components-button is-button is-primary" style="<?php echo esc_attr( $button_styles ); ?>">
<?php echo wp_kses_post( $values['submitButtonText'] ); ?>
</button>
</p>
<p id="wp-block-jetpack-mailchimp_consent-text" name="wp-block-jetpack-mailchimp_consent-text">
<?php echo wp_kses_post( $values['consentText'] ); ?>
</p>
</form>
<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_processing" role="status">
<?php echo esc_html( $values['processingLabel'] ); ?>
</div>
<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_success" role="status">
<?php echo esc_html( $values['successLabel'] ); ?>
</div>
<div class="wp-block-jetpack-mailchimp_notification wp-block-jetpack-mailchimp_error" role="alert">
<?php echo esc_html( $values['errorLabel'] ); ?>
</div>
</div>
</div>
<?php
$html = ob_get_clean();
return $html;
}
/**
* Mailchimp connection/list selection verification.
*
* @return boolean
*/
function jetpack_mailchimp_verify_connection() {
$option = get_option( 'jetpack_mailchimp' );
if ( ! $option ) {
return false;
}
$data = json_decode( $option, true );
if ( ! $data ) {
return false;
}
return isset( $data['follower_list_id'], $data['keyring_id'] );
}

View File

@@ -0,0 +1,31 @@
<?php
/**
* Map block.
*
* @since 6.8.0
*
* @package Jetpack
*/
jetpack_register_block(
'jetpack/map',
array(
'render_callback' => 'jetpack_map_block_load_assets',
)
);
/**
* Map block registration/dependency declaration.
*
* @param array $attr Array containing the map block attributes.
* @param string $content String containing the map block content.
*
* @return string
*/
function jetpack_map_block_load_assets( $attr, $content ) {
$api_key = Jetpack_Options::get_option( 'mapbox_api_key' );
Jetpack_Gutenberg::load_assets_as_required( 'map' );
return preg_replace( '/<div /', '<div data-api-key="' . esc_attr( $api_key ) . '" ', $content, 1 );
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* Markdown Block.
*
* @since 6.8.0
*
* @package Jetpack
*/
/**
* The block depends on the Markdown module to be active for now.
* Related discussion: https://github.com/Automattic/jetpack/issues/10294
*/
if (
( defined( 'IS_WPCOM' ) && IS_WPCOM )
|| ( method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'markdown' ) )
) {
jetpack_register_block( 'jetpack/markdown' );
}

View File

@@ -0,0 +1,63 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`MarkdownRenderer renders markdown to HTML as expected 1`] = `
<RawHTML
className="markdown"
onClick={[Function]}
>
&lt;h1&gt;Heading&lt;/h1&gt;
&lt;h2&gt;2nd Heading&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;List 1&lt;/li&gt;
&lt;li&gt;List 1&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;List 2&lt;/li&gt;
&lt;li&gt;List 2&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;List 3&lt;/li&gt;
&lt;li&gt;List 3&lt;/li&gt;
&lt;/ul&gt;
&lt;ol&gt;
&lt;li&gt;Red&lt;/li&gt;
&lt;li&gt;Green&lt;/li&gt;
&lt;li&gt;Blue&lt;/li&gt;
&lt;/ol&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;A list item.&lt;/p&gt;
&lt;p&gt;With multiple paragraphs.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Another item in the list.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;em&gt;em&lt;/em&gt;
&lt;em&gt;em&lt;/em&gt;
&lt;strong&gt;strong&lt;/strong&gt;
&lt;strong&gt;strong&lt;/strong&gt;
&lt;em&gt;&lt;strong&gt;em strong&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;*Literal asterisks*&lt;/p&gt;
&lt;p&gt;Link to &lt;a href="https://wordpress.com"&gt;WordPress&lt;/a&gt; and &lt;a href="https://jetpack.com/"&gt;https://jetpack.com/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;email me: &lt;a href="mailto:address@example.com"&gt;address@example.com&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Inline &lt;code&gt;code&lt;/code&gt; here.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Block of code with backticks.
&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;Block of code prefixed by four spaces
&lt;/code&gt;&lt;/pre&gt;
&lt;blockquote&gt;
&lt;p&gt;a blockquote.&lt;/p&gt;
&lt;p&gt;2nd paragraph in the blockquote.&lt;/p&gt;
&lt;h2&gt;H2 in a blockquote&lt;/h2&gt;
&lt;/blockquote&gt;
&lt;p&gt;A bunch of horizontal rules:&lt;/p&gt;
&lt;hr&gt;
&lt;hr&gt;
&lt;hr&gt;
&lt;hr&gt;
&lt;hr&gt;
&lt;p&gt;👋&lt;/p&gt;
</RawHTML>
`;

View File

@@ -0,0 +1,19 @@
<?php // phpcs:disable Squiz.Commenting.FileComment.Missing
/**
* Memberships block.
*
* @since 7.3.0
*
* @package Jetpack
*/
if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) || Jetpack::is_active() ) {
require_once JETPACK__PLUGIN_DIR . '/modules/memberships/class-jetpack-memberships.php';
jetpack_register_block(
'jetpack/recurring-payments',
array(
'render_callback' => array( Jetpack_Memberships::get_instance(), 'render_button' ),
)
);
}

View File

@@ -0,0 +1,41 @@
<?php
/**
* Repeat Visitor Block
*
* @since 7.2.0
*
* @package Jetpack
*/
jetpack_register_block(
'jetpack/repeat-visitor',
array(
'render_callback' => 'jetpack_repeat_visitor_block_render',
)
);
/**
* Repeat Visitor block dependency declaration.
*
* @param array $attributes Array containing the block attributes.
* @param string $content String containing the block content.
*
* @return string
*/
function jetpack_repeat_visitor_block_render( $attributes, $content ) {
Jetpack_Gutenberg::load_assets_as_required( 'repeat-visitor' );
$count = isset( $_COOKIE['jp-visit-counter'] ) ? intval( $_COOKIE['jp-visit-counter'] ) : 0;
$criteria = isset( $attributes['criteria'] ) ? $attributes['criteria'] : 'after-visits';
$threshold = isset( $attributes['threshold'] ) ? intval( $attributes['threshold'] ) : 3;
if (
( 'after-visits' === $criteria && $count >= $threshold ) ||
( 'before-visits' === $criteria && $count < $threshold )
) {
return $content;
}
// return an empty div so that view script increments the visit counter in the cookie.
return '<div class="wp-block-jetpack-repeat-visitor"></div>';
}

View File

@@ -0,0 +1,28 @@
<?php
/**
* Slideshow Block.
*
* @since 7.1.0
*
* @package Jetpack
*/
jetpack_register_block(
'jetpack/slideshow',
array(
'render_callback' => 'jetpack_slideshow_block_load_assets',
)
);
/**
* Slideshow block registration/dependency declaration.
*
* @param array $attr Array containing the slideshow block attributes.
* @param string $content String containing the slideshow block content.
*
* @return string
*/
function jetpack_slideshow_block_load_assets( $attr, $content ) {
Jetpack_Gutenberg::load_assets_as_required( 'slideshow' );
return $content;
}

View File

@@ -0,0 +1,98 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`renders as expected 1`] = `
<Gallery
galleryRef={
Object {
"current": null,
}
}
>
<Row
key="0"
>
<Column
key="0"
>
0
</Column>
</Row>
<Row
key="1"
>
<Column
key="0"
>
1
</Column>
</Row>
<Row
key="2"
>
<Column
key="0"
>
2
</Column>
<Column
key="1"
>
3
</Column>
<Column
key="2"
>
4
</Column>
<Column
key="3"
>
5
</Column>
</Row>
<Row
key="3"
>
<Column
key="0"
>
6
</Column>
<Column
key="1"
>
7
</Column>
</Row>
<Row
key="4"
>
<Column
key="0"
>
8
</Column>
<Column
key="1"
>
9
10
</Column>
</Row>
<Row
key="5"
>
<Column
key="0"
>
11
12
</Column>
<Column
key="1"
>
13
</Column>
</Row>
</Gallery>
`;

View File

@@ -0,0 +1,30 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`ratiosToMosaicRows transforms as expected 1`] = `
Array [
Array [
1,
],
Array [
1,
],
Array [
1,
1,
1,
1,
],
Array [
1,
1,
],
Array [
1,
2,
],
Array [
2,
1,
],
]
`;

View File

@@ -0,0 +1,171 @@
<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* Tiled Gallery block. Depends on the Photon module.
*
* @since 6.9.0
*
* @package Jetpack
*/
/**
* Jetpack Tiled Gallery Block class
*
* @since 7.3
*/
class Jetpack_Tiled_Gallery_Block {
/* Values for building srcsets */
const IMG_SRCSET_WIDTH_MAX = 2000;
const IMG_SRCSET_WIDTH_MIN = 600;
const IMG_SRCSET_WIDTH_STEP = 300;
/**
* Register the block
*/
public static function register() {
jetpack_register_block(
'jetpack/tiled-gallery',
array(
'render_callback' => array( __CLASS__, 'render' ),
)
);
}
/**
* Tiled gallery block registration
*
* @param array $attr Array containing the block attributes.
* @param string $content String containing the block content.
*
* @return string
*/
public static function render( $attr, $content ) {
Jetpack_Gutenberg::load_assets_as_required( 'tiled-gallery' );
$is_squareish_layout = self::is_squareish_layout( $attr );
if ( preg_match_all( '/<img [^>]+>/', $content, $images ) ) {
/**
* This block processes all of the images that are found and builds $find and $replace.
*
* The original img is added to the $find array and the replacement is made and added
* to the $replace array. This is so that the same find and replace operations can be
* made on the entire $content.
*/
$find = array();
$replace = array();
foreach ( $images[0] as $image_html ) {
if (
preg_match( '/data-width="([0-9]+)"/', $image_html, $img_height )
&& preg_match( '/data-height="([0-9]+)"/', $image_html, $img_width )
&& preg_match( '/src="([^"]+)"/', $image_html, $img_src )
) {
// Drop img src query string so it can be used as a base to add photon params
// for the srcset.
$src_parts = explode( '?', $img_src[1], 2 );
$orig_src = $src_parts[0];
$orig_height = absint( $img_height[1] );
$orig_width = absint( $img_width[1] );
// Because URLs are already "photon", the photon function used short-circuits
// before ssl is added. Detect ssl and add is if necessary.
$is_ssl = ! empty( $src_parts[1] ) && false !== strpos( $src_parts[1], 'ssl=1' );
if ( ! $orig_width || ! $orig_height || ! $orig_src ) {
continue;
}
$srcset_parts = array();
if ( $is_squareish_layout ) {
$min_width = min( self::IMG_SRCSET_WIDTH_MIN, $orig_width, $orig_height );
$max_width = min( self::IMG_SRCSET_WIDTH_MAX, $orig_width, $orig_height );
for ( $w = $min_width; $w <= $max_width; $w = min( $max_width, $w + self::IMG_SRCSET_WIDTH_STEP ) ) {
$srcset_src = add_query_arg(
array(
'resize' => $w . ',' . $w,
'strip' => 'all',
),
$orig_src
);
if ( $is_ssl ) {
$srcset_src = add_query_arg( 'ssl', '1', $srcset_src );
}
$srcset_parts[] = esc_url( $srcset_src ) . ' ' . $w . 'w';
if ( $w >= $max_width ) {
break;
}
}
} else {
$min_width = min( self::IMG_SRCSET_WIDTH_MIN, $orig_width );
$max_width = min( self::IMG_SRCSET_WIDTH_MAX, $orig_width );
for ( $w = $min_width; $w <= $max_width; $w = min( $max_width, $w + self::IMG_SRCSET_WIDTH_STEP ) ) {
$srcset_src = add_query_arg(
array(
'strip' => 'all',
'w' => $w,
),
$orig_src
);
if ( $is_ssl ) {
$srcset_src = add_query_arg( 'ssl', '1', $srcset_src );
}
$srcset_parts[] = esc_url( $srcset_src ) . ' ' . $w . 'w';
if ( $w >= $max_width ) {
break;
}
}
}
if ( ! empty( $srcset_parts ) ) {
$srcset = 'srcset="' . esc_attr( implode( ',', $srcset_parts ) ) . '"';
$find[] = $image_html;
$replace[] = str_replace( '<img', '<img ' . $srcset, $image_html );
}
}
}
if ( ! empty( $find ) ) {
$content = str_replace( $find, $replace, $content );
}
}
/**
* Filter the output of the Tiled Galleries content.
*
* @module tiled-gallery
*
* @since 6.9.0
*
* @param string $content Tiled Gallery block content.
*/
return apply_filters( 'jetpack_tiled_galleries_block_content', $content );
}
/**
* Determines whether a Tiled Gallery block uses square or circle images (1:1 ratio)
*
* Layouts are block styles and will be available as `is-style-[LAYOUT]` in the className
* attribute. The default (rectangular) will be omitted.
*
* @param {Array} $attr Attributes key/value array.
* @return {boolean} True if layout is squareish, otherwise false.
*/
private static function is_squareish_layout( $attr ) {
return isset( $attr['className'] )
&& (
'is-style-square' === $attr['className']
|| 'is-style-circle' === $attr['className']
);
}
}
if (
( defined( 'IS_WPCOM' ) && IS_WPCOM )
|| class_exists( 'Jetpack_Photon' ) && Jetpack::is_module_active( 'photon' )
) {
Jetpack_Tiled_Gallery_Block::register();
}

View File

@@ -0,0 +1,121 @@
<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* Ads Block.
*
* @since 7.1.0
*
* @package Jetpack
*/
class Jetpack_WordAds_Gutenblock {
const BLOCK_NAME = 'jetpack/wordads';
/**
* Check if site is on WP.com Simple.
*
* @return bool
*/
private static function is_wpcom() {
return defined( 'IS_WPCOM' ) && IS_WPCOM;
}
/**
* Check if the WordAds module is active.
*
* @return bool
*/
private static function is_jetpack_module_active() {
return method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'wordads' );
}
/**
* Check if the site is approved for ads for WP.com Simple sites.
*
* @return bool
*/
private static function is_available() {
if ( self::is_wpcom() ) {
return has_any_blog_stickers( array( 'wordads', 'wordads-approved', 'wordads-approved-misfits' ), get_current_blog_id() );
}
return self::is_jetpack_module_active();
}
/**
* Register the WordAds block.
*/
public static function register() {
if ( self::is_available() ) {
jetpack_register_block(
self::BLOCK_NAME,
array(
'render_callback' => array( 'Jetpack_WordAds_Gutenblock', 'gutenblock_render' ),
)
);
}
}
/**
* Set if the WordAds block is available.
*/
public static function set_availability() {
if ( ! self::is_available() ) {
Jetpack_Gutenberg::set_extension_unavailable( self::BLOCK_NAME, 'WordAds unavailable' );
return;
}
// Make the block available. Just in case it wasn't registed before.
Jetpack_Gutenberg::set_extension_available( self::BLOCK_NAME );
}
/**
* Renders the WordAds block.
*
* @param array $attr Block attributes.
*
* @return string Block HTML.
*/
public static function gutenblock_render( $attr ) {
global $wordads;
/** This filter is already documented in modules/wordads/wordads.php `insert_ad()` */
if ( empty( $wordads ) || is_feed() || apply_filters( 'wordads_inpost_disable', false ) ) {
return '';
}
if ( ! empty( $attr['hideMobile'] ) && $wordads->params->is_mobile() ) {
return '';
}
if ( ! self::is_wpcom() && $wordads->option( 'wordads_house' ) ) {
return $wordads->get_ad( 'inline', 'house' );
}
// section_id is mostly depricated at this point, but it helps us (devs) keep track of which ads end up where
// 6 is to keep track of gutenblock ads.
$section_id = $wordads->params->blog_id . '6';
$align = 'center';
if ( isset( $attr['align'] ) && in_array( $attr['align'], array( 'left', 'center', 'right' ), true ) ) {
$align = $attr['align'];
}
$align = 'align' . $align;
$ad_tag_ids = $wordads->get_ad_tags();
$format = 'mrec';
if ( isset( $attr['format'] ) && in_array( $attr['format'], array_keys( $ad_tag_ids ), true ) ) {
$format = $attr['format'];
}
$height = $ad_tag_ids[ $format ]['height'];
$width = $ad_tag_ids[ $format ]['width'];
$snippet = $wordads->get_ad_snippet( $section_id, $height, $width, 'gutenberg', $wordads->get_solo_unit_css() );
return $wordads->get_ad_div( 'inline', $snippet, array( $align ) );
}
}
add_action(
'init',
array( 'Jetpack_WordAds_Gutenblock', 'register' )
);
add_action(
'jetpack_register_gutenberg_extensions',
array( 'Jetpack_WordAds_Gutenblock', 'set_availability' )
);