get_id() ); $anfrage->set_param( 'context', 'view' ); $anfrage->set_param( 'dp', wc_get_price_decimals() ); $antwort = $controller->prepare_object_for_response( $order, $anfrage ); return $antwort instanceof WP_REST_Response ? $antwort->get_data() : null; } /** * Aktion talkward_bestellungen_senden: Bestellungen (+ gelöschte) an Talkward, bei Fehlern später nochmal. */ public static function senden( $ids, $geloescht = array(), $versuch = 0, $abgleich = null ): bool { $bestellungen = array(); foreach ( (array) $ids as $id ) { $daten = self::als_rest( wc_get_order( absint( $id ) ) ); if ( $daten ) { $bestellungen[] = $daten; } } if ( ! $bestellungen && ! $geloescht && null === $abgleich ) { return true; } $nutzlast = array( 'bestellungen' => $bestellungen, 'geloescht' => array_values( array_map( 'absint', (array) $geloescht ) ) ); if ( null !== $abgleich ) { $nutzlast['abgleich'] = $abgleich; } $antwort = Talkward_Api::senden( 'bestellungen', $nutzlast, 45 ); if ( ! is_wp_error( $antwort ) ) { // Tarif ohne Bestelldaten: Talkward hat nichts gespeichert, ab jetzt geht nichts mehr raus Talkward_Api::daten_merken( $antwort ); self::info( array( 'kontakt' => time(), 'fehler' => '' ) ); return true; } if ( 'talkward_getrennt' === $antwort->get_error_code() ) { return false; } self::info( array( 'fehler' => $antwort->get_error_message() ) ); $versuch = (int) $versuch + 1; if ( $versuch <= self::VERSUCHE && function_exists( 'as_schedule_single_action' ) ) { // 1, 2, 4 … Minuten, höchstens eine Stunde as_schedule_single_action( time() + min( HOUR_IN_SECONDS, 60 * ( 2 ** ( $versuch - 1 ) ) ), 'talkward_bestellungen_senden', array( array_values( (array) $ids ), array_values( (array) $geloescht ), $versuch ), self::GRUPPE ); } return false; } // ------------------------------------------------------------ Erster Abgleich public static function stand(): array { $s = get_option( self::OPTION_STAND, array() ); return is_array( $s ) ? $s : array(); } private static function abfrage( int $seite, bool $zaehlen = false ) { $args = array( 'type' => 'shop_order', 'status' => array_keys( wc_get_order_statuses() ), 'date_created' => '>=' . ( time() - self::TAGE * DAY_IN_SECONDS ), 'orderby' => 'ID', 'order' => 'ASC', 'limit' => self::PAKET, 'paged' => $seite, 'return' => 'ids', ); if ( $zaehlen ) { $args['paginate'] = true; $args['limit'] = 1; } return wc_get_orders( $args ); } /** Alle Bestellungen der letzten 90 Tage an Talkward (Fortschritt in der Option talkward_abgleich). */ public static function starten(): void { if ( ! self::shop() || ! Talkward_Api::daten() || ! function_exists( 'as_enqueue_async_action' ) ) { return; } as_unschedule_all_actions( 'talkward_abgleich_paket', array(), self::GRUPPE ); $zaehlung = self::abfrage( 1, true ); update_option( self::OPTION_STAND, array( 'gesamt' => (int) ( $zaehlung->total ?? 0 ), 'fertig' => 0, 'laeuft' => true, 'seit' => time(), ), false ); as_enqueue_async_action( 'talkward_abgleich_paket', array( 1 ), self::GRUPPE ); } public static function paket( $seite ): void { $seite = max( 1, absint( $seite ) ); $ids = self::abfrage( $seite ); $stand = self::stand(); $fertig = (int) ( $stand['fertig'] ?? 0 ) + count( $ids ); $weiter = count( $ids ) >= self::PAKET; $abgleich = array( 'gesamt' => max( (int) ( $stand['gesamt'] ?? 0 ), $fertig ), 'fertig' => $fertig, 'laeuft' => $weiter, ); if ( ! self::senden( $ids, array(), self::VERSUCHE, $abgleich ) ) { // Ohne Talkward kein Weiter; der nächste Versuch kommt über den Status-Takt oder „Neu abgleichen“. update_option( self::OPTION_STAND, array_merge( $stand, array( 'laeuft' => false, 'abgebrochen' => true ) ), false ); return; } update_option( self::OPTION_STAND, array_merge( $stand, $abgleich, array( 'abgebrochen' => false ) ), false ); if ( $weiter ) { as_enqueue_async_action( 'talkward_abgleich_paket', array( $seite + 1 ), self::GRUPPE ); } } // ------------------------------------------------------------ Status (stündlich) + Befehle aus Talkward public static function takt(): void { if ( ! Talkward_Api::verbunden() ) { return; } if ( function_exists( 'as_has_scheduled_action' ) && function_exists( 'as_schedule_recurring_action' ) ) { if ( ! as_has_scheduled_action( 'talkward_status', array(), self::GRUPPE ) ) { as_schedule_recurring_action( time() + 60, HOUR_IN_SECONDS, 'talkward_status', array(), self::GRUPPE ); } // Vorher ohne WooCommerce gelaufen: der Takt über WP-Cron wäre doppelt wp_clear_scheduled_hook( 'talkward_status' ); } elseif ( ! wp_next_scheduled( 'talkward_status' ) ) { // Ohne WooCommerce gibt es keinen Action Scheduler wp_schedule_event( time() + 60, 'hourly', 'talkward_status' ); } } public static function info( array $neu = array() ): array { $i = get_option( self::OPTION_INFO, array() ); $i = is_array( $i ) ? $i : array(); if ( $neu ) { $i = array_merge( $i, $neu ); update_option( self::OPTION_INFO, $i, false ); } return $i; } /** @return array|WP_Error */ public static function status() { if ( ! Talkward_Api::verbunden() ) { return new WP_Error( 'talkward_getrennt', '' ); } $stand = self::stand(); $antwort = Talkward_Api::senden( 'status', array_merge( Talkward_Api::umgebung(), array( 'widget' => Talkward_Widget::an(), 'abgleich' => $stand ? array( 'gesamt' => (int) ( $stand['gesamt'] ?? 0 ), 'fertig' => (int) ( $stand['fertig'] ?? 0 ), 'laeuft' => ! empty( $stand['laeuft'] ), ) : null, ) ), 20 ); if ( is_wp_error( $antwort ) ) { self::info( array( 'fehler' => $antwort->get_error_message() ) ); return $antwort; } $daten_vorher = Talkward_Api::daten(); $v = Talkward_Api::verbindung(); if ( array_key_exists( 'daten', $antwort ) ) { $v['daten'] = ! empty( $antwort['daten'] ); $v['daten_ab'] = sanitize_text_field( $antwort['daten_ab'] ?? ( $v['daten_ab'] ?? '' ) ); } $v['marke'] = array( 'key' => sanitize_key( $antwort['marke']['key'] ?? ( $v['marke']['key'] ?? '' ) ), 'name' => sanitize_text_field( $antwort['marke']['name'] ?? ( $v['marke']['name'] ?? '' ) ), ); $v['widget_schluessel'] = sanitize_text_field( $antwort['widget_schluessel'] ?? ( $v['widget_schluessel'] ?? '' ) ); update_option( Talkward_Api::OPTION, $v, false ); self::info( array( 'kontakt' => time(), 'fehler' => '', 'bestellungen' => (int) ( $antwort['bestellungen'] ?? 0 ), 'version_aktuell' => sanitize_text_field( $antwort['version_aktuell'] ?? '' ), ) ); if ( in_array( 'abgleich', (array) ( $antwort['befehle'] ?? array() ), true ) ) { self::starten(); } elseif ( ! $daten_vorher && Talkward_Api::daten() ) { // Tarif gewechselt: Talkward nimmt jetzt Bestellungen an, also alles der letzten 90 Tage schicken self::starten(); } // Abgebrochener erster Abgleich: jetzt, wo Talkward wieder antwortet, neu anstoßen. if ( ! empty( $stand['abgebrochen'] ) ) { self::starten(); } return $antwort; } }