Skip to content

Commit

Permalink
Merge branch 'datengraben/fix-translations-plugin-init' into datengra…
Browse files Browse the repository at this point in the history
…ben/documentation-filter-hooks
  • Loading branch information
datengraben committed Feb 10, 2025
2 parents fff9610 + b46cc7d commit cb6cef1
Show file tree
Hide file tree
Showing 72 changed files with 3,977 additions and 4,411 deletions.
904 changes: 7 additions & 897 deletions .editorconfig

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# .git-blame-ignore-revs
# Apply phpcbf to entire codebase
59340f68fb9c6d6e680ac3ad40a3d095e98837a6

# Apply phpcbf to php tests code
9f0bd0de52d5dca442317c14867be7702e1a7c73
62 changes: 60 additions & 2 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,66 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https:
<exclude name="Squiz.Commenting.InlineComment.InvalidEndChar" />
<exclude name="Squiz.Commenting.FileComment" />
<exclude name="Squiz.Commenting.FunctionComment.ParamCommentFullStop" />

<exclude-pattern>tests/</exclude-pattern>
</rule>


<!--
#############################################################################
Global exclusions for tests.
Copied from https://github.com/polylang/polylang/blob/master/phpcs.xml.dist
#############################################################################
-->

<rule ref="Generic.Commenting.Fixme.CommentFound">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>

<rule ref="Generic.Files.OneObjectStructurePerFile.MultipleFound">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>

<rule ref="Generic.PHP.NoSilencedErrors.Forbidden">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>

<rule ref="Squiz.Commenting.ClassComment.Missing">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>

<rule ref="Squiz.Commenting.FileComment">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>

<rule ref="Squiz.Commenting.FunctionComment.Missing">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>

<rule ref="Squiz.Commenting.FunctionComment.MissingParamComment">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>

<rule ref="Squiz.Commenting.VariableComment.Missing">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>

<rule ref="Squiz.PHP.CommentedOutCode.Found">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>

<rule ref="WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid">
<exclude-pattern>*/tests/phpunit/includes/testcase-trait.php</exclude-pattern>
</rule>

<rule ref="WordPress.NamingConventions.ValidHookName.UseUnderscores">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>

<rule ref="WordPress.PHP.NoSilencedErrors.Discouraged">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>

<rule ref="WordPress.Security">
<exclude-pattern>*/tests/*</exclude-pattern>
</rule>

<!-- Adds commonsbooking specific wrappers around wp_kses ... -->
Expand Down
2 changes: 1 addition & 1 deletion includes/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ function ( $posts, $query ) {

// Add filter to change post counts in admin lists for custom post types.
foreach ( Plugin::getCustomPostTypes() as $custom_post_type ) {
add_filter( 'views_edit-' . $custom_post_type::getPostType(), 'commonsbooking_custom_view_count', 10, 1 );
add_filter( 'views_edit-' . $custom_post_type, 'commonsbooking_custom_view_count', 10, 1 );
}

// Filter function for fix of counts in admin lists for custom post types.
Expand Down
82 changes: 60 additions & 22 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ public static function addCPTRoleCaps() {
$CBManagerAllowedCPT = self::getCBManagerCustomPostTypes();

Check failure on line 91 in src/Plugin.php

View workflow job for this annotation

GitHub Actions / WP latest on PHP 8.3

Call to an undefined static method CommonsBooking\Plugin::getCBManagerCustomPostTypes().
// Add capabilities for user roles
foreach ( $adminAllowedCPT as $customPostType ) {
self::addRoleCaps( $customPostType::$postType, 'administrator' );
self::addRoleCaps( $customPostType, 'administrator' );
// assign all capabilities of admin to CB-Manager (see comment above)
// We deliberately don't use the getManagerRoles from the UserRepository here, because the custom roles should be able to define their own permissions
self::addRoleCaps( $customPostType::$postType, self::$CB_MANAGER_ID );
self::addRoleCaps( $customPostType, self::$CB_MANAGER_ID );
}
/*
foreach ( $CBManagerAllowedCPT as $customPostType ) {
self::addRoleCaps( $customPostType::$postType, self::$CB_MANAGER_ID );
self::addRoleCaps( $customPostType, self::$CB_MANAGER_ID );
}
*/
}
Expand Down Expand Up @@ -157,19 +157,57 @@ public static function addCustomUserRoles() {
* When defining a CustomPostType, you must also define a model for it, which extends the CustomPost class.
* The existence of a model is checked in the @see PluginTest::testGetCustomPostTypes() test.
*
* @return CustomPostType[]
* @see Plugin::getCustomPostTypeClasses()
*
* @since 2.10.3 only returns custom post type enum string
*
* @return string[] enum array
*/
public static function getCustomPostTypes(): array {

return array_map(
function ( $type ) {
return ( $type )::getPostType();
},
self::getCustomPostTypeClasses()
);
}

/**
* @return class-string<CustomPostType>[] class array
*/
private static function getCustomPostTypeClasses() {
return [
new Item(),
new Location(),
new Timeframe(),
new Map(),
new \CommonsBooking\Wordpress\CustomPostType\Booking(),
new Restriction(),
\CommonsBooking\Wordpress\CustomPostType\Item::class,
\CommonsBooking\Wordpress\CustomPostType\Location::class,
\CommonsBooking\Wordpress\CustomPostType\Timeframe::class,
\CommonsBooking\Wordpress\CustomPostType\Map::class,
\CommonsBooking\Wordpress\CustomPostType\Booking::class,
\CommonsBooking\Wordpress\CustomPostType\Restriction::class,
];
}

/**
* @return CustomPostType[] instantiated objects
*/
public static function getCustomPostTypeObjects() {
return array_map(
function ( $type ) {
return new $type();
},
self::getCustomPostTypeClasses()
);
}

private static function getCBManagerCustomPostTypesObjects() {
return array_map(
function ( $type ) {
return new $type();
},
self::getCBManagerCustomPostTypeClasses()
);
}

/**
* Tests if a given post belongs to our CPTs
*
Expand All @@ -192,16 +230,16 @@ public static function isPostCustomPostType( $post ): bool {

/**
* Returns only custom post types, which are allowed for cb manager
*
* @return array
*
* @return class-string<CustomPostType>[] array of classes
*/
public static function getCBManagerCustomPostTypes(): array {
public static function getCBManagerCustomPostTypeClasses(): array {
return [
new Item(),
new Location(),
new Timeframe(),
new \CommonsBooking\Wordpress\CustomPostType\Booking(),
new Restriction(),
\CommonsBooking\Wordpress\CustomPostType\Item::class,
\CommonsBooking\Wordpress\CustomPostType\Location::class,
\CommonsBooking\Wordpress\CustomPostType\Timeframe::class,
\CommonsBooking\Wordpress\CustomPostType\Booking::class,
\CommonsBooking\Wordpress\CustomPostType\Restriction::class,
];
}

Expand Down Expand Up @@ -289,7 +327,7 @@ public static function addMenuPages() {
);

// Custom post types
$customPostTypes = commonsbooking_isCurrentUserAdmin() ? self::getCustomPostTypes() : self::getCBManagerCustomPostTypes();
$customPostTypes = commonsbooking_isCurrentUserAdmin() ? self::getCustomPostTypeObjects() : self::getCBManagerCustomPostTypesObjects();
foreach ( $customPostTypes as $cbCustomPostType ) {
$params = $cbCustomPostType->getMenuParams();
add_submenu_page(
Expand Down Expand Up @@ -398,7 +436,7 @@ public static function filterAdminBodyClass( $classes ) {
* Registers custom post types.
*/
public static function registerCustomPostTypes() {
foreach ( self::getCustomPostTypes() as $customPostType ) {
foreach ( self::getCustomPostTypeObjects() as $customPostType ) {
$cptArgs = $customPostType->getArgs();
// make export possible when using WP_DEBUG, this allows us to use the export feature for creating new E2E tests
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
Expand Down Expand Up @@ -948,7 +986,7 @@ public function setParentFile( $parent_file ): string {
// Set 'cb-dashboard' as parent for cb post types
if ( in_array( $current_screen->base, array( 'post', 'edit' ) ) ) {
foreach ( self::getCustomPostTypes() as $customPostType ) {
if ( $customPostType::getPostType() === $current_screen->post_type ) {
if ( $customPostType === $current_screen->post_type ) {
return 'cb-dashboard';
}
}
Expand Down Expand Up @@ -983,7 +1021,7 @@ public function getTheContent( $content ): string {
// Check if we're inside the main loop in a single post page.
if ( is_single() && in_the_loop() && is_main_query() ) {
global $post;
foreach ( self::getCustomPostTypes() as $customPostType ) {
foreach ( self::getCustomPostTypeClasses() as $customPostType ) {
if ( $customPostType::getPostType() === $post->post_type ) {
return $content . $customPostType::getView()::content( $post );
}
Expand Down
1 change: 0 additions & 1 deletion tests/php/API/AvailabilityRouteEmptyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ public function testsEmptyAvailabilitySuccess() {

$this->assertSame( 200, $response->get_status() );
$this->assertSame( 0, count( $response->get_data()->availability ) );

}
}
68 changes: 37 additions & 31 deletions tests/php/API/AvailabilityRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

class AvailabilityRouteTest extends CB_REST_Route_UnitTestCase {

const USER_ID = 1;
const CURRENT_DATE = '2021-05-21';
const USER_ID = 1;
const CURRENT_DATE = '2021-05-21';
protected $ENDPOINT = '/commonsbooking/v1/availability';
private array $locationIds;
private array $itemIds;
Expand All @@ -19,22 +19,22 @@ class AvailabilityRouteTest extends CB_REST_Route_UnitTestCase {
private $locationId;
private $itemId;

public function setUp() : void {
public function setUp(): void {
parent::setUp();

// TODO creates initial data (should be mocked in the future)
ClockMock::freeze( new \DateTime( self::CURRENT_DATE ) );

// Create location
$this->locationId = self::createLocation('Testlocation', 'publish');
$this->locationId = self::createLocation( 'Testlocation', 'publish' );

// Create Item
$this->itemId = self::createItem('TestItem', 'publish');
$this->itemId = self::createItem( 'TestItem', 'publish' );

$mocked = new \DateTimeImmutable( self::CURRENT_DATE );

$start = $mocked->modify( '-1 days');
$end = $mocked->modify( '+1 days');
$start = $mocked->modify( '-1 days' );
$end = $mocked->modify( '+1 days' );

$this->createTimeframe(
$this->locationId,
Expand Down Expand Up @@ -85,27 +85,29 @@ protected function createTimeframe(
$repetitionStart,
$repetitionEnd,
$type = Timeframe::BOOKABLE_ID,
$fullday = "on",
$fullday = 'on',
$repetition = 'w',
$grid = 0,
$startTime = '8:00 AM',
$endTime = '12:00 PM',
$postStatus = 'publish',
$weekdays = [ "1", "2", "3", "4", "5", "6", "7" ],
$weekdays = [ '1', '2', '3', '4', '5', '6', '7' ],
$postAuthor = self::USER_ID,
$maxDays = 3,
$advanceBookingDays = 30,
$showBookingCodes = "on",
$createBookingCodes = "on",
$showBookingCodes = 'on',
$createBookingCodes = 'on',
$postTitle = 'TestTimeframe'
) {
// Create Timeframe
$timeframeId = wp_insert_post( [
'post_title' => $postTitle,
'post_type' => Timeframe::$postType,
'post_status' => $postStatus,
'post_author' => $postAuthor
] );
$timeframeId = wp_insert_post(
[
'post_title' => $postTitle,
'post_type' => Timeframe::$postType,
'post_status' => $postStatus,
'post_author' => $postAuthor,
]
);

update_post_meta( $timeframeId, 'type', $type );
update_post_meta( $timeframeId, 'location-id', $locationId );
Expand Down Expand Up @@ -133,32 +135,36 @@ protected function createTimeframe(
return $timeframeId;
}

public function createLocation($title, $postStatus, $admins = []) {
$locationId = wp_insert_post( [
'post_title' => $title,
'post_type' => Location::$postType,
'post_status' => $postStatus
] );
public function createLocation( $title, $postStatus, $admins = [] ) {
$locationId = wp_insert_post(
[
'post_title' => $title,
'post_type' => Location::$postType,
'post_status' => $postStatus,
]
);

$this->locationIds[] = $locationId;

if (! empty($admins)) {
if ( ! empty( $admins ) ) {
update_post_meta( $locationId, COMMONSBOOKING_METABOX_PREFIX . 'location_admins', $admins );
}

return $locationId;
}

public function createItem($title, $postStatus, $admins = []) {
$itemId = wp_insert_post( [
'post_title' => $title,
'post_type' => Item::$postType,
'post_status' => $postStatus
] );
public function createItem( $title, $postStatus, $admins = [] ) {
$itemId = wp_insert_post(
[
'post_title' => $title,
'post_type' => Item::$postType,
'post_status' => $postStatus,
]
);

$this->itemIds[] = $itemId;

if (! empty($admins)) {
if ( ! empty( $admins ) ) {
update_post_meta( $itemId, COMMONSBOOKING_METABOX_PREFIX . 'item_admins', $admins );
}

Expand Down
2 changes: 1 addition & 1 deletion tests/php/API/CB_REST_Route_UnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ public function testRoute() {
$routes = $this->server->get_routes();
$this->assertArrayHasKey( $this->ENDPOINT, $routes );
}
}
}
6 changes: 3 additions & 3 deletions tests/php/API/CB_REST_UnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class CB_REST_UnitTestCase extends \WP_UnitTestCase {

protected $ENDPOINT;

public function setUp() : void {
public function setUp(): void {
parent::setUp();
GeoHelperTest::setUpGeoHelperMock( $this );

/** @var WP_REST_Server $wp_rest_server */
global $wp_rest_server;
$this->server = $wp_rest_server = new \WP_REST_Server;
$this->server = $wp_rest_server = new \WP_REST_Server();

// Enables api
Settings::updateOption( 'commonsbooking_options_api', 'api-activated', 'on' );
Expand All @@ -34,4 +34,4 @@ public function setUp() : void {
// Applies hook
do_action( 'rest_api_init' );
}
}
}
Loading

0 comments on commit cb6cef1

Please sign in to comment.