__( 'دسترسی ندارید.', 'rahkar-toolkit' ) ], 403 ); } check_ajax_referer( 'rahkar_test_notification' ); $recipient = isset( $_POST['recipient'] ) ? sanitize_text_field( wp_unslash( $_POST['recipient'] ) ) : ''; if ( '' === $recipient ) { wp_send_json_error( [ 'message' => __( 'شماره موبایل یا نشانی ایمیل را وارد کنید.', 'rahkar-toolkit' ) ] ); } $isEmail = str_contains( $recipient, '@' ); if ( ! $isEmail ) { $phone = PhoneNumber::parse( $recipient ); if ( null === $phone ) { wp_send_json_error( [ 'message' => __( 'شماره موبایل معتبر نیست. نمونه درست: ۰۹۱۲۳۴۵۶۷۸۹', 'rahkar-toolkit' ) ] ); } $recipient = $phone->canonical(); } elseif ( ! is_email( $recipient ) ) { wp_send_json_error( [ 'message' => __( 'نشانی ایمیل معتبر نیست.', 'rahkar-toolkit' ) ] ); } $code = (string) wp_rand( 10000, 99999 ); // A phone number is tested through the login path itself: the same // gateways in the same order, each with the login pattern configured for // it. A test that passes then means logins will work — not merely that the // account has credit. if ( ! $isEmail ) { $outcome = Plugin::make( OtpDispatcher::class )->send( $phone, $code ); if ( $outcome->isDelivered() ) { $name = DriverRegistry::choices( DriverRegistry::CHANNEL_SMS )[ $outcome->driverId() ] ?? $outcome->driverId(); wp_send_json_success( [ 'message' => sprintf( /* translators: 1: gateway name, 2: sample code */ __( 'ارسال شد از طریق %1$s. کد نمونه: %2$s', 'rahkar-toolkit' ), $name, $code ), ] ); } wp_send_json_error( [ 'message' => $outcome->hasNoGateway() ? __( 'هیچ درگاه پیامکی فعال و تنظیم نشده است. از صفحه «درگاه‌های پیامک» یکی را تنظیم کنید.', 'rahkar-toolkit' ) : sprintf( /* translators: %s: provider message */ __( 'ارسال نشد: %s', 'rahkar-toolkit' ), $outcome->message() ), ] ); } $message = new Message( // Sent as the login event on purpose: it exercises the same pattern // code and the same sender line a real login would, so a test that // passes means logins will work — not merely that the account has // credit. 'otp.login', $recipient, sprintf( /* translators: 1: site name, 2: test code */ __( '%1$s — پیام آزمایشی راهکار. کد نمونه: %2$s', 'rahkar-toolkit' ), get_bloginfo( 'name' ), $code ), [ 'code' => $code ], __( 'پیام آزمایشی راهکار', 'rahkar-toolkit' ), true ); $result = Plugin::make( Notifier::class )->sendNow( $message ); if ( $result->ok ) { wp_send_json_success( [ 'message' => '' !== $result->messageId ? sprintf( /* translators: %s: provider message id */ __( 'ارسال شد. شناسه پیام نزد سرویس‌دهنده: %s', 'rahkar-toolkit' ), $result->messageId ) : __( 'ارسال شد.', 'rahkar-toolkit' ), ] ); } wp_send_json_error( [ 'message' => sprintf( /* translators: 1: error code, 2: provider message */ __( 'ارسال نشد (%1$s): %2$s', 'rahkar-toolkit' ), $result->errorCode, $result->error ), ] ); } }