@@ -38,14 +38,14 @@ protected function __construct() {
3838 * Function to setup hooks.
3939 */
4040 public function setup_hooks (): void {
41- add_filter ( 'register_post_type_args ' , array ( $ this , 'restrict_cpt ' ) , 5 , 2 );
42- add_action ( 'init ' , array ( $ this , 'unregister_cpt ' ) , 20 );
43- add_action ( 'current_screen ' , array ( $ this , 'limit_pattern_library_posts ' ) );
44- add_action ( 'current_screen ' , array ( $ this , 'limit_template_posts ' ) );
45- add_filter ( 'register_post_type_args ' , array ( $ this , 'modify_pattern_library_labels ' ) , 10 , 2 );
46- add_action ( 'admin_menu ' , array ( $ this , 'modify_pattern_library_admin_menu ' ) , 999 );
47- add_filter ( 'default_content ' , array ( $ this , 'add_default_content_to_editor ' ) , 10 , 2 );
48- add_filter ( 'default_title ' , array ( $ this , 'add_default_title_to_editor ' ) , 10 , 2 );
41+ add_filter ( 'register_post_type_args ' , [ $ this , 'restrict_cpt ' ] , 5 , 2 );
42+ add_action ( 'init ' , [ $ this , 'unregister_cpt ' ] , 20 );
43+ add_action ( 'current_screen ' , [ $ this , 'limit_pattern_library_posts ' ] );
44+ add_action ( 'current_screen ' , [ $ this , 'limit_template_posts ' ] );
45+ add_filter ( 'register_post_type_args ' , [ $ this , 'modify_pattern_library_labels ' ] , 10 , 2 );
46+ add_action ( 'admin_menu ' , [ $ this , 'modify_pattern_library_admin_menu ' ] , 999 );
47+ add_filter ( 'default_content ' , [ $ this , 'add_default_content_to_editor ' ] , 10 , 2 );
48+ add_filter ( 'default_title ' , [ $ this , 'add_default_title_to_editor ' ] , 10 , 2 );
4949 }
5050
5151 /**
@@ -72,7 +72,7 @@ public function add_default_title_to_editor( string $title ): string {
7272 * @return array Modified arguments.
7373 */
7474 public function restrict_cpt ( array $ args , string $ post_type ): array {
75- if ( ! in_array ( $ post_type , array ( Pattern::SLUG , Template::SLUG ) , true ) ) {
75+ if ( ! in_array ( $ post_type , [ Pattern::SLUG , Template::SLUG ] , true ) ) {
7676 return $ args ;
7777 }
7878
@@ -88,7 +88,7 @@ public function restrict_cpt( array $args, string $post_type ): array {
8888 return $ args ;
8989 }
9090
91- if ( in_array ( $ post_type , array ( Pattern::SLUG , Template::SLUG ) , true ) ) {
91+ if ( in_array ( $ post_type , [ Pattern::SLUG , Template::SLUG ] , true ) ) {
9292 $ args ['public ' ] = false ;
9393 $ args ['show_ui ' ] = false ;
9494 $ args ['show_in_menu ' ] = false ;
@@ -99,15 +99,15 @@ public function restrict_cpt( array $args, string $post_type ): array {
9999 $ args ['exclude_from_search ' ] = true ;
100100 $ args ['publicly_queryable ' ] = false ;
101101 $ args ['show_in_rest ' ] = false ;
102- $ args ['capabilities ' ] = array (
102+ $ args ['capabilities ' ] = [
103103 'edit_post ' => 'do_not_allow ' ,
104104 'read_post ' => 'do_not_allow ' ,
105105 'delete_post ' => 'do_not_allow ' ,
106106 'edit_posts ' => 'do_not_allow ' ,
107107 'edit_others_posts ' => 'do_not_allow ' ,
108108 'publish_posts ' => 'do_not_allow ' ,
109109 'read_private_posts ' => 'do_not_allow ' ,
110- ) ;
110+ ] ;
111111 }
112112
113113 return $ args ;
@@ -142,22 +142,24 @@ public function limit_pattern_library_posts(): void {
142142 $ post_count = $ existing_posts ->publish + $ existing_posts ->draft + $ existing_posts ->pending + $ existing_posts ->private ;
143143
144144 // If a post already exists, redirect to edit screen.
145- if ( $ post_count > 0 ) {
146- // Get the existing post.
147- $ existing_post = get_posts (
148- array (
149- 'post_type ' => Pattern::SLUG ,
150- 'post_status ' => array ( 'publish ' , 'draft ' , 'pending ' , 'private ' ),
151- 'numberposts ' => 1 ,
152- 'suppress_filters ' => false ,
153- )
154- );
145+ if ( $ post_count <= 0 ) {
146+ return ;
147+ }
155148
156- if ( ! empty ( $ existing_post ) ) {
157- // Redirect to edit screen of the existing post.
158- wp_safe_redirect ( admin_url ( 'post.php?post= ' . $ existing_post [0 ]->ID . '&action=edit ' ) );
159- exit ;
160- }
149+ // Get the existing post.
150+ $ existing_post = get_posts (
151+ [
152+ 'post_type ' => Pattern::SLUG ,
153+ 'post_status ' => [ 'publish ' , 'draft ' , 'pending ' , 'private ' ],
154+ 'numberposts ' => 1 ,
155+ 'suppress_filters ' => false ,
156+ ]
157+ );
158+
159+ if ( ! empty ( $ existing_post ) ) {
160+ // Redirect to edit screen of the existing post.
161+ wp_safe_redirect ( admin_url ( 'post.php?post= ' . $ existing_post [0 ]->ID . '&action=edit ' ) );
162+ exit ;
161163 }
162164 }
163165
@@ -183,22 +185,24 @@ public function limit_template_posts(): void {
183185 $ post_count = $ existing_posts ->publish + $ existing_posts ->draft + $ existing_posts ->pending + $ existing_posts ->private ;
184186
185187 // If a post already exists, redirect to edit screen.
186- if ( $ post_count > 0 ) {
187- // Get the existing post.
188- $ existing_post = get_posts (
189- array (
190- 'post_type ' => Template::SLUG ,
191- 'post_status ' => array ( 'publish ' , 'draft ' , 'pending ' , 'private ' ),
192- 'numberposts ' => 1 ,
193- 'suppress_filters ' => false ,
194- )
195- );
188+ if ( $ post_count <= 0 ) {
189+ return ;
190+ }
196191
197- if ( ! empty ( $ existing_post ) ) {
198- // Redirect to edit screen of the existing post.
199- wp_safe_redirect ( admin_url ( 'post.php?post= ' . $ existing_post [0 ]->ID . '&action=edit ' ) );
200- exit ;
201- }
192+ // Get the existing post.
193+ $ existing_post = get_posts (
194+ [
195+ 'post_type ' => Template::SLUG ,
196+ 'post_status ' => [ 'publish ' , 'draft ' , 'pending ' , 'private ' ],
197+ 'numberposts ' => 1 ,
198+ 'suppress_filters ' => false ,
199+ ]
200+ );
201+
202+ if ( ! empty ( $ existing_post ) ) {
203+ // Redirect to edit screen of the existing post.
204+ wp_safe_redirect ( admin_url ( 'post.php?post= ' . $ existing_post [0 ]->ID . '&action=edit ' ) );
205+ exit ;
202206 }
203207 }
204208
@@ -257,28 +261,32 @@ public function modify_pattern_library_admin_menu(): void {
257261
258262 $ post_count = $ existing_posts ->publish + $ existing_posts ->draft + $ existing_posts ->pending + $ existing_posts ->private ;
259263
260- if ( $ post_count > 0 ) {
261- // Find the "Add New" menu item.
262- foreach ( $ submenu [ 'edit.php?post_type= ' . Pattern::SLUG ] as $ key => $ item ) {
263- if ( 'post-new.php?post_type= ' . Pattern::SLUG === $ item [2 ] ) {
264- // Get the existing post.
265- $ existing_post = get_posts (
266- array (
267- 'post_type ' => Pattern::SLUG ,
268- 'post_status ' => array ( 'publish ' , 'draft ' , 'pending ' , 'private ' ),
269- 'numberposts ' => 1 ,
270- 'suppress_filters ' => false ,
271- )
272- );
273-
274- if ( ! empty ( $ existing_post ) ) {
275- // Change the "Add New" link to edit the existing post.
276- $ submenu ['edit.php?post_type=onedesign-pattern ' ][ $ key ][2 ] = 'post.php?post= ' . $ existing_post [0 ]->ID . '&action=edit ' ; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- We need to modify for pattern library post type.
277- $ submenu ['edit.php?post_type=onedesign-pattern ' ][ $ key ][0 ] = esc_html__ ( 'Edit Pattern Library ' , 'onedesign ' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- We need to modify for pattern library post type.
278- }
279- break ;
280- }
264+ if ( $ post_count <= 0 ) {
265+ return ;
266+ }
267+
268+ // Find the "Add New" menu item.
269+ foreach ( $ submenu [ 'edit.php?post_type= ' . Pattern::SLUG ] as $ key => $ item ) {
270+ if ( 'post-new.php?post_type= ' . Pattern::SLUG !== $ item [2 ] ) {
271+ continue ;
272+ }
273+
274+ // Get the existing post.
275+ $ existing_post = get_posts (
276+ [
277+ 'post_type ' => Pattern::SLUG ,
278+ 'post_status ' => [ 'publish ' , 'draft ' , 'pending ' , 'private ' ],
279+ 'numberposts ' => 1 ,
280+ 'suppress_filters ' => false ,
281+ ]
282+ );
283+
284+ if ( ! empty ( $ existing_post ) ) {
285+ // Change the "Add New" link to edit the existing post.
286+ $ submenu ['edit.php?post_type=onedesign-pattern ' ][ $ key ][2 ] = 'post.php?post= ' . $ existing_post [0 ]->ID . '&action=edit ' ; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- We need to modify for pattern library post type.
287+ $ submenu ['edit.php?post_type=onedesign-pattern ' ][ $ key ][0 ] = esc_html__ ( 'Edit Pattern Library ' , 'onedesign ' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- We need to modify for pattern library post type.
281288 }
289+ break ;
282290 }
283291 }
284292
0 commit comments