the field the facts read. * * @var array> */ public const KEYS = array( 'product' => array( '_price' => 'sale_price', '_regular_price' => 'regular_price', '_sale_price' => 'sale_price', '_sale_price_dates_from' => 'date_on_sale_from', '_sale_price_dates_to' => 'date_on_sale_to', '_stock' => 'stock_quantity', '_stock_status' => 'stock_status', '_manage_stock' => 'manage_stock', '_backorders' => 'backorders', '_low_stock_amount' => 'low_stock_amount', '_sold_individually' => 'sold_individually', '_tax_status' => 'tax_status', '_tax_class' => 'tax_class', '_downloadable_files' => 'downloads', '_product_attributes' => 'attributes', '_children' => 'children', ), 'shop_coupon' => array( 'coupon_amount' => 'amount', 'discount_type' => 'discount_type', 'usage_limit' => 'usage_limit', 'usage_limit_per_user' => 'usage_limit_per_user', 'limit_usage_to_x_items' => 'limit_usage_to_x_items', 'date_expires' => 'date_expires', 'free_shipping' => 'free_shipping', 'individual_use' => 'individual_use', 'minimum_amount' => 'minimum_amount', 'maximum_amount' => 'maximum_amount', 'product_ids' => 'product_ids', 'exclude_product_ids' => 'excluded_product_ids', 'customer_email' => 'email_restrictions', ), 'shop_order' => array( '_order_total' => 'total', '_order_currency' => 'currency', '_payment_method' => 'payment_method', '_customer_user' => 'customer_id', '_paid_date' => 'date_paid', '_date_paid' => 'date_paid', ), ); /** * Post type => entity type. */ private const ENTITIES = array( 'product' => 'product', 'product_variation' => 'product_variation', 'shop_coupon' => 'coupon', 'shop_order' => 'order', ); /** * Decides an effect: Api::hold_at_effect(). * * @var Closure(ActionDescriptor, array, string, bool): EffectOutcome */ private Closure $decide; /** * Constructor. * * @param (Closure(ActionDescriptor, array, string, bool): EffectOutcome)|null $decide Effect decision; defaults to the parent's API. */ public function __construct( ?Closure $decide = null ) { $this->decide = $decide ?? static fn( ActionDescriptor $action, array $envelope, string $provider, bool $independent ): EffectOutcome => Api::hold_at_effect( $action, $envelope, $provider, $independent ); } /** * Hook the three short-circuits. */ public function register(): void { add_filter( 'update_post_metadata', array( $this, 'guard_update' ), 10, 4 ); add_filter( 'add_post_metadata', array( $this, 'guard_add' ), 10, 4 ); add_filter( 'delete_post_metadata', array( $this, 'guard_delete' ), 10, 5 ); } /** * Guard update_post_meta(). * * @param mixed $check Null to proceed; anything else short-circuits. * @param int $object_id Post id. * @param string $meta_key Meta key. * @param mixed $meta_value New value. * @return mixed */ public function guard_update( $check, $object_id, $meta_key, $meta_value ) { return $this->guard( $check, (int) $object_id, (string) $meta_key, $meta_value, 'update' ); } /** * Guard add_post_meta(). * * @param mixed $check Null to proceed; anything else short-circuits. * @param int $object_id Post id. * @param string $meta_key Meta key. * @param mixed $meta_value Value. * @return mixed */ public function guard_add( $check, $object_id, $meta_key, $meta_value ) { return $this->guard( $check, (int) $object_id, (string) $meta_key, $meta_value, 'add' ); } /** * Guard delete_post_meta(), and delete_metadata() across every post. * * @param mixed $check Null to proceed; anything else short-circuits. * @param int $object_id Post id; 0 with $delete_all. * @param string $meta_key Meta key. * @param mixed $meta_value Value to match, or empty. * @param bool $delete_all Delete the key from every post. * @return mixed */ public function guard_delete( $check, $object_id, $meta_key, $meta_value, $delete_all = false ) { if ( $delete_all ) { return $this->guard_everywhere( $check, (string) $meta_key ); } return $this->guard( $check, (int) $object_id, (string) $meta_key, $meta_value, 'delete' ); } /** * Decide a guarded meta write to one post. * * @param mixed $check The short-circuit so far. * @param int $post_id Post id. * @param string $meta_key Meta key. * @param mixed $meta_value Value. * @param string $operation add, update or delete. * @return mixed */ private function guard( $check, int $post_id, string $meta_key, $meta_value, string $operation ) { if ( null !== $check || null === Api::current_agent() ) { return $check; } $post_type = (string) get_post_type( $post_id ); $field = self::field( $post_type, $meta_key ); $type = self::ENTITIES[ $post_type ] ?? null; if ( null === $field || null === $type || InFlight::covers( $type, $post_id ) ) { return $check; } $action = new ActionDescriptor( 'unclassified', 'update', $type, $post_id, null, array( $field => 'delete' === $operation || ! is_scalar( $meta_value ) ? null : $meta_value, 'key' => $meta_key, 'hook' => $operation . '_post_metadata', ) ); $envelope = array( 'post_id' => $post_id, 'key' => $meta_key, 'value' => $meta_value, 'operation' => $operation, ); return $this->decide( $action, $envelope ) ? false : $check; } /** * Decide deleting a guarded key from every post. It has no one target * and cannot be replayed, so a hold is a denial. * * @param mixed $check The short-circuit so far. * @param string $meta_key Meta key. * @return mixed */ private function guard_everywhere( $check, string $meta_key ) { if ( null !== $check || null === Api::current_agent() ) { return $check; } foreach ( self::ENTITIES as $post_type => $type ) { if ( null === self::field( $post_type, $meta_key ) ) { continue; } $action = new ActionDescriptor( 'unclassified', 'update', $type, null, null, array( 'key' => $meta_key, 'delete_all' => true, 'hook' => 'delete_post_metadata', ) ); return $this->decide( $action, array( 'unreplayable' => $this ) ) ? false : $check; } return $check; } /** * Ask the firewall whether to veto, failing closed. * * @param ActionDescriptor $action The write. * @param array $envelope What a replay needs. */ private function decide( ActionDescriptor $action, array $envelope ): bool { try { return ( $this->decide )( $action, $envelope, self::PROVIDER_ID, false )->vetoes(); } catch ( \Throwable $failure ) { return true; } } /** * The field a guarded key holds for a post type, matched as MySQL * matches meta keys - case-insensitively, trailing spaces ignored - or * null when the key is not guarded there. * * @param string $post_type Post type. * @param string $meta_key Meta key as written. */ public static function field( string $post_type, string $meta_key ): ?string { $keys = self::KEYS[ 'product_variation' === $post_type ? 'product' : $post_type ] ?? array(); return $keys[ strtolower( rtrim( $meta_key ) ) ] ?? null; } }