Skip to content

Commit

Permalink
Merge pull request #90 from WordPress/namespaces
Browse files Browse the repository at this point in the history
Introduce PHP namespace
  • Loading branch information
psrpinto authored Feb 19, 2024
2 parents f5e547f + 72b60b6 commit 7c90a06
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<?php

namespace Wporg\TranslationEvents;

class WPORG_GP_Translation_Events_Active_Events_Cache {
use Exception;

class Active_Events_Cache {
public const CACHE_DURATION = 60 * 60 * 24; // 24 hours.
private const KEY = 'translation-events-active-events';

/**
* Cache active events.
*
* @param WPORG_GP_Translation_Events_Event[] $events Events to cache.
* @param Event[] $events Events to cache.
*
* @throws Exception When it fails to cache events.
*/
public function cache( array $events ): void {
Expand All @@ -20,7 +24,7 @@ public function cache( array $events ): void {
/**
* Returns the cached events, or null if nothing is cached.
*
* @return WPORG_GP_Translation_Events_Event[]|null
* @return Event[]|null
* @throws Exception When it fails to retrieve cached events.
*/
public function get(): ?array {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
<?php

class WPORG_GP_Translation_Events_Event {
namespace Wporg\TranslationEvents;

use DateTimeImmutable;
use DateTimeZone;
use Exception;

class Event {
private int $id;
private DateTimeImmutable $start;
private DateTimeImmutable $end;
Expand All @@ -11,12 +17,12 @@ class WPORG_GP_Translation_Events_Event {
*
* @throws Exception When dates are invalid.
*/
public static function from_post_meta( int $id, array $meta ): WPORG_GP_Translation_Events_Event {
public static function from_post_meta( int $id, array $meta ): Event {
if ( ! isset( $meta['_event_start'][0] ) || ! isset( $meta['_event_end'][0] ) || ! isset( $meta['_event_timezone'][0] ) ) {
throw new Exception( 'Invalid event meta' );
}

return new WPORG_GP_Translation_Events_Event(
return new Event(
$id,
DateTimeImmutable::createFromFormat( 'Y-m-d H:i:s', $meta['_event_start'][0], new DateTimeZone( 'UTC' ) ),
DateTimeImmutable::createFromFormat( 'Y-m-d H:i:s', $meta['_event_end'][0], new DateTimeZone( 'UTC' ) ),
Expand Down
24 changes: 11 additions & 13 deletions ...ass-wporg-gp-translation-events-route.php → includes/route.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
<?php

/**
* Routes: WPORG_GP_Translation_Events_Route class
*
* @package wporg-gp-translation-events
*/
class WPORG_GP_Translation_Events_Route extends GP_Route {
namespace Wporg\TranslationEvents;

use DateTime;
use DateTimeZone;
use Exception;
use GP_Route;
use WP_Query;

class Route extends GP_Route {
public const USER_META_KEY_ATTENDING = 'translation-events-attending';

/**
* WPORG_GP_Translation_Events_Route constructor.
*
* @since 0.0.1
*/
public function __construct() {
parent::__construct();
$this->template_path = __DIR__ . '/../templates/';
Expand Down Expand Up @@ -188,7 +186,7 @@ public function events_edit( int $event_id ) {
$create_delete_button = false;
$visibility_delete_button = 'inline-flex';

$stats_calculator = new WPORG_GP_Translation_Events_Stats_Calculator();
$stats_calculator = new Stats_Calculator();
if ( ! $stats_calculator->event_has_stats( $event ) ) {
$current_user = wp_get_current_user();
if ( $current_user->ID === $event->post_author || current_user_can( 'manage_options' ) ) {
Expand Down Expand Up @@ -230,7 +228,7 @@ public function events_details( string $event_slug ) {
$attending_event_ids = get_user_meta( $user->ID, self::USER_META_KEY_ATTENDING, true ) ?: array();
$user_is_attending = isset( $attending_event_ids[ $event_id ] );

$stats_calculator = new WPORG_GP_Translation_Events_Stats_Calculator();
$stats_calculator = new Stats_Calculator();
try {
$event_stats = $stats_calculator->for_event( $event );
} catch ( Exception $e ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?php

class WPORG_GP_Translation_Events_Stats_Row {
namespace Wporg\TranslationEvents;

use Exception;
use WP_Post;

class Stats_Row {
public int $created;
public int $reviewed;
public int $users;
Expand All @@ -12,54 +17,54 @@ public function __construct( $created, $reviewed, $users ) {
}
}

class WPORG_GP_Translation_Events_Event_Stats {
class Event_Stats {
/**
* Associative array of rows, with the locale as key.
*
* @var WPORG_GP_Translation_Events_Stats_Row[]
* @var Stats_Row[]
*/
private array $rows = array();

private WPORG_GP_Translation_Events_Stats_Row $totals;
private Stats_Row $totals;

/**
* Add a stats row.
*
* @throws Exception When incorrect locale is passed.
*/
public function add_row( string $locale, WPORG_GP_Translation_Events_Stats_Row $row ) {
public function add_row( string $locale, Stats_Row $row ) {
if ( ! $locale ) {
throw new Exception( 'locale must not be empty' );
}
$this->rows[ $locale ] = $row;
}

public function set_totals( WPORG_GP_Translation_Events_Stats_Row $totals ) {
public function set_totals( Stats_Row $totals ) {
$this->totals = $totals;
}

/**
* Get an associative array of rows, with the locale as key.
*
* @return WPORG_GP_Translation_Events_Stats_Row[]
* @return Stats_Row[]
*/
public function rows(): array {
return $this->rows;
}

public function totals(): WPORG_GP_Translation_Events_Stats_Row {
public function totals(): Stats_Row {
return $this->totals;
}
}

class WPORG_GP_Translation_Events_Stats_Calculator {
class Stats_Calculator {
/**
* Get stats for an event.
*
* @throws Exception When stats calculation failed.
*/
public function for_event( WP_Post $event ): WPORG_GP_Translation_Events_Event_Stats {
$stats = new WPORG_GP_Translation_Events_Event_Stats();
public function for_event( WP_Post $event ): Event_Stats {
$stats = new Event_Stats();
global $wpdb;

// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
Expand Down Expand Up @@ -94,7 +99,7 @@ public function for_event( WP_Post $event ): WPORG_GP_Translation_Events_Event_S
);
}

$stats_row = new WPORG_GP_Translation_Events_Stats_Row(
$stats_row = new Stats_Row(
$row->created,
$row->total - $row->created,
$row->users,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
<?php

class WPORG_GP_Translation_Events_Translation_Listener {
namespace Wporg\TranslationEvents;

use DateTimeImmutable;
use DateTimeZone;
use Exception;
use GP_Translation;
use GP_Translation_Set;

class Translation_Listener {
const ACTION_CREATE = 'create';
const ACTION_APPROVE = 'approve';
const ACTION_REJECT = 'reject';
const ACTION_REQUEST_CHANGES = 'request_changes';

private WPORG_GP_Translation_Events_Active_Events_Cache $active_events_cache;
private Active_Events_Cache $active_events_cache;

public function __construct( WPORG_GP_Translation_Events_Active_Events_Cache $active_events_cache ) {
public function __construct( Active_Events_Cache $active_events_cache ) {
$this->active_events_cache = $active_events_cache;
}

Expand Down Expand Up @@ -96,13 +104,13 @@ private function handle_action( GP_Translation $translation, int $user_id, strin
/**
* Get active events at a given time.
*
* @return WPORG_GP_Translation_Events_Event[]
* @return Event[]
* @throws Exception When it fails to get active events.
*/
private function get_active_events( DateTimeImmutable $at ): array {
$events = $this->active_events_cache->get();
if ( null === $events ) {
$cache_duration = WPORG_GP_Translation_Events_Active_Events_Cache::CACHE_DURATION;
$cache_duration = Active_Events_Cache::CACHE_DURATION;
$boundary_start = $at;
$boundary_end = $at->modify( "+$cache_duration seconds" );

Expand Down Expand Up @@ -133,7 +141,7 @@ private function get_active_events( DateTimeImmutable $at ): array {
$events = array();
foreach ( $event_ids as $event_id ) {
$meta = get_post_meta( $event_id );
$events[] = WPORG_GP_Translation_Events_Event::from_post_meta( $event_id, $meta );
$events[] = Event::from_post_meta( $event_id, $meta );
}

$this->active_events_cache->cache( $events );
Expand All @@ -151,16 +159,17 @@ function ( $event ) use ( $at ) {
/**
* Filter an array of events so that it only includes events the given user is attending.
*
* @param WPORG_GP_Translation_Events_Event[] $events Events.
* @return WPORG_GP_Translation_Events_Event[]
* @param Event[] $events Events.
*
* @return Event[]
*/
// phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
// phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.Found
private function select_events_user_is_registered_for( array $events, int $user_id ): array {
$attending_event_ids = get_user_meta( $user_id, WPORG_GP_Translation_Events_Route::USER_META_KEY_ATTENDING, true );
$attending_event_ids = get_user_meta( $user_id, Route::USER_META_KEY_ATTENDING, true );
return array_filter(
$events,
function ( WPORG_GP_Translation_Events_Event $event ) use ( $attending_event_ids ) {
function ( Event $event ) use ( $attending_event_ids ) {
return isset( $attending_event_ids[ $event->id() ] );
}
);
Expand Down
8 changes: 6 additions & 2 deletions templates/event.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
* Template for event page.
*/

namespace Wporg\TranslationEvents;

use WP_Post;

/** @var WP_Post $event */
/** @var int $event_id */
/** @var string $event_title */
/** @var string $event_description */
/** @var string $event_start */
/** @var string $event_end */
/** @var bool $user_is_attending */
/** @var WPORG_GP_Translation_Events_Event_Stats $event_stats */
/** @var Event_Stats $event_stats */

/* translators: %s: Event title. */
gp_title( sprintf( __( 'Translation Events - %s' ), esc_html( $event_title ) ) );
Expand Down Expand Up @@ -62,7 +66,7 @@
</tr>
</thead>
<tbody>
<?php /** @var $row WPORG_GP_Translation_Events_Stats_Row */ ?>
<?php /** @var $row Stats_Row */ ?>
<?php foreach ( $event_stats->rows() as $locale_ => $row ) : ?>
<tr>
<td><?php echo esc_html( $locale_ ); ?></td>
Expand Down
2 changes: 2 additions & 0 deletions templates/events-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Template for event form.
*/

namespace Wporg\TranslationEvents;

/** @var string $event_form_title */
/** @var string $event_form_name */
/** @var int $event_id */
Expand Down
5 changes: 5 additions & 0 deletions templates/events-header.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<?php
namespace Wporg\TranslationEvents;

?>

<div class="event-list-top-bar">
<ul class="event-list-nav">
<?php if ( is_user_logged_in() ) : ?>
Expand Down
5 changes: 5 additions & 0 deletions templates/events-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
* Events list page.
*/

namespace Wporg\TranslationEvents;

use DateTime;
use WP_Query;

/** @var WP_Query $current_events_query */
/** @var WP_Query $upcoming_events_query */

Expand Down
5 changes: 5 additions & 0 deletions templates/events-user-created.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
* Template for My Events.
*/

namespace Wporg\TranslationEvents;

use DateTime;
use WP_Query;

/** @var WP_Query $query */

gp_title( __( 'Translation Events' ) . ' - ' . esc_html__( 'My Events' ) );
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/dummy.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<?php

namespace Wporg\TranslationEvents\Tests;

use WP_UnitTestCase;

/**
* TODO: Remove this file once there are actual tests.
*/
Expand Down
Loading

0 comments on commit 7c90a06

Please sign in to comment.