indikatordesign

KK Form Submission to Testimonial Rotator

Apr 15th, 2017
588
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Snippet to automatically create a new testimonial for the Testimonial Rotator with the submitted data of a KK CUSTOM FORM/CRM - Form
  2. // KK CUSTOM FORM/CRM: https://elegantmarketplace.com/downloads/kk-custom-formcrm/
  3. // Testimonial Rotator: https://wordpress.org/plugins/testimonial-rotator/
  4.  
  5. add_action( 'kk_cust_form_cap', function( $fFields, $fArgs, $fID )
  6. {
  7.  
  8.     if ( 'testimonials' == $fID )
  9.     {
  10.  
  11.         $host    = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . '/';
  12.  
  13.         $imgURL  = ( new SimpleXMLElement( $fArgs['meta']['IMAGE'] ) )['href'];
  14.  
  15.         $imgPath = str_replace( $host, '', ABSPATH . $imgURL );
  16.  
  17.         $type    = wp_check_filetype( basename( $imgPath ), null );
  18.  
  19.         $atts    =
  20.         [
  21.  
  22.             'guid'           => $imgURL,
  23.             'post_mime_type' => wp_check_filetype( basename( $imgPath ), null )['type'],
  24.             'post_title'     => $fFields['FIRSTNAME'] . ' ' . $fFields['LASTNAME'],
  25.             'post_content'   => ( $cont = 'Testimonial image from ' . $fFields['FIRSTNAME'] . ' ' . $fFields['LASTNAME'] ),
  26.             'post_excerpt'   => $cont,
  27.             'post_status'    => 'inherit',
  28.  
  29.         ];
  30.  
  31.         $attID = wp_insert_attachment( $atts, $imgPath );
  32.  
  33.         $alt   = $fFields['FIRSTNAME'] . ' ' . $fFields['LASTNAME'] . ' - Testimonial Image';
  34.  
  35.         update_post_meta( $attID, '_wp_attachment_image_alt', $alt );
  36.  
  37.         $name  = '<p>' . $fFields['FIRSTNAME'] . ' ' . $fFields['LASTNAME'] . '</p>';
  38.  
  39.         if ( $fFields['WEBSITE'] && $fFields['COMPANY'] )
  40.         {
  41.  
  42.             $link = '<a href="' . $fFields['WEBSITE'] . '" target="_blank">' . '<p>' . $fFields['COMPANY'] . '</p>' . '</a>';
  43.  
  44.         } // end if
  45.  
  46.         else if ( $fFields['WEBSITE'] && empty( $fFields['COMPANY'] ) )
  47.         {
  48.  
  49.             $link = '<a href="' . $fFields['WEBSITE'] . '" target="_blank">' . $name . '</a>';
  50.  
  51.         } // end else if
  52.  
  53.         if ( isset( $link ) && $fFields['COMPANY'] )
  54.         {
  55.  
  56.             $cite = $link . $name;
  57.  
  58.         } // end if
  59.  
  60.         else if ( isset( $link ) && empty( $fFields['COMPANY'] ) )
  61.         {
  62.  
  63.             $cite = $link;
  64.  
  65.         } // end else if
  66.  
  67.         else
  68.         {
  69.  
  70.             $cite = $name;
  71.  
  72.         } // end else
  73.  
  74.         // 1 is a placeholder for the user-id of an anonymous user. Replace it as needed
  75.         $user = is_user_logged_in() ? wp_get_current_user()->ID : ( ( $id = get_user_by( 'email', $fFields['EMAIL'] )->ID ) ? $id : 1 );
  76.  
  77.         $args =
  78.         [
  79.  
  80.             'post_author'    => $user,
  81.             'post_content'   => $fFields['CONTENT'],
  82.             'post_title'     => $fFields['TITLE'],
  83.             'post_status'    => 'draft',
  84.             'comment_status' => 'closed',
  85.             'post_type'      => 'testimonial',
  86.             'meta_input'     =>
  87.             [
  88.  
  89.                 '_thumbnail_id' => $attID,
  90.                 '_rotator_id'   => '|' . $fFields['ROTATOR'] . '|',
  91.                 '_rating'       => $fFields['RATING'],
  92.  
  93.             ],
  94.         ];
  95.  
  96.         $postID = wp_insert_post( $args );
  97.  
  98.         $cite   = wp_kses( $cite, wp_kses_allowed_html( 'post' ) );
  99.  
  100.         update_post_meta( $postID, '_cite', $cite );
  101.  
  102.         require_once( ABSPATH . 'wp-admin/includes/image.php' );
  103.  
  104.         $attData = wp_generate_attachment_metadata( $attID, $imgPath );
  105.  
  106.         wp_update_attachment_metadata( $attID, $attData );
  107.  
  108.         set_post_thumbnail( $postID, $attID );
  109.  
  110.     } // end if
  111.  
  112. }, 10, 3 ); // end add_action
RAW Paste Data