Skip to content

Instantly share code, notes, and snippets.

View barryhughes's full-sized avatar
🇨🇦

Barry Hughes barryhughes

🇨🇦
  • Automattic
  • Vancouver Island, Canada
View GitHub Profile
@barryhughes
barryhughes / wp-cli-as-generate-actions.php
Last active April 20, 2021 18:12
Handy dandy testing tool; used to generate fake scheduled actions [WP CLI | Action Scheduler]
<?php
/**
* Generates a set of scheduled actions for testing purposes.
*
* Additionally, it provides test callbacks (which do very little except burn time).
* How much fun is that?
*
* An easy way to add this command is to place the file in the wp-content/mu-plugins
* directory, or else incldue it from a file in that directory.
*
@barryhughes
barryhughes / query-monitor-for-logged-out-users.php
Created April 7, 2021 20:30
Make Query Monitor available for logged out users. Useful alternative when Query Monitor's own authentication cookie-based support for the same thing is not enough.
<?php
/**
* Makes Query Monitor output available to all users.
*
* This can, for example, be added to mu-plugins/query-monitor.php (and can
* easily be disabled by changing 'init' to 'xinit', or commenting the whole
* thing out, or adding a return statement, etc).
*
* Reasons you might use this instead of simply setting Query Monitor's own
-- Delete WooCommerce session data for a specific user.
DELETE session_data,
session_meta
FROM wp_woocommerce_sessions AS session_data
JOIN wp_usermeta AS session_meta ON
session_meta.user_id = session_data.session_key
-- Set the `session_key` value to the target `user_id`.
@barryhughes
barryhughes / random-fact-string.php
Created March 2, 2021 18:16
Get a random fact.
<?php
/**
* Get a string containing a random fact.
*
* If a random fact cannot be fetched from the external service, a default is
* supplied instead.
*
* @return string
*/
<?php
/**
* Remove events tagged with any of the slugs listed below from
* event views.
*
* We deliberately avoid use of TEC functions and constants etc
* so that this doesn't break should that plugin temporarily be
* deactivated.
*/
add_filter( 'pre_get_posts', function( $query ) {
<?php
/**
* Facilitates dictating a separate (IP-address protected) Google Maps
* API key for server-side geocoding requests.
*/
class Server_Side_Google_Maps_Key {
/**
* @var string
*/
private $key = '';
@barryhughes
barryhughes / fetch-csv-file.php
Created January 12, 2020 22:04
Load a CSV file and transform to an array. Simple helper for quick and dirty scripting.
<?
/**
* Given a path to a valid CSV file, returns an array containing the data
* (an array of arrays, with outer arrays representing the rows and inner
* arrays representing the columns).
*
* @param string $path_to_csv
*
* @return array
*/
<?php
namespace WooCommerce_Helpers {
/**
* Looks for an existing WooCommerce attribute with the same label, or else
* creates a new attribute using the provided information.
*
* If a new attribute is successfully created, or an existing attribute is
* matched, the attribute object will be returned. Otherwise, bool false is
* returned.
*
@barryhughes
barryhughes / php-streams.http-basic-auth.php
Last active December 31, 2019 15:25
HTTP authentication with file_get_contents() | PHP
<?php
/**
* Make an HTTP request with basic authentication, using 'pure PHP'.
*
* @see https://www.php.net/manual/en/wrappers.http.php
* @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication
*/
$username = 'http_username';
$password = 'http_password';
@barryhughes
barryhughes / password-reset-link-generator.wp-cli.php
Last active December 20, 2019 16:30
Handy dandy WordPress password reset link generator (command line). Useful when we want to create a URL to share with a customer because they are for whatever reason having difficulties using the regular password reset facilities.