Advertisement
Guest User

license.php

a guest
May 23rd, 2021
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.48 KB | None | 0 0
  1. <?php
  2. namespace Bricks;
  3.  
  4. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  5.  
  6. class License {
  7.  
  8. public static $license_key = '';
  9. public static $license_status = '';
  10. public static $remote_base_url = 'https://bricksbuilder.io/api/commerce/';
  11.  
  12. public function __construct() {
  13. self::$license_key = get_option( 'bricks_license_key', false );
  14.  
  15. add_filter( 'pre_set_site_transient_update_themes', [$this, 'check_for_update'] );
  16.  
  17. add_action( 'wp_ajax_bricks_activate_license', [$this, 'activate_license'] );
  18. add_action( 'wp_ajax_bricks_deactivate_license', [$this, 'deactivate_license'] );
  19.  
  20. add_action( 'admin_notices', [$this, 'admin_notices_license_activation'] );
  21. add_action( 'admin_notices', [$this, 'admin_notices_license_mismatch'] );
  22. }
  23.  
  24. /**
  25. * Check remotely if newer version of Bricks is available
  26. *
  27. * @param $transient Transient for WordPress theme updates.
  28. * @return void
  29. */
  30. public static function check_for_update( $transient ) {
  31. // 'checked' is an array with all installed themes and their version numbers
  32. if ( empty( $transient->checked ) ) {
  33. return $transient;
  34. }
  35.  
  36. $license_key = self::$license_key;
  37.  
  38. if ( ! $license_key ) {
  39. return $transient;
  40. }
  41.  
  42. // Installed theme data
  43. $theme_data = wp_get_theme();
  44. $installed_version = $theme_data->Version;
  45.  
  46. // Check if Bricks is parent theme (i.e. Bricks child theme in use)
  47. if ( wp_get_theme()->parent() ) {
  48. $installed_version = wp_get_theme()->parent()->get( 'Version' );
  49. }
  50.  
  51. // Build theme update request URL with license_key and domain parameters
  52. $update_url = add_query_arg(
  53. [
  54. 'license_key' => $license_key,
  55. 'domain' => get_site_url(),
  56. 'time' => time(), // To avoid caching remote response
  57. ],
  58. self::$remote_base_url .'download/get_update_data'
  59. );
  60.  
  61. $request = Helpers::remote_get( $update_url );
  62.  
  63. // Check if remote GET request has been successful (better than using is_wp_error)
  64. if ( wp_remote_retrieve_response_code( $request ) !== 200 ) {
  65. return $transient;
  66. }
  67.  
  68. $request = json_decode( wp_remote_retrieve_body( $request ), true );
  69.  
  70. // Check remotely if user newer version of Bricks is available
  71. $latest_version = isset( $request['new_version'] ) ? $request['new_version'] : $installed_version;
  72. $newer_version_available = version_compare( $latest_version, $installed_version, '>' );
  73.  
  74. if ( ! $newer_version_available ) {
  75. return $transient;
  76. }
  77.  
  78. // Save Bricks-specific update data in transient
  79. $transient->response['bricks'] = $request;
  80.  
  81. return $transient;
  82. }
  83.  
  84. /**
  85. * Check license status when loading builder
  86. *
  87. * @see template_redirect
  88. */
  89. public static function license_is_valid() {
  90. return true;
  91. // Skip license check for builder iframe (check happens in builder panel)
  92. if ( bricks_is_builder_iframe() ) {
  93. return true;
  94. }
  95.  
  96. // Return: No license key found in db options table
  97. if ( ! self::$license_key ) {
  98. return false;
  99. }
  100.  
  101. // Valid license status'
  102. return in_array( self::get_license_status(), [
  103. 'active', // Active license
  104. 'processed', // Order processed
  105. 'past_due', // Payment past due (subscription)
  106. 'error_remote', // Remote server error (bricksbuilder.io)
  107. ] );
  108. }
  109.  
  110. /**
  111. * Get license status (stored locally in transient: bricks_license_status)
  112. *
  113. * If transient expired (after 12h) then get it remotely from Bricks server.
  114. *
  115. * @return array
  116. */
  117. public static function get_license_status() {
  118. return 'website_active';
  119. $license_key = self::$license_key;
  120.  
  121. if ( ! $license_key ) {
  122. return false;
  123. }
  124.  
  125. // Check license transient (expires after 12 hours)
  126. $transient_timeout = get_option ( '_transient_timeout_bricks_license_status' );
  127. $transient_timeout_in_hours = ( intval( $transient_timeout ) - time() ) / 60 / 60;
  128.  
  129. $license_status = get_transient( 'bricks_license_status' );
  130.  
  131. // No valid transient found: Get license status remotely
  132. if ( ! $transient_timeout || $transient_timeout_in_hours > 12 || false === $license_status ) {
  133. delete_transient( 'bricks_license_status' );
  134.  
  135. $response = Helpers::remote_get( add_query_arg( [
  136. 'license_key' => $license_key,
  137. 'site' => get_site_url(),
  138. 'time' => time(), // Avoid getting a cached remote response
  139. ],
  140. self::$remote_base_url . 'license/get_status'
  141. ) );
  142.  
  143. if ( is_wp_error( $response ) ) {
  144. $license_status = 'error_remote';
  145. }
  146.  
  147. $response = json_decode( wp_remote_retrieve_body( $response ), true );
  148.  
  149. if ( isset( $response['status'] ) ) {
  150. $license_status = $response['status'];
  151. }
  152.  
  153. // Save license status in transient (expires after 12 hours)
  154. self::set_license_status( $license_status );
  155. }
  156.  
  157. // Invalid license: Activate license on server (avoid having to deactivate & reactivate license for cloned sites, etc.)
  158. $invalid_license = ! in_array( $license_status, [
  159. 'active', // Active license
  160. 'processed', // Order processed
  161. 'past_due', // Payment past due (subscription)
  162. 'error_remote', // Remote server error (bricksbuilder.io)
  163. ] );
  164.  
  165. if ( $invalid_license ) {
  166. $license_status = self::activate_license();
  167.  
  168. return $license_status;
  169. }
  170.  
  171. return $license_status;
  172. }
  173.  
  174. /**
  175. * Save license status in transient (expires after 12 hours)
  176. */
  177. public static function set_license_status( $license_status ) {
  178. $expiration_time = 12 * HOUR_IN_SECONDS;
  179.  
  180. set_transient( 'bricks_license_status', $license_status, $expiration_time );
  181. }
  182.  
  183. /**
  184. * Activate license under "Bricks > License" (AJAX call on "Activate license" click)
  185. *
  186. * Also runs via PHP in 'get_license_status' to avoid having to deactivate & reactivate license (when cloning staging site, etc.)
  187. *
  188. * @return array
  189. */
  190. public static function activate_license() {
  191. $license_key = self::$license_key;
  192. $is_ajax = bricks_is_ajax_call();
  193.  
  194. if ( $is_ajax ) {
  195. Ajax::verify_request();
  196.  
  197. $license_key = isset( $_POST['licenseKey'] ) && ! empty( $_POST['licenseKey'] ) ? trim( $_POST['licenseKey'] ) : false;
  198.  
  199. if ( ! $license_key ) {
  200. wp_send_json_error( [
  201. 'message' => esc_html__( 'No license key provided.', 'bricks' )
  202. ] );
  203. }
  204. }
  205.  
  206. // Return: No license key found/submitted
  207. if ( ! $license_key ) {
  208. return;
  209. }
  210.  
  211. // Activate license key
  212. $response = wp_remote_post(
  213. self::$remote_base_url . 'license/activate_license',
  214. [
  215. // 'sslverify' => false,
  216. 'timeout' => 40,
  217. 'body' => [
  218. 'license_key' => $license_key,
  219. 'site' => get_site_url(),
  220. 'version' => BRICKS_VERSION,
  221. ],
  222. ]
  223. );
  224.  
  225. // Check for remote error(s)
  226. if ( is_wp_error( $response ) ) {
  227. if ( $is_ajax ) {
  228. wp_send_json_error( ['message' => $response->get_error_message()] );
  229. } else {
  230. return;
  231. }
  232. }
  233.  
  234. if ( wp_remote_retrieve_response_code( $response ) !== 200 ) {
  235. if ( $is_ajax ) {
  236. wp_send_json_error( ['message' => wp_remote_retrieve_response_message( $response )] );
  237. } else {
  238. return;
  239. }
  240. }
  241.  
  242. $response = json_decode( wp_remote_retrieve_body( $response ), true );
  243. $license_status = isset( $response['status'] ) ? $response['status'] : false;
  244.  
  245. // Return remote error
  246. if ( $response['type'] === 'error' && isset( $response['message'] ) ) {
  247. if ( $is_ajax ) {
  248. wp_send_json_error( ['message' => $response['message']] );
  249. } else {
  250. return;
  251. }
  252. }
  253.  
  254. // Return if no license status was sent back
  255. if ( ! $license_status ) {
  256. if ( $is_ajax ) {
  257. wp_send_json_error( ['message' => esc_html__( 'No license for provided license key found.', 'bricks' )] );
  258. } else {
  259. return;
  260. }
  261. }
  262.  
  263. // Save license key in db options table
  264. update_option( 'bricks_license_key', $license_key );
  265.  
  266. // Save license status in transient (expires after 12 hours)
  267. self::set_license_status( $license_status );
  268.  
  269. // Download remote templates data from server and store in db options table
  270. Templates::get_remote_templates_data();
  271.  
  272. if ( $is_ajax ) {
  273. wp_send_json_success( [
  274. 'message' => esc_html__( 'License activated.', 'bricks' ),
  275. 'status' => $license_status,
  276. ] );
  277. } else {
  278. return $license_status;
  279. }
  280. }
  281.  
  282. /**
  283. * Deactivate license
  284. *
  285. * @return null
  286. *
  287. * @since 0.1.0
  288. */
  289. public static function deactivate_license() {
  290. Ajax::verify_request();
  291.  
  292. // Activate license key
  293. $response = wp_remote_post(
  294. self::$remote_base_url . 'license/deactivate_license',
  295. [
  296. // 'sslverify' => false,
  297. 'timeout' => 40,
  298. 'body' => [
  299. 'license_key' => self::$license_key,
  300. 'site' => get_site_url(),
  301. ],
  302. ]
  303. );
  304.  
  305. delete_option( 'bricks_license_key' );
  306. delete_transient( 'bricks_license_status' );
  307. }
  308.  
  309. /**
  310. * Admin notice to activate license
  311. *
  312. * @return null/string
  313. */
  314. public static function admin_notices_license_activation() {
  315. // Show license key admin notice only to user roles which are allowed to use the builder
  316. if ( ! Capabilities::current_user_can_use_builder() ) {
  317. return;
  318. }
  319.  
  320. // Don't show license admin notice on license page itself
  321. if ( get_current_screen()->id === 'bricks_page_bricks-license' ) {
  322. return;
  323. }
  324.  
  325. // Check if license has been activated by checking for license key
  326. $license_key = self::$license_key;
  327.  
  328. // Check: License activated (local)
  329. if ( isset( $license_key ) && ! empty( $license_key ) ) {
  330. return;
  331. }
  332. ?>
  333. <div class="notice notice-info notice-license-activation">
  334. <div class="content-wrapper">
  335. <h4 class="title"><?php esc_html_e( 'Welcome to Bricks', 'bricks' ); ?></h4>
  336. <p><?php echo esc_html__( 'Activate your license to edit with Bricks, receive one-click updates, and access to all community templates.', 'bricks' ); ?></p>
  337. </div>
  338.  
  339. <a class="button button-primary" href="<?php echo esc_url( BRICKS_ADMIN_PAGE_URL_LICENSE ); ?>"><?php esc_html_e( 'Activate License', 'bricks' ); ?></a>
  340. </div>
  341. <?php
  342. }
  343.  
  344. /**
  345. * Admin notice to activate license
  346. *
  347. * @return null/string
  348. */
  349. public static function admin_notices_license_mismatch() {
  350. // Show license key admin notice only to user roles which are allowed to use the builder
  351. if ( ! Capabilities::current_user_can_use_builder() ) {
  352. return;
  353. }
  354.  
  355. // Don't show license admin notice on license page itself
  356. if ( get_current_screen()->id === 'bricks_page_bricks-license' ) {
  357. return;
  358. }
  359.  
  360. // Check for license status 'website_inactive'
  361. $license_status = get_transient( 'bricks_license_status' );
  362.  
  363. $license_error_title = false;
  364. $license_error_description = false;
  365.  
  366. switch ( $license_status ) {
  367. case 'license_key_invalid':
  368. $license_error_title = esc_html__( 'Error: Invalid license key', 'bricks' );
  369. $license_error_description = esc_html__( 'Your provided license key is invalid. Please deactive and then reactivate your license.', 'bricks' );
  370. break;
  371.  
  372. case 'website_inactive':
  373. $license_error_title = esc_html__( 'Error: License mismatch', 'bricks' );
  374. $license_error_description = esc_html__( 'Your website does not match your license key. Please deactive and then reactivate your license.', 'bricks' );
  375. break;
  376. }
  377.  
  378. if ( $license_error_title && $license_error_description ) { ?>
  379. <div class="notice notice-error notice-license-mismatch">
  380. <div class="content-wrapper">
  381. <h4 class="title"><?php echo esc_html( $license_error_title ); ?></h4>
  382. <p><?php echo esc_html( $license_error_description ); ?></p>
  383. </div>
  384. </div>
  385. <?php
  386. }
  387. }
  388.  
  389. }
  390.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement