Make WordPress Core

Ticket #43797: 43797.diff

File 43797.diff, 11.5 KB (added by xkon, 8 years ago)

introducing consent log tool

  • src/wp-admin/admin-ajax.php

     
    131131        'edit-theme-plugin-file',
    132132        'wp-privacy-export-personal-data',
    133133        'wp-privacy-erase-personal-data',
     134        'consent-log-remove-consents',
    134135);
    135136
    136137// Deprecated
  • src/wp-admin/includes/admin-filters.php

     
    4848// Privacy tools
    4949add_action( 'account_action_failed', '_wp_privacy_account_request_failed' );
    5050add_action( 'admin_menu', '_wp_privacy_hook_requests_page' );
     51add_action( 'admin_menu', '_wp_privacy_add_consent_log_submenu_page' );
    5152
    5253// Prerendering.
    5354if ( ! is_customize_preview() ) {
  • src/wp-admin/includes/ajax-actions.php

     
    46374637
    46384638        wp_send_json_success( $response );
    46394639}
     4640
     4641/**
     4642 * Ajax for removing consents of a User ID
     4643 *
     4644 * @since 4.9.6
     4645 *
     4646 * @uses sanitize_text_field()
     4647 * @uses WP_Query()
     4648 * @uses have_posts()
     4649 * @uses the_post()
     4650 * @uses wp_delete_post()
     4651 * @uses get_the_ID()
     4652 * @uses wp_send_json_success()
     4653 */
     4654function wp_ajax_consent_log_remove_consents() {
     4655
     4656        $uid = sanitize_text_field( $_POST['uid'] );
     4657
     4658        $args = array(
     4659                'post_type'      => 'consent_log',
     4660                'posts_per_page' => '-1',
     4661                'post_status'    => array( 'consent_accepted', 'consent_declined' ),
     4662                'meta_query'     => array(
     4663                        '_user_email' => array(
     4664                                'key'   => '_cl_uid',
     4665                                'value' => $uid,
     4666                        ),
     4667                ),
     4668        );
     4669
     4670        $query = new WP_Query( $args );
     4671
     4672        if ( $query->have_posts() ) {
     4673                while ( $query->have_posts() ) {
     4674                        $query->the_post();
     4675
     4676                        wp_delete_post( get_the_ID() );
     4677                }
     4678        }
     4679
     4680        wp_send_json_success();
     4681}
  • src/wp-admin/includes/user.php

     
    15231523        }
    15241524
    15251525}
     1526
     1527
     1528/**
     1529 * Add Tools submenu page for Consent Log.
     1530 *
     1531 * @since 4.9.6
     1532 *
     1533 * @uses add_submenu_page()
     1534 *
     1535 */
     1536function _wp_privacy_add_consent_log_submenu_page() {
     1537        add_submenu_page( 'tools.php', __( 'Consent Log' ), __( 'Consent Log' ), 'manage_options', 'consent_log', '_wp_privacy_consent_log_page' );
     1538}
     1539
     1540/**
     1541 * Creates the page for Consent Log
     1542 *
     1543 * @since 4.9.6
     1544 *
     1545 * @uses esc_attr_e()
     1546 * @uses WP_Query()
     1547 * @uses have_posts()
     1548 * @uses the_post()
     1549 * @uses esc_html()
     1550 * @uses get_post_meta()
     1551 * @uses get_the_ID()
     1552 * @uses get_the_date()
     1553 * @uses get_the_time()
     1554 *
     1555 * @return void
     1556 */
     1557
     1558function _wp_privacy_consent_log_page() {
     1559
     1560        if ( ! current_user_can( 'delete_users' ) ) {
     1561                wp_die( esc_html__( 'Sorry, you are not allowed to manage privacy on this site.' ) );
     1562        }
     1563
     1564        // "Borrow" xfn.js for now so we don't have to create new files.
     1565        wp_enqueue_script( 'xfn' );
     1566
     1567        ?>
     1568        <div class="wrap">
     1569        <h1>Consent Log</h1>
     1570        <hr class="wp-header-end">
     1571        <form method="post" class="consents-log-remove-form" id="consents-log-remove-form">
     1572                <h2><?php esc_html_e( 'Remove all the consents of a user.' ); ?></h2>
     1573                <label for="user_id_to_remove_consent"><?php esc_html_e( 'User ID' ); ?></label>
     1574                <input type="text" required class="regular-text" id="user_id_to_remove_consent" name="user_id_to_remove_consent" />
     1575                <?php submit_button( __( 'Remove' ), 'secondary', 'submit', false ); ?>
     1576        </form>
     1577        <hr />
     1578        <table class="widefat striped">
     1579                <thead>
     1580                        <tr>
     1581                                <th><?php esc_attr_e( 'User ID' ); ?></th>
     1582                                <th><?php esc_attr_e( 'Consent ID' ); ?></th>
     1583                                <th><?php esc_attr_e( 'Status' ); ?></th>
     1584                                <th><?php esc_attr_e( 'Date - Time' ); ?></th>
     1585                        </tr>
     1586                </thead>
     1587                <tfoot>
     1588                        <tr>
     1589                                <td><?php esc_attr_e( 'User ID' ); ?></td>
     1590                                <td><?php esc_attr_e( 'Consent ID' ); ?></td>
     1591                                <td><?php esc_attr_e( 'Status' ); ?></td>
     1592                                <td><?php esc_attr_e( 'Date - Time' ); ?></td>
     1593                        </tr>
     1594                </tfoot>
     1595                <?php
     1596                $args = array(
     1597                        'post_type'      => 'consent_log',
     1598                        'posts_per_page' => '-1',
     1599                        'post_status'    => array( 'consent_accepted', 'consent_declined' ),
     1600                );
     1601
     1602                $query = new WP_Query( $args );
     1603
     1604                if ( $query->have_posts() ) {
     1605                        while ( $query->have_posts() ) {
     1606                                $query->the_post();
     1607                        ?>
     1608                                <tr>
     1609                                        <td class="row-title">
     1610                                                <?php
     1611                                                echo esc_html( get_post_meta( get_the_ID(), '_cl_uid', true ) );
     1612                                                ?>
     1613                                        </td>
     1614                                        <td>
     1615                                                <?php
     1616                                                echo esc_html( get_post_meta( get_the_ID(), '_cl_cid', true ) );
     1617                                                ?>
     1618                                        </td>
     1619                                        <td>
     1620                                                <?php
     1621                                                if ( 'consent_accepted' === get_post_status( get_the_ID() ) ) {
     1622                                                        _e( 'Accepted' );
     1623                                                } else {
     1624                                                        _e( 'Declined' );
     1625                                                }
     1626                                                ?>
     1627                                        </td>
     1628                                        <td>
     1629                                                <?php
     1630                                                echo get_the_date() . ' - ' . get_the_time();
     1631                                                ?>
     1632                                        </td>
     1633                                </tr>
     1634                        <?php
     1635                        }
     1636                }
     1637                ?>
     1638        </table>
     1639        <?php
     1640}
     1641
     1642/**
     1643 * Consent Log class - A class for logging consents in a consent_log custom post-type.
     1644 *
     1645 * @since 4.9.6
     1646 */
     1647class Consent_Log {
     1648
     1649        /**
     1650         * Checks if the consent exists in the CPT
     1651         *
     1652         * @uses sanitize_text_field()
     1653         * @uses WP_Query
     1654         * @uses have_posts()
     1655         * @uses the_post()
     1656         * @uses get_the_ID()
     1657         *
     1658         * @param string $uid The user's email address.
     1659         * @param string $cid Consent ID.
     1660         *
     1661         * @return mixed consent id if exists | false if there's no consent
     1662         */
     1663        public function consent_exists( $uid, $cid ) {
     1664
     1665                $uid = sanitize_text_field( $uid );
     1666                $cid = sanitize_text_field( $cid );
     1667
     1668                $args = array(
     1669                        'post_type'      => 'consent_log',
     1670                        'posts_per_page' => '1',
     1671                        'post_status'    => array( 'consent_accepted', 'consent_declined' ),
     1672                        'meta_query'     => array(
     1673                                'relation'            => 'AND',
     1674                                '_user_email'         => array(
     1675                                        'key'   => '_cl_uid',
     1676                                        'value' => $uid,
     1677                                ),
     1678                                '_consent_identifier' => array(
     1679                                        'key'   => '_cl_cid',
     1680                                        'value' => $cid,
     1681                                ),
     1682                        ),
     1683                );
     1684
     1685                $query = new WP_Query( $args );
     1686
     1687                if ( $query->have_posts() ) {
     1688                        while ( $query->have_posts() ) {
     1689                                $query->the_post();
     1690                                return get_the_ID();
     1691                        }
     1692                }
     1693
     1694                return false;
     1695        }
     1696
     1697        /**
     1698         * Checks the consent is of status 1=accepted
     1699         *
     1700         * @uses sanitize_text_field()
     1701         * @uses Consent_Log::consent_exists()
     1702         * @uses get_post_meta()
     1703         *
     1704         * @param string $uid The user's email address.
     1705         * @param string $cid Consent ID.
     1706         *
     1707         * @return boolean true/false depending if the consent is accepted
     1708         */
     1709        public function has_consent( $uid, $cid ) {
     1710
     1711                $uid = sanitize_text_field( $uid );
     1712                $cid = sanitize_text_field( $cid );
     1713
     1714                $exists = $this->consent_exists( $uid, $cid );
     1715
     1716                if ( $exists ) {
     1717                        if ( 'consent_accepted' === get_post_status( $exists ) ) {
     1718                                return true;
     1719                        }
     1720                }
     1721
     1722                return false;
     1723        }
     1724
     1725        /**
     1726         * Adds a new consent in the CPT
     1727         *
     1728         * @uses sanitize_text_field()
     1729         * @uses intval()
     1730         * @uses Consent_Log::consent_exists()
     1731         * @uses wp_insert_post()
     1732         * @uses current_time()
     1733         * @uses update_post_meta()
     1734         *
     1735         * @param string $uid The user's email address.
     1736         * @param string $cid Consent ID.
     1737         * @param mixed  $sid Consent Status.
     1738         *
     1739         * @return boolean true/false depending if the consent is updated
     1740         */
     1741        public function create_consent( $uid, $cid, $sid ) {
     1742
     1743                $uid = sanitize_text_field( $uid );
     1744                $cid = sanitize_text_field( $cid );
     1745                $sid = sanitize_text_field( $sid );
     1746
     1747                $exists = $this->consent_exists( $uid, $cid );
     1748
     1749                if ( ! $exists ) {
     1750
     1751                        $user_id = 0;
     1752
     1753                        $consent = wp_insert_post(
     1754                                array(
     1755                                        'post_author'   => $user_id,
     1756                                        'post_status'   => $sid,
     1757                                        'post_type'     => 'consent_log',
     1758                                        'post_date'     => current_time( 'mysql', false ),
     1759                                        'post_date_gmt' => current_time( 'mysql', true ),
     1760                                ), true
     1761                        );
     1762
     1763                        update_post_meta( $consent, '_cl_uid', $uid );
     1764                        update_post_meta( $consent, '_cl_cid', $cid );
     1765
     1766                        return true;
     1767                }
     1768
     1769                return false;
     1770        }
     1771
     1772        /**
     1773         * Delete a consent from the CPT
     1774         *
     1775         * @uses sanitize_text_field()
     1776         * @uses Consent_Log::consent_exists()
     1777         * @uses wp_delete_post()
     1778         * @uses current_time()
     1779         *
     1780         * @param string $uid The user's email address.
     1781         * @param string $cid Consent ID.
     1782         *
     1783         * @return boolean true/false depending if the consent is deleted
     1784         */
     1785        public function remove_consent( $uid, $cid ) {
     1786
     1787                $uid = sanitize_text_field( $uid );
     1788                $cid = sanitize_text_field( $cid );
     1789
     1790                $exists = $this->consent_exists( $uid, $cid );
     1791
     1792                if ( $exists ) {
     1793
     1794                        wp_delete_post( $exists );
     1795
     1796                        return true;
     1797                }
     1798
     1799                return false;
     1800        }
     1801
     1802        /**
     1803         * Update a consent from the CPT
     1804         *
     1805         * @uses sanitize_text_field()
     1806         * @uses intval()
     1807         * @uses Consent_Log::consent_exists()
     1808         * @uses wp_update_post()
     1809         *
     1810         * @param string $uid The user's email address.
     1811         * @param string $cid Consent ID.
     1812         * @param mixed  $sid Consent Status.
     1813         *
     1814         * @return boolean true/false depending if the consent is updated
     1815         */
     1816        public function update_consent( $uid, $cid, $sid ) {
     1817
     1818                $uid = sanitize_text_field( $uid );
     1819                $cid = sanitize_text_field( $cid );
     1820                $sid = sanitize_text_field( $sid );
     1821
     1822                $exists = $this->consent_exists( $uid, $cid );
     1823
     1824                if ( $exists ) {
     1825
     1826                        $args = array(
     1827                                'ID'          => $exists,
     1828                                'post_status' => $sid,
     1829                        );
     1830
     1831                        wp_update_post( $args );
     1832
     1833                        return true;
     1834                }
     1835                return false;
     1836        }
     1837}
  • src/wp-admin/js/xfn.js

     
    140140
    141141                do_next_erasure( 1, 1 );
    142142        } );
     143
     144        $( '#consents-log-remove-form' ).submit( function( e ) {
     145                var data,
     146                        uid = $( '#user_id_to_remove_consent' ).val();
     147
     148                e.preventDefault();
     149
     150                data = {
     151                        'action': 'consent-log-remove-consents',
     152                        'uid': uid,
     153                };
     154
     155                $.post(
     156                        ajaxurl,
     157                        data,
     158                        function( response ) {
     159                                if ( true === response.success ) {
     160                                        window.location.href = window.location.href;
     161                                }
     162                        });
     163        });
    143164} );
  • src/wp-includes/post.php

     
    258258                )
    259259        );
    260260
     261        register_post_type(
     262                'consent_logs', array(
     263                        'labels'           => array(
     264                                'name'          => __( 'Consent Log' ),
     265                                'singular_name' => __( 'Consent Log' ),
     266                        ),
     267                        'public'           => false,
     268                        '_builtin'         => true, /* internal use only. don't use this when registering your own post type. */
     269                        'hierarchical'     => false,
     270                        'rewrite'          => false,
     271                        'query_var'        => false,
     272                        'can_export'       => false,
     273                        'delete_with_user' => false,
     274                )
     275        );
     276
    261277        register_post_status(
    262278                'publish', array(
    263279                        'label'       => _x( 'Published', 'post status' ),
     
    365381                        'exclude_from_search' => false,
    366382                )
    367383        );
     384
     385        register_post_status(
     386                'consent-accepted', array(
     387                        'label'               => _x( 'Accepted', 'consent status' ),
     388                        'internal'            => true,
     389                        '_builtin'            => true, /* internal use only. */
     390                        'exclude_from_search' => false,
     391                )
     392        );
     393
     394        register_post_status(
     395                'consent-declined', array(
     396                        'label'               => _x( 'Declined', 'consent status' ),
     397                        'internal'            => true,
     398                        '_builtin'            => true, /* internal use only. */
     399                        'exclude_from_search' => false,
     400                )
     401        );
    368402}
    369403
    370404/**