-
Notifications
You must be signed in to change notification settings - Fork 844
Forms: Remove classic Admin initialization and migrate functionality #46254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2efd4ca
75423ad
2cd7387
d0b67b2
a9e2ff7
b1bc9b9
c777be5
d61b1fc
4f6e184
11296d4
c2eb35d
91eeaa9
3cdf870
dc9401f
88d3d32
de0af73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: major | ||
| Type: removed | ||
|
|
||
| Forms: Removes classic Admin initialization code |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,8 @@ | |
| * Class Admin | ||
| * | ||
| * Singleton for Grunion admin area support. | ||
| * | ||
| * This class will be removed in a future version. | ||
| */ | ||
| class Admin { | ||
| /** | ||
|
|
@@ -37,9 +39,12 @@ class Admin { | |
| /** | ||
| * Instantiates this singleton class | ||
| * | ||
| * @deprecated $$next-version$$ | ||
| * | ||
| * @return Admin The Admin class instance. | ||
| */ | ||
| public static function init() { | ||
| _deprecated_function( __METHOD__, 'package-$$next-version$$' ); | ||
| static $instance = false; | ||
|
|
||
| if ( ! $instance ) { | ||
|
|
@@ -81,9 +86,6 @@ public function __construct() { | |
|
|
||
| add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); | ||
| add_action( 'admin_footer-edit.php', array( $this, 'print_export_modal' ) ); | ||
|
|
||
| add_action( 'wp_ajax_grunion_export_to_gdrive', array( $this, 'export_to_gdrive' ) ); | ||
| add_action( 'wp_ajax_grunion_gdrive_connection', array( $this, 'test_gdrive_connection' ) ); | ||
| } | ||
|
Comment on lines
86
to
89
|
||
|
|
||
| /** | ||
|
|
@@ -151,70 +153,14 @@ public function print_export_modal() { | |
| * Ajax handler for wp_ajax_grunion_export_to_gdrive. | ||
| * Exports data to Google Drive, based on POST data. | ||
| * | ||
| * @deprecated $$next-version$$ | ||
| * | ||
| * @see Contact_Form_Plugin::get_feedback_entries_from_post | ||
| */ | ||
| public function export_to_gdrive() { | ||
| $post_data = wp_unslash( $_POST ); | ||
| if ( | ||
| ! current_user_can( 'export' ) | ||
| || empty( sanitize_text_field( $post_data[ $this->export_nonce_field_gdrive ] ) ) | ||
| || ! wp_verify_nonce( sanitize_text_field( $post_data[ $this->export_nonce_field_gdrive ] ), 'feedback_export' ) | ||
| ) { | ||
| wp_send_json_error( | ||
| __( 'You aren\'t authorized to do that.', 'jetpack-forms' ), | ||
| 403, | ||
| JSON_UNESCAPED_SLASHES | ||
| ); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $grunion = Contact_Form_Plugin::init(); | ||
| $export_data = $grunion->get_feedback_entries_from_post(); | ||
|
|
||
| $fields = is_array( $export_data ) ? array_keys( $export_data ) : array(); | ||
| $row_count = ! is_array( $export_data ) || empty( $export_data ) ? 0 : count( reset( $export_data ) ); | ||
|
|
||
| $sheet_data = array( $fields ); | ||
|
|
||
| for ( $i = 0; $i < $row_count; $i++ ) { | ||
|
|
||
| $current_row = array(); | ||
| _deprecated_function( __METHOD__, 'package-$$next-version$$', 'Automattic\Jetpack\Forms\ContactForm\Contact_Form_Plugin::init()->export_to_gdrive()' ); | ||
|
|
||
| /** | ||
| * Put all the fields in `$current_row` array. | ||
| */ | ||
| foreach ( $fields as $single_field_name ) { | ||
| $current_row[] = $export_data[ $single_field_name ][ $i ]; | ||
| } | ||
|
|
||
| $sheet_data[] = $current_row; | ||
| } | ||
|
|
||
| $user_id = (int) get_current_user_id(); | ||
|
|
||
| if ( ! empty( $post_data['post'] ) && $post_data['post'] !== 'all' ) { | ||
| $spreadsheet_title = sprintf( | ||
| '%1$s - %2$s', | ||
| $this->get_export_filename( get_the_title( (int) $post_data['post'] ) ), | ||
| gmdate( 'Y-m-d H:i' ) | ||
| ); | ||
| } else { | ||
| $spreadsheet_title = sprintf( '%s - %s', $this->get_export_filename(), gmdate( 'Y-m-d H:i' ) ); | ||
| } | ||
|
|
||
| $sheet = Google_Drive::create_sheet( $user_id, $spreadsheet_title, $sheet_data ); | ||
|
|
||
| $grunion->record_tracks_event( 'forms_export_responses', array( 'format' => 'gsheets' ) ); | ||
|
|
||
| wp_send_json( | ||
| array( | ||
| 'success' => ! is_wp_error( $sheet ), | ||
| 'data' => $sheet, | ||
| ), | ||
| 200, | ||
| JSON_UNESCAPED_SLASHES | ||
| ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Test results:
Notes:
|
||
| return Contact_Form_Plugin::init()->export_to_gdrive(); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |
| use Automattic\Jetpack\Extensions\Contact_Form\Contact_Form_Block; | ||
| use Automattic\Jetpack\Forms\Jetpack_Forms; | ||
| use Automattic\Jetpack\Forms\Service\Form_Webhooks; | ||
| use Automattic\Jetpack\Forms\Service\Google_Drive; | ||
| use Automattic\Jetpack\Forms\Service\Hostinger_Reach_Integration; | ||
| use Automattic\Jetpack\Forms\Service\MailPoet_Integration; | ||
| use Automattic\Jetpack\Forms\Service\Post_To_Url; | ||
|
|
@@ -97,6 +98,13 @@ class Contact_Form_Plugin { | |
| 'feedback_id' => '', | ||
| ); | ||
|
|
||
| /** | ||
| * GDrive export nonce field name | ||
| * | ||
| * @var string The nonce field name for GDrive export. | ||
| */ | ||
| private $export_nonce_field_gdrive = 'feedback_export_nonce_gdrive'; | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if it makes any sense to move all the Google drive logic to the Google_Drive class that was created in August? That's in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it would? But I'm also on the side of sticking to what we have tested and worked well. We can always follow up or find optimizations, specially in terms of code readability and organization. I just wanted to stay as simple as possible, after all, this is janitorial and not meant to take so much of us. That said, feel free to push changes and add tests or test instructions. Or create a PR branching out of this one if that helps as a "follow up reminder". |
||
| /** | ||
| * Initializing function. | ||
| */ | ||
|
|
@@ -221,6 +229,7 @@ protected function __construct() { | |
| if ( is_admin() ) { | ||
| add_action( 'wp_ajax_feedback_export', array( $this, 'download_feedback_as_csv' ) ); | ||
| add_action( 'wp_ajax_create_new_form', array( $this, 'create_new_form' ) ); | ||
| add_action( 'wp_ajax_grunion_export_to_gdrive', array( $this, 'export_to_gdrive' ) ); | ||
| } | ||
| add_action( 'admin_menu', array( $this, 'admin_menu' ) ); | ||
| add_action( 'current_screen', array( $this, 'unread_count' ) ); | ||
|
|
@@ -3653,4 +3662,98 @@ public function redirect_edit_feedback_to_jetpack_forms() { | |
| wp_safe_redirect( $redirect_url ); | ||
| exit; | ||
| } | ||
|
|
||
| /** | ||
| * Validates the export to Google Drive request. | ||
| * | ||
| * @param array $post_data The POST data to validate. | ||
| * @return bool True if the request is valid, false otherwise. | ||
| */ | ||
| public function validate_export_to_gdrive_request( $post_data ) { | ||
| if ( ! current_user_can( 'export' ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| if ( empty( $post_data[ $this->export_nonce_field_gdrive ] ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| $nonce = sanitize_text_field( $post_data[ $this->export_nonce_field_gdrive ] ); | ||
| if ( ! wp_verify_nonce( $nonce, 'feedback_export' ) ) { | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Ajax handler for wp_ajax_grunion_export_to_gdrive. | ||
| * Exports data to Google Drive, based on POST data. | ||
| * | ||
| * @see Contact_Form_Plugin::get_feedback_entries_from_post | ||
| */ | ||
| public function export_to_gdrive() { | ||
| // phpcs:ignore WordPress.Security.NonceVerification.Missing -- verification is done on validate_export_to_gdrive_request function | ||
| $post_data = wp_unslash( $_POST ); | ||
|
|
||
| if ( ! $this->validate_export_to_gdrive_request( $post_data ) ) { | ||
| wp_send_json_error( | ||
| __( 'You aren\'t authorized to do that.', 'jetpack-forms' ), | ||
| 403, | ||
| JSON_UNESCAPED_SLASHES | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| $grunion = self::init(); | ||
| $export_data = $grunion->get_feedback_entries_from_post(); | ||
|
|
||
| $fields = is_array( $export_data ) ? array_keys( $export_data ) : array(); | ||
| $row_count = ! is_array( $export_data ) || empty( $export_data ) ? 0 : count( reset( $export_data ) ); | ||
|
|
||
| $sheet_data = array( $fields ); | ||
|
|
||
| for ( $i = 0; $i < $row_count; $i++ ) { | ||
|
|
||
| $current_row = array(); | ||
|
|
||
| /** | ||
| * Put all the fields in `$current_row` array. | ||
| */ | ||
| foreach ( $fields as $single_field_name ) { | ||
| if ( isset( $export_data[ $single_field_name ][ $i ] ) ) { | ||
| $current_row[] = $this->esc_csv( $export_data[ $single_field_name ][ $i ] ); | ||
| } else { | ||
| $current_row[] = ''; | ||
| } | ||
| } | ||
|
|
||
| $sheet_data[] = $current_row; | ||
| } | ||
|
|
||
| $user_id = (int) get_current_user_id(); | ||
|
|
||
| if ( ! empty( $post_data['post'] ) && $post_data['post'] !== 'all' ) { | ||
| $spreadsheet_title = sprintf( | ||
| '%1$s - %2$s', | ||
| Util::get_export_filename( get_the_title( (int) $post_data['post'] ) ), | ||
| gmdate( 'Y-m-d H:i' ) | ||
| ); | ||
| } else { | ||
| $spreadsheet_title = sprintf( '%s - %s', Util::get_export_filename(), gmdate( 'Y-m-d H:i' ) ); | ||
| } | ||
|
|
||
| $sheet = Google_Drive::create_sheet( $user_id, $spreadsheet_title, $sheet_data ); | ||
|
|
||
| $grunion->record_tracks_event( 'forms_export_responses', array( 'format' => 'gsheets' ) ); | ||
|
|
||
| wp_send_json( | ||
| array( | ||
| 'success' => ! is_wp_error( $sheet ), | ||
| 'data' => $sheet, | ||
| ), | ||
| is_wp_error( $sheet ) ? 500 : 200, | ||
| JSON_UNESCAPED_SLASHES | ||
| ); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.