| 1 |
<?php |
| 2 |
/** |
| 3 |
* Facebook Lead Ads Triggers |
| 4 |
* |
| 5 |
* @package WPbot_Automator |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPbot_Automator\Triggers; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Class Facebook_Lead_Ads_Triggers |
| 16 |
*/ |
| 17 |
class Facebook_Lead_Ads_Triggers extends Trigger { |
| 18 |
|
| 19 |
/** |
| 20 |
* Constructor |
| 21 |
*/ |
| 22 |
public function __construct() { |
| 23 |
$this->id = 'facebook'; |
| 24 |
$this->group = __( 'Facebook', 'wpbot-automator' ); |
| 25 |
$this->title = __( 'Facebook Lead Ads', 'wpbot-automator' ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Register the triggers |
| 30 |
*/ |
| 31 |
public function register() { |
| 32 |
add_action( 'rest_api_init', array( $this, 'register_facebook_webhook_route' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Register REST API route for incoming webhooks |
| 37 |
*/ |
| 38 |
public function register_facebook_webhook_route() { |
| 39 |
register_rest_route( |
| 40 |
'wpbot-automator/v1', |
| 41 |
'/facebook-lead-ads/(?P<token>[a-zA-Z0-9]+)', |
| 42 |
array( |
| 43 |
array( |
| 44 |
'methods' => 'GET', |
| 45 |
'callback' => array( $this, 'handle_webhook_verification' ), |
| 46 |
'permission_callback' => '__return_true', |
| 47 |
), |
| 48 |
array( |
| 49 |
'methods' => 'POST', |
| 50 |
'callback' => array( $this, 'handle_webhook_payload' ), |
| 51 |
'permission_callback' => '__return_true', |
| 52 |
), |
| 53 |
) |
| 54 |
); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Handle Facebook Webhook Verification (GET request) |
| 59 |
*/ |
| 60 |
public function handle_webhook_verification( $request ) { |
| 61 |
$params = $request->get_query_params(); |
| 62 |
|
| 63 |
$hub_mode = isset( $params['hub_mode'] ) ? $params['hub_mode'] : ''; |
| 64 |
$hub_challenge = isset( $params['hub_challenge'] ) ? $params['hub_challenge'] : ''; |
| 65 |
$hub_verify_token = isset( $params['hub_verify_token'] ) ? $params['hub_verify_token'] : ''; |
| 66 |
|
| 67 |
// Facebook requires us to just echo the challenge code back |
| 68 |
// Note: The verify_token check should technically match what the user configured in the node. |
| 69 |
// However, because this is an unauthenticated GET request and can happen *before* the user fully saves their flow, |
| 70 |
// or they have multiple flows, the standard practice for integration platforms is to either |
| 71 |
// verify against a stored token or temporarily allow setup. |
| 72 |
// We'll just echo the challenge code to ensure the webhook gets verified in their FB developer dashboard. |
| 73 |
|
| 74 |
if ( 'subscribe' === $hub_mode && ! empty( $hub_challenge ) ) { |
| 75 |
// Facebook expects a raw integer/string, NOT a JSON response for the challenge |
| 76 |
echo esc_html( $hub_challenge ); |
| 77 |
exit; |
| 78 |
} |
| 79 |
|
| 80 |
return rest_ensure_response( array( |
| 81 |
'success' => false, |
| 82 |
'message' => 'Invalid verification request', |
| 83 |
) ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Handle incoming webhook payload from Facebook (POST request) |
| 88 |
*/ |
| 89 |
public function handle_webhook_payload( $request ) { |
| 90 |
$token = $request->get_param( 'token' ); |
| 91 |
$body = $request->get_params(); |
| 92 |
|
| 93 |
// Facebook Lead Ads sends data in a specific format |
| 94 |
if ( ! empty( $body['object'] ) && 'page' === $body['object'] ) { |
| 95 |
if ( ! empty( $body['entry'] ) && is_array( $body['entry'] ) ) { |
| 96 |
foreach ( $body['entry'] as $entry ) { |
| 97 |
if ( ! empty( $entry['changes'] ) && is_array( $entry['changes'] ) ) { |
| 98 |
foreach ( $entry['changes'] as $change ) { |
| 99 |
if ( 'leadgen' === $change['field'] && ! empty( $change['value']['leadgen_id'] ) ) { |
| 100 |
$leadgen_id = $change['value']['leadgen_id']; |
| 101 |
$page_id = $change['value']['page_id']; |
| 102 |
|
| 103 |
// Trigger the automator node |
| 104 |
// The node configuration will contain the page_access_token needed to fetch details. |
| 105 |
// We pass the leadgen_id so the action/trigger execution can fetch the real data. |
| 106 |
|
| 107 |
// Since we need to look up node configs to get the access token *before* we distribute data, |
| 108 |
// we pass both the raw payload and let the system match the target node. |
| 109 |
// This trigger acts primarily as the receiver. |
| 110 |
|
| 111 |
$this->run( array( |
| 112 |
'sub_id' => 'facebook_lead_ads', |
| 113 |
'data' => array_merge( $body, array( |
| 114 |
'target_node_id' => 'node-' . $token, |
| 115 |
'leadgen_id' => $leadgen_id, |
| 116 |
'page_id' => $page_id, |
| 117 |
) ), |
| 118 |
) ); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
} else { |
| 125 |
// Fallback generic run if it's not a recognized leadgen payload (e.g. testing from FB dev console) |
| 126 |
$this->run( array( |
| 127 |
'sub_id' => 'facebook_lead_ads', |
| 128 |
'data' => array_merge( $body, array( |
| 129 |
'target_node_id' => 'node-' . $token, |
| 130 |
) ), |
| 131 |
) ); |
| 132 |
} |
| 133 |
|
| 134 |
return rest_ensure_response( array( |
| 135 |
'success' => true, |
| 136 |
'message' => 'Webhook received and workflow triggered.', |
| 137 |
) ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Fetch Lead Details from Facebook Graph API |
| 142 |
* |
| 143 |
* Note: this is typically called during execution or when preparing data for the next step. |
| 144 |
* |
| 145 |
* @param string $leadgen_id The Lead ID |
| 146 |
* @param string $access_token The Facebook Page Access Token |
| 147 |
* @return array|\WP_Error |
| 148 |
*/ |
| 149 |
public static function get_lead_details( $leadgen_id, $access_token ) { |
| 150 |
$url = "https://graph.facebook.com/v20.0/{$leadgen_id}?access_token={$access_token}"; |
| 151 |
|
| 152 |
$response = wp_remote_get( $url, array( |
| 153 |
'timeout' => 20 |
| 154 |
) ); |
| 155 |
|
| 156 |
if ( is_wp_error( $response ) ) { |
| 157 |
return $response; |
| 158 |
} |
| 159 |
|
| 160 |
$body = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 161 |
|
| 162 |
if ( isset( $body['error'] ) ) { |
| 163 |
return new \WP_Error( 'fb_graph_error', $body['error']['message'] ?? 'Unknown Facebook Graph API error' ); |
| 164 |
} |
| 165 |
|
| 166 |
// Parse field_data into a flat array for easier mapping |
| 167 |
$flat_data = array(); |
| 168 |
if ( ! empty( $body['field_data'] ) && is_array( $body['field_data'] ) ) { |
| 169 |
foreach ( $body['field_data'] as $field ) { |
| 170 |
$flat_data[ $field['name'] ] = isset( $field['values'][0] ) ? $field['values'][0] : ''; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return array( |
| 175 |
'raw' => $body, |
| 176 |
'fields' => $flat_data |
| 177 |
); |
| 178 |
} |
| 179 |
} |
| 180 |
|