|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Facebook Commerce Recommendation Override for /fbcollection/ |
| 4 | + * New URL Example: /fbcollection/?clicked_product_id=SKU123_123&shown_product_ids=SKU456_456,SKU789_789 |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Facebook\WooCommerce; |
| 8 | + |
| 9 | +use WooCommerce\Facebook\Framework\Logger; |
| 10 | + |
| 11 | +defined( 'ABSPATH' ) || exit; |
| 12 | + |
| 13 | +/** |
| 14 | + * Commerce Page Override class for handling /fbcollection/ endpoint |
| 15 | + */ |
| 16 | +class Commerce_Page_Override { |
| 17 | + |
| 18 | + const REWRITE_VERSION = '1.0.0'; // Bump this ONLY when rewrite rules change. |
| 19 | + |
| 20 | + public function __construct() { |
| 21 | + add_action( 'init', [ $this, 'register_rewrite_rule' ] ); |
| 22 | + add_filter( 'query_vars', [ $this, 'add_query_vars' ] ); |
| 23 | + add_action( 'woocommerce_product_query', [ $this, 'modify_product_query' ] ); |
| 24 | + add_action( 'plugins_loaded', [ $this, 'check_and_trigger_flush' ] ); |
| 25 | + add_action( 'init', [ $this, 'flush_rewrite_if_needed' ] ); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Register /fbcollection/ as a virtual WooCommerce archive page. |
| 30 | + */ |
| 31 | + public function register_rewrite_rule() { |
| 32 | + add_rewrite_rule( '^fbcollection/?$', 'index.php?post_type=product&custom_fbcollection_page=1', 'top' ); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Add custom query variable. |
| 37 | + * |
| 38 | + * @param array $vars Query variables. |
| 39 | + * @return array |
| 40 | + */ |
| 41 | + public function add_query_vars( $vars ) { |
| 42 | + $vars[] = 'custom_fbcollection_page'; |
| 43 | + return $vars; |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Modify WooCommerce product query to inject custom product IDs. |
| 48 | + * |
| 49 | + * @param WP_Query $query The WooCommerce product query. |
| 50 | + */ |
| 51 | + public function modify_product_query( $query ) { |
| 52 | + if ( 1 !== intval( get_query_var( 'custom_fbcollection_page' ) ) ) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + // Debug logger to verify query filtering triggers |
| 57 | + Logger::log( |
| 58 | + 'FBCollection Query Triggered: ' . print_r( $_GET, true ), |
| 59 | + [], |
| 60 | + array( |
| 61 | + 'should_send_log_to_meta' => false, |
| 62 | + 'should_save_log_in_woocommerce' => true, |
| 63 | + 'woocommerce_log_level' => \WC_Log_Levels::DEBUG, |
| 64 | + ) |
| 65 | + ); |
| 66 | + |
| 67 | + // Helper to extract product_id from retailer_id format |
| 68 | + $extract_product_id = function ( $retailer_id ) { |
| 69 | + if ( false !== strpos( $retailer_id, '_' ) ) { |
| 70 | + $parts = explode( '_', $retailer_id ); |
| 71 | + return absint( end( $parts ) ); |
| 72 | + } |
| 73 | + return absint( $retailer_id ); |
| 74 | + }; |
| 75 | + |
| 76 | + // Parse clicked_product_id |
| 77 | + $clicked_product_id_raw = sanitize_text_field( wp_unslash( $_GET['clicked_product_id'] ?? '' ) ); |
| 78 | + $clicked_product_id = $extract_product_id( $clicked_product_id_raw ); |
| 79 | + |
| 80 | + // Parse shown_product_ids |
| 81 | + $shown_product_ids_raw = explode( ',', sanitize_text_field( wp_unslash( $_GET['shown_product_ids'] ?? '' ) ) ); |
| 82 | + $shown_product_ids = array_map( $extract_product_id, array_map( 'sanitize_text_field', $shown_product_ids_raw ) ); |
| 83 | + $shown_product_ids = array_filter( $shown_product_ids ); // Remove empty/invalid |
| 84 | + $shown_product_ids = array_slice( $shown_product_ids, 0, 30 ); // Limit to 30 |
| 85 | + |
| 86 | + $final_product_ids = []; |
| 87 | + |
| 88 | + if ( $clicked_product_id && 'product' === get_post_type( $clicked_product_id ) ) { |
| 89 | + $final_product_ids[] = $clicked_product_id; |
| 90 | + } |
| 91 | + |
| 92 | + $valid_shown_ids = array_filter( |
| 93 | + $shown_product_ids, |
| 94 | + function ( $id ) use ( $clicked_product_id ) { |
| 95 | + if ( $clicked_product_id === $id ) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + $product = wc_get_product( $id ); |
| 99 | + return $product && $product->is_visible(); |
| 100 | + } |
| 101 | + ); |
| 102 | + |
| 103 | + $final_product_ids = array_merge( $final_product_ids, $valid_shown_ids ); |
| 104 | + |
| 105 | + if ( ! empty( $final_product_ids ) ) { |
| 106 | + $query->set( 'post__in', $final_product_ids ); |
| 107 | + $query->set( 'orderby', 'post__in' ); |
| 108 | + $query->set( 'posts_per_page', count( $final_product_ids ) ); |
| 109 | + } else { |
| 110 | + $query->set( 'orderby', 'popularity' ); |
| 111 | + $query->set( 'posts_per_page', 8 ); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Versioned Rewrite Rules Flush on Upgrade. |
| 117 | + */ |
| 118 | + public function check_and_trigger_flush() { |
| 119 | + $stored_version = get_option( 'fbwcommerce_rewrites_flushed' ); |
| 120 | + if ( self::REWRITE_VERSION !== $stored_version ) { |
| 121 | + update_option( 'fbwcommerce_flush_needed', true ); |
| 122 | + update_option( 'fbwcommerce_rewrites_flushed', self::REWRITE_VERSION ); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public function flush_rewrite_if_needed() { |
| 127 | + // This function checks if a flush is required (flagged by check_and_trigger_flush) |
| 128 | + // If the flag is set, it flushes rewrite rules and deletes the flag to avoid repeat flushes. |
| 129 | + if ( get_option( 'fbwcommerce_flush_needed' ) ) { |
| 130 | + flush_rewrite_rules(); |
| 131 | + delete_option( 'fbwcommerce_flush_needed' ); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments