Skip to content
4 changes: 2 additions & 2 deletions projects/packages/forms/.phan/baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@
// PhanPluginMixedKeyNoKey : 1 occurrence
// PhanPluginRedundantAssignment : 1 occurrence
// PhanPossiblyNullTypeMismatchProperty : 1 occurrence
// PhanTypeArraySuspiciousNullable : 1 occurrence
// PhanTypeMismatchReturnNullable : 1 occurrence
// PhanUnreferencedUseNormal : 1 occurrence

// Currently, file_suppressions and directory_suppressions are the only supported suppressions
'file_suppressions' => [
'src/contact-form/class-admin.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanTypeArraySuspiciousNullable', 'PhanTypeMismatchArgument', 'PhanTypeMismatchArgumentProbablyReal', 'PhanTypeMismatchReturn'],
'src/contact-form/class-admin.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanTypeMismatchArgument', 'PhanTypeMismatchArgumentProbablyReal', 'PhanTypeMismatchReturn'],
'src/contact-form/class-contact-form-field.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanPossiblyNullTypeMismatchProperty', 'PhanTypeConversionFromArray', 'PhanTypeMismatchArgument', 'PhanTypeMismatchReturnProbablyReal'],
'src/contact-form/class-contact-form-plugin.php' => ['PhanPluginDuplicateAdjacentStatement', 'PhanPluginDuplicateConditionalNullCoalescing', 'PhanTypeMismatchArgument', 'PhanTypeMismatchArgumentProbablyReal', 'PhanTypeMismatchReturnProbablyReal'],
'src/contact-form/class-contact-form-shortcode.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanTypeMismatchReturnProbablyReal'],
'src/contact-form/class-contact-form.php' => ['PhanPluginDuplicateConditionalNullCoalescing', 'PhanPluginRedundantAssignment', 'PhanTypeMismatchArgument', 'PhanTypeMismatchReturnNullable', 'PhanTypeMismatchReturnProbablyReal'],
'src/dashboard/class-dashboard-view-switch.php' => ['PhanUnreferencedUseNormal'],
'src/service/class-google-drive.php' => ['PhanTypeMismatchReturnProbablyReal'],
'tests/php/contact-form/Contact_Form_Plugin_Test.php' => ['PhanPluginMixedKeyNoKey'],
'tests/php/contact-form/Util_Test.php' => ['PhanDeprecatedFunction'],
],
// 'directory_suppressions' => ['src/directory_name' => ['PhanIssueName1', 'PhanIssueName2']] can be manually added if needed.
// (directory_suppressions will currently be ignored by subsequent calls to --save-baseline, but may be preserved in future Phan releases)
Expand Down
4 changes: 4 additions & 0 deletions projects/packages/forms/changelog/remove-forms-old-admin
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: major
Type: removed

Forms: Removes classic Admin initialization code
69 changes: 9 additions & 60 deletions projects/packages/forms/src/contact-form/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* Class Admin
*
* Singleton for Grunion admin area support.
*
* This class will be removed in a future version.
*/
class Admin {
/**
Expand All @@ -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 ) {
Expand Down Expand Up @@ -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' ) );
}

/**
Expand Down Expand Up @@ -151,67 +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
);

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++ ) {
_deprecated_function( __METHOD__, 'package-$$next-version$$', 'Automattic\Jetpack\Forms\ContactForm\Contact_Form_Plugin::init()->export_to_gdrive()' );

$current_row = array();

/**
* 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,
)
);
return Contact_Form_Plugin::init()->export_to_gdrive();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Automattic\Jetpack\Constants;
use Automattic\Jetpack\Extensions\Contact_Form\Contact_Form_Block;
use Automattic\Jetpack\Forms\Jetpack_Forms;
use Automattic\Jetpack\Forms\Service\Google_Drive;
use Automattic\Jetpack\Forms\Service\MailPoet_Integration;
use Automattic\Jetpack\Forms\Service\Post_To_Url;
use Automattic\Jetpack\Status;
Expand Down Expand Up @@ -91,6 +92,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';

/**
* Initializing function.
*/
Expand Down Expand Up @@ -215,6 +223,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' ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@enejb - I saw we moved this one. @CGastrell had also flagged the download_feedback_as_csv method, also tied to ajax actions. Do we need to move and still load that outside of this admin class?

}
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'current_screen', array( $this, 'unread_count' ) );
Expand Down Expand Up @@ -3444,4 +3453,75 @@ public function redirect_edit_feedback_to_jetpack_forms() {
wp_safe_redirect( $redirect_url );
exit;
}

/**
* 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() {
$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
);
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[] = $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,
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public static function add_hooks( $screen ) {
* Admin header.
*/
public static function admin_head() {
remove_action( 'media_buttons', array( Admin::init(), 'grunion_media_button' ), 999 );
add_action( 'media_buttons', array( __CLASS__, 'grunion_media_button' ), 999 );
}

Expand Down
3 changes: 0 additions & 3 deletions projects/packages/forms/src/contact-form/class-util.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ class Util {
* Registers all relevant actions and filters for this class.
*/
public static function init() {
if ( is_admin() ) {
Admin::init();
}

add_filter( 'template_include', '\Automattic\Jetpack\Forms\ContactForm\Util::grunion_contact_form_set_block_template_attribute' );

Expand Down
Loading
Loading