Guest User

Untitled

a guest
Dec 10th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.06 KB | None | 0 0
  1. class JDN_Create_Media_File {
  2.  
  3.     var $post_id;
  4.     var $image_url;
  5.     var $wp_upload_url;
  6.     var $attachment_id;
  7.  
  8.     /**
  9.      * Setup the class variables
  10.      */
  11.     public function __construct( $image_url, $post_id = 0 ) {
  12.  
  13.         // Setup class variables
  14.         $this->image_url = esc_url( $image_url );
  15.         $this->post_id = absint( $post_id );
  16.         $this->wp_upload_url = $this->get_wp_upload_url();
  17.         $this->attachment_id = $this->attachment_id ?: false;
  18.  
  19.         return $this->create_image_id();
  20.  
  21.     }
  22.  
  23.     /**
  24.      * Set the upload directory
  25.      */
  26.     private function get_wp_upload_url() {
  27.         $wp_upload_dir = wp_upload_dir();
  28.         return isset( $wp_upload_dir['url'] ) ? $wp_upload_dir['url'] : false;
  29.     }
  30.  
  31.     /**
  32.      * Create the image and return the new media upload id.
  33.      *
  34.      * @see https://gist.github.com/hissy/7352933
  35.      *
  36.      * @see http://codex.wordpress.org/Function_Reference/wp_insert_attachment#Example
  37.      */
  38.     public function create_image_id() {
  39.  
  40.         if( $this->attachment_id )
  41.             return $this->attachment_id;
  42.  
  43.         if( empty( $this->image_url ) || empty( $this->wp_upload_url ) )
  44.             return false;
  45.  
  46.         $filename = basename( $this->image_url );
  47.  
  48.         $upload_file = wp_upload_bits( $filename, null, file_get_contents( $this->image_url ) );
  49.  
  50.         if ( ! $upload_file['error'] ) {
  51.             $wp_filetype = wp_check_filetype( $filename, null );
  52.             $attachment = array(
  53.                 'post_mime_type' => $wp_filetype['type'],
  54.                 'post_parent' => $this->post_id,
  55.                 'post_title' => preg_replace('/\.[^.]+$/', '', $filename),
  56.                 'post_content' => '',
  57.                 'post_status' => 'inherit'
  58.             );
  59.             $attachment_id = wp_insert_attachment( $attachment, $upload_file['file'], $this->post_id );
  60.  
  61.             if( ! is_wp_error( $attachment_id ) ) {
  62.  
  63.                 require_once( ABSPATH . "wp-admin" . '/includes/image.php' );
  64.                 require_once( ABSPATH . 'wp-admin/includes/media.php' );
  65.  
  66.                 $attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );
  67.                 wp_update_attachment_metadata( $attachment_id, $attachment_data );
  68.  
  69.                 $this->attachment_id = $attachment_id;
  70.  
  71.                 return $attachment_id;
  72.             }
  73.         }
  74.  
  75.         return false;
  76.  
  77.     } // end function get_image_id
  78. }
  79.  
  80.  
  81. $gravity_form_id = 1; // gravity form id, or replace {$gravity_form_id} below with this number
  82. add_filter( "gform_after_submission_1", 'jdn_set_post_acf_gallery_field', 10, 2 );
  83. function jdn_set_post_acf_gallery_field( $entry, $form ) {
  84.  
  85.     $gf_images_field_id = 3; // the upload field id
  86.     $acf_field_id = 'field_5de3b8157dffc'; // the acf gallery field id
  87.  
  88.     // get post, if there isn't one, bail
  89.     if( isset( $entry['post_id'] ) ) {
  90.         $post = get_post( $entry['post_id'] );
  91.         if( is_null( $post ) )
  92.             return;
  93.     } else {
  94.         return;
  95.     }
  96.  
  97.     // Clean up images upload and create array for gallery field
  98.     if( isset( $entry[ $gf_images_field_id ] ) ) {
  99.         $images = stripslashes( $entry[ $gf_images_field_id ] );
  100.         $images = json_decode( $images, true );
  101.         if( !empty( $images ) && is_array( $images ) ) {
  102.             $gallery = array();
  103.             foreach( $images as $key => $value ) {
  104.                 // NOTE: this is the other function you need: https://gist.github.com/joshuadavidnelson/164a0a0744f0693d5746
  105.                 if( ! class_exists( 'JDN_Create_Media_File' ) )
  106.                     break;
  107.  
  108.                 // Create the media library attachment and store the attachment id in the gallery array
  109.                 $create_image = new JDN_Create_Media_File( $value, $post->ID );
  110.                 $image_id = $create_image->attachment_id;
  111.                 if( absint( $image_id ) ) {
  112.                     $gallery[] = $image_id;
  113.                 }
  114.             }
  115.         }
  116.     }
  117.  
  118.     update_field( $acf_field_id, $gallery, $post->ID );
  119. }
Advertisement
Add Comment
Please, Sign In to add comment