Form_Edit_Page_Handler::action_duplicate_form()

Handles the duplicate form action.

Description

Duplicates the form and redirects back to the referer URL.

See also

Source

File: src/db-objects/forms/form-edit-page-handler.php

	public function action_duplicate_form() {
		if ( ! isset( $_REQUEST['form_id'] ) ) {
			wp_die( __( 'Missing form ID.', 'torro-forms' ), '', 400 );
		}

		if ( ! isset( $_REQUEST['_wpnonce'] ) ) {
			wp_die( __( 'Missing nonce.', 'torro-forms' ), '', 400 );
		}

		$form_id = (int) $_REQUEST['form_id'];

		if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], $this->form_manager->get_prefix() . 'duplicate_form_' . $form_id ) ) {
			wp_die( __( 'Invalid nonce.', 'torro-forms' ), '', 403 );
		}

		$form = $this->form_manager->get( $form_id );
		if ( ! $form ) {
			wp_die( __( 'Invalid form ID.', 'torro-forms' ), '', 404 );
		}

		$new_form = $form->duplicate();
		if ( is_wp_error( $new_form ) ) {
			$feedback = array(
				'type'     => 'error',
				/* translators: 1: form title, 2: error message */
				'message' => sprintf( __( 'The form “%1$s” could not be duplicated: %2$s', 'torro-forms' ), $form->title, $new_form->get_error_message() ),
			);
		} else {
			$feedback = array(
				'type'     => 'success',
				/* translators: 1: form title, 2: new form edit URL */
				'message' => sprintf( __( 'The form &#8220;%1$s&#8221; was duplicated successfully. <a href="%2$s">View the duplicate</a>', 'torro-forms' ), $form->title, get_edit_post_link( $new_form->id ) ),
			);
		}

		$meta_key = $this->form_manager->get_prefix() . 'duplicate_feedback';

		$this->form_manager->update_meta( $form->id, $meta_key, $feedback );

		$redirect_url = add_query_arg( $meta_key, $form->id, wp_get_referer() );

		wp_redirect( $redirect_url );
		exit;
	}

Changelog

Changelog
Version Description
1.0.0 Introduced.