diff --git a/src/block/timeline/index.php b/src/block/timeline/index.php index 862192d1e..d1a476f84 100644 --- a/src/block/timeline/index.php +++ b/src/block/timeline/index.php @@ -9,6 +9,34 @@ exit; } +if ( ! function_exists( 'stackable_is_timeline_ios_polyfill_user_agent' ) ) { + /** + * Whether the Timeline iOS line-fill workaround should run for this visitor. + * + * Chrome, Edge, and Android browsers include "Safari/" in their user agent. + * Real Safari also includes "Version/" and does not include "Chrome/". + * Keep iPhone, iPad, and Safari so iPadOS Safari (which can report the same + * user agent as macOS Safari) still gets the workaround. + * See https://developer.apple.com/forums/thread/119186 + * + * @param string $user_agent + * @return bool + */ + function stackable_is_timeline_ios_polyfill_user_agent( $user_agent ) { + if ( empty( $user_agent ) ) { + return false; + } + + if ( stripos( $user_agent, 'iPhone' ) !== false || stripos( $user_agent, 'iPad' ) !== false ) { + return true; + } + + return stripos( $user_agent, 'Safari/' ) !== false && + stripos( $user_agent, 'Version/' ) !== false && + stripos( $user_agent, 'Chrome/' ) === false; + } +} + if ( ! function_exists( 'stackable_load_timeline_ios_frontend_polyfill' ) ) { function stackable_load_timeline_ios_frontend_polyfill( $block_content, $block ) { // Add class stk-block-timeline__ios-polyfill to $block_content @@ -16,10 +44,7 @@ function stackable_load_timeline_ios_frontend_polyfill( $block_content, $block ) } $user_agent = ! empty( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : ''; - // Add polyfill if device is iPhone/iPad - // Include Safari because by default the User Agent in Safari on iPadOS is same on MacOS - // Reference: https://developer.apple.com/forums/thread/119186 - if ( ! empty( $user_agent ) && ( stripos( $user_agent, 'iPhone' ) !== false || stripos( $user_agent, 'iPad' ) !== false || stripos( $user_agent, 'Safari/' ) !== false ) ) { + if ( stackable_is_timeline_ios_polyfill_user_agent( $user_agent ) ) { add_filter( 'render_block_stackable/timeline', 'stackable_load_timeline_ios_frontend_polyfill', 10, 2 ); } } diff --git a/tests/phpunit/TimelineIosPolyfillTest.php b/tests/phpunit/TimelineIosPolyfillTest.php new file mode 100644 index 000000000..6bd6558ab --- /dev/null +++ b/tests/phpunit/TimelineIosPolyfillTest.php @@ -0,0 +1,92 @@ +require_plugin_file( 'src/block/timeline/index.php' ); + } + + /** + * Known-good user agents. Chrome, Edge, and Android include "Safari/" + * for compatibility, but they are not Safari. + * + * @return array + */ + public function user_agent_provider() { + return array( + 'empty' => array( + '', + false, + ), + 'chrome desktop' => array( + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', + false, + ), + 'chrome android' => array( + 'Mozilla/5.0 (Linux; Android 13; Redmi 10C) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.6045.193 Mobile Safari/537.36', + false, + ), + 'edge desktop' => array( + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0', + false, + ), + 'android webview' => array( + 'Mozilla/5.0 (Linux; Android 10; Redmi 10C Build/QP1A.190711.020; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/119.0.6045.193 Mobile Safari/537.36', + false, + ), + 'firefox desktop' => array( + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0', + false, + ), + 'safari macos' => array( + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 14_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1 Safari/605.1.15', + true, + ), + 'safari ipados desktop ua' => array( + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15', + true, + ), + 'iphone safari' => array( + 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1', + true, + ), + 'iphone chrome' => array( + 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/120.0.6099.119 Mobile/15E148 Safari/604.1', + true, + ), + 'ipad safari' => array( + 'Mozilla/5.0 (iPad; CPU OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1', + true, + ), + ); + } + + /** + * @dataProvider user_agent_provider + * + * @param string $user_agent + * @param bool $expected + */ + public function test_ios_polyfill_user_agent_detection( $user_agent, $expected ) { + $this->assertSame( + $expected, + stackable_is_timeline_ios_polyfill_user_agent( $user_agent ) + ); + } + + public function test_polyfill_adds_class_at_render_time() { + $html = '
'; + $out = stackable_load_timeline_ios_frontend_polyfill( $html, array() ); + + $this->assertSame( + '', + $out + ); + } +}