PageRenderTime 31ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/tf-flexslider/classes/class-woothemes-plugin-updater.php

https://bitbucket.org/rchlmrtn/chiari
PHP | 469 lines | 236 code | 78 blank | 155 comment | 62 complexity | e70d42f1c7789c9d9b512235f2d3612d MD5 | raw file
  1. <?php
  2. /**
  3. * WooThemes Plugin Update Checker
  4. *
  5. * Use the WordPress Update Manager to check for plugin updates at WooThemes.com.
  6. *
  7. * @version 1.0.0
  8. * @category Plugins
  9. * @package WordPress
  10. * @subpackage WooFramework
  11. * @author WooThemes
  12. * @since 1.0.0
  13. *
  14. * TABLE OF CONTENTS
  15. *
  16. * - var $api_url
  17. * - var $api_key
  18. *
  19. * - var $plugin_path
  20. * - var $plugin_url
  21. * - var $plugin_prefix
  22. * - var $plugin_base
  23. *
  24. * - var $username
  25. * - var $password
  26. * - Constructor()
  27. * - init()
  28. * - authenticate()
  29. * - load_user_data()
  30. * - register_nav_menu_link()
  31. * - admin_screen()
  32. * - admin_screen_logic()
  33. * - contextual_help()
  34. * - admin_notice()
  35. * - update_check()
  36. * - plugin_information()
  37. * - prepare_request()
  38. * - get_api_data()
  39. * - instance_exists()
  40. * - hide_admin_notice()
  41. */
  42. class WooThemes_Plugin_Updater {
  43. var $api_url = 'https://www.woothemes.com/api';
  44. var $api_key = ''; // Unique value, used to determine the plugin to be updated.
  45. var $plugin_token = 'woothemes-plugin-updater';
  46. var $plugin_path;
  47. var $plugin_url;
  48. var $plugin_prefix = 'woothemes_plugin_updater_';
  49. var $plugin_base;
  50. var $username = '';
  51. var $password = '';
  52. /**
  53. * WooThemes_Plugin_Updater function.
  54. *
  55. * @access public
  56. * @return void
  57. */
  58. function WooThemes_Plugin_Updater ( $file ) {
  59. $this->plugin_path = dirname( $file );
  60. $this->plugin_url = trailingslashit( WP_PLUGIN_URL ) . plugin_basename( dirname( $file ) );
  61. $this->plugin_base = plugin_basename( $file );
  62. } // End Constructor
  63. /**
  64. * init function.
  65. *
  66. * @access public
  67. * @return void
  68. */
  69. function init () {
  70. // Don't do anything without the API key.
  71. if ( $this->api_key == '' ) { return; }
  72. // Register Navigation Menu Link
  73. add_action( 'admin_menu', array( &$this, 'register_nav_menu_link' ), 10 );
  74. // Check For Updates
  75. add_filter( 'pre_set_site_transient_update_plugins', array( &$this, 'update_check' ) );
  76. // Check For Plugin Information
  77. add_filter( 'plugins_api', array( &$this, 'plugin_information' ), 10, 3 );
  78. if ( ! $this->instance_exists() ) {
  79. add_action( 'init', array( &$this, 'hide_admin_notice' ), 0 );
  80. }
  81. } // End init()
  82. /**
  83. * authenticate function.
  84. *
  85. * @access public
  86. * @param string $username
  87. * @param string $password
  88. * @return boolean $is_valid
  89. */
  90. function authenticate ( $username, $password ) {
  91. $is_valid = false;
  92. if ( $username != '' && $password != '' ) {
  93. $params = array( 'username' => $username, 'password' => md5( $password ), 'action' => 'authenticate' );
  94. $xmlobj = $this->get_api_data( $params );
  95. if ( $xmlobj[0] == 'OK' ) {
  96. $is_valid = true;
  97. }
  98. }
  99. return $is_valid;
  100. } // End authenticate()
  101. /**
  102. * load_user_data function.
  103. *
  104. * @access public
  105. * @return void
  106. */
  107. function load_user_data () {
  108. $user = get_option( $this->plugin_prefix . 'username' );
  109. $pass = get_option( $this->plugin_prefix . 'password' );
  110. if ( $user != '' && $pass != '' ) {
  111. $this->username = $user;
  112. $this->password = $pass;
  113. }
  114. } // End load_user_data()
  115. /**
  116. * register_nav_menu_link function.
  117. *
  118. * @access public
  119. * @return void
  120. * @uses admin_screen_logic()
  121. */
  122. function register_nav_menu_link () {
  123. // Don't register the menu if it's already there.
  124. if ( $this->instance_exists() ) { return; }
  125. // Setup Admin Notices
  126. add_action ( 'admin_notices', array( &$this, 'admin_notice' ) );
  127. if ( function_exists( 'add_submenu_page' ) ) {
  128. //$this->admin_screen = add_submenu_page( 'index.php', __( 'WooThemes Updates', 'wooslider' ), __( 'WooThemes Updates', 'wooslider' ), 'switch_themes', $this->plugin_token, array( &$this, 'admin_screen' ) );
  129. }
  130. // Load admin screen logic.
  131. if ( isset( $_POST['woo-action'] ) && ( $_POST['woo-action'] == 'woo-plugin-updater-login' ) ) {
  132. $this->admin_screen_logic();
  133. }
  134. } // End register_nav_menu_link()
  135. /**
  136. * admin_screen function.
  137. *
  138. * @access public
  139. * @return void
  140. */
  141. function admin_screen () {
  142. ?>
  143. <div class="wrap">
  144. <?php screen_icon( 'plugins' ); ?>
  145. <h2><?php _e( 'WooThemes Plugin Updater', 'wooslider' ); ?></h2>
  146. <form name="woo-plugin-updater-login" id="woo-plugin-updater-login" action="<?php echo admin_url( 'index.php?page=' . $this->plugin_token ); ?>" method="post">
  147. <fieldset>
  148. <table class="form-table">
  149. <tbody>
  150. <tr>
  151. <th scope="row"><label for="username"><?php _e( 'WooThemes Username', 'wooslider' ); ?>:</label></th>
  152. <td><input type="text" class="input-text input-woo_user regular-text" name="username" id="woo_user" value="" /></td>
  153. </tr>
  154. <tr>
  155. <th scope="row"><label for="password"><?php _e( 'WooThemes Password', 'wooslider' ); ?>:</label></th>
  156. <td><input type="password" class="input-text input-woo_pass regular-text" name="password" id="woo_pass" value="" /></td>
  157. </tr>
  158. </tbody>
  159. </table>
  160. </fieldset>
  161. <fieldset>
  162. <p class="submit">
  163. <button type="submit" name="woo_login" id="woo_login" class="button-primary"><?php _e( 'Login', 'wooslider' ); ?></button>
  164. </p>
  165. <input type="hidden" name="woo-action" value="woo-plugin-updater-login" />
  166. <input type="hidden" name="page" value="woo-plugin-updater" />
  167. </fieldset>
  168. </form>
  169. </div><!--/.wrap-->
  170. <?php
  171. } // End admin_screen()
  172. /**
  173. * admin_screen_logic function.
  174. *
  175. * @access public
  176. * @return void
  177. */
  178. function admin_screen_logic () {
  179. $is_valid = $this->authenticate( $_POST['username'], $_POST['password'] );
  180. if ( $is_valid ) {
  181. $username = trim( strip_tags( $_POST['username'] ) );
  182. $password = md5( trim( strip_tags( $_POST['password'] ) ) );
  183. update_option( $this->plugin_prefix . 'username', $username );
  184. update_option( $this->plugin_prefix . 'password', $password );
  185. // Refresh the login screen.
  186. wp_redirect( admin_url( 'index.php?page=' . $this->plugin_token ) ); exit;
  187. } else {
  188. // Refresh the login screen with an error message.
  189. wp_redirect( admin_url( 'index.php?page=' . $this->plugin_token . '&type=error' ) ); exit;
  190. }
  191. } // End admin_screen_logic()
  192. /**
  193. * admin_notice function.
  194. *
  195. * @access public
  196. * @return void
  197. */
  198. function admin_notice () {
  199. $notice = '';
  200. $this->load_user_data();
  201. // Admin notice for switching login details.
  202. if ( isset( $_GET['page'] ) && ( $_GET['page'] == $this->plugin_token ) ) {
  203. if ( $this->username != '' && $this->password != '' ) {
  204. $notice = '<div id="woo-plugin-updater-notice" class="updated fade">' . "\n";
  205. $notice .= '<p><strong>' . __( 'Switch WooThemes Account', 'wooslider' ) . '</strong></p><p>' . "\n";
  206. $notice .= sprintf( __( 'You are currently logged in as %1$s. To switch to a different WooThemes account, please fill in the login details below.', 'wooslider' ), '<strong>' . $this->username . '</strong>' );
  207. $notice .= "\n" . '</p>' . "\n";
  208. $notice .= '<p><a href="' . admin_url( 'update-core.php' ) . '">' . __( 'Update your Plugins', 'wooslider' ) . ' &rarr;</a></p>' . "\n";
  209. $notice .= '</div>' . "\n";
  210. }
  211. if ( isset( $_GET['type'] ) && ( $_GET['type'] == 'error' ) ) {
  212. $notice .= '<div id="woo-plugin-updater-error-notice" class="error fade">' . "\n";
  213. $notice .= '<p><strong>' . __( 'Login Error', 'wooslider' ) . '</strong></p><p>' . "\n";
  214. $notice .= __( 'The login details supplied are invalid. Please try again.', 'wooslider' );
  215. $notice .= "\n" . '</p>' . "\n";
  216. $notice .= '</div>' . "\n";
  217. }
  218. }
  219. // Admin notice for if no login details are set, to notify the user.
  220. if ( ( ! isset( $_GET['page'] ) || ( isset( $_GET['page'] ) && ( $_GET['page'] != $this->plugin_token ) ) ) && ( $this->username == '' || $this->password == '' ) && ( get_option( $this->plugin_prefix . 'hide-admin-notice', false ) != true ) ) {
  221. $notice = '<div id="woo-plugin-updater-notice" class="updated fade">' . "\n";
  222. $notice .= '<p class="alignleft"><strong>' . __( 'Enable WooThemes Plugin Updates.', 'wooslider' ) . '</strong> ' . "\n";
  223. $notice .= sprintf( __( 'Please <a href="%1$s">login</a> to enable automatic plugin updates.', 'wooslider' ), 'index.php?page=' . $this->plugin_token );
  224. $notice .= "\n" . '</p>' . "\n";
  225. $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  226. $delimiter = '?';
  227. if ( stristr( $url, '?' ) == true ) {
  228. $delimiter = '&';
  229. }
  230. $url .= $delimiter . 'woo-hide-updatenotice=true';
  231. $notice .= '<p class="alignright submitbox"><a href="' . $url . '" class="submitdelete">' . __( 'Hide This Message', 'wooslider' ) . '</a></p>' . "\n";
  232. $notice .= '<br class="clear" />';
  233. $notice .= '</div>' . "\n";
  234. }
  235. $notice = '';
  236. if ( $notice != '' ) {
  237. echo $notice;
  238. }
  239. } // End admin_notice()
  240. /**
  241. * update_check function.
  242. *
  243. * @access public
  244. * @param object $transient
  245. * @return object $transient
  246. */
  247. function update_check ( $transient ) {
  248. // Check if the transient contains the 'checked' information
  249. // If no, just return its value without hacking it
  250. if( empty( $transient->checked ) )
  251. return $transient;
  252. // The transient contains the 'checked' information
  253. // Now append to it information form your own API
  254. $plugin_slug = $this->plugin_base;
  255. // Get the user's data.
  256. $this->load_user_data();
  257. // POST data to send to your API
  258. $args = array(
  259. 'action' => 'update-check',
  260. 'plugin_name' => $plugin_slug,
  261. 'version' => $transient->checked[$plugin_slug],
  262. 'api_key' => $this->api_key,
  263. 'username' => $this->username,
  264. 'password' => $this->password
  265. );
  266. // Send request checking for an update
  267. $response = $this->prepare_request( $args );
  268. // If response is false, don't alter the transient
  269. if( false !== $response ) {
  270. $transient->response[$plugin_slug] = $response;
  271. }
  272. return $transient;
  273. } // End update_check()
  274. function plugin_information ( $false, $action, $args ) {
  275. $plugin_slug = $this->plugin_base;
  276. $transient = get_site_transient( 'update_plugins' );
  277. // Check if this plugins API is about this plugin
  278. if( $args->slug != $plugin_slug ) {
  279. return false;
  280. }
  281. // POST data to send to your API
  282. $args = array(
  283. 'action' => 'plugin_information',
  284. 'plugin_name' => $plugin_slug,
  285. 'version' => $transient->checked[$plugin_slug],
  286. 'api_key' => $this->api_key,
  287. 'username' => $this->username,
  288. 'password' => $this->password
  289. );
  290. // Send request for detailed information
  291. $response = $this->prepare_request( $args );
  292. return $response;
  293. } // End plugin_information()
  294. /**
  295. * prepare_request function.
  296. *
  297. * @access public
  298. * @param array $args
  299. * @return object $response or boolean false
  300. */
  301. function prepare_request( $args ) {
  302. // Send request
  303. $request = wp_remote_post( $this->api_url, array( 'body' => $args ) );
  304. // Make sure the request was successful
  305. if( is_wp_error( $request )
  306. or
  307. wp_remote_retrieve_response_code( $request ) != 200
  308. ) {
  309. // Request failed
  310. return false;
  311. }
  312. // Read server response, which should be an object
  313. $response = maybe_unserialize( wp_remote_retrieve_body( $request ) );
  314. if( is_object( $response ) ) {
  315. return $response;
  316. } else {
  317. // Unexpected response
  318. return false;
  319. }
  320. } // End prepare_request()
  321. /**
  322. * get_api_data function.
  323. *
  324. * @description Return the contents of a URL using wp_remote_post().
  325. * @access public
  326. * @param array $params (default: array())
  327. * @return string $data
  328. */
  329. function get_api_data ( $params = array() ) {
  330. $response = wp_remote_post( $this->api_url, array(
  331. 'method' => 'POST',
  332. 'timeout' => 45,
  333. 'redirection' => 5,
  334. 'httpversion' => '1.0',
  335. 'blocking' => true,
  336. 'headers' => array(),
  337. 'body' => $params,
  338. 'cookies' => array(),
  339. 'sslverify' => false
  340. )
  341. );
  342. if( is_wp_error( $response ) ) {
  343. $data = '<?xml version="1.0"?><status>FAILED</status>';
  344. } else {
  345. $data = $response['body'];
  346. }
  347. try {
  348. $xmlobj = new SimpleXmlElement( $data );
  349. } catch ( Exception $e ) {
  350. $data = '<?xml version="1.0"?><status>FAILED</status>';
  351. $xmlobj = new SimpleXmlElement( $data );
  352. }
  353. return $xmlobj;
  354. } // End get_api_data()
  355. /**
  356. * instance_exists function.
  357. *
  358. * @access public
  359. * @return void
  360. */
  361. function instance_exists () {
  362. global $submenu;
  363. $exists = false;
  364. // Check if the menu item already exists.
  365. if ( isset( $submenu['index.php'] ) && is_array( $submenu['index.php'] ) ) {
  366. foreach ( $submenu['index.php'] as $k => $v ) {
  367. if ( isset( $v[2] ) && ( $v[2] == $this->plugin_token ) ) {
  368. $exists = true;
  369. break;
  370. }
  371. }
  372. }
  373. return $exists;
  374. } // End instance_exists()
  375. /**
  376. * hide_admin_notice function.
  377. *
  378. * @access public
  379. * @return void
  380. */
  381. function hide_admin_notice () {
  382. if ( isset( $_GET['woo-hide-updatenotice'] ) && ( $_GET['woo-hide-updatenotice'] == 'true' ) ) {
  383. update_option( $this->plugin_prefix . 'hide-admin-notice', true );
  384. $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  385. $url = str_replace( 'woo-hide-updatenotice=true', '', $url );
  386. wp_redirect( $url ); exit;
  387. }
  388. } // End hide_admin_notice()
  389. } // End Class
  390. ?>