Skip to content

Commit 272dffb

Browse files
committed
Improved Pdf.Ninja API settings screen
- added a user-provided email address field for requesting a new key from the API - fixed a minor error reporting bug when requesting a new key from the API fails
1 parent cdfd5d3 commit 272dffb

File tree

2 files changed

+92
-49
lines changed

2 files changed

+92
-49
lines changed

html/pdfninja_integration_edit.html

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<p>{top-message}</p>
1+
<p>{top-message-api-settings}</p>
22

33
<form method="post" action="{edit-link}">
44
{nonce}
@@ -38,7 +38,23 @@
3838
<p>{security-warning}</p>
3939

4040
<p class="submit">
41-
<input type="submit" class="button button-primary" value="{save-label}" name="submit" />
41+
<input type="submit" class="button button-primary" value="{save-label}" name="save" />
42+
</p>
43+
</form>
44+
45+
<p>{top-message-new-key}</p>
46+
47+
<form method="post" action="{edit-link}">
48+
{nonce}
49+
<table class="form-table">
50+
<tbody>
51+
<tr>
52+
<th scope="row"><label for="email">{email-label}</label></th>
53+
<td><input type="text" value="{email-value}" id="email" name="email" class="regular-text code" /></td>
54+
</tr>
55+
</tbody>
56+
</table>
57+
<p class="submit">
4258
<input type="submit" class="button button-primary" value="{new-label}" name="new" />
4359
</p>
4460
</form>

modules/pdf-ninja.php

Lines changed: 74 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,18 @@ public function get_key()
9494
if( $fail )
9595
throw new Exception( __( "Failed to get the Pdf.Ninja API key on last attempt. Please retry manually.", 'pdf-forms-for-contact-form-7' ) );
9696

97-
$this->set_key( $this->generate_key() );
97+
// create new key if it hasn't yet been set
98+
try
99+
{
100+
$key = $this->generate_key();
101+
}
102+
catch(Exception $e)
103+
{
104+
set_transient( 'wpcf7_pdf_forms_pdfninja_key_failure', true, 12 * HOUR_IN_SECONDS );
105+
throw $e;
106+
}
107+
108+
$this->set_key( $key );
98109
}
99110

100111
return $this->key;
@@ -111,50 +122,56 @@ public function set_key( $value )
111122
return true;
112123
}
113124

125+
/*
126+
* Determines administrator's email address (for use with requesting a new key from the API)
127+
*/
128+
private function get_admin_email()
129+
{
130+
$current_user = wp_get_current_user();
131+
if( ! $current_user )
132+
return null;
133+
134+
$email = sanitize_email( $current_user->user_email );
135+
if( ! $email )
136+
return null;
137+
138+
return $email;
139+
}
140+
114141
/*
115142
* Requests a key from the API server
116143
*/
117-
public function generate_key()
144+
public function generate_key( $email = null )
118145
{
119-
try
146+
if( $email === null )
147+
$email = get_admin_email();
148+
149+
if( $email === null )
150+
throw new Exception( __( "Failed to determine the administrator's email address.", 'pdf-forms-for-contact-form-7' ) );
151+
152+
$key = null;
153+
154+
// try to get the key the normal way
155+
try { $key = $this->api_get_key( $email ); }
156+
catch(Exception $e)
120157
{
121-
$current_user = wp_get_current_user();
122-
if( ! $current_user )
123-
throw new Exception( __( "Failed to determine the current user.", 'pdf-forms-for-contact-form-7' ) );
124-
125-
$email = sanitize_email( $current_user->user_email );
126-
if( ! $email )
127-
throw new Exception( __( "Failed to determine the current user's email address.", 'pdf-forms-for-contact-form-7' ) );
128-
129-
$key = null;
158+
// if we are not running for the first time, throw on error
159+
$old_key = WPCF7::get_option( 'wpcf7_pdf_forms_pdfninja_key' );
160+
if( $old_key )
161+
throw $e;
130162

131-
// try to get the key the normal way
163+
// there might be an issue with certificate verification on this system, disable it and try again
164+
$this->set_verify_ssl( false );
132165
try { $key = $this->api_get_key( $email ); }
133166
catch(Exception $e)
134167
{
135-
// if we are not running for the first time, throw on error
136-
$old_key = WPCF7::get_option( 'wpcf7_pdf_forms_pdfninja_key' );
137-
if( $old_key )
138-
throw $e;
139-
140-
// there might be an issue with certificate verification on this system, disable it and try again
141-
$this->set_verify_ssl( false );
142-
try { $key = $this->api_get_key( $email ); }
143-
catch(Exception $e)
144-
{
145-
// if it still fails, revert and throw
146-
$this->set_verify_ssl( true );
147-
throw $e;
148-
}
168+
// if it still fails, revert and throw
169+
$this->set_verify_ssl( true );
170+
throw $e;
149171
}
150-
151-
return $key;
152-
}
153-
catch(Exception $e)
154-
{
155-
set_transient( 'wpcf7_pdf_forms_pdfninja_key_failure', true, 12 * HOUR_IN_SECONDS );
156-
throw $e;
157172
}
173+
174+
return $key;
158175
}
159176

160177
/*
@@ -905,20 +922,27 @@ public function load( $action = '' )
905922

906923
$success = true;
907924

908-
$api_url = isset( $_POST['api_url'] ) ? trim( wp_unslash( $_POST['api_url'] ) ) : null;
909-
if( $success && $this->get_api_url() != $api_url ) $success = $this->set_api_url( $api_url );
910-
911-
$api_version = isset( $_POST['api-version'] ) ? trim( wp_unslash( $_POST['api-version'] ) ) : false;
912-
if( $success && $this->get_api_version() != $api_version ) $success = $this->set_api_version( $api_version );
913-
914-
$nosslverify = isset( $_POST['nosslverify'] ) ? trim( wp_unslash( $_POST['nosslverify'] ) ) : false;
915-
if( $success ) $success = $this->set_verify_ssl( !(bool)$nosslverify );
925+
if( isset( $_POST['save'] ) && $_POST['save'] )
926+
{
927+
$api_url = isset( $_POST['api_url'] ) ? trim( wp_unslash( $_POST['api_url'] ) ) : null;
928+
if( $success && $this->get_api_url() != $api_url ) $success = $this->set_api_url( $api_url );
929+
930+
$api_version = isset( $_POST['api-version'] ) ? trim( wp_unslash( $_POST['api-version'] ) ) : false;
931+
if( $success && $this->get_api_version() != $api_version ) $success = $this->set_api_version( $api_version );
932+
933+
$nosslverify = isset( $_POST['nosslverify'] ) ? trim( wp_unslash( $_POST['nosslverify'] ) ) : false;
934+
if( $success ) $success = $this->set_verify_ssl( !(bool)$nosslverify );
935+
936+
$key = isset( $_POST['key'] ) ? trim( wp_unslash( $_POST['key'] ) ) : null;
937+
}
916938

917939
if( isset( $_POST['new'] ) && $_POST['new'] )
918-
$key = $this->generate_key();
919-
else
920-
$key = isset( $_POST['key'] ) ? trim( wp_unslash( $_POST['key'] ) ) : null;
921-
if( $success && $key ) $success = $this->set_key( $key );
940+
{
941+
$email = isset( $_POST['email'] ) ? trim( wp_unslash( $_POST['email'] ) ) : null;
942+
$key = $this->generate_key( $email );
943+
}
944+
945+
if( $success && isset( $key ) && $key ) $success = $this->set_key( $key );
922946

923947
if( $success )
924948
wp_safe_redirect( $this->menu_page_url( array( 'message' => 'success' ) ) );
@@ -1012,7 +1036,8 @@ public function display_edit()
10121036
try { $key = $this->get_key(); } catch(Exception $e) { }
10131037

10141038
echo WPCF7_Pdf_Forms::render( 'pdfninja_integration_edit', array(
1015-
'top-message' => esc_html__( "The following form allows you to edit your API key.", 'pdf-forms-for-contact-form-7' ),
1039+
'top-message-api-settings' => esc_html__( "The following form allows you to edit your API settings.", 'pdf-forms-for-contact-form-7' ),
1040+
'top-message-new-key' => esc_html__( "The following form allows you to request a new key from the API.", 'pdf-forms-for-contact-form-7' ),
10161041
'key-label' => esc_html__( 'API Key', 'pdf-forms-for-contact-form-7' ),
10171042
'key' => esc_html( $key ),
10181043
'api-url-label' => esc_html__( 'API URL', 'pdf-forms-for-contact-form-7' ),
@@ -1025,6 +1050,8 @@ public function display_edit()
10251050
'security-label' => esc_html__( 'Data Security', 'pdf-forms-for-contact-form-7' ),
10261051
'no-ssl-verify-label' => esc_html__( 'Ignore certificate verification errors', 'pdf-forms-for-contact-form-7' ),
10271052
'no-ssl-verify-value' => !$this->get_verify_ssl() ? 'checked' : '',
1053+
'email-label' => esc_html__( "Administrator's Email Address", 'pdf-forms-for-contact-form-7' ),
1054+
'email-value' => esc_html__( $this->get_admin_email() ),
10281055
'security-warning' => esc_html__( 'Warning: Using plain HTTP or disabling certificate verification can lead to data leaks.', 'pdf-forms-for-contact-form-7' ),
10291056
'edit-link' => esc_url( $this->menu_page_url( 'action=edit' ) ),
10301057
'nonce' => wp_nonce_field( 'wpcf7-pdfninja-edit' ),

0 commit comments

Comments
 (0)