11<?php
2+
23namespace Cbx \Phpspreadsheet ;
34
45class PDUpdater
@@ -42,6 +43,46 @@ public function authorize($token)
4243 $ this ->authorize_token = $ token ;
4344 }//end function authorize
4445
46+ public function initialize ()
47+ {
48+ add_filter ('pre_set_site_transient_update_plugins ' , [$ this , 'modify_transient ' ], 10 , 1 );
49+ add_filter ('plugins_api ' , [$ this , 'plugin_popup ' ], 10 , 3 );
50+ add_filter ('upgrader_post_install ' , [$ this , 'after_install ' ], 10 , 3 );
51+ add_filter ("http_request_args " , [$ this , "addHeaders " ], 10 , 3 );
52+ }//end function get_repository_info
53+
54+ public function modify_transient ($ transient )
55+ {
56+ if (property_exists ($ transient , 'checked ' )) {
57+ if ($ checked = $ transient ->checked ) {
58+ $ this ->get_repository_info ();
59+ if (isset ($ this ->github_response ['tag_name ' ])) {
60+ $ tag_name = str_replace ("v " , "" , $ this ->github_response ['tag_name ' ]);
61+ $ tag_name = str_replace ("V " , "" , $ tag_name );
62+ $ out_of_date = version_compare ($ tag_name , $ checked [$ this ->basename ], 'gt ' );
63+
64+ if ($ out_of_date ) {
65+ $ new_files = $ this ->github_response ['zipball_url ' ];
66+ $ slug = current (explode ('/ ' , $ this ->basename ));
67+
68+ $ plugin = [
69+ 'url ' => $ this ->plugin ['PluginURI ' ],
70+ 'slug ' => $ slug ,
71+ 'package ' => $ new_files ,
72+ 'new_version ' => $ tag_name
73+ ];
74+ // print_r($plugin);
75+ // exit;
76+ $ transient ->response [$ this ->basename ] = (object ) $ plugin ;
77+ }
78+ }
79+
80+ }
81+ }
82+
83+ return $ transient ;
84+ }//end function initialize
85+
4586 private function get_repository_info ()
4687 {
4788 if (is_null ($ this ->github_response )) {
@@ -71,93 +112,74 @@ private function get_repository_info()
71112
72113 $ response = json_decode ($ response , true );
73114
115+
74116 if (is_array ($ response )) {
75117 $ response = current ($ response );
76118 }
77119
78- if ($ this ->authorize_token ) {
79- $ response ['zipball_url ' ] = add_query_arg ('access_token ' , $ this ->authorize_token , $ response ['zipball_url ' ]);
120+ if ($ this ->authorize_token && isset ($ response ['zipball_url ' ])) {
121+ $ response ['zipball_url ' ] = add_query_arg (
122+ 'access_token ' ,
123+ $ this ->authorize_token ,
124+ $ response ['zipball_url ' ]
125+ );
126+ $ this ->github_response = $ response ;
80127 }
81-
82- $ this ->github_response = $ response ;
83128 }
84- }//end function get_repository_info
129+ }//end function modify_transient
85130
86- public function initialize ( )
131+ public function plugin_popup ( $ result , $ action , $ args )
87132 {
88- add_filter ('pre_set_site_transient_update_plugins ' , [$ this , 'modify_transient ' ], 10 , 1 );
89- add_filter ('plugins_api ' , [$ this , 'plugin_popup ' ], 10 , 3 );
90- add_filter ('upgrader_post_install ' , [$ this , 'after_install ' ], 10 , 3 );
91- add_filter ("http_request_args " , [$ this , "addHeaders " ], 10 , 3 );
92- }//end function initialize
133+ if ($ action !== 'plugin_information ' ) {
134+ return false ;
135+ }
93136
94- public function modify_transient ($ transient )
95- {
96- if (property_exists ($ transient , 'checked ' )) {
97- if ($ checked = $ transient ->checked ) {
137+ if (!empty ($ args ->slug )) {
138+ if ($ args ->slug == current (explode ('/ ' , $ this ->basename ))) {
98139 $ this ->get_repository_info ();
99140
100- $ tag_name = str_replace ("v " , "" , $ this ->github_response ['tag_name ' ]);
101- $ tag_name = str_replace ("V " , "" , $ tag_name );
102- $ out_of_date = version_compare ($ tag_name , $ checked [$ this ->basename ], 'gt ' );
103-
104- if ($ out_of_date ) {
105- $ new_files = $ this ->github_response ['zipball_url ' ];
141+ if (isset ($ this ->github_response ['zipball_url ' ])) {
106142 $ slug = current (explode ('/ ' , $ this ->basename ));
107143
144+ $ tag_name = str_replace ("v " , "" , $ this ->github_response ['tag_name ' ]);
145+ $ tag_name = str_replace ("V " , "" , $ tag_name );
146+
108147 $ plugin = [
109- 'url ' => $ this ->plugin ['PluginURI ' ] ,
148+ 'name ' => isset ( $ this ->plugin ['Name ' ]) ? $ this -> plugin [ ' Name ' ] : '' ,
110149 'slug ' => $ slug ,
111- 'package ' => $ new_files ,
112- 'new_version ' => $ tag_name
150+ 'requires ' => '5.3 ' ,
151+ 'tested ' => '5.4 ' ,
152+ 'version ' => $ tag_name ,
153+ 'author ' => $ this ->plugin ['Author ' ],
154+ 'author_profile ' => $ this ->plugin ['AuthorURI ' ],
155+ 'last_updated ' => $ this ->github_response ['published_at ' ],
156+ 'homepage ' => $ this ->plugin ['PluginURI ' ],
157+ 'short_description ' => isset ($ this ->plugin ['Description ' ]) ? $ this ->plugin ['Description ' ] : '' ,
158+ 'sections ' => [
159+ 'Description ' => isset ($ this ->plugin ['Description ' ]) ? $ this ->plugin ['Description ' ] : '' ,
160+ 'Updates ' => isset ($ this ->github_response ['body ' ]) ? $ this ->github_response ['body ' ] : '' ,
161+ ],
162+ 'download_link ' => $ this ->github_response ['zipball_url ' ]
113163 ];
114164
115- $ transient -> response [ $ this -> basename ] = (object ) $ plugin ;
165+ return (object ) $ plugin ;
116166 }
117- }
118- }
119-
120- return $ transient ;
121- }//end function modify_transient
122167
123- public function plugin_popup ($ result , $ action , $ args )
124- {
125- if ($ action !== 'plugin_information ' ) {
126- return false ;
127- }
128- if (!empty ($ args ->slug )) {
129- if ($ args ->slug == current (explode ('/ ' , $ this ->basename ))) {
130- $ this ->get_repository_info ();
131- $ slug = current (explode ('/ ' , $ this ->basename ));
132-
133- $ tag_name = str_replace ("v " , "" , $ this ->github_response ['tag_name ' ]);
134- $ tag_name = str_replace ("V " , "" , $ tag_name );
135-
136- $ plugin = [
137- 'name ' => isset ($ this ->plugin ['Name ' ]) ? $ this ->plugin ['Name ' ] : '' ,
138- 'slug ' => $ slug ,
139- 'requires ' => '5.3 ' ,
140- 'tested ' => '5.4 ' ,
141- 'version ' => $ tag_name ,
142- 'author ' => $ this ->plugin ['Author ' ],
143- 'author_profile ' => $ this ->plugin ['AuthorURI ' ],
144- 'last_updated ' => $ this ->github_response ['published_at ' ],
145- 'homepage ' => $ this ->plugin ['PluginURI ' ],
146- 'short_description ' => isset ($ this ->plugin ['Description ' ]) ? $ this ->plugin ['Description ' ] : '' ,
147- 'sections ' => [
148- 'Description ' => isset ($ this ->plugin ['Description ' ]) ? $ this ->plugin ['Description ' ] : '' ,
149- 'Updates ' => isset ($ this ->github_response ['body ' ]) ? $ this ->github_response ['body ' ] : '' ,
150- ],
151- 'download_link ' => $ this ->github_response ['zipball_url ' ]
152- ];
153-
154- return (object ) $ plugin ;
155168 }
156169 }
157170
158171 return $ result ;
159172 }//end function plugin_popup
160173
174+ /**
175+ * Take care on after plugin install
176+ *
177+ * @param $response
178+ * @param $hook_extra
179+ * @param $result
180+ *
181+ * @return mixed
182+ */
161183 public function after_install ($ response , $ hook_extra , $ result )
162184 {
163185 global $ wp_filesystem ;
@@ -173,16 +195,24 @@ public function after_install($response, $hook_extra, $result)
173195 return $ result ;
174196 }//end function after_install
175197
198+ /**
199+ * Add github authorization token for plugin download from github
200+ *
201+ * @param $parsed_args
202+ * @param $url
203+ *
204+ * @return mixed
205+ */
176206 public function addHeaders ($ parsed_args , $ url )
177207 {
178208 if (empty ($ parsed_args ['headers ' ])) {
179209 $ parsed_args ['headers ' ] = [];
180210 }
181211
182- if (strpos ($ url , "https://api.github.com/repos/ {$ this ->username }/ {$ this ->repository }" ) !== FALSE ) {
212+ if (strpos ($ url , "https://api.github.com/repos/ {$ this ->username }/ {$ this ->repository }" ) !== false ) {
183213 $ parsed_args ['headers ' ]['Authorization ' ] = "token $ this ->authorize_token " ;
184-
185214 }
215+
186216 return $ parsed_args ;
187217 }//end function addHeaders
188- }// end class PDUpdater
218+ }//end class PDUpdater
0 commit comments