Advertisement
verygoodplugins

Untitled

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