Skip to content

Commit

Permalink
Merge pull request #1022 from 10up/feature/toggle-controls-on-status-…
Browse files Browse the repository at this point in the history
…change

Toggle Distributor controls when post status changes
  • Loading branch information
peterwilsoncc authored Mar 8, 2023
2 parents 7744348 + 9031503 commit db587b0
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 8 deletions.
4 changes: 4 additions & 0 deletions assets/css/push.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ $mediaSmallScreen : "min-width: 480px";
$mediaMediumScreen : "min-width: 768px";
$mediaGutenbergSmallScreen : "max-width: 782px";

#wp-admin-bar-distributor.hide {
display: none;
}

#distributor-push-wrapper,
#wpadminbar #wp-admin-bar-distributor-placeholder > .ab-item {
display: none;
Expand Down
15 changes: 14 additions & 1 deletion assets/js/gutenberg-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { PluginDocumentSettingPanel } from '@wordpress/edit-post';
import { __, _n, _x, sprintf } from '@wordpress/i18n';
import { registerPlugin } from '@wordpress/plugins';

const { document, dtGutenberg, MouseEvent } = window;
const { document, dt, dtGutenberg, MouseEvent } = window;

/**
* Add ability to show the admin bar, if needed
Expand Down Expand Up @@ -179,11 +179,23 @@ const DistributorPlugin = () => {
return null;
}

const distributorTopMenu = document.querySelector(
'#wp-admin-bar-distributor'
);

// eslint-disable-next-line no-shadow, react-hooks/rules-of-hooks -- permission checks are needed.
const post = useSelect( ( select ) =>
select( 'core/editor' ).getCurrentPost()
);
// Make the post title available to the top menu.
dt.postTitle = post.title;

// If we are on a non-supported post status, change what we show
if (
dtGutenberg.supportedPostStati &&
! dtGutenberg.supportedPostStati.includes( postStatus )
) {
distributorTopMenu?.classList.add( 'hide' );
return (
<PluginDocumentSettingPanel
title={ __( 'Distributor', 'distributor' ) }
Expand All @@ -196,6 +208,7 @@ const DistributorPlugin = () => {
);
}

distributorTopMenu?.classList.remove( 'hide' );
return (
<PluginDocumentSettingPanel
title={ __( 'Distributor', 'distributor' ) }
Expand Down
9 changes: 8 additions & 1 deletion assets/js/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,14 @@ jQuery( window ).on( 'load', () => {
// Determine if we need to hide the admin bar
maybeHideAdminBar();

if ( distributorPushWrapper.classList.contains( 'loaded' ) ) {
// If the post title has changed, we need to reload the template.
if (
distributorPushWrapper.classList.contains( 'loaded' ) &&
dt.postTitle === dt.previousPostTitle
) {
return;
}
dt.previousPostTitle = dt.postTitle;

distributorPushWrapper.classList.remove( 'message-error' );
distributorPushWrapper.classList.add( 'loaded' );
Expand Down Expand Up @@ -395,13 +400,15 @@ jQuery( window ).on( 'load', () => {
connections: mustacheData.connections,
foundConnections: mustacheData.connections.length,
showSearch: 5 < mustacheData.connections.length,
postTitle: dt.postTitle,
}
);

setDisabledConnections();
} else {
distributorPushWrapper.innerHTML = template( {
connections: dtConnections,
postTitle: dt.postTitle,
} );
}

Expand Down
11 changes: 7 additions & 4 deletions includes/push-ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function syndicatable() {

global $pagenow;

if ( 'post.php' !== $pagenow ) {
if ( 'post.php' !== $pagenow && 'post-new.php' !== $pagenow ) {
return false;
}
} else {
Expand All @@ -77,17 +77,19 @@ function syndicatable() {
}
}

global $post;
$post = get_post();

if ( empty( $post ) ) {
return;
}

if ( ! in_array( $post->post_status, \Distributor\Utils\distributable_post_statuses(), true ) ) {
// If we're using the classic editor, we need to make sure the post has a distributable status.
if ( ! use_block_editor_for_post( $post ) && ! in_array( $post->post_status, \Distributor\Utils\distributable_post_statuses(), true ) ) {
return false;
}

if ( ! in_array( get_post_type(), $distributable_post_types, true ) || ( ! empty( $_GET['post_type'] ) && 'dt_ext_connection' === $_GET['post_type'] ) ) { // @codingStandardsIgnoreLine Nonce not required
$distributable_post_types = array_diff( $distributable_post_types, array( 'dt_ext_connection' ) );
if ( ! in_array( get_post_type(), $distributable_post_types, true ) ) {
return false;
}

Expand Down Expand Up @@ -421,6 +423,7 @@ function enqueue_scripts( $hook ) {
'nonce' => wp_create_nonce( 'dt-push' ),
'loadConnectionsNonce' => wp_create_nonce( 'dt-load-connections' ),
'postId' => (int) get_the_ID(),
'postTitle' => get_the_title(),
'ajaxurl' => esc_url( admin_url( 'admin-ajax.php' ) ),
'messages' => array(
'ajax_error' => __( 'Ajax error:', 'distributor' ),
Expand Down
1 change: 1 addition & 0 deletions includes/syndicated-post-ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ function enqueue_gutenberg_edit_scripts() {
if ( file_exists( $asset_file ) ) {
$asset_data = require $asset_file;
}
$asset_data['dependencies'][] = 'dt-push';

wp_enqueue_script( 'dt-gutenberg-syndicated-post', plugins_url( '/dist/js/gutenberg-syndicated-post.min.js', __DIR__ ), $asset_data['dependencies'], $asset_data['version'], true );
wp_set_script_translations( 'dt-gutenberg-syndicated-post', 'distributor', DT_PLUGIN_PATH . 'lang' );
Expand Down
2 changes: 1 addition & 1 deletion templates/show-connections-amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div class="inner">
{{#foundConnections}}
<?php /* translators: %s the post title */ ?>
<p><?php echo sprintf( esc_html__( 'Distribute &quot;%s&quot; to other connections.', 'distributor' ), esc_html( get_the_title( $post->ID ) ) ); ?></p>
<p><?php echo sprintf( esc_html__( 'Distribute &quot;%s&quot; to other connections.', 'distributor' ), '{{ postTitle }}' ); ?></p>

<div class="connections-selector">
<div>
Expand Down
2 changes: 1 addition & 1 deletion templates/show-connections.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div class="inner">
<# if ( ! _.isEmpty( connections ) ) { #>
<?php /* translators: %s the post title */ ?>
<p><?php echo sprintf( esc_html__( 'Distribute &quot;%s&quot; to other connections.', 'distributor' ), esc_html( get_the_title( $post->ID ) ) ); ?></p>
<p><?php echo sprintf( esc_html__( 'Distribute &quot;%s&quot; to other connections.', 'distributor' ), '{{ postTitle }}' ); ?></p>

<div class="connections-selector">
<div>
Expand Down

0 comments on commit db587b0

Please sign in to comment.