Skip to content

Instantly share code, notes, and snippets.

View PatelUtkarsh's full-sized avatar
🧘

Utkarsh Patel PatelUtkarsh

🧘
View GitHub Profile
@PatelUtkarsh
PatelUtkarsh / extract.js
Created December 17, 2024 06:46
Extract course code from training gov au - use node 20
import { writeFile } from 'fs/promises';
async function fetchWithTimeout(url, timeout = 30000) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
try {
const response = await fetch(url, {
signal: controller.signal,
headers: {
@PatelUtkarsh
PatelUtkarsh / index.php
Last active November 25, 2024 10:17
Benchmarking HTML Parsing: PHP 8.4 vs WP_HTML_Tag_Processor
<?php
/**
* DOM Operations Benchmark Test
*
* Comparing PHP 8.4 DOM vs WP_HTML_Tag_Processor
*/
class DOMBenchmark {
private const SAMPLE_HTML = <<<HTML
<main>
@PatelUtkarsh
PatelUtkarsh / index.php
Last active March 6, 2025 13:47
Photon use custom CDN.
<?php
$cdn_image_domain_to_use = 'https://{replace-me}.cloudfront.net'
$cdn_image_domain_to_use_parsed = wp_parse_url( $cdn_image_domain_to_use );
// Stop creating crops and just use the original image.
add_filter( 'jetpack_photon_noresize_mode', '__return_true' );
// Setup custom domain.
add_filter( 'jetpack_photon_domain', fn() => $cdn_image_domain_to_use );
@PatelUtkarsh
PatelUtkarsh / parse_block.php
Created July 24, 2024 04:27
To parse block names and counts from a JSON export from phpMyAdmin. Must export 'post_content' column.
<?php
// Function to extract block names from post content
function extractBlockNames($content) {
$blockNames = [];
$pattern = '/<!-- wp:([\w\/-]+)/';
preg_match_all($pattern, $content, $matches);
if (!empty($matches[1])) {
$blockNames = $matches[1];
@PatelUtkarsh
PatelUtkarsh / Clean-tags-sql.sh
Created April 24, 2024 10:51
Clean tags with zero count on multisite
#!/bin/bash
# Get all blog ids
blog_ids=$(wp site list --field=blog_id)
# Loop through each blog id
for blog_id in $blog_ids
do
if [ "$blog_id" -eq 1 ]; then
continue
@PatelUtkarsh
PatelUtkarsh / clean-tags.sh
Last active April 24, 2024 09:30
WP-CLI script to remove extra tags from WordPress on multisite
#!/bin/bash
# Get all site urls
site_urls=$(wp site list --field=url)
# Loop through each site url
for site_url in $site_urls
do
# Recount all post tags
wp term recount post_tag --url=$site_url --skip-plugins --skip-themes
@PatelUtkarsh
PatelUtkarsh / jetpack-config.php
Last active April 2, 2024 12:14
Disable jetpack sync
<?php
/**
* Plugin Name: Disable jetpack sync.
*/
add_filter( 'jetpack_sync_modules', '__return_empty_array', PHP_INT_MAX );
@PatelUtkarsh
PatelUtkarsh / redirect-canonical.php
Last active February 13, 2024 08:31
WordPress network site with different home url and site adress with reverse proxy redirects trailing slash to site address instead of home url.
<?php
/**
* Plugin name: Redirect canonical to home URL for trailing slash.
* Description: Fix for WordPress network site with different homeurl and site address with reverse proxy, which redirects non-trailing slash to site address instead of home url.
* Version: 1.0
* Author: Utkarsh
* Author URI: https://utkarshpatel.com
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
@PatelUtkarsh
PatelUtkarsh / auto-merge.yml
Last active January 20, 2024 05:14
Auto merge PR on adding label and trigger deployment if the branch supports it. Must have PAT(Personal access token) added to GH action.
name: Auto Merge and Push on Label
on:
pull_request_target:
types: [labeled]
jobs:
auto-merge-and-push:
runs-on: ubuntu-latest
if: github.event.label.name == 'In dev'
@PatelUtkarsh
PatelUtkarsh / perf.php
Last active February 25, 2023 12:48
Compare performance of class_implements vs implementsInterface (using ReflectionClass) for 1,00,000 class.
<?php
// Define an interface
interface MyInterface {
public function myFunction();
}
// Define 1000 classes that implement the interface
for ($i = 1; $i <= 100000; $i++) {
$className = 'MyClass' . $i;
eval("