@@ -275,6 +275,9 @@ public function init_ajax() {
275
275
// Add AJAX callback for de-authorizing with Dropbox.
276
276
add_action ( 'wp_ajax_gfdropbox_deauthorize ' , array ( $ this , 'ajax_deauthorize ' ) );
277
277
278
+ // Add AJAX callback for removing the access token so that the user can re-authorize.
279
+ add_action ( 'wp_ajax_gfdropbox_remove_access_token ' , array ( $ this , 'ajax_remove_access_token ' ) );
280
+
278
281
// Add AJAX callback for checking app key/secret validity.
279
282
add_action ( 'wp_ajax_gfdropbox_valid_app_key_secret ' , array ( $ this , 'ajax_is_valid_app_key_secret ' ) );
280
283
@@ -725,29 +728,28 @@ public function settings_auth_token_button( $field, $echo = true ) {
725
728
// Get account information.
726
729
$ account = $ this ->api ->get_current_account ();
727
730
728
- $ html .= '<p> ' ;
729
- $ html .= sprintf (
730
- // Translators: 1. Link to forms list page, 2. Closing </a> tag
731
- esc_html__ ( 'To configure Dropbox with your form(s), choose a form you wish to use Dropbox with from the %sforms list page%s, and select Dropbox from the form settings menu. ' , 'gravityformsdropbox ' ),
732
- '<a href="?page=gf_edit_forms"> ' ,
733
- '</a> '
734
- );
735
- $ html .= '</p> ' ;
736
- $ html .= '<p><strong> ' . esc_html__ ( 'Dropbox Account Status ' ) . '</strong></p> ' ;
737
- $ html .= '<p><span class="gform-status-indicator gform-status--active"> ' ;
738
- if ( $ this ->is_gravityforms_supported ( '2.5 ' ) ) {
739
- $ html .= '<svg viewBox="0 0 6 6" xmlns="http://www.w3.org/2000/svg"><circle cx="3" cy="2" r="1" stroke-width="2"/></svg> ' ;
740
- }
741
- if ( isset ( $ account ->name ) ) {
742
- $ html .= esc_html__ ( 'Connected to Dropbox as: ' , 'gravityformsdropbox ' );
743
- $ html .= esc_html ( $ account ->name ->display_name ) . '</span></p> ' ;
731
+ $ is_account_disabled = method_exists ( $ account , 'getCode ' ) && $ account ->getCode () === 400 ;
732
+
733
+ if ( $ is_account_disabled ) {
734
+ $ html .= $ this ->get_error_indicator ();
744
735
} else {
745
- $ html .= esc_html__ ( 'Connected to Dropbox. ' , 'gravityformsdropbox ' ) . '</span></p> ' ;
736
+
737
+ $ html .= '<p> ' ;
738
+ $ html .= sprintf (
739
+ // Translators: 1. Link to forms list page, 2. Closing </a> tag.
740
+ esc_html__ ( 'To configure Dropbox with your form(s), choose a form you wish to use Dropbox with from the %sforms list page%s, and select Dropbox from the form settings menu. ' , 'gravityformsdropbox ' ),
741
+ '<a href="?page=gf_edit_forms"> ' ,
742
+ '</a> '
743
+ );
744
+ $ html .= '</p> ' ;
745
+ $ html .= '<p><strong> ' . esc_html__ ( 'Dropbox Account Status ' ) . '</strong></p> ' ;
746
+ $ html .= $ this ->get_status_indicator ( $ account );
747
+
748
+ $ html .= sprintf (
749
+ ' <a href="#" class="button primary" id="gform_dropbox_deauth_button">%1$s</a> ' ,
750
+ esc_html__ ( 'Disconnect from Dropbox ' , 'gravityformsdropbox ' )
751
+ );
746
752
}
747
- $ html .= sprintf (
748
- ' <a href="#" class="button primary" id="gform_dropbox_deauth_button">%1$s</a> ' ,
749
- esc_html__ ( 'Disconnect from Dropbox ' , 'gravityformsdropbox ' )
750
- );
751
753
752
754
} catch ( Exception $ e ) {
753
755
@@ -829,6 +831,81 @@ public function settings_auth_token_button( $field, $echo = true ) {
829
831
830
832
}
831
833
834
+ /**
835
+ * Get the correct status indicator markup for the version of Core.
836
+ *
837
+ * @since 3.1.2
838
+ *
839
+ * @param $account The account being evaluated.
840
+ *
841
+ * @return string
842
+ */
843
+ private function get_status_indicator ( $ account ) {
844
+
845
+ // Indicator styles were updated in 2.8.8
846
+ if ( $ this ->is_gravityforms_supported ( '2.8.8 ' ) ) {
847
+ $ html = '<p><span class="gform-status-indicator gform-status-indicator--size-sm gform-status-indicator--theme-cosmos gform-status--active gform-status--no-icon gform-status--no-hover"> ' ;
848
+ $ html .= '<span class="gform-status-indicator-status gform-typography--weight-medium gform-typography--size-text-xs"> ' ;
849
+
850
+ if ( isset ( $ account ->name ) ) {
851
+ $ html .= esc_html__ ( 'Connected to Dropbox as: ' , 'gravityformsdropbox ' );
852
+ $ html .= esc_html ( $ account ->name ->display_name );
853
+ } else {
854
+ $ html .= esc_html__ ( 'Connected to Dropbox. ' , 'gravityformsdropbox ' );
855
+ }
856
+
857
+ $ html .= '</span></span></p> ' ;
858
+
859
+ return $ html ;
860
+ }
861
+
862
+ $ html = '<p><span class="gform-status-indicator gform-status--static gform-status--active"> ' ;
863
+ if ( $ this ->is_gravityforms_supported ( '2.5 ' ) ) {
864
+ $ html .= '<svg viewBox="0 0 6 6" xmlns="http://www.w3.org/2000/svg"><circle cx="3" cy="2" r="1" stroke-width="2"/></svg> ' ;
865
+ }
866
+ if ( isset ( $ account ->name ) ) {
867
+ $ html .= esc_html__ ( 'Connected to Dropbox as: ' , 'gravityformsdropbox ' );
868
+ $ html .= esc_html ( $ account ->name ->display_name ) . '</span></p> ' ;
869
+ } else {
870
+ $ html .= esc_html__ ( 'Connected to Dropbox. ' , 'gravityformsdropbox ' ) . '</span></p> ' ;
871
+ }
872
+
873
+ return $ html ;
874
+ }
875
+
876
+ /**
877
+ * Get the correct error indicator markup for the version of Core.
878
+ *
879
+ * @since 3.1.2
880
+ *
881
+ * @return string
882
+ */
883
+ private function get_error_indicator () {
884
+
885
+ // Indicator styles were updated in 2.8.8
886
+ if ( $ this ->is_gravityforms_supported ( '2.8.8 ' ) ) {
887
+ $ html = '<p><span class="gform-status-indicator gform-status-indicator--size-sm gform-status-indicator--theme-cosmos gform-status--error gform-status--no-icon gform-status--no-hover"> ' ;
888
+ $ html .= '<span class="gform-status-indicator-status gform-typography--weight-medium gform-typography--size-text-xs"> ' ;
889
+ $ html .= esc_html__ ( 'The connected Dropbox app is disabled or has been deleted. Please reconnect. ' , 'gravityformsdropbox ' );
890
+ $ html .= '</span></span></p> ' ;
891
+
892
+ $ html .= sprintf (
893
+ ' <a href="#" class="button primary" id="gform_dropbox_reconnect_button">%1$s</a> ' ,
894
+ esc_html__ ( 'Reconnect to Dropbox ' , 'gravityformsdropbox ' )
895
+ );
896
+
897
+ return $ html ;
898
+ }
899
+
900
+ $ html = '<p> <span class="gform-status-indicator gform-status--static gform-status--error"> ' . esc_html__ ( 'The connected Dropbox app is disabled or has been deleted. Please reconnect. ' , 'gravityformsdropbox ' ) . ' </span></p> ' ;
901
+ $ html = sprintf (
902
+ ' <a href="#" class="button primary" id="gform_dropbox_reconnect_button">%1$s</a> ' ,
903
+ esc_html__ ( 'Reconnect to Dropbox ' , 'gravityformsdropbox ' )
904
+ );
905
+
906
+ return $ html ;
907
+ }
908
+
832
909
/**
833
910
* Renders settings section for custom Dropbox app.
834
911
*
@@ -911,6 +988,34 @@ public function custom_app_settings() {
911
988
912
989
}
913
990
991
+ /**
992
+ * Removes access token and custom app settings to deauthorize.
993
+ *
994
+ * @since 3.2
995
+ *
996
+ * @param array $settings Existing settings.
997
+ *
998
+ * @return void
999
+ */
1000
+ public function remove_access_token ( $ settings = null ) {
1001
+
1002
+ if ( ! $ settings ) {
1003
+ $ settings = $ this ->get_plugin_settings ();
1004
+ }
1005
+
1006
+ // Remove access token from settings to deauthorize.
1007
+ unset( $ settings ['accessToken ' ] );
1008
+ unset( $ settings ['access_token_expires ' ] );
1009
+ unset( $ settings ['refresh_token ' ] );
1010
+
1011
+ // Removing custom app settings if they exist.
1012
+ unset( $ settings ['customAppKey ' ] );
1013
+ unset( $ settings ['customAppSecret ' ] );
1014
+
1015
+ // Save settings.
1016
+ $ this ->update_plugin_settings ( $ settings );
1017
+ }
1018
+
914
1019
/**
915
1020
* Deauthorize with Dropbox.
916
1021
*
@@ -944,13 +1049,7 @@ public function ajax_deauthorize() {
944
1049
// Log that we revoked the access token.
945
1050
$ this ->log_debug ( __METHOD__ . '(): Access token revoked. ' );
946
1051
947
- // Remove access token from settings.
948
- unset( $ settings ['accessToken ' ] );
949
- unset( $ settings ['access_token_expires ' ] );
950
- unset( $ settings ['refresh_token ' ] );
951
-
952
- // Save settings.
953
- $ this ->update_plugin_settings ( $ settings );
1052
+ $ this ->remove_access_token ( $ settings );
954
1053
955
1054
// Return success response.
956
1055
wp_send_json_success ();
@@ -961,25 +1060,48 @@ public function ajax_deauthorize() {
961
1060
$ this ->log_debug ( __METHOD__ . '(): Unable to revoke access token; ' . $ e ->getMessage () );
962
1061
963
1062
if ( $ e ->getCode () === 401 ) {
964
- // Remove access token from settings because it was already revoked.
965
- unset( $ settings ['accessToken ' ] );
966
- unset( $ settings ['access_token_expires ' ] );
967
- unset( $ settings ['refresh_token ' ] );
968
1063
969
- // Save settings.
970
- $ this ->update_plugin_settings ( $ settings );
1064
+ $ this ->remove_access_token ( $ settings );
1065
+
1066
+ // Return success response.
1067
+ wp_send_json_success ();
1068
+
1069
+ } else {
1070
+
1071
+ // Return error response.
1072
+ wp_send_json_error (
1073
+ array (
1074
+ 'message ' => $ e ->getMessage (),
1075
+ 'code ' => $ e ->getCode (),
1076
+ )
1077
+ );
1078
+
971
1079
}
1080
+ }
1081
+ }
972
1082
973
- // Return error response.
974
- wp_send_json_error (
975
- array (
976
- 'message ' => $ e ->getMessage (),
977
- 'code ' => $ e ->getCode (),
978
- )
979
- );
1083
+ /**
1084
+ * Handles the AJAX request to remove the access token .
1085
+ *
1086
+ * @since 3.2
1087
+ */
1088
+ public function ajax_remove_access_token () {
1089
+
1090
+ // Verify nonce.
1091
+ if ( false === wp_verify_nonce ( rgget ( 'nonce ' ), 'gfdropbox_deauthorize ' ) ) {
1092
+ wp_send_json_error ( array ( 'message ' => esc_html__ ( 'Access denied. ' , 'gravityformsdropbox ' ) ) );
1093
+ }
980
1094
1095
+ // If user is not authorized, exit.
1096
+ if ( ! GFCommon::current_user_can_any ( $ this ->_capabilities_settings_page ) ) {
1097
+ wp_send_json_error ( array ( 'message ' => esc_html__ ( 'Access denied. ' , 'gravityformsdropbox ' ) ) );
981
1098
}
982
1099
1100
+ // Remove access token.
1101
+ $ this ->remove_access_token ();
1102
+
1103
+ // Return success response.
1104
+ wp_send_json_success ();
983
1105
}
984
1106
985
1107
@@ -1812,6 +1934,40 @@ public function process_feed_files( $feed, $entry, $form ) {
1812
1934
1813
1935
}
1814
1936
1937
+ /**
1938
+ * Passes the folder path through the gform_dropbox_folder_path filter.
1939
+ *
1940
+ * @since 3.2
1941
+ *
1942
+ * @param string $folder_path The folder in the Dropbox account where the files will be stored.
1943
+ * @param array $form Form object.
1944
+ * @param int|string $field_id The ID of the field currently being processed.
1945
+ * @param array $entry Entry object.
1946
+ * @param array $feed Feed object.
1947
+ * @param string $file_url The URL of the file being processed.
1948
+ *
1949
+ * @return string
1950
+ */
1951
+ public function filter_folder_path ( $ folder_path , $ form , $ field_id , $ entry , $ feed , $ file_url ) {
1952
+ /**
1953
+ * Modify the destination folder configured on the Dropbox feed.
1954
+ *
1955
+ * @since 1.0
1956
+ * @since 3.2 Added the $file_url param.
1957
+ *
1958
+ * @param string $folder_path The folder in the Dropbox account where the files will be stored.
1959
+ * @param array $form Form object.
1960
+ * @param int|string $field_id The ID of the field currently being processed.
1961
+ * @param array $entry Entry object.
1962
+ * @param array $feed Feed object.
1963
+ * @param string $file_url The URL of the file being processed.
1964
+ */
1965
+ return gf_apply_filters ( array (
1966
+ 'gform_dropbox_folder_path ' ,
1967
+ rgar ( $ form , 'id ' ),
1968
+ ), $ folder_path , $ form , $ field_id , $ entry , $ feed , $ file_url );
1969
+ }
1970
+
1815
1971
/**
1816
1972
* Process Dropbox upload fields.
1817
1973
*
@@ -1850,19 +2006,7 @@ public function process_dropbox_fields( $field, $feed, $entry, $form ) {
1850
2006
}
1851
2007
1852
2008
// Get destination path.
1853
- $ folder_path = rgars ( $ feed , 'meta/destinationFolder ' );
1854
-
1855
- /**
1856
- * Modify the destination folder configured on the Dropbox feed.
1857
- *
1858
- * @since 1.0
1859
- * @param string $folder_path The folder in the Dropbox account where the files will be stored.
1860
- * @param array $form Form object.
1861
- * @param string $field_id The ID of the field currently being processed.
1862
- * @param array $entry Entry object.
1863
- * @param array $feed Feed object.
1864
- */
1865
- $ folder_path = gf_apply_filters ( array ( 'gform_dropbox_folder_path ' , $ form ['id ' ] ), $ folder_path , $ form , $ field ->id , $ entry , $ feed );
2009
+ $ folder_path = $ this ->filter_folder_path ( rgars ( $ feed , 'meta/destinationFolder ' ), $ form , $ field ->id , $ entry , $ feed , $ file );
1866
2010
1867
2011
// Add starting slash to folder path.
1868
2012
$ folder_path = strpos ( $ folder_path , '/ ' ) !== 0 ? '/ ' . $ folder_path : $ folder_path ;
@@ -2076,17 +2220,7 @@ public function upload_file( $file, $form, $field_id, $entry, $feed ) {
2076
2220
2077
2221
}
2078
2222
2079
- /**
2080
- * Modify the destination folder configured on the Dropbox feed.
2081
- *
2082
- * @since 1.0
2083
- * @param string $folder_path The folder in the Dropbox account where the files will be stored.
2084
- * @param array $form Form object.
2085
- * @param string $field_id The ID of the field currently being processed.
2086
- * @param array $entry Entry object.
2087
- * @param array $feed Feed object.
2088
- */
2089
- $ folder_path = gf_apply_filters ( array ( 'gform_dropbox_folder_path ' , $ form ['id ' ] ), $ file ['destination ' ], $ form , $ field_id , $ entry , $ feed );
2223
+ $ folder_path = $ this ->filter_folder_path ( rgar ( $ file , 'destination ' ), $ form , $ field_id , $ entry , $ feed , rgar ( $ file , 'url ' ) );
2090
2224
2091
2225
// If destination folder is not the root folder, ensure the folder exists.
2092
2226
if ( '/ ' !== $ folder_path ) {
@@ -2302,9 +2436,8 @@ public function filter_gform_pre_send_email( $email, $message_format, $notificat
2302
2436
continue ;
2303
2437
}
2304
2438
2305
- // Get file name.
2306
- $ file_name = basename ( $ attachment_path );
2307
- $ file_name = str_replace ( '?dl=0 ' , '' , $ file_name );
2439
+ // Get file name without the Dropbox query arguments.
2440
+ $ file_name = basename ( strtok ( $ attachment_path , '? ' ) );
2308
2441
2309
2442
// Get path to local file.
2310
2443
$ local_path = GFFormsModel::get_upload_path ( $ entry ['form_id ' ] ) . GFCommon::format_date ( rgar ( $ entry , 'date_created ' ), false , '/Y/m/ ' , false ) . $ file_name ;
@@ -2313,6 +2446,7 @@ public function filter_gform_pre_send_email( $email, $message_format, $notificat
2313
2446
if ( file_exists ( $ local_path ) ) {
2314
2447
$ email ['attachments ' ][ $ a ] = $ local_path ;
2315
2448
} else {
2449
+ gf_dropbox ()->log_debug ( __METHOD__ . '(): Removing attachment from notification as file " ' . $ local_path . '" does not exist. ' );
2316
2450
unset( $ email ['attachments ' ][ $ a ] );
2317
2451
}
2318
2452
0 commit comments