From 13b4378b84aaa49d24c8c6c28314add57d51390b Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Wed, 21 Aug 2024 22:30:05 +0800 Subject: [PATCH 01/46] Add tracks classes --- class-workflow.php | 15 ++++++ .../telemetry/class-vip-workflow-tracker.php | 54 +++++++++++++++++++ modules/telemetry/telemetry-init.php | 34 ++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 modules/telemetry/class-vip-workflow-tracker.php create mode 100644 modules/telemetry/telemetry-init.php diff --git a/class-workflow.php b/class-workflow.php index 06bb88de..6f0393ab 100644 --- a/class-workflow.php +++ b/class-workflow.php @@ -3,6 +3,7 @@ namespace VIPWorkflow; use VIPWorkflow\Common\PHP\Module; +use VIPWorkflow\Telemetry\Telemetry; use stdClass; // Core class @@ -122,6 +123,10 @@ private function setup_actions() { add_action( 'admin_init', [ $this, 'action_admin_init' ] ); add_action( 'admin_menu', [ $this, 'action_admin_menu' ] ); + + // need to ensure all plugins are loaded first so that the Telemetry + // lib is available for use + add_action( 'plugin_loaded', [ $this, 'start_tracking' ] ); } /** @@ -385,6 +390,16 @@ public function update_all_module_options( $mod_name, $new_options ) { public function register_scripts_and_styles() { wp_enqueue_style( 'vw-admin-css', VIP_WORKFLOW_URL . 'common/css/vip-workflow-admin.css', false, VIP_WORKFLOW_VERSION, 'all' ); } + + /** + * Start tracking events + */ + public function start_tracking() { + if ( class_exists( 'Automattic\VIP\Telemetry\Tracks' ) ) { + $telemetry = new Telemetry(); + $telemetry->init(); + } + } } VIP_Workflow::instance(); diff --git a/modules/telemetry/class-vip-workflow-tracker.php b/modules/telemetry/class-vip-workflow-tracker.php new file mode 100644 index 00000000..6f7f88b2 --- /dev/null +++ b/modules/telemetry/class-vip-workflow-tracker.php @@ -0,0 +1,54 @@ +tracks = $tracks; + } + + /** + * Record an event + * + * @param String $event_name The event name + * @param array $event_data The event data + */ + public function record_event( String $event_name, array $event_data = [] ): void { + $this->tracks->record_event( $event_name, $event_data ); + } + + /** + * Wrap event callbacks in closure to inject the Tracker class + * + * @param callable $callback The callback function + * @param Tracker $tracker The Tracker class + * @return callable|null The wrapped callback function + */ + public static function track_event( String $callback, Tracker $tracker ): callable|null { + if ( is_callable( $callback ) ) { + return function () use ( $callback, $tracker ) { + // get the arguments passed to the callback + $args = func_get_args(); + // add the tracker to the arguments + $args[] = $tracker; + + return call_user_func_array( $callback, $args ); + }; + } + return null; + } +} diff --git a/modules/telemetry/telemetry-init.php b/modules/telemetry/telemetry-init.php new file mode 100644 index 00000000..e3b80a93 --- /dev/null +++ b/modules/telemetry/telemetry-init.php @@ -0,0 +1,34 @@ +tracker = new Tracker( $telemetry ); + } + + /** + * Initialize the module and register event callbacks + */ + public function init(): void { + add_action( + 'transition_post_status', + Tracker::track_event( 'VIPWorkflow\Telemetry\Events\record_custom_status_change', $this->tracker ), + 10, + 3 + ); + } +} From 453ad23911954233ef8bf6dcc36924a10f080dda Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Wed, 21 Aug 2024 22:30:14 +0800 Subject: [PATCH 02/46] Add events --- modules/custom-status/custom-status.php | 6 ++++ modules/telemetry/events/status-events.php | 38 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 modules/telemetry/events/status-events.php diff --git a/modules/custom-status/custom-status.php b/modules/custom-status/custom-status.php index dbcda91d..9f62f718 100644 --- a/modules/custom-status/custom-status.php +++ b/modules/custom-status/custom-status.php @@ -465,6 +465,12 @@ public function add_custom_status( $term, $args = [] ) { $slug = ( ! empty( $args['slug'] ) ) ? $args['slug'] : sanitize_title( $term ); unset( $args['slug'] ); $encoded_description = $this->get_encoded_description( $args ); + + /** + * Fires before a custom status is added to the database. + */ + do_action( 'vw_add_custom_status', $term, $args ); + $response = wp_insert_term( $term, self::TAXONOMY_KEY, [ 'slug' => $slug, 'description' => $encoded_description, diff --git a/modules/telemetry/events/status-events.php b/modules/telemetry/events/status-events.php new file mode 100644 index 00000000..867b95cc --- /dev/null +++ b/modules/telemetry/events/status-events.php @@ -0,0 +1,38 @@ +post_type !== 'post' ) { + return; + } + + if ( in_array( $new_status, [ $old_status, 'inherit', 'auto-draft', 'publish', 'draft' ] ) ) { + return; + } + + $tracker->record_event( 'custom_status_change', [ + 'new_status' => $new_status, + 'old_status' => $old_status, + 'post_id' => $post->ID, + ] ); +} + +function record_add_custom_status( + string $term, + array $args, + Tracker $tracker + ): void { + $tracker->record_event( 'add_custom_status', [ + 'term' => $term, + 'args' => $args, + ] ); +} From 6796d6892eb9ee6cc9c1a248796a8d75f7be928e Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Wed, 21 Aug 2024 23:19:44 +0800 Subject: [PATCH 03/46] Fix telemetry namespace --- modules/telemetry/class-vip-workflow-tracker.php | 2 +- modules/telemetry/events/status-events.php | 2 +- modules/telemetry/{telemetry-init.php => telemetry.php} | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename modules/telemetry/{telemetry-init.php => telemetry.php} (93%) diff --git a/modules/telemetry/class-vip-workflow-tracker.php b/modules/telemetry/class-vip-workflow-tracker.php index 6f7f88b2..68b354a3 100644 --- a/modules/telemetry/class-vip-workflow-tracker.php +++ b/modules/telemetry/class-vip-workflow-tracker.php @@ -1,6 +1,6 @@ Date: Wed, 21 Aug 2024 23:19:55 +0800 Subject: [PATCH 04/46] Load telemetry module on init --- class-workflow.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/class-workflow.php b/class-workflow.php index 6f0393ab..6d4cfd6f 100644 --- a/class-workflow.php +++ b/class-workflow.php @@ -120,13 +120,11 @@ private function load_modules() { private function setup_actions() { add_action( 'init', [ $this, 'action_init' ] ); add_action( 'init', [ $this, 'action_init_after' ], 1000 ); + // Load the telemetry module in init as all plugins are loaded by then + add_action( 'init', [ $this, 'start_tracking' ] ); add_action( 'admin_init', [ $this, 'action_admin_init' ] ); add_action( 'admin_menu', [ $this, 'action_admin_menu' ] ); - - // need to ensure all plugins are loaded first so that the Telemetry - // lib is available for use - add_action( 'plugin_loaded', [ $this, 'start_tracking' ] ); } /** From 04d5d9674630be17b091f0b7da86868a34515d8b Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Wed, 21 Aug 2024 23:21:59 +0800 Subject: [PATCH 05/46] Fix telemetry namespace reference --- class-workflow.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/class-workflow.php b/class-workflow.php index 6d4cfd6f..f703f1ce 100644 --- a/class-workflow.php +++ b/class-workflow.php @@ -3,7 +3,7 @@ namespace VIPWorkflow; use VIPWorkflow\Common\PHP\Module; -use VIPWorkflow\Telemetry\Telemetry; +use VIPWorkflow\Modules\Telemetry\Telemetry; use stdClass; // Core class From ba30eedb7964dd5fa2faf99bb8bc1ae4a75ae3f2 Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Wed, 21 Aug 2024 23:34:18 +0800 Subject: [PATCH 06/46] Fix namespace reference --- modules/telemetry/events/status-events.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/telemetry/events/status-events.php b/modules/telemetry/events/status-events.php index fb3ac788..162bb427 100644 --- a/modules/telemetry/events/status-events.php +++ b/modules/telemetry/events/status-events.php @@ -2,7 +2,7 @@ namespace VIPWorkflow\Modules\Telemetry\Events; -use VIPWorkflow\Telemetry\Tracker; +use VIPWorkflow\Modules\Telemetry\Tracker; use WP_Post; function record_custom_status_change( From 3ca463c2b12406463ff65f0ce378d35d216f24d5 Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Wed, 21 Aug 2024 23:43:17 +0800 Subject: [PATCH 07/46] Require the tracker class --- modules/telemetry/telemetry.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index 2c23c796..a11efe9f 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -4,6 +4,8 @@ use Automattic\VIP\Telemetry\Tracks; +require_once __DIR__ . '/class-vip-workflow-tracker.php'; + class Telemetry { /** * Tracker instance From 251c8e8371e1dc7e8d061860f0e0f4269a3d4a35 Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Thu, 22 Aug 2024 11:15:53 +0800 Subject: [PATCH 08/46] Require events files --- modules/telemetry/telemetry.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index a11efe9f..1a29c775 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -5,6 +5,7 @@ use Automattic\VIP\Telemetry\Tracks; require_once __DIR__ . '/class-vip-workflow-tracker.php'; +require_once __DIR__ . '/events/status-events.php'; class Telemetry { /** @@ -28,7 +29,7 @@ public function __construct() { public function init(): void { add_action( 'transition_post_status', - Tracker::track_event( 'VIPWorkflow\Telemetry\Events\record_custom_status_change', $this->tracker ), + Tracker::track_event( 'VIPWorkflow\Modules\Telemetry\Events\record_custom_status_change', $this->tracker ), 10, 3 ); From 84b0bf87d7dcc9e241dc82c914758ba500c7f01d Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Thu, 22 Aug 2024 11:30:18 +0800 Subject: [PATCH 09/46] Add custom status added event --- modules/custom-status/custom-status.php | 3 +++ modules/telemetry/telemetry.php | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/modules/custom-status/custom-status.php b/modules/custom-status/custom-status.php index 9f62f718..1efab1bc 100644 --- a/modules/custom-status/custom-status.php +++ b/modules/custom-status/custom-status.php @@ -468,6 +468,9 @@ public function add_custom_status( $term, $args = [] ) { /** * Fires before a custom status is added to the database. + * + * @param string $term The status to add or update + * @param array|string $args Change the values of the inserted term */ do_action( 'vw_add_custom_status', $term, $args ); diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index 1a29c775..bc365dbe 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -33,5 +33,11 @@ public function init(): void { 10, 3 ); + add_action( + 'vw_add_custom_status', + Tracker::track_event( 'VIPWorkflow\Modules\Telemetry\Events\record_add_custom_status', $this->tracker ), + 10, + 3 + ); } } From cf715c2631832a6f46b3f5d8ecc23ab407256aec Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Thu, 22 Aug 2024 11:34:59 +0800 Subject: [PATCH 10/46] Add custom status delete event --- modules/custom-status/custom-status.php | 9 +++++++++ modules/telemetry/events/status-events.php | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/modules/custom-status/custom-status.php b/modules/custom-status/custom-status.php index 1efab1bc..60d4db1a 100644 --- a/modules/custom-status/custom-status.php +++ b/modules/custom-status/custom-status.php @@ -573,6 +573,15 @@ public function delete_custom_status( $status_id, $args = [] ) { // Reset status cache again, as reassign_post_status() will recache prior statuses $this->custom_statuses_cache = []; + /** + * Fires before a custom status is deleted from the database. + * + * @param int $status_id The ID of the status being deleted + * @param string $old_status_slug The slug of the status being deleted + * @param array $args The arguments passed to the delete function + */ + do_action( 'vw_delete_custom_status', $status_id, $old_status_slug, $args ); + return wp_delete_term( $status_id, self::TAXONOMY_KEY, $args ); } else { return new WP_Error( 'restricted', __( 'Restricted status ', 'vip-workflow' ) . '(' . $this->get_custom_status_by( 'id', $status_id )->name . ')' ); diff --git a/modules/telemetry/events/status-events.php b/modules/telemetry/events/status-events.php index 162bb427..1eb8933b 100644 --- a/modules/telemetry/events/status-events.php +++ b/modules/telemetry/events/status-events.php @@ -36,3 +36,16 @@ function record_add_custom_status( 'args' => $args, ] ); } + +function record_delete_custom_status( + int $status_id, + string $slug, + array $args, + Tracker $tracker + ): void { + $tracker->record_event( 'delete_custom_status', [ + 'status_id' => $status_id, + 'slug' => $slug, + 'args' => $args, + ] ); +} From b7a2349e5e8bdacc7f6a21e1fc165d4534ea4e27 Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Thu, 22 Aug 2024 11:41:21 +0800 Subject: [PATCH 11/46] Add custom status change event --- modules/custom-status/custom-status.php | 8 ++++++++ modules/telemetry/events/status-events.php | 13 ++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/modules/custom-status/custom-status.php b/modules/custom-status/custom-status.php index 60d4db1a..1d3028b0 100644 --- a/modules/custom-status/custom-status.php +++ b/modules/custom-status/custom-status.php @@ -532,6 +532,14 @@ public function update_custom_status( $status_id, $args = [] ) { $encoded_description = $this->get_encoded_description( $args_to_encode ); $args['description'] = $encoded_description; + /** + * Fires before a custom status is updated in the database. + * + * @param int $status_id The ID of the status being updated + * @param array $args The arguments passed to the update function + */ + do_action( 'vw_update_custom_status', $status_id, $args ); + $updated_status_array = wp_update_term( $status_id, self::TAXONOMY_KEY, $args ); $updated_status = $this->get_custom_status_by( 'id', $updated_status_array['term_id'] ); diff --git a/modules/telemetry/events/status-events.php b/modules/telemetry/events/status-events.php index 1eb8933b..ab9ae85f 100644 --- a/modules/telemetry/events/status-events.php +++ b/modules/telemetry/events/status-events.php @@ -19,7 +19,7 @@ function record_custom_status_change( return; } - $tracker->record_event( 'custom_status_change', [ + $tracker->record_event( 'post_custom_status_change', [ 'new_status' => $new_status, 'old_status' => $old_status, 'post_id' => $post->ID, @@ -49,3 +49,14 @@ function record_delete_custom_status( 'args' => $args, ] ); } + +function record_update_custom_status( + int $status_id, + array $args, + Tracker $tracker + ): void { + $tracker->record_event( 'update_custom_status', [ + 'status_id' => $status_id, + 'args' => $args, + ] ); +} From b593e454405cbf8e50c688f326921269adea3c68 Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Thu, 22 Aug 2024 11:41:40 +0800 Subject: [PATCH 12/46] Add hooks into the new actions --- modules/telemetry/telemetry.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index bc365dbe..8ceac3d2 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -37,7 +37,19 @@ public function init(): void { 'vw_add_custom_status', Tracker::track_event( 'VIPWorkflow\Modules\Telemetry\Events\record_add_custom_status', $this->tracker ), 10, + 2 + ); + add_action( + 'vw_delete_custom_status', + Tracker::track_event( 'VIPWorkflow\Modules\Telemetry\Events\record_delete_custom_status', $this->tracker ), + 10, 3 ); + add_action( + 'vw_update_custom_status', + Tracker::track_event( 'VIPWorkflow\Modules\Telemetry\Events\record_update_custom_status', $this->tracker ), + 10, + 2 + ); } } From 4622304c5765cfd3046213062f1b74efcec1b19b Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Thu, 22 Aug 2024 12:23:45 +0800 Subject: [PATCH 13/46] Add notification events --- modules/notifications/notifications.php | 9 +++++++++ .../telemetry/events/notification-events.php | 20 +++++++++++++++++++ modules/telemetry/telemetry.php | 7 +++++++ 3 files changed, 36 insertions(+) create mode 100644 modules/telemetry/events/notification-events.php diff --git a/modules/notifications/notifications.php b/modules/notifications/notifications.php index fa113164..6fa5a11e 100644 --- a/modules/notifications/notifications.php +++ b/modules/notifications/notifications.php @@ -192,6 +192,15 @@ public function notification_status_change( $new_status, $old_status, $post ) { $body .= $this->get_notification_footer( $post ); + /** + * Fires before a notification is sent + * + * @param WP_Post $post The post object + * @param string $subject The subject of the email + * @param WP_User $current_user The user who is taking the action + */ + do_action( 'vw_notification_status_change', $post, $subject, $current_user ); + $this->send_email( 'status-change', $post, $subject, $body ); // ToDo: See how we can optimize this, using batching as well as async processing. diff --git a/modules/telemetry/events/notification-events.php b/modules/telemetry/events/notification-events.php new file mode 100644 index 00000000..73f015b9 --- /dev/null +++ b/modules/telemetry/events/notification-events.php @@ -0,0 +1,20 @@ +record_event( 'notification_sent', [ + 'subject' => $subject, + 'post' => (array) $post, + 'user' => (array) $user, + ] ); +} diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index 8ceac3d2..ac64f135 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -6,6 +6,7 @@ require_once __DIR__ . '/class-vip-workflow-tracker.php'; require_once __DIR__ . '/events/status-events.php'; +require_once __DIR__ . '/events/notification-events.php'; class Telemetry { /** @@ -51,5 +52,11 @@ public function init(): void { 10, 2 ); + add_action( + 'vw_notification_status_change', + Tracker::track_event( 'VIPWorkflow\Modules\Telemetry\Events\record_notification_sent', $this->tracker ), + 10, + 3 + ); } } From 77b64a0a96b11df3f6b193d4336f74b7643752d6 Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Thu, 22 Aug 2024 12:37:31 +0800 Subject: [PATCH 14/46] Fix phpcs errors --- modules/telemetry/class-vip-workflow-tracker.php | 6 +++--- modules/telemetry/events/status-events.php | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/modules/telemetry/class-vip-workflow-tracker.php b/modules/telemetry/class-vip-workflow-tracker.php index 68b354a3..bf206b8e 100644 --- a/modules/telemetry/class-vip-workflow-tracker.php +++ b/modules/telemetry/class-vip-workflow-tracker.php @@ -24,10 +24,10 @@ public function __construct( Tracks $tracks ) { /** * Record an event * - * @param String $event_name The event name + * @param string $event_name The event name * @param array $event_data The event data */ - public function record_event( String $event_name, array $event_data = [] ): void { + public function record_event( string $event_name, array $event_data = [] ): void { $this->tracks->record_event( $event_name, $event_data ); } @@ -38,7 +38,7 @@ public function record_event( String $event_name, array $event_data = [] ): void * @param Tracker $tracker The Tracker class * @return callable|null The wrapped callback function */ - public static function track_event( String $callback, Tracker $tracker ): callable|null { + public static function track_event( string $callback, Tracker $tracker ): callable|null { if ( is_callable( $callback ) ) { return function () use ( $callback, $tracker ) { // get the arguments passed to the callback diff --git a/modules/telemetry/events/status-events.php b/modules/telemetry/events/status-events.php index ab9ae85f..665b03cf 100644 --- a/modules/telemetry/events/status-events.php +++ b/modules/telemetry/events/status-events.php @@ -10,8 +10,8 @@ function record_custom_status_change( string $old_status, WP_Post $post, Tracker $tracker - ): void { - if ( $post->post_type !== 'post' ) { +): void { + if ( 'post' !== $post->post_type ) { return; } @@ -30,7 +30,7 @@ function record_add_custom_status( string $term, array $args, Tracker $tracker - ): void { +): void { $tracker->record_event( 'add_custom_status', [ 'term' => $term, 'args' => $args, @@ -42,7 +42,7 @@ function record_delete_custom_status( string $slug, array $args, Tracker $tracker - ): void { +): void { $tracker->record_event( 'delete_custom_status', [ 'status_id' => $status_id, 'slug' => $slug, @@ -54,7 +54,7 @@ function record_update_custom_status( int $status_id, array $args, Tracker $tracker - ): void { +): void { $tracker->record_event( 'update_custom_status', [ 'status_id' => $status_id, 'args' => $args, From aee43978a1aaaacc23a44c0625286d37ccf6e47e Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Thu, 22 Aug 2024 21:17:55 +0800 Subject: [PATCH 15/46] Add admin update event --- class-workflow.php | 9 +++++++ modules/telemetry/events/settings-events.php | 26 ++++++++++++++++++++ modules/telemetry/telemetry.php | 6 +++++ 3 files changed, 41 insertions(+) create mode 100644 modules/telemetry/events/settings-events.php diff --git a/class-workflow.php b/class-workflow.php index f703f1ce..8b59091d 100644 --- a/class-workflow.php +++ b/class-workflow.php @@ -156,6 +156,15 @@ public function action_admin_init() { $this->$mod_name->upgrade( $previous_version ); } } + + /** + * Fires when the plugin is upgraded + * + * @param string $previous_version The previous version of the plugin + * @param string $new_version The new version of the plugin + */ + do_action( 'vw_upgrade_version', $previous_version, VIP_WORKFLOW_VERSION ); + update_option( $this->options_group . 'version', VIP_WORKFLOW_VERSION ); } elseif ( ! $previous_version ) { update_option( $this->options_group . 'version', VIP_WORKFLOW_VERSION ); diff --git a/modules/telemetry/events/settings-events.php b/modules/telemetry/events/settings-events.php new file mode 100644 index 00000000..4448c566 --- /dev/null +++ b/modules/telemetry/events/settings-events.php @@ -0,0 +1,26 @@ +custom_status->get_custom_statuses(); + + // Get all posts count + $posts_count = wp_count_posts(); + // Only care about published and posts with custom status + $total_posts = (int) $posts_count->publish; + foreach ( $custom_statuses as $status ) { + $total_posts += (int) $posts_count->{ $status->slug }; + } + + $tracker->record_event( 'administration_update', [ + 'previous_version' => $previous_version, + 'new_version' => $new_version, + 'custom_statuses' => count( $custom_statuses ), + 'total_posts' => $total_posts, + ] ); +} diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index ac64f135..b6eda922 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -58,5 +58,11 @@ public function init(): void { 10, 3 ); + add_action( + 'vw_upgrade_version', + Tracker::track_event( 'VIPWorkflow\Modules\Telemetry\Events\record_admin_update', $this->tracker ), + 10, + 3 + ); } } From f97a6f24909386741480b7c01e29eb02e6eedae6 Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Mon, 26 Aug 2024 09:41:49 +0800 Subject: [PATCH 16/46] Fix merge error --- class-workflow.php | 1 + 1 file changed, 1 insertion(+) diff --git a/class-workflow.php b/class-workflow.php index 60a139b0..a5311dff 100644 --- a/class-workflow.php +++ b/class-workflow.php @@ -4,6 +4,7 @@ use VIPWorkflow\Modules\Shared\PHP\Module; use VIPWorkflow\Modules\Telemetry\Telemetry; +use stdClass; // Core class #[\AllowDynamicProperties] From cf6b2de6b77fae372eea03900b85e63c3fd85a5f Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Mon, 26 Aug 2024 13:33:44 +0800 Subject: [PATCH 17/46] Add settings events --- modules/settings/settings.php | 12 ++++++- modules/telemetry/events/settings-events.php | 38 ++++++++++++++++++++ modules/telemetry/telemetry.php | 9 ++++- 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/modules/settings/settings.php b/modules/settings/settings.php index b87fee68..c7ada027 100644 --- a/modules/settings/settings.php +++ b/modules/settings/settings.php @@ -263,8 +263,18 @@ public function helper_settings_validate_and_save() { $new_options = VIP_Workflow::instance()->$module_name->settings_validate( $new_options ); } + $old_options = (array) VIP_Workflow::instance()->$module_name->module->options; + + /** + * Fires before saving the settings for all modules + * + * @param array $new_options The new options + * @param array $old_options The old options + */ + do_action( 'vw_save_settings', $new_options, $old_options ); + // Cast our object and save the data. - $new_options = (object) array_merge( (array) VIP_Workflow::instance()->$module_name->module->options, $new_options ); + $new_options = (object) array_merge( (array) $old_options, $new_options ); VIP_Workflow::instance()->update_all_module_options( VIP_Workflow::instance()->$module_name->module->name, $new_options ); // Redirect back to the settings page that was submitted without any previous messages diff --git a/modules/telemetry/events/settings-events.php b/modules/telemetry/events/settings-events.php index 4448c566..6214823d 100644 --- a/modules/telemetry/events/settings-events.php +++ b/modules/telemetry/events/settings-events.php @@ -24,3 +24,41 @@ function record_admin_update( string $previous_version, string $new_version, Tra 'total_posts' => $total_posts, ] ); } + +function record_settings_update( array $new_options, array $old_options, Tracker $tracker ): void { + if ( $new_options['publish_guard'] !== $old_options['publish_guard'] ) { + record_publish_guard_toggle( $new_options['publish_guard'], $tracker ); + } + + if ( $new_options['send_to_webhook'] !== $old_options['send_to_webhook'] ) { + record_send_to_webhook_toggle( $new_options['send_to_webhook'], $new_options['webhook_url'], $tracker ); + } +} + +function record_publish_guard_toggle( bool $enabled, Tracker $tracker ): void { + if ( $enabled ) { + $tracker->record_event( 'publish_guard_enabled', [ + 'enabled' => $enabled, + ] ); + return; + } + + $tracker->record_event( 'publish_guard_disabled', [ + 'enabled' => $enabled, + ] ); +} + +function record_send_to_webhook_toggle( bool $enabled, string $url, Tracker $tracker ): void { + if ( $enabled ) { + $tracker->record_event( 'send_to_webhook_enabled', [ + 'enabled' => $enabled, + 'url' => $url, + ] ); + return; + } + + $tracker->record_event( 'send_to_webhook_disabled', [ + 'enabled' => $enabled, + 'url' => $url, + ] ); +} diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index b6eda922..bf83e683 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -7,6 +7,7 @@ require_once __DIR__ . '/class-vip-workflow-tracker.php'; require_once __DIR__ . '/events/status-events.php'; require_once __DIR__ . '/events/notification-events.php'; +require_once __DIR__ . '/events/settings-events.php'; class Telemetry { /** @@ -62,7 +63,13 @@ public function init(): void { 'vw_upgrade_version', Tracker::track_event( 'VIPWorkflow\Modules\Telemetry\Events\record_admin_update', $this->tracker ), 10, - 3 + 2 + ); + add_action( + 'vw_save_settings', + Tracker::track_event( 'VIPWorkflow\Modules\Telemetry\Events\record_settings_update', $this->tracker ), + 10, + 2 ); } } From f6d51638b534ff198725eb7423eaf65a76662511 Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Mon, 26 Aug 2024 13:35:21 +0800 Subject: [PATCH 18/46] Fix phpcs errors --- modules/telemetry/events/settings-events.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/telemetry/events/settings-events.php b/modules/telemetry/events/settings-events.php index 6214823d..a26c034f 100644 --- a/modules/telemetry/events/settings-events.php +++ b/modules/telemetry/events/settings-events.php @@ -52,13 +52,13 @@ function record_send_to_webhook_toggle( bool $enabled, string $url, Tracker $tra if ( $enabled ) { $tracker->record_event( 'send_to_webhook_enabled', [ 'enabled' => $enabled, - 'url' => $url, + 'url' => $url, ] ); return; } $tracker->record_event( 'send_to_webhook_disabled', [ 'enabled' => $enabled, - 'url' => $url, + 'url' => $url, ] ); } From 8c7f9ded80565a9e764bc7041c0db0357c002fbb Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Tue, 27 Aug 2024 16:22:58 +0800 Subject: [PATCH 19/46] Change event names to fit tracks convention --- modules/telemetry/events/status-events.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/telemetry/events/status-events.php b/modules/telemetry/events/status-events.php index 665b03cf..dcb07daa 100644 --- a/modules/telemetry/events/status-events.php +++ b/modules/telemetry/events/status-events.php @@ -19,7 +19,7 @@ function record_custom_status_change( return; } - $tracker->record_event( 'post_custom_status_change', [ + $tracker->record_event( 'post_custom_status_changed', [ 'new_status' => $new_status, 'old_status' => $old_status, 'post_id' => $post->ID, @@ -31,7 +31,7 @@ function record_add_custom_status( array $args, Tracker $tracker ): void { - $tracker->record_event( 'add_custom_status', [ + $tracker->record_event( 'custom_status_created', [ 'term' => $term, 'args' => $args, ] ); @@ -43,7 +43,7 @@ function record_delete_custom_status( array $args, Tracker $tracker ): void { - $tracker->record_event( 'delete_custom_status', [ + $tracker->record_event( 'custom_status_deleted', [ 'status_id' => $status_id, 'slug' => $slug, 'args' => $args, @@ -55,7 +55,7 @@ function record_update_custom_status( array $args, Tracker $tracker ): void { - $tracker->record_event( 'update_custom_status', [ + $tracker->record_event( 'custom_status_changed', [ 'status_id' => $status_id, 'args' => $args, ] ); From 1484e37eb986f780babb89c527958022fbe395e2 Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Wed, 28 Aug 2024 16:53:57 +0800 Subject: [PATCH 20/46] Remove unneeded data in events --- modules/custom-status/custom-status.php | 3 ++- modules/telemetry/events/notification-events.php | 3 +-- modules/telemetry/events/status-events.php | 6 +++--- modules/telemetry/telemetry.php | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/custom-status/custom-status.php b/modules/custom-status/custom-status.php index 9ac046f3..ad78b648 100644 --- a/modules/custom-status/custom-status.php +++ b/modules/custom-status/custom-status.php @@ -469,9 +469,10 @@ public function add_custom_status( $term, $args = [] ) { * Fires before a custom status is added to the database. * * @param string $term The status to add or update + * @param string $slug The slug of the status * @param array|string $args Change the values of the inserted term */ - do_action( 'vw_add_custom_status', $term, $args ); + do_action( 'vw_add_custom_status', $term, $slug, $args ); $response = wp_insert_term( $term, self::TAXONOMY_KEY, [ 'slug' => $slug, diff --git a/modules/telemetry/events/notification-events.php b/modules/telemetry/events/notification-events.php index 73f015b9..4cd696e4 100644 --- a/modules/telemetry/events/notification-events.php +++ b/modules/telemetry/events/notification-events.php @@ -14,7 +14,6 @@ function record_notification_sent( ) { $tracker->record_event( 'notification_sent', [ 'subject' => $subject, - 'post' => (array) $post, - 'user' => (array) $user, + 'post_id' => $post->ID, ] ); } diff --git a/modules/telemetry/events/status-events.php b/modules/telemetry/events/status-events.php index dcb07daa..5c735e60 100644 --- a/modules/telemetry/events/status-events.php +++ b/modules/telemetry/events/status-events.php @@ -28,12 +28,13 @@ function record_custom_status_change( function record_add_custom_status( string $term, + string $slug, array $args, Tracker $tracker ): void { $tracker->record_event( 'custom_status_created', [ 'term' => $term, - 'args' => $args, + 'slug' => $slug, ] ); } @@ -46,7 +47,6 @@ function record_delete_custom_status( $tracker->record_event( 'custom_status_deleted', [ 'status_id' => $status_id, 'slug' => $slug, - 'args' => $args, ] ); } @@ -57,6 +57,6 @@ function record_update_custom_status( ): void { $tracker->record_event( 'custom_status_changed', [ 'status_id' => $status_id, - 'args' => $args, + 'slug' => $args['slug'], ] ); } diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index bf83e683..3e7936b6 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -39,7 +39,7 @@ public function init(): void { 'vw_add_custom_status', Tracker::track_event( 'VIPWorkflow\Modules\Telemetry\Events\record_add_custom_status', $this->tracker ), 10, - 2 + 3 ); add_action( 'vw_delete_custom_status', From 82596947dc3c7aa86b53b8b109219e48862fad47 Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Wed, 28 Aug 2024 16:58:24 +0800 Subject: [PATCH 21/46] Remove url from enable webhook events --- modules/telemetry/events/settings-events.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/telemetry/events/settings-events.php b/modules/telemetry/events/settings-events.php index a26c034f..af291ecf 100644 --- a/modules/telemetry/events/settings-events.php +++ b/modules/telemetry/events/settings-events.php @@ -52,13 +52,11 @@ function record_send_to_webhook_toggle( bool $enabled, string $url, Tracker $tra if ( $enabled ) { $tracker->record_event( 'send_to_webhook_enabled', [ 'enabled' => $enabled, - 'url' => $url, ] ); return; } $tracker->record_event( 'send_to_webhook_disabled', [ 'enabled' => $enabled, - 'url' => $url, ] ); } From 2c8ae470e9afc766aa0eb7d337473c5e73346b4e Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Wed, 28 Aug 2024 16:59:44 +0800 Subject: [PATCH 22/46] Remove refs to webhook url --- modules/telemetry/events/settings-events.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/telemetry/events/settings-events.php b/modules/telemetry/events/settings-events.php index af291ecf..ada80936 100644 --- a/modules/telemetry/events/settings-events.php +++ b/modules/telemetry/events/settings-events.php @@ -31,7 +31,7 @@ function record_settings_update( array $new_options, array $old_options, Tracker } if ( $new_options['send_to_webhook'] !== $old_options['send_to_webhook'] ) { - record_send_to_webhook_toggle( $new_options['send_to_webhook'], $new_options['webhook_url'], $tracker ); + record_send_to_webhook_toggle( $new_options['send_to_webhook'], $tracker ); } } @@ -48,7 +48,7 @@ function record_publish_guard_toggle( bool $enabled, Tracker $tracker ): void { ] ); } -function record_send_to_webhook_toggle( bool $enabled, string $url, Tracker $tracker ): void { +function record_send_to_webhook_toggle( bool $enabled, Tracker $tracker ): void { if ( $enabled ) { $tracker->record_event( 'send_to_webhook_enabled', [ 'enabled' => $enabled, From 5752761a3e7e03a51257369550656b7e257602db Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Wed, 28 Aug 2024 17:26:06 +0800 Subject: [PATCH 23/46] Use get supported post types method --- modules/telemetry/events/status-events.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/telemetry/events/status-events.php b/modules/telemetry/events/status-events.php index 5c735e60..488dc09b 100644 --- a/modules/telemetry/events/status-events.php +++ b/modules/telemetry/events/status-events.php @@ -3,6 +3,7 @@ namespace VIPWorkflow\Modules\Telemetry\Events; use VIPWorkflow\Modules\Telemetry\Tracker; +use VIPWorkflow\VIP_Workflow; use WP_Post; function record_custom_status_change( @@ -11,7 +12,7 @@ function record_custom_status_change( WP_Post $post, Tracker $tracker ): void { - if ( 'post' !== $post->post_type ) { + if ( ! in_array( $post->post_type, VIP_Workflow::instance()->custom_status->get_supported_post_types() ) ) { return; } From a2dc1b5fefe29f51c89f84a7a2093ea4f466d251 Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Wed, 28 Aug 2024 21:50:51 +0800 Subject: [PATCH 24/46] Loop through supported post types Iterate through supported post types as well when sending admin update event --- modules/telemetry/events/settings-events.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/modules/telemetry/events/settings-events.php b/modules/telemetry/events/settings-events.php index ada80936..2c78fafa 100644 --- a/modules/telemetry/events/settings-events.php +++ b/modules/telemetry/events/settings-events.php @@ -8,13 +8,19 @@ function record_admin_update( string $previous_version, string $new_version, Tracker $tracker ): void { // Get all custom statuses $custom_statuses = VIP_Workflow::instance()->custom_status->get_custom_statuses(); + // Get supported post types + $supported_post_types = VIP_Workflow::instance()->custom_status->get_supported_post_types(); - // Get all posts count - $posts_count = wp_count_posts(); - // Only care about published and posts with custom status - $total_posts = (int) $posts_count->publish; - foreach ( $custom_statuses as $status ) { - $total_posts += (int) $posts_count->{ $status->slug }; + $total_posts = 0; + foreach ( $supported_post_types as $post_type ) { + // Get all posts count for each post type + $posts_count = wp_count_posts( $post_type ); + + // Only care about published and posts with custom status + $total_posts += (int) $posts_count->publish; + foreach ( $custom_statuses as $status ) { + $total_posts += (int) $posts_count->{ $status->slug }; + } } $tracker->record_event( 'administration_update', [ From 4db9b2b92941572b482ad7a9aabfe86fa811e93b Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Tue, 3 Sep 2024 16:28:23 +0800 Subject: [PATCH 25/46] Refactor design to reduce complexity --- .../telemetry/class-vip-workflow-tracker.php | 54 ------- .../telemetry/events/notification-events.php | 47 ++++-- modules/telemetry/events/settings-events.php | 128 ++++++++++------ modules/telemetry/events/status-events.php | 139 ++++++++++++------ modules/telemetry/telemetry.php | 40 +++-- 5 files changed, 236 insertions(+), 172 deletions(-) delete mode 100644 modules/telemetry/class-vip-workflow-tracker.php diff --git a/modules/telemetry/class-vip-workflow-tracker.php b/modules/telemetry/class-vip-workflow-tracker.php deleted file mode 100644 index bf206b8e..00000000 --- a/modules/telemetry/class-vip-workflow-tracker.php +++ /dev/null @@ -1,54 +0,0 @@ -tracks = $tracks; - } - - /** - * Record an event - * - * @param string $event_name The event name - * @param array $event_data The event data - */ - public function record_event( string $event_name, array $event_data = [] ): void { - $this->tracks->record_event( $event_name, $event_data ); - } - - /** - * Wrap event callbacks in closure to inject the Tracker class - * - * @param callable $callback The callback function - * @param Tracker $tracker The Tracker class - * @return callable|null The wrapped callback function - */ - public static function track_event( string $callback, Tracker $tracker ): callable|null { - if ( is_callable( $callback ) ) { - return function () use ( $callback, $tracker ) { - // get the arguments passed to the callback - $args = func_get_args(); - // add the tracker to the arguments - $args[] = $tracker; - - return call_user_func_array( $callback, $args ); - }; - } - return null; - } -} diff --git a/modules/telemetry/events/notification-events.php b/modules/telemetry/events/notification-events.php index 4cd696e4..d3de66e5 100644 --- a/modules/telemetry/events/notification-events.php +++ b/modules/telemetry/events/notification-events.php @@ -2,18 +2,43 @@ namespace VIPWorkflow\Modules\Telemetry\Events; -use VIPWorkflow\Modules\Telemetry\Tracker; +use Automattic\VIP\Telemetry\Tracks; use WP_Post; use WP_User; -function record_notification_sent( - WP_Post $post, - string $subject, - WP_User $user, - Tracker $tracker -) { - $tracker->record_event( 'notification_sent', [ - 'subject' => $subject, - 'post_id' => $post->ID, - ] ); +class Notification_Events { + /** + * Tracks instance + * + * @var Tracks + */ + protected Tracks $tracks; + + /** + * Constructor + * + * @param Tracks $tracks The Tracks instance + */ + public function __construct( Tracks $tracks ) { + $this->tracks = $tracks; + } + + /** + * Record an event when a notification is sent + * + * @param WP_Post $post The post object + * @param string $subject The notification subject + * @param WP_User $user The user object + * @param Tracker $tracker The Tracker instance + */ + public function record_notification_sent( + WP_Post $post, + string $subject, + WP_User $user, + ): void { + $this->tracks->record_event( 'notification_sent', [ + 'subject' => $subject, + 'post_id' => $post->ID, + ] ); + } } diff --git a/modules/telemetry/events/settings-events.php b/modules/telemetry/events/settings-events.php index 2c78fafa..7817f8fe 100644 --- a/modules/telemetry/events/settings-events.php +++ b/modules/telemetry/events/settings-events.php @@ -2,67 +2,107 @@ namespace VIPWorkflow\Modules\Telemetry\Events; -use VIPWorkflow\Modules\Telemetry\Tracker; +use Automattic\VIP\Telemetry\Tracks; use VIPWorkflow\VIP_Workflow; -function record_admin_update( string $previous_version, string $new_version, Tracker $tracker ): void { - // Get all custom statuses - $custom_statuses = VIP_Workflow::instance()->custom_status->get_custom_statuses(); - // Get supported post types - $supported_post_types = VIP_Workflow::instance()->custom_status->get_supported_post_types(); +class Settings_Events { + /** + * Tracks instance + * + * @var Tracks + */ + protected Tracks $tracks; - $total_posts = 0; - foreach ( $supported_post_types as $post_type ) { - // Get all posts count for each post type - $posts_count = wp_count_posts( $post_type ); + /** + * Constructor + * + * @param Tracks $tracks The Tracks instance + */ + public function __construct( Tracks $tracks ) { + $this->tracks = $tracks; + } + + /** + * Record an event when the plugin is upgraded + * + * @param string $previous_version The previous version + * @param string $new_version The new version + */ + public function record_admin_update( string $previous_version, string $new_version ): void { + // Get all custom statuses + $custom_statuses = VIP_Workflow::instance()->custom_status->get_custom_statuses(); + // Get supported post types + $supported_post_types = VIP_Workflow::instance()->custom_status->get_supported_post_types(); - // Only care about published and posts with custom status - $total_posts += (int) $posts_count->publish; - foreach ( $custom_statuses as $status ) { - $total_posts += (int) $posts_count->{ $status->slug }; + $total_posts = 0; + foreach ( $supported_post_types as $post_type ) { + // Get all posts count for each post type + $posts_count = wp_count_posts( $post_type ); + + // Only care about published and posts with custom status + $total_posts += (int) $posts_count->publish; + foreach ( $custom_statuses as $status ) { + $total_posts += (int) $posts_count->{ $status->slug }; + } } + + $this->tracks->record_event( 'administration_update', [ + 'previous_version' => $previous_version, + 'new_version' => $new_version, + 'custom_statuses' => count( $custom_statuses ), + 'total_posts' => $total_posts, + ] ); } - $tracker->record_event( 'administration_update', [ - 'previous_version' => $previous_version, - 'new_version' => $new_version, - 'custom_statuses' => count( $custom_statuses ), - 'total_posts' => $total_posts, - ] ); -} + /** + * Record an event when the settings are updated + * + * @param array $new_options The new options + * @param array $old_options The old options + */ + public function record_settings_update( array $new_options, array $old_options ): void { + if ( $new_options['publish_guard'] !== $old_options['publish_guard'] ) { + $this->record_publish_guard_toggle( $new_options['publish_guard'] ); + } -function record_settings_update( array $new_options, array $old_options, Tracker $tracker ): void { - if ( $new_options['publish_guard'] !== $old_options['publish_guard'] ) { - record_publish_guard_toggle( $new_options['publish_guard'], $tracker ); + if ( $new_options['send_to_webhook'] !== $old_options['send_to_webhook'] ) { + $this->record_send_to_webhook_toggle( $new_options['send_to_webhook'] ); + } } - if ( $new_options['send_to_webhook'] !== $old_options['send_to_webhook'] ) { - record_send_to_webhook_toggle( $new_options['send_to_webhook'], $tracker ); - } -} + /** + * Record an event when the publish guard is toggled + * + * @param bool $enabled Whether the publish guard is enabled + */ + protected function record_publish_guard_toggle( bool $enabled ): void { + if ( $enabled ) { + $this->tracks->record_event( 'publish_guard_enabled', [ + 'enabled' => $enabled, + ] ); + return; + } -function record_publish_guard_toggle( bool $enabled, Tracker $tracker ): void { - if ( $enabled ) { - $tracker->record_event( 'publish_guard_enabled', [ + $this->tracks->record_event( 'publish_guard_disabled', [ 'enabled' => $enabled, ] ); - return; } - $tracker->record_event( 'publish_guard_disabled', [ - 'enabled' => $enabled, - ] ); -} + /** + * Record an event when the send to webhook is toggled + * + * @param bool $enabled Whether the send to webhook is enabled + */ + protected function record_send_to_webhook_toggle( bool $enabled ): void { + if ( $enabled ) { + $this->tracks->record_event( 'send_to_webhook_enabled', [ + 'enabled' => $enabled, + ] ); + return; + } -function record_send_to_webhook_toggle( bool $enabled, Tracker $tracker ): void { - if ( $enabled ) { - $tracker->record_event( 'send_to_webhook_enabled', [ + $this->tracks->record_event( 'send_to_webhook_disabled', [ 'enabled' => $enabled, ] ); - return; } - - $tracker->record_event( 'send_to_webhook_disabled', [ - 'enabled' => $enabled, - ] ); } diff --git a/modules/telemetry/events/status-events.php b/modules/telemetry/events/status-events.php index 488dc09b..28d78cf3 100644 --- a/modules/telemetry/events/status-events.php +++ b/modules/telemetry/events/status-events.php @@ -2,62 +2,103 @@ namespace VIPWorkflow\Modules\Telemetry\Events; -use VIPWorkflow\Modules\Telemetry\Tracker; +use Automattic\VIP\Telemetry\Tracks; use VIPWorkflow\VIP_Workflow; use WP_Post; -function record_custom_status_change( - string $new_status, - string $old_status, - WP_Post $post, - Tracker $tracker -): void { - if ( ! in_array( $post->post_type, VIP_Workflow::instance()->custom_status->get_supported_post_types() ) ) { - return; - } +class Status_Events { + /** + * Tracks instance + * + * @var Tracks + */ + protected Tracks $tracks; - if ( in_array( $new_status, [ $old_status, 'inherit', 'auto-draft', 'publish', 'draft' ] ) ) { - return; + /** + * Constructor + * + * @param Tracks $tracks The Tracks instance + */ + public function __construct( Tracks $tracks ) { + $this->tracks = $tracks; } - $tracker->record_event( 'post_custom_status_changed', [ - 'new_status' => $new_status, - 'old_status' => $old_status, - 'post_id' => $post->ID, - ] ); -} + /** + * Record an event when a post's custom status changes + * + * @param string $new_status The new status + * @param string $old_status The old status + * @param WP_Post $post The post object + */ + public function record_custom_status_change( + string $new_status, + string $old_status, + WP_Post $post, + ): void { + if ( ! in_array( $post->post_type, VIP_Workflow::instance()->custom_status->get_supported_post_types() ) ) { + return; + } -function record_add_custom_status( - string $term, - string $slug, - array $args, - Tracker $tracker -): void { - $tracker->record_event( 'custom_status_created', [ - 'term' => $term, - 'slug' => $slug, - ] ); -} + if ( in_array( $new_status, [ $old_status, 'inherit', 'auto-draft', 'publish', 'draft' ] ) ) { + return; + } -function record_delete_custom_status( - int $status_id, - string $slug, - array $args, - Tracker $tracker -): void { - $tracker->record_event( 'custom_status_deleted', [ - 'status_id' => $status_id, - 'slug' => $slug, - ] ); -} + $this->tracks->record_event( 'post_custom_status_changed', [ + 'new_status' => $new_status, + 'old_status' => $old_status, + 'post_id' => $post->ID, + ] ); + } -function record_update_custom_status( - int $status_id, - array $args, - Tracker $tracker -): void { - $tracker->record_event( 'custom_status_changed', [ - 'status_id' => $status_id, - 'slug' => $args['slug'], - ] ); + /** + * Record an event when a custom status is created + * + * @param string $term The term name + * @param string $slug The term slug + * @param array $args The term arguments + */ + public function record_add_custom_status( + string $term, + string $slug, + array $args, + ): void { + $this->tracks->record_event( 'custom_status_created', [ + 'term' => $term, + 'slug' => $slug, + ] ); + } + + /** + * Record an event when a custom status is deleted + * + * @param int $status_id The status ID + * @param string $slug The status slug + * @param array $args The status arguments + */ + function record_delete_custom_status( + int $status_id, + string $slug, + array $args, + ): void { + $this->tracks->record_event( 'custom_status_deleted', [ + 'status_id' => $status_id, + 'slug' => $slug, + ] ); + } + + /** + * Record an event when a custom status is updated + * + * @param int $status_id The status ID + * @param array $args The status arguments + */ + function record_update_custom_status( + int $status_id, + array $args, + ): void { + $this->tracks->record_event( 'custom_status_changed', [ + 'status_id' => $status_id, + 'slug' => $args['slug'], + ] ); + } } diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index 3e7936b6..6aadf20d 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -2,72 +2,84 @@ namespace VIPWorkflow\Modules\Telemetry; -use Automattic\VIP\Telemetry\Tracks; - -require_once __DIR__ . '/class-vip-workflow-tracker.php'; require_once __DIR__ . '/events/status-events.php'; require_once __DIR__ . '/events/notification-events.php'; require_once __DIR__ . '/events/settings-events.php'; +use Automattic\VIP\Telemetry\Tracks; +use VIPWorkflow\Modules\Telemetry\Events\Status_Events; +use VIPWorkflow\Modules\Telemetry\Events\Notification_Events; +use VIPWorkflow\Modules\Telemetry\Events\Settings_Events; + class Telemetry { /** * Tracker instance * - * @var Tracker + * @var Tracks */ - protected Tracker $tracker; + protected Tracks $tracks; /** * Constructor */ public function __construct() { - $telemetry = new Tracks( 'vip_workflow_' ); - $this->tracker = new Tracker( $telemetry ); + $this->tracks = new Tracks( 'vip_workflow_' ); + + // init the events classes + } /** * Initialize the module and register event callbacks */ public function init(): void { + // Add custom status events + $status_events = new Status_Events( $this->tracks ); add_action( 'transition_post_status', - Tracker::track_event( 'VIPWorkflow\Modules\Telemetry\Events\record_custom_status_change', $this->tracker ), + [ $status_events, 'record_custom_status_change' ], 10, 3 ); add_action( 'vw_add_custom_status', - Tracker::track_event( 'VIPWorkflow\Modules\Telemetry\Events\record_add_custom_status', $this->tracker ), + [ $status_events, 'record_add_custom_status' ], 10, 3 ); add_action( 'vw_delete_custom_status', - Tracker::track_event( 'VIPWorkflow\Modules\Telemetry\Events\record_delete_custom_status', $this->tracker ), + [ $status_events, 'record_delete_custom_status' ], 10, 3 ); add_action( 'vw_update_custom_status', - Tracker::track_event( 'VIPWorkflow\Modules\Telemetry\Events\record_update_custom_status', $this->tracker ), + [ $status_events, 'record_update_custom_status' ], 10, 2 ); + + // Add notification events + $notification_events = new Notification_Events( $this->tracks ); add_action( 'vw_notification_status_change', - Tracker::track_event( 'VIPWorkflow\Modules\Telemetry\Events\record_notification_sent', $this->tracker ), + [ $notification_events, 'record_notification_sent' ], 10, 3 ); + + // add settings events + $settings_events = new Settings_Events( $this->tracks ); add_action( 'vw_upgrade_version', - Tracker::track_event( 'VIPWorkflow\Modules\Telemetry\Events\record_admin_update', $this->tracker ), + [ $settings_events, 'record_admin_update' ], 10, 2 ); add_action( 'vw_save_settings', - Tracker::track_event( 'VIPWorkflow\Modules\Telemetry\Events\record_settings_update', $this->tracker ), + [ $settings_events, 'record_settings_update' ], 10, 2 ); From a14504b6609e790fe08d25637cdceefcab1667ca Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Tue, 3 Sep 2024 16:42:14 +0800 Subject: [PATCH 26/46] Fix merge errors --- modules/custom-status/custom-status.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/modules/custom-status/custom-status.php b/modules/custom-status/custom-status.php index 7f09b82f..2f71704d 100644 --- a/modules/custom-status/custom-status.php +++ b/modules/custom-status/custom-status.php @@ -601,14 +601,14 @@ public function delete_custom_status( $status_id, $args = [] ) { if ( is_wp_error( $reassigned_result ) ) { return $reassigned_result; } - - /** - * Fires before a custom status is deleted from the database. - * - * @param int $status_id The ID of the status being deleted - * @param string $old_status_slug The slug of the status being deleted - * @param array $args The arguments passed to the delete function - */ + + /** + * Fires before a custom status is deleted from the database. + * + * @param int $status_id The ID of the status being deleted + * @param string $old_status_slug The slug of the status being deleted + * @param array $args The arguments passed to the delete function + */ do_action( 'vw_delete_custom_status', $status_id, $old_status_slug, $args ); $result = wp_delete_term( $status_id, self::TAXONOMY_KEY, $args ); From df704fcf41bf3fefa3ca9d5aab0d33094b6cccce Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Tue, 3 Sep 2024 16:42:22 +0800 Subject: [PATCH 27/46] Fix phpcs errors --- modules/telemetry/events/status-events.php | 4 ++-- modules/telemetry/telemetry.php | 3 --- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/modules/telemetry/events/status-events.php b/modules/telemetry/events/status-events.php index 28d78cf3..221a249d 100644 --- a/modules/telemetry/events/status-events.php +++ b/modules/telemetry/events/status-events.php @@ -75,7 +75,7 @@ public function record_add_custom_status( * @param string $slug The status slug * @param array $args The status arguments */ - function record_delete_custom_status( + public function record_delete_custom_status( int $status_id, string $slug, array $args, @@ -92,7 +92,7 @@ function record_delete_custom_status( * @param int $status_id The status ID * @param array $args The status arguments */ - function record_update_custom_status( + public function record_update_custom_status( int $status_id, array $args, ): void { diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index 6aadf20d..b4fbc988 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -24,9 +24,6 @@ class Telemetry { */ public function __construct() { $this->tracks = new Tracks( 'vip_workflow_' ); - - // init the events classes - } /** From 3a369543e0e36cb0a155329ded3fb8eb560e2197 Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Tue, 3 Sep 2024 16:45:35 +0800 Subject: [PATCH 28/46] Remove extra spacing --- modules/custom-status/custom-status.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/custom-status/custom-status.php b/modules/custom-status/custom-status.php index 2f71704d..c935d919 100644 --- a/modules/custom-status/custom-status.php +++ b/modules/custom-status/custom-status.php @@ -602,7 +602,7 @@ public function delete_custom_status( $status_id, $args = [] ) { return $reassigned_result; } - /** + /** * Fires before a custom status is deleted from the database. * * @param int $status_id The ID of the status being deleted From a0f9415fec276b3acf490773e9dc9db2548f8d36 Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Wed, 4 Sep 2024 13:07:52 +0800 Subject: [PATCH 29/46] Move action calls into their respective classes --- .../telemetry/events/notification-events.php | 12 +++++ modules/telemetry/events/settings-events.php | 18 ++++++++ modules/telemetry/events/status-events.php | 30 +++++++++++++ modules/telemetry/telemetry.php | 45 ++----------------- 4 files changed, 63 insertions(+), 42 deletions(-) diff --git a/modules/telemetry/events/notification-events.php b/modules/telemetry/events/notification-events.php index d3de66e5..c9be4b73 100644 --- a/modules/telemetry/events/notification-events.php +++ b/modules/telemetry/events/notification-events.php @@ -23,6 +23,18 @@ public function __construct( Tracks $tracks ) { $this->tracks = $tracks; } + /** + * Register the event callbacks + */ + public function register_events(): void { + add_action( + 'vw_notification_status_change', + [ $this, 'record_notification_sent' ], + 10, + 3 + ); + } + /** * Record an event when a notification is sent * diff --git a/modules/telemetry/events/settings-events.php b/modules/telemetry/events/settings-events.php index 7817f8fe..02c7aacf 100644 --- a/modules/telemetry/events/settings-events.php +++ b/modules/telemetry/events/settings-events.php @@ -22,6 +22,24 @@ public function __construct( Tracks $tracks ) { $this->tracks = $tracks; } + /** + * Register the event callbacks + */ + public function register_events(): void { + add_action( + 'vw_upgrade_version', + [ $this, 'record_admin_update' ], + 10, + 2 + ); + add_action( + 'vw_save_settings', + [ $this, 'record_settings_update' ], + 10, + 2 + ); + } + /** * Record an event when the plugin is upgraded * diff --git a/modules/telemetry/events/status-events.php b/modules/telemetry/events/status-events.php index 221a249d..7f0e820a 100644 --- a/modules/telemetry/events/status-events.php +++ b/modules/telemetry/events/status-events.php @@ -23,6 +23,36 @@ public function __construct( Tracks $tracks ) { $this->tracks = $tracks; } + /** + * Register the event callbacks + */ + public function register_events(): void { + add_action( + 'transition_post_status', + [ $this, 'record_custom_status_change' ], + 10, + 3 + ); + add_action( + 'vw_add_custom_status', + [ $this, 'record_add_custom_status' ], + 10, + 3 + ); + add_action( + 'vw_delete_custom_status', + [ $this, 'record_delete_custom_status' ], + 10, + 3 + ); + add_action( + 'vw_update_custom_status', + [ $this, 'record_update_custom_status' ], + 10, + 2 + ); + } + /** * Record an event when a post's custom status changes * diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index b4fbc988..6d54122f 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -32,53 +32,14 @@ public function __construct() { public function init(): void { // Add custom status events $status_events = new Status_Events( $this->tracks ); - add_action( - 'transition_post_status', - [ $status_events, 'record_custom_status_change' ], - 10, - 3 - ); - add_action( - 'vw_add_custom_status', - [ $status_events, 'record_add_custom_status' ], - 10, - 3 - ); - add_action( - 'vw_delete_custom_status', - [ $status_events, 'record_delete_custom_status' ], - 10, - 3 - ); - add_action( - 'vw_update_custom_status', - [ $status_events, 'record_update_custom_status' ], - 10, - 2 - ); + $status_events->register_events(); // Add notification events $notification_events = new Notification_Events( $this->tracks ); - add_action( - 'vw_notification_status_change', - [ $notification_events, 'record_notification_sent' ], - 10, - 3 - ); + $notification_events->register_events(); // add settings events $settings_events = new Settings_Events( $this->tracks ); - add_action( - 'vw_upgrade_version', - [ $settings_events, 'record_admin_update' ], - 10, - 2 - ); - add_action( - 'vw_save_settings', - [ $settings_events, 'record_settings_update' ], - 10, - 2 - ); + $settings_events->register_events(); } } From 07fb1468b736c69b9b9c6a5ef025ee36b0ccd741 Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Thu, 5 Sep 2024 16:11:25 +0800 Subject: [PATCH 30/46] Refactor status event hooks refactor to reduce unnecessary and/or vague data being passed to the callback --- modules/custom-status/custom-status.php | 10 ++++++++-- modules/telemetry/events/status-events.php | 17 +++++++++-------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/modules/custom-status/custom-status.php b/modules/custom-status/custom-status.php index c935d919..830edc75 100644 --- a/modules/custom-status/custom-status.php +++ b/modules/custom-status/custom-status.php @@ -546,9 +546,15 @@ public function update_custom_status( $status_id, $args = [] ) { * Fires before a custom status is updated in the database. * * @param int $status_id The ID of the status being updated - * @param array $args The arguments passed to the update function + * @param string $slug The slug of the status being updated + * @param int $position The position of the status being updated */ - do_action( 'vw_update_custom_status', $status_id, $args ); + do_action( + 'vw_update_custom_status', + $status_id, + isset( $args['slug'] ) ? $args['slug'] : $old_status->slug, + isset( $args['position'] ) ? $args['position'] : $old_status->position + ); $updated_status = wp_update_term( $status_id, self::TAXONOMY_KEY, $args ); diff --git a/modules/telemetry/events/status-events.php b/modules/telemetry/events/status-events.php index 7f0e820a..73265b40 100644 --- a/modules/telemetry/events/status-events.php +++ b/modules/telemetry/events/status-events.php @@ -37,19 +37,19 @@ public function register_events(): void { 'vw_add_custom_status', [ $this, 'record_add_custom_status' ], 10, - 3 + 2 ); add_action( 'vw_delete_custom_status', [ $this, 'record_delete_custom_status' ], 10, - 3 + 2 ); add_action( 'vw_update_custom_status', [ $this, 'record_update_custom_status' ], 10, - 2 + 3 ); } @@ -90,7 +90,6 @@ public function record_custom_status_change( public function record_add_custom_status( string $term, string $slug, - array $args, ): void { $this->tracks->record_event( 'custom_status_created', [ 'term' => $term, @@ -108,7 +107,6 @@ public function record_add_custom_status( public function record_delete_custom_status( int $status_id, string $slug, - array $args, ): void { $this->tracks->record_event( 'custom_status_deleted', [ 'status_id' => $status_id, @@ -120,15 +118,18 @@ public function record_delete_custom_status( * Record an event when a custom status is updated * * @param int $status_id The status ID - * @param array $args The status arguments + * @param string $slug The status slug + * @param int $position The status position */ public function record_update_custom_status( int $status_id, - array $args, + string $slug, + int $position, ): void { $this->tracks->record_event( 'custom_status_changed', [ 'status_id' => $status_id, - 'slug' => $args['slug'], + 'slug' => $slug, + 'position' => $position, ] ); } } From c9ac0647a7a79da1316e03f9d6eb5408ffdc094e Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Thu, 5 Sep 2024 16:13:38 +0800 Subject: [PATCH 31/46] Refactor admin update event Split total posts count into published posts and posts with custom status --- modules/telemetry/events/settings-events.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/modules/telemetry/events/settings-events.php b/modules/telemetry/events/settings-events.php index 02c7aacf..59c2128c 100644 --- a/modules/telemetry/events/settings-events.php +++ b/modules/telemetry/events/settings-events.php @@ -52,15 +52,16 @@ public function record_admin_update( string $previous_version, string $new_versi // Get supported post types $supported_post_types = VIP_Workflow::instance()->custom_status->get_supported_post_types(); - $total_posts = 0; + $published_posts = 0; + $custom_status_posts = 0; foreach ( $supported_post_types as $post_type ) { // Get all posts count for each post type $posts_count = wp_count_posts( $post_type ); // Only care about published and posts with custom status - $total_posts += (int) $posts_count->publish; + $published_posts += (int) $posts_count->publish; foreach ( $custom_statuses as $status ) { - $total_posts += (int) $posts_count->{ $status->slug }; + $custom_status_posts += (int) $posts_count->{ $status->slug }; } } @@ -68,7 +69,7 @@ public function record_admin_update( string $previous_version, string $new_versi 'previous_version' => $previous_version, 'new_version' => $new_version, 'custom_statuses' => count( $custom_statuses ), - 'total_posts' => $total_posts, + 'published_posts' => $published_posts, ] ); } From 163daded7fb13b2408c548bda8c2543ffb2aa186 Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Fri, 6 Sep 2024 18:40:33 +0800 Subject: [PATCH 32/46] Fix custom status posts count --- modules/telemetry/events/settings-events.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/telemetry/events/settings-events.php b/modules/telemetry/events/settings-events.php index 59c2128c..b9da7426 100644 --- a/modules/telemetry/events/settings-events.php +++ b/modules/telemetry/events/settings-events.php @@ -66,10 +66,10 @@ public function record_admin_update( string $previous_version, string $new_versi } $this->tracks->record_event( 'administration_update', [ - 'previous_version' => $previous_version, - 'new_version' => $new_version, - 'custom_statuses' => count( $custom_statuses ), - 'published_posts' => $published_posts, + 'previous_version' => $previous_version, + 'new_version' => $new_version, + 'custom_status_posts' => $custom_status_posts, + 'published_posts' => $published_posts, ] ); } From 6a58f5ae591b571ecb8933c58ff152776a7d0f5b Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Mon, 9 Sep 2024 14:35:42 +0800 Subject: [PATCH 33/46] Remove vague $args argument for actions --- modules/custom-status/custom-status.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/modules/custom-status/custom-status.php b/modules/custom-status/custom-status.php index 830edc75..f4f9c216 100644 --- a/modules/custom-status/custom-status.php +++ b/modules/custom-status/custom-status.php @@ -466,16 +466,16 @@ public function add_custom_status( $term, $args = [] ) { $args['position'] = $default_position; } - $encoded_description = $this->get_encoded_description( $args ); - /** * Fires before a custom status is added to the database. * * @param string $term The status to add or update * @param string $slug The slug of the status - * @param array|string $args Change the values of the inserted term + * @param string $description Change the values of the inserted term */ - do_action( 'vw_add_custom_status', $term, $slug, $args ); + do_action( 'vw_add_custom_status', $term, $slug, $args['description'] ); + + $encoded_description = $this->get_encoded_description( $args ); $inserted_term = wp_insert_term( $term, self::TAXONOMY_KEY, [ 'slug' => $slug, @@ -613,9 +613,8 @@ public function delete_custom_status( $status_id, $args = [] ) { * * @param int $status_id The ID of the status being deleted * @param string $old_status_slug The slug of the status being deleted - * @param array $args The arguments passed to the delete function */ - do_action( 'vw_delete_custom_status', $status_id, $old_status_slug, $args ); + do_action( 'vw_delete_custom_status', $status_id, $old_status_slug ); $result = wp_delete_term( $status_id, self::TAXONOMY_KEY, $args ); if ( ! $result ) { From 6e7fdd28a6de876c15dacf34fa51e81ba9ebdd1b Mon Sep 17 00:00:00 2001 From: Hanif Norman Date: Mon, 9 Sep 2024 14:35:56 +0800 Subject: [PATCH 34/46] Fix documentation --- modules/telemetry/events/status-events.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/telemetry/events/status-events.php b/modules/telemetry/events/status-events.php index 73265b40..74eb6a25 100644 --- a/modules/telemetry/events/status-events.php +++ b/modules/telemetry/events/status-events.php @@ -85,7 +85,6 @@ public function record_custom_status_change( * * @param string $term The term name * @param string $slug The term slug - * @param array $args The term arguments */ public function record_add_custom_status( string $term, @@ -102,7 +101,6 @@ public function record_add_custom_status( * * @param int $status_id The status ID * @param string $slug The status slug - * @param array $args The status arguments */ public function record_delete_custom_status( int $status_id, From 1937f8347ce19ec40298841c78ac0b1ffe77cbf5 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Wed, 9 Oct 2024 12:23:01 -0600 Subject: [PATCH 35/46] Migrate telemetry module to static (single-file) module --- modules/notifications/notifications.php | 6 +- .../telemetry/events/notification-events.php | 56 ----- modules/telemetry/events/settings-events.php | 127 ----------- modules/telemetry/events/status-events.php | 133 ------------ modules/telemetry/telemetry.php | 205 ++++++++++++++++-- vip-workflow.php | 20 +- 6 files changed, 204 insertions(+), 343 deletions(-) delete mode 100644 modules/telemetry/events/notification-events.php delete mode 100644 modules/telemetry/events/settings-events.php delete mode 100644 modules/telemetry/events/status-events.php diff --git a/modules/notifications/notifications.php b/modules/notifications/notifications.php index cf34a7f4..91d91a89 100644 --- a/modules/notifications/notifications.php +++ b/modules/notifications/notifications.php @@ -158,11 +158,9 @@ public static function notification_status_change( string $new_status, string $o /** * Fires after a notification is sent * - * @param WP_Post $post The post object - * @param string $subject The subject of the email - * @param WP_User $current_user The user who is taking the action + * @param int $post_id The post ID of the post that was updated. */ - do_action( 'vw_notification_status_change', $post, $subject, $current_user ); + do_action( 'vw_notification_status_change', $post->ID ); } } diff --git a/modules/telemetry/events/notification-events.php b/modules/telemetry/events/notification-events.php deleted file mode 100644 index c9be4b73..00000000 --- a/modules/telemetry/events/notification-events.php +++ /dev/null @@ -1,56 +0,0 @@ -tracks = $tracks; - } - - /** - * Register the event callbacks - */ - public function register_events(): void { - add_action( - 'vw_notification_status_change', - [ $this, 'record_notification_sent' ], - 10, - 3 - ); - } - - /** - * Record an event when a notification is sent - * - * @param WP_Post $post The post object - * @param string $subject The notification subject - * @param WP_User $user The user object - * @param Tracker $tracker The Tracker instance - */ - public function record_notification_sent( - WP_Post $post, - string $subject, - WP_User $user, - ): void { - $this->tracks->record_event( 'notification_sent', [ - 'subject' => $subject, - 'post_id' => $post->ID, - ] ); - } -} diff --git a/modules/telemetry/events/settings-events.php b/modules/telemetry/events/settings-events.php deleted file mode 100644 index b9da7426..00000000 --- a/modules/telemetry/events/settings-events.php +++ /dev/null @@ -1,127 +0,0 @@ -tracks = $tracks; - } - - /** - * Register the event callbacks - */ - public function register_events(): void { - add_action( - 'vw_upgrade_version', - [ $this, 'record_admin_update' ], - 10, - 2 - ); - add_action( - 'vw_save_settings', - [ $this, 'record_settings_update' ], - 10, - 2 - ); - } - - /** - * Record an event when the plugin is upgraded - * - * @param string $previous_version The previous version - * @param string $new_version The new version - */ - public function record_admin_update( string $previous_version, string $new_version ): void { - // Get all custom statuses - $custom_statuses = VIP_Workflow::instance()->custom_status->get_custom_statuses(); - // Get supported post types - $supported_post_types = VIP_Workflow::instance()->custom_status->get_supported_post_types(); - - $published_posts = 0; - $custom_status_posts = 0; - foreach ( $supported_post_types as $post_type ) { - // Get all posts count for each post type - $posts_count = wp_count_posts( $post_type ); - - // Only care about published and posts with custom status - $published_posts += (int) $posts_count->publish; - foreach ( $custom_statuses as $status ) { - $custom_status_posts += (int) $posts_count->{ $status->slug }; - } - } - - $this->tracks->record_event( 'administration_update', [ - 'previous_version' => $previous_version, - 'new_version' => $new_version, - 'custom_status_posts' => $custom_status_posts, - 'published_posts' => $published_posts, - ] ); - } - - /** - * Record an event when the settings are updated - * - * @param array $new_options The new options - * @param array $old_options The old options - */ - public function record_settings_update( array $new_options, array $old_options ): void { - if ( $new_options['publish_guard'] !== $old_options['publish_guard'] ) { - $this->record_publish_guard_toggle( $new_options['publish_guard'] ); - } - - if ( $new_options['send_to_webhook'] !== $old_options['send_to_webhook'] ) { - $this->record_send_to_webhook_toggle( $new_options['send_to_webhook'] ); - } - } - - /** - * Record an event when the publish guard is toggled - * - * @param bool $enabled Whether the publish guard is enabled - */ - protected function record_publish_guard_toggle( bool $enabled ): void { - if ( $enabled ) { - $this->tracks->record_event( 'publish_guard_enabled', [ - 'enabled' => $enabled, - ] ); - return; - } - - $this->tracks->record_event( 'publish_guard_disabled', [ - 'enabled' => $enabled, - ] ); - } - - /** - * Record an event when the send to webhook is toggled - * - * @param bool $enabled Whether the send to webhook is enabled - */ - protected function record_send_to_webhook_toggle( bool $enabled ): void { - if ( $enabled ) { - $this->tracks->record_event( 'send_to_webhook_enabled', [ - 'enabled' => $enabled, - ] ); - return; - } - - $this->tracks->record_event( 'send_to_webhook_disabled', [ - 'enabled' => $enabled, - ] ); - } -} diff --git a/modules/telemetry/events/status-events.php b/modules/telemetry/events/status-events.php deleted file mode 100644 index 74eb6a25..00000000 --- a/modules/telemetry/events/status-events.php +++ /dev/null @@ -1,133 +0,0 @@ -tracks = $tracks; - } - - /** - * Register the event callbacks - */ - public function register_events(): void { - add_action( - 'transition_post_status', - [ $this, 'record_custom_status_change' ], - 10, - 3 - ); - add_action( - 'vw_add_custom_status', - [ $this, 'record_add_custom_status' ], - 10, - 2 - ); - add_action( - 'vw_delete_custom_status', - [ $this, 'record_delete_custom_status' ], - 10, - 2 - ); - add_action( - 'vw_update_custom_status', - [ $this, 'record_update_custom_status' ], - 10, - 3 - ); - } - - /** - * Record an event when a post's custom status changes - * - * @param string $new_status The new status - * @param string $old_status The old status - * @param WP_Post $post The post object - */ - public function record_custom_status_change( - string $new_status, - string $old_status, - WP_Post $post, - ): void { - if ( ! in_array( $post->post_type, VIP_Workflow::instance()->custom_status->get_supported_post_types() ) ) { - return; - } - - if ( in_array( $new_status, [ $old_status, 'inherit', 'auto-draft', 'publish', 'draft' ] ) ) { - return; - } - - $this->tracks->record_event( 'post_custom_status_changed', [ - 'new_status' => $new_status, - 'old_status' => $old_status, - 'post_id' => $post->ID, - ] ); - } - - /** - * Record an event when a custom status is created - * - * @param string $term The term name - * @param string $slug The term slug - */ - public function record_add_custom_status( - string $term, - string $slug, - ): void { - $this->tracks->record_event( 'custom_status_created', [ - 'term' => $term, - 'slug' => $slug, - ] ); - } - - /** - * Record an event when a custom status is deleted - * - * @param int $status_id The status ID - * @param string $slug The status slug - */ - public function record_delete_custom_status( - int $status_id, - string $slug, - ): void { - $this->tracks->record_event( 'custom_status_deleted', [ - 'status_id' => $status_id, - 'slug' => $slug, - ] ); - } - - /** - * Record an event when a custom status is updated - * - * @param int $status_id The status ID - * @param string $slug The status slug - * @param int $position The status position - */ - public function record_update_custom_status( - int $status_id, - string $slug, - int $position, - ): void { - $this->tracks->record_event( 'custom_status_changed', [ - 'status_id' => $status_id, - 'slug' => $slug, - 'position' => $position, - ] ); - } -} diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index 6d54122f..69017b06 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -2,14 +2,11 @@ namespace VIPWorkflow\Modules\Telemetry; -require_once __DIR__ . '/events/status-events.php'; -require_once __DIR__ . '/events/notification-events.php'; -require_once __DIR__ . '/events/settings-events.php'; - use Automattic\VIP\Telemetry\Tracks; -use VIPWorkflow\Modules\Telemetry\Events\Status_Events; -use VIPWorkflow\Modules\Telemetry\Events\Notification_Events; -use VIPWorkflow\Modules\Telemetry\Events\Settings_Events; +use VIPWorkflow\Modules\CustomStatus; +use VIPWorkflow\Modules\Shared\PHP\HelperUtilities; +use WP_Post; +use WP_User; class Telemetry { /** @@ -17,29 +14,193 @@ class Telemetry { * * @var Tracks */ - protected Tracks $tracks; + protected static Tracks $tracks; /** - * Constructor + * Initialize the module and register event callbacks */ - public function __construct() { - $this->tracks = new Tracks( 'vip_workflow_' ); + public static function init(): void { + self::$tracks = new Tracks( 'vip_workflow_', [ 'plugin_version' => VIP_WORKFLOW_VERSION ] ); + + // Custom Status events + add_action( 'transition_post_status', [ __CLASS__, 'record_custom_status_change' ], 10, 3 ); + add_action( 'vw_add_custom_status', [ __CLASS__, 'record_add_custom_status' ], 10, 2 ); + add_action( 'vw_delete_custom_status', [ __CLASS__, 'record_delete_custom_status' ], 10, 2 ); + add_action( 'vw_update_custom_status', [ __CLASS__, 'record_update_custom_status' ], 10, 3 ); + + // Notification events + add_action( 'vw_notification_status_change', [ __CLASS__, 'record_notification_sent' ], 10, 1 ); + + // Settings events + add_action( 'vw_upgrade_version', [ __CLASS__, 'record_admin_update' ], 10, 2 ); + add_action( 'vw_save_settings', [ __CLASS__, 'record_settings_update' ], 10, 2 ); } + // Custom Status events + /** - * Initialize the module and register event callbacks + * Record an event when a post's custom status changes + * + * @param string $new_status The new status + * @param string $old_status The old status + * @param WP_Post $post The post object */ - public function init(): void { - // Add custom status events - $status_events = new Status_Events( $this->tracks ); - $status_events->register_events(); + public static function record_custom_status_change( string $new_status, string $old_status, WP_Post $post ): void { + if ( ! in_array( $post->post_type, HelperUtilities::get_supported_post_types() ) ) { + return; + } - // Add notification events - $notification_events = new Notification_Events( $this->tracks ); - $notification_events->register_events(); + if ( in_array( $new_status, [ $old_status, 'inherit', 'auto-draft', 'publish', 'draft' ] ) ) { + return; + } + + self::$tracks->record_event( 'post_custom_status_changed', [ + 'new_status' => $new_status, + 'old_status' => $old_status, + 'post_id' => $post->ID, + ] ); + } + + /** + * Record an event when a custom status is created + * + * @param string $term The term name + * @param string $slug The term slug + */ + public static function record_add_custom_status( string $term, string $slug ): void { + self::$tracks->record_event( 'custom_status_created', [ + 'term' => $term, + 'slug' => $slug, + ] ); + } - // add settings events - $settings_events = new Settings_Events( $this->tracks ); - $settings_events->register_events(); + /** + * Record an event when a custom status is deleted + * + * @param int $status_id The status ID + * @param string $slug The status slug + */ + public static function record_delete_custom_status( int $status_id, string $slug ): void { + self::$tracks->record_event( 'custom_status_deleted', [ + 'status_id' => $status_id, + 'slug' => $slug, + ] ); + } + + /** + * Record an event when a custom status is updated + * + * @param int $status_id The status ID + * @param string $slug The status slug + * @param int $position The status position + */ + public static function record_update_custom_status( int $status_id, string $slug, int $position ): void { + self::$tracks->record_event( 'custom_status_changed', [ + 'status_id' => $status_id, + 'slug' => $slug, + 'position' => $position, + ] ); + } + + // Notification events + + /** + * Record an event when a notification is sent + * + * @param int $post_id The post ID that was updated. + */ + public static function record_notification_sent( int $post_id ): void { + self::$tracks->record_event( 'notification_sent', [ + 'post_id' => $post_id, + ] ); + } + + // Settings events + + /** + * Record an event when the plugin is upgraded + * + * @param string $previous_version The previous version + * @param string $new_version The new version + */ + public function record_admin_update( string $previous_version, string $new_version ): void { + // Get all custom statuses + $custom_statuses = CustomStatus::get_custom_statuses(); + // Get supported post types + $supported_post_types = HelperUtilities::get_supported_post_types(); + + $published_posts = 0; + $custom_status_posts = 0; + foreach ( $supported_post_types as $post_type ) { + // Get all posts count for each post type + $posts_count = wp_count_posts( $post_type ); + + // Only care about published and posts with custom status + $published_posts += (int) $posts_count->publish; + foreach ( $custom_statuses as $status ) { + $custom_status_posts += (int) $posts_count->{ $status->slug }; + } + } + + self::$tracks->record_event( 'administration_update', [ + 'previous_version' => $previous_version, + 'new_version' => $new_version, + 'custom_status_posts' => $custom_status_posts, + 'published_posts' => $published_posts, + ] ); + } + + /** + * Record an event when the settings are updated + * + * @param array $new_options The new options + * @param array $old_options The old options + */ + public function record_settings_update( array $new_options, array $old_options ): void { + if ( $new_options['publish_guard'] !== $old_options['publish_guard'] ) { + $this->record_publish_guard_toggle( $new_options['publish_guard'] ); + } + + if ( $new_options['send_to_webhook'] !== $old_options['send_to_webhook'] ) { + $this->record_send_to_webhook_toggle( $new_options['send_to_webhook'] ); + } + } + + /** + * Record an event when the publish guard is toggled + * + * @param bool $enabled Whether the publish guard is enabled + */ + protected function record_publish_guard_toggle( bool $enabled ): void { + if ( $enabled ) { + self::$tracks->record_event( 'publish_guard_enabled', [ + 'enabled' => $enabled, + ] ); + return; + } + + self::$tracks->record_event( 'publish_guard_disabled', [ + 'enabled' => $enabled, + ] ); + } + + /** + * Record an event when the send to webhook is toggled + * + * @param bool $enabled Whether the send to webhook is enabled + */ + protected function record_send_to_webhook_toggle( bool $enabled ): void { + if ( $enabled ) { + self::$tracks->record_event( 'send_to_webhook_enabled', [ + 'enabled' => $enabled, + ] ); + return; + } + + self::$tracks->record_event( 'send_to_webhook_disabled', [ + 'enabled' => $enabled, + ] ); } } + +Telemetry::init(); diff --git a/vip-workflow.php b/vip-workflow.php index 5be07caa..3757319b 100644 --- a/vip-workflow.php +++ b/vip-workflow.php @@ -48,6 +48,14 @@ add_action( 'admin_init', function () { $previous_version = get_option( 'vip_workflow_version' ); if ( $previous_version && version_compare( $previous_version, VIP_WORKFLOW_VERSION, '<' ) ) { + /** + * Fires when the plugin is upgraded + * + * @param string $previous_version The previous version of the plugin + * @param string $new_version The new version of the plugin + */ + do_action( 'vw_upgrade_version', $previous_version, VIP_WORKFLOW_VERSION ); + update_option( 'vip_workflow_version', VIP_WORKFLOW_VERSION ); } elseif ( ! $previous_version ) { update_option( 'vip_workflow_version', VIP_WORKFLOW_VERSION ); @@ -62,8 +70,18 @@ require_once VIP_WORKFLOW_ROOT . '/modules/shared/php/util.php'; require_once VIP_WORKFLOW_ROOT . '/modules/shared/php/core-hacks.php'; -// Modules +// Modules - Telemetry +if ( class_exists( 'Automattic\VIP\Telemetry\Tracks' ) ) { + // Telemetry is only initialized if the Telemetry library is available on VIP's platform + require_once VIP_WORKFLOW_ROOT . '/modules/telemetry/telemetry.php'; +} + +require_once VIP_WORKFLOW_ROOT . '/modules/telemetry/telemetry.php'; + +// Modules - Settings require_once VIP_WORKFLOW_ROOT . '/modules/settings/settings.php'; + +// Other modules require_once VIP_WORKFLOW_ROOT . '/modules/custom-status/custom-status.php'; require_once VIP_WORKFLOW_ROOT . '/modules/editorial-metadata/editorial-metadata.php'; require_once VIP_WORKFLOW_ROOT . '/modules/notifications/notifications.php'; From d742496b223a6d79e2f00fdcf3b1456cd757b03b Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Wed, 9 Oct 2024 14:39:13 -0600 Subject: [PATCH 36/46] Cleanup unnecessary data in telemetry events --- modules/custom-status/custom-status.php | 26 ++++----- modules/telemetry/telemetry.php | 76 +++++++++++-------------- 2 files changed, 43 insertions(+), 59 deletions(-) diff --git a/modules/custom-status/custom-status.php b/modules/custom-status/custom-status.php index 1b877b5b..b8a72f3c 100644 --- a/modules/custom-status/custom-status.php +++ b/modules/custom-status/custom-status.php @@ -534,20 +534,20 @@ public static function add_custom_status( array $args ): WP_Term|WP_Error { return $inserted_term; } + $term_id = $inserted_term['term_id']; + /** * Fires after a custom status is added to the database. * - * @param string $term The status to add or update - * @param string $slug The slug of the status - * @param string $description Change the values of the inserted term + * @param int $term_id The ID of the custom status. + * @param string $term_name The name of the custom status. + * @param string $slug The slug of the custom status. */ - do_action( 'vw_add_custom_status', $term_name, $term_to_save['slug'], $term_to_save['description'] ); + do_action( 'vw_add_custom_status', $term_id, $term_name, $term_to_save['slug'] ); // Reset our internal object cache self::$custom_statuses_cache = []; - $term_id = $inserted_term['term_id']; - $position = $args[ self::METADATA_POSITION_KEY ]; $required_metadata_ids = $args[ self::METADATA_REQ_EDITORIAL_IDS_KEY ] ?? []; $required_user_ids = $args[ self::METADATA_REQ_USER_IDS_KEY ] ?? []; @@ -656,19 +656,15 @@ public static function update_custom_status( int $status_id, array $args = [] ): return $updated_term; } + $slug = isset( $args['slug'] ) ? $args['slug'] : $old_status->slug; + /** * Fires after a custom status is updated in the database. * - * @param int $status_id The ID of the status being updated + * @param int $term_id The ID of the status being updated * @param string $slug The slug of the status being updated - * @param int $position The position of the status being updated */ - do_action( - 'vw_update_custom_status', - $status_id, - isset( $args['slug'] ) ? $args['slug'] : $old_status->slug, - isset( $args['position'] ) ? $args['position'] : $old_status->position - ); + do_action( 'vw_update_custom_status', $status_id, $slug ); $status_result = self::get_custom_status_by( 'id', $status_id ); @@ -726,7 +722,7 @@ public static function delete_custom_status( int $status_id ): bool|WP_Error { /** * Fires after a custom status is deleted from the database. * - * @param int $status_id The ID of the status being deleted + * @param int $term_id The ID of the status being deleted * @param string $old_status_slug The slug of the status being deleted */ do_action( 'vw_delete_custom_status', $status_id, $old_status_slug ); diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index 69017b06..b9442324 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -24,9 +24,9 @@ public static function init(): void { // Custom Status events add_action( 'transition_post_status', [ __CLASS__, 'record_custom_status_change' ], 10, 3 ); - add_action( 'vw_add_custom_status', [ __CLASS__, 'record_add_custom_status' ], 10, 2 ); + add_action( 'vw_add_custom_status', [ __CLASS__, 'record_add_custom_status' ], 10, 3 ); add_action( 'vw_delete_custom_status', [ __CLASS__, 'record_delete_custom_status' ], 10, 2 ); - add_action( 'vw_update_custom_status', [ __CLASS__, 'record_update_custom_status' ], 10, 3 ); + add_action( 'vw_update_custom_status', [ __CLASS__, 'record_update_custom_status' ], 10, 2 ); // Notification events add_action( 'vw_notification_status_change', [ __CLASS__, 'record_notification_sent' ], 10, 1 ); @@ -50,13 +50,13 @@ public static function record_custom_status_change( string $new_status, string $ return; } - if ( in_array( $new_status, [ $old_status, 'inherit', 'auto-draft', 'publish', 'draft' ] ) ) { + if ( in_array( $new_status, [ $old_status, 'inherit', 'auto-draft', 'publish' ] ) ) { return; } self::$tracks->record_event( 'post_custom_status_changed', [ - 'new_status' => $new_status, 'old_status' => $old_status, + 'new_status' => $new_status, 'post_id' => $post->ID, ] ); } @@ -64,41 +64,42 @@ public static function record_custom_status_change( string $new_status, string $ /** * Record an event when a custom status is created * - * @param string $term The term name - * @param string $slug The term slug + * @param int $term_id The term's ID + * @param string $term_name The term's name + * @param string $slug The term's slug */ - public static function record_add_custom_status( string $term, string $slug ): void { + + public static function record_add_custom_status( int $term_id, string $term_name, string $term_slug ): void { self::$tracks->record_event( 'custom_status_created', [ - 'term' => $term, - 'slug' => $slug, + 'term_id' => $term_id, + 'name' => $term_name, + 'slug' => $term_slug, ] ); } /** * Record an event when a custom status is deleted * - * @param int $status_id The status ID + * @param int $term_id The custom status term ID * @param string $slug The status slug */ - public static function record_delete_custom_status( int $status_id, string $slug ): void { + public static function record_delete_custom_status( int $term_id, string $slug ): void { self::$tracks->record_event( 'custom_status_deleted', [ - 'status_id' => $status_id, - 'slug' => $slug, + 'term_id' => $term_id, + 'slug' => $slug, ] ); } /** * Record an event when a custom status is updated * - * @param int $status_id The status ID + * @param int $term_id The custom status term ID * @param string $slug The status slug - * @param int $position The status position */ - public static function record_update_custom_status( int $status_id, string $slug, int $position ): void { + public static function record_update_custom_status( int $status_id, string $slug ): void { self::$tracks->record_event( 'custom_status_changed', [ 'status_id' => $status_id, 'slug' => $slug, - 'position' => $position, ] ); } @@ -124,34 +125,31 @@ public static function record_notification_sent( int $post_id ): void { * @param string $new_version The new version */ public function record_admin_update( string $previous_version, string $new_version ): void { - // Get all custom statuses - $custom_statuses = CustomStatus::get_custom_statuses(); - // Get supported post types + $custom_statuses = CustomStatus::get_custom_statuses(); $supported_post_types = HelperUtilities::get_supported_post_types(); - $published_posts = 0; $custom_status_posts = 0; + foreach ( $supported_post_types as $post_type ) { - // Get all posts count for each post type + // Get all posts count for this post type $posts_count = wp_count_posts( $post_type ); - // Only care about published and posts with custom status - $published_posts += (int) $posts_count->publish; foreach ( $custom_statuses as $status ) { - $custom_status_posts += (int) $posts_count->{ $status->slug }; + if ( isset( $posts_count->{ $status->slug } ) ) { + $custom_status_posts += (int) $posts_count->{ $status->slug }; + } } } - self::$tracks->record_event( 'administration_update', [ + self::$tracks->record_event( 'plugin_update', [ 'previous_version' => $previous_version, 'new_version' => $new_version, 'custom_status_posts' => $custom_status_posts, - 'published_posts' => $published_posts, ] ); } /** - * Record an event when the settings are updated + * Record presence of publish guard and webhook settings if toggled * * @param array $new_options The new options * @param array $old_options The old options @@ -173,15 +171,10 @@ public function record_settings_update( array $new_options, array $old_options ) */ protected function record_publish_guard_toggle( bool $enabled ): void { if ( $enabled ) { - self::$tracks->record_event( 'publish_guard_enabled', [ - 'enabled' => $enabled, - ] ); - return; + self::$tracks->record_event( 'publish_guard_enabled' ); + } else { + self::$tracks->record_event( 'publish_guard_disabled' ); } - - self::$tracks->record_event( 'publish_guard_disabled', [ - 'enabled' => $enabled, - ] ); } /** @@ -191,15 +184,10 @@ protected function record_publish_guard_toggle( bool $enabled ): void { */ protected function record_send_to_webhook_toggle( bool $enabled ): void { if ( $enabled ) { - self::$tracks->record_event( 'send_to_webhook_enabled', [ - 'enabled' => $enabled, - ] ); - return; + self::$tracks->record_event( 'send_to_webhook_enabled' ); + } else { + self::$tracks->record_event( 'send_to_webhook_disabled' ); } - - self::$tracks->record_event( 'send_to_webhook_disabled', [ - 'enabled' => $enabled, - ] ); } } From 093419e9897214e18e69c5c77a77df7566afc22e Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Wed, 9 Oct 2024 14:46:10 -0600 Subject: [PATCH 37/46] Remove double-include of Telemetry module --- modules/telemetry/telemetry.php | 1 - vip-workflow.php | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index b9442324..fa3d6446 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -6,7 +6,6 @@ use VIPWorkflow\Modules\CustomStatus; use VIPWorkflow\Modules\Shared\PHP\HelperUtilities; use WP_Post; -use WP_User; class Telemetry { /** diff --git a/vip-workflow.php b/vip-workflow.php index 3757319b..5a670352 100644 --- a/vip-workflow.php +++ b/vip-workflow.php @@ -71,13 +71,11 @@ require_once VIP_WORKFLOW_ROOT . '/modules/shared/php/core-hacks.php'; // Modules - Telemetry -if ( class_exists( 'Automattic\VIP\Telemetry\Tracks' ) ) { +if ( class_exists( '\Automattic\VIP\Telemetry\Tracks' ) ) { // Telemetry is only initialized if the Telemetry library is available on VIP's platform require_once VIP_WORKFLOW_ROOT . '/modules/telemetry/telemetry.php'; } -require_once VIP_WORKFLOW_ROOT . '/modules/telemetry/telemetry.php'; - // Modules - Settings require_once VIP_WORKFLOW_ROOT . '/modules/settings/settings.php'; From 9a810f2c48bcbd0ccb68372296df83f2a335c9b6 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Thu, 10 Oct 2024 11:31:28 -0600 Subject: [PATCH 38/46] Fix telemetry method signatures to static --- modules/telemetry/telemetry.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index fa3d6446..345d2753 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -123,7 +123,7 @@ public static function record_notification_sent( int $post_id ): void { * @param string $previous_version The previous version * @param string $new_version The new version */ - public function record_admin_update( string $previous_version, string $new_version ): void { + public static function record_admin_update( string $previous_version, string $new_version ): void { $custom_statuses = CustomStatus::get_custom_statuses(); $supported_post_types = HelperUtilities::get_supported_post_types(); @@ -153,13 +153,13 @@ public function record_admin_update( string $previous_version, string $new_versi * @param array $new_options The new options * @param array $old_options The old options */ - public function record_settings_update( array $new_options, array $old_options ): void { + public static function record_settings_update( array $new_options, array $old_options ): void { if ( $new_options['publish_guard'] !== $old_options['publish_guard'] ) { - $this->record_publish_guard_toggle( $new_options['publish_guard'] ); + self::record_publish_guard_toggle( $new_options['publish_guard'] ); } if ( $new_options['send_to_webhook'] !== $old_options['send_to_webhook'] ) { - $this->record_send_to_webhook_toggle( $new_options['send_to_webhook'] ); + self::record_send_to_webhook_toggle( $new_options['send_to_webhook'] ); } } @@ -168,7 +168,7 @@ public function record_settings_update( array $new_options, array $old_options ) * * @param bool $enabled Whether the publish guard is enabled */ - protected function record_publish_guard_toggle( bool $enabled ): void { + protected static function record_publish_guard_toggle( bool $enabled ): void { if ( $enabled ) { self::$tracks->record_event( 'publish_guard_enabled' ); } else { @@ -181,7 +181,7 @@ protected function record_publish_guard_toggle( bool $enabled ): void { * * @param bool $enabled Whether the send to webhook is enabled */ - protected function record_send_to_webhook_toggle( bool $enabled ): void { + protected static function record_send_to_webhook_toggle( bool $enabled ): void { if ( $enabled ) { self::$tracks->record_event( 'send_to_webhook_enabled' ); } else { From 9178ddd788d70cf32cfb9046969b4f53f3bc272b Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Thu, 10 Oct 2024 11:46:43 -0600 Subject: [PATCH 39/46] Fix publish guard and webhook toggle telemetry checks --- modules/telemetry/telemetry.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index 345d2753..b80ab911 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -158,18 +158,18 @@ public static function record_settings_update( array $new_options, array $old_op self::record_publish_guard_toggle( $new_options['publish_guard'] ); } - if ( $new_options['send_to_webhook'] !== $old_options['send_to_webhook'] ) { - self::record_send_to_webhook_toggle( $new_options['send_to_webhook'] ); + if ( $new_options['webhook_url'] !== $old_options['webhook_url'] ) { + self::record_send_to_webhook_toggle( $new_options['webhook_url'] ); } } /** * Record an event when the publish guard is toggled * - * @param bool $enabled Whether the publish guard is enabled + * @param string $publish_guard_value Either 'on' or 'off'. */ - protected static function record_publish_guard_toggle( bool $enabled ): void { - if ( $enabled ) { + protected static function record_publish_guard_toggle( string $publish_guard_value ): void { + if ( 'on' === $publish_guard_value ) { self::$tracks->record_event( 'publish_guard_enabled' ); } else { self::$tracks->record_event( 'publish_guard_disabled' ); @@ -179,13 +179,13 @@ protected static function record_publish_guard_toggle( bool $enabled ): void { /** * Record an event when the send to webhook is toggled * - * @param bool $enabled Whether the send to webhook is enabled + * @param string $webhook_url A string indicating a webhook URL, or an empty string if none is set. */ - protected static function record_send_to_webhook_toggle( bool $enabled ): void { - if ( $enabled ) { - self::$tracks->record_event( 'send_to_webhook_enabled' ); - } else { + protected static function record_send_to_webhook_toggle( string $webhook_url ): void { + if ( '' === $webhook_url ) { self::$tracks->record_event( 'send_to_webhook_disabled' ); + } else { + self::$tracks->record_event( 'send_to_webhook_enabled' ); } } } From 951e25435774e8829e7b82ed5a03908896d269b8 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Thu, 10 Oct 2024 12:04:24 -0600 Subject: [PATCH 40/46] Change custom_status_changed event to not fire for reorders --- modules/custom-status/custom-status.php | 12 +++++------- modules/telemetry/telemetry.php | 18 +++++++++++++----- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/modules/custom-status/custom-status.php b/modules/custom-status/custom-status.php index b8a72f3c..62dafcd2 100644 --- a/modules/custom-status/custom-status.php +++ b/modules/custom-status/custom-status.php @@ -656,19 +656,17 @@ public static function update_custom_status( int $status_id, array $args = [] ): return $updated_term; } - $slug = isset( $args['slug'] ) ? $args['slug'] : $old_status->slug; + $updated_status = self::get_custom_status_by( 'id', $status_id ); /** * Fires after a custom status is updated in the database. * - * @param int $term_id The ID of the status being updated - * @param string $slug The slug of the status being updated + * @param WP_Term $updated_status The updated status WP_Term object. + * @param array $update_args The arguments used to update the status. */ - do_action( 'vw_update_custom_status', $status_id, $slug ); + do_action( 'vw_update_custom_status', $updated_status, $args ); - $status_result = self::get_custom_status_by( 'id', $status_id ); - - return $status_result; + return $updated_status; } /** diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index b80ab911..b86fc165 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -6,6 +6,7 @@ use VIPWorkflow\Modules\CustomStatus; use VIPWorkflow\Modules\Shared\PHP\HelperUtilities; use WP_Post; +use WP_Term; class Telemetry { /** @@ -92,13 +93,20 @@ public static function record_delete_custom_status( int $term_id, string $slug ) /** * Record an event when a custom status is updated * - * @param int $term_id The custom status term ID - * @param string $slug The status slug + * @param WP_Term $updated_status The updated status WP_Term object. + * @param array $update_args The arguments used to update the status. */ - public static function record_update_custom_status( int $status_id, string $slug ): void { + public static function record_update_custom_status( WP_Term $updated_status, array $update_args ): void { + $is_position_update = 1 === count( $update_args ) && isset( $update_args['position'] ); + if ( $is_position_update ) { + // Ignore position changes, as they fire for every custom status when statuses are reordered + return; + } + self::$tracks->record_event( 'custom_status_changed', [ - 'status_id' => $status_id, - 'slug' => $slug, + 'term_id' => $updated_status->term_id, + 'name' => $updated_status->name, + 'slug' => $updated_status->slug, ] ); } From fbfac949498f1531aca6b627ee2d8e7a309117ea Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Thu, 10 Oct 2024 12:19:38 -0600 Subject: [PATCH 41/46] Change post_custom_status_changed event to record when a post moves from a custom status to 'publish' --- modules/telemetry/telemetry.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index b86fc165..0dec79e8 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -47,10 +47,20 @@ public static function init(): void { */ public static function record_custom_status_change( string $new_status, string $old_status, WP_Post $post ): void { if ( ! in_array( $post->post_type, HelperUtilities::get_supported_post_types() ) ) { + // This isn't a supported post type + return; + } elseif ( $old_status === $new_status ) { + // The status hasn't changed + return; + } elseif ( in_array( $new_status, [ 'inherit', 'auto-draft' ] ) ) { + // The status hasn't changed, or it moved into an auto-generated status return; } - if ( in_array( $new_status, [ $old_status, 'inherit', 'auto-draft', 'publish' ] ) ) { + $status_slugs = wp_list_pluck( CustomStatus::get_custom_statuses(), 'slug' ); + + if ( ! in_array( $new_status, $status_slugs, true ) && ! in_array( $old_status, $status_slugs, true ) ) { + // The status isn't moving to or from a custom status return; } From f74e7981459ed5f6e7564c3a55265362e5c9a544 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Thu, 10 Oct 2024 12:42:09 -0600 Subject: [PATCH 42/46] Update the notification_sent event to only fire when an email or webhook notification was sent, and track which --- modules/notifications/notifications.php | 68 ++++++++++++++----------- modules/telemetry/telemetry.php | 10 ++-- 2 files changed, 46 insertions(+), 32 deletions(-) diff --git a/modules/notifications/notifications.php b/modules/notifications/notifications.php index 91d91a89..1307e465 100644 --- a/modules/notifications/notifications.php +++ b/modules/notifications/notifications.php @@ -147,20 +147,36 @@ public static function notification_status_change( string $new_status, string $o $action = 'status-change'; - self::schedule_emails( $action, $post, $subject, $body ); + $notification_email = OptionsUtilities::get_options_by_key( 'email_address' ); + $is_email_scheduled = '' !== $notification_email; - /* translators: 1: user name, 2: post type, 3: post id, 4: edit link, 5: post title, 6: old status, 7: new status */ - $webhook_format = __( '*%1$s* changed the status of *%2$s #%3$s - <%4$s|%5$s>* from *%6$s* to *%7$s*', 'vip-workflow' ); - $webhook_message = sprintf( $webhook_format, $current_user_display_name, $post_type, $post_id, $edit_link, $post_title, $old_status, $new_status ); + if ( $is_email_scheduled ) { + $recipients = [ $notification_email ]; + self::schedule_emails( $recipients, $action, $post, $subject, $body ); + } - self::schedule_webhook_notification( $webhook_message, $action, $post->post_modified_gmt ); + $webhook_url = OptionsUtilities::get_options_by_key( 'webhook_url' ); + $is_webhook_scheduled = '' !== $webhook_url; - /** - * Fires after a notification is sent - * - * @param int $post_id The post ID of the post that was updated. - */ - do_action( 'vw_notification_status_change', $post->ID ); + if ( $is_webhook_scheduled ) { + /* translators: 1: user name, 2: post type, 3: post id, 4: edit link, 5: post title, 6: old status, 7: new status */ + $webhook_format = __( '*%1$s* changed the status of *%2$s #%3$s - <%4$s|%5$s>* from *%6$s* to *%7$s*', 'vip-workflow' ); + $webhook_message = sprintf( $webhook_format, $current_user_display_name, $post_type, $post_id, $edit_link, $post_title, $old_status, $new_status ); + + self::schedule_webhook_notification( $webhook_message, $action, $post->post_modified_gmt ); + } + + // Fire the notification status change action if any notifications were scheduled + if ( $is_email_scheduled || $is_webhook_scheduled ) { + /** + * Fires after a notification is sent + * + * @param int $post_id The post ID of the post that was updated. + * @param bool $is_email_scheduled True if an email was scheduled as part of the notification, false otherwise. + * @param bool $is_webhook_scheduled True if a webhook was scheduled as part of the notification, false otherwise. + */ + do_action( 'vw_notification_status_change', $post->ID, $is_email_scheduled, $is_webhook_scheduled ); + } } } @@ -181,27 +197,21 @@ public static function get_notification_footer(): string { /** * Send email notifications * + * @param array $recipients An array of string email addresses to send to * @param string $action (status-change) * @param string $subject Subject of the email * @param string $message Body of the email * @param string $message_headers. (optional) Message headers */ - public static function schedule_emails( string $action, WP_Post $post, string $subject, string $message, string $message_headers = '' ): void { - // Ensure the email address is set from settings. - if ( empty( OptionsUtilities::get_options_by_key( 'email_address' ) ) ) { - return; - } - - $email_recipients = [ OptionsUtilities::get_options_by_key( 'email_address' ) ]; - + public static function schedule_emails( array $recipients, string $action, WP_Post $post, string $subject, string $message, string $message_headers = '' ): void { /** * Filter the email recipients * - * @param array $email_recipients Array of email recipients + * @param array $recipients Array of email recipients * @param string $action Action being taken, eg. status-change * @param WP_Post $post Post object */ - $email_recipients = apply_filters( 'vw_notification_email_recipients', $email_recipients, $action, $post ); + $recipients = apply_filters( 'vw_notification_email_recipients', $recipients, $action, $post ); /** * Filter the email subject @@ -230,8 +240,8 @@ public static function schedule_emails( string $action, WP_Post $post, string $s */ $message_headers = apply_filters( 'vw_notification_email_headers', $message_headers, $action, $post ); - if ( ! empty( $email_recipients ) ) { - wp_schedule_single_event( time(), 'vw_send_scheduled_emails', [ $email_recipients, $subject, $message, $message_headers ] ); + if ( ! empty( $recipients ) ) { + wp_schedule_single_event( time(), 'vw_send_scheduled_emails', [ $recipients, $subject, $message, $message_headers ] ); } } @@ -255,16 +265,11 @@ public static function send_emails( array $recipients, string $subject, string $ /** * Schedule a webhook notification * - * @param string $webhook_message Message to be sent to webhook + * @param string $webhook_url URL to be send the webhook to * @param string $action Action being taken, eg. status-change * @param string $timestamp Timestamp of the message, eg. the time at which the post was updated */ public static function schedule_webhook_notification( string $webhook_message, string $action, string $timestamp ): void { - // Ensure the webhook URL is set from settings. - if ( empty( OptionsUtilities::get_options_by_key( 'webhook_url' ) ) ) { - return; - } - $message_type = 'plugin:vip-workflow:' . $action; wp_schedule_single_event( time(), 'vw_send_scheduled_webhook', [ $webhook_message, $message_type, $timestamp ] ); @@ -281,6 +286,11 @@ public static function schedule_webhook_notification( string $webhook_message, s public static function send_to_webhook( string $message, string $message_type, string $timestamp ): bool { $webhook_url = OptionsUtilities::get_options_by_key( 'webhook_url' ); + if ( empty( $webhook_url ) ) { + // This can happen if the webhook URL was cleared after scheduling this notification + return false; + } + // Set up the payload $payload = [ 'type' => $message_type, diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index 0dec79e8..346be027 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -29,7 +29,7 @@ public static function init(): void { add_action( 'vw_update_custom_status', [ __CLASS__, 'record_update_custom_status' ], 10, 2 ); // Notification events - add_action( 'vw_notification_status_change', [ __CLASS__, 'record_notification_sent' ], 10, 1 ); + add_action( 'vw_notification_status_change', [ __CLASS__, 'record_notification_sent' ], 10, 3 ); // Settings events add_action( 'vw_upgrade_version', [ __CLASS__, 'record_admin_update' ], 10, 2 ); @@ -125,11 +125,15 @@ public static function record_update_custom_status( WP_Term $updated_status, arr /** * Record an event when a notification is sent * - * @param int $post_id The post ID that was updated. + * @param int $post_id The post ID of the post that was updated. + * @param bool $is_email_scheduled True if an email was scheduled as part of the notification, false otherwise. + * @param bool $is_webhook_scheduled True if a webhook was scheduled as part of the notification, false otherwise. */ - public static function record_notification_sent( int $post_id ): void { + public static function record_notification_sent( int $post_id, $is_email_scheduled, $is_webhook_scheduled ): void { self::$tracks->record_event( 'notification_sent', [ 'post_id' => $post_id, + 'email' => $is_email_scheduled, + 'webhook' => $is_webhook_scheduled, ] ); } From e09f6aaaaed9f99c34aa658a77a732b612a4fa59 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Thu, 10 Oct 2024 12:48:13 -0600 Subject: [PATCH 43/46] Record when email notification settings are changed --- modules/telemetry/telemetry.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index 346be027..9f2df7cc 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -183,6 +183,10 @@ public static function record_settings_update( array $new_options, array $old_op if ( $new_options['webhook_url'] !== $old_options['webhook_url'] ) { self::record_send_to_webhook_toggle( $new_options['webhook_url'] ); } + + if ( $new_options['email_address'] !== $old_options['email_address'] ) { + self::record_send_to_email_toggle( $new_options['email_address'] ); + } } /** @@ -199,7 +203,7 @@ protected static function record_publish_guard_toggle( string $publish_guard_val } /** - * Record an event when the send to webhook is toggled + * Record an event when send to webhook is toggled * * @param string $webhook_url A string indicating a webhook URL, or an empty string if none is set. */ @@ -210,6 +214,19 @@ protected static function record_send_to_webhook_toggle( string $webhook_url ): self::$tracks->record_event( 'send_to_webhook_enabled' ); } } + + /** + * Record an event when send to email is changed + * + * @param string $email_address A string indicating a webhook URL, or an empty string if none is set. + */ + protected static function record_send_to_email_toggle( string $email_address ): void { + if ( '' === $email_address ) { + self::$tracks->record_event( 'send_to_email_disabled' ); + } else { + self::$tracks->record_event( 'send_to_email_enabled' ); + } + } } Telemetry::init(); From c764c4e2cb2432d91d3f762e299db824a0cf57f6 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Fri, 11 Oct 2024 12:05:02 -0600 Subject: [PATCH 44/46] Add published posts count to update telemetry --- modules/telemetry/telemetry.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index 9f2df7cc..6ee236f0 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -149,15 +149,18 @@ public static function record_admin_update( string $previous_version, string $ne $custom_statuses = CustomStatus::get_custom_statuses(); $supported_post_types = HelperUtilities::get_supported_post_types(); - $custom_status_posts = 0; + $published_post_count = 0; + $custom_status_post_count = 0; foreach ( $supported_post_types as $post_type ) { // Get all posts count for this post type $posts_count = wp_count_posts( $post_type ); + $published_post_count += (int) $posts_count->publish; + foreach ( $custom_statuses as $status ) { if ( isset( $posts_count->{ $status->slug } ) ) { - $custom_status_posts += (int) $posts_count->{ $status->slug }; + $custom_status_post_count += (int) $posts_count->{ $status->slug }; } } } @@ -165,7 +168,8 @@ public static function record_admin_update( string $previous_version, string $ne self::$tracks->record_event( 'plugin_update', [ 'previous_version' => $previous_version, 'new_version' => $new_version, - 'custom_status_posts' => $custom_status_posts, + 'published_posts' => $published_post_count, + 'custom_status_posts' => $custom_status_post_count, ] ); } From 8c42cb3d0ed78ee3590f0b3595431082614603b6 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Fri, 11 Oct 2024 12:49:13 -0600 Subject: [PATCH 45/46] Send create/update telemetry events after object has been verified. Count required fields in events --- modules/custom-status/custom-status.php | 37 +++++++++++++------------ modules/telemetry/telemetry.php | 34 +++++++++++++++-------- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/modules/custom-status/custom-status.php b/modules/custom-status/custom-status.php index 40de7ddf..b3201382 100644 --- a/modules/custom-status/custom-status.php +++ b/modules/custom-status/custom-status.php @@ -549,15 +549,6 @@ public static function add_custom_status( array $args ): WP_Term|WP_Error { $term_id = $inserted_term['term_id']; - /** - * Fires after a custom status is added to the database. - * - * @param int $term_id The ID of the custom status. - * @param string $term_name The name of the custom status. - * @param string $slug The slug of the custom status. - */ - do_action( 'vw_add_custom_status', $term_id, $term_name, $term_to_save['slug'] ); - // Reset our internal object cache self::$custom_statuses_cache = []; @@ -584,6 +575,15 @@ public static function add_custom_status( array $args ): WP_Term|WP_Error { $term_result = self::get_custom_status_by( 'id', $term_id ); + if ( false !== $term_result ) { + /** + * Fires after a custom status is added to the database. + * + * @param WP_Term $term The custom status term object. + */ + do_action( 'vw_add_custom_status', $term_result ); + } + return $term_result; } @@ -670,13 +670,15 @@ public static function update_custom_status( int $status_id, array $args = [] ): $updated_status = self::get_custom_status_by( 'id', $status_id ); - /** - * Fires after a custom status is updated in the database. - * - * @param WP_Term $updated_status The updated status WP_Term object. - * @param array $update_args The arguments used to update the status. - */ - do_action( 'vw_update_custom_status', $updated_status, $args ); + if ( $updated_status ) { + /** + * Fires after a custom status is updated in the database. + * + * @param WP_Term $updated_status The updated status WP_Term object. + * @param array $update_args The arguments used to update the status. + */ + do_action( 'vw_update_custom_status', $updated_status, $args ); + } return $updated_status; } @@ -733,9 +735,10 @@ public static function delete_custom_status( int $status_id ): bool|WP_Error { * Fires after a custom status is deleted from the database. * * @param int $term_id The ID of the status being deleted + * @param string $term_name The name of the status being deleted * @param string $old_status_slug The slug of the status being deleted */ - do_action( 'vw_delete_custom_status', $status_id, $old_status_slug ); + do_action( 'vw_delete_custom_status', $status_id, $old_status->name, $old_status_slug ); // Reset status cache again, as reassign_post_status() will recache prior statuses self::$custom_statuses_cache = []; diff --git a/modules/telemetry/telemetry.php b/modules/telemetry/telemetry.php index 6ee236f0..c2b21f45 100644 --- a/modules/telemetry/telemetry.php +++ b/modules/telemetry/telemetry.php @@ -25,7 +25,7 @@ public static function init(): void { // Custom Status events add_action( 'transition_post_status', [ __CLASS__, 'record_custom_status_change' ], 10, 3 ); add_action( 'vw_add_custom_status', [ __CLASS__, 'record_add_custom_status' ], 10, 3 ); - add_action( 'vw_delete_custom_status', [ __CLASS__, 'record_delete_custom_status' ], 10, 2 ); + add_action( 'vw_delete_custom_status', [ __CLASS__, 'record_delete_custom_status' ], 10, 3 ); add_action( 'vw_update_custom_status', [ __CLASS__, 'record_update_custom_status' ], 10, 2 ); // Notification events @@ -74,16 +74,19 @@ public static function record_custom_status_change( string $new_status, string $ /** * Record an event when a custom status is created * - * @param int $term_id The term's ID - * @param string $term_name The term's name - * @param string $slug The term's slug + * @param WP_Term $term The custom status term object */ - public static function record_add_custom_status( int $term_id, string $term_name, string $term_slug ): void { + public static function record_add_custom_status( WP_Term $term ): void { + $required_user_count = count( $updated_status->meta[ CustomStatus::METADATA_REQ_USER_IDS_KEY ] ?? [] ); + $required_editorial_metadata_count = count( $updated_status->meta[ CustomStatus::METADATA_REQ_EDITORIAL_IDS_KEY ] ?? [] ); + self::$tracks->record_event( 'custom_status_created', [ - 'term_id' => $term_id, - 'name' => $term_name, - 'slug' => $term_slug, + 'term_id' => $term->term_id, + 'name' => $term->name, + 'slug' => $term->slug, + 'required_users' => $required_user_count, + 'required_em' => $required_editorial_metadata_count, ] ); } @@ -91,11 +94,13 @@ public static function record_add_custom_status( int $term_id, string $term_name * Record an event when a custom status is deleted * * @param int $term_id The custom status term ID + * @param string $term_name The custom status term name * @param string $slug The status slug */ - public static function record_delete_custom_status( int $term_id, string $slug ): void { + public static function record_delete_custom_status( int $term_id, string $term_name, string $slug ): void { self::$tracks->record_event( 'custom_status_deleted', [ 'term_id' => $term_id, + 'name' => $term_name, 'slug' => $slug, ] ); } @@ -113,10 +118,15 @@ public static function record_update_custom_status( WP_Term $updated_status, arr return; } + $required_user_count = count( $updated_status->meta[ CustomStatus::METADATA_REQ_USER_IDS_KEY ] ?? [] ); + $required_editorial_metadata_count = count( $updated_status->meta[ CustomStatus::METADATA_REQ_EDITORIAL_IDS_KEY ] ?? [] ); + self::$tracks->record_event( 'custom_status_changed', [ - 'term_id' => $updated_status->term_id, - 'name' => $updated_status->name, - 'slug' => $updated_status->slug, + 'term_id' => $updated_status->term_id, + 'name' => $updated_status->name, + 'slug' => $updated_status->slug, + 'required_users' => $required_user_count, + 'required_em' => $required_editorial_metadata_count, ] ); } From b67ae56837f3e6579a24925549319659f3248208 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Fri, 11 Oct 2024 13:09:43 -0600 Subject: [PATCH 46/46] Replace `empty()` checks with explicit types --- modules/notifications/notifications.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/notifications/notifications.php b/modules/notifications/notifications.php index 1307e465..f6a3bc46 100644 --- a/modules/notifications/notifications.php +++ b/modules/notifications/notifications.php @@ -240,7 +240,7 @@ public static function schedule_emails( array $recipients, string $action, WP_Po */ $message_headers = apply_filters( 'vw_notification_email_headers', $message_headers, $action, $post ); - if ( ! empty( $recipients ) ) { + if ( [] !== $recipients ) { wp_schedule_single_event( time(), 'vw_send_scheduled_emails', [ $recipients, $subject, $message, $message_headers ] ); } } @@ -286,7 +286,7 @@ public static function schedule_webhook_notification( string $webhook_message, s public static function send_to_webhook( string $message, string $message_type, string $timestamp ): bool { $webhook_url = OptionsUtilities::get_options_by_key( 'webhook_url' ); - if ( empty( $webhook_url ) ) { + if ( ! is_string( $webhook_url ) || strlen( $webhook_url ) === 0 ) { // This can happen if the webhook URL was cleared after scheduling this notification return false; }