'; } ); }); add_action('rest_api_init', function () { // Get Forms List register_rest_route('cf7spay/v1', '/forms', [ 'methods' => 'GET', 'callback' => 'cf7spay_get_forms', 'permission_callback' => function () { return current_user_can('manage_options'); } ]); // Get Entries by Form register_rest_route('cf7spay/v1', '/entries', [ 'methods' => 'GET', 'callback' => 'cf7spay_get_entries', 'permission_callback' => function () { return current_user_can('manage_options'); } ]); // Get Entry Details register_rest_route('cf7spay/v1', '/entry', [ 'methods' => 'GET', 'callback' => 'cf7spay_get_entry', 'permission_callback' => function () { return current_user_can('manage_options'); } ]); // Bulk action register_rest_route('cf7spay/v1', '/bulk-action', [ 'methods' => 'POST', 'callback' => 'cf7spay_bulk_action_api', 'permission_callback' => function () { return current_user_can('manage_options'); } ]); }); function cf7spay_get_forms() { global $wpdb; $table = $wpdb->prefix . 'cf7spay_forms'; $forms = get_posts([ 'post_type' => 'wpcf7_contact_form', 'numberposts' => -1 ]); $data = []; foreach ($forms as $form) { $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $table WHERE form_post_id=%d", $form->ID) ); $data[] = [ 'id' => $form->ID, 'title' => $form->post_title, 'count' => $count ]; } return $data; } function cf7spay_get_entries($request) { global $wpdb; $formId = (int) $request['formId']; $table = $wpdb->prefix . 'cf7spay_forms'; $results = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $table WHERE form_post_id=%d", $formId) ); $formTitle = get_the_title($formId); $data = []; foreach ($results as $row) { $values = unserialize($row->form_value); $data[] = [ 'id' => $row->form_id, 'date' => $row->form_date, 'data' => $values, 'form_name' => $formTitle ]; } return $data; } function cf7spay_get_entry($request) { global $wpdb; $formId = (int) $request['formId']; $entryId = (int) $request['entryId']; $table = $wpdb->prefix . 'cf7spay_forms'; $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE form_post_id=%d AND form_id=%d", $formId, $entryId ) ); if (!$row) return []; $data = unserialize($row->form_value); return [ 'id' => $row->form_id, 'date' => $row->form_date, 'data' => $data, // ADD THIS BLOCK 'meta' => [ 'item_name' => get_post_meta($entryId, 'cf7wpay_item_name', true), 'item_number' => get_post_meta($entryId, 'cf7wpay_item_number', true), 'payment_status' => get_post_meta($entryId, 'cf7wpay_payment_status', true), 'amount' => get_post_meta($entryId, 'cf7wpay_payment_amount', true), 'currency' => get_post_meta($entryId, 'cf7wpay_payment_currency', true), 'txn_id' => get_post_meta($entryId, 'cf7wpay_txn_id', true), ] ]; } function cf7spay_bulk_action_api($request) { global $wpdb; $ids = $request->get_param('ids'); $action = $request->get_param('action'); $table = $wpdb->prefix . 'cf7spay_forms'; if (!$ids || !is_array($ids)) { return ['success' => false]; } foreach ($ids as $id) { $row = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $table WHERE form_id=%d", $id) ); if (!$row) continue; $data = unserialize($row->form_value); if ($action === 'delete') { $wpdb->delete($table, ['form_id' => $id]); continue; } if ($action === 'read' || $action === 'unread') { $data['cf7wpay_status'] = $action; } $wpdb->update( $table, ['form_value' => serialize($data)], ['form_id' => $id] ); } return ['success' => true]; }