Skip to content

Commit

Permalink
Add a filter to generate the event slug if it is stored as draft
Browse files Browse the repository at this point in the history
  • Loading branch information
amieiro committed Feb 15, 2024
1 parent 66e1094 commit 3b21431
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 6 additions & 1 deletion templates/event.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@

<div class="event-details-page">
<div class="event-details-head">
<h1><?php echo esc_html( $event_title ); ?></h1>
<h1>
<?php echo esc_html( $event_title ); ?>
<?php if ( 'draft' === $event->post_status ): ?>
<span class="event-label-draft"><?php echo esc_html( $event->post_status ); ?></span>
<?php endif; ?>
</h1>
<p>Host: <a href="<?php echo esc_attr( get_author_posts_url( $event->post_author ) ); ?>"><?php echo esc_html( get_the_author_meta( 'display_name', $event->post_author ) ); ?></a></p>
</div>
<div class="event-details-left">
Expand Down
20 changes: 20 additions & 0 deletions wporg-gp-translation-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,26 @@ function gp_event_nav_menu_items( array $items ): array {
// Add the events link to the GlotPress main menu.
add_filter( 'gp_nav_menu_items', 'gp_event_nav_menu_items' );

/**
* Generate a slug for the event post type when we save a draft event.
*
* @param array $data An array of slashed post data.
* @param array $postarr An array of sanitized, but otherwise unmodified post data.
* @return array The modified post data.
*/
function generate_event_slug( $data, $postarr ) {

Check warning on line 306 in wporg-gp-translation-events.php

View workflow job for this annotation

GitHub Actions / PHP

The method parameter $postarr is never used
if ( $data[ 'post_type' ] === 'event' && $data[ 'post_status' ] === 'draft' ) {

Check failure on line 307 in wporg-gp-translation-events.php

View workflow job for this annotation

GitHub Actions / PHP

Array keys must NOT be surrounded by spaces if they only contain a string or an integer.

Check failure on line 307 in wporg-gp-translation-events.php

View workflow job for this annotation

GitHub Actions / PHP

Use Yoda Condition checks, you must.

Check failure on line 307 in wporg-gp-translation-events.php

View workflow job for this annotation

GitHub Actions / PHP

Array keys must NOT be surrounded by spaces if they only contain a string or an integer.

Check failure on line 307 in wporg-gp-translation-events.php

View workflow job for this annotation

GitHub Actions / PHP

Use Yoda Condition checks, you must.
// Generate a slug based on the event title if it's not provided

Check failure on line 308 in wporg-gp-translation-events.php

View workflow job for this annotation

GitHub Actions / PHP

Inline comments must end in full-stops, exclamation marks, or question marks
if ( empty( $data[ 'post_name' ] ) ) {

Check failure on line 309 in wporg-gp-translation-events.php

View workflow job for this annotation

GitHub Actions / PHP

Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
$data[ 'post_name' ] = sanitize_title( $data[ 'post_title' ] );

Check failure on line 310 in wporg-gp-translation-events.php

View workflow job for this annotation

GitHub Actions / PHP

Array keys must NOT be surrounded by spaces if they only contain a string or an integer.

Check failure on line 310 in wporg-gp-translation-events.php

View workflow job for this annotation

GitHub Actions / PHP

Array keys must NOT be surrounded by spaces if they only contain a string or an integer.
}
}

return $data;
}

add_filter('wp_insert_post_data', 'generate_event_slug', 10, 2);

Check failure on line 317 in wporg-gp-translation-events.php

View workflow job for this annotation

GitHub Actions / PHP

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 317 in wporg-gp-translation-events.php

View workflow job for this annotation

GitHub Actions / PHP

Expected 1 spaces before closing parenthesis; 0 found

add_action(
'gp_init',
function () {
Expand Down

0 comments on commit 3b21431

Please sign in to comment.