Skip to content

Commit

Permalink
Merge pull request #595 from jefferyto/apple-touch-startup-image-filter
Browse files Browse the repository at this point in the history
Add apple_touch_startup_images filter
  • Loading branch information
westonruter authored Aug 17, 2021
2 parents 4aa2b8a + 50e7daf commit 69dc3c2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 5 deletions.
34 changes: 34 additions & 0 deletions tests/test-class-wp-web-app-manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,44 @@ public function test_init() {
* @covers WP_Web_App_Manifest::manifest_link_and_meta()
*/
public function test_manifest_link_and_meta() {
$this->mock_site_icon();

$added_images = array(
array(
'href' => home_url( '/340.png' ),
'media' => '(device-width: 340px)',
),
array(
'href' => home_url( '/480.png' ),
),
);

add_filter(
'apple_touch_startup_images',
function ( $images ) use ( $added_images ) {
$this->assertInternalType( 'array', $images );
$this->assertCount( 1, $images );
$images = array_merge( $images, $added_images );
$images[] = array( 'bad' => 'yes' );
return $images;
}
);

ob_start();
$this->instance->manifest_link_and_meta();
$output = ob_get_clean();

$this->assertSame( 3, substr_count( $output, '<link rel="apple-touch-startup-image"' ) );
$this->assertContains( sprintf( '<link rel="apple-touch-startup-image" href="%s">', esc_url( get_site_icon_url() ) ), $output );
foreach ( $added_images as $added_image ) {
$tag = sprintf( '<link rel="apple-touch-startup-image" href="%s"', esc_url( $added_image['href'] ) );
if ( isset( $added_image['media'] ) ) {
$tag .= sprintf( ' media="%s"', esc_attr( $added_image['media'] ) );
}
$tag .= '>';
$this->assertContains( $tag, $output );
}

$this->assertContains( '<link rel="manifest"', $output );
$this->assertContains( rest_url( WP_Web_App_Manifest::REST_NAMESPACE . WP_Web_App_Manifest::REST_ROUTE ), $output );
$this->assertContains( '<meta name="theme-color" content="', $output );
Expand Down
43 changes: 38 additions & 5 deletions wp-includes/class-wp-web-app-manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,48 @@ public function manifest_link_and_meta() {
?>
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes">

<?php
$icons = isset( $manifest['icons'] ) ? $manifest['icons'] : array();
usort( $icons, array( $this, 'sort_icons_callback' ) );
$icon = array_shift( $icons );
?>
<?php if ( ! empty( $icon ) ) : ?>
<link rel="apple-touch-startup-image" href="<?php echo esc_url( $icon['src'] ); ?>">
<?php endif; ?>
<?php

$images = array();
if ( ! empty( $icon ) ) {
$images[] = array( 'href' => $icon['src'] );
}

/**
* Filters splash screen images for Safari on iOS.
*
* @param array $images {
* Array of splash screen images and their attributes.
*
* @type array ...$0 {
* Array of splash screen image attributes.
*
* @type string $href URL of splash screen image. Required.
* @type string $media Media query for when the splash screen image should be used.
* }
* }
*/
$images = apply_filters( 'apple_touch_startup_images', $images );

foreach ( $images as $key => $image ) {
if ( ! is_array( $image ) ) {
continue;
}

if ( ! isset( $image['href'] ) || ! esc_url( $image['href'], array( 'http', 'https' ) ) ) {
continue;
}

printf( '<link rel="apple-touch-startup-image" href="%s"', esc_url( $image['href'] ) );
if ( isset( $image['media'] ) ) {
printf( ' media="%s"', esc_attr( $image['media'] ) );
}
echo ">\n";
}
break;
endswitch;
?>
Expand Down

0 comments on commit 69dc3c2

Please sign in to comment.