Advertisement
verygoodplugins

Untitled

Aug 20th, 2024
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.03 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! isset( $_GET['wpf_action'] ) ) {
  4.     exit();
  5. }
  6.  
  7. if ( ! defined( 'ABSPATH' ) ) {
  8.  
  9.     // Normally ABSPATH isn't defined here, but this allows for overriding it with
  10.     // auto_prepend_file or by including this file via a bootstrap.
  11.  
  12.     $full_path    = getcwd();
  13.     $ar           = explode( 'wp-', $full_path );
  14.     $wp_root_path = $ar[0];
  15.  
  16.     define( 'ABSPATH', $wp_root_path );
  17.  
  18. }
  19.  
  20. define( 'SHORTINIT', true ); // load the minumum files required to get to the database.
  21.  
  22. require ABSPATH . DIRECTORY_SEPARATOR . 'wp-load.php';
  23.  
  24. // WordPress is available now.
  25.  
  26. // Try to find the contact ID in the URL.
  27.  
  28. $contact_id = false;
  29.  
  30. if ( isset( $_REQUEST['contact']['id'] ) ) {
  31.     $contact_id = absint( $_REQUEST['contact']['id'] ); // ActiveCampaign.
  32. }
  33.  
  34. if ( isset( $_REQUEST['contactId'] ) ) {
  35.     $contact_id = absint( $_REQUEST['contactId'] ); // Infusionsoft.
  36. }
  37.  
  38. if ( isset( $_REQUEST['contact_id'] ) ) {
  39.     $contact_id = sanitize_text_field( wp_unslash( $_REQUEST['contact_id'] ) ); // Default.
  40. }
  41.  
  42. // Try via the payload. Drip first.
  43.  
  44. $payload = json_decode( file_get_contents( 'php://input' ) );
  45.  
  46. if ( isset( $payload->event ) && ( $payload->event == 'subscriber.applied_tag' || $payload->event == 'subscriber.removed_tag' || $payload->event == 'subscriber.updated_custom_field' || $payload->event == 'subscriber.updated_email_address' ) ) {
  47.  
  48.     // Drip admin settings webhooks.
  49.     $contact_id = sanitize_key( $payload->data->subscriber->id );
  50.  
  51. } elseif ( isset( $payload->subscriber ) ) {
  52.  
  53.     // Drip automation / rules triggers.
  54.     $contact_id = sanitize_key( $payload->subscriber->id );
  55.  
  56. } elseif ( isset( $payload->id ) ) {
  57.  
  58.     $contact_id = absint( $payload->id ); // FluentCRM, Brevo, and others.
  59.  
  60. }
  61.  
  62. if ( ! $contact_id ) {
  63.     wp_die( 'No contact ID specified.' );
  64. }
  65.  
  66. $settings = get_option( 'wpf_options' );
  67.  
  68. if ( ! isset( $_GET['access_key'] ) || $_GET['access_key'] !== $settings['access_key'] ) {
  69.     wp_die( 'Invalid access key' );
  70. }
  71.  
  72. $action = sanitize_text_field( wp_unslash( $_GET['wpf_action'] ) );
  73.  
  74. // Now create the action to perform based on the wpf_action parameter.
  75.  
  76. if ( 'update' === $action || 'update_tags' === $action ) {
  77.  
  78.     $user_id = wp_cache_get( "wpf_cid_{$contact_id}" ); // try to get it from the cache.
  79.  
  80.     if ( false === $user_id ) {
  81.  
  82.         global $wpdb;
  83.  
  84.         // Update and Update Tags require a user ID.
  85.  
  86.         $sql     = $wpdb->prepare( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value = %s", "{$settings['crm']}_contact_id", $contact_id );
  87.         $user_id = $wpdb->get_var( $sql );
  88.  
  89.         if ( null === $user_id ) {
  90.             wp_die( 'No matching user found', 'Not Found', 200 );
  91.         }
  92.  
  93.         wp_cache_set( "wpf_cid_{$contact_id}", $user_id );
  94.  
  95.     }
  96.  
  97.     $data = array(
  98.         array(
  99.             'users_tags_sync',
  100.             array( $user_id ),
  101.         ),
  102.     );
  103.  
  104.     if ( 'update' === $action ) {
  105.  
  106.         $data[] = array(
  107.             'pull_users_meta',
  108.             array( $user_id ),
  109.         );
  110.  
  111.     }
  112. } elseif ( 'add' === $action ) {
  113.  
  114.  
  115.     if ( is_numeric( $contact_id ) ) {
  116.         // Most platforms use numeric IDs but Drip, Mailchimp, and Salesforce use alphanumeric hashes.
  117.         $contact_id = absint( $contact_id );
  118.     }
  119.  
  120.     $data = array(
  121.         array(
  122.             'import_users',
  123.             array(
  124.                 $contact_id,
  125.                 array(
  126.                     'role'              => isset( $_GET['role'] ) ? sanitize_text_field( wp_unslash( $_GET['role'] ) ) : false,
  127.                     'send_notification' => isset( $_GET['send_notification'] ) && 'true' === $_GET['send_notification'] ? true : false,
  128.                 ),
  129.             ),
  130.         ),
  131.     );
  132.  
  133. } else {
  134.     wp_die( 'Invalid action' );
  135. }
  136.  
  137. // We have our data, now save it to the options table so the background worker can find it.
  138.  
  139. $unique  = md5( microtime() . rand() );
  140. $prepend = 'wpf_background_process_';
  141.  
  142. $key = substr( $prepend . $unique, 0, 48 );
  143.  
  144. update_site_option( $key, $data );
  145.  
  146. // Make sure that the cron task is enabled.
  147.  
  148. if ( empty( $settings['enable_cron'] ) ) {
  149.     $settings['enable_cron'] = true;
  150.     update_option( 'wpf_options', $settings );
  151. }
  152.  
  153. // All done!
  154.  
  155. wp_die( 'Success. Saved <code>' . $key . '</code> with <pre>' . print_r( $data, true ) . '</pre>', 'Success', 200 );
  156.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement