Skip to content

Commit 4bb19e4

Browse files
committed
Privacy: Store plugin callbacks in associative array for flexibility.
The personal data export and erasure tools allow plugins to register their own callbacks, in order to add additional data to the export and erasure processes. Previously, these were registered without specifying a constant identifier in the array of callbacks. Using mutable integers makes it difficult for plugins to modify the callbacks of other plugins, though. Using associative array keys instead provides a covenient and reliable way to identify and interact with another plugin's callbacks. Props desrosj, allendav, ocean90. Fixes #43931. git-svn-id: https://develop.svn.wordpress.org/trunk@43154 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 7cca1e9 commit 4bb19e4

6 files changed

Lines changed: 26 additions & 23 deletions

File tree

src/wp-admin/includes/admin-filters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@
133133
add_action( 'upgrader_process_complete', 'wp_update_themes', 10, 0 );
134134

135135
// Privacy hooks
136-
add_filter( 'wp_privacy_personal_data_export_page', 'wp_privacy_process_personal_data_export_page', 10, 6 );
136+
add_filter( 'wp_privacy_personal_data_export_page', 'wp_privacy_process_personal_data_export_page', 10, 7 );
137137
add_action( 'wp_privacy_personal_data_export_file', 'wp_privacy_generate_personal_data_export_file', 10 );
138138

139139
// Privacy policy text changes check.

src/wp-admin/includes/ajax-actions.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4409,24 +4409,24 @@ function wp_ajax_wp_privacy_export_personal_data() {
44094409
wp_send_json_error( __( 'Exporter index out of range.' ) );
44104410
}
44114411

4412-
$index = $exporter_index - 1;
4413-
44144412
if ( $page < 1 ) {
44154413
wp_send_json_error( __( 'Page index cannot be less than one.' ) );
44164414
}
44174415

4418-
$exporter = $exporters[ $index ];
4416+
$exporter_keys = array_keys( $exporters );
4417+
$exporter_key = $exporter_keys[ $exporter_index - 1 ];
4418+
$exporter = $exporters[ $exporter_key ];
44194419

44204420
if ( ! is_array( $exporter ) ) {
44214421
wp_send_json_error(
4422-
/* translators: %d: array index */
4423-
sprintf( __( 'Expected an array describing the exporter at index %d.' ), $exporter_index )
4422+
/* translators: %s: array index */
4423+
sprintf( __( 'Expected an array describing the exporter at index %s.' ), $exporter_key )
44244424
);
44254425
}
44264426
if ( ! array_key_exists( 'exporter_friendly_name', $exporter ) ) {
44274427
wp_send_json_error(
4428-
/* translators: %d: array index */
4429-
sprintf( __( 'Exporter array at index %d does not include a friendly name.' ), $exporter_index )
4428+
/* translators: %s: array index */
4429+
sprintf( __( 'Exporter array at index %s does not include a friendly name.' ), $exporter_key )
44304430
);
44314431
}
44324432
if ( ! array_key_exists( 'callback', $exporter ) ) {
@@ -4442,8 +4442,8 @@ function wp_ajax_wp_privacy_export_personal_data() {
44424442
);
44434443
}
44444444

4445-
$callback = $exporters[ $index ]['callback'];
4446-
$exporter_friendly_name = $exporters[ $index ]['exporter_friendly_name'];
4445+
$callback = $exporter['callback'];
4446+
$exporter_friendly_name = $exporter['exporter_friendly_name'];
44474447

44484448
$response = call_user_func( $callback, $email_address, $page );
44494449
if ( is_wp_error( $response ) ) {
@@ -4495,8 +4495,9 @@ function wp_ajax_wp_privacy_export_personal_data() {
44954495
* @param int $page The page for this response.
44964496
* @param int $request_id The privacy request post ID associated with this request.
44974497
* @param bool $send_as_email Whether the final results of the export should be emailed to the user.
4498+
* @param int $exporter_key The key (slug) of the exporter that provided this data.
44984499
*/
4499-
$response = apply_filters( 'wp_privacy_personal_data_export_page', $response, $exporter_index, $email_address, $page, $request_id, $send_as_email );
4500+
$response = apply_filters( 'wp_privacy_personal_data_export_page', $response, $exporter_index, $email_address, $page, $request_id, $send_as_email, $exporter_key );
45004501

45014502
if ( is_wp_error( $response ) ) {
45024503
wp_send_json_error( $response );
@@ -4591,8 +4592,9 @@ function wp_ajax_wp_privacy_erase_personal_data() {
45914592
wp_send_json_error( __( 'Page index cannot be less than one.' ) );
45924593
}
45934594

4594-
$index = $eraser_index - 1; // Convert to zero based for eraser index.
4595-
$eraser = $erasers[ $index ];
4595+
$eraser_keys = array_keys( $erasers );
4596+
$eraser_key = $eraser_keys[ $eraser_index - 1 ];
4597+
$eraser = $erasers[ $eraser_key ];
45964598

45974599
if ( ! is_array( $eraser ) ) {
45984600
/* translators: %d: array index */
@@ -4614,8 +4616,8 @@ function wp_ajax_wp_privacy_erase_personal_data() {
46144616
wp_send_json_error( sprintf( __( 'Eraser array at index %d does not include a friendly name.' ), $eraser_index ) );
46154617
}
46164618

4617-
$callback = $erasers[ $index ]['callback'];
4618-
$eraser_friendly_name = $erasers[ $index ]['eraser_friendly_name'];
4619+
$callback = $eraser['callback'];
4620+
$eraser_friendly_name = $eraser['eraser_friendly_name'];
46194621

46204622
$response = call_user_func( $callback, $email_address, $page );
46214623

@@ -4706,12 +4708,13 @@ function wp_ajax_wp_privacy_erase_personal_data() {
47064708
* @since 4.9.6
47074709
*
47084710
* @param array $response The personal data for the given exporter and page.
4709-
* @param int $exporter_index The index of the exporter that provided this data.
4711+
* @param int $eraser_index The index of the eraser that provided this data.
47104712
* @param string $email_address The email address associated with this personal data.
47114713
* @param int $page The page for this response.
47124714
* @param int $request_id The privacy request post ID associated with this request.
4715+
* @param int $eraser_key The key (slug) of the eraser that provided this data.
47134716
*/
4714-
$response = apply_filters( 'wp_privacy_personal_data_erasure_page', $response, $eraser_index, $email_address, $page, $request_id );
4717+
$response = apply_filters( 'wp_privacy_personal_data_erasure_page', $response, $eraser_index, $email_address, $page, $request_id, $eraser_key );
47154718

47164719
if ( is_wp_error( $response ) ) {
47174720
wp_send_json_error( $response );

src/wp-admin/includes/file.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2262,9 +2262,10 @@ function wp_privacy_send_personal_data_export_email( $request_id ) {
22622262
* @param int $page The page of personal data for this exporter. Begins at 1.
22632263
* @param int $request_id The request ID for this personal data export.
22642264
* @param bool $send_as_email Whether the final results of the export should be emailed to the user.
2265+
* @param string $exporter_key The slug (key) of the exporter.
22652266
* @return array The filtered response.
22662267
*/
2267-
function wp_privacy_process_personal_data_export_page( $response, $exporter_index, $email_address, $page, $request_id, $send_as_email ) {
2268+
function wp_privacy_process_personal_data_export_page( $response, $exporter_index, $email_address, $page, $request_id, $send_as_email, $exporter_key ) {
22682269
/* Do some simple checks on the shape of the response from the exporter.
22692270
* If the exporter response is malformed, don't attempt to consume it - let it
22702271
* pass through to generate a warning to the user by default ajax processing.

src/wp-includes/comment.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3285,7 +3285,7 @@ function wp_handle_comment_submission( $comment_data ) {
32853285
* @return array $exporters An array of personal data exporters.
32863286
*/
32873287
function wp_register_comment_personal_data_exporter( $exporters ) {
3288-
$exporters[] = array(
3288+
$exporters['wordpress-comments'] = array(
32893289
'exporter_friendly_name' => __( 'WordPress Comments' ),
32903290
'callback' => 'wp_comments_personal_data_exporter',
32913291
);
@@ -3390,7 +3390,7 @@ function wp_comments_personal_data_exporter( $email_address, $page = 1 ) {
33903390
* @return array $erasers An array of personal data erasers.
33913391
*/
33923392
function wp_register_comment_personal_data_eraser( $erasers ) {
3393-
$erasers[] = array(
3393+
$erasers['wordpress-comments'] = array(
33943394
'eraser_friendly_name' => __( 'WordPress Comments' ),
33953395
'callback' => 'wp_comments_personal_data_eraser',
33963396
);
@@ -3498,4 +3498,3 @@ function wp_comments_personal_data_eraser( $email_address, $page = 1 ) {
34983498
'done' => $done,
34993499
);
35003500
}
3501-

src/wp-includes/media.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4102,7 +4102,7 @@ function wpview_media_sandbox_styles() {
41024102
* @return array An array of personal data exporters.
41034103
*/
41044104
function wp_register_media_personal_data_exporter( $exporters ) {
4105-
$exporters[] = array(
4105+
$exporters['wordpress-media'] = array(
41064106
'exporter_friendly_name' => __( 'WordPress Media' ),
41074107
'callback' => 'wp_media_personal_data_exporter',
41084108
);

src/wp-includes/user.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2834,7 +2834,7 @@ function _wp_privacy_action_request_types() {
28342834
* @return array An array of personal data exporters.
28352835
*/
28362836
function wp_register_user_personal_data_exporter( $exporters ) {
2837-
$exporters[] = array(
2837+
$exporters['wordpress-user'] = array(
28382838
'exporter_friendly_name' => __( 'WordPress User' ),
28392839
'callback' => 'wp_user_personal_data_exporter',
28402840
);

0 commit comments

Comments
 (0)