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 logging for patterns and flags #432

Merged
merged 3 commits into from
Mar 14, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

require_once __DIR__ . '/includes/class-rest-flags-controller.php';
require_once __DIR__ . '/includes/class-rest-favorite-controller.php';
require_once __DIR__ . '/includes/logging.php';
require_once __DIR__ . '/includes/pattern-post-type.php';
require_once __DIR__ . '/includes/pattern-flag-post-type.php';
require_once __DIR__ . '/includes/pattern-validation.php';
Expand Down
101 changes: 101 additions & 0 deletions public_html/wp-content/plugins/pattern-directory/includes/logging.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace WordPressdotorg\Pattern_Directory\Logging;

use WordPressdotorg\InternalNotes;
use const WordPressdotorg\Pattern_Directory\Pattern_Post_Type\POST_TYPE as PATTERN_POST_TYPE;
use const WordPressdotorg\Pattern_Directory\Pattern_Flag_Post_Type\{ PENDING_STATUS, RESOLVED_STATUS, POST_TYPE as FLAG_POST_TYPE };

/**
* Actions and filters.
*/
add_action( 'transition_post_status', __NAMESPACE__ . '\flag_status_change', 10, 3 );

/**
* Check if logging is enabled for patterns.
*
* @return bool
*/
function logging_enabled() {
$support = post_type_supports( PATTERN_POST_TYPE, 'wporg-internal-notes' )
&& post_type_supports( PATTERN_POST_TYPE, 'wporg-log-notes' );
$callable_func = is_callable( '\\WordPressdotorg\\InternalNotes\\create_note' );

return $support && $callable_func;
}

/**
* Add a log entry to a pattern when a flag's status changes.
*
* @param string $new_status
* @param string $old_status
* @param \WP_Post $post
*
* @return void
*/
function flag_status_change( $new_status, $old_status, $post ) {
if ( ! logging_enabled() ) {
return;
}

if ( FLAG_POST_TYPE !== get_post_type( $post ) ) {
return;
}

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}

$pattern_id = $post->post_parent;

if ( ! $pattern_id ) {
return;
}

$new = get_post_status_object( $new_status );
$user = get_user_by( 'id', $post->post_author );
$user_handle = sprintf(
'@%s',
$user->user_login
);

$msg = '';
if ( $new_status === $old_status ) {
return;
} elseif ( 'new' === $old_status && PENDING_STATUS === $new_status ) {
$msg = sprintf(
// translators: User name;
__( 'New flag submitted by %s', 'wporg-patterns' ),
esc_html( $user_handle ),
);
} elseif ( PENDING_STATUS === $new_status ) {
$msg = sprintf(
// translators: 1. User name; 2. Post status;
__( 'Flag submitted by %1$s set to %2$s', 'wporg-patterns' ),
esc_html( $user_handle ),
esc_html( $new->label )
);
} elseif ( RESOLVED_STATUS === $new_status ) {
$msg = sprintf(
// translators: 1. User name; 2. Post status;
__( 'Flag submitted by %1$s marked as %2$s', 'wporg-patterns' ),
esc_html( $user_handle ),
esc_html( $new->label )
);
} elseif ( 'trash' === $new_status ) {
$msg = sprintf(
// translators: User name;
__( 'Flag submitted by %s moved to trash.', 'wporg-patterns' ),
esc_html( $user_handle )
);
}

if ( $msg ) {
$data = array(
'post_excerpt' => $msg,
'post_type' => InternalNotes\LOG_POST_TYPE,
);

InternalNotes\create_note( $pattern_id, $data );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function register_post_type_data() {
'show_in_admin_bar' => false,
'show_in_rest' => true,
'rest_controller_class' => '\\WordPressdotorg\\Pattern_Directory\\REST_Flags_Controller',
'supports' => array( 'author', 'excerpt', 'wporg-internal-notes' ),
'supports' => array( 'author', 'excerpt' ),
'can_export' => false,
'delete_with_user' => false,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function register_post_type_data() {
'public' => true,
'show_in_rest' => true,
'rewrite' => array( 'slug' => 'pattern' ),
'supports' => array( 'title', 'editor', 'author', 'custom-fields', 'revisions', 'wporg-internal-notes' ),
'supports' => array( 'title', 'editor', 'author', 'custom-fields', 'revisions', 'wporg-internal-notes', 'wporg-log-notes' ),
'capability_type' => array( 'pattern', 'patterns' ),
'map_meta_cap' => true,
)
Expand Down