Няма описание

Licensing.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. class PUM_Licensing {
  6. /**
  7. * Is the license currently active?
  8. *
  9. * @param object|bool|null $license
  10. *
  11. * @return bool
  12. */
  13. public static function license_is_valid( $license = null ) {
  14. return self::get_status( $license ) === 'valid';
  15. }
  16. /**
  17. * @param object|bool|null $license
  18. *
  19. * @return bool
  20. */
  21. public static function has_license( $license = null ) {
  22. return ! empty( $license ) && is_object( $license );
  23. }
  24. /**
  25. * @param object|bool|null $license
  26. *
  27. * @return bool|false|int|string
  28. */
  29. public static function get_license_expiration( $license = null ) {
  30. if ( ! self::has_license( $license ) || ! self::license_is_valid( $license ) ) {
  31. return false;
  32. }
  33. return 'lifetime' === $license->expires ? 'lifetime' : strtotime( $license->expires, current_time( 'timestamp' ) );
  34. }
  35. /**
  36. * Get the current license status.
  37. *
  38. * @return string
  39. */
  40. public static function get_status( $license = null, $has_key = false ) {
  41. $status = $has_key ? 'inactive' : 'empty';
  42. if ( self::has_license( $license ) ) {
  43. // activate_license 'invalid' on anything other than valid, so if there was an error capture it
  44. if ( false === $license->success ) {
  45. $error = property_exists( $license, 'error' ) ? $license->error : $status;
  46. switch ( $error ) {
  47. case 'expired' :
  48. $status = 'expired';
  49. break;
  50. case 'revoked' :
  51. case 'missing' :
  52. case 'invalid' :
  53. case 'site_inactive' :
  54. case 'item_name_mismatch' :
  55. case 'no_activations_left':
  56. case 'license_not_activable':
  57. default :
  58. $status = 'error';
  59. break;
  60. }
  61. } else {
  62. $status = 'valid';
  63. }
  64. }
  65. return $status;
  66. }
  67. /**
  68. * @param object|bool|null $license
  69. *
  70. * @param string $key
  71. *
  72. * @return array
  73. */
  74. public static function get_status_messages( $license = null, $key = '' ) {
  75. $messages = array();
  76. if ( self::has_license( $license ) ) {
  77. // activate_license 'invalid' on anything other than valid, so if there was an error capture it
  78. if ( false === $license->success ) {
  79. switch ( $license->error ) {
  80. case 'expired' :
  81. $messages[] = sprintf( __( 'Your license key expired on %s. Please %srenew your license key%s.', 'popup-maker' ), date_i18n( get_option( 'date_format' ), strtotime( $license->expires, current_time( 'timestamp' ) ) ), '<a target="_blank" href="https://wppopupmaker.com/checkout/?edd_license_key=' . $key . '&utm_campaign=Licensing&utm_source=plugin-settings-page-licenses-tab&utm_medium=expired&utm_content=pum_license">', '</a>' );
  82. break;
  83. case 'disabled' :
  84. case 'revoked' :
  85. $messages[] = sprintf( __( 'Your license key has been disabled. Please %scontact support%s for more information.', 'popup-maker' ), '<a target="_blank" href="https://wppopupmaker.com/support/?utm_campaign=Licensing&utm_source=plugin-settings-page-licenses-tab&utm_content=pum_license&utm_medium=revoked">', '</a>' );
  86. break;
  87. case 'missing' :
  88. $messages[] = sprintf( __( 'Invalid license. Please %svisit your account page%s and verify it.', 'popup-maker' ), '<a target="_blank" href="https://wppopupmaker.com/your-account/license-keys/?utm_campaign=Licensing&utm_source=plugin-settings-page-licenses-tab&utm_content=pum_license&utm_medium=missing">', '</a>' );
  89. break;
  90. case 'invalid' :
  91. case 'site_inactive' :
  92. $messages[] = sprintf( __( 'Your %s is not active for this URL. Please %svisit your account page%s to manage your license key URLs.', 'popup-maker' ), Popup_Maker::$NAME, '<a target="_blank" href="https://wppopupmaker.com/your-account/license-keys/?utm_campaign=Licensing&utm_source=plugin-settings-page-licenses-tab&utm_content=pum_license&utm_medium=invalid">', '</a>' );
  93. break;
  94. case 'item_name_mismatch' :
  95. $messages[] = sprintf( __( 'This appears to be an invalid license key for %s.', 'popup-maker' ), Popup_Maker::$NAME );
  96. break;
  97. case 'no_activations_left':
  98. $messages[] = sprintf( __( 'Your license key has reached its activation limit. %sView possible upgrades%s now.', 'popup-maker' ), '<a target="_blank" href="https://wppopupmaker.com/your-account/license-keys/?utm_campaign=Licensing&utm_source=plugin-settings-page-licenses-tab&utm_content=pum_license&utm_medium=no-activations-left">', '</a>' );
  99. break;
  100. case 'license_not_activable':
  101. $messages[] = __( 'The key you entered belongs to a bundle, please use the product specific license key.', 'popup-maker' );
  102. break;
  103. default :
  104. $error = ! empty( $license->error ) ? $license->error : __( 'unknown_error', 'popup-maker' );
  105. $messages[] = sprintf( __( 'There was an error with this license key: %s. Please %scontact our support team%s.', 'popup-maker' ), $error, '<a target="_blank" href="https://wppopupmaker.com/support/?utm_campaign=Licensing&utm_source=plugin-settings-page-licenses-tab&utm_content=pum_license&utm_medium=error-contact-support">', '</a>' );
  106. break;
  107. }
  108. } else {
  109. switch ( $license->license ) {
  110. case 'valid' :
  111. default:
  112. $now = current_time( 'timestamp' );
  113. $expiration = strtotime( $license->expires, current_time( 'timestamp' ) );
  114. if ( 'lifetime' === $license->expires ) {
  115. $messages[] = __( 'License key never expires.', 'popup-maker' );
  116. } elseif ( $expiration > $now && $expiration - $now < ( DAY_IN_SECONDS * 30 ) ) {
  117. $messages[] = sprintf( __( 'Your license key expires soon! It expires on %s. %sRenew your license key%s.', 'popup-maker' ), date_i18n( get_option( 'date_format' ), strtotime( $license->expires, current_time( 'timestamp' ) ) ), '<a target="_blank" href="https://wppopupmaker.com/checkout/?edd_license_key=' . $key . '&utm_campaign=Licensing&utm_source=plugin-settings-page-licenses-tab&utm_content=Popup+Maker+license&utm_medium=renew">', '</a>' );
  118. } else {
  119. $messages[] = sprintf( __( 'Your license key expires on %s.', 'popup-maker' ), date_i18n( get_option( 'date_format' ), strtotime( $license->expires, current_time( 'timestamp' ) ) ) );
  120. }
  121. break;
  122. }
  123. }
  124. } else {
  125. $messages[] = sprintf( __( 'To receive updates, please enter your valid %s license key.', 'popup-maker' ), Popup_Maker::$NAME );;
  126. }
  127. return $messages;
  128. }
  129. /**
  130. * @param object|bool|null $license
  131. *
  132. * @return bool|string
  133. */
  134. public static function get_status_classes( $license = null ) {
  135. $class = false;
  136. if ( self::has_license( $license ) && false !== $license->success ) {
  137. $now = current_time( 'timestamp' );
  138. $expiration = self::get_license_expiration( $license );
  139. if ( 'lifetime' === $expiration ) {
  140. $class = 'pum-license-lifetime-notice';
  141. } elseif ( $expiration > $now && $expiration - $now < ( DAY_IN_SECONDS * 30 ) ) {
  142. $class = 'pum-license-expires-soon-notice';
  143. } else {
  144. $class = 'pum-license-expiration-date-notice';
  145. }
  146. }
  147. return $class;
  148. }
  149. }