Skip to content

Instantly share code, notes, and snippets.

View kasparsd's full-sized avatar

Kaspars Dambis kasparsd

View GitHub Profile
@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;
<?php
function preseto_rest_with_extra_post_stati( $query, $request ) {
if ( current_user_can( 'read' ) ) {
$query['post_status'] = [ 'publish', 'private' ];
}
return $query;
}

Sitemap HTML Tree

Generate a human-readable sitemap tree from an XML sitemap:

php sitemap-to-html.php https://xwp.co/sitemap_index.xml

Then open the generated sitemap-*.html file in your web browser.

Sitemap URL CSV

@kasparsd
kasparsd / fix-ssl-please.php
Created March 8, 2024 14:06
Fix SSL (HTTPs) URLs in WP content
<?php
/*
Plugin Name: Fix SSL Please
Plugin URI: https://github.com/kasparsd/ssl-fix
GitHub URI: https://github.com/kasparsd/ssl-fix
Description: Ensure that everything works over SSL
Version: 0.1.1
Author: Kaspars Dambis
Author URI: http://kaspars.net
*/
@kasparsd
kasparsd / admin-pwa.php
Last active February 20, 2024 12:17
wp-admin-pwa.php
<?php
/**
* Plugin Name: Admin PWA
*/
namespace Preseto\Admin_PWA;
const ACTION_QUERY_VAR = 'admin-pwa';
function manifest_url() {
@kasparsd
kasparsd / zip-chunks.php
Last active January 16, 2024 09:10
Zip chunks with PHP (split ZIP with max size)
<?php // Run as `php zip-chunks.php path/to/directory`.
$chunk_limit = 1024 * 1024 * 1024; // 1 GB
function get_files( $path ) {
$files = glob( rtrim( $path, '\\/' ) . '/*' );
foreach ( $files as $file ) {
if ( is_dir( $file ) ) {
$files = array_merge( $files, get_files( $file ) );
}