diff --git a/gp-file-upload-pro/gpfup-update-filename-markup.php b/gp-file-upload-pro/gpfup-update-filename-markup.php new file mode 100644 index 00000000..d8df89c3 --- /dev/null +++ b/gp-file-upload-pro/gpfup-update-filename-markup.php @@ -0,0 +1,188 @@ +_args = wp_parse_args( $args, array( + 'form_id' => false, + 'field_id' => false, + ) ); + + // Do not proceed if Gravity Forms is not installed & activated. + if ( ! class_exists( 'GFCommon' ) ) { + return; + } + + add_action( 'init', array( $this, 'init' ) ); + } + + public function init() { + $form_id = $this->_args['form_id']; + $field_id = $this->_args['field_id']; + + // Time for hooks. + add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 ); + add_action( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 ); + + $args = array( 'gpeb_post_file_population' ); + + // If a form ID is provided, add it to the args. + if ( $form_id ) { + $args[] = $form_id; + + // If a field ID is provided, add it to the args. + if ( $field_id ) { + $args[] = $field_id; + } + } + + add_action( implode( '_', $args ), array( + $this, + 'populate_file_data', + ), 10, 3 ); + } + + /** + * Populate file data. + * + * @param $file_upload_data + * @param $form + * @param $field + * + * @return void + */ + public function populate_file_data( $file_upload_data, $form, $field ) { + add_action( 'wp_print_footer_scripts', function () use ( $file_upload_data, $form, $field ) { + $form_id = rgar( $form, 'id' ); + $field_id = rgar( $field, 'id' ); + + echo '"; + } ); + } + + public function load_form_script( $form, $is_ajax_enabled ) { + if ( ! $this->is_applicable_form( $form ) ) { + return $form; + } + + if ( ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) { + add_action( 'wp_footer', array( $this, 'output_script' ) ); + } + + if ( ! has_action( 'gform_preview_footer', array( $this, 'output_script' ) ) ) { + add_action( 'gform_preview_footer', array( $this, 'output_script' ) ); + } + + return $form; + } + + public function output_script() { + ?> + + + + is_applicable_form( $form ) ) { + return; + } + + $args = array( + 'formId' => $this->_args['form_id'], + 'fieldId' => $this->_args['field_id'], + ); + + $script = 'new ' . __CLASS__ . '( ' . wp_json_encode( $args ) . ' );'; + $slug = implode( '_', array( strtolower( __CLASS__ ), $this->_args['form_id'], $this->_args['field_id'] ) ); + + GFFormDisplay::add_init_script( $form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script ); + } + + public function is_applicable_form( $form ) { + $form_id = isset( $form['id'] ) ? $form['id'] : $form; + + return empty( $this->_args['form_id'] ) || (int) $form_id === (int) $this->_args['form_id']; + } +} + +// Usage instructions. + +/* + * Example Usage (for demonstration purposes only): + * + * - Update 'form_id' and 'field_id' to match your specific requirements. + */ +new GPFUP_Update_Filename_Markup( + array( + 'form_id' => 33, + 'field_id' => 4, // Update to your file upload field ID. + ) +);