| | 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 | */ |
| | 1536 | function _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 | |
| | 1558 | function _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 | */ |
| | 1647 | class 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 | } |