Skip to content

Commit 7cd3d96

Browse files
authored
Forms: Create a new page instead of post when creating a new form from dashboard (#43668)
* create page instead of post * check page editing capability and respond with errors * changelog
1 parent cd48bb4 commit 7cd3d96

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-16
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: changed
3+
4+
Forms: Create a new page instead of post when creating a new form from dashboard

projects/packages/forms/src/contact-form/class-contact-form-plugin.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,11 +1965,21 @@ public function download_feedback_as_csv() {
19651965
}
19661966

19671967
/**
1968-
* Create a new post with a Form block
1968+
* Create a new page with a Form block
19691969
*/
19701970
public function create_new_form() {
19711971
if ( ! isset( $_POST['newFormNonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['newFormNonce'] ) ), 'create_new_form' ) ) {
1972-
wp_die( esc_html__( 'Invalid nonce', 'jetpack-forms' ) );
1972+
wp_send_json_error(
1973+
__( 'Invalid nonce', 'jetpack-forms' ),
1974+
403
1975+
);
1976+
}
1977+
1978+
if ( ! current_user_can( 'edit_pages' ) ) {
1979+
wp_send_json_error(
1980+
__( 'You do not have permission to create pages', 'jetpack-forms' ),
1981+
403
1982+
);
19731983
}
19741984

19751985
$pattern_name = isset( $_POST['pattern'] ) ? sanitize_text_field( wp_unslash( $_POST['pattern'] ) ) : null;
@@ -1988,20 +1998,24 @@ public function create_new_form() {
19881998

19891999
$post_id = wp_insert_post(
19902000
array(
2001+
'post_type' => 'page',
19912002
'post_title' => esc_html__( 'Jetpack Forms', 'jetpack-forms' ),
19922003
'post_content' => $pattern_content,
19932004
)
19942005
);
19952006

1996-
if ( ! is_wp_error( $post_id ) ) {
1997-
$array_result = array(
1998-
'post_url' => admin_url( 'post.php?post=' . intval( $post_id ) . '&action=edit' ),
2007+
if ( is_wp_error( $post_id ) ) {
2008+
wp_send_json_error(
2009+
$post_id->get_error_message(),
2010+
500
2011+
);
2012+
} else {
2013+
wp_send_json(
2014+
array(
2015+
'post_url' => admin_url( 'post.php?post=' . intval( $post_id ) . '&action=edit' ),
2016+
)
19992017
);
2000-
2001-
wp_send_json( $array_result );
20022018
}
2003-
2004-
wp_die();
20052019
}
20062020

20072021
/**

projects/packages/forms/src/dashboard/hooks/use-create-form.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,34 @@ export default function useCreateForm(): CreateFormReturn {
3535
}
3636

3737
const response = await fetch( window.ajaxurl, { method: 'POST', body: data } );
38-
const { post_url: postUrl }: { post_url: string } = await response.json();
38+
39+
const {
40+
success,
41+
post_url: postUrl,
42+
data: message,
43+
}: { success?: boolean; data?: string; post_url?: string } = await response.json();
44+
45+
if ( success === false ) {
46+
throw new Error( message );
47+
}
3948

4049
return postUrl;
4150
}, [] );
4251

4352
const openNewForm = useCallback(
4453
async ( { formPattern, showPatterns, analyticsEvent }: ClickHandlerProps ) => {
45-
const postUrl = await createForm( formPattern );
54+
try {
55+
const postUrl = await createForm( formPattern );
4656

47-
if ( postUrl ) {
48-
analyticsEvent?.( { formPattern } );
57+
if ( postUrl ) {
58+
analyticsEvent?.( { formPattern } );
4959

50-
window.open(
51-
`${ postUrl }${ showPatterns && ! formPattern ? '&showJetpackFormsPatterns' : '' }`
52-
);
60+
window.open(
61+
`${ postUrl }${ showPatterns && ! formPattern ? '&showJetpackFormsPatterns' : '' }`
62+
);
63+
}
64+
} catch ( error ) {
65+
console.error( error.message ); // eslint-disable-line no-console
5366
}
5467
},
5568
[ createForm ]

0 commit comments

Comments
 (0)