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 / entity-arb-data-maps.php
Created March 6, 2024 21:23
WooCommerce/dynamic properties: associating arbitrary values with individual entity instances.
<?php
/**
* Simple approach showing a structured approach to associating arbitrary, ephemeral
* (non-persisted) data with individual product objects and other entities.
*
* Benefits include avoidance of collisions between two plugins using the same key,
* the range of checks that can be applied to stored values, etc.
*
* @link https://github.com/woocommerce/woocommerce/issues/45286
*/
@barryhughes
barryhughes / scenarios-for-17355.php
Created January 30, 2024 15:25
Mu-plugin code that continuously assigns (currently unassigned) product attribute terms to products, then creates yet more unassigned product attribute terms. This purposefully uses the WP API and avoids the WC API, as a way to replicate conditions that *might* lead to the problem described in https://github.com/woocommerce/woocommerce/issues/17355
<?php
/**
* Assigns currently unassigned product attribute terms to products, to try and
* replicate the problem in the linked issue. Also generates new unassigned terms
* on each request, to further exacerbate things.
*
* We deliberately do not use the WooCommerce API, because in this case we are
* try to circumvent WC's cache invalidation logic.
*
* @see https://github.com/woocommerce/woocommerce/issues/17355
@barryhughes
barryhughes / detect-legacy-wc-rest-api-usage.php
Last active November 27, 2023 18:11
Detect usage of WooCommerce's legacy REST API. Logs any attempts, and displays an admin notice.
<?php
/**
* Plugin name: Detect Legacy REST API Usage (WooCommerce)
* Description: Attempts to detect and log usage of WooCommerce's legacy REST API.
* Version: 2023-11-24.1
*/
function detect_and_log_wc_legacy_api_requests() {
global $wp;
@barryhughes
barryhughes / woocommerce-802-kill-top-seller-query.php
Created August 23, 2023 17:02
"Nullify" the top seller query, within the WooCommerce Status dashboard widget (might be helpful, until such time as the query results are cached).
<?php
/**
* The top-seller query (used for the WooCommerce Status dashboard widget)
* runs on every dashboard request.
*
* It can effectively be nullified using this snippet, if the performance
* impact is too great.
*
* (Tested with WC 8.0.2)
@barryhughes
barryhughes / live-click-org-email.php
Last active July 3, 2023 23:50
Add to a custom plugin file or your theme's functions.php file to convert organizer email addresses to live, clickable links
/**
* Convert organizer emails into live "mailto:" links that users can click on.
*
* @param $email
* @return string
*/
function organizer_live_email_link( $email ) {
if ( ! is_email( $email ) || ! is_singular( Tribe__Events__Main::POSTTYPE ) ) return $email;
return '<a href="mailto:' . esc_attr( $email ) . '">' . esc_html( $email ) . '</a>';
}
@barryhughes
barryhughes / woo-store-api.full-variation-data.php
Last active May 12, 2023 13:44
Alter the wp-json/wc/store/v1/products/<ID> response so that it includes 'more complete' information about variations.
<?php
/**
* Modify /wp-json/wc/store/v1/products/<ID> so that it returns more complete data about each variation.
*
* Not extensively tested, just captures an idea about how we might do this.
*/
add_filter( 'rest_request_after_callbacks', function ( $response, $handler, $request ) {
// If this is not a Store API product request, do not interfere.
if ( ! str_starts_with( $request->get_route(), '/wc/store/v1/products/' ) || $request->get_method() !== 'GET' ) {
@barryhughes
barryhughes / pr37847-alt__shipping-flat-rate-class-handling.diff
Created May 10, 2023 23:48
A possible alternative to the initial solution in WooCommerce PR#37847 (relates to this comment: https://github.com/woocommerce/woocommerce/pull/37847#issuecomment-1542940612)
diff --git a/plugins/woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php b/plugins/woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php
index 4b140a814f..5719a8a12b 100644
--- a/plugins/woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php
+++ b/plugins/woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php
@@ -177,8 +177,16 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
// Also handles BW compatibility when slugs were used instead of ids.
$shipping_class_term = get_term_by( 'slug', $shipping_class, 'product_shipping_class' );
$class_cost_string = $shipping_class_term && $shipping_class_term->term_id ? $this->get_option( 'class_cost_' . $shipping_class_term->term_id, $this->get_option( 'class_cost_' . $shipping_class, '' ) ) : $this->get_option( 'no_class_cost', '' );
-
- if ( '' === $class_cost_string ) {
@barryhughes
barryhughes / as-update-callback-cleanup.sql
Last active December 21, 2022 20:27
Cleanup `woocommerce_run_update_callback` actions.
-- Cleans up any non-pending WooCommerce Update Callback scheduled actions
-- and the corresponding log table entries.
--
-- Note that in many cases you may need to adjust the table prefix from
-- 'wp_' to whatever is in effect within your site.
DELETE actions,
action_logs
FROM wp_actionscheduler_actions AS actions
JOIN wp_actionscheduler_logs AS action_logs
@barryhughes
barryhughes / as-cleanup.sql
Created December 21, 2022 20:18
Delete non-pending actions and their logs (Action Scheduler)
DELETE actions,
action_logs
FROM wp_actionscheduler_actions AS actions
JOIN wp_actionscheduler_logs AS action_logs
ON actions.action_id = action_logs.action_id
WHERE actions.hook = 'woocommerce_run_update_callback'
AND actions.status <> 'pending'
@barryhughes
barryhughes / plain-textify-html-links.php
Created October 10, 2019 15:38
Inelegant but handy and regex-free way of swapping out HTML links with a plain text equivalent.