auth = $auth; add_action( 'admin_bar_menu', array( $this, 'admin_bar' ), 90 ); add_action( 'admin_notices', array( $this, 'admin_notice' ) ); add_action( 'admin_init', array( $this, 'sync_inbox' ) ); add_action( 'agents24x7_disconnecting', array( $this, 'disconnect' ) ); add_filter( 'woocommerce_rest_prepare_note', array( $this, 'localise_note' ), 10, 3 ); add_action( 'agents24x7_approval_changed', array( $this, 'refresh' ) ); } private function text( $translated, $hebrew ) { return 0 === strpos( get_user_locale(), 'he' ) ? $hebrew : $translated; } private function cache_key() { return self::CACHE_KEY . '_' . md5( home_url() . wp_json_encode( $this->auth->get_auth_headers() ) ) . ( class_exists( 'WooCommerce' ) ? '_store' : '_site' ); } private function data() { if ( ! current_user_can( 'manage_options' ) || ! $this->auth->is_authenticated() ) { return null; } $cached = get_transient( $this->cache_key() ); if ( false !== $cached ) { return $cached ? $cached : null; } // Notification outages must not change the plugin's service status or show an error banner. $response = wp_remote_post( rtrim( Agents24x7_Config::get_api_url(), '/' ) . '/wordpress/api', array( 'headers' => array_merge( $this->auth->get_auth_headers(), array( 'Content-Type' => 'application/json', 'X-WordPress-Site' => home_url(), 'X-WordPress-WooCommerce' => class_exists( 'WooCommerce' ) ? 'true' : 'false', 'X-WordPress-User' => get_current_user_id(), ) ), 'body' => wp_json_encode( array( 'type' => 'query', 'functionName' => 'wordpress/notifications:get', 'args' => array( 'siteUrl' => home_url() ) ) ), 'timeout' => 5, ) ); $data = null; if ( ! is_wp_error( $response ) && 200 === wp_remote_retrieve_response_code( $response ) ) { $body = json_decode( wp_remote_retrieve_body( $response ), true ); $candidate = isset( $body['data'] ) ? $body['data'] : null; if ( is_array( $candidate ) && isset( $candidate['waiting'], $candidate['urgent'], $candidate['newKinds'] ) && is_int( $candidate['waiting'] ) && $candidate['waiting'] >= 0 && is_array( $candidate['urgent'] ) && is_array( $candidate['newKinds'] ) ) { $data = $candidate; } } set_transient( $this->cache_key(), $data ? $data : array(), 300 ); return $data; } public function refresh() { if ( ! current_user_can( 'manage_options' ) ) { return; } delete_transient( $this->cache_key() ); $this->data(); } private function link( $item_id = '' ) { return admin_url( 'admin.php?page=agents24x7-work' . ( $item_id ? '&workItem=' . rawurlencode( $item_id ) : '' ) ); } public function menu_title() { $data = $this->data(); $count = $data ? $data['waiting'] : 0; return 'Agents24x7' . ( $count > 0 ? ' ' . $count . '' : '' ); } public function admin_bar( $bar ) { $data = $this->data(); if ( ! $data || ! $data['waiting'] ) { return; } $bar->add_node( array( 'id' => 'agents24x7-needs-you', 'title' => sprintf( __( 'Agents24x7 (%d)', 'agents24x7' ), $data['waiting'] ), 'href' => $this->link(), ) ); } public function admin_notice( $plugin_page = false ) { if ( ! current_user_can( 'manage_options' ) ) { return; } $screen = get_current_screen(); if ( ! $screen || ( 'dashboard' !== $screen->id && ( ! $plugin_page || false === strpos( $screen->id, 'agents24x7' ) ) ) ) { return; } $data = $this->data(); if ( ! $data ) { return; } foreach ( $data['urgent'] as $item ) { if ( empty( $item['id'] ) || empty( $item['reason'] ) ) { continue; } if ( 'approval_expiring' === $item['reason'] ) { $message = $this->text( __( 'An approval expires within 24 hours.', 'agents24x7' ), 'אישור יפוג בתוך 24 שעות.' ); } elseif ( 'publish_failed' === $item['reason'] ) { $message = $this->text( __( 'Publishing approved content failed.', 'agents24x7' ), 'פרסום תוכן שאושר נכשל.' ); } else { continue; } echo '

' . esc_html( $message ) . ' ' . esc_html( $this->text( __( 'Open', 'agents24x7' ), 'פתיחה' ) ) . '

'; } } private function inbox_key( $suffix ) { return 'agents24x7_notification_' . $suffix . '_' . substr( $this->cache_key(), strlen( self::CACHE_KEY ) + 1 ); } private function note_name( $kind ) { return 'agents24x7-waiting-' . substr( $this->cache_key(), strlen( self::CACHE_KEY ) + 1 ) . '-' . $kind; } public function disconnect() { if ( class_exists( 'Automattic\\WooCommerce\\Admin\\Notes\\Notes' ) ) { $names = get_option( 'agents24x7_notification_note_names', array() ); foreach ( array_keys( get_option( 'agents24x7_notification_topics', array() ) ) as $kind ) { $names[] = 'agents24x7-waiting-' . $kind; } foreach ( $names as $name ) { \Automattic\WooCommerce\Admin\Notes\Notes::delete_notes_with_name( $name ); } delete_option( 'agents24x7_notification_note_names' ); } delete_transient( $this->cache_key() ); } public function localise_note( $response, $note, $request ) { if ( 0 !== strpos( $note['name'], 'agents24x7-waiting-' ) ) { return $response; } $data = $response->get_data(); $data['content'] = $this->text( __( 'New work items need your attention. Open to review them.', 'agents24x7' ), 'משימות חדשות דורשות את תשומת לבך. אפשר לפתוח ולעיין בהן.' ); foreach ( $data['actions'] as &$action ) { $label = $this->text( __( 'Open', 'agents24x7' ), 'פתיחה' ); if ( is_object( $action ) ) { $action->label = $label; } else { $action['label'] = $label; } } unset( $action ); $response->set_data( $data ); return $response; } public function sync_inbox() { if ( ! current_user_can( 'manage_options' ) || ! class_exists( 'WooCommerce' ) || ! class_exists( 'Automattic\\WooCommerce\\Admin\\Notes\\Note' ) || ! class_exists( 'Automattic\\WooCommerce\\Admin\\Notes\\Notes' ) ) { return; } $data = $this->data(); if ( ! $data ) { return; } $lock_key = $this->inbox_key( 'inbox_lock' ); if ( ! add_option( $lock_key, time(), '', false ) ) { if ( (int) get_option( $lock_key ) < time() - 60 ) { delete_option( $lock_key ); } return; } try { $seen = get_option( $this->inbox_key( 'topics' ), array() ); $last_note = (int) get_option( $this->inbox_key( 'last_note' ), 0 ); $prefix = $this->note_name( '' ); foreach ( get_option( 'agents24x7_notification_note_names', array() ) as $name ) { if ( 0 !== strpos( $name, $prefix ) ) { continue; } $kind = substr( $name, strlen( $prefix ) ); foreach ( \Automattic\WooCommerce\Admin\Notes\Notes::load_data_store()->get_notes_with_name( $name ) as $note_id ) { $saved_note = new \Automattic\WooCommerce\Admin\Notes\Note( $note_id ); $seen[ $kind ] = array( 'active' => true ); $last_note = max( $last_note, $saved_note->get_date_created()->getTimestamp() ); } } $active = array_column( $data['newKinds'], 'kind' ); foreach ( $seen as $kind => $state ) { if ( ! empty( $state['active'] ) && ! in_array( $kind, $active, true ) ) { \Automattic\WooCommerce\Admin\Notes\Notes::delete_notes_with_name( $this->note_name( $kind ) ); $seen[ $kind ]['active'] = false; } } foreach ( $data['newKinds'] as $topic ) { if ( empty( $topic['kind'] ) || empty( $topic['id'] ) || ! preg_match( '/^[a-z_]+$/', $topic['kind'] ) || isset( $seen[ $topic['kind'] ] ) || time() - $last_note < 86400 ) { continue; } // A WooCommerce note error must never break wp-admin: log it and try again on the next sync. try { $note = new \Automattic\WooCommerce\Admin\Notes\Note(); $name = $this->note_name( $topic['kind'] ); // Register before saving so disconnect also cleans up an interrupted creation. $names = get_option( 'agents24x7_notification_note_names', array() ); if ( ! in_array( $name, $names, true ) ) { $names[] = $name; update_option( 'agents24x7_notification_note_names', $names, false ); } $note->set_name( $name ); $note->set_title( __( 'Agents24x7', 'agents24x7' ) ); $note->set_content( 'agents24x7_waiting_work' ); $note->set_type( \Automattic\WooCommerce\Admin\Notes\Note::E_WC_ADMIN_NOTE_INFORMATIONAL ); $note->set_source( 'agents24x7' ); $note->add_action( 'open', 'open', $this->link( $topic['id'] ) ); $note->save(); $seen[ $topic['kind'] ] = array( 'active' => true ); $last_note = time(); } catch ( \Throwable $error ) { error_log( 'Agents24x7: could not add the Inbox note: ' . $error->getMessage() ); } } update_option( $this->inbox_key( 'last_note' ), $last_note, false ); update_option( $this->inbox_key( 'topics' ), $seen, false ); } finally { delete_option( $lock_key ); } } }