Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for WP personal data exporter & eraser #1355 #1356

Merged
merged 15 commits into from
Oct 21, 2023
Merged
18 changes: 16 additions & 2 deletions src/Model/Booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,6 @@ public function bookingLinkUrl(): string {
* Returns true when booking is cancelled. This might not correctly reflect the status of the booking when $this->cancel() has been called.
* In order to correctly reflect this, you need to call wp_cache_flush() before calling this function.
*
*
*
* @return bool
*/
public function isCancelled(): bool {
Expand Down Expand Up @@ -586,4 +584,20 @@ public function isConfirmed() : bool {
public function isUnconfirmed() : bool {
return $this->post_status === 'unconfirmed';
}

/**
* Will get the status of a booking as a human-readable string
* @return string
*/
public function getStatus() : string {
if ( $this->isConfirmed() ) {
return __( 'Confirmed', 'commonsbooking' );
} elseif ( $this->isUnconfirmed() ) {
return __( 'Unconfirmed', 'commonsbooking' );
} elseif ( $this->isCancelled() ) {
return __( 'Cancelled', 'commonsbooking' );
} else {
return __( 'Unknown', 'commonsbooking' );
}
}
}
2 changes: 1 addition & 1 deletion src/Model/CustomPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public function getDate() {

/**
* Returns user data.
* @return mixed
* @return false|\WP_User
*/
public function getUserData() {
return get_userdata( $this->post_author );
Expand Down
36 changes: 36 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,36 @@ public static function registerAdminOptions() {
}
}

/**
* Registers all user data exporters ({@link https://developer.wordpress.org/plugins/privacy/adding-the-personal-data-exporter-to-your-plugin/}).
*
* @param array $exporters
*
* @return mixed
*/
public static function registerUserDataExporters( $exporters ) {
$exporters[COMMONSBOOKING_PLUGIN_SLUG] = array(
'exporter_friendly_name' => __( 'CommonsBooking Bookings', 'commonsbooking' ),
'callback' => array( \CommonsBooking\Wordpress\CustomPostType\Booking::class, 'exportUserBookingsByEmail' ),
);
return $exporters;
}

/**
* Registers all user data erasers ({@link https://developer.wordpress.org/plugins/privacy/adding-the-personal-data-eraser-to-your-plugin/}).
*
* @param $erasers
*
* @return mixed
*/
public static function registerUserDataErasers( $erasers ) {
$erasers[COMMONSBOOKING_PLUGIN_SLUG] = array(
'eraser_friendly_name' => __( 'CommonsBooking Bookings', 'commonsbooking' ),
'callback' => array( \CommonsBooking\Wordpress\CustomPostType\Booking::class, 'removeUserBookingsByEmail'),
);
return $erasers;
}

/**
* Gets location position for locations without coordinates.
*/
Expand Down Expand Up @@ -608,6 +638,12 @@ function ( $plugin_data ) {
return (COMMONSBOOKING_PLUGIN_URL . '/vendor/ed-itsolutions/cmb2-field-ajax-search/');
});

//hook into WordPress personal data exporter
add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'registerUserDataExporters' ) );

//hook into WordPress personal data eraser
add_filter( 'wp_privacy_personal_data_erasers', array( $this, 'registerUserDataErasers' ) );

// iCal rewrite
iCalendar::initRewrite();

Expand Down
120 changes: 77 additions & 43 deletions src/Repository/Booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,7 @@ public static function getEndingBookingsByDate( int $timestamp, array $customArg
// Overwrite args with passed custom args
$args = array_merge( $args, $customArgs );

$query = new WP_Query( $args );
if ( $query->have_posts() ) {
$posts = $query->get_posts();

// Filter by post_status, query seems not to work reliable
$posts = array_filter(
$posts,
function ( $post ) use ( $args ) {
return in_array( $post->post_status, $args['post_status'] );
}
);

foreach ( $posts as &$post ) {
$post = new \CommonsBooking\Model\Booking( $post );
}

return $posts;
}

return [];
return self::getModelsFromQuery( $args );
}

/**
Expand Down Expand Up @@ -293,27 +274,7 @@ public static function getByTimerange(
$args = array_merge( $args, $customArgs );



$query = new WP_Query( $args );
if ( $query->have_posts() ) {
$posts = $query->get_posts();

// Filter by post_status, query seems not to work reliable
$posts = array_filter(
$posts,
function ( $post ) use ( $args ) {
return in_array( $post->post_status, $args['post_status'] );
}
);

foreach ( $posts as &$post ) {
$post = new \CommonsBooking\Model\Booking( $post );
}

return $posts;
}

return [];
return self::getModelsFromQuery( $args );
}

/**
Expand All @@ -325,7 +286,7 @@ function ( $post ) use ( $args ) {
* @return array
* @throws Exception
*/
public static function getForUser( $user, bool $asModel = false, $startDate = null ): array {
public static function getForUser( \WP_User $user, bool $asModel = false, $startDate = null ): array {
$customId = $user->ID;

$cacheItem = Plugin::getCacheItem( $customId );
Expand Down Expand Up @@ -380,7 +341,8 @@ public static function getForCurrentUser( bool $asModel = false, $startDate = nu
}

/**
* Returns bookings.
* Returns bookings. This uses the CommonsBooking\Repository\Timeframe::get() method which
* is not based on the WP_Query class but will perform its own SQL query.
*
* @param array $locations
* @param array $items
Expand Down Expand Up @@ -411,6 +373,47 @@ public static function get(
);
}

/**
* We use this function instead of the getForUser() function when we need to paginate the results.
* This is to prevent timeouts for bigger queries such as data exports. As opposed to the getForUser() function,
* this function will use the WP_Query class to perform the query allowing us to use the pagination features of WP_Query.
*
* @param \WP_User $user The user for which to get the bookings.
* @param int $page The current page that is processed.
* @param int $perPage The number of bookings per page. A lower number will result in faster queries.
* @param array $customArgs Valid WP_Query args array.
*
* @return array
Copy link
Contributor

Choose a reason for hiding this comment

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

Hier fehlt noch Model/Booking type annotation

*/
public static function getForUserPaginated(
\WP_User $user,
int $page = 1,
int $perPage = 10,
$customArgs = [],
$postStatus = [ 'confirmed', 'unconfirmed', 'canceled' , 'publish', 'inherit' ]
): array {
$args = array(
'author' => $user->ID,
'post_type' => \CommonsBooking\Wordpress\CustomPostType\Booking::$postType,
'meta_query' => array(
array(
'key' => 'type',
'value' => Timeframe::BOOKING_ID,
'compare' => '=',
),
),
'post_status' => $postStatus,
'posts_per_page' => $perPage,
'paged' => $page,
'orderby' => 'ID',
'order' => 'ASC',
);
// Overwrite args with passed custom args
$args = array_merge( $args, $customArgs );

return self::getModelsFromQuery( $args );
}

/**
* Gets all bookings that are affected by the given restriction.
*
Expand Down Expand Up @@ -467,4 +470,35 @@ public static function getExistingBookings( $itemId, $locationId, $startDate, $e

}

/**
* Will take a valid WP_Query args array and return an array of Booking models.
*
* @param array $args
*
* @return \CommonsBooking\Model\Booking[]
* @throws Exception
*/
private static function getModelsFromQuery( array $args ): array {
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
$posts = $query->get_posts();

// Filter by post_status, query seems not to work reliable
$posts = array_filter(
$posts,
function ( $post ) use ( $args ) {
return in_array( $post->post_status, $args['post_status'] );
}
);

foreach ( $posts as &$post ) {
$post = new \CommonsBooking\Model\Booking( $post );
}

return $posts;
}

return [];
}

}
1 change: 1 addition & 0 deletions src/Repository/Timeframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public static function getBookableForCurrentUser(
* Why? We have different types of timeframes and in some cases we need multiple of them.
* In this case we need this function.
* Other functions use this one as base function for more specialized searches.
* This function is not based on the WP_Query class, probably because of performance reasons.
Copy link
Contributor

Choose a reason for hiding this comment

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

Vielleicht eher ein TODO Investigate 😆 würde mich aber auch mal interessieren.

*
* @param array $locations
* @param array $items
Expand Down
9 changes: 8 additions & 1 deletion src/View/Booking.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ public static function getTemplateData(): void {
}

/**
* @param int $postsPerPage
* @param \WP_User|null $user
*
* @return array|false|mixed
* @throws Exception
*/
public static function getBookingListData($postsPerPage = 6, $user = null) {
public static function getBookingListData( int $postsPerPage = 6, \WP_User $user = null) {

//sets selected user to current user when no specific user is passed
if ($user == null) {
Expand Down Expand Up @@ -326,6 +329,10 @@ public static function getBookingListiCal($user = null):String{

$user = get_user_by('id', $user);

if (!$user){
return false;
}

$bookingList = self::getBookingListData(999,$user);

//returns false when booking list is empty
Expand Down
Loading