Form::duplicate( bool $as_draft = true, string $new_title = '' )

Duplicates the form including all of its contents (except submissions).

Description

See also

Parameters

$as_draft

(bool) (Optional) Whether to set the new form post to 'draft' status initially.

Default value: true

$new_title

(string) (Optional) New form title. Default is the original title prefixed with 'Copy of '.

Default value: ''

Return

(awsmug\Torro_Forms\DB_Objects\Forms\Form|WP_Error) New form object on success, error object on failure.

Source

File: src/db-objects/forms/form.php

	public function duplicate( $as_draft = true, $new_title = '' ) {
		if ( empty( $this->original->ID ) ) {
			return new WP_Error( 'form_post_not_exist', __( 'The form post does not exist in the database.', 'torro-forms' ) );
		}

		$post_data = get_post( $this->original->ID, ARRAY_A );
		if ( ! $post_data ) {
			return new WP_Error( 'form_post_not_exist', __( 'The form post does not exist in the database.', 'torro-forms' ) );
		}

		$post_data['ID'] = '';
		$post_data['post_date'] = $post_data['post_modified'] = $post_data['post_date_gmt'] = $post_data['post_modified_gmt'] = '';

		if ( ! empty( $new_title ) ) {
			$post_data['post_title'] = $new_title;
		} else {
			/* translators: %s: original form title */
			$post_data['post_title'] = sprintf( _x( 'Copy of %s', 'duplicated form title', 'torro-forms' ), $post_data['post_title'] );
		}

		unset( $post_data['post_name'] );

		if ( ! $as_draft ) {
			$post_data['post_status'] = 'publish';
		} else {
			$post_data['post_status'] = 'draft';
		}

		$new_form_id = wp_insert_post( $post_data, true );
		if ( is_wp_error( $new_form_id ) ) {
			return $new_form_id;
		}

		$this->duplicate_terms_for_form( $new_form_id );
		$this->duplicate_metadata_for_form( $new_form_id );
		$this->duplicate_comments_for_form( $new_form_id );

		foreach ( $this->get_containers() as $container ) {
			$container->duplicate( $new_form_id );
		}

		return $this->manager->get( $new_form_id );
	}

Changelog

Changelog
Version Description
1.0.0 Introduced.