php Gravity Perks // GP媒体库// Ajax上传

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php Gravity Perks // GP媒体库// Ajax上传相关的知识,希望对你有一定的参考价值。

<?php
/**
 * Gravity Perks // GP Media Library // Ajax Upload
 *
 * Upload images to the Media Library as they are uploaded via the Gravity Forms Multi-file Upload field.
 *
 * @version  0.1
 * @author   David Smith <david@gravitywiz.com>
 * @license  GPL-2.0+
 * @link     http://gravitywiz.com/documentation/gravity-forms-media-library/
 *
 * Plugin Name:  GP Media Library - Ajax Upload
 * Plugin URI:   http://gravitywiz.com/documentation/gravity-forms-media-library/
 * Description:  Upload images to the Media Library as they are uploaded via the Gravity Forms Multi-file Upload field.
 * Author:       Gravity Wiz
 * Version:      0.1
 * Author URI:   http://gravitywiz.com/
 */
class GPML_Ajax_Upload {

    public function __construct( $args = array() ) {

        $this->_args = wp_parse_args( $args, array(
            'default_entry_id' => 1,
        ) );

        add_action( 'init', array( $this, 'init' ), 11 );

    }

    public function init() {

	    if( ! is_callable( 'gp_media_library' ) ) {
		    return;
	    }

	    add_action( 'gform_post_multifile_upload', array( $this, 'upload' ), 10, 5 );

	    remove_action( 'gform_entry_post_save', array( gp_media_library(), 'maybe_upload_to_media_library' ) );
	    add_filter( 'gform_entry_post_save', array( $this, 'update_entry_field_values' ), 10, 2 );

    }

    public function upload( $form, $field, $uploaded_filename, $tmp_file_name, $file_path ) {

	    if( ! gp_media_library()->is_applicable_field( $field ) ) {
		    return;
	    }

	    $field_uid = implode( '_', array( rgpost( 'gform_unique_id' ), $field->id ) );

	    $renamed_file_path = str_replace( basename( $file_path ), $uploaded_filename, $file_path );
		rename( $file_path, $renamed_file_path );

	    $id = gp_media_library()->upload_to_media_library( $renamed_file_path, $field, array( 'id' => $this->_args['default_entry_id'] ) );
	    if( is_wp_error( $id ) ) {
		    return;
	    }

	    // Get file IDs.
	    $ids = gform_get_meta( $this->_args['default_entry_id'], sprintf( 'gpml_ids_%s', $field_uid ) );

	    if( ! is_array( $ids ) ) {
		    $ids = array();
	    }

	    $ids[] = $id[0];

	    // Update file IDs.
	    gform_update_meta( $this->_args['default_entry_id'], sprintf( 'gpml_ids_%s', $field_uid ), $ids );

    }

    public function update_entry_field_values( $entry, $form ) {

	    foreach( $form['fields'] as $field ) {

		    if( $field->get_input_type() != 'fileupload' || ! $field->multipleFiles ) {
			    continue;
		    }

		    $field_uid = implode( '_', array( rgpost( 'gform_unique_id' ), $field->id ) );
		    $ids = gform_get_meta( $this->_args['default_entry_id'], sprintf( 'gpml_ids_%s', $field_uid ) );
		    if( empty( $ids ) ) {
		    	continue;
		    }

		    $new_value = array();

		    foreach( $ids as $id ) {
			    if( ! is_wp_error( $id ) ) {
				    $new_value[] = wp_get_attachment_url( $id );
			    }
		    }

		    $entry[ $field->id ] = json_encode( $new_value );

		    // Save our changes to the DB for this entry.
		    GFAPI::update_entry_field( $entry['id'], $field->id, $entry[ $field->id ] );

		    // Delete temporary file ID meta.
		    gform_delete_meta( $this->_args['default_entry_id'], sprintf( 'gpml_ids_%s', $field_uid ) );

		    // Save the file ID meta to the actual entry/field.
		    gp_media_library()->update_file_ids( $entry['id'], $field->id, $ids );

	    }

	    return $entry;
    }

}

# Configuration

new GPML_Ajax_Upload();

以上是关于php Gravity Perks // GP媒体库// Ajax上传的主要内容,如果未能解决你的问题,请参考以下文章

php Gravity Perks // GP条件定价//显示价格标签

php Gravity Perks // GP限制提交//集体应用全局限制

php Gravity Perks // GP电子商务领域//动态设置税额

php Gravity Perks // GP嵌套表单//从值隐藏产品名称

php Gravity Perks // GP通知计划程序//信用卡到期

php Gravity Perks // GP唯一ID //从Gravity PDF中排除唯一ID字段