Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tracks integration #21

Merged
merged 51 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
13b4378
Add tracks classes
hanifn Aug 21, 2024
453ad23
Add events
hanifn Aug 21, 2024
6796d68
Fix telemetry namespace
hanifn Aug 21, 2024
594ca5d
Load telemetry module on init
hanifn Aug 21, 2024
04d5d96
Fix telemetry namespace reference
hanifn Aug 21, 2024
ba30eed
Fix namespace reference
hanifn Aug 21, 2024
3ca463c
Require the tracker class
hanifn Aug 21, 2024
251c8e8
Require events files
hanifn Aug 22, 2024
84b0bf8
Add custom status added event
hanifn Aug 22, 2024
cf715c2
Add custom status delete event
hanifn Aug 22, 2024
b7a2349
Add custom status change event
hanifn Aug 22, 2024
b593e45
Add hooks into the new actions
hanifn Aug 22, 2024
4622304
Add notification events
hanifn Aug 22, 2024
77b64a0
Fix phpcs errors
hanifn Aug 22, 2024
aee4397
Add admin update event
hanifn Aug 22, 2024
042f399
Merge branch 'trunk' into add/tracks
hanifn Aug 26, 2024
f97a6f2
Fix merge error
hanifn Aug 26, 2024
cf6b2de
Add settings events
hanifn Aug 26, 2024
f6d5163
Fix phpcs errors
hanifn Aug 26, 2024
8c7f9de
Change event names to fit tracks convention
hanifn Aug 27, 2024
1484e37
Remove unneeded data in events
hanifn Aug 28, 2024
8259694
Remove url from enable webhook events
hanifn Aug 28, 2024
2c8ae47
Remove refs to webhook url
hanifn Aug 28, 2024
5752761
Use get supported post types method
hanifn Aug 28, 2024
a2dc1b5
Loop through supported post types
hanifn Aug 28, 2024
4db9b2b
Refactor design to reduce complexity
hanifn Sep 3, 2024
bd52c78
Merge branch 'trunk' into add/tracks
hanifn Sep 3, 2024
a14504b
Fix merge errors
hanifn Sep 3, 2024
df704fc
Fix phpcs errors
hanifn Sep 3, 2024
3a36954
Remove extra spacing
hanifn Sep 3, 2024
a0f9415
Move action calls into their respective classes
hanifn Sep 4, 2024
07fb146
Refactor status event hooks
hanifn Sep 5, 2024
c9ac064
Refactor admin update event
hanifn Sep 5, 2024
163dade
Fix custom status posts count
hanifn Sep 6, 2024
6a58f5a
Remove vague $args argument for actions
hanifn Sep 9, 2024
6e7fdd2
Fix documentation
hanifn Sep 9, 2024
5a5c762
Merge branch 'trunk' into add/tracks
alecgeatches Oct 9, 2024
1937f83
Migrate telemetry module to static (single-file) module
alecgeatches Oct 9, 2024
d742496
Cleanup unnecessary data in telemetry events
alecgeatches Oct 9, 2024
093419e
Remove double-include of Telemetry module
alecgeatches Oct 9, 2024
9a810f2
Fix telemetry method signatures to static
alecgeatches Oct 10, 2024
9178ddd
Fix publish guard and webhook toggle telemetry checks
alecgeatches Oct 10, 2024
951e254
Change custom_status_changed event to not fire for reorders
alecgeatches Oct 10, 2024
fbfac94
Change post_custom_status_changed event to record when a post moves f…
alecgeatches Oct 10, 2024
f74e798
Update the notification_sent event to only fire when an email or webh…
alecgeatches Oct 10, 2024
e09f6aa
Record when email notification settings are changed
alecgeatches Oct 10, 2024
c764c4e
Add published posts count to update telemetry
alecgeatches Oct 11, 2024
d3d6aa9
Merge branch 'trunk' into add/tracks
alecgeatches Oct 11, 2024
8c42cb3
Send create/update telemetry events after object has been verified. C…
alecgeatches Oct 11, 2024
b67ae56
Replace `empty()` checks with explicit types
alecgeatches Oct 11, 2024
a22064a
Merge branch 'trunk' into add/tracks
alecgeatches Oct 14, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 56 additions & 31 deletions modules/custom-status/custom-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ class CustomStatus {
const SETTINGS_SLUG = 'vw-custom-status';

// The metadata keys for the custom status term
const METADATA_POSITION_KEY = 'position';
const METADATA_POSITION_KEY = 'position';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spacing is interesting as I would have thought running composer run cbf should catch that. Anything that you think would help with this? Prettier maybe on php files as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great question! I've been curious about this but hadn't tracked it down yet. After a quick investigation, it appears to be because this is a PHPCS warning, and we ignore those in our phpcs/phpcbf runs:

"cs": "@php ./vendor/bin/phpcs -p -s -v -n . --standard=\"phpcs.xml.dist\" --extensions=php --ignore=\"/vendor/*,/node_modules/*\"",
"cbf": "@php ./vendor/bin/phpcbf -p -s -v -n . --standard=\"phpcs.xml.dist\" --extensions=php --ignore=\"/vendor/*,/node_modules/*\"",

From running ./vendor/bin/phpcs --help, we can see the -n parameter is doing this:

  -n        Do not include warnings. Shortcut for "--warning-severity=0".

What's happening here is my local PHPCS extensions is auto-fixing this warning, but it's being ignored by our tests, even though it's a warning in our rules. If you're curious, here's the result of running phpcs without -n:

./vendor/bin/phpcs -p -s -v --standard="phpcs.xml.dist" --extensions=php --ignore="/vendor/*,/node_modules/*"

# ...

FILE: modules/shared/php/core-hacks.php
----------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
----------------------------------------------------------------------------------------------------------------------------------
 85 | WARNING | The method parameter $post_parent is never used (Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed)
----------------------------------------------------------------------------------------------------------------------------------


FILE: modules/custom-status/rest/custom-status-endpoint.php
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 25 WARNINGS AFFECTING 25 LINES
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  35 | WARNING | [x] Array double arrow not aligned correctly; expected 18 space(s) between "'name'" and double arrow, but found 14. (WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)
  46 | WARNING | [x] Array double arrow not aligned correctly; expected 11 space(s) between "'description'" and double arrow, but found 7. (WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)
  62 | WARNING | [x] Array double arrow not aligned correctly; expected 5 space(s) between "'required_user_ids'" and double arrow, but found 1.
     |         |     (WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)
  81 | WARNING | [x] Array double arrow not aligned correctly; expected 18 space(s) between "'name'" and double arrow, but found 14. (WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)
  90 | WARNING | [x] Array double arrow not aligned correctly; expected 20 space(s) between "'id'" and double arrow, but found 16. (WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)
 103 | WARNING | [x] Array double arrow not aligned correctly; expected 11 space(s) between "'description'" and double arrow, but found 7. (WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)
 119 | WARNING | [x] Array double arrow not aligned correctly; expected 5 space(s) between "'required_user_ids'" and double arrow, but found 1.
     |         |     (WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)
 201 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 18 spaces but found 15 spaces (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
 202 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 18 spaces but found 15 spaces (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
 203 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 11 spaces but found 8 spaces (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
 205 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
 234 | WARNING | [x] Array double arrow not aligned correctly; expected 18 space(s) between "'name'" and double arrow, but found 15. (WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)
 235 | WARNING | [x] Array double arrow not aligned correctly; expected 11 space(s) between "'description'" and double arrow, but found 8. (WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)
 236 | WARNING | [x] Array double arrow not aligned correctly; expected 18 space(s) between "'slug'" and double arrow, but found 15. (WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)
 238 | WARNING | [x] Array double arrow not aligned correctly; expected 5 space(s) between "'required_user_ids'" and double arrow, but found 1.
     |         |     (WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)
 253 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 22 spaces but found 19 spaces (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
 254 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 18 spaces but found 15 spaces (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
 255 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 18 spaces but found 15 spaces (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
 256 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 11 spaces but found 8 spaces (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
 258 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
 275 | WARNING | [x] Equals sign not aligned correctly; expected 1 space but found 3 spaces (Generic.Formatting.MultipleStatementAlignment.IncorrectWarning)
 300 | WARNING | [x] Array double arrow not aligned correctly; expected 18 space(s) between "'name'" and double arrow, but found 15. (WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)
 301 | WARNING | [x] Array double arrow not aligned correctly; expected 11 space(s) between "'description'" and double arrow, but found 8. (WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)
 302 | WARNING | [x] Array double arrow not aligned correctly; expected 18 space(s) between "'slug'" and double arrow, but found 15. (WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)
 304 | WARNING | [x] Array double arrow not aligned correctly; expected 5 space(s) between "'required_user_ids'" and double arrow, but found 1.
     |         |     (WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 25 MARKED SNIFF VIOLATIONS AUTOMATICALLY
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


FILE: modules/shared/php/util.php
--------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 6 WARNINGS AFFECTING 3 LINES
--------------------------------------------------------------------------------------------------------------------------------------------------------
 60 | WARNING | Processing form data without nonce verification. (WordPress.Security.NonceVerification.Recommended)
 60 | WARNING | Processing form data without nonce verification. (WordPress.Security.NonceVerification.Recommended)
 61 | WARNING | Processing form data without nonce verification. (WordPress.Security.NonceVerification.Recommended)
 61 | WARNING | Detected usage of a non-sanitized input variable: $_GET['preview_id'] (WordPress.Security.ValidatedSanitizedInput.InputNotSanitized)
 62 | WARNING | Processing form data without nonce verification. (WordPress.Security.NonceVerification.Recommended)
 62 | WARNING | Detected usage of a non-sanitized input variable: $_GET['preview_nonce'] (WordPress.Security.ValidatedSanitizedInput.InputNotSanitized)
--------------------------------------------------------------------------------------------------------------------------------------------------------


FILE: modules/shared/php/options-utilities.php
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 4 WARNINGS AFFECTING 4 LINES
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 14 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
 26 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
 80 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 8 spaces but found 7 spaces (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
 81 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


FILE: modules/settings/settings.php
---------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
---------------------------------------------------------------------------------------------------------------
 125 | WARNING | The method parameter $args is never used (Generic.CodeAnalysis.UnusedFunctionParameter.Found)
---------------------------------------------------------------------------------------------------------------


FILE: modules/shared/php/helper-utilities.php
---------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 7 WARNINGS AFFECTING 6 LINES
---------------------------------------------------------------------------------------------------------------------
  68 | WARNING | Processing form data without nonce verification. (WordPress.Security.NonceVerification.Recommended)
  68 | WARNING | Processing form data without nonce verification. (WordPress.Security.NonceVerification.Recommended)
  76 | WARNING | Processing form data without nonce verification. (WordPress.Security.NonceVerification.Recommended)
  77 | WARNING | Processing form data without nonce verification. (WordPress.Security.NonceVerification.Recommended)
  82 | WARNING | Processing form data without nonce verification. (WordPress.Security.NonceVerification.Recommended)
 100 | WARNING | Processing form data without nonce verification. (WordPress.Security.NonceVerification.Recommended)
 105 | WARNING | Processing form data without nonce verification. (WordPress.Security.NonceVerification.Recommended)
---------------------------------------------------------------------------------------------------------------------


FILE: modules/settings/views/settings.php
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 26 | WARNING | [x] Array double arrow not aligned correctly; expected 15 space(s) between "'type'" and double arrow, but found 1. (WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)
 27 | WARNING | [x] Array double arrow not aligned correctly; expected 8 space(s) between "'dismissible'" and double arrow, but found 1. (WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


FILE: modules/shared/php/meta-cleanup-utilities.php
------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
------------------------------------------------------------------------------------------------------------------------------------------------------------
 26 | WARNING | [x] Equals sign not aligned correctly; expected 1 space but found 2 spaces (Generic.Formatting.MultipleStatementAlignment.IncorrectWarning)
------------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------------------------------------------------------------


FILE: modules/preview/preview.php
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 2 WARNINGS AFFECTING 2 LINES
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 47 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 1 space but found 2 spaces (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
 48 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 3 spaces but found 4 spaces (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


FILE: modules/editorial-metadata/editorial-metadata.php
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 11 WARNINGS AFFECTING 11 LINES
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 283 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 32 spaces but found 1 space (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
 284 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
 307 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
 320 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
 321 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 1 space but found 2 spaces (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
 326 | WARNING | [x] Usage of ELSE IF is discouraged; use ELSEIF instead (PSR2.ControlStructures.ElseIfDeclaration.NotAllowed)
 335 | WARNING | [x] Usage of ELSE IF is discouraged; use ELSEIF instead (PSR2.ControlStructures.ElseIfDeclaration.NotAllowed)
 358 | WARNING | [x] Usage of ELSE IF is discouraged; use ELSEIF instead (PSR2.ControlStructures.ElseIfDeclaration.NotAllowed)
 366 | WARNING | [x] Array double arrow not aligned correctly; expected 8 space(s) between "'name'" and double arrow, but found 4. (WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)
 367 | WARNING | [x] Array double arrow not aligned correctly; expected 8 space(s) between "'slug'" and double arrow, but found 4. (WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned)
 394 | WARNING | [x] Usage of ELSE IF is discouraged; use ELSEIF instead (PSR2.ControlStructures.ElseIfDeclaration.NotAllowed)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 11 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


FILE: modules/notifications/notifications.php
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 1 WARNING AFFECTING 1 LINE
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 257 | WARNING | `wp_mail` should be used sparingly. For any bulk emailing should be handled by a 3rd party service, in order to prevent domain or IP addresses being flagged as spam.
     |         | (WordPressVIPMinimum.Functions.RestrictedFunctions.wp_mail_wp_mail)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


FILE: modules/custom-status/custom-status.php
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
FOUND 0 ERRORS AND 5 WARNINGS AFFECTING 3 LINES
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  35 | WARNING | [x] Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space (Generic.Formatting.MultipleStatementAlignment.NotSameWarning)
 383 | WARNING | [ ] json_encode() is discouraged. Use wp_json_encode() instead. (WordPress.WP.AlternativeFunctions.json_encode_json_encode)
 922 | WARNING | [ ] Processing form data without nonce verification. (WordPress.Security.NonceVerification.Recommended)
 922 | WARNING | [ ] Processing form data without nonce verification. (WordPress.Security.NonceVerification.Recommended)
 922 | WARNING | [ ] Detected usage of a non-sanitized input variable: $_REQUEST['post_status'] (WordPress.Security.ValidatedSanitizedInput.InputNotSanitized)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Time: 660ms; Memory: 14MB

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like most of these are alignment, with a few other changes like else if -> elseif and some nonce checks we'll either need to add or ignore.

Since we don't have a lot of these, I think it would make sense to remove -n in another PR along with the fixes. This would help avoid inconsistencies with local formatting tools, and I think our code is in a close enough state to just remove these last warnings. What do you think?

const METADATA_REQ_EDITORIAL_IDS_KEY = 'required_metadata_ids';
const METADATA_REQ_EDITORIALS_KEY = 'required_metadatas';
const METADATA_REQ_USER_IDS_KEY = 'required_user_ids';
const METADATA_REQ_USERS_KEY = 'required_users';
const METADATA_REQ_EDITORIALS_KEY = 'required_metadatas';
const METADATA_REQ_USER_IDS_KEY = 'required_user_ids';
const METADATA_REQ_USERS_KEY = 'required_users';

private static $custom_statuses_cache = [];

Expand Down Expand Up @@ -155,10 +155,10 @@ public static function setup_install(): void {
'position' => 4,
],
[
'name' => __( 'Pending Review' ),
'slug' => 'pending',
'description' => __( 'Post needs to be reviewed by an editor.', 'vip-workflow' ),
'position' => 5,
'name' => __( 'Pending Review' ),
'slug' => 'pending',
'description' => __( 'Post needs to be reviewed by an editor.', 'vip-workflow' ),
'position' => 5,
],
];

Expand Down Expand Up @@ -202,10 +202,10 @@ public static function action_admin_enqueue_scripts(): void {
wp_enqueue_style( 'vip-workflow-custom-status-styles', VIP_WORKFLOW_URL . 'dist/modules/custom-status/custom-status-configure.css', [ 'wp-components' ], $asset_file['version'] );

wp_localize_script( 'vip-workflow-custom-status-configure', 'VW_CUSTOM_STATUS_CONFIGURE', [
'custom_statuses' => self::modify_custom_statuses_with_editorial_metadata(),
'custom_statuses' => self::modify_custom_statuses_with_editorial_metadata(),
'editorial_metadatas' => EditorialMetadata::get_editorial_metadata_terms(),
'url_edit_status' => CustomStatusEndpoint::get_crud_url(),
'url_reorder_status' => CustomStatusEndpoint::get_reorder_url(),
'url_edit_status' => CustomStatusEndpoint::get_crud_url(),
'url_reorder_status' => CustomStatusEndpoint::get_reorder_url(),
] );
}

Expand Down Expand Up @@ -265,7 +265,7 @@ private static function modify_custom_statuses_with_editorial_metadata(): array
// Add the required editorial metadata to the custom statuses for UI purposes
foreach ( $custom_statuses as $status ) {
$required_metadata_ids = $status->meta[ self::METADATA_REQ_EDITORIAL_IDS_KEY ] ?? [];
$required_metadatas = [];
$required_metadatas = [];
foreach ( $required_metadata_ids as $metadata_id ) {
$required_metadatas[] = $editorial_metadatas[ $metadata_id ];
}
Expand Down Expand Up @@ -327,7 +327,7 @@ public static function post_admin_header(): void {
$custom_statuses = self::get_custom_statuses();

// $selected can be empty, but must be set because it's used as a JS variable
$selected = '';
$selected = '';

if ( ! empty( $post ) ) {
// Get the status of the current post
Expand Down Expand Up @@ -534,14 +534,23 @@ 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 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'] );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd actually not put this here. This should be on line 571 instead, right before the get_custom_status_by call. I've tried to make the whole process transactional:

  • Save the status
  • Save each of the metadata info as term_meta linked to the status
  • If any step fails, delete all the bad data

So, keeping this here would mess the event up if the status is deleted as bad data. Plus, we would be able to potentially have the entire status object be sent to the event tracking code. That would be useful for recording what type of EM fields are usually linked, etc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good thinking! I changed this in 8c42cb3 to go one step further and wait until after get_custom_status_by() is called, and pass that into the action.

On the receiving side, we now use those meta fields to track the count of required users and required EM fields on custom status changes using the method you suggested above.


// Reset our internal object cache
self::$custom_statuses_cache = [];

$term_id = $inserted_term['term_id'];

$position = $args[ self::METADATA_POSITION_KEY ];
$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 ] ?? [];
$required_user_ids = $args[ self::METADATA_REQ_USER_IDS_KEY ] ?? [];

// In case of failure, data cleanup happens which includes the term and the meta keys.

Expand Down Expand Up @@ -576,7 +585,7 @@ public static function update_custom_status( int $status_id, array $args = [] ):
$old_status = self::get_custom_status_by( 'id', $status_id );
if ( is_wp_error( $old_status ) ) {
return $old_status;
} else if ( ! $old_status ) {
} elseif ( ! $old_status ) {
return new WP_Error( 'invalid', __( "Custom status doesn't exist.", 'vip-workflow' ) );
}

Expand Down Expand Up @@ -610,8 +619,8 @@ public static function update_custom_status( int $status_id, array $args = [] ):
}

$term_fields_to_update = [
'name' => isset( $args['name'] ) ? $args['name'] : $old_status->name,
'slug' => isset( $args['slug'] ) ? $args['slug'] : $old_status->slug,
'name' => isset( $args['name'] ) ? $args['name'] : $old_status->name,
'slug' => isset( $args['slug'] ) ? $args['slug'] : $old_status->slug,
'description' => isset( $args['description'] ) ? $args['description'] : $old_status->description,
];

Expand Down Expand Up @@ -647,9 +656,17 @@ public static function update_custom_status( int $status_id, array $args = [] ):
return $updated_term;
}

$status_result = self::get_custom_status_by( 'id', $status_id );
$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 );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing as add in that, we should provide the entire status imo. That way we can get access to the EM fields linked as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See this comment above, did exactly that.


return $status_result;
return $updated_status;
}

/**
Expand All @@ -663,7 +680,7 @@ public static function delete_custom_status( int $status_id ): bool|WP_Error {
$old_status = self::get_custom_status_by( 'id', $status_id );
if ( is_wp_error( $old_status ) ) {
return $old_status;
} else if ( ! $old_status ) {
} elseif ( ! $old_status ) {
return new WP_Error( 'invalid', __( "Custom status doesn't exist.", 'vip-workflow' ) );
}

Expand Down Expand Up @@ -700,6 +717,14 @@ public static function delete_custom_status( int $status_id ): bool|WP_Error {
return new WP_Error( 'invalid', __( 'Unable to delete custom status.', 'vip-workflow' ) );
}

/**
* Fires after a custom status is deleted from the database.
*
* @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 );

// Reset status cache again, as reassign_post_status() will recache prior statuses
self::$custom_statuses_cache = [];

Expand Down Expand Up @@ -737,9 +762,9 @@ public static function get_custom_statuses(): array {
$statuses = get_terms( [
'taxonomy' => self::TAXONOMY_KEY,
'hide_empty' => false,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_key' => self::METADATA_POSITION_KEY,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_key' => self::METADATA_POSITION_KEY,
]);

if ( is_wp_error( $statuses ) || empty( $statuses ) ) {
Expand All @@ -748,7 +773,7 @@ public static function get_custom_statuses(): array {

// Add metadata to each term
$statuses = array_map( function ( $status ) {
$term_meta = apply_filters( 'vw_register_custom_status_meta', [], $status );
$term_meta = apply_filters( 'vw_register_custom_status_meta', [], $status );
$status->meta = $term_meta;

return $status;
Expand Down Expand Up @@ -784,7 +809,7 @@ public static function get_custom_status_by( string $field, int|string $value ):
if ( is_wp_error( $custom_status ) || ! $custom_status ) {
$custom_status = false;
} else {
$term_meta = apply_filters( 'vw_register_custom_status_meta', [], $custom_status );
$term_meta = apply_filters( 'vw_register_custom_status_meta', [], $custom_status );
$custom_status->meta = $term_meta;
}

Expand All @@ -806,9 +831,9 @@ public static function get_core_statuses(): array {
'description' => __( 'Post is a draft; not ready for review or publication.', 'vip-workflow' ),
],
[
'name' => __( 'Pending Review' ),
'slug' => 'pending',
'description' => __( 'Post needs to be reviewed by an editor.', 'vip-workflow' ),
'name' => __( 'Pending Review' ),
'slug' => 'pending',
'description' => __( 'Post needs to be reviewed by an editor.', 'vip-workflow' ),
],
];

Expand Down
73 changes: 45 additions & 28 deletions modules/notifications/notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public static function notification_status_change( string $new_status, string $o

$body = '';

$post_id = $post->ID;
$post_title = vw_draft_or_post_title( $post_id );
$post_type = $post->post_type;
$post_id = $post->ID;
$post_title = vw_draft_or_post_title( $post_id );
$post_type = $post->post_type;
$subject_post_type = ucfirst( $post_type );

if ( 0 != $current_user->ID ) {
Expand Down Expand Up @@ -147,13 +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' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ingeniumed The notification code has been modified to check for email/webhook presence in this upper function. We use this data ($is_email_scheduled and $is_webhook_scheduled) to determine if we should trigger vw_notification_status_change telemetry at all, and also to include which notification types were sent in that datapoint.

$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 );
}

$webhook_url = OptionsUtilities::get_options_by_key( 'webhook_url' );
$is_webhook_scheduled = '' !== $webhook_url;

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 );
}

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 );
}
}
}

Expand All @@ -174,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
Expand All @@ -203,7 +220,7 @@ public static function schedule_emails( string $action, WP_Post $post, string $s
* @param string $action Action being taken, eg. status-change
*
*/
$subject = apply_filters( 'vw_notification_email_subject', $subject, $action, $post );
$subject = apply_filters( 'vw_notification_email_subject', $subject, $action, $post );

/**
* Filter the email message
Expand All @@ -212,7 +229,7 @@ public static function schedule_emails( string $action, WP_Post $post, string $s
* @param string $action Action being taken, eg. status-change
* @param WP_Post $post Post object
*/
$message = apply_filters( 'vw_notification_email_message', $message, $action, $post );
$message = apply_filters( 'vw_notification_email_message', $message, $action, $post );

/**
* Filter the email headers
Expand All @@ -223,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 ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should switch the use of empty for the actual [] check.

Hat tip to Alexey for that.

wp_schedule_single_event( time(), 'vw_send_scheduled_emails', [ $recipients, $subject, $message, $message_headers ] );
}
}

Expand All @@ -248,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 ] );
Expand All @@ -274,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 ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here.

I know this is code that's just been moved but figured it's good to target it now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b67ae56. This one is a bit tricky because get_options_by_key() has a return type of string|array|bool|null, so we need to be extra explicit here about what type we're expecting.

// This can happen if the webhook URL was cleared after scheduling this notification
return false;
}

// Set up the payload
$payload = [
'type' => $message_type,
Expand Down
Loading