Last active
March 8, 2021 09:23
-
-
Save wpmudev-sls/b5803d4000d5ee2437731a0ade6dcdec to your computer and use it in GitHub Desktop.
Support upload field type for custom field of Post Data. Note, this MU plugin required the upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Forminator Pro] - Support upload field type for custom field of Post Data | |
* Description: [Forminator Pro] - Support upload field type for custom field of Post Data. Note, this MU plugin required the upload field that set before the post data field. Also, you should enable show files in media library. | |
* Author: Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
return; | |
} | |
/** | |
* Screenshots: | |
* https://share.getcloudapp.com/KouZqQOn | |
* https://share.getcloudapp.com/7KupwDJz/ | |
*/ | |
add_action( 'after_setup_theme', 'wpmudev_fm_support_upload_field_type_for_custom_field_func', 100 ); | |
function wpmudev_fm_support_upload_field_type_for_custom_field_func() { | |
if ( defined('FORMINATOR_PRO') && class_exists( 'Forminator' ) ) { | |
class WPMUDEV_FM_U4CF{ | |
private $upload_datas = []; | |
private $custom_upload_fields = []; | |
private $save_to_lib = 0; | |
public function __construct(){ | |
add_filter( 'forminator_handle_specific_field_types', array( $this, 'cache_upload_file_datas' ), 10, 3 ); | |
if( class_exists('acf') ){ | |
add_action( 'forminator_post_data_field_post_saved', array( $this, 'update_upload_field_meta' ), 10, 3 ); | |
} | |
} | |
public function cache_upload_file_datas( $field_data, $form_field_obj, $field_array ){ | |
if( isset( $field_array['type'] ) ){ | |
if( $field_array['type'] === 'upload' && isset( $field_data['file']['file_url'] ) ){ | |
$this->upload_datas[ $field_array['element_id'] ] = $field_data['file']['file_url']; | |
$this->save_to_lib = ! empty( $field_array['use_library'] ); | |
}elseif( $field_array['type'] === 'postdata' ){ | |
if( ! empty( $field_array['options'] ) ){ | |
foreach( $field_array['options'] as $meta ){ | |
if( ! empty( $meta['value'] ) && strpos( $meta['value'], '{upload-') === 0 ){ | |
$upload_field = trim( $meta['value'], ' {}' ); | |
if( isset( $this->upload_datas[ $upload_field ] ) ){ | |
$this->custom_upload_fields[ $meta['label'] ] = $this->upload_datas[ $upload_field ]; | |
} | |
} | |
} | |
if( $this->custom_upload_fields ){ | |
foreach( $field_data['post-custom'] as $id => $meta ){ | |
if( isset( $this->custom_upload_fields[ $meta['key'] ] ) ){ | |
$field_data['post-custom'][ $id ]['value'] = $this->custom_upload_fields[ $meta['key'] ]; | |
} | |
} | |
} | |
} | |
} | |
} | |
return $field_data; | |
} | |
public function update_upload_field_meta( $post_id, $field, $data ){ | |
if( ! empty( $data['post-custom'] ) && $this->save_to_lib ){ | |
foreach( $data['post-custom'] as $custom_field ){ | |
if( isset( $custom_field['key'] ) && false !== strpos( $custom_field['key'], 'upload-' ) ){ | |
$file = get_field( $custom_field['key'], $post_id ); | |
if( $file ){ | |
$file_id = attachment_url_to_postid( $file ); | |
if( $file_id ){ | |
update_field( $custom_field['key'], $file_id, $post_id ); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
$run = new WPMUDEV_FM_U4CF; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment