PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/vendors/edd-software-licensing/theme-license-admin.php

https://gitlab.com/CueFox/cf-utility-pro
PHP | 448 lines | 262 code | 75 blank | 111 comment | 59 complexity | bc403e317243414f1b74e87bd316d5c3 MD5 | raw file
  1. <?php
  2. /**
  3. * Theme updater admin page and functions.
  4. *
  5. * @package EDD Sample Theme
  6. */
  7. class EDD_Theme_Updater_Admin {
  8. /**
  9. * Variables required for the theme updater
  10. *
  11. * @since 1.0.0
  12. * @type string
  13. */
  14. protected $remote_api_url = null;
  15. protected $theme_slug = null;
  16. protected $version = null;
  17. protected $author = null;
  18. protected $download_id = null;
  19. protected $renew_url = null;
  20. protected $strings = null;
  21. /**
  22. * Initialize the class.
  23. *
  24. * @since 1.0.0
  25. */
  26. function __construct( $config = array(), $strings = array() ) {
  27. $config = wp_parse_args( $config, array(
  28. 'remote_api_url' => 'http://easydigitaldownloads.com',
  29. 'theme_slug' => get_template(),
  30. 'item_name' => '',
  31. 'license' => '',
  32. 'version' => '',
  33. 'author' => '',
  34. 'download_id' => '',
  35. 'renew_url' => ''
  36. ) );
  37. // Set config arguments
  38. $this->remote_api_url = $config['remote_api_url'];
  39. $this->item_name = $config['item_name'];
  40. $this->theme_slug = sanitize_key( $config['theme_slug'] );
  41. $this->version = $config['version'];
  42. $this->author = $config['author'];
  43. $this->download_id = $config['download_id'];
  44. $this->renew_url = $config['renew_url'];
  45. // Populate version fallback
  46. if ( '' == $config['version'] ) {
  47. $theme = wp_get_theme( $this->theme_slug );
  48. $this->version = $theme->get( 'Version' );
  49. }
  50. // Strings passed in from the updater config
  51. $this->strings = $strings;
  52. add_action( 'admin_init', array( $this, 'updater' ) );
  53. add_action( 'admin_init', array( $this, 'register_option' ) );
  54. add_action( 'admin_init', array( $this, 'license_action' ) );
  55. add_action( 'admin_menu', array( $this, 'license_menu' ) );
  56. add_action( 'update_option_' . $this->theme_slug . '_license_key', array( $this, 'activate_license' ), 10, 2 );
  57. add_filter( 'http_request_args', array( $this, 'disable_wporg_request' ), 5, 2 );
  58. }
  59. /**
  60. * Creates the updater class.
  61. *
  62. * since 1.0.0
  63. */
  64. function updater() {
  65. /* If there is no valid license key status, don't allow updates. */
  66. if ( get_option( $this->theme_slug . '_license_key_status', false) != 'valid' ) {
  67. return;
  68. }
  69. if ( !class_exists( 'EDD_Theme_Updater' ) ) {
  70. // Load our custom theme updater
  71. include( dirname( __FILE__ ) . '/theme-updater-class.php' );
  72. }
  73. new EDD_Theme_Updater(
  74. array(
  75. 'remote_api_url' => $this->remote_api_url,
  76. 'version' => $this->version,
  77. 'license' => trim( get_option( $this->theme_slug . '_license_key' ) ),
  78. 'item_name' => $this->item_name,
  79. 'author' => $this->author
  80. ),
  81. $this->strings
  82. );
  83. }
  84. /**
  85. * Adds a menu item for the theme license under the appearance menu.
  86. *
  87. * since 1.0.0
  88. */
  89. function license_menu() {
  90. $strings = $this->strings;
  91. add_theme_page(
  92. $strings['theme-license'],
  93. $strings['theme-license'],
  94. 'manage_options',
  95. $this->theme_slug . '-license',
  96. array( $this, 'license_page' )
  97. );
  98. }
  99. /**
  100. * Outputs the markup used on the theme license page.
  101. *
  102. * since 1.0.0
  103. */
  104. function license_page() {
  105. $strings = $this->strings;
  106. $license = trim( get_option( $this->theme_slug . '_license_key' ) );
  107. $status = get_option( $this->theme_slug . '_license_key_status', false );
  108. // Checks license status to display under license key
  109. if ( ! $license ) {
  110. $message = $strings['enter-key'];
  111. } else {
  112. // delete_transient( $this->theme_slug . '_license_message' );
  113. if ( ! get_transient( $this->theme_slug . '_license_message', false ) ) {
  114. set_transient( $this->theme_slug . '_license_message', $this->check_license(), ( 60 * 60 * 24 ) );
  115. }
  116. $message = get_transient( $this->theme_slug . '_license_message' );
  117. }
  118. ?>
  119. <div class="wrap">
  120. <h2><?php echo $strings['theme-license'] ?></h2>
  121. <form method="post" action="options.php">
  122. <?php settings_fields( $this->theme_slug . '-license' ); ?>
  123. <table class="form-table">
  124. <tbody>
  125. <tr valign="top">
  126. <th scope="row" valign="top">
  127. <?php echo $strings['license-key']; ?>
  128. </th>
  129. <td>
  130. <input id="<?php echo $this->theme_slug; ?>_license_key" name="<?php echo $this->theme_slug; ?>_license_key" type="text" class="regular-text" value="<?php echo esc_attr( $license ); ?>" />
  131. <p class="description">
  132. <?php echo $message; ?>
  133. </p>
  134. </td>
  135. </tr>
  136. <?php if ( $license ) { ?>
  137. <tr valign="top">
  138. <th scope="row" valign="top">
  139. <?php echo $strings['license-action']; ?>
  140. </th>
  141. <td>
  142. <?php
  143. wp_nonce_field( $this->theme_slug . '_nonce', $this->theme_slug . '_nonce' );
  144. if ( 'valid' == $status ) { ?>
  145. <input type="submit" class="button-secondary" name="<?php echo $this->theme_slug; ?>_license_deactivate" value="<?php esc_attr_e( $strings['deactivate-license'] ); ?>"/>
  146. <?php } else { ?>
  147. <input type="submit" class="button-secondary" name="<?php echo $this->theme_slug; ?>_license_activate" value="<?php esc_attr_e( $strings['activate-license'] ); ?>"/>
  148. <?php }
  149. ?>
  150. </td>
  151. </tr>
  152. <?php } ?>
  153. </tbody>
  154. </table>
  155. <?php submit_button(); ?>
  156. </form>
  157. <?php
  158. }
  159. /**
  160. * Registers the option used to store the license key in the options table.
  161. *
  162. * since 1.0.0
  163. */
  164. function register_option() {
  165. register_setting(
  166. $this->theme_slug . '-license',
  167. $this->theme_slug . '_license_key',
  168. array( $this, 'sanitize_license' )
  169. );
  170. }
  171. /**
  172. * Sanitizes the license key.
  173. *
  174. * since 1.0.0
  175. *
  176. * @param string $new License key that was submitted.
  177. * @return string $new Sanitized license key.
  178. */
  179. function sanitize_license( $new ) {
  180. $old = get_option( $this->theme_slug . '_license_key' );
  181. if ( $old && $old != $new ) {
  182. // New license has been entered, so must reactivate
  183. delete_option( $this->theme_slug . '_license_key_status' );
  184. delete_transient( $this->theme_slug . '_license_message' );
  185. }
  186. return $new;
  187. }
  188. /**
  189. * Makes a call to the API.
  190. *
  191. * @since 1.0.0
  192. *
  193. * @param array $api_params to be used for wp_remote_get.
  194. * @return array $response decoded JSON response.
  195. */
  196. function get_api_response( $api_params ) {
  197. // Call the custom API.
  198. $response = wp_remote_get(
  199. add_query_arg( $api_params, $this->remote_api_url ),
  200. array( 'timeout' => 15, 'sslverify' => false )
  201. );
  202. // Make sure the response came back okay.
  203. if ( is_wp_error( $response ) ) {
  204. return false;
  205. }
  206. $response = json_decode( wp_remote_retrieve_body( $response ) );
  207. return $response;
  208. }
  209. /**
  210. * Activates the license key.
  211. *
  212. * @since 1.0.0
  213. */
  214. function activate_license() {
  215. $license = trim( get_option( $this->theme_slug . '_license_key' ) );
  216. // Data to send in our API request.
  217. $api_params = array(
  218. 'edd_action' => 'activate_license',
  219. 'license' => $license,
  220. 'item_name' => urlencode( $this->item_name )
  221. );
  222. $license_data = $this->get_api_response( $api_params );
  223. // $response->license will be either "active" or "inactive"
  224. if ( $license_data && isset( $license_data->license ) ) {
  225. update_option( $this->theme_slug . '_license_key_status', $license_data->license );
  226. delete_transient( $this->theme_slug . '_license_message' );
  227. }
  228. }
  229. /**
  230. * Deactivates the license key.
  231. *
  232. * @since 1.0.0
  233. */
  234. function deactivate_license() {
  235. // Retrieve the license from the database.
  236. $license = trim( get_option( $this->theme_slug . '_license_key' ) );
  237. // Data to send in our API request.
  238. $api_params = array(
  239. 'edd_action' => 'deactivate_license',
  240. 'license' => $license,
  241. 'item_name' => urlencode( $this->item_name )
  242. );
  243. $license_data = $this->get_api_response( $api_params );
  244. // $license_data->license will be either "deactivated" or "failed"
  245. if ( $license_data && ( $license_data->license == 'deactivated' ) ) {
  246. delete_option( $this->theme_slug . '_license_key_status' );
  247. delete_transient( $this->theme_slug . '_license_message' );
  248. }
  249. }
  250. /**
  251. * Constructs a renewal link
  252. *
  253. * @since 1.0.0
  254. */
  255. function get_renewal_link() {
  256. // If a renewal link was passed in the config, use that
  257. if ( '' != $this->renew_url ) {
  258. return $this->renew_url;
  259. }
  260. // If download_id was passed in the config, a renewal link can be constructed
  261. $license_key = trim( get_option( $this->theme_slug . '_license_key', false ) );
  262. if ( '' != $this->download_id && $license_key ) {
  263. $url = esc_url( $this->remote_api_url );
  264. $url .= '/checkout/?edd_license_key=' . $license_key . '&download_id=' . $this->download_id;
  265. return $url;
  266. }
  267. // Otherwise return the remote_api_url
  268. return $this->remote_api_url;
  269. }
  270. /**
  271. * Checks if a license action was submitted.
  272. *
  273. * @since 1.0.0
  274. */
  275. function license_action() {
  276. if ( isset( $_POST[ $this->theme_slug . '_license_activate' ] ) ) {
  277. if ( check_admin_referer( $this->theme_slug . '_nonce', $this->theme_slug . '_nonce' ) ) {
  278. $this->activate_license();
  279. }
  280. }
  281. if ( isset( $_POST[$this->theme_slug . '_license_deactivate'] ) ) {
  282. if ( check_admin_referer( $this->theme_slug . '_nonce', $this->theme_slug . '_nonce' ) ) {
  283. $this->deactivate_license();
  284. }
  285. }
  286. }
  287. /**
  288. * Checks if license is valid and gets expire date.
  289. *
  290. * @since 1.0.0
  291. *
  292. * @return string $message License status message.
  293. */
  294. function check_license() {
  295. $license = trim( get_option( $this->theme_slug . '_license_key' ) );
  296. $strings = $this->strings;
  297. $api_params = array(
  298. 'edd_action' => 'check_license',
  299. 'license' => $license,
  300. 'item_name' => urlencode( $this->item_name )
  301. );
  302. $license_data = $this->get_api_response( $api_params );
  303. // If response doesn't include license data, return
  304. if ( !isset( $license_data->license ) ) {
  305. $message = $strings['license-unknown'];
  306. return $message;
  307. }
  308. // Get expire date
  309. $expires = false;
  310. if ( isset( $license_data->expires ) ) {
  311. $expires = date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires ) );
  312. $renew_link = '<a href="' . esc_url( $this->get_renewal_link() ) . '" target="_blank">' . $strings['renew'] . '</a>';
  313. }
  314. // Get site counts
  315. $site_count = $license_data->site_count;
  316. $license_limit = $license_data->license_limit;
  317. // If unlimited
  318. if ( 0 == $license_limit ) {
  319. $license_limit = $strings['unlimited'];
  320. }
  321. if ( $license_data->license == 'valid' ) {
  322. $message = $strings['license-key-is-active'] . ' ';
  323. if ( $expires ) {
  324. $message .= sprintf( $strings['expires%s'], $expires ) . ' ';
  325. }
  326. if ( $site_count && $license_limit ) {
  327. $message .= sprintf( $strings['%1$s/%2$-sites'], $site_count, $license_limit );
  328. }
  329. } else if ( $license_data->license == 'expired' ) {
  330. if ( $expires ) {
  331. $message = sprintf( $strings['license-key-expired-%s'], $expires );
  332. } else {
  333. $message = $strings['license-key-expired'];
  334. }
  335. if ( $renew_link ) {
  336. $message .= ' ' . $renew_link;
  337. }
  338. } else if ( $license_data->license == 'invalid' ) {
  339. $message = $strings['license-keys-do-not-match'];
  340. } else if ( $license_data->license == 'inactive' ) {
  341. $message = $strings['license-is-inactive'];
  342. } else if ( $license_data->license == 'disabled' ) {
  343. $message = $strings['license-key-is-disabled'];
  344. } else if ( $license_data->license == 'site_inactive' ) {
  345. // Site is inactive
  346. $message = $strings['site-is-inactive'];
  347. } else {
  348. $message = $strings['license-status-unknown'];
  349. }
  350. return $message;
  351. }
  352. /**
  353. * Disable requests to wp.org repository for this theme.
  354. *
  355. * @since 1.0.0
  356. */
  357. function disable_wporg_request( $r, $url ) {
  358. // If it's not a theme update request, bail.
  359. if ( 0 !== strpos( $url, 'https://api.wordpress.org/themes/update-check/1.1/' ) ) {
  360. return $r;
  361. }
  362. // Decode the JSON response
  363. $themes = json_decode( $r['body']['themes'] );
  364. // Remove the active parent and child themes from the check
  365. $parent = get_option( 'template' );
  366. $child = get_option( 'stylesheet' );
  367. unset( $themes->themes->$parent );
  368. unset( $themes->themes->$child );
  369. // Encode the updated JSON response
  370. $r['body']['themes'] = json_encode( $themes );
  371. return $r;
  372. }
  373. }