__( 'Not available.', 'ai-eshop-optimizer' ) ), 403 ); } private function posted_sources() { $raw = isset( $_POST['sources'] ) ? wp_unslash( $_POST['sources'] ) : ''; $ids = is_array( $raw ) ? $raw : array_filter( explode( ',', (string) $raw ) ); return array_map( 'sanitize_key', $ids ); } public function ajax_scan() { AIEO_DMM::verify_ajax_request(); if ( ! $this->guard() ) { $this->deny(); } wp_send_json_success( AIEO_Contact_Discovery::scan() ); } public function ajax_preview() { AIEO_DMM::verify_ajax_request(); if ( ! $this->guard() ) { $this->deny(); } $sources = $this->posted_sources(); $res = AIEO_Contact_Discovery::convert( $sources, array( 'dry_run' => true, 'limit' => 25 ) ); $total = AIEO_Contact_Discovery::candidate_count( $sources ); wp_send_json_success( array( 'total' => $total, 'samples' => $res['samples'], 'table' => $this->sample_table( $res['samples'], (int) $total ), ) ); } private function sample_table( array $samples, $total ) { $rows = array(); foreach ( $samples as $i => $r ) { $email = (string) ( $r['email'] ?? '' ); $name = (string) ( $r['name'] ?? '' ); $seen = (string) ( $r['sources'] ?? '' ); $rows[] = array( '_id' => (string) ( $i + 1 ), 'email' => $email, 'name' => $name, 'sources' => $seen, '_s' => array( 'email' => $email, 'name' => $name, 'sources' => $seen, ), '_text' => $email . ' ' . $name, ); } $shown = count( $rows ); return AIEO_Table::local_answer( 'aieo-cd-table', array( array( 'key' => 'email', 'label' => __( 'Email', 'ai-eshop-optimizer' ), 'sort_type' => 'text' ), array( 'key' => 'name', 'label' => __( 'Name', 'ai-eshop-optimizer' ), 'sort_type' => 'text' ), array( 'key' => 'sources', 'label' => __( 'Seen in', 'ai-eshop-optimizer' ), 'sort_type' => 'text' ), ), $rows, array( 'prefix' => 'cd_', 'size' => 25, 'caption' => __( 'Discovered contacts', 'ai-eshop-optimizer' ), 'empty' => esc_html__( 'No contacts without an account in the ticked sources.', 'ai-eshop-optimizer' ), 'toolbar' => \Rooster\Admin\Filters::bar( 'aieo-cd-table', array(), array(), array( 'search' => array( 'label' => __( 'Search', 'ai-eshop-optimizer' ), 'placeholder' => __( 'Email or name', 'ai-eshop-optimizer' ) ), 'prefix' => 'cd_', 'keys' => array( 's' => 'cd_s' ), ) ), 'capped' => $total > $shown && $shown > 0 ? array( 'shown' => $shown, 'total' => $total, ) : null, ) ); } public function ajax_settings() { AIEO_DMM::verify_ajax_request(); if ( ! $this->guard() ) { $this->deny(); } $s = AIEO_Contact_Discovery::save_settings( array( 'conversion_enabled' => ! empty( $_POST['conversion_enabled'] ) && 'false' !== $_POST['conversion_enabled'], 'notify' => ! empty( $_POST['notify'] ) && 'false' !== $_POST['notify'], 'sources' => $this->posted_sources(), ) ); wp_send_json_success( $s ); } public function ajax_start() { AIEO_DMM::verify_ajax_request(); if ( ! $this->guard() ) { $this->deny(); } $res = AIEO_Contact_Discovery::start_job( $this->posted_sources(), AIEO_Contact_Discovery::settings()['notify'] ); if ( is_wp_error( $res ) ) { wp_send_json_error( array( 'message' => $res->get_error_message() ) ); } wp_send_json_success( array( 'target' => (int) $res, 'job' => AIEO_Contact_Discovery::job() ) ); } public function ajax_progress() { AIEO_DMM::verify_ajax_request(); if ( ! $this->guard() ) { $this->deny(); } wp_send_json_success( array( 'job' => AIEO_Contact_Discovery::job() ) ); } public function ajax_cancel() { AIEO_DMM::verify_ajax_request(); if ( ! $this->guard() ) { $this->deny(); } AIEO_Contact_Discovery::cancel_job(); wp_send_json_success( array( 'job' => AIEO_Contact_Discovery::job() ) ); } public function render_subtab() { if ( ! class_exists( 'AIEO_Contact_Discovery' ) ) { list( $aieo_cd_open, $aieo_cd_close ) = AIEO_DMM_UI::card( array( 'icon' => 'search', 'title' => $this->get_label() ) ); echo $aieo_cd_open; echo \Rooster\Admin\Ui::notice( esc_html__( 'Contact discovery engine is not loaded.', 'ai-eshop-optimizer' ), 'destructive', array( 'inline' => true ) ); echo $aieo_cd_close; return; } $this->render_template( 'contact-discovery.php', array( 'nonce' => wp_create_nonce( AIEO_DMM::NONCE_ACTION ), 'sources' => AIEO_Contact_Discovery::sources(), 'settings' => AIEO_Contact_Discovery::settings(), 'last' => get_option( AIEO_Contact_Discovery::OPT_LAST_SCAN, array() ), 'job' => AIEO_Contact_Discovery::job(), ) ); } }