get_settings(); add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_exporter' ) ); add_filter( 'wp_privacy_personal_data_erasers', array( $this, 'register_eraser' ) ); if ( 'yes' !== $s['enabled'] ) { return; } if ( did_action( 'woocommerce_init' ) && function_exists( 'woocommerce_register_additional_checkout_field' ) ) { $this->register_fields( $s ); } elseif ( ! did_action( 'woocommerce_init' ) ) { add_action( 'woocommerce_init', function () use ( $s ) { $this->register_fields( $s ); }, 20 ); } else { add_action( 'woocommerce_blocks_loaded', function () use ( $s ) { $this->register_fields( $s ); } ); } add_filter( 'woocommerce_get_default_value_for_' . self::FIELD_DAY, array( $this, 'default_day' ), 10, 3 ); add_filter( 'woocommerce_get_default_value_for_' . self::FIELD_MONTH, array( $this, 'default_month' ), 10, 3 ); add_filter( 'woocommerce_get_default_value_for_' . self::FIELD_YEAR, array( $this, 'default_year' ), 10, 3 ); add_filter( 'render_block_woocommerce/checkout-additional-information-block', array( $this, 'step_description' ), 10, 1 ); add_action( 'woocommerce_blocks_checkout_block_registration', array( $this, 'register_hint_script' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_sheet' ), 20 ); add_filter( 'render_block_woocommerce/order-confirmation-additional-fields', array( $this, 'confirmation_row' ), 10, 1 ); add_action( 'woocommerce_order_details_after_customer_details', array( $this, 'order_rows_start' ), 9, 1 ); add_action( 'woocommerce_order_details_after_customer_details', array( $this, 'order_rows_end' ), 11, 1 ); add_action( 'woocommerce_email', array( $this, 'wrap_email_fields' ), 10, 1 ); if ( did_action( 'woocommerce_email' ) && function_exists( 'WC' ) ) { $this->wrap_email_fields( WC()->mailer() ); } add_action( 'woocommerce_store_api_checkout_update_order_meta', array( $this, 'sync_to_user_meta' ) ); add_action( 'woocommerce_store_api_checkout_order_processed', array( $this, 'sync_to_user_meta' ) ); add_action( 'woocommerce_edit_account_form', array( $this, 'render_account_field' ) ); add_filter( 'woocommerce_checkout_fields', array( $this, 'checkout_fields' ), 20 ); add_filter( 'woocommerce_form_field_' . self::CLASSIC_TYPE, array( $this, 'checkout_field_html' ), 10, 4 ); add_action( 'woocommerce_after_checkout_validation', array( $this, 'validate_checkout_field' ), 10, 2 ); add_action( 'woocommerce_checkout_create_order', array( $this, 'save_checkout_field' ), 10, 1 ); add_action( 'woocommerce_checkout_order_processed', array( $this, 'sync_classic_order' ), 10, 3 ); add_action( 'woocommerce_save_account_details_errors', array( $this, 'save_account_field' ), 10, 2 ); add_action( 'template_redirect', array( $this, 'suppress_aw_birthdays_account_field' ), 1 ); } const DOB_META_KEYS = array( 'aieo_birthday_md', 'aieo_birthday_full', '_automatewoo_birthday_md', '_automatewoo_birthday_full' ); public function register_exporter( $exporters ) { $exporters['aieo-birthday'] = array( 'exporter_friendly_name' => __( 'Rooster One — Birthday', 'ai-eshop-optimizer' ), 'callback' => array( $this, 'export_birthday' ), ); return $exporters; } public function register_eraser( $erasers ) { $erasers['aieo-birthday'] = array( 'eraser_friendly_name' => __( 'Rooster One — Birthday', 'ai-eshop-optimizer' ), 'callback' => array( $this, 'erase_birthday' ), ); return $erasers; } public function export_birthday( $email, $page = 1 ) { $data = array(); $user = get_user_by( 'email', $email ); if ( $user ) { $md = (string) get_user_meta( $user->ID, 'aieo_birthday_md', true ); if ( '' === $md ) { $md = (string) get_user_meta( $user->ID, '_automatewoo_birthday_md', true ); } $full = (string) get_user_meta( $user->ID, 'aieo_birthday_full', true ); if ( '' === $full ) { $full = (string) get_user_meta( $user->ID, '_automatewoo_birthday_full', true ); } $items = array(); if ( '' !== $md ) { $items[] = array( 'name' => __( 'Birthday (month-day, MM-DD)', 'ai-eshop-optimizer' ), 'value' => $md ); } if ( '' !== $full ) { $items[] = array( 'name' => __( 'Date of birth', 'ai-eshop-optimizer' ), 'value' => $full ); } if ( $items ) { $data[] = array( 'group_id' => 'aieo-birthday', 'group_label' => __( 'Birthday', 'ai-eshop-optimizer' ), 'item_id' => 'aieo-birthday-' . (int) $user->ID, 'data' => $items, ); } } return array( 'data' => $data, 'done' => true ); } public function erase_birthday( $email, $page = 1 ) { $removed = false; $user = get_user_by( 'email', $email ); if ( $user ) { foreach ( self::DOB_META_KEYS as $k ) { if ( '' !== (string) get_user_meta( $user->ID, $k, true ) ) { delete_user_meta( $user->ID, $k ); $removed = true; } } } return array( 'items_removed' => $removed, 'items_retained' => false, 'messages' => $removed ? array( __( 'Birthday removed. The customer may set a new birthday from My Account.', 'ai-eshop-optimizer' ) ) : array(), 'done' => true, ); } private function register_fields( $s ) { if ( ! self::asks() ) { if ( self::takes_the_form() ) { return; } if ( self::is_page_view() && ! did_action( 'wp' ) ) { add_action( 'wp', function () use ( $s ) { if ( ! self::draws_the_form() ) { $this->add_fields( $s ); } }, 0 ); return; } if ( self::draws_the_form() ) { return; } } $this->add_fields( $s ); } private function add_fields( $s ) { try { woocommerce_register_additional_checkout_field( array( 'id' => self::FIELD_DAY, 'label' => __( 'Day', 'ai-eshop-optimizer' ), 'optionalLabel' => __( 'Day', 'ai-eshop-optimizer' ), 'placeholder' => __( 'Day', 'ai-eshop-optimizer' ), 'location' => 'order', 'type' => 'select', 'required' => false, 'options' => self::day_options(), 'attributes' => $this->field_attributes( 'day' ), ) ); woocommerce_register_additional_checkout_field( array( 'id' => self::FIELD_MONTH, 'label' => __( 'Month', 'ai-eshop-optimizer' ), 'optionalLabel' => __( 'Month', 'ai-eshop-optimizer' ), 'placeholder' => __( 'Month', 'ai-eshop-optimizer' ), 'location' => 'order', 'type' => 'select', 'required' => false, 'options' => self::month_options(), 'attributes' => $this->field_attributes( 'month' ), ) ); woocommerce_register_additional_checkout_field( array( 'id' => self::FIELD_YEAR, 'label' => __( 'Year', 'ai-eshop-optimizer' ), 'optionalLabel' => __( 'Year', 'ai-eshop-optimizer' ), 'placeholder' => __( 'Year', 'ai-eshop-optimizer' ), 'location' => 'order', 'type' => 'select', 'required' => false, 'options' => self::year_options(), 'attributes' => $this->field_attributes( 'year' ), ) ); } catch ( \Throwable $e ) { unset( $e ); } } public function field_attributes( $part ) { $attributes = array( 'data-aieo-bday' => $part, 'autocomplete' => 'bday-' . $part, ); if ( '' !== $this->hint() ) { $attributes['aria-describedby'] = self::HINT_ID; } return $attributes; } private static function day_options() { $o = array(); for ( $d = 1; $d <= 31; $d++ ) { $o[] = array( 'value' => (string) $d, 'label' => (string) $d ); } return $o; } private static function month_options() { $loc = ( isset( $GLOBALS['wp_locale'] ) && is_object( $GLOBALS['wp_locale'] ) ) ? $GLOBALS['wp_locale'] : null; $o = array(); for ( $m = 1; $m <= 12; $m++ ) { $o[] = array( 'value' => (string) $m, 'label' => $loc ? (string) $loc->get_month( $m ) : (string) $m ); } return $o; } private static function year_options() { $o = array(); $year = (int) gmdate( 'Y' ); for ( $y = $year; $y >= $year - 100; $y-- ) { $o[] = array( 'value' => (string) $y, 'label' => (string) $y ); } return $o; } public function default_day( $value = '' ) { return $this->prefill_part( 'day', $value ); } public function default_month( $value = '' ) { return $this->prefill_part( 'month', $value ); } public function default_year( $value = '' ) { return $this->prefill_part( 'year', $value ); } private function prefill_part( $part, $value = '' ) { $uid = get_current_user_id(); if ( $uid <= 0 ) { return $value; } $full = (string) get_user_meta( $uid, 'aieo_birthday_full', true ); if ( '' === $full ) { $full = (string) get_user_meta( $uid, '_automatewoo_birthday_full', true ); } $md = (string) get_user_meta( $uid, 'aieo_birthday_md', true ); if ( '' === $md ) { $md = (string) get_user_meta( $uid, '_automatewoo_birthday_md', true ); } $y = 0; $m = 0; $d = 0; if ( '' !== $full && preg_match( '#^(\d{4})-(\d{2})-(\d{2})$#', $full, $mm ) ) { $y = (int) $mm[1]; $m = (int) $mm[2]; $d = (int) $mm[3]; } elseif ( '' !== $md && preg_match( '#^(\d{2})-(\d{2})$#', $md, $mm ) ) { $m = (int) $mm[1]; $d = (int) $mm[2]; } if ( 'day' === $part ) { return $d > 0 ? (string) $d : $value; } if ( 'month' === $part ) { return $m > 0 ? (string) $m : $value; } return $y > 0 ? (string) $y : $value; } const SHEET = 'aieo-birthday'; public function hint() { $s = $this->get_settings(); return 'yes' === $s['enabled'] ? trim( wp_strip_all_tags( (string) $s['message'] ) ) : ''; } const HINT_ID = 'aieo-birthday-hint'; const HINT_BLOCK = 'aieo/birthday-hint'; const HINT_SCRIPT = 'aieo-birthday-hint'; public static function asks() { return ! self::known( (int) get_current_user_id() ); } public static function takes_the_form() { return function_exists( 'WC' ) && WC() && method_exists( WC(), 'is_store_api_request' ) && WC()->is_store_api_request(); } private static function is_page_view() { if ( is_admin() || wp_doing_cron() || wp_doing_ajax() || ( defined( 'WP_CLI' ) && WP_CLI ) || ! function_exists( 'WC' ) || ! WC() ) { return false; } if ( method_exists( WC(), 'is_rest_api_request' ) && WC()->is_rest_api_request() ) { return false; } return ! isset( $_GET['wc-ajax'] ) && ! isset( $_GET['wc-api'] ); } public static function draws_the_form() { if ( ! did_action( 'wp' ) || ! is_page() ) { return false; } if ( isset( $_GET['order-received'] ) || isset( $_GET['order-pay'] ) || ( function_exists( 'is_wc_endpoint_url' ) && ( is_wc_endpoint_url( 'order-received' ) || is_wc_endpoint_url( 'order-pay' ) ) ) ) { return false; } $page = function_exists( 'wc_get_page_id' ) ? (int) wc_get_page_id( 'checkout' ) : 0; if ( $page > 0 && is_page( $page ) ) { return true; } $post = get_queried_object(); return $post instanceof WP_Post && has_block( 'woocommerce/checkout', $post ); } public static function year_in_range( $year ) { $now = (int) gmdate( 'Y' ); return $year >= $now - 100 && $year <= $now; } public static function known( $user_id ) { if ( $user_id <= 0 ) { return false; } return '' !== (string) get_user_meta( $user_id, 'aieo_birthday_md', true ) || '' !== (string) get_user_meta( $user_id, '_automatewoo_birthday_md', true ); } public function group_name() { $label = trim( (string) $this->get_settings()['label'] ); return ( '' !== $label ? $label : __( 'Birthday', 'ai-eshop-optimizer' ) ) . ' ' . __( '(optional)', 'ai-eshop-optimizer' ); } public static function step_is_shared() { if ( ! class_exists( '\Automattic\WooCommerce\Blocks\Package' ) || ! class_exists( '\Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields' ) ) { return false; } try { $fields = \Automattic\WooCommerce\Blocks\Package::container()->get( \Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields::class )->get_fields_for_location( 'order' ); } catch ( \Throwable $e ) { return false; } return (bool) array_diff( array_keys( (array) $fields ), array( self::FIELD_DAY, self::FIELD_MONTH, self::FIELD_YEAR ) ); } public function step_description( $html ) { if ( 'yes' !== $this->get_settings()['enabled'] || ! self::asks() || ! is_string( $html ) || ! class_exists( 'WP_HTML_Tag_Processor' ) ) { return $html; } $tags = new WP_HTML_Tag_Processor( $html ); if ( ! $tags->next_tag() || 'woocommerce/checkout-additional-information-block' !== $tags->get_attribute( 'data-block-name' ) ) { return $html; } if ( '' === trim( (string) $tags->get_attribute( 'data-description' ) ) && ! self::step_is_shared() ) { $tags->set_attribute( 'data-description', $this->group_name() ); $html = $tags->get_updated_html(); } $hint = $this->hint(); $end = strrpos( $html, '' ); if ( '' === $hint || false === $end ) { return $html; } $hold = new WP_HTML_Tag_Processor( '
' ); $hold->next_tag(); $hold->set_attribute( 'data-block-name', self::HINT_BLOCK ); $hold->set_attribute( 'data-hint', $hint ); $hold->set_attribute( 'class', 'wp-block-aieo-birthday-hint' ); return substr( $html, 0, $end ) . $hold->get_updated_html() . substr( $html, $end ); } public function register_hint_script( $registry ) { if ( ! is_object( $registry ) || ! method_exists( $registry, 'register' ) || ! interface_exists( '\Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface' ) ) { return; } if ( 'yes' !== $this->get_settings()['enabled'] || ! self::asks() || '' === $this->hint() ) { return; } $src = plugins_url( 'assets/js/aieo-birthday-hint.js', __DIR__ ); $registry->register( new class( $src ) implements \Automattic\WooCommerce\Blocks\Integrations\IntegrationInterface { private $src; public function __construct( $src ) { $this->src = (string) $src; } public function get_name() { return 'aieo-birthday-hint'; } public function initialize() { $file = dirname( __DIR__ ) . '/assets/js/aieo-birthday-hint.js'; wp_register_script( AIEO_Runtime_Birthday::HINT_SCRIPT, $this->src, array( 'wc-blocks-checkout', 'wp-element' ), is_readable( $file ) ? (string) filemtime( $file ) : false, true ); } public function get_script_handles() { return array( AIEO_Runtime_Birthday::HINT_SCRIPT ); } public function get_editor_script_handles() { return array(); } public function get_script_data() { return array(); } } ); } public function enqueue_sheet() { $on_checkout = function_exists( 'is_checkout' ) && is_checkout() && ! ( function_exists( 'is_wc_endpoint_url' ) && is_wc_endpoint_url( 'order-received' ) ) && self::asks(); $on_account = function_exists( 'is_wc_endpoint_url' ) && is_wc_endpoint_url( 'edit-account' ); if ( ( ! $on_checkout && ! $on_account ) || 'yes' !== $this->get_settings()['enabled'] ) { return; } wp_register_style( self::SHEET, false, array(), '1' ); wp_enqueue_style( self::SHEET ); wp_add_inline_style( self::SHEET, self::sheet( $on_checkout && ! self::step_is_shared() ) ); } public static function sheet( $named = true ) { $grid = 'display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1.6fr) minmax(0,1.1fr);gap:var(--wp--preset--spacing--2,8px)'; $phone = 'grid-template-columns:minmax(0,1fr) minmax(0,1.4fr) minmax(0,1fr)'; $hint = 'margin:0;font-size:var(--wp--preset--font-size--xs,.75rem);color:var(--wp--preset--color--muted,inherit)'; $hidden = 'position:absolute;width:1px;height:1px;overflow:hidden;clip-path:inset(50%);white-space:nowrap'; $gap = 'var(--wp--preset--spacing--2,8px)'; $share = static function ( $part, $whole ) use ( $gap ) { return 'width:calc((100% - 2 * ' . $gap . ') * ' . $part . ' / ' . $whole . ' - .1px)'; }; $step = '.wp-block-woocommerce-checkout-additional-information-block'; $field = $step . ' .wc-block-components-select-input-aieo-birthday_'; $three = $field . 'day,' . $field . 'month,' . $field . 'year'; return ( $named ? $step . ' .wc-block-components-checkout-step__description{font-size:var(--wp--preset--font-size--sm,.875rem);font-weight:600;color:inherit}' . $step . ' .wc-block-components-address-form{margin-bottom:' . $gap . '}' : '' ) . $three . '{display:inline-block;vertical-align:top;box-sizing:border-box;min-width:0}' . $field . 'day{' . $share( 1, 3.7 ) . '}' . $field . 'month{' . $share( 1.6, 3.7 ) . ';margin-inline-start:' . $gap . '}' . $field . 'year{' . $share( 1.1, 3.7 ) . ';margin-inline-start:' . $gap . '}' . $field . 'day .wc-blocks-components-select__label,' . $field . 'month .wc-blocks-components-select__label,' . $field . 'year .wc-blocks-components-select__label{' . $hidden . '}' . $step . ' .aieo-birthday-hint{' . $hint . '}' . '.aieo-account-birthday{display:grid;gap:var(--wp--preset--spacing--2,8px);min-width:0;margin:0;padding:0;border:0}' . '.aieo-account-birthday__legend{float:left;width:100%;margin:0 0 var(--wp--preset--spacing--3,12px);padding:0;font-size:var(--wp--preset--font-size--sm,.875rem);font-weight:600}' . '.aieo-account-birthday__legend small{font-weight:400;color:var(--wp--preset--color--muted,inherit)}' . '.aieo-account-birthday__legend+*{clear:both}' . '.aieo-account-birthday__fields{' . $grid . '}' . '.aieo-account-birthday__field{min-width:0}' . '.aieo-account-birthday__field>select{width:100%;min-width:0}' . '.aieo-account-birthday__hint{' . $hint . '}' . '@media (max-width:767px){' . $field . 'day{' . $share( 1, 3.4 ) . '}' . $field . 'month{' . $share( 1.4, 3.4 ) . '}' . $field . 'year{' . $share( 1, 3.4 ) . '}.aieo-account-birthday__fields{' . $phone . '}}'; } public static function order_birthday( $order ) { if ( ! $order instanceof WC_Order ) { return ''; } $day = (int) $order->get_meta( self::META_DAY, true ); $month = (int) $order->get_meta( self::META_MONTH, true ); $year = (int) $order->get_meta( self::META_YEAR, true ); if ( $day < 1 || $month < 1 || ! checkdate( $month, $day, $year > 0 ? $year : 2000 ) ) { return ''; } $at = gmmktime( 12, 0, 0, $month, $day, $year > 0 ? $year : 2000 ); return $year > 0 ? wp_date( __( 'j F Y', 'ai-eshop-optimizer' ), $at, new DateTimeZone( 'UTC' ) ) : wp_date( __( 'j F', 'ai-eshop-optimizer' ), $at, new DateTimeZone( 'UTC' ) ); } public function one_row( $html, $order, $form = 'list' ) { $words = self::order_birthday( $order ); if ( '' === $words || ! is_string( $html ) ) { return $html; } $rows = array(); foreach ( array( __( 'Day', 'ai-eshop-optimizer' ), __( 'Month', 'ai-eshop-optimizer' ), __( 'Year', 'ai-eshop-optimizer' ) ) as $label ) { $name = preg_quote( esc_html( $label ), '#' ); if ( 'email' === $form ) { $rows[] = '