1212final class PluginUpdate implements Registerable
1313{
1414
15- private Plugin $ plugin ;
15+ private $ plugin ;
1616
17- private Site $ upgrade_url_template ;
17+ private $ upgrade_url_template ;
1818
19- public function __construct (Plugin $ plugin , Site $ upgrade_url_template )
20- {
21- $ this ->plugin = $ plugin ;
22- $ this ->upgrade_url_template = $ upgrade_url_template ;
23- }
19+ public function __construct (Plugin $ plugin , Site $ upgrade_url_template )
20+ {
21+ $ this ->plugin = $ plugin ;
22+ $ this ->upgrade_url_template = $ upgrade_url_template ;
23+ }
2424
25- public function register (): void
26- {
27- add_action (
28- 'in_plugin_update_message- ' . $ this ->plugin ->get_basename (),
29- [$ this , 'render_additional_message ' ],
30- 10 ,
31- 2
32- );
25+ public function register (): void
26+ {
27+ add_action (
28+ 'in_plugin_update_message- ' . $ this ->plugin ->get_basename (),
29+ [$ this , 'render_additional_message ' ],
30+ 10 ,
31+ 2
32+ );
3333
34- // Use wp_clean_update_cache() to bypass cache
35- add_filter ('pre_set_site_transient_update_plugins ' , [$ this , 'add_upgrade_notice_to_response ' ], 20 );
34+ // Use wp_clean_update_cache() to bypass cache
35+ add_filter ('pre_set_site_transient_update_plugins ' , [$ this , 'add_upgrade_notice_to_response ' ], 20 );
3636
37- add_action ('admin_head ' , function () {
38- $ screen = get_current_screen ();
37+ add_action ('admin_head ' , function () {
38+ $ screen = get_current_screen ();
3939
40- if ( ! $ screen || $ screen ->base !== 'plugins ' ) {
41- return ;
42- }
40+ if ( ! $ screen || $ screen ->base !== 'plugins ' ) {
41+ return ;
42+ }
4343
44- ?>
44+ ?>
4545
4646 <style>
4747 p.ac-plugin-icon-upgrade::before {
4848 content: '\f534' !important;
4949 }
5050 </style>
5151
52- <?php
53- });
54- }
55-
56- private function get_general_warning_message (): string
57- {
58- return __ (
59- 'You are upgrading to a new a major version of Admin Columns, which can include <strong>breaking changes</strong>. ' ,
60- 'codepress-admin-columns '
61- );
62- }
63-
64- public function render_additional_message (array $ data , object $ response ): void
65- {
66- $ version = new Version ($ response ->new_version );
67-
68- if ($ this ->plugin ->get_version ()->get_major_version () >= $ version ->get_major_version ()) {
69- return ;
70- }
71-
72- $ tpl = $ this ->get_general_warning_message () . ' ' . _x (
73- 'Please review the %s for details. ' ,
74- '%s contains the link to upgrade guide. Label is translated separately. ' ,
75- 'codepress-admin-columns '
76- );
77-
78- $ tpl = wp_kses ($ tpl , [
79- 'strong ' => [],
80- ]
81- );
82-
83- $ url = sprintf ((string )$ this ->upgrade_url_template , $ version ->get_major_version ());
84-
85- $ url = sprintf (
86- '<a href="%s" target="_blank">%s</a> ' ,
87- $ url ,
88- esc_html_x ('upgrade guide ' , 'Anchor text to upgrade guide. ' , 'codepress-admin-columns ' )
89- );
90-
91- $ message = sprintf (
92- $ tpl ,
93- $ url
94- );
95-
96- echo '</p><p class="ac-plugin-icon-upgrade"> ' . $ message ;
97- }
98-
99- /**
100- * Add upgrade notice to the update message on the updates page in the WordPress admin
101- */
102- public function add_upgrade_notice_to_response ($ transient ): ? object
103- {
104- $ basename = $ this ->plugin ->get_basename ();
105-
106- if (
107- is_object ($ transient ) &&
108- isset ($ transient ->checked , $ transient ->response ) &&
109- ! empty ($ transient ->checked [$ basename ]) &&
110- ! empty ($ transient ->response [$ basename ])
111- ) {
112- $ update_major = (new Version ($ transient ->response [$ basename ]->new_version ))->get_major_version ();
113-
114- if ($ update_major > $ this ->plugin ->get_version ()->get_major_version ()) {
115- $ notice = sprintf (
116- '%s: %s %s ' ,
117- esc_html__ ('Warning ' ),
118- strip_tags ($ this ->get_general_warning_message ()),
119- esc_html__ ('See the Plugins page for details. ' , 'codepress-admin-columns ' )
120- );
121-
122- $ transient ->response [$ basename ]->upgrade_notice = $ notice ;
123- }
124- }
125-
126- return $ transient ;
127- }
52+ <?php
53+ });
54+ }
55+
56+ private function get_general_warning_message (): string
57+ {
58+ return __ (
59+ 'You are upgrading to a new a major version of Admin Columns, which can include <strong>breaking changes</strong>. ' ,
60+ 'codepress-admin-columns '
61+ );
62+ }
63+
64+ public function render_additional_message (array $ data , object $ response ): void
65+ {
66+ $ version = new Version ($ response ->new_version );
67+
68+ if ($ this ->plugin ->get_version ()->get_major_version () >= $ version ->get_major_version ()) {
69+ return ;
70+ }
71+
72+ $ tpl = $ this ->get_general_warning_message () . ' ' . _x (
73+ 'Please review the %s for details. ' ,
74+ '%s contains the link to upgrade guide. Label is translated separately. ' ,
75+ 'codepress-admin-columns '
76+ );
77+
78+ $ tpl = wp_kses ($ tpl , [
79+ 'strong ' => [],
80+ ]
81+ );
82+
83+ $ url = sprintf ((string )$ this ->upgrade_url_template , $ version ->get_major_version ());
84+
85+ $ url = sprintf (
86+ '<a href="%s" target="_blank">%s</a> ' ,
87+ $ url ,
88+ esc_html_x ('upgrade guide ' , 'Anchor text to upgrade guide. ' , 'codepress-admin-columns ' )
89+ );
90+
91+ $ message = sprintf (
92+ $ tpl ,
93+ $ url
94+ );
95+
96+ echo '</p><p class="ac-plugin-icon-upgrade"> ' . $ message ;
97+ }
98+
99+ /**
100+ * Add upgrade notice to the update message on the updates page in the WordPress admin
101+ */
102+ public function add_upgrade_notice_to_response ($ transient )
103+ {
104+ $ basename = $ this ->plugin ->get_basename ();
105+
106+ if (
107+ is_object ($ transient ) &&
108+ isset ($ transient ->checked , $ transient ->response ) &&
109+ ! empty ($ transient ->checked [$ basename ]) &&
110+ ! empty ($ transient ->response [$ basename ])
111+ ) {
112+ $ update_major = (new Version ($ transient ->response [$ basename ]->new_version ))->get_major_version ();
113+
114+ if ($ update_major > $ this ->plugin ->get_version ()->get_major_version ()) {
115+ $ notice = sprintf (
116+ '%s: %s %s ' ,
117+ esc_html__ ('Warning ' ),
118+ strip_tags ($ this ->get_general_warning_message ()),
119+ esc_html__ ('See the Plugins page for details. ' , 'codepress-admin-columns ' )
120+ );
121+
122+ $ transient ->response [$ basename ]->upgrade_notice = $ notice ;
123+ }
124+ }
125+
126+ return $ transient ;
127+ }
128128
129129}
0 commit comments