Skip to content

Commit 8f9c570

Browse files
R1shabh-GuptaR1shabh-Guptat-hamanosabernhardt
authored
Fix: Hide "View Post" link for non-viewable post types (#71356)
* fix: hide admin bar View Post link for non-viewable posts * test: add unit tests for listener hooks functionality * fix: restore "View Post" link visibility when switching from non-viewable to viewable post types Co-authored-by: R1shabh-Gupta <rishabhwp@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: sabernhardt <sabernhardt@git.wordpress.org>
1 parent 9515074 commit 8f9c570

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

packages/edit-post/src/components/editor-initialization/listener-hooks.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { useSelect } from '@wordpress/data';
55
import { useEffect, useRef } from '@wordpress/element';
66
import { store as editorStore } from '@wordpress/editor';
7+
import { store as coreStore } from '@wordpress/core-data';
78

89
/**
910
* Internal dependencies
@@ -18,12 +19,17 @@ import {
1819
* post link in the admin bar.
1920
*/
2021
export const useUpdatePostLinkListener = () => {
21-
const { newPermalink } = useSelect(
22-
( select ) => ( {
23-
newPermalink: select( editorStore ).getCurrentPost().link,
24-
} ),
25-
[]
26-
);
22+
const { isViewable, newPermalink } = useSelect( ( select ) => {
23+
const { getPostType } = select( coreStore );
24+
const { getCurrentPost, getEditedPostAttribute } =
25+
select( editorStore );
26+
const postType = getPostType( getEditedPostAttribute( 'type' ) );
27+
return {
28+
isViewable: postType?.viewable,
29+
newPermalink: getCurrentPost().link,
30+
};
31+
}, [] );
32+
2733
const nodeToUpdateRef = useRef();
2834

2935
useEffect( () => {
@@ -36,6 +42,13 @@ export const useUpdatePostLinkListener = () => {
3642
if ( ! newPermalink || ! nodeToUpdateRef.current ) {
3743
return;
3844
}
45+
46+
if ( ! isViewable ) {
47+
nodeToUpdateRef.current.style.display = 'none';
48+
return;
49+
}
50+
51+
nodeToUpdateRef.current.style.display = '';
3952
nodeToUpdateRef.current.setAttribute( 'href', newPermalink );
40-
}, [ newPermalink ] );
53+
}, [ newPermalink, isViewable ] );
4154
};

packages/edit-post/src/components/editor-initialization/test/listener-hooks.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ describe( 'listener hook tests', () => {
3333
...storeConfig,
3434
selectors: {
3535
getCurrentPost: jest.fn(),
36+
getCurrentPostType: jest.fn(),
37+
getEditedPostAttribute: jest.fn(),
38+
},
39+
},
40+
core: {
41+
...storeConfig,
42+
selectors: {
43+
getPostType: jest.fn(),
3644
},
3745
},
3846
'core/viewport': {
@@ -69,6 +77,13 @@ describe( 'listener hook tests', () => {
6977
mockStores[ store ].selectors[ functionName ].mockReturnValue( value );
7078
};
7179

80+
const setupPostTypeScenario = ( postType, isViewable = true ) => {
81+
setMockReturnValue( 'core/editor', 'getEditedPostAttribute', postType );
82+
setMockReturnValue( 'core', 'getPostType', {
83+
viewable: isViewable,
84+
} );
85+
};
86+
7287
afterEach( () => {
7388
Object.values( mockStores ).forEach( ( storeMocks ) => {
7489
Object.values( storeMocks.selectors ).forEach( ( mock ) => {
@@ -95,28 +110,36 @@ describe( 'listener hook tests', () => {
95110
};
96111

97112
const setAttribute = jest.fn();
113+
const mockElement = {
114+
setAttribute,
115+
style: { display: '' },
116+
};
98117
const mockSelector = jest.fn();
99118
beforeEach( () => {
119+
// Reset the mock element style
120+
mockElement.style.display = '';
100121
// eslint-disable-next-line testing-library/no-node-access
101-
document.querySelector = mockSelector.mockReturnValue( {
102-
setAttribute,
103-
} );
122+
document.querySelector =
123+
mockSelector.mockReturnValue( mockElement );
104124
} );
105125
afterEach( () => {
106126
setAttribute.mockClear();
107127
mockSelector.mockClear();
128+
mockElement.style.display = '';
108129
} );
109130
it( 'updates nothing if there is no view link available', () => {
110131
mockSelector.mockImplementation( () => null );
111132
setMockReturnValue( 'core/editor', 'getCurrentPost', {
112133
link: 'foo',
113134
} );
135+
setupPostTypeScenario( 'post', true );
114136
render( <TestedOutput /> );
115137

116138
expect( setAttribute ).not.toHaveBeenCalled();
117139
} );
118140
it( 'updates nothing if there is no permalink', () => {
119141
setMockReturnValue( 'core/editor', 'getCurrentPost', { link: '' } );
142+
setupPostTypeScenario( 'post', true );
120143
render( <TestedOutput /> );
121144

122145
expect( setAttribute ).not.toHaveBeenCalled();
@@ -125,6 +148,7 @@ describe( 'listener hook tests', () => {
125148
setMockReturnValue( 'core/editor', 'getCurrentPost', {
126149
link: 'foo',
127150
} );
151+
setupPostTypeScenario( 'post', true );
128152
const { rerender } = render( <TestedOutput /> );
129153

130154
rerender( <TestedOutput /> );
@@ -139,6 +163,7 @@ describe( 'listener hook tests', () => {
139163
setMockReturnValue( 'core/editor', 'getCurrentPost', {
140164
link: 'foo',
141165
} );
166+
setupPostTypeScenario( 'post', true );
142167
render( <TestedOutput /> );
143168
expect( setAttribute ).toHaveBeenCalledTimes( 1 );
144169
act( () => {
@@ -150,6 +175,7 @@ describe( 'listener hook tests', () => {
150175
setMockReturnValue( 'core/editor', 'getCurrentPost', {
151176
link: 'foo',
152177
} );
178+
setupPostTypeScenario( 'post', true );
153179
render( <TestedOutput /> );
154180
expect( setAttribute ).toHaveBeenCalledTimes( 1 );
155181
expect( setAttribute ).toHaveBeenCalledWith( 'href', 'foo' );
@@ -163,5 +189,15 @@ describe( 'listener hook tests', () => {
163189
expect( setAttribute ).toHaveBeenCalledTimes( 2 );
164190
expect( setAttribute ).toHaveBeenCalledWith( 'href', 'bar' );
165191
} );
192+
it( 'hides the "View Post" link when editing non-viewable post types', () => {
193+
setMockReturnValue( 'core/editor', 'getCurrentPost', {
194+
link: 'foo',
195+
} );
196+
setupPostTypeScenario( 'wp_block', false );
197+
render( <TestedOutput /> );
198+
199+
expect( setAttribute ).not.toHaveBeenCalled();
200+
expect( mockElement ).toHaveProperty( 'style.display', 'none' );
201+
} );
166202
} );
167203
} );

0 commit comments

Comments
 (0)