1+ <?php
2+
3+ /**
4+ * Plugin loader.
5+ *
6+ * @package OnePlace\Swissknife
7+ * @copyright 2019 Verein onePlace
8+ * @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License, version 2
9+ * @link https://1plc.ch/wordpress-plugins/swissknife
10+ */
11+
12+ namespace OnePlace \Swissknife \Modules ;
13+
14+ use OnePlace \Swissknife \Plugin ;
15+
16+ final class Updater {
17+ /**
18+ * Main instance of the module
19+ *
20+ * @since 0.1-stable
21+ * @var Plugin|null
22+ */
23+ private static $ instance = null ;
24+
25+ private $ sRepoUser ;
26+ private $ sRepoName ;
27+ private $ sRepoToken ;
28+ private $ sSlug ;
29+ private $ githubAPIResult ;
30+ private $ aPluginData ;
31+
32+ /**
33+ * Enable Google Sitekit IP Anonymization
34+ *
35+ * @since 0.1-stable
36+ */
37+ public function register () {
38+ $ this ->sRepoName = 'PLC_WP_Swissknife ' ;
39+ $ this ->sRepoUser = 'OnePlc ' ;
40+ $ this ->sRepoToken = '' ;
41+ $ this ->sSlug = 'wpplc-swissknife ' ;
42+ $ this ->aPluginData = [
43+ 'Name ' => 'WP PLC Swissknife ' ,
44+ 'AuthorName ' => 'onePlace ' ,
45+ 'PluginURI ' => 'https://wp.1plc.ch/wordpress-plugins/swissknife ' ,
46+ 'Description ' => 'onePlace Swissknife for Wordpress. Increase Wordpress Security and Performance ' ,
47+ ];
48+
49+ // For TESTING ONLY
50+ set_site_transient ( 'update_plugins ' , null );
51+
52+ add_filter ( "pre_set_site_transient_update_plugins " , [$ this , "setTransient " ] );
53+
54+ add_filter ( "plugins_api " , [ $ this , "setPluginInfo " ], 10 , 3 );
55+ }
56+
57+ // Push in plugin version information to display in the details lightbox
58+ public function setPluginInfo ( $ false , $ action , $ response ) {
59+ // Get plugin & GitHub release information
60+ //$this->initPluginData();
61+ $ this ->getRepoReleaseInfo ();
62+
63+ // If nothing is found, do nothing
64+ if ( empty ( $ response ->slug ) || $ response ->slug != $ this ->sSlug ) {
65+ return false ;
66+ }
67+
68+ // Add our plugin information
69+ $ response ->last_updated = $ this ->githubAPIResult ->published_at ;
70+ $ response ->slug = $ this ->sSlug ;
71+ $ response ->plugin_name = $ this ->pluginData ["Name " ];
72+ $ response ->version = $ this ->githubAPIResult ->tag_name ;
73+ $ response ->author = $ this ->pluginData ["AuthorName " ];
74+ $ response ->homepage = $ this ->pluginData ["PluginURI " ];
75+
76+ // Create tabs in the lightbox
77+ $ response ->sections = [
78+ 'description ' => $ this ->pluginData ["Description " ],
79+ 'changelog ' => class_exists ( "Parsedown " )
80+ ? \Parsedown::instance ()->parse ( $ this ->githubAPIResult ->body )
81+ : $ this ->githubAPIResult ->body
82+ ];
83+
84+ // This is our release download zip file
85+ $ downloadLink = $ this ->githubAPIResult ->zipball_url ;
86+
87+ // Include the access token for private GitHub repos
88+ if ( !empty ( $ this ->sRepoToken ) ) {
89+ $ downloadLink = add_query_arg (
90+ [ "access_token " => $ this ->sRepoToken ],
91+ $ downloadLink
92+ );
93+ }
94+ $ response ->download_link = $ downloadLink ;
95+
96+ return $ response ;
97+ }
98+
99+ public function setTransient ($ transient ) {
100+ // If we have checked the plugin data before, don't re-check
101+ if ( empty ( $ transient ->checked ) ) {
102+ return $ transient ;
103+ }
104+
105+ // Get plugin & GitHub release information
106+ $ this ->getRepoReleaseInfo ();
107+
108+ // Check the versions if we need to do an update
109+ $ doUpdate = version_compare ( $ this ->githubAPIResult ->tag_name , $ transient ->checked [$ this ->sSlug ] );
110+
111+ // Update the transient to include our updated plugin data
112+ if ( $ doUpdate == 1 ) {
113+ $ package = $ this ->githubAPIResult ->zipball_url ;
114+
115+ // Include the access token for private GitHub repos
116+ if ( !empty ( $ this ->sRepoToken ) ) {
117+ $ package = add_query_arg ( [ "access_token " => $ this ->sRepoToken ], $ package );
118+ }
119+
120+ $ obj = new \stdClass ();
121+ $ obj ->slug = $ this ->sSlug ;
122+ $ obj ->new_version = $ this ->githubAPIResult ->tag_name ;
123+ $ obj ->url = 'https://1plc.ch ' ;
124+ $ obj ->package = $ package ;
125+ $ obj ->tested = '5.3 ' ;
126+ $ obj ->requires = '5.2.4 ' ;
127+
128+ $ transient ->response [$ this ->sSlug .'/ ' .$ this ->sSlug .'.php ' ] = $ obj ;
129+ }
130+
131+ return $ transient ;
132+ }
133+
134+ public function getRepoReleaseInfo () {
135+ if ( ! empty ( $ this ->githubAPIResult ) ) {
136+ return ;
137+ }
138+ // Query the Gitea API
139+ $ url = "https://api.github.com/repos/ {$ this ->sRepoUser }/ {$ this ->sRepoName }/releases " ;
140+
141+ // We need the access token for private repos
142+ if ( ! empty ( $ this ->sRepoToken ) ) {
143+ $ url = add_query_arg ( [ "access_token " => $ this ->sRepoToken ], $ url );
144+ }
145+ // Get the results
146+ $ this ->githubAPIResult = wp_remote_retrieve_body ( wp_remote_get ( $ url , ['sslverify ' =>false ] ) );
147+
148+ if ( ! empty ( $ this ->githubAPIResult ) ) {
149+ $ this ->githubAPIResult = json_decode ( $ this ->githubAPIResult );
150+ }
151+
152+ // Use only the latest release
153+ if ( is_array ( $ this ->githubAPIResult ) ) {
154+ $ this ->githubAPIResult = $ this ->githubAPIResult [0 ];
155+ }
156+ }
157+
158+ /**
159+ * Loads the plugin main instance and initializes it.
160+ *
161+ * @since 0.1-stable
162+ *
163+ * @param string $main_file Absolute path to the plugin main file.
164+ * @return bool True if the plugin main instance could be loaded, false otherwise.
165+ */
166+ public static function load () {
167+ if ( null !== static ::$ instance ) {
168+ return false ;
169+ }
170+ static ::$ instance = new self ();
171+ static ::$ instance ->register ();
172+ return true ;
173+ }
174+ }
0 commit comments