Skip to content

Commit

Permalink
remove #1418 (moved to separate PR)
Browse files Browse the repository at this point in the history
  • Loading branch information
hansmorb committed Nov 21, 2023
1 parent 01b7e77 commit 2ddceff
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 87 deletions.
2 changes: 1 addition & 1 deletion src/Model/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function getWeeks(): array {
}

// set cache expiration to force daily fresh after midnight
Plugin::setCacheItem( $weeks, array( 'misc' ), $customId, true, 'midnight' );
Plugin::setCacheItem( $weeks, array( 'misc' ), $customId, 'midnight' );

return $weeks;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Week.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function getDays() {
}

// set cache expiration to force daily fresh after midnight
Plugin::setCacheItem( $days, array( 'misc' ), $customId, true, 'midnight' );
Plugin::setCacheItem( $days, array( 'misc' ), $customId, 'midnight' );

return $days;
}
Expand Down
13 changes: 5 additions & 8 deletions src/Repository/Timeframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public static function getPostIdsByType( array $types = [], array $items = [], a
}

$customId = md5( serialize( $types ) );
$cacheItem = Plugin::getCacheItem( $customId, false );
$cacheItem = Plugin::getCacheItem( $customId );
if ( $cacheItem ) {
return $cacheItem;
} else {
Expand Down Expand Up @@ -252,9 +252,8 @@ public static function getPostIdsByType( array $types = [], array $items = [], a

Plugin::setCacheItem(
$postIds,
Wordpress::getTags( $posts, $items, $locations ),
$customId,
false
Wordpress::getTags($posts, $items, $locations),
$customId
);

return $postIds;
Expand All @@ -274,7 +273,7 @@ public static function getPostIdsByType( array $types = [], array $items = [], a
* @return array
*/
private static function getPostsByBaseParams( ?string $date, ?int $minTimestamp, ?int $maxTimestamp, array $postIds, array $postStatus ): array {
$cacheItem = Plugin::getCacheItem('', false);
$cacheItem = Plugin::getCacheItem();
if ( $cacheItem ) {
return $cacheItem;
} else {
Expand Down Expand Up @@ -313,9 +312,7 @@ private static function getPostsByBaseParams( ?string $date, ?int $minTimestamp,

Plugin::setCacheItem(
$posts,
Wordpress::getTags( $posts, $postIds ),
'',
false
Wordpress::getTags($posts, $postIds)
);

return $posts;
Expand Down
30 changes: 1 addition & 29 deletions src/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,7 @@ public static function getOwners(): array {
}

/**
* Why? Because we have different kinds of caches for a given role type.
* The different caches right now only differentiate between privileged and unprivileged users and managers.
* Depending on the items the manager have been assigned, they could be privileged to some items and unprivileged to others.
* That's why we need to get the user ID for the managers.
* @param \WP_User|null $user
*
* @return int 0 = unprivileged, 1 = privileged, other = manager
*/
public static function getRoleTypeID( \WP_User $user = null ): int {
if ( $user == null ) {
$user = wp_get_current_user();
if ( ! $user ) {
return 0;
}
}
if ( commonsbooking_isUserAdmin( $user ) ) {
return 1;
}
if ( commonsbooking_isUserCBManager( $user ) ) {
return $user->ID;
}

return 0;
}

/**
* Returns an associative array of all User Roles as
* roleID => translated role name
* so that it can be used in the CMB2 select field.
* Returns an array of all User Roles as roleID => translated role name
*
* @return array
*/
Expand Down
23 changes: 8 additions & 15 deletions src/Service/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace CommonsBooking\Service;

use CommonsBooking\Map\MapShortcode;
use CommonsBooking\Repository\UserRepository;
use CommonsBooking\View\Calendar;
use CommonsBooking\Settings\Settings;
use Exception;
Expand All @@ -21,20 +20,19 @@ trait Cache {
/**
* Returns cache item based on calling class, function and args.
*
* @param null $custom_id custom id to add to cache id
* @param bool $userRoleSensitive if false, the cache will be shared between all users
* @param null $custom_id
*
* @return mixed
* @throws InvalidArgumentException
*/
public static function getCacheItem( $custom_id = null, bool $userRoleSensitive = true ) {
public static function getCacheItem( $custom_id = null ) {
if ( WP_DEBUG ) {
return false;
}

try {
/** @var CacheItem $cacheItem */
$cacheKey = self::getCacheId( $custom_id, $userRoleSensitive );
$cacheKey = self::getCacheId( $custom_id );
$cacheItem = self::getCache()->getItem( $cacheKey );
if ( $cacheItem->isHit() ) {
return $cacheItem->get();
Expand All @@ -46,25 +44,21 @@ public static function getCacheItem( $custom_id = null, bool $userRoleSensitive

/**
* Returns cache id, based on calling class, function and args.
*
* @since 2.7.2 added Plugin_Dir to Namespace to avoid conflicts on multiple instances on same server
*
* @param null $custom_id
* @param bool $userRoleSensitive if false, the cache will be shared between all users
*
* @return string
* @since 2.7.2 added Plugin_Dir to Namespace to avoid conflicts on multiple instances on same server
*/
public static function getCacheId( $custom_id = null, bool $userRoleSensitive = true ): string {
public static function getCacheId( $custom_id = null ): string {
$backtrace = debug_backtrace()[2];
$backtrace = self::sanitizeArgsArray( $backtrace );
$namespace = COMMONSBOOKING_PLUGIN_DIR;
$namespace .= '_' . str_replace( '\\', '_', strtolower( $backtrace['class'] ) );
$namespace .= '_' . $backtrace['function'];
$backtraceArgs = $backtrace['args'];
$namespace .= '_' . serialize( $backtraceArgs );
if ( $userRoleSensitive ) {
$userGroupID = UserRepository::getRoleTypeID();
$namespace .= '_' . $userGroupID;
}
if ( $custom_id ) {
$namespace .= $custom_id;
}
Expand Down Expand Up @@ -145,14 +139,13 @@ public static function getCache( string $namespace = '', int $defaultLifetime =
* @param $value
* @param array $tags
* @param null $custom_id
* @param bool $userRoleSensitive if false, the cache will be shared between all users
* @param string|null $expirationString set expiration as timestamp or string 'midnight' to set expiration to 00:00 next day
*
* @return bool
* @throws InvalidArgumentException
* @throws \Psr\Cache\CacheException
*/
public static function setCacheItem( $value, array $tags, $custom_id = null, bool $userRoleSensitive = true, ?string $expirationString = null ): bool {
public static function setCacheItem( $value, array $tags, $custom_id = null, ?string $expirationString = null ): bool {
// Set a default expiration to make sure, that we get rid of stale items, if there are some
// too much space
$expiration = 604800;
Expand All @@ -172,7 +165,7 @@ public static function setCacheItem( $value, array $tags, $custom_id = null, boo

$cache = self::getCache( '', intval( $expiration ) );
/** @var CacheItem $cacheItem */
$cacheKey = self::getCacheId( $custom_id, $userRoleSensitive );
$cacheKey = self::getCacheId( $custom_id );
$cacheItem = $cache->getItem( $cacheKey );
$cacheItem->tag($tags);
$cacheItem->set( $value );
Expand Down
4 changes: 2 additions & 2 deletions src/View/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public static function getCalendarDataArray( $item, $location, string $startDate

if ( count( $bookableTimeframes ) ) {
$closestBookableTimeframe = self::getClosestBookableTimeFrameForToday( $bookableTimeframes );
$advanceBookingDays = $closestBookableTimeframe->getAdvanceBookingDays();
$advanceBookingDays = intval( $closestBookableTimeframe->getFieldValue( 'timeframe-advance-booking-days' ) );
$firstBookableDay = $closestBookableTimeframe->getFirstBookableDay();

// Only if passed daterange must not be kept
Expand Down Expand Up @@ -538,7 +538,7 @@ public static function prepareJsonResponse(
}

// set transient expiration time to midnight to force cache refresh by daily basis to allow dynamic advanced booking day feature
Plugin::setCacheItem( $jsonResponse, [ 'misc' ], $customCacheKey, true, 'midnight' );
Plugin::setCacheItem( $jsonResponse, [ 'misc' ], $customCacheKey, 'midnight' );
}

return $jsonResponse;
Expand Down
4 changes: 0 additions & 4 deletions tests/php/Model/TimeframeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,10 +587,6 @@ public function testGetLatestPossibleBookingDateTimestamp() {
//the default advance booking days in our tests are 30
$advanceBookingDays = 30;
ClockMock::freeze(new \DateTime(self::CURRENT_DATE));
//we have to do this as a subscriber, because admins can book anytime
$this->createSubscriber();
wp_set_current_user($this->subscriberId);

//case 1: timeframe is longer than advance booking days
$lateTimeframe = new Timeframe(
$this->createTimeframe(
Expand Down
11 changes: 0 additions & 11 deletions tests/php/Repository/UserRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,6 @@ public function testUserHasRoles() {
$user->remove_role( Plugin::$CB_MANAGER_ID );
}

public function testGetRoleTypeID() {
$subscriber = get_user_by('id', $this->subscriberId);
$manager = get_user_by('id', $this->cbManagerUserID);
$admin = get_user_by('id', $this->adminUserID);
$this->assertEquals(0, UserRepository::getRoleTypeID($subscriber));
wp_logout();
$this->assertEquals(0, UserRepository::getRoleTypeID());
$this->assertEquals(1, UserRepository::getRoleTypeID($admin));
$this->assertEquals($manager->ID, UserRepository::getRoleTypeID($manager));
}

protected function setUp(): void {
parent::setUp();
$this->createCBManager();
Expand Down
10 changes: 0 additions & 10 deletions tests/php/View/CalendarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public function testKeepDateRangeParam() {
}

public function testAdvancedBookingDays() {
//set our user to a normal subscriber so that the booking is not privileged
wp_set_current_user( $this->subscriberId);
$startDate = date( 'Y-m-d', strtotime( 'midnight' ) );
$endDate = date( 'Y-m-d', strtotime( '+60 days midnight' ) );
$jsonresponse = Calendar::getCalendarDataArray(
Expand Down Expand Up @@ -73,8 +71,6 @@ public function testAdvancedBookingDays() {
$maxBookableDays = date_diff( $latestPossibleBookingDate, $timeframeStart )->days;

$this->assertTrue( $maxBookableDays == (self::bookingDaysInAdvance - self::timeframeStart - 1) );

wp_logout();
}

public function testClosestBookableTimeFrameFuntion() {
Expand Down Expand Up @@ -150,9 +146,6 @@ public function testBookingOffset() {
30,
2
);
//set our user to a normal subscriber so that the booking is not privileged
//we need to do this after creating our custom posts, or else the subscriber would be the author
wp_set_current_user( $this->subscriberId);
$jsonresponse = Calendar::getCalendarDataArray(
$otherItemId,
$otherLocationId,
Expand All @@ -165,7 +158,6 @@ public function testBookingOffset() {
//considering the offset, today and tomorrow should be locked
$this->assertTrue($days[$today]['locked']);
$this->assertTrue($days[date('Y-m-d', strtotime('+1 day', strtotime($today)))]['locked']);
wp_logout();
}

protected function setUp() : void {
Expand All @@ -181,8 +173,6 @@ protected function setUp() : void {
// set booking days in advance
update_post_meta( $this->timeframeId, Timeframe::META_TIMEFRAME_ADVANCE_BOOKING_DAYS, self::bookingDaysInAdvance );

$this->createSubscriber();

$this->closestTimeframe = $this->createTimeframe(
$this->locationId,
$this->itemId,
Expand Down
5 changes: 0 additions & 5 deletions tests/php/View/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ protected function setUp() : void {
$now = time();
$this->now = $now;

//set our user to the subscriber so that it is not showing the views for admins ( which don't have to oblige to some booking restrictions )
$this->createSubscriber();
wp_set_current_user( $this->subscriberId );

$timeframeId = $this->createTimeframe(
$this->locationId,
$this->itemId,
Expand Down Expand Up @@ -121,7 +117,6 @@ protected function setUp() : void {
}

protected function tearDown() : void {
wp_logout();
parent::tearDown();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/php/Wordpress/CustomPostTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ protected function setUpBookingCodesTable() {
location bigint(20) unsigned NOT NULL,
item bigint(20) unsigned NOT NULL,
code varchar(100) NOT NULL,
PRIMARY KEY (date, timeframe, location, item, code)
PRIMARY KEY (date, timeframe, location, item, code)
) $charset_collate;";

$wpdb->query( $sql );
Expand Down

0 comments on commit 2ddceff

Please sign in to comment.