Skip to content

Commit

Permalink
Added format setting and preview. Small cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauteri committed Dec 12, 2023
1 parent ec6f4f0 commit 642c545
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 18 deletions.
13 changes: 9 additions & 4 deletions includes/core/classes/class-event.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,17 @@ public static function get_taxonomy_registration_args(): array {
* @return string The formatted display date and time or an em dash if not available.
*/
public function get_display_datetime(): string {
$settings = Settings::get_instance();
$date_format = $settings->get_value( 'general', 'formatting', 'date_format' );
$time_format = $settings->get_value( 'general', 'formatting', 'time_format' );
$timezone = $settings->get_value( 'general', 'formatting', 'show_timezone' ) ? ' T' : '';

if ( $this->is_same_date() ) {
$start = $this->get_datetime_start( 'l, F j, Y g:i A' );
$end = $this->get_datetime_end( 'g:i A T' );
$start = $this->get_datetime_start( $date_format . ' ' . $time_format );
$end = $this->get_datetime_end( $time_format . $timezone );
} else {
$start = $this->get_datetime_start( 'l, F j, Y, g:i A' );
$end = $this->get_datetime_end( 'l, F j, Y, g:i A T' );
$start = $this->get_datetime_start( $date_format . ', ' . $time_format );
$end = $this->get_datetime_end( $date_format . ', ' . $time_format . $timezone );
}

if ( ! empty( $start ) && ! empty( $end ) ) {
Expand Down
2 changes: 1 addition & 1 deletion includes/core/classes/class-rsvp.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Rsvp {
*/
public function __construct( int $post_id ) {
$this->event = get_post( $post_id );
$this->max_attending_limit = Settings::get_instance()->get_value( 'gp_general', 'general', 'max_attending_limit' );
$this->max_attending_limit = Settings::get_instance()->get_value( 'general', 'general', 'max_attending_limit' );
}

/**
Expand Down
31 changes: 24 additions & 7 deletions includes/core/classes/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected function setup_hooks(): void {
add_action( 'admin_head', array( $this, 'remove_sub_options' ) );
add_action( 'admin_init', array( $this, 'register_settings' ) );
add_action( 'gatherpress_settings_section', array( $this, 'render_settings_form' ) );

add_action( 'gatherpress_text_after', array ( $this, 'datetime_preview' ), 10, 2 );
add_filter( 'submenu_file', array( $this, 'select_menu' ) );
}

Expand Down Expand Up @@ -296,7 +296,7 @@ public function text( string $sub_page, string $section, string $option, array $
'value' => $value,
'label' => $option_settings['field']['label'] ?? '',
'size' => $option_settings['field']['size'] ?? 'regular',
'description' => $option_settings['description'] ?? '',
'description' => $option_settings['field']['description'] ?? '',
),
true
);
Expand Down Expand Up @@ -328,7 +328,7 @@ public function number( string $sub_page, string $section, string $option, array
'value' => $value,
'label' => $option_settings['field']['label'] ?? '',
'size' => $option_settings['field']['size'] ?? 'regular',
'description' => $option_settings['description'] ?? '',
'description' => $option_settings['field']['description'] ?? '',
),
true
);
Expand Down Expand Up @@ -360,7 +360,7 @@ public function checkbox( string $sub_page, string $section, string $option, arr
'option' => Utility::prefix_key( $option ),
'value' => $value,
'label' => $option_settings['field']['label'] ?? '',
'description' => $option_settings['description'] ?? '',
'description' => $option_settings['field']['description'] ?? '',
),
true
);
Expand Down Expand Up @@ -391,7 +391,7 @@ public function autocomplete( string $sub_page, string $section, string $option,
'name' => $name,
'option' => Utility::prefix_key( $option ),
'value' => $value,
'description' => $option_settings['description'] ?? '',
'description' => $option_settings['field']['description'] ?? '',
'field_options' => $option_settings['field']['options'] ?? array(),
),
true
Expand All @@ -413,8 +413,9 @@ public function autocomplete( string $sub_page, string $section, string $option,
* @return mixed The value of the option or its default value.
*/
public function get_value( string $sub_page, string $section = '', string $option = '' ) {
$options = $this->get_options( $sub_page );
$default = $this->get_default_value( $sub_page, $section, $option );
$sub_page = Utility::prefix_key( $sub_page );
$options = $this->get_options( $sub_page );
$default = $this->get_default_value( $sub_page, $section, $option );

return (
isset( $options[ $section ][ $option ] )
Expand Down Expand Up @@ -610,4 +611,20 @@ public function select_menu( $submenu ): string {
return (string) $submenu;
}

public function datetime_preview( $name, $value ): void {
if (
'gp_general[formatting][date_format]' === $name ||
'gp_general[formatting][time_format]' === $name
) {
Utility::render_template(
sprintf( '%s/includes/templates/admin/settings/partials/datetime_preview.php', GATHERPRESS_CORE_PATH ),
array(
'name' => $name,
'value' => $value,
),
true
);
}
}

}
2 changes: 1 addition & 1 deletion includes/core/classes/class-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ public function sortable_columns( array $columns ): array {
*/
public function get_the_event_date( $the_date ): string {
$settings = Settings::get_instance();
$use_event_date = $settings->get_value( 'gp_general', 'general', 'post_or_event_date' );
$use_event_date = $settings->get_value( 'general', 'general', 'post_or_event_date' );

// Check if the post is of the 'Event' post type and if event date should be used.
if ( Event::POST_TYPE !== get_post_type() || 1 !== intval( $use_event_date ) ) {
Expand Down
6 changes: 5 additions & 1 deletion includes/core/classes/class-utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public static function render_template( string $path, array $variables = array()
* @return string The key with the 'gp_' prefix.
*/
public static function prefix_key( string $key ): string {
return sprintf( 'gp_%s', $key );
if ( 0 !== strpos( $key, 'gp_' ) ) {
$key = sprintf( 'gp_%s', $key );
}

return $key;
}

/**
Expand Down
44 changes: 44 additions & 0 deletions includes/core/classes/settings/class-general.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,50 @@ protected function get_section(): array {
),
),
),
'formatting' => array(
'name' => __( 'Date & Time Formatting', 'gatherpress' ),
'description' => __( '<a href="https://wordpress.org/documentation/article/customize-date-and-time-format/">Documentation on date and time formatting</a>.', 'gatherpress' ),
'options' => array(
'date_format' => array(
'labels' => array(
'name' => __( 'Date Format', 'gatherpress' ),
),
'field' => array(
'label' => __( 'Format of date for scheduled events.', 'gatherpress' ),
'type' => 'text',
'size' => 'regular',
'options' => array(
'default' => 'l, F j, Y',
),
),
),
'time_format' => array(
'labels' => array(
'name' => __( 'Time Format', 'gatherpress' ),
),
'field' => array(
'label' => __( 'Format of time for scheduled events.', 'gatherpress' ),
'type' => 'text',
'size' => 'regular',
'options' => array(
'default' => 'g:i A',
),
),
),
'show_timezone' => array(
'labels' => array(
'name' => __( 'Show Timezone', 'gatherpress' ),
),
'field' => array(
'label' => __( 'Display the timezone for scheduled events.', 'gatherpress' ),
'type' => 'checkbox',
'options' => array(
'default' => '1',
),
),
),
)
),
'pages' => array(
'name' => __( 'Event Archive Pages', 'gatherpress' ),
'description' => __( 'GatherPress allows you to set event archives to pages you have created.', 'gatherpress' ),
Expand Down
2 changes: 1 addition & 1 deletion includes/templates/admin/settings/fields/autocomplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
<?php
if ( ! empty( $description ) ) {
?>
<p class="description"><?php echo esc_html( $description ); ?></p>
<p class="description"><?php echo wp_kses_post( $description ); ?></p>
<?php
}
2 changes: 1 addition & 1 deletion includes/templates/admin/settings/fields/checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
<?php
if ( ! empty( $description ) ) {
?>
<p class="description"><?php echo esc_html( $description ); ?></p>
<p class="description"><?php echo wp_kses_post( $description ); ?></p>
<?php
}
2 changes: 1 addition & 1 deletion includes/templates/admin/settings/fields/number.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<?php
if ( ! empty( $description ) ) {
?>
<p class="description"><?php echo esc_html( $description ); ?></p>
<p class="description"><?php echo wp_kses_post( $description ); ?></p>
<?php
}
?>
Expand Down
4 changes: 3 additions & 1 deletion includes/templates/admin/settings/fields/text.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
<?php
if ( ! empty( $description ) ) {
?>
<p class="description"><?php echo esc_html( $description ); ?></p>
<p class="description"><?php echo wp_kses_post( $description ); ?></p>
<?php
}

do_action( 'gatherpress_text_after', $name, $value );
?>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
if ( ! isset( $name, $value ) ) {
return;
}
?>
<p>
<strong><?php esc_html_e( 'Preview:' ); ?></strong> <span><?php echo esc_html( date_i18n( $value ) ); ?></span>
</p>

0 comments on commit 642c545

Please sign in to comment.