PLL_Settings_Licenses

Settings class for licenses.

Defined (1)

The class is defined in the following location(s).

/settings/settings-licenses.php  
  1. class PLL_Settings_Licenses extends PLL_Settings_Module { 
  2. protected $items; 
  3.  
  4. /** 
  5. * Constructor 
  6. * @since 1.9 
  7. * @param object $polylang polylang object 
  8. */ 
  9. public function __construct( &$polylang ) { 
  10. parent::__construct( $polylang, array( 
  11. 'module' => 'licenses',  
  12. 'title' => __( 'License keys', 'polylang' ),  
  13. 'description' => __( 'Manage licenses for Polylang Pro or addons.', 'polylang' ),  
  14. ) ); 
  15.  
  16. $this->buttons['cancel'] = sprintf( '<button type="button" class="button button-secondary cancel">%s</button>', __( 'Close' ) ); 
  17.  
  18. $this->items = apply_filters( 'pll_settings_licenses', array() ); 
  19.  
  20. add_action( 'wp_ajax_pll_deactivate_license', array( $this, 'deactivate_license' ) ); 
  21.  
  22. /** 
  23. * Tells if the module is active 
  24. * @since 1.9 
  25. * @return bool 
  26. */ 
  27. public function is_active() { 
  28. return ! empty( $this->items ); 
  29.  
  30. /** 
  31. * Displays the settings form 
  32. * @since 1.9 
  33. */ 
  34. protected function form() { 
  35. if ( ! empty( $this->items ) ) { ?> 
  36. <table id="pll-licenses-table" class="form-table"><?php 
  37. foreach ( $this->items as $item ) { 
  38. echo $this->get_row( $item ); 
  39. } ?> 
  40. </table><?php 
  41.  
  42. /** 
  43. * Get the html for a row (one per license key) for display 
  44. * @since 1.9 
  45. * @param array $item licence id, name and key 
  46. * @return string 
  47. */ 
  48. protected function get_row( $item ) { 
  49. if ( ! empty( $item->license_data ) ) { 
  50. $license = $item->license_data; 
  51.  
  52. $class = 'license-null'; 
  53.  
  54. $out = sprintf( 
  55. '<td><label for="pll-licenses[%1$s]">%2$s</label></td>' . 
  56. '<td><input name="licenses[%1$s]" id="pll-licenses[%1$s]" type="text" value="%3$s" class="regular-text code" />',  
  57. esc_attr( $item->id ), esc_attr( $item->name ), esc_html( $item->license_key ) 
  58. ); 
  59.  
  60. if ( ! empty( $license ) && is_object( $license ) ) { 
  61. $now = current_time( 'timestamp' ); 
  62. $expiration = strtotime( $license->expires, $now ); 
  63.  
  64. // Special case: the license expired after the last check 
  65. if ( $license->success && $expiration < $now ) { 
  66. $license->success = false; 
  67. $license->error = 'expired'; 
  68.  
  69. if ( false === $license->success ) { 
  70. $class = 'notice-error notice-alt'; 
  71.  
  72. switch ( $license->error ) { 
  73. case 'expired' : 
  74. $message = sprintf( 
  75. /** translators: %1$s is a date, %2$s and %3$s are html tags */ 
  76. __( 'Your license key expired on %1$s. Please %2$srenew your license key%3$s.', 'polylang' ),  
  77. date_i18n( get_option( 'date_format' ), strtotime( $license->expires, current_time( 'timestamp' ) ) ),  
  78. sprintf( '<a href="%s" target="_blank">', 'https://polylang.pro/checkout/?edd_license_key=' . $item->license_key ),  
  79. '</a>' 
  80. ); 
  81. break; 
  82.  
  83. case 'missing' : 
  84. $message = sprintf( 
  85. /** translators: %s are html tags */ 
  86. __( 'Invalid license. Please %svisit your account page%s and verify it.', 'polylang' ),  
  87. sprintf( '<a href="%s" target="_blank">', 'https://polylang.pro/account' ),  
  88. '</a>' 
  89. ); 
  90. break; 
  91.  
  92. case 'invalid' : 
  93. case 'site_inactive' : 
  94. $message = sprintf( 
  95. /** translators: %1$s is a product name, %2$s and %3$s are html tags */ 
  96. __( 'Your %1$s license key is not active for this URL. Please %2$svisit your account page%3$s to manage your license key URLs.', 'polylang' ),  
  97. $item->name,  
  98. sprintf( '<a href="%s" target="_blank">', 'https://polylang.pro/account' ),  
  99. '</a>' 
  100. ); 
  101. break; 
  102.  
  103. case 'item_name_mismatch' : 
  104. /** translators: %s is a product name */ 
  105. $message = sprintf( __( 'This is not a %s license key.', 'polylang' ), $item->name ); 
  106. break; 
  107.  
  108. case 'no_activations_left': 
  109. /** translators: %s are html tags */ 
  110. $message = sprintf( 
  111. __( 'Your license key has reached its activation limit. %sView possible upgrades%s now.', 'polylang' ),  
  112. sprintf( '<a href="%s" target="_blank">', 'https://polylang.pro/account' ),  
  113. '</a>' 
  114. ); 
  115. break; 
  116. } else { 
  117. $class = 'license-valid'; 
  118.  
  119. $out .= sprintf( '<button id="deactivate_%s" type="button" class="button button-secondary pll-deactivate-license">%s</button>', $item->id, __( 'Deactivate', 'polylang' ) ); 
  120.  
  121. if ( 'lifetime' === $license->expires ) { 
  122. $message = __( 'The license key never expires.', 'polylang' ); 
  123. } elseif ( $expiration > $now && $expiration - $now < ( DAY_IN_SECONDS * 30 ) ) { 
  124. $class = 'notice-warning notice-alt'; 
  125. $message = sprintf( 
  126. /** translators: %1$s is a date, %2$s and %3$s are html tags */ 
  127. __( 'Your license key expires soon! It expires on %1$s. %2$sRenew your license key%3$s.', 'polylang' ),  
  128. date_i18n( get_option( 'date_format' ), strtotime( $license->expires, $now ) ),  
  129. sprintf( '<a href="%s" target="_blank">', 'https://polylang.pro/checkout/?edd_license_key=' . $item->license_key ),  
  130. '</a>' 
  131. ); 
  132. } else { 
  133. $message = sprintf( 
  134. /** translators: %s is a date */ 
  135. __( 'Your license key expires on %s.', 'polylang' ),  
  136. date_i18n( get_option( 'date_format' ), strtotime( $license->expires, $now ) ) 
  137. ); 
  138.  
  139. if ( ! empty( $message ) ) { 
  140. $out .= '<p>' . $message . '</p>'; 
  141.  
  142. return sprintf( '<tr id="pll-license-%s" class="%s">%s</tr>', $item->id, $class, $out ); 
  143.  
  144. /** 
  145. * Ajax method to save the license keys and activate the licenses at the same time 
  146. * Overrides parent's method 
  147. * @since 1.9 
  148. */ 
  149. public function save_options() { 
  150. check_ajax_referer( 'pll_options', '_pll_nonce' ); 
  151. if ( ! current_user_can( 'manage_options' ) ) { 
  152. wp_die( -1 ); 
  153.  
  154. if ( $this->module === $_POST['module'] && ! empty( $_POST['licenses'] ) ) { 
  155. $x = new WP_Ajax_Response(); 
  156. foreach ( $this->items as $item ) { 
  157. $updated_item = $item->activate_license( sanitize_text_field( $_POST['licenses'][ $item->id ] ) ); 
  158. $x->Add( array( 'what' => 'license-update', 'data' => $item->id, 'supplemental' => array( 'html' => $this->get_row( $updated_item ) ) ) ); 
  159.  
  160. // Updated message 
  161. add_settings_error( 'general', 'settings_updated', __( 'Settings saved.' ), 'updated' ); 
  162. ob_start(); 
  163. settings_errors(); 
  164. $x->Add( array( 'what' => 'success', 'data' => ob_get_clean() ) ); 
  165. $x->send(); 
  166.  
  167. /** 
  168. * Ajax method to deactivate a license 
  169. * @since 1.9 
  170. */ 
  171. public function deactivate_license() { 
  172. check_ajax_referer( 'pll_options', '_pll_nonce' ); 
  173.  
  174. if ( ! current_user_can( 'manage_options' ) ) { 
  175. wp_die( -1 ); 
  176.  
  177. $id = sanitize_text_field( substr( $_POST['id'], 11 ) ); 
  178. wp_send_json( array( 
  179. 'id' => $id,  
  180. 'html' => $this->get_row( $this->items[ $id ]->deactivate_license() ),  
  181. ) );