Skip to content

Instantly share code, notes, and snippets.

View kasparsd's full-sized avatar

Kaspars Dambis kasparsd

View GitHub Profile
<?php
namespace YourVendor\Project;
class Config {
private static array $config = [];
private function __construct() {
// Private constructor to prevent direct instantiation
}
<?php
namespace YourVendor\WooCommerce_CLI;
use WP_CLI;
use WooCommerce;
function anon_orders( $args, $assoc_args ) {
$order_ids = wc_get_orders(
[
proxy_cache_path /var/cache/cf-images levels=1:2 keys_zone=CF_IMAGES:10m inactive=7d max_size=10MB;
server {
# ... here goes the rest of your config.
# Proxy example.com/cfimg/original/path/image.jpg?cf=width=100,quality=80
# to example.com/cdn-cgi/image/width=100,quality=80/original/path/image.jpg
location ~* ^/cfimg/(.*)$ {
resolver 1.1.1.1;
@kasparsd
kasparsd / cloudflare-image-server.php
Created February 12, 2025 07:30
Cloudflare Image API mock WordPress plugin
<?php
/**
* Plugin Name: Cloudflare Image API Mock
* Description: Replicate Cloudflare Image API functionality locally.
*
* IMPORTANT: The code below shouldn't be used on production since the
* image path resolution is vulnerable to directory traversal attacks.
*/
add_action(
<?php
// See https://x.com/PoeHaH/status/1875290205894164758
function run_times( callable $callback, int $runs ) {
$start = microtime(true);
for ($i = 0; $i < $runs; $i++) {
$callback();
}
return microtime(true) - $start;
}
@kasparsd
kasparsd / wp-zips.md
Last active November 21, 2024 11:47
WordPress package ZIP structure and contents

WordPress core, plugin and theme ZIP structure

WordPress core:

wget https://wordpress.org/latest.zip
unzip -l latest.zip | head
Archive:  latest.zip
  Length      Date    Time    Name
---------  ---------- -----   ----

0 11-12-2024 22:43 wordpress/

@kasparsd
kasparsd / commit-msg.php
Created October 18, 2024 09:20
Git hook commit-msg script to enforce Conventional Commits
<?php
$commit_message = file_get_contents( $argv[1] );
$is_common_commit = preg_match(
'/^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test){1}(\([\w\-\.,\|\:]+\))?(!)?:\s?([\w ])+([\s\S]*)/m',
$commit_message,
$matches
);
@kasparsd
kasparsd / wp-api-mirror-config.php
Last active October 1, 2024 13:26
Replace WP.org API calls with custom hostnames
<?php
/**
* Plugin Name: WP API Mirror Config
*
* Place this file in `wp-content/mu-plugins/wp-api-mirror.php`
* and it will be activated automatically.
*
* Note that most requests to api.wordpress.org contain dynamic payload
* such as list of activated plugins, themes and your site URLs. Some of
* them also use POST requests so be sure to configure heavy caching that
@kasparsd
kasparsd / wp-multisite-sso.php
Created September 25, 2024 15:01
A prototype of native WP multisite single sign-on (SSO)
<?php
/**
* Plugin Name: Multisite SSO Login
* Network: true
*
* @see https://x.com/konstruktors/status/1838956536049094659
*/
if ( ! is_multisite() ) {
return;