PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/woocommerce-create-customer-on-order/includes/updates/cxthemes-plugin-update-checker.php

https://bitbucket.org/daniellbraun/wildernessatthesmokies
PHP | 281 lines | 180 code | 56 blank | 45 comment | 35 complexity | 60c8c285b39cc8392e21fc5ec77b45e3 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-3.0, GPL-2.0, GPL-3.0, MIT
  1. <?php
  2. // Include the 3rd party - Plugin Update Checker
  3. require 'plugin-update-checker/plugin-update-checker.php';
  4. if ( class_exists( 'CX_Create_Customer_On_Order_Plugin_Update_Checker' ) ) return;
  5. class CX_Create_Customer_On_Order_Plugin_Update_Checker {
  6. public $metadataUrl;
  7. public $pluginFile;
  8. public $slug;
  9. public $plugin_update_checker;
  10. public $cx_updater_version;
  11. public function __construct( $pluginFile, $slug ) {
  12. $this->pluginFile = $pluginFile;
  13. $this->slug = $slug;
  14. $this->cx_updater_version = '1'; // Used to increment the Enqueue Scripts to avoid caching.
  15. $metadataUrl = 'http://updates.cxthemes.com/'; // Live
  16. // Debugging:
  17. /*if ( strpos( $_SERVER['HTTP_HOST'], 'localhost' ) <= 7 )
  18. $metadataUrl = 'http://localhost/cxthemes-updates/'; // Local
  19. else
  20. $metadataUrl = 'http://updates.cxthemes.com/'; // Live*/
  21. // Merge in nthe query_vars.
  22. $metadataUrl = add_query_arg(
  23. array(
  24. 'action' => 'get_metadata',
  25. 'slug' => $slug,
  26. 'license_key' => get_option( "cx_{$this->slug}_envato_purchase_code" ),
  27. ),
  28. $metadataUrl
  29. );
  30. // Make sure there is never mailformed multiple slashed url check that will fail on some setups.
  31. // e.g. `http://updates.cxthemes.com//?test=test`
  32. $metadataUrl = preg_replace('/([^:])(\/{2,})/', '$1/', $metadataUrl);
  33. $this->metadataUrl = $metadataUrl;
  34. /**
  35. * Main: do the initial plugin update check.
  36. */
  37. $this->plugin_update_checker = PucFactory::buildUpdateChecker(
  38. $this->metadataUrl,
  39. $pluginFile,
  40. $slug
  41. );
  42. // Enqueue Scripts/Styles.
  43. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
  44. // Render purchase-code interfaces.
  45. add_action( 'admin_notices', array( $this, 'show_purchase_code_interface' ), 10 );
  46. add_filter( 'plugin_row_meta', array( $this, 'plugin_row_meta' ), 5, 2 );
  47. }
  48. /**
  49. * Enqueue CSS and Scripts
  50. *
  51. * @date 20-08-2014
  52. * @since 1.0
  53. */
  54. public function enqueue_scripts( $hook_suffix ) {
  55. global $pagenow;
  56. if ( 'plugins.php' == $pagenow || 'plugin-install.php' == $pagenow ) {
  57. wp_enqueue_style(
  58. 'cx-plugin-update-css',
  59. plugins_url( 'assets/updates.css', __FILE__ ),
  60. array(),
  61. $this->cx_updater_version
  62. );
  63. wp_enqueue_script(
  64. 'cx-plugin-update-js',
  65. plugins_url( 'assets/updates.js', __FILE__ ),
  66. array( 'jquery' ),
  67. $this->cx_updater_version
  68. );
  69. if ( isset( $_GET['plugin'] ) && $this->slug == $_GET['plugin'] ) {
  70. wp_localize_script(
  71. 'cx-plugin-update-js',
  72. 'cx_plugin_update_object',
  73. array(
  74. 'current_plugin' => $this->slug,
  75. 'current_plugin_is_licenced' => ( ( bool ) get_option( "cx_{$this->slug}_envato_purchase_code" ) ),
  76. 'update_button_text' => __( 'Please register on the Plugins page to get this update', 'create-customer-order' ),
  77. 'plugin_register_url' => esc_url( admin_url( "plugins.php?{$this->slug}-purchase-code" ) ),
  78. )
  79. );
  80. }
  81. }
  82. }
  83. // Purchase Code interface.
  84. function show_purchase_code_interface() {
  85. global $pagenow;
  86. if ( 'plugins.php' == $pagenow && isset( $_GET["{$this->slug}-purchase-code"] ) ) {
  87. // Get submitted purchase code.
  88. $submitted_code = isset( $_POST['cx-plugin-update-code'] ) ? trim( $_POST['cx-plugin-update-code'] ) : '' ;
  89. // Validation, on form submit.
  90. $notification = FALSE; // Control var.
  91. if ( isset( $_POST['cx-plugin-update-code-action'] ) && 'submit' == $_POST['cx-plugin-update-code-action'] ) {
  92. if ( 36 !== strlen( $submitted_code ) ) {
  93. /**
  94. * Submitted code is the wrong format.
  95. */
  96. $notification = 'wrong-format';
  97. delete_option( "cx_{$this->slug}_envato_purchase_code" );
  98. }
  99. else {
  100. /**
  101. * Submitted code is the correct format.
  102. */
  103. $temp_url = $this->metadataUrl;
  104. $temp_url = remove_query_arg( 'license_key', $temp_url );
  105. $temp_url = add_query_arg( array( 'license_key' => $submitted_code ), $temp_url );
  106. $this->plugin_update_checker = PucFactory::buildUpdateChecker(
  107. $temp_url, // New Local Update Checker
  108. $this->pluginFile,
  109. $this->slug
  110. );
  111. // It's required that we remove this filter so the word 'Check for Updates' doesn't appear twice.
  112. remove_filter( 'plugin_row_meta', array( $this->plugin_update_checker, 'addCheckForUpdatesLink'), 10, 2);
  113. // This must be checked with notices-hidden - @ - as it fails with a notice.
  114. @ $result = $this->plugin_update_checker->requestUpdate();
  115. // Debugging:
  116. // s( $result );
  117. if ( null == $result || ! isset( $result->download_url ) ) {
  118. /**
  119. * The update server is down :(
  120. */
  121. $notification = 'connection-error';
  122. delete_option( "cx_{$this->slug}_envato_purchase_code" );
  123. // s( $notification );
  124. }
  125. else {
  126. $parts = parse_url( $result->download_url );
  127. parse_str( $parts['query'], $query );
  128. if ( ! isset( $query['license_key'] ) ) {
  129. /**
  130. * We've checked and this purchase code is not valid- you don't own our product :(
  131. */
  132. $notification = 'invalid-purchase-code';
  133. delete_option( "cx_{$this->slug}_envato_purchase_code" );
  134. // s( $notification );
  135. }
  136. else {
  137. /**
  138. * Success, you own our product :)
  139. */
  140. $notification = 'success';
  141. update_option( "cx_{$this->slug}_envato_purchase_code", $submitted_code );
  142. // s( $notification );
  143. }
  144. }
  145. }
  146. }
  147. ?>
  148. <div class="cx-plugin-update-enter-code notice notice-success is-dismissibleZZ">
  149. <form method="post">
  150. <input type="hidden" name="cx-plugin-update-code-action" value="submit">
  151. <p><?php _e( 'To get automatic updates you need to input your valid purchase code, for our product, from Envato. The purchase code looks like: <code>f128a5a1-5a1c-4e4e-82ec-a12b3c4d5e6f</code> and should have been emailed to you when you bought our plugin, or you can retrieve it by logging into <a href="http://codecanyon.net/" target="_blank">CodeCanyon.net</a> go to Downloads > <em>Plugin Name</em> > Download > License certificate & purchase code.', 'create-customer-order' ); ?></p>
  152. <?php if ( $notification ) { ?>
  153. <!-- Validation Notifications -->
  154. <?php if ( 'wrong-format' == $notification ) { ?>
  155. <div class="cx-plugin-update-notification">
  156. <?php _e( 'That is not a valid purchase code format. It should look like: <code>f128a5a1-5a1c-4e4e-82ec-a12b3c4d5e6f</code>.', 'create-customer-order' ); ?>
  157. </div>
  158. <?php } else if ( 'connection-error' == $notification ) { ?>
  159. <div class="cx-plugin-update-notification">
  160. <?php _e( 'You are unable to connect to our update server at this time. Please check your connection, or try again later.', 'create-customer-order' ); ?>
  161. </div>
  162. <?php } else if ( 'invalid-purchase-code' == $notification ) { ?>
  163. <div class="cx-plugin-update-notification">
  164. <?php _e( "It doesn't seem like you have purchased our plugin yet. Please <a href='#'>purchase it from CodeCanyon</a> to get access to support and updates.", 'create-customer-order' ); ?>
  165. </div>
  166. <?php } else if ( 'success' == $notification ) { ?>
  167. <div class="cx-plugin-update-notification cx-plugin-update-notification-success">
  168. <?php _e( "Thanks. You're registered and can now receive updates.", 'create-customer-order' ); ?>
  169. </div>
  170. <?php } ?>
  171. <!-- / Validation Notifications -->
  172. <?php } ?>
  173. <div class="ec-plugin-update-row">
  174. <label><?php _e( 'Purchase Code', 'create-customer-order' ); ?></label>
  175. <input type="text" name="cx-plugin-update-code" value="<?php echo esc_attr( ( $submitted_code ? $submitted_code : get_option( "cx_{$this->slug}_envato_purchase_code" ) ) ); ?>" />&nbsp;
  176. <input type="submit" class="button" value="<?php _e( 'Save', 'create-customer-order' ); ?>" >
  177. </div>
  178. </form>
  179. </div>
  180. <?php
  181. }
  182. }
  183. /**
  184. * Show row meta on the plugin screen.
  185. *
  186. * @param mixed $links Plugin Row Meta
  187. * @param mixed $file Plugin Base file
  188. * @return array
  189. */
  190. public function plugin_row_meta( $links, $file ) {
  191. if ( $file == plugin_basename( $this->pluginFile ) ) {
  192. $purchase_code = get_option( "cx_{$this->slug}_envato_purchase_code" );
  193. $url = esc_url( admin_url( "plugins.php?{$this->slug}-purchase-code" ) );
  194. if ( ! $purchase_code ) {
  195. // It's required that we remove this filter so the word 'Check for Updates' doesn't appear twice.
  196. remove_filter( 'plugin_row_meta', array( $this->plugin_update_checker, 'addCheckForUpdatesLink'), 10, 2);
  197. $row_meta = array(
  198. 'support' => '<a class="cx-plugin-update-row-meta-register-for-updates" href="' . $url . '">' . __( 'Register for Updates', 'create-customer-order' ) . '</a>' . '<span class="cx-plugin-update-row-meta-register-for-updates-inline"><span class="cx-plugin-update-row-meta-divider"> | </span>' . sprintf( __( '<em><a href="%s">Register here</a> to get this update</em>', 'create-customer-order' ), $url ) . '</span>',
  199. );
  200. $links = array_merge( $links, $row_meta );
  201. }
  202. else {
  203. $row_meta = array(
  204. 'support' => '<a class="cx-plugin-update-row-meta-change-purchase-code" href="' . $url . '" title="' . esc_attr( __( '', 'create-customer-order' ) ) . '">' . __( 'Edit registration', 'create-customer-order' ) . '</a>',
  205. );
  206. $links = array_merge( $links, $row_meta );
  207. }
  208. }
  209. return (array) $links;
  210. }
  211. }