|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Class to handle filtering fields in the checkout billing form. |
| 4 | + * |
| 5 | + * @see https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/ |
| 6 | + * @author Michael Beckwith <[email protected]> |
| 7 | + * @package WebDevStudios\CCForWoo\View\Checkout |
| 8 | + * @since 2019-08-22 |
| 9 | + */ |
| 10 | + |
| 11 | +namespace WebDevStudios\CCForWoo\View\Checkout; |
| 12 | + |
| 13 | +use WebDevStudios\OopsWP\Utility\Hookable; |
| 14 | + |
| 15 | +/** |
| 16 | + * Class CampaignId |
| 17 | + * |
| 18 | + * @author Michael Beckwith <[email protected]> |
| 19 | + * @package WebDevStudios\CCForWoo\View\Checkout |
| 20 | + * @since 2019-08-22 |
| 21 | + */ |
| 22 | +class CampaignId implements Hookable { |
| 23 | + /** |
| 24 | + * The name of the meta field for the customer's preference. |
| 25 | + * This constant will be used both in usermeta (for users) and postmeta (for orders). |
| 26 | + * |
| 27 | + * @var string |
| 28 | + * @since 2019-08-22 |
| 29 | + */ |
| 30 | + const CUSTOMER_CAMPAIGN_ID_KEY = 'campaign_activity_id'; |
| 31 | + |
| 32 | + /** |
| 33 | + * Register actions and filters with WordPress. |
| 34 | + * |
| 35 | + * @author Michael Beckwith <[email protected]> |
| 36 | + * @since 2019-08-22 |
| 37 | + */ |
| 38 | + public function register_hooks() { |
| 39 | + add_action( 'init', [ $this, 'save_campaign_id' ], 11 ); |
| 40 | + add_action( 'woocommerce_checkout_update_order_meta', [ $this, 'save_user_campaign_id_to_order' ] ); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Save the user preference to the order meta. |
| 45 | + * |
| 46 | + * @param int $order_id The order ID. |
| 47 | + * |
| 48 | + * @author Michael Beckwith <[email protected]> |
| 49 | + * @since 2019-08-22 |
| 50 | + * @return void |
| 51 | + */ |
| 52 | + public function save_user_campaign_id_to_order( $order_id ) { |
| 53 | + $preference = $this->get_stored_campaign_id(); |
| 54 | + |
| 55 | + if ( empty( $preference ) ) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + add_post_meta( $order_id, self::CUSTOMER_CAMPAIGN_ID_KEY, $preference, true ); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Save the campaign ID for the session. |
| 64 | + * |
| 65 | + * @throws \Exception DateTime exception. |
| 66 | + * |
| 67 | + * @author Michael Beckwith <[email protected]> |
| 68 | + * @since 2019-08-22 |
| 69 | + * @return void |
| 70 | + */ |
| 71 | + public function save_campaign_id() { |
| 72 | + $campaign_id = filter_input( INPUT_GET, 'source', FILTER_SANITIZE_STRING ); |
| 73 | + |
| 74 | + if ( ! empty( $campaign_id ) ) { |
| 75 | + setcookie( 'ctct_woo_campaign_id', $campaign_id, 0, '/' ); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Get the submitted customer newsletter preference. |
| 81 | + * |
| 82 | + * @author Michael Beckwith <[email protected]> |
| 83 | + * @since 2019-08-22 |
| 84 | + * @return string |
| 85 | + */ |
| 86 | + private function get_stored_campaign_id() { |
| 87 | + return isset( $_COOKIE['ctct_woo_campaign_id'] ) |
| 88 | + ? sanitize_text_field( $_COOKIE['ctct_woo_campaign_id'] ) |
| 89 | + : ''; |
| 90 | + } |
| 91 | +} |
0 commit comments