| @@ -6,8 +6,11 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace SureDonation\Inc\Admin; |
| 9 | 9 | |
| 10 | +use SureDonation\Inc\Helper; | |
| 11 | +use SureDonation\Inc\Payments\Payment_Helper; | |
| 12 | +use SureDonation\Inc\Post_Types\Donation_Form; | |
| 10 | 13 | use SureDonation\Inc\Traits\Get_Instance; |
| 11 | 14 | |
| 12 | 15 | // Exit if accessed directly. |
| 13 | 16 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -32,8 +35,9 @@ | ||
| 32 | 35 | */ |
| 33 | 36 | public function __construct() { |
| 34 | 37 | add_action( 'wp_ajax_suredonation_recommended_plugin_install', 'wp_ajax_install_plugin' ); |
| 35 | 38 | add_action( 'wp_ajax_suredonation_recommended_plugin_activate', [ $this, 'recommended_plugin_activate' ] ); |
| 39 | + add_action( 'wp_ajax_suredonation_integration', [ $this, 'generate_data_for_suretriggers_integration' ] ); | |
| 36 | 40 | } |
| 37 | 41 | |
| 38 | 42 | /** |
| 39 | 43 | * Activate a recommended plugin. |
| @@ -89,8 +93,134 @@ | ||
| 89 | 93 | wp_send_json_success( |
| 90 | 94 | [ |
| 91 | 95 | 'success' => true, |
| 92 | 96 | 'message' => __( 'Plugin activated successfully.', 'suredonation' ), |
| 97 | + ] | |
| 98 | + ); | |
| 99 | + } | |
| 100 | + | |
| 101 | + /** | |
| 102 | + * Generate the configuration payload required by the OttoKit (formerly | |
| 103 | + * SureTriggers) embed to render the automation builder iframe. | |
| 104 | + * | |
| 105 | + * Called from the donation form settings. The embed is scoped to the | |
| 106 | + * form's CAMPAIGN — `embedded_identifier` and `selected_options` use the | |
| 107 | + * campaign ID so every form in a campaign shares the same automations, | |
| 108 | + * matching OttoKit's campaign-level trigger filtering. | |
| 109 | + * | |
| 110 | + * @since 1.2.0 | |
| 111 | + * @return void | |
| 112 | + */ | |
| 113 | + public function generate_data_for_suretriggers_integration() { | |
| 114 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 115 | + wp_send_json_error( [ 'message' => __( 'You do not have permission to access this page.', 'suredonation' ) ] ); | |
| 116 | + } | |
| 117 | + | |
| 118 | + if ( ! check_ajax_referer( 'suredonation_suretriggers_nonce', 'security', false ) ) { | |
| 119 | + wp_send_json_error( [ 'message' => __( 'Invalid nonce.', 'suredonation' ) ] ); | |
| 120 | + } | |
| 121 | + | |
| 122 | + if ( ! Helper::is_suretriggers_ready() ) { | |
| 123 | + wp_send_json_error( | |
| 124 | + [ | |
| 125 | + 'code' => 'invalid_secret_key', | |
| 126 | + 'message' => __( 'OttoKit is not configured properly.', 'suredonation' ), | |
| 127 | + ] | |
| 128 | + ); | |
| 129 | + } | |
| 130 | + | |
| 131 | + $form_id = isset( $_POST['formId'] ) ? absint( wp_unslash( $_POST['formId'] ) ) : 0; | |
| 132 | + | |
| 133 | + if ( ! $form_id || Donation_Form::POST_TYPE !== get_post_type( $form_id ) ) { | |
| 134 | + wp_send_json_error( [ 'message' => __( 'Invalid form ID.', 'suredonation' ) ] ); | |
| 135 | + } | |
| 136 | + | |
| 137 | + // Resolve the campaign this form belongs to — automations are scoped per campaign. | |
| 138 | + $campaign_id = absint( Helper::get_string_value( get_post_meta( $form_id, Donation_Form::META_CAMPAIGN_ID, true ) ) ); | |
| 139 | + | |
| 140 | + if ( ! $campaign_id ) { | |
| 141 | + wp_send_json_error( | |
| 142 | + [ | |
| 143 | + 'code' => 'no_campaign', | |
| 144 | + 'message' => __( 'This form is not linked to a campaign yet.', 'suredonation' ), | |
| 145 | + ] | |
| 146 | + ); | |
| 147 | + } | |
| 148 | + | |
| 149 | + $campaign = get_post( $campaign_id ); | |
| 150 | + $campaign_name = ( $campaign && ! empty( $campaign->post_title ) ) | |
| 151 | + ? $campaign->post_title | |
| 152 | + /* translators: %d: Campaign ID. */ | |
| 153 | + : sprintf( __( 'Campaign #%d', 'suredonation' ), $campaign_id ); | |
| 154 | + | |
| 155 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Filter is owned by the OttoKit plugin. | |
| 156 | + $api_url = apply_filters( 'suretriggers_get_iframe_url', SUREDONATION_SURETRIGGERS_INTEGRATION_BASE_URL ); | |
| 157 | + | |
| 158 | + // Format required by OttoKit to inject the builder iframe into the target id. | |
| 159 | + $body = [ | |
| 160 | + 'client_id' => 'SureDonation', | |
| 161 | + 'st_embed_url' => $api_url, | |
| 162 | + 'embedded_identifier' => $campaign_id, | |
| 163 | + 'target' => 'suretriggers-iframe-wrapper', // The div where OttoKit injects the iframe must have this id. | |
| 164 | + 'event' => [ | |
| 165 | + 'label' => __( 'Donation Completed', 'suredonation' ), | |
| 166 | + 'value' => 'suredonation_donation_completed', | |
| 167 | + 'description' => __( 'Runs when a donation payment is completed', 'suredonation' ), | |
| 168 | + ], | |
| 169 | + 'summary' => $campaign_name, | |
| 170 | + 'selected_options' => [ | |
| 171 | + // Key must match the OttoKit trigger's campaign field name | |
| 172 | + // (`suredonation_campaign`) for the builder to pre-select it. | |
| 173 | + 'suredonation_campaign' => [ | |
| 174 | + 'value' => $campaign_id, | |
| 175 | + 'label' => $campaign_name, | |
| 176 | + ], | |
| 177 | + ], | |
| 178 | + 'integration' => 'SureDonation', | |
| 179 | + // Flat shape — keys must mirror the runtime trigger context that | |
| 180 | + // OttoKit's WP module emits (get_donation_context / the pluggable | |
| 181 | + // "Fetch Data"). A nested `donation` object here makes OttoKit label | |
| 182 | + // the fields "Donation Campaign Id" etc., which then don't match the | |
| 183 | + // flat runtime keys ("Campaign Id"), so field mappings resolve empty. | |
| 184 | + 'sample_response' => [ | |
| 185 | + 'donation_id' => 12, | |
| 186 | + 'campaign_id' => $campaign_id, | |
| 187 | + 'campaign_title' => $campaign_name, | |
| 188 | + 'form_id' => $form_id, | |
| 189 | + 'form_title' => get_the_title( $form_id ), | |
| 190 | + 'donor_id' => 1, | |
| 191 | + 'donor_name' => __( 'John Doe', 'suredonation' ), | |
| 192 | + 'donor_email' => '[email protected]', | |
| 193 | + 'donor_phone' => '', | |
| 194 | + 'amount' => 100.00, | |
| 195 | + 'fees_covered' => 0, | |
| 196 | + 'currency' => Payment_Helper::get_currency(), | |
| 197 | + 'payment_status' => 'completed', | |
| 198 | + 'payment_mode' => 'live', | |
| 199 | + 'gateway' => 'stripe', | |
| 200 | + 'transaction_id' => 'pi_1234567890', | |
| 201 | + 'donation_type' => 'one-time', | |
| 202 | + 'is_anonymous' => 0, | |
| 203 | + 'donor_comment' => '', | |
| 204 | + 'subscription_id' => '', | |
| 205 | + 'subscription_status' => '', | |
| 206 | + 'created_at' => gmdate( 'Y-m-d H:i:s' ), | |
| 207 | + 'updated_at' => gmdate( 'Y-m-d H:i:s' ), | |
| 208 | + ], | |
| 209 | + ]; | |
| 210 | + | |
| 211 | + wp_send_json_success( | |
| 212 | + [ | |
| 213 | + 'message' => 'success', | |
| 214 | + /** | |
| 215 | + * Filter the configuration payload passed to the OttoKit embed. | |
| 216 | + * | |
| 217 | + * @param array<string,mixed> $body Embed configuration payload. | |
| 218 | + * @param int $campaign_id Resolved campaign ID. | |
| 219 | + * @param int $form_id Donation form ID the embed was opened from. | |
| 220 | + * @since 1.2.0 | |
| 221 | + */ | |
| 222 | + 'data' => apply_filters( 'suredonation_suretriggers_integration_data_filter', $body, $campaign_id, $form_id ), | |
| 93 | 223 | ] |
| 94 | 224 | ); |
| 95 | 225 | } |
| 96 | 226 | } |