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

Add a settings popover to exclude post-types from tracking #51

Merged
merged 7 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
25 changes: 25 additions & 0 deletions assets/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ button.prpl-info-icon {
height: 100px;
}

.prpl-header-right {
display: flex;
gap: var(--prpl-padding);
align-items: center;
}

/*------------------------------------*\
Charts container.
\*------------------------------------*/
Expand Down Expand Up @@ -1088,3 +1094,22 @@ button.prpl-info-icon {
#prpl-dashboard-widget-todo-header p, #todo-list li {
font-size: 14px;
}

/*------------------------------------*\
Settings popup.
\*------------------------------------*/
#prpl-settings-form label {
display: block;
}

#prpl-settings-form p {
max-width: 42em;
}

#prpl-settings-form h3 {
font-size: 1.15em;
}

#prpl-settings-form button.button-primary {
margin-top: 1em;
}
18 changes: 18 additions & 0 deletions assets/js/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
document.getElementById( 'prpl-settings-form' ).addEventListener( 'submit', function( event ) {
event.preventDefault();
const form = new FormData( this );
const data = form.getAll( 'prpl-settings-post-types-exclude[]' );
aristath marked this conversation as resolved.
Show resolved Hide resolved

// Save the options.
progressPlannerAjaxRequest( {
url: progressPlanner.ajaxUrl,
data: {
action: 'progress_planner_save_cpt_settings',
_ajax_nonce: progressPlanner.nonce,
include_post_types: data,
}
} );

document.getElementById( 'submit-exclude-post-types' ).disabled = true;
aristath marked this conversation as resolved.
Show resolved Hide resolved
document.getElementById( 'submit-exclude-post-types' ).innerHTML = progressPlanner.l10n.saved;
aristath marked this conversation as resolved.
Show resolved Hide resolved
} );
14 changes: 8 additions & 6 deletions classes/activities/class-content-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
* @return string[]
*/
public static function get_post_types_names() {
$post_types = \get_post_types( [ 'public' => true ] );
$post_types = \array_filter( $post_types, 'is_post_type_viewable' );
unset( $post_types['attachment'] );
unset( $post_types['elementor_library'] ); // Elementor templates are not a post type we want to track.

return array_keys( $post_types );
$default = [ 'post', 'page' ];
$include_post_types = \array_filter(
Settings::get( [ 'include_post_types' ], $default ),
function( $post_type ) {

Check failure on line 28 in classes/activities/class-content-helpers.php

View workflow job for this annotation

GitHub Actions / Check code style

Expected 1 space after FUNCTION keyword; 0 found

Check failure on line 28 in classes/activities/class-content-helpers.php

View workflow job for this annotation

GitHub Actions / Check code style

Expected 1 space after FUNCTION keyword; 0 found
return $post_type && \post_type_exists( $post_type ) && is_post_type_viewable( $post_type );
}
);
return empty( $include_post_types ) ? $default : \array_values( $include_post_types );
}

/**
Expand Down
38 changes: 38 additions & 0 deletions classes/admin/class-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public function __construct() {
private function register_hooks() {
\add_action( 'admin_menu', [ $this, 'add_page' ] );
\add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
\add_action( 'wp_ajax_progress_planner_save_cpt_settings', [ $this, 'save_cpt_settings' ] );
}

/**
Expand Down Expand Up @@ -187,6 +188,15 @@ public static function register_scripts() {
true
);

// Register the admin script for the settings popup.
\wp_register_script(
'progress-planner-settings',
PROGRESS_PLANNER_URL . '/assets/js/settings.js',
[ 'progress-planner-ajax' ],
filemtime( PROGRESS_PLANNER_DIR . '/assets/js/settings.js' ),
true
);

// Register the admin script for the page.
\wp_register_script(
'progress-planner-admin',
Expand Down Expand Up @@ -214,6 +224,15 @@ public static function register_scripts() {
// Localize the scripts.
\wp_localize_script( 'progress-planner-onboard', 'progressPlanner', $localize_data );
\wp_localize_script( 'progress-planner-admin', 'progressPlanner', $localize_data );
$localize_data_settings = array_merge(
$localize_data,
[
'l10n' => [
'saved' => \esc_html__( 'Saved', 'progress-planner' ),
],
]
);
\wp_localize_script( 'progress-planner-settings', 'progressPlanner', $localize_data_settings );
\wp_localize_script(
'progress-planner-todo',
'progressPlannerTodo',
Expand All @@ -240,6 +259,7 @@ public static function enqueue_scripts() {
\wp_enqueue_script( 'progress-planner-onboard' );
\wp_enqueue_script( 'progress-planner-admin' );
\wp_enqueue_script( 'progress-planner-todo' );
\wp_enqueue_script( 'progress-planner-settings' );
}

/**
Expand All @@ -255,4 +275,22 @@ public static function enqueue_styles() {
filemtime( PROGRESS_PLANNER_DIR . '/assets/css/admin.css' )
);
}

/**
* Save the post types settings.
*
* @return void
*/
public function save_cpt_settings() {
\check_ajax_referer( 'progress_planner', 'nonce', false );
$include_post_types = isset( $_POST['include_post_types'] ) ? \sanitize_text_field( \wp_unslash( $_POST['include_post_types'] ) ) : 'post,page';
$include_post_types = \explode( ',', $include_post_types );
\Progress_Planner\Settings::set( 'include_post_types', $include_post_types );

\wp_send_json_success(
[
'message' => \esc_html__( 'Settings saved.', 'progress-planner' ),
]
);
}
}
14 changes: 12 additions & 2 deletions classes/popups/class-popup.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,28 @@ public function __construct() {
}

/**
* Render the widget content.
* Render the triggering button.
*
* @return void
*/
public function render() {
public function render_button() {
?>
<!-- The triggering button. -->
<button class="prpl-info-icon" popovertarget="prpl-popover-<?php echo \esc_attr( $this->id ); ?>">
<span class="dashicons dashicons-info-outline"></span>
<span class="screen-reader-text"><?php \esc_html_e( 'More info', 'progress-planner' ); ?>
</button>
<?php
}

/**
* Render the widget content.
*
* @return void
*/
public function render() {
?>
<?php $this->render_button(); ?>
<div id="prpl-popover-<?php echo \esc_attr( $this->id ); ?>" class="prpl-popover" popover>
<!-- The content. -->
<?php $this->the_content(); ?>
Expand Down
72 changes: 72 additions & 0 deletions classes/popups/class-settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Settings popup.
*
* @package Progress_Planner
*/

namespace Progress_Planner\Popups;

use Progress_Planner\Activities\Content_Helpers;

/**
* Settings popup.
*/
final class Settings extends Popup {

/**
* The popover ID.
*
* @var string
*/
protected $id = 'settings';

/**
* Render the triggering button.
*
* @return void
*/
public function render_button() {
?>
<!-- The triggering button. -->
<button class="prpl-info-icon" popovertarget="prpl-popover-<?php echo \esc_attr( $this->id ); ?>">
<span class="dashicons dashicons-admin-generic"></span>
<span class="screen-reader-text"><?php \esc_html_e( 'Settings', 'progress-planner' ); ?>
</button>
<?php
}

/**
* Render the widget content.
*
* @return void
*/
protected function the_content() {
$saved_settings = Content_Helpers::get_post_types_names();
$post_types = \array_filter( \get_post_types( [ 'public' => true ] ), 'is_post_type_viewable' );
unset( $post_types['attachment'] );
unset( $post_types['elementor_library'] ); // Elementor templates are not a post type we want to track.
?>

<h2><?php \esc_html_e( 'Settings', 'progress-planner' ); ?></h2>

<form id="prpl-settings-form">
<h3><?php \esc_html_e( 'Post Types', 'progress-planner' ); ?></h3>
<p><?php \esc_html_e( 'Select the post types you want to include in activity scores. This setting will affect which post-type activities get tracked.', 'progress-planner' ); ?></p>
<?php foreach ( $post_types as $post_type ) : ?>
<label>
<input
type="checkbox"
name="prpl-settings-post-types-exclude[]"
jdevalk marked this conversation as resolved.
Show resolved Hide resolved
value="<?php echo \esc_attr( $post_type ); ?>"
<?php checked( \in_array( $post_type, $saved_settings, true ) ); ?>
/>
<?php echo \esc_html( \get_post_type_object( $post_type )->labels->name ); ?>
</label>
<?php endforeach; ?>

<button id="submit-exclude-post-types" class="button button-primary"><?php \esc_html_e( 'Save', 'progress-planner' ); ?></button>
aristath marked this conversation as resolved.
Show resolved Hide resolved
</form>
<?php
}
}
83 changes: 43 additions & 40 deletions views/admin-page-header.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,48 @@
?>
</div>

<div class="prpl-header-select-range">
<label for="prpl-select-range" class="screen-reader-text">
<?php \esc_html_e( 'Select range:', 'progress-planner' ); ?>
</label>
<select id="prpl-select-range">
<?php
foreach ( [
'-3 months' => \esc_html__( 'Activity over the past 3 months', 'progress-planner' ),
'-6 months' => \esc_html__( 'Activity over the past 6 months', 'progress-planner' ),
'-12 months' => \esc_html__( 'Activity over the past 12 months', 'progress-planner' ),
'-18 months' => \esc_html__( 'Activity over the past 18 months', 'progress-planner' ),
'-24 months' => \esc_html__( 'Activity over the past 24 months', 'progress-planner' ),
] as $progress_planner_range => $progress_planner_label ) {
printf(
'<option value="%1$s" %2$s>%3$s</option>',
\esc_attr( $progress_planner_range ),
\selected( $progress_planner_active_range, $progress_planner_range, false ),
\esc_html( $progress_planner_label )
);
}
?>
</select>
<label for="prpl-select-frequency" class="screen-reader-text">
<?php \esc_html_e( 'Select frequency:', 'progress-planner' ); ?>
</label>
<select id="prpl-select-frequency">
<?php
foreach ( [
'weekly' => \esc_html__( 'Weekly', 'progress-planner' ),
'monthly' => \esc_html__( 'Monthly', 'progress-planner' ),
] as $progress_planner_frequency => $progress_planner_label ) {
printf(
'<option value="%1$s" %2$s>%3$s</option>',
\esc_attr( $progress_planner_frequency ),
\selected( $progress_planner_active_frequency, $progress_planner_frequency, false ),
\esc_html( $progress_planner_label )
);
}
?>
</select>
<div class="prpl-header-right">
<?php new \Progress_Planner\Popups\Settings(); ?>
<div class="prpl-header-select-range">
<label for="prpl-select-range" class="screen-reader-text">
<?php \esc_html_e( 'Select range:', 'progress-planner' ); ?>
</label>
<select id="prpl-select-range">
<?php
foreach ( [
'-3 months' => \esc_html__( 'Activity over the past 3 months', 'progress-planner' ),
'-6 months' => \esc_html__( 'Activity over the past 6 months', 'progress-planner' ),
'-12 months' => \esc_html__( 'Activity over the past 12 months', 'progress-planner' ),
'-18 months' => \esc_html__( 'Activity over the past 18 months', 'progress-planner' ),
'-24 months' => \esc_html__( 'Activity over the past 24 months', 'progress-planner' ),
] as $progress_planner_range => $progress_planner_label ) {
printf(
'<option value="%1$s" %2$s>%3$s</option>',
\esc_attr( $progress_planner_range ),
\selected( $progress_planner_active_range, $progress_planner_range, false ),
\esc_html( $progress_planner_label )
);
}
?>
</select>
<label for="prpl-select-frequency" class="screen-reader-text">
<?php \esc_html_e( 'Select frequency:', 'progress-planner' ); ?>
</label>
<select id="prpl-select-frequency">
<?php
foreach ( [
'weekly' => \esc_html__( 'Weekly', 'progress-planner' ),
'monthly' => \esc_html__( 'Monthly', 'progress-planner' ),
] as $progress_planner_frequency => $progress_planner_label ) {
printf(
'<option value="%1$s" %2$s>%3$s</option>',
\esc_attr( $progress_planner_frequency ),
\selected( $progress_planner_active_frequency, $progress_planner_frequency, false ),
\esc_html( $progress_planner_label )
);
}
?>
</select>
</div>
</div>
</div>
Loading