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

Toggle Distributor controls when post status changes #1022

Merged
merged 9 commits into from
Mar 8, 2023

Conversation

ggutenberg
Copy link
Contributor

Description of the Change

Renders the Distributor element in the admin bar whether the post status is distributable or not. If it's not, the element is hidden by CSS. When the post status becomes distributable, the element is displayed.

Also adds a watch command to the npm scripts.

Closes #141

How to test the Change

  1. Edit a Draft post. Note that the Distributor button in the admin bar is not available, and the Distributor element in the Inspector says "Distribution options available once published".
  2. Publish the post. Note that the Distributor button in the admin bar is now available, and the Distributor element has a button to "Distribute page".
  3. Reload the editor page. Note that the Distributor button in the admin bar is still available, as is the "Distribute page" button.
  4. Click Switch to draft and unpublish the post. Note that the admin bar and Distributor Inspector element are as they were in step 1.

Changelog Entry

Changed - Toggles the Distributor admin bar element in Gutenberg based on the post status.

Credits

Props @ggutenberg

Checklist:

  • I agree to follow this project's Code of Conduct.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests pass.

@ggutenberg ggutenberg requested a review from a team as a code owner March 2, 2023 16:58
@ggutenberg ggutenberg requested review from iamdharmesh and removed request for a team March 2, 2023 16:58
@jeffpaul jeffpaul requested a review from peterwilsoncc March 2, 2023 17:24
@jeffpaul jeffpaul added this to the 2.0.0 milestone Mar 2, 2023
Copy link
Collaborator

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

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

I've added a few notes. the main item that needs improvement is to prevent the errors on the new post screen.

When hacking the syndicatable() function to test this out (by adding return true at the start ;) I noticed that the post title needs to be updated via JavaScript too.

The UI shows the post title on page load rather than the post title at publication. In this screen shot it shows as "Auto Draft" as that's the default title WP uses.

Screen Shot 2023-03-03 at 2 15 10 pm

package.json Outdated
@@ -37,6 +37,7 @@
},
"scripts": {
"build": "wp-scripts build",
"watch": "wp-scripts build --watch",
Copy link
Collaborator

Choose a reason for hiding this comment

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

The start script on line 54 already does this (wp-scripts start being an alias).

Did you add this because watch is a more common name than start? I've been considering the same because it's a more common name.

Copy link
Contributor Author

@ggutenberg ggutenberg Mar 3, 2023

Choose a reason for hiding this comment

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

Ah, I didn't realize start does the same thing. I did a search in package.json for the word watch and couldn't find anything, so I added it.

Do you want me to remove this or alias it to npm run start?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you want me to remove this or alias it to npm run start?

Let's remove it for now. I'll review our other open-source plugins for the common names, whether it be dev, watch or something else.

includes/push-ui.php Show resolved Hide resolved
includes/push-ui.php Outdated Show resolved Hide resolved
assets/js/gutenberg-plugin.js Outdated Show resolved Hide resolved
assets/js/gutenberg-plugin.js Outdated Show resolved Hide resolved
@ggutenberg ggutenberg requested a review from peterwilsoncc March 3, 2023 15:45
Copy link
Collaborator

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

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

I think this is really close, just a few things I've noticed.

Instead of using dtGutenberg to store the title, use the dt object. That's available wherever the admin bar is: classic editor, block editor and on the front end.

I've pushed 3367955 to include the page title at load in the dt object, that will ensure it's present for the front end and the classic editor.

I noticed on the classic editor, the menu bar shows and allows a user to distribute a draft.

dist-new-post

For the classic editor, I think we'll need to do something to prevent the Distributor menu from displaying on unpublished posts (the classic editor uses a full refresh) so it can be hidden. Maybe check use_block_editor_for_post(). You'll need to confirm the classic-editor-remember meta data has been added to the auto-draft at that point.

I'm trying to figure out if we'll need to protect against users unhiding the menu bar in Gutenberg and pushing a draft/non-distributable status to another site. Unfortunately, I haven't been able to figure out how we might go about this.

Thanks for the progress so far, it's looking really nice.

select( 'core/editor' ).getCurrentPost()
);
// Make the post title available to the top menu.
dtGutenberg.postTitle = post.title;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
dtGutenberg.postTitle = post.title;
dt.postTitle = post.title;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is strange. If I modify the const { document, dtGutenberg, MouseEvent } = window; line and change it to const { document, dtGutenberg, MouseEvent, dt } = window;, dt is undefined. However if I don't make that modification I'm able to reference it in code directly via dt.postTitle or window.dt.postTitle. Going with window.dt.postTitle for now as it's more explicit and doesn't require an eslint exception, but please let me know if you have a better suggestion.

// If the post title has changed, we need to reload the template.
if (
distributorPushWrapper.classList.contains( 'loaded' ) &&
dtGutenberg.postTitle === dtGutenberg.previousPostTitle
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
dtGutenberg.postTitle === dtGutenberg.previousPostTitle
dt.postTitle === dt.previousPostTitle

return;
}
dtGutenberg.previousPostTitle = dtGutenberg.postTitle;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
dtGutenberg.previousPostTitle = dtGutenberg.postTitle;
dt.previousPostTitle = dt.postTitle;

@@ -394,13 +399,15 @@ jQuery( window ).on( 'load', () => {
connections: mustacheData.connections,
foundConnections: mustacheData.connections.length,
showSearch: 5 < mustacheData.connections.length,
postTitle: dtGutenberg.postTitle,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
postTitle: dtGutenberg.postTitle,
postTitle: dt.postTitle,

}
);

setDisabledConnections();
} else {
distributorPushWrapper.innerHTML = template( {
connections: dtConnections,
postTitle: dtGutenberg.postTitle,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
postTitle: dtGutenberg.postTitle,
postTitle: dt.postTitle,

@ggutenberg
Copy link
Contributor Author

@peterwilsoncc

You'll need to confirm the classic-editor-remember meta data has been added to the auto-draft at that point.

Are you referring to the draft created on the source side, or on the target side? I've done some digging and familiarized myself with the Classic Editor plugin and how it uses this metadata, but am still unsure what the goal of this requirement is.

@ggutenberg ggutenberg requested a review from peterwilsoncc March 6, 2023 22:37
@peterwilsoncc
Copy link
Collaborator

Are you referring to the draft created on the source side, or on the target side? I've done some digging and familiarized myself with the Classic Editor plugin and how it uses this metadata, but am still unsure what the goal of this requirement is.

I'm referring to the source site, so the check you pushed to syndicatable() looks correct but it could do with a comment.

This is strange. If I modify the const { document, dtGutenberg, MouseEvent } = window; line and change it to const { document, dtGutenberg, MouseEvent, dt } = window;, dt is undefined. However if I don't make that modification I'm able to reference it in code directly via dt.postTitle or window.dt.postTitle.

It looks like the dt-push script is enqueued after the dt-gutenberg-syndicated-post script. You might need to force the order by adding dt-push as a dependency if it doesn't cause any problems:

$asset_file = DT_PLUGIN_PATH . '/dist/js/gutenberg-syndicated-post.min.asset.php';
// Fallback asset data.
$asset_data = array(
	'version'      => DT_VERSION,
	'dependencies' => array(),
);
if ( file_exists( $asset_file ) ) {
	$asset_data = require $asset_file;
}
$asset_data['dependencies'][] = 'dt-push'; // NEW LINE.

wp_enqueue_script( 'dt-gutenberg-syndicated-post', plugins_url( '/dist/js/gutenberg-syndicated-post.min.js', __DIR__ ), $asset_data['dependencies'], $asset_data['version'], true );

@ggutenberg
Copy link
Contributor Author

@peterwilsoncc Thanks. PR updated.

peterwilsoncc
peterwilsoncc previously approved these changes Mar 8, 2023
Copy link
Collaborator

@peterwilsoncc peterwilsoncc left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks so much for your patience over the last few days.

I took the liberty of pushing an E2E test in d9f355a to ensure the distributor panel works as expected.

As the block editors selectors change from time-to-time, it may fail in which case I will revert and merge the existing work as is.

@peterwilsoncc peterwilsoncc merged commit db587b0 into develop Mar 8, 2023
@peterwilsoncc peterwilsoncc deleted the feature/toggle-controls-on-status-change branch March 8, 2023 04:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Gutenberg: Extra Refresh to Push Content In Gutenberg
3 participants