php 重力表单过滤器,用于处理与WP Offload S3 Lite插件的文件上载集成。改编自https://wordpress.org/support/topic/suppo

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 重力表单过滤器,用于处理与WP Offload S3 Lite插件的文件上载集成。改编自https://wordpress.org/support/topic/suppo相关的知识,希望对你有一定的参考价值。

<?php 

include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); 
require_once WP_CONTENT_DIR.'/plugins/amazon-web-services/vendor/aws/aws-autoloader.php';
use \Aws\S3\S3Client;

add_filter('gform_upload_path', 'gform_change_upload_path', 10, 2);
add_action('gform_after_submission', 'gform_submit_to_s3', 10, 2);

//change gravity form upload path to point to S3
function gform_change_upload_path($path_info, $form_id)
{
	$bucket_info = get_option('tantan_wordpress_s3');
	$as3cf_is_active = is_plugin_active('amazon-s3-and-cloudfront/wordpress-s3.php');

	if(!empty($bucket_info['bucket']) && $as3cf_is_active )
	{
		$gform_link = explode('wp-content/uploads/', $path_info["url"]);
		$domain = $bucket_info['bucket'];

		if($bucket_info['domain'] == 'subdomain')
		{
			$domain = $bucket_info['bucket'].'.s3.amazonaws.com';
		}
		else if($bucket_info['domain'] == 'path')
		{
			$domain = 's3.amazonaws.com/'.$bucket_info['bucket'];
		}
		else if($bucket_info['domain'] == 'cloudfront')
		{
			$domain = $bucket_info['cloudfront'];
		}

		$protocol = ($bucket_info['ssl'] !== 'request' ? $bucket_info['ssl'] : 'http'.(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 's' : ''));
		$path_info["url"] = (strpos($domain, 'http') === false ? $protocol : '')."://".$domain.'/'.$gform_link[1];
	}

	return $path_info;
}

//submit file to s3
function gform_submit_to_s3($entry, $form)
{
	$bucket_info = get_option('tantan_wordpress_s3');
	$as3cf_is_active = is_plugin_active('amazon-s3-and-cloudfront/wordpress-s3.php');

	if(!empty($form['fields']) && !empty($bucket_info['bucket']) && $as3cf_is_active )
	{
		$s3Client =S3Client::factory(array(
		    'key'    => AWS_ACCESS_KEY_ID, 
		    'secret' => AWS_SECRET_ACCESS_KEY
		));

		foreach ($form['fields'] as $field)
		{
			if($field->type == 'fileupload' && !empty($entry[$field->id]))
			{
				$gform_link = explode('/gravity_forms/', $entry[$field->id]);
				$upload_dir = wp_upload_dir();
				$file_url = $upload_dir['baseurl'].'/gravity_forms/'.$gform_link[1];
				$url_parts = parse_url( $file_url );
				$full_path = $_SERVER['DOCUMENT_ROOT'] . $url_parts['path'];

				$s3Client->putObject(array(
					 'Bucket'           => $bucket_info['bucket'],
					    'Key'          => 'gravity_forms/'.$gform_link[1],
					    'SourceFile'   => $full_path,
					    'ACL'          => 'public-read',
					));
			}
		}
	}
}

?>

以上是关于php 重力表单过滤器,用于处理与WP Offload S3 Lite插件的文件上载集成。改编自https://wordpress.org/support/topic/suppo的主要内容,如果未能解决你的问题,请参考以下文章

WP重力形式 - 添加范围滑块

php 将表单名称作为数据属性添加到重力表单表单元素

将提交时的重力表单重定向到刚刚创建的帖子

php 重力Wiz //重力形式//使用条目填充表单(可选择在提交时更新条目)

php 重力表单启用密码字段

php 更改重力表单的默认提交按钮