Make WordPress Core


Ignore:
Timestamp:
05/03/2018 07:37:32 PM (6 years ago)
Author:
azaozz
Message:

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.
Merges [43154] to the 4.9 branch.
Fixes #43931.

Location:
branches/4.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/src/wp-admin/includes/ajax-actions.php

    r43111 r43157  
    41004100        }
    41014101
    4102         $index = $exporter_index - 1;
    4103 
    41044102        if ( $page < 1 ) {
    41054103            wp_send_json_error( __( 'Page index cannot be less than one.' ) );
    41064104        }
    41074105
    4108         $exporter = $exporters[ $index ];
     4106        $exporter_keys = array_keys( $exporters );
     4107        $exporter_key  = $exporter_keys[ $exporter_index - 1 ];
     4108        $exporter      = $exporters[ $exporter_key ];
    41094109
    41104110        if ( ! is_array( $exporter ) ) {
    41114111            wp_send_json_error(
    4112                 /* translators: %d: array index */
    4113                 sprintf( __( 'Expected an array describing the exporter at index %d.' ), $exporter_index )
     4112                /* translators: %s: array index */
     4113                sprintf( __( 'Expected an array describing the exporter at index %s.' ), $exporter_key )
    41144114            );
    41154115        }
    41164116        if ( ! array_key_exists( 'exporter_friendly_name', $exporter ) ) {
    41174117            wp_send_json_error(
    4118                 /* translators: %d: array index */
    4119                 sprintf( __( 'Exporter array at index %d does not include a friendly name.' ), $exporter_index )
     4118                /* translators: %s: array index */
     4119                sprintf( __( 'Exporter array at index %s does not include a friendly name.' ), $exporter_key )
    41204120            );
    41214121        }
     
    41334133        }
    41344134
    4135         $callback = $exporters[ $index ]['callback'];
    4136         $exporter_friendly_name = $exporters[ $index ]['exporter_friendly_name'];
     4135        $callback               = $exporter['callback'];
     4136        $exporter_friendly_name = $exporter['exporter_friendly_name'];
    41374137
    41384138        $response = call_user_func( $callback, $email_address, $page );
     
    41864186     * @param int    $request_id      The privacy request post ID associated with this request.
    41874187     * @param bool   $send_as_email   Whether the final results of the export should be emailed to the user.
     4188     * @param int    $exporter_key    The key (slug) of the exporter that provided this data.
    41884189     */
    4189     $response = apply_filters( 'wp_privacy_personal_data_export_page', $response, $exporter_index, $email_address, $page, $request_id, $send_as_email );
     4190    $response = apply_filters( 'wp_privacy_personal_data_export_page', $response, $exporter_index, $email_address, $page, $request_id, $send_as_email, $exporter_key );
    41904191
    41914192    if ( is_wp_error( $response ) ) {
     
    42824283        }
    42834284
    4284         $index  = $eraser_index - 1; // Convert to zero based for eraser index.
    4285         $eraser = $erasers[ $index ];
     4285        $eraser_keys = array_keys( $erasers );
     4286        $eraser_key  = $eraser_keys[ $eraser_index - 1 ];
     4287        $eraser      = $erasers[ $eraser_key ];
    42864288
    42874289        if ( ! is_array( $eraser ) ) {
     
    43054307        }
    43064308
    4307         $callback             = $erasers[ $index ]['callback'];
    4308         $eraser_friendly_name = $erasers[ $index ]['eraser_friendly_name'];
     4309        $callback             = $eraser['callback'];
     4310        $eraser_friendly_name = $eraser['eraser_friendly_name'];
    43094311
    43104312        $response = call_user_func( $callback, $email_address, $page );
     
    43974399     *
    43984400     * @param array  $response        The personal data for the given exporter and page.
    4399      * @param int    $exporter_index  The index of the exporter that provided this data.
     4401     * @param int    $eraser_index    The index of the eraser that provided this data.
    44004402     * @param string $email_address   The email address associated with this personal data.
    44014403     * @param int    $page            The page for this response.
    44024404     * @param int    $request_id      The privacy request post ID associated with this request.
     4405     * @param int    $eraser_key      The key (slug) of the eraser that provided this data.
    44034406     */
    4404     $response = apply_filters( 'wp_privacy_personal_data_erasure_page', $response, $eraser_index, $email_address, $page, $request_id );
     4407    $response = apply_filters( 'wp_privacy_personal_data_erasure_page', $response, $eraser_index, $email_address, $page, $request_id, $eraser_key );
    44054408
    44064409    if ( is_wp_error( $response ) ) {
Note: See TracChangeset for help on using the changeset viewer.