countries ) { $calling = WC()->countries->get_country_calling_code( $country_code ); $calling = is_array( $calling ) ? ( 1 === count( $calling ) ? reset( $calling ) : '' ) : (string) $calling; $calling = preg_replace( '/\D+/', '', $calling ); if ( '' !== $calling ) { return $calling . substr( $digits, 1 ); } } return $digits; } /** * The configured pilot phone as digits, '' when the filter is off. * * @return string */ function orderping_pilot_phone_digits() { return orderping_normalize_phone( (string) get_option( ORDERPING_OPTION_PILOT_PHONE, '' ) ); } /** * Whether the plugin may act on this order. Single gate used by every emitter * (capture, updates, permission field storage). * * @param WC_Order|mixed $order Order. * @return bool */ function orderping_order_in_scope( $order ) { if ( ! $order instanceof WC_Order ) { return false; } $pilot = orderping_pilot_phone_digits(); if ( '' === $pilot ) { return true; // Filter off: every order. } $phone = orderping_normalize_phone( $order->get_billing_phone(), (string) $order->get_billing_country() ); return '' !== $phone && hash_equals( $pilot, $phone ); } /** * Whether the checkout field should render for the shopper right now. With a * pilot filter set, only when the phone typed so far (WooCommerce keeps the * session customer in step with the form) matches it. * * @return bool */ function orderping_checkout_in_scope() { $pilot = orderping_pilot_phone_digits(); if ( '' === $pilot ) { return true; } if ( ! function_exists( 'WC' ) || ! WC()->customer ) { return false; } $phone = orderping_normalize_phone( WC()->customer->get_billing_phone(), (string) WC()->customer->get_billing_country() ); return '' !== $phone && hash_equals( $pilot, $phone ); } /** * Settings handler: saves the pilot phone. Admin only, nonce checked. */ function orderping_handle_save_pilot() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'You do not have permission to do this.', 'commerceping' ), 403 ); } check_admin_referer( 'orderping_save_pilot' ); $raw = isset( $_POST['orderping_pilot_phone'] ) ? sanitize_text_field( wp_unslash( $_POST['orderping_pilot_phone'] ) ) : ''; $digits = preg_replace( '/\D+/', '', $raw ); if ( '' === $digits ) { delete_option( ORDERPING_OPTION_PILOT_PHONE ); orderping_redirect_to_settings( 'pilot_cleared' ); } if ( 0 === strpos( $digits, '00' ) ) { $digits = substr( $digits, 2 ); // 0041... is +41... } // E.164: country code first (never a leading 0), at most 15 digits. if ( 0 === strpos( $digits, '0' ) || strlen( $digits ) < 8 || strlen( $digits ) > 15 ) { orderping_redirect_to_settings( 'pilot_invalid' ); } update_option( ORDERPING_OPTION_PILOT_PHONE, '+' . $digits, false ); orderping_redirect_to_settings( 'pilot_saved' ); }