Skip to content

Commit

Permalink
Add Event_Capabilities class
Browse files Browse the repository at this point in the history
  • Loading branch information
psrpinto committed Apr 17, 2024
1 parent 61f4cda commit 034b285
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
require_once __DIR__ . '/includes/event/event-repository.php';
require_once __DIR__ . '/includes/event/event-repository-cached.php';
require_once __DIR__ . '/includes/event/event-form-handler.php';
require_once __DIR__ . '/includes/event/event-capabilities.php';
require_once __DIR__ . '/includes/stats/stats-calculator.php';
require_once __DIR__ . '/includes/stats/stats-importer.php';
require_once __DIR__ . '/includes/stats/stats-listener.php';
Expand Down
46 changes: 46 additions & 0 deletions includes/event/event-capabilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Wporg\TranslationEvents\Event;

use WP_User;

class Event_Capabilities {
private const CREATE = 'create_translation_event';

private const CAPS = array(
self::CREATE,
);

private function has_cap( string $cap, array $args, WP_User $user ): bool {
switch ( $cap ) {
case self::CREATE:
return $this->has_create( $user );
}

return false;
}

private function has_create( WP_User $user ): bool {

Check warning on line 23 in includes/event/event-capabilities.php

View workflow job for this annotation

GitHub Actions / phpcs

The method parameter $user is never used
// TODO.
return true;
}

public function register_hooks(): void {
add_action(
'user_has_cap',
function ( $allcaps, $caps, $args, $user ) {
foreach ( $caps as $cap ) {
if ( ! in_array( $cap, self::CAPS, true ) ) {
continue;
}
if ( $this->has_cap( $cap, $args, $user ) ) {
$allcaps[ $cap ] = true;
}
}
return $allcaps;
},
10,
4,
);
}
}
21 changes: 21 additions & 0 deletions tests/event/event-capabilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Wporg\Tests\Event;

use GP_UnitTestCase;
use Wporg\TranslationEvents\Attendee\Attendee_Repository;
use Wporg\TranslationEvents\Event\Event_Capabilities;
use Wporg\TranslationEvents\Event\Event_Repository;
use Wporg\TranslationEvents\Tests\Event_Factory;
use Wporg\TranslationEvents\Tests\Stats_Factory;

class Event_Capabilities_Test extends GP_UnitTestCase {
public function setUp(): void {
parent::setUp();
$this->event_factory = new Event_Factory();
$this->stats_factory = new Stats_Factory();
$this->attendee_repository = new Attendee_Repository();
$this->event_repository = new Event_Repository( $this->attendee_repository );
$this->capilities = new Event_Capabilities();
}
}
6 changes: 6 additions & 0 deletions wporg-gp-translation-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use WP_Query;
use Wporg\TranslationEvents\Attendee\Attendee;
use Wporg\TranslationEvents\Attendee\Attendee_Repository;
use Wporg\TranslationEvents\Event\Event_Capabilities;
use Wporg\TranslationEvents\Event\Event_Form_Handler;
use Wporg\TranslationEvents\Event\Event_Repository_Cached;
use Wporg\TranslationEvents\Event\Event_Repository_Interface;
Expand All @@ -34,6 +35,8 @@
class Translation_Events {
public const CPT = 'translation_event';

private Event_Capabilities $event_capabilities;

public static function get_instance(): Translation_Events {
static $instance = null;
if ( null === $instance ) {
Expand Down Expand Up @@ -75,6 +78,9 @@ public function __construct() {
if ( is_admin() ) {
Upgrade::upgrade_if_needed();
}

$this->event_capabilities = new Event_Capabilities();
$this->event_capabilities->register_hooks();
}

public function gp_init() {
Expand Down

0 comments on commit 034b285

Please sign in to comment.