Skip to content

Instantly share code, notes, and snippets.

View westonruter's full-sized avatar

Weston Ruter westonruter

View GitHub Profile
<?php
/**
* This file is intended to be executed using WP-CLI's eval-file command.
*/
$bookmark_name = 'the-bookmark';
$html = <<<HTML
<html lang="en">
<head>
Warning: The next_token() method was called another time for +HTML. in /var/www/html/wp-content/plugins/html-processor-debug/html-processor-debug.php on line 80
<HTML xpath="/*[2][self::HTML]">
Warning: The next_token() method was called another time for +HEAD. in /var/www/html/wp-content/plugins/html-processor-debug/html-processor-debug.php on line 80
<HEAD xpath="/*[2][self::HTML]/*[2][self::HEAD]">
Warning: The next_token() method was called another time for +META. in /var/www/html/wp-content/plugins/html-processor-debug/html-processor-debug.php on line 80
<META xpath="/*[2][self::HTML]/*[2][self::HEAD]/*[3][self::META]">
Warning: The next_token() method was called another time for +TITLE. in /var/www/html/wp-content/plugins/html-processor-debug/html-processor-debug.php on line 80
<TITLE xpath="/*[2][self::HTML]/*[2][self::HEAD]/*[6][self::TITLE]">
Warning: The next_token() method was called another time for -HEAD. in /var/www/html/wp-content/plugins/html-processor-d
<?php
$sizes_values = array_filter(
array_map(
static function ( string $size ): string {
$size = trim( $size, " \t\f\r\n" );
$lower_sizes = strtolower( $size );
if ( 'auto' === $lower_sizes ) {
// Normalize to lower-case.
$size = $lower_sizes;

Usage:

The default repetition count is 1000. You can pass a different iteration

wp eval-file html-api-benchmark.php
wp eval-file html-api-benchmark.php 10000

See PR WordPress/performance#1471

<?php
/**
* Plugin Name: Auto-Sizes via HTML Tag Processor
*/
function auto_sizes_update_content_img_tag_with_html_tag_processor( $html ): string {
if ( ! is_string( $html ) ) {
$html = '';
}
<?php
$values = [];
foreach ( array( 'PHP_OUTPUT_HANDLER_START', 'PHP_OUTPUT_HANDLER_WRITE', 'PHP_OUTPUT_HANDLER_FLUSH', 'PHP_OUTPUT_HANDLER_CLEAN', 'PHP_OUTPUT_HANDLER_FINAL', 'PHP_OUTPUT_HANDLER_END' ) as $constant ) {
if ( constant( $constant ) & $phase ) {
$values[] = $constant;
}
}
error_log( 'Phase: ' . implode( ', ', $values ) );
<?php
/**
* Plugin Name: Force Low-Priority Interactivity API Script Modules
* Plugin URI: https://core.trac.wordpress.org/ticket/61734
* Description: Improves Largest Contentful Paint by forcing the module scripts (from the Interactivity API) to be loaded with a low priority rather than the default high priority. This prevents network contention with loading the LCP element.
* Requires at least: 6.5
* Requires PHP: 7.2
* Version: 0.1
* Author: Weston Ruter
* Author URI: https://weston.ruter.net/
<?php
/**
* Plugin Name: Try Slow Click Event Directives
* Plugin URI: https://gist.github.com/westonruter/59f7c58230bd5d1e82733e5b87d245dc
* Description: Testing
* Requires at least: 6.5
* Requires PHP: 7.2
* Version: 0.1.0
* Author: Weston Ruter
* Update URI: https://gist.github.com/westonruter/59f7c58230bd5d1e82733e5b87d245dc
# HTTP Archive query for how often the mediaelement-core script is on a WordPress page.
#
# WPP Research, Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
@westonruter
westonruter / js-self-profile.mjs
Created April 8, 2024 21:05
POC for how to hack Puppeteer to be able to use the JS Self-Profiling API
// Usage: node js-self-profile.mjs https://example.com/
import puppeteer from 'puppeteer';
import http from 'http';
import https from 'https';
const PROXY_SERVER_PORT = 8080;
// Create an HTTP server so that we can inject the Document-Policy header in the response. Otherwise, the JS Self-Profiling API is not available.
const server = http.createServer(async (req, res) => {