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

Lint routes and templates #83

Merged
merged 22 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/coding-standards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
composer-options: "--no-progress --no-ansi --no-interaction"

- name: Run PHPCS
run: vendor/bin/phpcs -q --report=checkstyle | cs2pr --notices-as-warnings
run: vendor/bin/phpcs --standard=phpcs.xml -q --report=checkstyle | cs2pr --notices-as-warnings
60 changes: 48 additions & 12 deletions includes/class-wporg-gp-translation-events-route.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class WPORG_GP_Translation_Events_Route extends GP_Route {
* @since 0.0.1
*/
public function __construct() {
$this->template_path = dirname( __FILE__ ) . '/../templates/';
parent::__construct();
$this->template_path = __DIR__ . '/../templates/';
}

/**
Expand All @@ -23,16 +24,40 @@ public function __construct() {
* @return void
*/
public function events_list() {
$current_datetime_utc = ( new DateTime( 'now', new DateTimeZone( 'UTC' ) ) )->format( 'Y-m-d H:i:s' );
$_current_events_paged = isset( $_GET['current_events_paged'] ) && is_numeric( $_GET['current_events_paged'] ) ? $_GET['current_events_paged'] : 1;
$_upcoming_events_paged = isset( $_GET['upcoming_events_paged'] ) && is_numeric( $_GET['upcoming_events_paged'] ) ? $_GET['upcoming_events_paged'] : 1;
$current_datetime_utc = null;
try {
$current_datetime_utc = ( new DateTime( 'now', new DateTimeZone( 'UTC' ) ) )->format( 'Y-m-d H:i:s' );
} catch ( Exception $e ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( $e );
$this->die_with_error( 'Something is wrong.' );
}

$_current_events_paged = 1;
$_upcoming_events_paged = 1;

// phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['current_events_paged'] ) ) {
$value = sanitize_text_field( wp_unslash( $_GET['current_events_paged'] ) );
if ( is_numeric( $value ) ) {
$_current_events_paged = (int) $value;
}
}
if ( isset( $_GET['upcoming_events_paged'] ) ) {
$value = sanitize_text_field( wp_unslash( $_GET['upcoming_events_paged'] ) );
if ( is_numeric( $value ) ) {
$_upcoming_events_paged = (int) $value;
}
}
// phpcs:enable

$current_events_args = array(
'post_type' => 'event',
'posts_per_page' => 10,
'current_events_paged' => $_current_events_paged,
'paged' => $_current_events_paged,
'post_status' => 'publish',
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
'meta_query' => array(
array(
'key' => '_event_start',
Expand All @@ -58,6 +83,7 @@ public function events_list() {
'upcoming_events_paged' => $_upcoming_events_paged,
'paged' => $_upcoming_events_paged,
'post_status' => 'publish',
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
'meta_query' => array(
array(
'key' => '_event_start',
Expand Down Expand Up @@ -91,6 +117,7 @@ public function events_create() {
$event_timezone = '';
$event_start = '';
$event_end = '';
$event_url = '';

$this->tmpl( 'events-form', get_defined_vars() );
}
Expand All @@ -102,7 +129,7 @@ public function events_create() {
*
* @return void
*/
public function events_edit( $event_id ) {
public function events_edit( int $event_id ) {
if ( ! is_user_logged_in() ) {
$this->die_with_error( 'You must be logged in to edit an event', 403 );
}
Expand All @@ -117,12 +144,21 @@ public function events_edit( $event_id ) {
$css_show_url = '';
$event_title = $event->post_title;
$event_description = $event->post_content;
$event_timezone = get_post_meta( $event_id, '_event_timezone', true ) ?: '';
$event_start = self::convertToTimezone( get_post_meta( $event_id, '_event_start', true ), $event_timezone ) ?? '';
$event_end = self::convertToTimezone( get_post_meta( $event_id, '_event_end', true ), $event_timezone ) ?? '';
$event_status = $event->post_status;
list( $permalink, $post_name ) = get_sample_permalink( $event_id );
$permalink = str_replace( '%pagename%', $post_name, $permalink );
$event_url = get_site_url() . gp_url( wp_make_link_relative( $permalink ) );
$event_timezone = get_post_meta( $event_id, '_event_timezone', true ) ?: '';

try {
$event_start = self::convertToTimezone( get_post_meta( $event_id, '_event_start', true ), $event_timezone );
$event_end = self::convertToTimezone( get_post_meta( $event_id, '_event_end', true ), $event_timezone );
} catch ( Exception $e ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( $e );
$this->die_with_error( 'Something is wrong.' );
}

$this->tmpl( 'events-form', get_defined_vars() );
}

Expand All @@ -133,7 +169,7 @@ public function events_edit( $event_id ) {
*
* @return void
*/
public function events_details( $event_slug ) {
public function events_details( string $event_slug ) {
$user = wp_get_current_user();
$event = get_page_by_path( $event_slug, OBJECT, 'event' );
if ( ! $event ) {
Expand All @@ -152,6 +188,7 @@ public function events_details( $event_slug ) {
try {
$event_stats = $stats_calculator->for_event( $event );
} catch ( Exception $e ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( $e );
$this->die_with_error( 'Failed to calculate event stats' );
}
Expand All @@ -163,8 +200,6 @@ public function events_details( $event_slug ) {
* Toggle whether the current user is attending an event.
* If the user is not currently marked as attending, they will be marked as attending.
* If the user is currently marked as attending, they will be marked as not attending.
*
* @param int $event_id
*/
public function events_attend( int $event_id ) {
$user = wp_get_current_user();
Expand Down Expand Up @@ -230,8 +265,9 @@ public function events_user_created() {
* @param string $time_zone The time zone.
*
* @return string The date time in the time zone.
* @throws Exception When date is invalid.
*/
public static function convertToTimezone( $date_time, $time_zone ) {
public static function convertToTimezone( string $date_time, string $time_zone ): string {
return ( new DateTime( $date_time, new DateTimeZone( 'UTC' ) ) )->setTimezone( new DateTimeZone( $time_zone ) )->format( 'Y-m-d H:i:s' );
}
}
8 changes: 2 additions & 6 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@
<arg name="colors"/>
<arg name="extensions" value="php"/>

<!--
<file>./includes/</file>
<file>./templates/</file>
<file>./tests/</file>
-->
<file>./includes/class-wporg-gp-translation-events-active-events-cache.php</file>
<file>./includes/class-wporg-gp-translation-events-event.php</file>
<file>./includes/class-wporg-gp-translation-events-stats-calculator.php</file>
<file>./includes/class-wporg-gp-translation-events-translation-listener.php</file>
<file>./wporg-gp-translation-events.php</file>

<rule ref="WordPress">
Expand All @@ -25,5 +19,7 @@
<exclude name="Squiz.Commenting.FunctionComment.MissingParamTag"/>
<exclude name="WordPress.Files.FileName.InvalidClassFileName"/>
<exclude name="Generic.Files.OneObjectStructurePerFile.MultipleFound"/>
<exclude name="Universal.Operators.DisallowShortTernary.Found"/>
<exclude name="Generic.Commenting.DocComment.MissingShort"/>
</rule>
</ruleset>
41 changes: 23 additions & 18 deletions templates/event.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
<?php
/**
* Template for event page.
*/

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

/* translators: %s: Event title. */
gp_title( sprintf( __( 'Translation Events - %s' ), esc_html( $event_title ) ) );
gp_tmpl_header();
gp_tmpl_load( 'events-header', get_defined_vars(), dirname( __FILE__ ) );

gp_tmpl_load( 'events-header', get_defined_vars(), __DIR__ );
?>

<div class="event-details-page">
<div class="event-details-head">
<h1>
<?php echo esc_html( $event_title ); ?>
<?php if ( 'draft' === $event->post_status ): ?>
<?php if ( 'draft' === $event->post_status ) : ?>
<span class="event-label-draft"><?php echo esc_html( $event->post_status ); ?></span>
<?php endif; ?>
</h1>
Expand All @@ -32,19 +37,19 @@
<div class="event-details-date">
<p><span class="dashicons dashicons-clock"></span> <time class="event-utc-time" datetime="<?php echo esc_attr( $event_start ); ?>"></time> - <time class="event-utc-time" datetime="<?php echo esc_attr( $event_end ); ?>"></time></p>
</div>
<?php if ( is_user_logged_in() ) : ?>
<?php if ( is_user_logged_in() ) : ?>
<div class="event-details-join">
<form class="event-details-attend" method="post" action="<?php echo esc_url( gp_url( "/events/attend/$event_id" ) )?>">
<?php if ( ! $user_is_attending ): ?>
<form class="event-details-attend" method="post" action="<?php echo esc_url( gp_url( "/events/attend/$event_id" ) ); ?>">
<?php if ( ! $user_is_attending ) : ?>
<input type="submit" class="button is-primary" value="Attend Event"/>
<?php else: ?>
<?php else : ?>
<input type="submit" class="button is-secondary" value="You're attending"/>
<?php endif ?>
</form>
</div>
<?php endif; ?>
</div>
<?php if ( ! empty( $event_stats->rows() ) ):?>
<?php if ( ! empty( $event_stats->rows() ) ) : ?>
<div class="event-details-stats">
<h2>Stats</h2>
<table>
Expand All @@ -57,20 +62,20 @@
</tr>
</thead>
<tbody>
<?php /** @var $row WPORG_GP_Translation_Events_Stats_Row */?>
<?php foreach ( $event_stats->rows() as $locale => $row ): ?>
<?php /** @var $row WPORG_GP_Translation_Events_Stats_Row */ ?>
<?php foreach ( $event_stats->rows() as $locale_ => $row ) : ?>
<tr>
<td><?php echo esc_html( $locale ) ?></td>
<td><?php echo esc_html( $row->created ) ?></td>
<td><?php echo esc_html( $row->reviewed ) ?></td>
<td><?php echo esc_html( $row->users ) ?></td>
<td><?php echo esc_html( $locale_ ); ?></td>
<td><?php echo esc_html( $row->created ); ?></td>
<td><?php echo esc_html( $row->reviewed ); ?></td>
<td><?php echo esc_html( $row->users ); ?></td>
</tr>
<?php endforeach ?>
<tr class="event-details-stats-totals">
<td>Total</td>
<td><?php echo esc_html( $event_stats->totals()->created ) ?></td>
<td><?php echo esc_html( $event_stats->totals()->reviewed ) ?></td>
<td><?php echo esc_html( $event_stats->totals()->users ) ?></td>
<td><?php echo esc_html( $event_stats->totals()->created ); ?></td>
<td><?php echo esc_html( $event_stats->totals()->reviewed ); ?></td>
<td><?php echo esc_html( $event_stats->totals()->users ); ?></td>
</tr>
</tbody>
</table>
Expand Down
20 changes: 17 additions & 3 deletions templates/events-form.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
<?php
/**
* Template for event form.
*/

/** @var string $event_form_title */
/** @var string $event_form_name */
/** @var int $event_id */
/** @var string $event_title */
/** @var string $event_description */
/** @var string $event_start */
/** @var string $event_end */
/** @var string $event_timezone */
/** @var string $event_url */
/** @var string $css_show_url */

gp_title( __( 'Translation Events' ) . ' - ' . esc_html( $event_form_title . ' - ' . $event_title ) );
gp_tmpl_header();
gp_tmpl_load( 'events-header', get_defined_vars(), dirname( __FILE__ ) );

gp_tmpl_load( 'events-header', get_defined_vars(), __DIR__ );
?>

<h2 class="event-page-title"><?php echo esc_html( $event_form_title ); ?></h2>
Expand All @@ -18,7 +32,7 @@
</div>
<div id="event-url" class="<?php echo esc_attr( $css_show_url ); ?>">
<label for="event-permalink">Event URL</label>
<a id="event-permalink" class="event-permalink" href="<?php echo esc_url( gp_url( wp_make_link_relative( $permalink ) ) ) ?>" target="_blank"><?php echo esc_url( get_site_url() . gp_url( wp_make_link_relative( $permalink ) ) ) ?></a>
<a id="event-permalink" class="event-permalink" href="<?php echo esc_url( $event_url ); ?>" target="_blank"><?php echo esc_url( $event_url ); ?></a>
</div>
<div>
<label for="event-description">Event Description</label>
Expand Down
60 changes: 35 additions & 25 deletions templates/events-list.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
<?php
/**
* Events list page.
*/

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

gp_title( __( 'Translation Events' ) );
gp_tmpl_header();
gp_tmpl_load( 'events-header', get_defined_vars(), dirname( __FILE__ ) );
gp_tmpl_load( 'events-header', get_defined_vars(), __DIR__ );
?>



<h2 class="event_page_title">Translation Events</h2>
<div class="event-left-col">
<?php
Expand All @@ -16,27 +21,30 @@
<?php
while ( $current_events_query->have_posts() ) :
$current_events_query->the_post();
$event_start = ( new DateTime( get_post_meta( get_the_ID(), '_event_start', true ) ) )->format('l, F j, Y');
$event_start = ( new DateTime( get_post_meta( get_the_ID(), '_event_start', true ) ) )->format( 'l, F j, Y' );
$event_url = gp_url( wp_make_link_relative( get_the_permalink() ) );
?>
<li class="event-list-item">
<span class="event-list-date"><?php echo esc_html( $event_start ); ?></span>
<a href="<?php echo esc_url( gp_url( wp_make_link_relative( get_the_permalink() ) ) ) ?>"><?php the_title(); ?></a> by <span><?php the_author(); ?></span>
<a href="<?php echo esc_url( $event_url ); ?>"><?php the_title(); ?></a> by <span><?php the_author(); ?></span>
<p><?php the_excerpt(); ?></p>
</li>
<?php
<?php
endwhile;
?>
</ul>

<?php
echo paginate_links(
array(
'total' => $current_events_query->max_num_pages,
'current' => max( 1, $current_events_query->query_vars['current_events_paged'] ),
'format' => '?current_events_paged=%#%',
'prev_text' => '&laquo; Previous',
'next_text' => 'Next &raquo;',
)
echo wp_kses_post(
paginate_links(
array(
'total' => $current_events_query->max_num_pages,
'current' => max( 1, $current_events_query->query_vars['current_events_paged'] ),
'format' => '?current_events_paged=%#%',
'prev_text' => '&laquo; Previous',
'next_text' => 'Next &raquo;',
)
psrpinto marked this conversation as resolved.
Show resolved Hide resolved
) ?? ''
);

wp_reset_postdata();
Expand All @@ -48,27 +56,29 @@
<?php
while ( $upcoming_events_query->have_posts() ) :
$upcoming_events_query->the_post();
$event_start = ( new DateTime( get_post_meta( get_the_ID(), '_event_start', true ) ) )->format('l, F j, Y');
$event_start = ( new DateTime( get_post_meta( get_the_ID(), '_event_start', true ) ) )->format( 'l, F j, Y' );
?>
<li class="event-list-item">
<span class="event-list-date"><?php echo esc_html( $event_start ); ?></span>
<a href="<?php echo esc_url( gp_url( wp_make_link_relative( get_the_permalink() ) ) ) ?>"><?php the_title(); ?></a> by <span><?php the_author(); ?></span>
<a href="<?php echo esc_url( gp_url( wp_make_link_relative( get_the_permalink() ) ) ); ?>"><?php the_title(); ?></a> by <span><?php the_author(); ?></span>
<p><?php the_excerpt(); ?></p>
</li>
<?php
<?php
endwhile;
?>
</ul>

<?php
echo paginate_links(
array(
'total' => $upcoming_events_query->max_num_pages,
'current' => max( 1, $upcoming_events_query->query_vars['upcoming_events_paged'] ),
'format' => '?upcoming_events_paged=%#%',
'prev_text' => '&laquo; Previous',
'next_text' => 'Next &raquo;',
)
echo wp_kses_post(
paginate_links(
array(
'total' => $upcoming_events_query->max_num_pages,
'current' => max( 1, $upcoming_events_query->query_vars['upcoming_events_paged'] ),
'format' => '?upcoming_events_paged=%#%',
'prev_text' => '&laquo; Previous',
'next_text' => 'Next &raquo;',
)
) ?? ''
);

wp_reset_postdata();
Expand Down
Loading
Loading