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[] = '
  • ' . $name . ': [^<]*
  • '; } elseif ( 'plain' === $form ) { $rows[] = '(?m:^' . preg_quote( $label, '#' ) . ': [^\n]*\n)'; } else { $rows[] = '
    ' . $name . '
    [^<]*
    '; } } $first = null; $html = (string) preg_replace_callback( '#' . implode( '|', $rows ) . '#', static function ( $m ) use ( &$first ) { if ( null === $first ) { $first = true; return "\0"; } return ''; }, $html ); if ( null === $first ) { return $html; } $label = trim( (string) $this->get_settings()['label'] ); $label = '' !== $label ? $label : __( 'Birthday', 'ai-eshop-optimizer' ); if ( 'email' === $form ) { $row = '
  • ' . esc_html( $label ) . ': ' . esc_html( $words ) . '
  • '; } elseif ( 'plain' === $form ) { $row = $label . ': ' . $words . "\n"; } else { $row = '
    ' . esc_html( $label ) . '
    ' . esc_html( $words ) . '
    '; } return str_replace( "\0", $row, $html ); } public function confirmation_row( $html ) { if ( ! is_string( $html ) || false === strpos( $html, '
    ' ) ) { return $html; } $order = function_exists( 'wc_get_order' ) ? wc_get_order( absint( get_query_var( 'order-received' ) ) ) : null; return $order instanceof WC_Order ? $this->one_row( $html, $order ) : $html; } public function order_rows_start( $order = null ) { ob_start(); } public function order_rows_end( $order = null ) { $html = (string) ob_get_clean(); echo $this->one_row( $html, $order ); } private $mailer = null; public function wrap_email_fields( $mailer ) { if ( ! is_object( $mailer ) || ! method_exists( $mailer, 'additional_checkout_fields' ) ) { return; } if ( 30 !== has_action( 'woocommerce_email_customer_details', array( $mailer, 'additional_checkout_fields' ) ) ) { return; } remove_action( 'woocommerce_email_customer_details', array( $mailer, 'additional_checkout_fields' ), 30 ); $this->mailer = $mailer; add_action( 'woocommerce_email_customer_details', array( $this, 'email_fields' ), 30, 3 ); } public function email_fields( $order = null, $sent_to_admin = false, $plain_text = false ) { if ( ! is_object( $this->mailer ) ) { return; } $level = ob_get_level(); ob_start(); try { $this->mailer->additional_checkout_fields( $order, $sent_to_admin, $plain_text ); } catch ( \Throwable $e ) { while ( ob_get_level() > $level ) { ob_end_flush(); } throw $e; } while ( ob_get_level() > $level + 1 ) { ob_end_flush(); } $html = (string) ob_get_clean(); echo $this->one_row( $html, $order, $plain_text ? 'plain' : 'email' ); } public function sync_to_user_meta( $order ) { if ( ! is_a( $order, 'WC_Order' ) ) { return; } $day = (int) $order->get_meta( self::META_DAY ); $month = (int) $order->get_meta( self::META_MONTH ); $year = (int) $order->get_meta( self::META_YEAR ); if ( $day > 0 || $month > 0 ) { $md = self::mk_md( $month, $day ); $full = ( '' !== $md && $year > 0 && checkdate( $month, $day, $year ) ) ? sprintf( '%04d-%s', $year, $md ) : ''; $raw = ( '' !== $md ) ? ( $year > 0 ? sprintf( '%02d/%02d/%04d', $day, $month, $year ) : sprintf( '%02d/%02d', $day, $month ) ) : ''; } else { $raw = trim( (string) $order->get_meta( self::META_KEY ) ); $md = self::parse_md( $raw ); $full = self::parse_full( $raw ); } if ( '' === $md ) { return; } $order->update_meta_data( '_aieo_birthday_raw', $raw ); $order->update_meta_data( '_aieo_birthday_md', $md ); $order->save(); $user_id = (int) $order->get_customer_id(); if ( $user_id > 0 && '' !== $md ) { $existing = (string) get_user_meta( $user_id, 'aieo_birthday_md', true ); if ( '' === $existing ) { $existing = (string) get_user_meta( $user_id, '_automatewoo_birthday_md', true ); } if ( '' !== $existing ) { return; } update_user_meta( $user_id, 'aieo_birthday_md', $md ); update_user_meta( $user_id, '_automatewoo_birthday_md', $md ); if ( '' !== $full ) { update_user_meta( $user_id, 'aieo_birthday_full', $full ); update_user_meta( $user_id, '_automatewoo_birthday_full', $full ); } else { $existing = (string) get_user_meta( $user_id, 'aieo_birthday_full', true ); if ( '' !== $existing && substr( $existing, 5 ) !== $md ) { delete_user_meta( $user_id, 'aieo_birthday_full' ); delete_user_meta( $user_id, '_automatewoo_birthday_full' ); } } } } public function render_account_field() { $user_id = get_current_user_id(); if ( $user_id <= 0 ) { return; } $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 ); } $locked = ( '' !== $md ); $chosen = array( 'day' => 0, 'month' => 0, 'year' => 0 ); if ( '' !== $full && preg_match( '#^(\d{4})-(\d{2})-(\d{2})$#', $full, $mm ) ) { $chosen = array( 'day' => (int) $mm[3], 'month' => (int) $mm[2], 'year' => (int) $mm[1] ); } elseif ( '' !== $md && preg_match( '#^(\d{2})-(\d{2})$#', $md, $mm ) ) { $chosen = array( 'day' => (int) $mm[2], 'month' => (int) $mm[1], 'year' => 0 ); } $s = $this->get_settings(); $msg = isset( $s['message'] ) ? trim( (string) $s['message'] ) : ''; $hint = $locked ? esc_html__( 'Your birthday can’t be changed once set.', 'ai-eshop-optimizer' ) : ( '' !== $msg ? wp_kses( $msg, array( 'strong' => array(), 'em' => array(), 'br' => array() ) ) . ' ' : '' ) . esc_html__( 'It can’t be changed once saved.', 'ai-eshop-optimizer' ); echo $this->group_html( 'woocommerce-form-row form-row form-row-wide', $chosen, $locked, $hint ); } const CLASSIC_TYPE = 'aieo_birthday'; public function checkout_fields( $fields ) { if ( ! is_array( $fields ) || 'yes' !== $this->get_settings()['enabled'] || ! self::asks() ) { return $fields; } $fields['order'] = array( self::CLASSIC_TYPE => array( 'type' => self::CLASSIC_TYPE, 'required' => false, 'priority' => 5, ), ) + ( isset( $fields['order'] ) && is_array( $fields['order'] ) ? $fields['order'] : array() ); return $fields; } public function checkout_field_html( $field = '', $key = '', $args = array(), $value = null ) { if ( 'yes' !== $this->get_settings()['enabled'] || ! self::asks() ) { return ''; } $chosen = array(); foreach ( array( 'day', 'month', 'year' ) as $part ) { $chosen[ $part ] = isset( $_POST[ 'aieo_birthday_' . $part ] ) ? absint( wp_unslash( $_POST[ 'aieo_birthday_' . $part ] ) ) : 0; } return $this->group_html( 'form-row form-row-wide', $chosen, false, esc_html( $this->hint() ) ); } private function group_html( $row_class, array $chosen, $locked, $hint ) { $s = $this->get_settings(); $label = ( '' !== trim( (string) $s['label'] ) ) ? (string) $s['label'] : __( 'Birthday', 'ai-eshop-optimizer' ); $dis = $locked ? ' disabled' : ''; $loc = ( isset( $GLOBALS['wp_locale'] ) && is_object( $GLOBALS['wp_locale'] ) ) ? $GLOBALS['wp_locale'] : null; $html = '
    '; $html .= ''; $html .= ''; if ( '' !== $hint ) { $html .= ''; } return $html . '
    '; } private static function posted_birthday() { $out = array(); foreach ( array( 'day', 'month', 'year' ) as $part ) { $out[ $part ] = isset( $_POST[ 'aieo_birthday_' . $part ] ) ? absint( wp_unslash( $_POST[ 'aieo_birthday_' . $part ] ) ) : 0; } return $out; } public function validate_checkout_field( $data, $errors ) { if ( 'yes' !== $this->get_settings()['enabled'] || ! self::asks() || ! $errors instanceof WP_Error ) { return; } $b = self::posted_birthday(); if ( 0 === $b['day'] && 0 === $b['month'] && 0 === $b['year'] ) { return; } if ( '' === self::mk_md( $b['month'], $b['day'] ) || ( $b['year'] > 0 && ( ! self::year_in_range( $b['year'] ) || ! checkdate( $b['month'], $b['day'], $b['year'] ) ) ) ) { $errors->add( 'aieo_birthday_invalid', __( 'Please choose a valid birthday (day and month).', 'ai-eshop-optimizer' ), array( 'id' => 'aieo-bday-day' ) ); } } public function save_checkout_field( $order ) { if ( ! $order instanceof WC_Order || 'yes' !== $this->get_settings()['enabled'] || ! self::asks() ) { return; } $b = self::posted_birthday(); if ( '' === self::mk_md( $b['month'], $b['day'] ) || ( $b['year'] > 0 && ! self::year_in_range( $b['year'] ) ) ) { return; } $order->update_meta_data( self::META_DAY, (string) $b['day'] ); $order->update_meta_data( self::META_MONTH, (string) $b['month'] ); if ( $b['year'] > 0 ) { $order->update_meta_data( self::META_YEAR, (string) $b['year'] ); } } public function sync_classic_order( $order_id, $posted = array(), $order = null ) { $order = $order instanceof WC_Order ? $order : wc_get_order( $order_id ); if ( $order instanceof WC_Order && '' !== (string) $order->get_meta( self::META_DAY ) ) { $this->sync_to_user_meta( $order ); } } public function save_account_field( $errors, $user ) { if ( ! is_a( $user, 'WP_User' ) || (int) $user->ID <= 0 ) { return; } if ( ( is_a( $errors, 'WP_Error' ) && $errors->get_error_messages() ) || 0 !== wc_notice_count( 'error' ) ) { return; } $user_id = (int) $user->ID; $existing = (string) get_user_meta( $user_id, 'aieo_birthday_md', true ); if ( '' === $existing ) { $existing = (string) get_user_meta( $user_id, '_automatewoo_birthday_md', true ); } if ( '' !== $existing ) { return; } $day = isset( $_POST['aieo_birthday_day'] ) ? absint( wp_unslash( $_POST['aieo_birthday_day'] ) ) : 0; $month = isset( $_POST['aieo_birthday_month'] ) ? absint( wp_unslash( $_POST['aieo_birthday_month'] ) ) : 0; $year = isset( $_POST['aieo_birthday_year'] ) ? absint( wp_unslash( $_POST['aieo_birthday_year'] ) ) : 0; if ( 0 === $day && 0 === $month && 0 === $year ) { return; } $md = self::mk_md( $month, $day ); if ( '' === $md ) { if ( is_a( $errors, 'WP_Error' ) ) { $errors->add( 'aieo_birthday_invalid', __( 'Please choose a valid birthday (day and month).', 'ai-eshop-optimizer' ) ); } return; } if ( $year > 0 && ! self::year_in_range( $year ) ) { if ( is_a( $errors, 'WP_Error' ) ) { $errors->add( 'aieo_birthday_invalid', __( 'Please choose a valid birthday (day and month).', 'ai-eshop-optimizer' ) ); } return; } update_user_meta( $user_id, 'aieo_birthday_md', $md ); update_user_meta( $user_id, '_automatewoo_birthday_md', $md ); if ( $year > 0 ) { list( $mo, $da ) = explode( '-', $md ); if ( checkdate( (int) $mo, (int) $da, $year ) ) { $full = sprintf( '%04d-%s', $year, $md ); update_user_meta( $user_id, 'aieo_birthday_full', $full ); update_user_meta( $user_id, '_automatewoo_birthday_full', $full ); } } } public function suppress_aw_birthdays_account_field() { if ( ! class_exists( 'AutomateWoo\\Birthdays\\Frontend' ) ) { return; } remove_action( 'woocommerce_edit_account_form', array( 'AutomateWoo\\Birthdays\\Frontend', 'add_birthday_field_to_edit_account_form' ) ); remove_action( 'woocommerce_save_account_details_errors', array( 'AutomateWoo\\Birthdays\\Frontend', 'save_account_details_form' ) ); } public static function parse_md( $raw ) { $raw = trim( (string) $raw ); if ( preg_match( '#^(\d{4})[/.\-](\d{1,2})[/.\-](\d{1,2})$#', $raw, $m ) ) { return self::mk_md( (int) $m[2], (int) $m[3] ); } if ( preg_match( '#^(\d{1,2})[/.\-](\d{1,2})[/.\-]\d{2,4}$#', $raw, $m ) ) { return self::resolve_dm( (int) $m[1], (int) $m[2] ); } if ( preg_match( '#^(\d{1,2})[/.\-](\d{1,2})$#', $raw, $m ) ) { return self::resolve_dm( (int) $m[1], (int) $m[2] ); } return ''; } public static function parse_full( $raw ) { $raw = trim( (string) $raw ); if ( preg_match( '#^(\d{4})[/.\-](\d{1,2})[/.\-](\d{1,2})$#', $raw, $m ) ) { return checkdate( (int) $m[2], (int) $m[3], (int) $m[1] ) ? sprintf( '%04d-%02d-%02d', $m[1], $m[2], $m[3] ) : ''; } if ( preg_match( '#^(\d{1,2})[/.\-](\d{1,2})[/.\-](\d{4})$#', $raw, $m ) ) { $md = self::resolve_dm( (int) $m[1], (int) $m[2] ); if ( '' !== $md ) { list( $mo, $da ) = explode( '-', $md ); if ( checkdate( (int) $mo, (int) $da, (int) $m[3] ) ) { return sprintf( '%04d-%s', $m[3], $md ); } } } return ''; } private static function resolve_dm( $a, $b ) { if ( $a > 12 && $b <= 12 ) { return self::mk_md( $b, $a ); } if ( $b > 12 && $a <= 12 ) { return self::mk_md( $a, $b ); } return self::mk_md( $b, $a ); } private static function mk_md( $month, $day ) { $month = (int) $month; $day = (int) $day; if ( ! checkdate( $month, $day, 2024 ) ) { return ''; } return sprintf( '%02d-%02d', $month, $day ); } private function get_settings() { $stored = get_option( self::OPTION_KEY, array() ); if ( ! is_array( $stored ) ) { $stored = array(); } return self::fill_i18n_defaults( array_merge( array( 'enabled' => 'no', 'label' => '', 'message' => '', ), $stored ) ); } private static function fill_i18n_defaults( array $s ) { if ( ! did_action( 'init' ) ) { return $s; } $i18n = array( 'label' => __( 'Birthday', 'ai-eshop-optimizer' ), 'message' => __( 'Fill in your birthday to receive your birthday gift from the store.', 'ai-eshop-optimizer' ), ); foreach ( $i18n as $k => $v ) { if ( ! isset( $s[ $k ] ) || '' === trim( (string) $s[ $k ] ) ) { $s[ $k ] = $v; } } return $s; } } }