1+ <?php
2+ class wp_auto_update
3+ {
4+ /**
5+ * The plugin current version
6+ * @var string
7+ */
8+ public $ current_version ;
9+
10+ /**
11+ * The plugin remote update path
12+ * @var string
13+ */
14+ public $ update_path ;
15+
16+ /**
17+ * Plugin Slug (plugin_directory/plugin_file.php)
18+ * @var string
19+ */
20+ public $ plugin_slug ;
21+
22+ /**
23+ * Plugin name (plugin_file)
24+ * @var string
25+ */
26+ public $ slug ;
27+
28+ /**
29+ * Initialize a new instance of the WordPress Auto-Update class
30+ * @param string $current_version
31+ * @param string $plugin_slug
32+ */
33+ function __construct ($ current_version , $ plugin_slug )
34+ {
35+ // Set the class public variables
36+ $ this ->current_version = $ current_version ;
37+ $ this ->update_path = 'http://ws10.multinet.se/edu-plugin/update.php?oldv= ' . $ this ->current_version ;
38+ $ this ->plugin_slug = $ plugin_slug ;
39+ list ($ t1 , $ t2 ) = explode ('/ ' , $ plugin_slug );
40+ $ this ->slug = str_replace ('.php ' , '' , $ t2 );
41+
42+ // define the alternative API for updating checking
43+ add_filter ('pre_set_site_transient_update_plugins ' , array (&$ this , 'check_update ' ));
44+
45+ // Define the alternative response for information checking
46+ add_filter ('plugins_api ' , array (&$ this , 'check_info ' ), 10 , 3 );
47+ }
48+
49+ /**
50+ * Add our self-hosted autoupdate plugin to the filter transient
51+ *
52+ * @param $transient
53+ * @return object $ transient
54+ */
55+ public function check_update ($ transient )
56+ {
57+ if (empty ($ transient ->checked )) {
58+ return $ transient ;
59+ }
60+
61+ // Get the remote version
62+ $ remote_version = $ this ->getRemote_version ();
63+
64+ // If a newer version is available, add the update
65+ if (version_compare ($ this ->current_version , $ remote_version , '< ' )) {
66+ $ obj = new stdClass ();
67+ $ obj ->slug = $ this ->slug ;
68+ $ obj ->new_version = $ remote_version ;
69+ $ obj ->url = $ this ->update_path ;
70+ $ obj ->package = $ this ->update_path ;
71+ $ transient ->response [$ this ->plugin_slug ] = $ obj ;
72+ }
73+ return $ transient ;
74+ }
75+
76+ /**
77+ * Add our self-hosted description to the filter
78+ *
79+ * @param boolean $false
80+ * @param array $action
81+ * @param object $arg
82+ * @return bool|object
83+ */
84+ public function check_info ($ false , $ action , $ arg )
85+ {
86+ if ($ arg ->slug === $ this ->slug ) {
87+ $ information = $ this ->getRemote_information ();
88+ return $ information ;
89+ }
90+ return false ;
91+ }
92+
93+ /**
94+ * Return the remote version
95+ * @return string $remote_version
96+ */
97+ public function getRemote_version ()
98+ {
99+ $ request = wp_remote_post ($ this ->update_path , array ('body ' => array ('action ' => 'version ' )));
100+ if (!is_wp_error ($ request ) || wp_remote_retrieve_response_code ($ request ) === 200 ) {
101+ return $ request ['body ' ];
102+ }
103+ return false ;
104+ }
105+
106+ /**
107+ * Get information about the remote version
108+ * @return bool|object
109+ */
110+ public function getRemote_information ()
111+ {
112+ $ request = wp_remote_post ($ this ->update_path , array ('body ' => array ('action ' => 'info ' )));
113+ if (!is_wp_error ($ request ) || wp_remote_retrieve_response_code ($ request ) === 200 ) {
114+ return unserialize ($ request ['body ' ]);
115+ }
116+ return false ;
117+ }
118+
119+ /**
120+ * Return the status of the plugin licensing
121+ * @return boolean $remote_license
122+ */
123+ public function getRemote_license ()
124+ {
125+ $ request = wp_remote_post ($ this ->update_path , array ('body ' => array ('action ' => 'license ' )));
126+ if (!is_wp_error ($ request ) || wp_remote_retrieve_response_code ($ request ) === 200 ) {
127+ return $ request ['body ' ];
128+ }
129+ return false ;
130+ }
131+ }
0 commit comments