setError( __( 'You are not allowed to synchronize records.', 'sprintplan-pipedrive-integration' ) ); return; } $module = isset( $_POST['wp_module'] ) ? sanitize_key( wp_unslash( $_POST['wp_module'] ) ) : ''; $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; $nonce = isset( $_POST['_wpspi_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpspi_nonce'] ) ) : ''; if ( ! $id || ! in_array( $module, array( 'orders', 'products', 'customers' ), true ) ) { $this->setError( __( 'The selected record or WordPress module is invalid.', 'sprintplan-pipedrive-integration' ) ); return; } if ( ! wp_verify_nonce( $nonce, 'wpspi_sync_' . $module . '_' . $id ) ) { $this->setError( __( 'Synchronization was not performed because the security check failed. Refresh the page and try again.', 'sprintplan-pipedrive-integration' ) ); return; } $sync = new WPSPI_Smart_Pipedrive_Public( false ); switch ( $module ) { case 'orders': $result = $sync->addOrderToPipedrive( $id ); break; case 'products': $result = $sync->addProductToPipedrive( $id ); break; case 'customers': $result = $sync->addUserToPipedrive( $id ); break; } if ( is_wp_error( $result ) ) { WPSPI_Smart_Pipedrive_API::log_error( 'Manual ' . $module . ' sync failed for record ' . $id . ': ' . $result->get_error_message() ); $this->setError( sprintf( /* translators: 1: module name, 2: record ID, 3: error details */ __( '%1$s #%2$d was not synchronized: %3$s', 'sprintplan-pipedrive-integration' ), ucfirst( $module ), $id, $result->get_error_message() ) ); return; } $synced = isset( $result['synced'] ) && is_array( $result['synced'] ) ? $result['synced'] : array(); if ( empty( $synced ) ) { $this->setError( __( 'No Pipedrive records were synchronized. Check that synchronization is enabled and that active field mappings exist.', 'sprintplan-pipedrive-integration' ) ); return; } $messages = array(); foreach ( $synced as $record ) { $messages[] = sprintf( '%1$s %2$s (ID %3$d)', ucfirst( $record['module'] ), $record['operation'], absint( $record['id'] ) ); } $this->notice = array( 'type' => 'success', 'message' => sprintf( /* translators: 1: WordPress module, 2: record ID, 3: successful Pipedrive operations */ __( '%1$s #%2$d synchronized successfully: %3$s.', 'sprintplan-pipedrive-integration' ), ucfirst( $module ), $id, implode( ', ', $messages ) ), ); } private function setError( $message ) { $this->notice = array( 'type' => 'error', 'message' => $message, ); } public function displaySynchData() { $notice = $this->notice; require_once WPSPI_PLUGIN_PATH . 'admin/partials/synchronization.php'; } }