Advertisement
verygoodplugins

Untitled

Jun 8th, 2022 (edited)
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.22 KB | None | 0 0
  1. namespace POC_CF_CI;
  2.  
  3. if ( ! class_exists( 'apply_tag_trigger' ) ) {
  4.  
  5.     class apply_tag_trigger
  6.     {
  7.         protected static $instance = null;
  8.         /**
  9.          * Private constructor to make this a singleton
  10.          *
  11.          * @access private
  12.          */
  13.         private function __construct()
  14.         {
  15.             add_action( 'wpf_tags_applied', [ $this, 'wpf_tags_applied_cb' ], 10, 2 );
  16.         }
  17.  
  18.         public static function get_instance() {
  19.             if ( null == self::$instance ) {
  20.                 self::$instance = new self;
  21.             }
  22.             return self::$instance;
  23.         }
  24.  
  25.         public function wpf_tags_applied_cb( $user_id, $tags ) {
  26.  
  27.             wpf_log( 'notice', $user_id, 'DEBUG: wpf_tags_applied triggered', array( 'tag_array' => $tags ) );
  28.  
  29.             error_log('hooked action running for ' . $user_id);
  30.             error_log(print_r($tags, true));
  31.  
  32.             error_log( print_r($tags, 1) );
  33.             foreach ($tags as $tag) {
  34.                 // Here '14' is a length of 'consolidation_' text. We are checking if the tag starts with this substring.
  35.                 //if ( 'consolidation_' === substr( strtolower( $tag ), 0, 14 ) ) {
  36.  
  37.                 if ( strpos( $tag, 'consolidation_' ) !== false ) {
  38.                     error_log('consolidation tag found');
  39.  
  40.                     error_log('PROCESSING ITEMS CHECK');
  41.  
  42.                     $this->process_items_check( (int) $user_id );
  43.                     // We just want to check whether we have consolidation item's tag is applied in the current list of tags.
  44.                     break;
  45.                 }
  46.             }
  47.         }
  48.  
  49.         private function process_items_check( int $user_id = 0 ) {
  50.  
  51.             wpf_log( 'notice', $user_id, 'DEBUG: process items check user ID ' . $user_id );
  52.  
  53.             if ( $user_id ) {
  54.  
  55.                 // Lets get all the ignored and completed items so that we can only fetch and check access of fewer items.
  56.                 $completed  = get_user_meta( $user_id, 'consolidation_completed', true );
  57.                 $ignored    = get_user_meta( $user_id, 'consolidation_ignored', true );
  58.                 $tobe       = get_user_meta( $user_id, 'consolidation_tobe', true );
  59.  
  60.                 if ( empty( $completed ) ) $completed = array();
  61.                 if ( empty( $ignored ) ) $ignored = array();
  62.                 if ( empty( $tobe ) ) $tobe = array();
  63.                 $combined_status_posts = array_merge( $completed, $ignored, $tobe );
  64.  
  65.  
  66.                 error_log('tobe before');
  67.                 error_log( print_r($tobe, 1) );
  68.  
  69.                 $all_items = get_posts(
  70.                     array(
  71.                         'fields'        => 'ids',
  72.                         'numberposts'   => -1,
  73.                         'post_type'     => 'consolidation',
  74.                         'post_status'   => 'publish',
  75.                         'post__not_in'  => $combined_status_posts,
  76.                     )
  77.                 );
  78.  
  79.                 error_log('all items');
  80.                 error_log( print_r($all_items, 1) );
  81.  
  82.                 if ( ! empty( $all_items ) ) {
  83.                     $flag_send_mail = false;
  84.                     $new_item_id = 0; // used in sending email.
  85.                     foreach ( $all_items as $item_id) {
  86.                         if( wp_fusion()->access->user_can_access( $item_id, $user_id ) ) {
  87.                             error_log(' has access to ' . $item_id );
  88.                             array_push( $tobe, $item_id );
  89.                             $flag_send_mail = true;
  90.                             $new_item_id = $item_id;
  91.                         }
  92.                     }
  93.                     error_log('user id ');
  94.                     error_log( $user_id );
  95.                     error_log('tobe');
  96.                     error_log( print_r($tobe, 1) );
  97.                     update_user_meta( $user_id, 'consolidation_tobe', $tobe );
  98.  
  99.                     wpf_log( 'notice', $user_id, 'DEBUG: consolidation_tobe updated.' );
  100.  
  101.                     // send email for newly available items, if the user has 'member_basic' tag.
  102.                     if ( $flag_send_mail && wp_fusion()->user->has_tag( 'member_basic', $user_id ) ) {
  103.                         $emails_setting = get_option( 'consolidation_emails' );
  104.                         if ( isset( $emails_setting[ 'item_available' ] ) ) {
  105.                             $sub    = $emails_setting[ 'item_available' ][ 'sub' ];
  106.                             $body   = $emails_setting[ 'item_available' ][ 'body' ];
  107.  
  108.                             // {nickname}
  109.                             $userdata = get_userdata( $user_id );
  110.                             $nickname = ( ! empty( $userdata->first_name ) ) ? $userdata->first_name : $userdata->user_login;
  111.  
  112.                             // {item_type}
  113.                             $items = get_option( 'consolidation_items' );
  114.                             $item_slug = get_post_meta( $new_item_id, 'item_type', true );
  115.                             $item_type = @$items[ $item_slug ]['name'];
  116.  
  117.                             $task   = get_post( $new_item_id );
  118.  
  119.                             // {task_title}
  120.                             $task_title = $task->post_title;
  121.  
  122.                             // {task_desc}
  123.                             // $task_desc =  apply_filters( 'the_content', $task->post_content );
  124.                             $task_desc =  $task->post_content;
  125.  
  126.                             $search = array( '{nickname}', '{task_title}', '{item_type}', '{task_desc}' );
  127.                             $replace = array( $nickname, $task_title, $item_type, $task_desc );
  128.  
  129.                             $sub  = str_replace( $search, $replace, $sub );
  130.                             $body = do_shortcode( str_replace( $search, $replace, $body ) );
  131.  
  132.                             $headers = array('Content-Type: text/html; charset=UTF-8');
  133.  
  134.                             wpf_log( 'notice', $user_id, 'DEBUG: sending mail.' );
  135.                             wp_mail( $userdata->user_email, $sub, $body, $headers );
  136.                             // $this->send_email_via_intercom( $user_id, $sub, $body );
  137.                         }
  138.                     }
  139.                 }
  140.             }
  141.         }
  142.  
  143.         /**
  144.          * DEPRECATED.
  145.          */
  146.         private function send_email_via_intercom( $user_id, $subject, $body ) {
  147.  
  148.             error_log( 'Sending intercom email to user: ' . $user_id );
  149.             // $ch_user = get_user_by( 'email', 'marlonsabala@gmail.com' );
  150.  
  151.             $intercom_id = get_user_meta( $user_id, 'wpf_intercom_contact_id', true );
  152.  
  153.             error_log( 'Intercom ID is: ' . $intercom_id );
  154.  
  155.             if ( $intercom_id ) {
  156.                 $ch = curl_init();
  157.  
  158.                 curl_setopt( $ch, CURLOPT_URL, 'https://api.intercom.io/messages' );
  159.                 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
  160.                 curl_setopt( $ch, CURLOPT_POST, 1 );
  161.                 curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode(
  162.                     array(
  163.                         "type"          => "admin",
  164.                         "message_type"  => "email",
  165.                         "subject"       => $subject,
  166.                         "body"          => $body,
  167.                         "template"      => "personal",
  168.                         "from"          => array( "type" => "admin", "id" => "1425743" ),
  169.                         "to"            => array( "type" => "user", "id" => $intercom_id ),
  170.                     )
  171.                 ));
  172.  
  173.                 $headers = array();
  174.                 $headers[] = 'Authorization: Bearer dG9rOjU1YmMwYTAyX2MzNDhfNGQ1YV9iODBhX2E2NGM5ZTUzNzQ4ZDoxOjA=';
  175.                 $headers[] = 'Accept: application/json';
  176.                 $headers[] = 'Content-Type: application/json';
  177.                 curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  178.  
  179.                 $result = curl_exec( $ch );
  180.  
  181.                 error_log( 'Intercom API response: ' . print_r( $result, 1 ) );
  182.  
  183.                 if ( curl_errno( $ch ) ) {
  184.                     error_log( 'Error:' . curl_error( $ch ) );
  185.                 }
  186.                 curl_close( $ch );
  187.             }
  188.         }
  189.     }
  190.  
  191.     apply_tag_trigger::get_instance();
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement