feat: Affiliate program enrichment (Link Builder, Curated Collections, Smart Links)

This commit is contained in:
Dwindi Ramadhana
2026-06-03 14:04:17 +07:00
parent fd8eb38512
commit f8c733832e
22 changed files with 1348 additions and 10 deletions

View File

@@ -48,6 +48,33 @@ class AffiliateCustomerController
'callback' => [$this, 'update_payment_details'],
'permission_callback' => [$this, 'check_permission'],
]);
// Affiliate Collections
register_rest_route($this->namespace, '/account/affiliate/collections', [
[
'methods' => WP_REST_Server::READABLE,
'callback' => [$this, 'get_collections'],
'permission_callback' => [$this, 'check_permission'],
],
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [$this, 'create_collection'],
'permission_callback' => [$this, 'check_permission'],
]
]);
register_rest_route($this->namespace, '/account/affiliate/collections/(?P<id>\d+)', [
[
'methods' => WP_REST_Server::EDITABLE,
'callback' => [$this, 'update_collection'],
'permission_callback' => [$this, 'check_permission'],
],
[
'methods' => WP_REST_Server::DELETABLE,
'callback' => [$this, 'delete_collection'],
'permission_callback' => [$this, 'check_permission'],
]
]);
}
public function check_permission()
@@ -89,6 +116,12 @@ class AffiliateCustomerController
$affiliate['commission_rate'] = $effective_rate;
$affiliate['total_earnings'] = $earnings->total_earnings ?: 0;
$affiliate['pending_earnings'] = $earnings->pending_earnings ?: 0;
if (class_exists('\WooNooW\Modules\Affiliate\AffiliateSettings')) {
$affiliate['collections_enabled'] = \WooNooW\Modules\Affiliate\AffiliateSettings::get_setting('woonoow_affiliate_enable_curated_collections', true);
} else {
$affiliate['collections_enabled'] = true;
}
return rest_ensure_response($affiliate);
}
@@ -318,4 +351,125 @@ class AffiliateCustomerController
return $sanitized;
}
// --- Collections ---
public function get_collections(WP_REST_Request $request)
{
if (class_exists('\WooNooW\Modules\Affiliate\AffiliateSettings') && !\WooNooW\Modules\Affiliate\AffiliateSettings::get_setting('woonoow_affiliate_enable_curated_collections', true)) {
return new \WP_Error('rest_forbidden', 'Curated collections are disabled.', ['status' => 403]);
}
global $wpdb;
$user_id = get_current_user_id();
$affiliate_table = $wpdb->prefix . 'woonoow_affiliates';
$collections_table = $wpdb->prefix . 'woonoow_affiliate_collections';
$affiliate = $wpdb->get_row($wpdb->prepare("SELECT id, referral_code FROM $affiliate_table WHERE user_id = %d", $user_id));
if (!$affiliate) {
return rest_ensure_response([]);
}
$collections = $wpdb->get_results($wpdb->prepare("SELECT * FROM $collections_table WHERE affiliate_id = %d ORDER BY created_at DESC", $affiliate->id), ARRAY_A);
foreach ($collections as &$collection) {
$collection['product_ids'] = $collection['product_ids'] ? json_decode($collection['product_ids'], true) : [];
$collection['link'] = site_url("/collection/{$collection['slug']}");
}
return rest_ensure_response($collections);
}
public function create_collection(WP_REST_Request $request)
{
global $wpdb;
$user_id = get_current_user_id();
$affiliate_table = $wpdb->prefix . 'woonoow_affiliates';
$collections_table = $wpdb->prefix . 'woonoow_affiliate_collections';
$affiliate = $wpdb->get_row($wpdb->prepare("SELECT id FROM $affiliate_table WHERE user_id = %d", $user_id));
if (!$affiliate) {
return new \WP_Error('not_found', 'Affiliate profile not found', ['status' => 404]);
}
$title = sanitize_text_field($request->get_param('title'));
$description = sanitize_textarea_field($request->get_param('description'));
$product_ids = $request->get_param('product_ids');
if (!is_array($product_ids)) $product_ids = [];
$product_ids = array_map('intval', $product_ids);
if (count($product_ids) > 20) {
return new \WP_Error('too_many_products', 'A collection can have a maximum of 20 products.', ['status' => 400]);
}
$slug = sanitize_title($title);
// Check unique slug for this affiliate
$existing = $wpdb->get_var($wpdb->prepare("SELECT id FROM $collections_table WHERE affiliate_id = %d AND slug = %s", $affiliate->id, $slug));
if ($existing) {
$slug .= '-' . wp_generate_password(4, false);
}
$data = [
'affiliate_id' => $affiliate->id,
'title' => $title,
'slug' => $slug,
'description' => $description,
'product_ids' => json_encode($product_ids)
];
$wpdb->insert($collections_table, $data);
$data['id'] = $wpdb->insert_id;
$data['product_ids'] = $product_ids;
return rest_ensure_response($data);
}
public function update_collection(WP_REST_Request $request)
{
global $wpdb;
$user_id = get_current_user_id();
$id = (int) $request->get_param('id');
$affiliate_table = $wpdb->prefix . 'woonoow_affiliates';
$collections_table = $wpdb->prefix . 'woonoow_affiliate_collections';
$affiliate = $wpdb->get_row($wpdb->prepare("SELECT id FROM $affiliate_table WHERE user_id = %d", $user_id));
if (!$affiliate) return new \WP_Error('unauthorized', 'Unauthorized', ['status' => 401]);
$collection = $wpdb->get_row($wpdb->prepare("SELECT id FROM $collections_table WHERE id = %d AND affiliate_id = %d", $id, $affiliate->id));
if (!$collection) return new \WP_Error('not_found', 'Collection not found', ['status' => 404]);
$title = sanitize_text_field($request->get_param('title'));
$description = sanitize_textarea_field($request->get_param('description'));
$product_ids = $request->get_param('product_ids');
if (!is_array($product_ids)) $product_ids = [];
$product_ids = array_map('intval', $product_ids);
if (count($product_ids) > 20) {
return new \WP_Error('too_many_products', 'A collection can have a maximum of 20 products.', ['status' => 400]);
}
$wpdb->update($collections_table, [
'title' => $title,
'description' => $description,
'product_ids' => json_encode($product_ids)
], ['id' => $id]);
return rest_ensure_response(['success' => true]);
}
public function delete_collection(WP_REST_Request $request)
{
global $wpdb;
$user_id = get_current_user_id();
$id = (int) $request->get_param('id');
$affiliate_table = $wpdb->prefix . 'woonoow_affiliates';
$collections_table = $wpdb->prefix . 'woonoow_affiliate_collections';
$affiliate = $wpdb->get_row($wpdb->prepare("SELECT id FROM $affiliate_table WHERE user_id = %d", $user_id));
if (!$affiliate) return new \WP_Error('unauthorized', 'Unauthorized', ['status' => 401]);
$wpdb->delete($collections_table, ['id' => $id, 'affiliate_id' => $affiliate->id]);
return rest_ensure_response(['success' => true]);
}
}

View File

@@ -28,6 +28,7 @@ use WooNooW\Branding;
use WooNooW\Frontend\Assets as FrontendAssets;
use WooNooW\Frontend\Shortcodes;
use WooNooW\Frontend\TemplateOverride;
use WooNooW\Frontend\SmartRotator;
use WooNooW\Frontend\PageAppearance;
class Bootstrap {
@@ -47,6 +48,7 @@ class Bootstrap {
FrontendAssets::init();
// Note: Shortcodes removed - WC pages now redirect to SPA routes via TemplateOverride
TemplateOverride::init();
SmartRotator::init();
new PageAppearance();
// Activity Log
@@ -75,6 +77,13 @@ class Bootstrap {
// Load custom variation attributes for WooCommerce admin
add_action('woocommerce_product_variation_object_read', [self::class, 'load_variation_attributes']);
}
public static function init_frontend() {
Shortcodes::init();
TemplateOverride::init();
SmartRotator::init();
PageAppearance::init();
}
/**
* Properly initialize WooCommerce cart for REST API requests

View File

@@ -243,6 +243,19 @@ class Assets
$base_path = '';
}
// Also force empty base path for /collection/ routes since they are global
$spa_path_var = get_query_var('woonoow_spa_path');
if (!empty($spa_path_var) && strpos($spa_path_var, 'collection/') === 0) {
$base_path = '';
}
// Handle serve_spa_for_frontpage_routes which bypasses WP queries
$request_uri = $_SERVER['REQUEST_URI'] ?? '/';
$path = parse_url($request_uri, PHP_URL_PATH);
if (strpos($path, '/collection/') === 0) {
$base_path = '';
}
// Check if BrowserRouter is enabled (default: true for SEO)
$use_browser_router = $appearance_settings['general']['use_browser_router'] ?? true;
@@ -263,6 +276,9 @@ class Assets
'useBrowserRouter' => $use_browser_router,
'frontPageSlug' => $front_page_slug,
'spaMode' => $appearance_settings['general']['spa_mode'] ?? 'full',
'affiliateSettings' => [
'enableCuratedCollections' => class_exists('\WooNooW\Modules\Affiliate\AffiliateSettings') ? \WooNooW\Modules\Affiliate\AffiliateSettings::get_setting('woonoow_affiliate_enable_curated_collections', true) : false,
],
'security' => \WooNooW\Compat\SecuritySettingsProvider::get_public_settings(),
];
@@ -460,7 +476,7 @@ class Assets
}
// Check path prefixes
$prefix_routes = ['/shop/', '/my-account/', '/product/'];
$prefix_routes = ['/shop/', '/my-account/', '/product/', '/collection/'];
foreach ($prefix_routes as $prefix) {
if (strpos($path, $prefix) === 0) {
return true;

View File

@@ -98,6 +98,13 @@ class ShopController
],
],
]);
// Get affiliate collection (public)
register_rest_route($namespace, '/shop/collections/(?P<slug>[a-zA-Z0-9-]+)', [
'methods' => 'GET',
'callback' => [__CLASS__, 'get_collection'],
'permission_callback' => '__return_true',
]);
}
/**
@@ -290,6 +297,57 @@ class ShopController
return new WP_REST_Response($products, 200);
}
/**
* Get affiliate collection by slug
*/
public static function get_collection(WP_REST_Request $request)
{
global $wpdb;
$slug = sanitize_title($request->get_param('slug'));
$collections_table = $wpdb->prefix . 'woonoow_affiliate_collections';
$collection = $wpdb->get_row($wpdb->prepare("SELECT * FROM $collections_table WHERE slug = %s", $slug));
if (!$collection) {
return new WP_Error('not_found', 'Collection not found', ['status' => 404]);
}
$product_ids = $collection->product_ids ? json_decode($collection->product_ids, true) : [];
if (!is_array($product_ids)) $product_ids = [];
$products = [];
if (!empty($product_ids)) {
$args = [
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'post__in' => $product_ids,
'orderby' => 'post__in',
];
$query = new \WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$product = wc_get_product(get_the_ID());
if ($product) {
$products[] = self::format_product($product);
}
}
wp_reset_postdata();
}
}
$data = [
'id' => $collection->id,
'title' => $collection->title,
'description' => $collection->description,
'products' => $products
];
return new WP_REST_Response($data, 200);
}
/**
* Format product data for API response
*/

View File

@@ -0,0 +1,83 @@
<?php
namespace WooNooW\Frontend;
if (!defined('ABSPATH')) exit;
class SmartRotator
{
public static function init()
{
add_action('init', [__CLASS__, 'register_rewrite_rules']);
add_filter('query_vars', [__CLASS__, 'register_query_vars']);
add_action('template_redirect', [__CLASS__, 'handle_redirect']);
}
public static function register_rewrite_rules()
{
// Match /go/collection-slug
add_rewrite_rule(
'^go/([^/]+)/?$',
'index.php?woonoow_go_slug=$matches[1]',
'top'
);
}
public static function register_query_vars($vars)
{
$vars[] = 'woonoow_go_slug';
return $vars;
}
public static function handle_redirect()
{
$slug = get_query_var('woonoow_go_slug');
if (empty($slug)) {
return;
}
global $wpdb;
$collections_table = $wpdb->prefix . 'woonoow_affiliate_collections';
$affiliates_table = $wpdb->prefix . 'woonoow_affiliates';
// Lookup collection and affiliate in one query
$query = $wpdb->prepare(
"SELECT c.product_ids, a.referral_code
FROM {$collections_table} c
JOIN {$affiliates_table} a ON c.affiliate_id = a.id
WHERE c.slug = %s LIMIT 1",
$slug
);
$result = $wpdb->get_row($query);
if (!$result || empty($result->product_ids)) {
// Fallback: 404 or redirect to shop
wp_safe_redirect(site_url('/shop/'));
exit;
}
$product_ids = json_decode($result->product_ids, true);
if (!is_array($product_ids) || empty($product_ids)) {
wp_safe_redirect(site_url('/shop/'));
exit;
}
// Randomly pick a product
$random_product_id = $product_ids[array_rand($product_ids)];
// Get the permalink for the product
$target_url = get_permalink($random_product_id);
if (!$target_url) {
wp_safe_redirect(site_url('/shop/'));
exit;
}
// Append the affiliate referral code
$target_url = add_query_arg('ref', $result->referral_code, $target_url);
// Redirect to the product page
wp_redirect($target_url, 302);
exit;
}
}

View File

@@ -211,6 +211,13 @@ class TemplateOverride
);
}
// /collection/* → SPA page (global, independent of frontpage setting)
add_rewrite_rule(
'^collection/(.*)$',
'index.php?page_id=' . $spa_page_id . '&woonoow_spa_path=collection/$matches[1]',
'top'
);
// Register query var for the SPA path
add_filter('query_vars', function ($vars) {
$vars[] = 'woonoow_spa_path';
@@ -430,7 +437,7 @@ class TemplateOverride
}
// Check path prefixes (for sub-routes)
$prefix_routes = ['/shop/', '/my-account/', '/product/', '/checkout/'];
$prefix_routes = ['/shop/', '/my-account/', '/product/', '/checkout/', '/collection/'];
foreach ($prefix_routes as $prefix) {
if (strpos($path, $prefix) === 0) {
$should_serve_spa = true;
@@ -500,7 +507,7 @@ class TemplateOverride
// Check if this is a SPA route
// We include /product/ and standard endpoints
$spa_routes = ['/product/', '/cart', '/checkout', '/my-account'];
$spa_routes = ['/product/', '/cart', '/checkout', '/my-account', '/go/'];
foreach ($spa_routes as $route) {
if (strpos($requested_url, $route) !== false) {

View File

@@ -17,6 +17,7 @@ class AffiliateManager
private static $affiliates_table = 'woonoow_affiliates';
private static $referrals_table = 'woonoow_referrals';
private static $payouts_table = 'woonoow_affiliate_payouts';
private static $collections_table = 'woonoow_affiliate_collections';
/**
* Initialize
@@ -37,6 +38,7 @@ class AffiliateManager
$affiliates_table = $wpdb->prefix . self::$affiliates_table;
$referrals_table = $wpdb->prefix . self::$referrals_table;
$payouts_table = $wpdb->prefix . self::$payouts_table;
$collections_table = $wpdb->prefix . self::$collections_table;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
@@ -135,6 +137,23 @@ class AffiliateManager
}
}
// Collections Table
$sql_collections = "CREATE TABLE $collections_table (
id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
affiliate_id bigint(20) UNSIGNED NOT NULL,
title varchar(255) NOT NULL,
slug varchar(255) NOT NULL,
description text DEFAULT NULL,
product_ids longtext DEFAULT NULL,
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY affiliate_slug (affiliate_id, slug),
KEY affiliate_id (affiliate_id)
) $charset_collate;";
dbDelta($sql_collections);
// Payouts Table
$sql_payouts = "CREATE TABLE $payouts_table (
id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,

View File

@@ -108,6 +108,12 @@ class AffiliateModule
}
}
}
// Create collections table if it doesn't exist
$collections_table = $wpdb->prefix . 'woonoow_affiliate_collections';
if ($wpdb->get_var("SHOW TABLES LIKE '$collections_table'") !== $collections_table) {
AffiliateManager::create_tables();
}
}
/**

View File

@@ -58,6 +58,12 @@ class AffiliateSettings {
'description' => __('Automatically approve new affiliate applications.', 'woonoow'),
'default' => false,
],
'woonoow_affiliate_enable_curated_collections' => [
'type' => 'toggle',
'label' => __('Enable Curated Collections', 'woonoow'),
'description' => __('Allow affiliates to create and share custom curated product collections.', 'woonoow'),
'default' => true,
],
'woonoow_affiliate_allow_self_referral' => [
'type' => 'toggle',
'label' => __('Allow Self-Referrals', 'woonoow'),

View File

@@ -102,9 +102,37 @@ class AffiliateTracker
'samesite' => 'Lax'
];
// Capture referral code
$referral_code = '';
// 1. Capture from ?ref= parameter
if (isset($_GET['ref']) && !empty($_GET['ref'])) {
$referral_code = sanitize_text_field($_GET['ref']);
}
// 2. Or capture from collection slug in URL (e.g., /collection/my-slug)
if (empty($referral_code)) {
$request_uri = $_SERVER['REQUEST_URI'] ?? '/';
$path = parse_url($request_uri, PHP_URL_PATH);
// Extract collection slug, accounting for possible subdirectories (e.g. /store/collection/slug)
if (preg_match('#/collection/([^/]+)#', $path, $matches)) {
$collection_slug = sanitize_text_field($matches[1]);
global $wpdb;
$collections_table = $wpdb->prefix . 'woonoow_affiliate_collections';
$affiliates_table = $wpdb->prefix . 'woonoow_affiliates';
$referral_code = $wpdb->get_var($wpdb->prepare("
SELECT a.referral_code
FROM $collections_table c
JOIN $affiliates_table a ON c.affiliate_id = a.id
WHERE c.slug = %s
", $collection_slug));
}
}
// Set the cookie if we found a referral code
if (!empty($referral_code)) {
$result = setcookie(self::COOKIE_NAME, $referral_code, $options);
$_COOKIE[self::COOKIE_NAME] = $referral_code;
error_log('[AffiliateTracker] Set woonoow_ref cookie: ' . $referral_code . ', result=' . ($result ? 'true' : 'false'));