Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .monorepo-source.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"productId": "funkycommerce-headless",
"repository": "coded-letter/superfunky-theme",
"source": "coded-letter/coded-letter-monorepo@7d65229487a558b1e2d5a1316ced1022f462fead",
"source": "coded-letter/coded-letter-monorepo@4468ad3ce07b72c1b264ebd3042e4a91779e7c0b",
"sourcePath": "workspace/backend/wordpress/themes/free/funkycommerce-headless",
"installSlug": "funkycommerce-headless",
"kind": "theme",
Expand Down
72 changes: 72 additions & 0 deletions build/archive-settings.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { spawnSync } from "node:child_process";
import { fileURLToPath } from "node:url";
import test from "node:test";
import { Engine } from "php-parser";

const source = (name) => readFileSync(new URL(`../${name}`, import.meta.url), "utf8");
const settings = source("inc/archive-settings.php");
const functions = source("functions.php");
const nativeGrid = source("inc/native-shortcodes.php");
const parser = new Engine({ parser: { suppressErrors: false } });

test("native PHP resolves settings, filter precedence, GraphQL, and option invalidation", (context) => {
const result = spawnSync(process.env.PHP_BINARY || "php", [
fileURLToPath(new URL("archive-settings.test.php", import.meta.url)),
], { encoding: "utf8" });
if (result.error?.code === "ENOENT" && !process.env.PHP_BINARY) {
context.skip("PHP is unavailable; set PHP_BINARY to run native behavior assertions.");
return;
}
assert.ifError(result.error);
assert.equal(result.status, 0, `${result.stdout}\n${result.stderr}`);
assert.match(result.stdout, /Archive settings behavior passed/);
});

test("archive settings parse and load independently of native/headless rendering mode", () => {
assert.doesNotThrow(() => parser.parseCode(settings));
assert.match(functions, /require_once .*\/inc\/archive-settings\.php/);
assert.doesNotMatch(settings, /funkycommerce_is_headless_mode/);
assert.doesNotMatch(source("inc/native-woocommerce.php"), /function funkycommerce_native_products_per_page/);
});

test("GraphQL exposes independent WordPress and effective WooCommerce sizes including unlimited", () => {
assert.match(settings, /get_option\( 'posts_per_page', 10 \)/);
assert.match(settings, /apply_filters\( 'loop_shop_per_page', wc_get_default_products_per_row\(\) \* wc_get_default_product_rows_per_page\(\) \)/);
assert.match(settings, /-1 === \$count \|\| \$count > 0/);
assert.match(settings, /'funkycommerceArchiveSettings'/);
assert.match(settings, /'resolve' => 'funkycommerce_archive_settings'/);
for (const field of ["postsPerPage", "productsPerPage"]) {
assert.match(settings, new RegExp(`'${field}'\\s*=> array\\( 'type' => array\\( 'non_null' => 'Int' \\)`));
}
});

test("saved positive overrides remain shared while an absent override inherits WooCommerce", () => {
assert.match(settings, /\$settings\['products_per_page'\] \?\? 0/);
assert.match(settings, /add_filter\( 'loop_shop_per_page', 'funkycommerce_native_products_per_page', 20 \)/);
const field = source("inc/control-center-schema.php").split("'products_per_page' =>")[1].split("\n")[0];
assert.match(field, /'default' => '0'/);
assert.match(field, /'min' => '0'/);
});

test("reading and WooCommerce option changes invalidate prerender settings and schedule rebuilding", () => {
for (const option of ["posts_per_page", "woocommerce_catalog_columns", "woocommerce_catalog_rows"]) {
assert.ok(settings.includes(`'${option}'`));
}
for (const hook of ["updated_option", "added_option", "deleted_option"]) {
assert.ok(settings.includes(`add_action( '${hook}', 'funkycommerce_collect_archive_setting_change', 30 )`));
}
assert.match(settings, /funkycommerce_schedule_content_build\(\)/);
assert.match(settings, /array\( 'config:storefront' \)/);
assert.match(source("inc/artifact-invalidation.php"), /function funkycommerce_collect_control_center_change[\s\S]*funkycommerce_schedule_content_build\(\)/);
});

test("native and headless grid markers inherit settings unless the editor specifies a positive size", () => {
const grid = functions.split("'grid' => array(")[1].split("'offset'")[0];
assert.match(grid, /'page_size'\s*=> array\( 'default' => 0, 'type' => 'integer', 'min' => 0, 'max' => 48 \)/);
assert.match(nativeGrid, /\$a\['page_size'\] > 0 \? \(int\) \$a\['page_size'\] : \$settings\[ 'product' === \$type \? 'productsPerPage' : 'postsPerPage' \]/);
assert.match(nativeGrid, /'offset'\s*=> -1 === \$per_page \? 0/);
assert.match(nativeGrid, /array_slice\( \$result\['ids'\], \$base_offset \)/);
assert.match(nativeGrid, /-1 !== \$per_page \? \(int\) ceil/);
});
96 changes: 96 additions & 0 deletions build/archive-settings.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

define( 'ABSPATH', __DIR__ );
$options = array();
$hooks = array();
$builds = 0;
$invalidations = array();

function get_option( $key, $default = false ) {
return $GLOBALS['options'][ $key ] ?? $default;
}
function add_filter( $hook, $callback, $priority = 10 ) {
$GLOBALS['hooks'][ $hook ][ $priority ][] = $callback;
}
function add_action( $hook, $callback, $priority = 10 ) {
add_filter( $hook, $callback, $priority );
}
function apply_filters( $hook, $value ) {
$priorities = $GLOBALS['hooks'][ $hook ] ?? array();
ksort( $priorities );
foreach ( $priorities as $callbacks ) {
foreach ( $callbacks as $callback ) {
$value = $callback( $value );
}
}
return $value;
}
function funkycommerce_schedule_content_build() {
++$GLOBALS['builds'];
}
function funkycommerce_collect_artifact_changes( $dependencies, $reason ) {
$GLOBALS['invalidations'][] = array( $dependencies, $reason );
}
function register_graphql_object_type( $name, $definition ) {
$GLOBALS['graphql_types'][ $name ] = $definition;
}
function register_graphql_field( $type, $name, $definition ) {
$GLOBALS['graphql_fields'][ $type ][ $name ] = $definition;
}
function expect_same( $expected, $actual, $message ) {
if ( $expected !== $actual ) {
throw new RuntimeException( $message . ': ' . json_encode( $actual ) );
}
}
function enable_woocommerce_fixture() {
function wc_get_default_products_per_row() {
return get_option( 'woocommerce_catalog_columns', 3 );
}
function wc_get_default_product_rows_per_page() {
return get_option( 'woocommerce_catalog_rows', 4 );
}
}

require __DIR__ . '/../inc/archive-settings.php';

expect_same( array( 'postsPerPage' => 10, 'productsPerPage' => 12 ), funkycommerce_archive_settings(), 'No-Woo defaults' );
$options['posts_per_page'] = '7';
enable_woocommerce_fixture();
$options['woocommerce_catalog_columns'] = 5;
$options['woocommerce_catalog_rows'] = 3;
expect_same( array( 'postsPerPage' => 7, 'productsPerPage' => 15 ), funkycommerce_archive_settings(), 'Native settings are independent' );

$options['funkycommerce_control_center'] = array( 'products_per_page' => '0' );
expect_same( 15, funkycommerce_archive_settings()['productsPerPage'], 'Zero inherits Woo' );
$options['funkycommerce_control_center']['products_per_page'] = '9';
expect_same( 9, funkycommerce_archive_settings()['productsPerPage'], 'Saved overrides remain effective' );
expect_same( 9, apply_filters( 'loop_shop_per_page', 15 ), 'Native and headless loops agree' );
add_filter( 'loop_shop_per_page', static function ( $count ) { return $count * 2; }, 30 );
expect_same( 18, funkycommerce_archive_settings()['productsPerPage'], 'Later Woo filters still apply' );
add_filter( 'loop_shop_per_page', static function () { return -1; }, 40 );
$options['posts_per_page'] = -1;
expect_same( array( 'postsPerPage' => -1, 'productsPerPage' => -1 ), funkycommerce_archive_settings(), 'Unlimited native archives' );
expect_same( 250, funkycommerce_archive_page_size( '250', 10 ), 'Visual size is not capped to transport batch size' );
expect_same( 10, funkycommerce_archive_page_size( 'invalid', 10 ), 'Invalid settings log and use the documented default' );

foreach ( $hooks['graphql_register_types'][10] as $callback ) {
$callback();
}
$field = $GLOBALS['graphql_fields']['RootQuery']['funkycommerceArchiveSettings'];
expect_same( array( 'non_null' => 'FunkyCommerceArchiveSettings' ), $field['type'], 'GraphQL field contract' );
expect_same( funkycommerce_archive_settings(), call_user_func( $field['resolve'] ), 'GraphQL executes the shared resolver' );

foreach ( array( 'updated_option', 'added_option', 'deleted_option' ) as $hook ) {
foreach ( $hooks[ $hook ][30] as $callback ) {
$callback( 'unrelated_option' );
foreach ( array( 'posts_per_page', 'woocommerce_catalog_columns', 'woocommerce_catalog_rows' ) as $option ) {
$callback( $option );
}
}
}
expect_same( 9, $builds, 'Only native archive option changes schedule builds' );
expect_same( 9, count( $invalidations ), 'Each relevant change invalidates hydration' );
foreach ( $invalidations as $invalidation ) {
expect_same( array( array( 'config:storefront' ), 'archive_settings_changed' ), $invalidation, 'Settings dependency is invalidated' );
}
echo "Archive settings behavior passed\n";
5 changes: 3 additions & 2 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
exit;
}

define( 'FUNKYCOMMERCE_HEADLESS_VERSION', '1.2.38' );
define( 'FUNKYCOMMERCE_HEADLESS_VERSION', '1.2.39' );

/**
* Whether Superfunky Pro is active and licensed.
Expand Down Expand Up @@ -171,6 +171,7 @@ function funkycommerce_frontend_url( $path = '' ) {
require_once get_template_directory() . '/inc/admin-view-links.php';
require_once get_template_directory() . '/inc/frontend-theme.php';
require_once get_template_directory() . '/inc/custom-404.php';
require_once get_template_directory() . '/inc/archive-settings.php';
require_once get_template_directory() . '/inc/native-woocommerce.php';
require_once get_template_directory() . '/inc/native-shortcodes.php';
require_once get_template_directory() . '/inc/build-webhooks.php';
Expand Down Expand Up @@ -533,7 +534,7 @@ function funkycommerce_content_shortcode_schemas() {
'card_variant' => array( 'default' => 'default', 'enum' => array( 'default', 'compact', 'editorial', 'minimal', 'gallery', 'simple', 'variation', 'expandable' ) ),
'layout' => array( 'default' => 'standard', 'enum' => array( 'standard', 'compact', 'editorial', 'masonry' ) ),
'columns' => array( 'default' => 3, 'type' => 'integer', 'min' => 1, 'max' => 6 ),
'page_size' => array( 'default' => 12, 'type' => 'integer', 'min' => 1, 'max' => 48 ),
'page_size' => array( 'default' => 0, 'type' => 'integer', 'min' => 0, 'max' => 48 ),
'offset' => funkycommerce_collection_shortcode_offset_definition(),
'paginated' => array( 'default' => 'true', 'type' => 'boolean' ),
'include' => array( 'default' => '' ),
Expand Down
77 changes: 77 additions & 0 deletions inc/archive-settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Native archive sizes shared by WordPress, WooCommerce, and the storefront.
*
* @package FunkyCommerceHeadless
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

function funkycommerce_archive_page_size( $value, $fallback ) {
$count = filter_var( $value, FILTER_VALIDATE_INT );
if ( false !== $count && ( -1 === $count || $count > 0 ) ) {
return $count;
}
error_log( 'FunkyCommerce: invalid native archive page size; using the default.' );
return $fallback;
}

function funkycommerce_native_products_per_page( $count ) {
$settings = (array) get_option( 'funkycommerce_control_center', array() );
$value = $settings['products_per_page'] ?? 0;
return 0 === $value || '0' === $value
? $count
: funkycommerce_archive_page_size( $value, $count );
}
add_filter( 'loop_shop_per_page', 'funkycommerce_native_products_per_page', 20 );

function funkycommerce_archive_settings() {
$products_per_page = 12;
if ( function_exists( 'wc_get_default_products_per_row' ) && function_exists( 'wc_get_default_product_rows_per_page' ) ) {
$products_per_page = funkycommerce_archive_page_size(
apply_filters( 'loop_shop_per_page', wc_get_default_products_per_row() * wc_get_default_product_rows_per_page() ),
12
);
}
return array(
'postsPerPage' => funkycommerce_archive_page_size( get_option( 'posts_per_page', 10 ), 10 ),
'productsPerPage' => $products_per_page,
);
}

function funkycommerce_register_archive_settings_graphql() {
register_graphql_object_type(
'FunkyCommerceArchiveSettings',
array(
'fields' => array(
'postsPerPage' => array( 'type' => array( 'non_null' => 'Int' ) ),
'productsPerPage' => array( 'type' => array( 'non_null' => 'Int' ) ),
),
)
);
register_graphql_field(
'RootQuery',
'funkycommerceArchiveSettings',
array(
'type' => array( 'non_null' => 'FunkyCommerceArchiveSettings' ),
'resolve' => 'funkycommerce_archive_settings',
)
);
}
add_action( 'graphql_register_types', 'funkycommerce_register_archive_settings_graphql' );

function funkycommerce_collect_archive_setting_change( $option ) {
if ( ! in_array( $option, array( 'posts_per_page', 'woocommerce_catalog_columns', 'woocommerce_catalog_rows' ), true ) ) {
return;
}
funkycommerce_schedule_content_build();
funkycommerce_collect_artifact_changes(
array( 'config:storefront' ),
'archive_settings_changed'
);
}
add_action( 'updated_option', 'funkycommerce_collect_archive_setting_change', 30 );
add_action( 'added_option', 'funkycommerce_collect_archive_setting_change', 30 );
add_action( 'deleted_option', 'funkycommerce_collect_archive_setting_change', 30 );
2 changes: 1 addition & 1 deletion inc/control-center-schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ function funkycommerce_control_center_sections() {
'currency_rate_mode' => array( 'label' => __( 'Currency rate mode', 'funkycommerce-headless' ), 'type' => 'select', 'tier' => 'pro', 'default' => 'automatic', 'options' => array( 'automatic' => __( 'Automatic public rates', 'funkycommerce-headless' ), 'manual' => __( 'Manual rates', 'funkycommerce-headless' ) ) ),
'currency_manual_rates' => array( 'label' => __( 'Manual currency rates', 'funkycommerce-headless' ), 'type' => 'json', 'tier' => 'pro', 'default' => '{}', 'description' => __( 'JSON object keyed by ISO currency code, for example {"USD":1.08}.', 'funkycommerce-headless' ) ),
'order_prefix' => array( 'label' => __( 'Order number prefix', 'funkycommerce-headless' ), 'type' => 'text', 'tier' => 'pro', 'default' => '#' ),
'products_per_page' => array( 'label' => __( 'Products per page', 'funkycommerce-headless' ), 'type' => 'number', 'tier' => 'free', 'default' => '12', 'min' => '1', 'max' => '100', 'step' => '1' ),
'products_per_page' => array( 'label' => __( 'Products per page override', 'funkycommerce-headless' ), 'type' => 'number', 'tier' => 'free', 'default' => '0', 'min' => '0', 'max' => '100', 'step' => '1', 'description' => __( '0 follows native WooCommerce products per row and rows per page. A positive value overrides both native and headless product archives.', 'funkycommerce-headless' ) ),
'stock_badge_enabled' => array( 'label' => __( 'Stock-status badges', 'funkycommerce-headless' ), 'type' => 'toggle', 'tier' => 'free', 'default' => 'yes' ),
'reviews_enabled' => array( 'label' => __( 'Product reviews and ratings', 'funkycommerce-headless' ), 'type' => 'toggle', 'tier' => 'free', 'default' => 'yes' ),
'products_no_price_behavior' => array( 'label' => __( 'Products without a price', 'funkycommerce-headless' ), 'type' => 'select', 'tier' => 'free', 'default' => 'free', 'options' => array( 'free' => __( 'Treat as free', 'funkycommerce-headless' ), 'inquiry' => __( 'Show an inquiry form', 'funkycommerce-headless' ) ) ),
Expand Down
10 changes: 7 additions & 3 deletions inc/native-shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,8 @@ function funkycommerce_native_render_grid( $atts ) {
$type = in_array( $a['type'], array( 'product', 'post', 'community-article' ), true ) ? $a['type'] : 'product';

$page = isset( $_GET['fcp'] ) ? max( 1, absint( wp_unslash( $_GET['fcp'] ) ) ) : 1; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$per_page = max( 1, (int) $a['page_size'] );
$settings = funkycommerce_archive_settings();
$per_page = $a['page_size'] > 0 ? (int) $a['page_size'] : $settings[ 'product' === $type ? 'productsPerPage' : 'postsPerPage' ];
$base_offset = max( 0, (int) $a['offset'] );

// WP_Query treats any explicitly-set `offset` (including 0) as overriding
Expand All @@ -1277,10 +1278,13 @@ function funkycommerce_native_render_grid( $atts ) {
$filters['limit'] = $per_page;
$overrides = array(
'posts_per_page' => $per_page,
'offset' => $base_offset + ( ( $page - 1 ) * $per_page ),
'offset' => -1 === $per_page ? 0 : $base_offset + ( ( $page - 1 ) * $per_page ),
);

$result = funkycommerce_native_query_by_type( $type, $filters, $overrides );
if ( -1 === $per_page && $base_offset > 0 ) {
$result['ids'] = array_slice( $result['ids'], $base_offset );
}
$cards = '';
foreach ( $result['ids'] as $id ) {
$cards .= funkycommerce_native_card_for_type( $type, $id, $a['card_variant'] );
Expand All @@ -1291,7 +1295,7 @@ function funkycommerce_native_render_grid( $atts ) {
}

$remaining = max( 0, $result['found'] - $base_offset );
$max_pages = $remaining > 0 ? (int) ceil( $remaining / $per_page ) : 0;
$max_pages = $remaining > 0 && -1 !== $per_page ? (int) ceil( $remaining / $per_page ) : 0;

$pagination = '';
if ( ! empty( $a['paginated'] ) && $max_pages > 1 ) {
Expand Down
11 changes: 0 additions & 11 deletions inc/native-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,6 @@ function funkycommerce_native_woocommerce_body_classes( $classes ) {
}
add_filter( 'body_class', 'funkycommerce_native_woocommerce_body_classes' );

/**
* Reuse the catalogue "Products per page" setting across the shop archive and
* every product taxonomy archive.
*/
function funkycommerce_native_products_per_page( $count ) {
$settings = funkycommerce_native_woocommerce_settings();
$value = absint( $settings['products_per_page'] ?? 0 );
return $value > 0 ? $value : $count;
}
add_filter( 'loop_shop_per_page', 'funkycommerce_native_products_per_page', 20 );

/**
* Whether product reviews and ratings are enabled from the Control Center.
*/
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "funkycommerce-headless-theme",
"private": true,
"version": "1.2.38",
"version": "1.2.39",
"description": "Tailwind/build tooling for the FunkyCommerce Headless WordPress theme's native frontend assets (block templates, template parts, loader, and Spotify slot).",
"scripts": {
"build:css": "tailwindcss -i ./assets/css/theme-source.css -o ./assets/dist/theme.css --minify",
Expand Down
9 changes: 8 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tags: headless, woocommerce, wpgraphql, full-site-editing
Requires at least: 6.7
Tested up to: 6.8
Requires PHP: 7.4
Stable tag: 1.2.38
Stable tag: 1.2.39
License: GPL-2.0-or-later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Expand All @@ -15,6 +15,13 @@ Control Center, the theme also ships a complete native WordPress rendering path
"Native frontend theme" below) with accessible header/footer/navigation templates and
core front/home/singular/archive/search/404 routes styled to match the storefront.

== 1.2.39 highlights ==

* Shares native WordPress posts-per-page and effective WooCommerce products-per-page settings with headless archive grids and pagination.
* Makes grid shortcodes inherit those settings by default; explicit positive page_size values remain editorial overrides.
* Defaults Products per page override to 0 (inherit WooCommerce); existing saved positive overrides remain effective in both rendering modes.
* Schedules storefront rebuilding and settings invalidation when native pagination settings change.

== 1.2.38 highlights ==

* Restores configured, debounced storefront builds for CMS changes in shadow and artifact modes so new Tailwind utilities compile.
Expand Down
2 changes: 1 addition & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Description: A minimal, content-first block theme for polished WordPress preview
Requires at least: 6.7
Tested up to: 6.8
Requires PHP: 7.4
Version: 1.2.38
Version: 1.2.39
Update URI: https://github.com/coded-letter/superfunky-theme
License: GPL-2.0-or-later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Expand Down
Loading