@@ -94,7 +94,18 @@ public function get_key()
94
94
if ( $ fail )
95
95
throw new Exception ( __ ( "Failed to get the Pdf.Ninja API key on last attempt. Please retry manually. " , 'pdf-forms-for-contact-form-7 ' ) );
96
96
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 );
98
109
}
99
110
100
111
return $ this ->key ;
@@ -111,50 +122,56 @@ public function set_key( $value )
111
122
return true ;
112
123
}
113
124
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
+
114
141
/*
115
142
* Requests a key from the API server
116
143
*/
117
- public function generate_key ()
144
+ public function generate_key ( $ email = null )
118
145
{
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 )
120
157
{
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 ;
130
162
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 );
132
165
try { $ key = $ this ->api_get_key ( $ email ); }
133
166
catch (Exception $ e )
134
167
{
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 ;
149
171
}
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 ;
157
172
}
173
+
174
+ return $ key ;
158
175
}
159
176
160
177
/*
@@ -905,20 +922,27 @@ public function load( $action = '' )
905
922
906
923
$ success = true ;
907
924
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
+ }
916
938
917
939
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 );
922
946
923
947
if ( $ success )
924
948
wp_safe_redirect ( $ this ->menu_page_url ( array ( 'message ' => 'success ' ) ) );
@@ -1012,7 +1036,8 @@ public function display_edit()
1012
1036
try { $ key = $ this ->get_key (); } catch (Exception $ e ) { }
1013
1037
1014
1038
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 ' ),
1016
1041
'key-label ' => esc_html__ ( 'API Key ' , 'pdf-forms-for-contact-form-7 ' ),
1017
1042
'key ' => esc_html ( $ key ),
1018
1043
'api-url-label ' => esc_html__ ( 'API URL ' , 'pdf-forms-for-contact-form-7 ' ),
@@ -1025,6 +1050,8 @@ public function display_edit()
1025
1050
'security-label ' => esc_html__ ( 'Data Security ' , 'pdf-forms-for-contact-form-7 ' ),
1026
1051
'no-ssl-verify-label ' => esc_html__ ( 'Ignore certificate verification errors ' , 'pdf-forms-for-contact-form-7 ' ),
1027
1052
'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 () ),
1028
1055
'security-warning ' => esc_html__ ( 'Warning: Using plain HTTP or disabling certificate verification can lead to data leaks. ' , 'pdf-forms-for-contact-form-7 ' ),
1029
1056
'edit-link ' => esc_url ( $ this ->menu_page_url ( 'action=edit ' ) ),
1030
1057
'nonce ' => wp_nonce_field ( 'wpcf7-pdfninja-edit ' ),
0 commit comments