Guest User

class-theme-activation.php

a guest
May 6th, 2024
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.03 KB | None | 0 0
  1. <?php
  2. /**
  3. * Theme Activation.
  4. *
  5. * @package Newsreader
  6. */
  7.  
  8. if ( ! class_exists( 'CSCO_Theme_Activation' ) ) {
  9.  
  10. /**
  11. * This class to activate your theme and open up new opportunities.
  12. */
  13. class CSCO_Theme_Activation {
  14.  
  15. /**
  16. * The purchase code.
  17. *
  18. * @var string $purchase_code The purchase code.
  19. */
  20. public $purchase_code = null;
  21.  
  22. /**
  23. * The current theme slug.
  24. *
  25. * @var string $theme The current theme slug.
  26. */
  27. public $theme;
  28.  
  29. /**
  30. * The current theme name.
  31. *
  32. * @var string $theme_name The current theme name.
  33. */
  34. public $theme_name;
  35.  
  36. /**
  37. * The server domain.
  38. *
  39. * @var string $server The server domain.
  40. */
  41. public $server;
  42.  
  43. /**
  44. * The current domain.
  45. *
  46. * @var string $theme The current domain.
  47. */
  48. public $domain;
  49.  
  50. /**
  51. * The message.
  52. *
  53. * @var string $msg The message.
  54. */
  55. public $msg;
  56.  
  57. /**
  58. * Constructor.
  59. */
  60. public function __construct() {
  61. $this->init();
  62.  
  63. $this->trigger_license();
  64.  
  65. /** Initialize actions */
  66. add_action( 'csco_theme_dashboard_tabs', array( $this, 'register_settings' ) );
  67. add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
  68. add_action( 'admin_notices', array( $this, 'display_license_notice' ) );
  69. add_action( 'wp_ajax_csco_activation_dismissed_handler', array( $this, 'dismissed_handler' ) );
  70. }
  71.  
  72. /**
  73. * Initialization
  74. */
  75. public function init() {
  76. // Set current theme slug.
  77. $this->theme = get_template();
  78. // Set current theme name.
  79. $this->theme_name = $this->get_theme_data( 'Name' );
  80. // Set server url.
  81. $this->server = $this->get_theme_data( 'AuthorURI' );
  82. // Set current domain.
  83. $this->domain = $this->format_domain( is_multisite() ? network_site_url() : site_url() );
  84. }
  85.  
  86. /**
  87. * Format the domain according to certain rules.
  88. *
  89. * @param string $string The name of domain.
  90. */
  91. public function format_domain( $string ) {
  92. $string = rtrim( $string, '/' );
  93.  
  94. // Remove 'WWW' from URL inside a string.
  95. $string = str_replace( 'www.', '', $string );
  96.  
  97. return $string;
  98. }
  99.  
  100. /**
  101. * Get data about the theme.
  102. *
  103. * @param mixed $name The name of param.
  104. */
  105. public function get_theme_data( $name ) {
  106. $data = wp_get_theme( $this->theme );
  107.  
  108. return $data->get( $name );
  109. }
  110.  
  111.  
  112. /**
  113. * Add settings to dashboard
  114. *
  115. * @param array $tabs The tabs.
  116. */
  117. public function register_settings( $tabs ) {
  118.  
  119. $tabs[] = array(
  120. 'slug' => 'license',
  121. 'label' => esc_html__( 'License Information', 'newsreader' ),
  122. 'priority' => 10,
  123. 'content' => $this->render_settings(),
  124. );
  125.  
  126. return $tabs;
  127. }
  128.  
  129. /**
  130. * Set message.
  131. *
  132. * @param string $text The text of message.
  133. * @param string $type The type of message.
  134. */
  135. public function set_message( $text, $type = 'error' ) {
  136. ob_start();
  137. ?>
  138. <div class="notice notice-<?php echo esc_attr( $type ); ?>">
  139. <p><?php echo wp_kses( $text, 'common' ); ?></p>
  140. </div>
  141. <?php
  142. return ob_get_clean();
  143. }
  144.  
  145. /**
  146. * Purified from the database information about notification.
  147. */
  148. public function reset_notices() {
  149. delete_transient( sprintf( '%s_license_expired', $this->theme ) );
  150.  
  151. delete_transient( sprintf( '%s_license_limit', $this->theme ) );
  152. }
  153.  
  154. /**
  155. * Get option name with purchase code.
  156. *
  157. * @param array $data The data of license.
  158. */
  159. public function setting_purchase_code( $data = null ) {
  160. $item_id = isset( $data['item_id'] ) ? $data['item_id'] : csco_get_license_data( 'item_id' );
  161.  
  162. return sprintf( 'envato_purchase_code_%s', $item_id );
  163. }
  164.  
  165. /**
  166. * Get option name with license data.
  167. */
  168. public function setting_license_data() {
  169. return sprintf( '%s_license_data', $this->theme );
  170. }
  171.  
  172.  
  173. /**
  174. * Get option name with subscribe.
  175. */
  176. public function setting_subscribe() {
  177. return sprintf( '%s_license_subscribe', md5( $this->domain ) );
  178. }
  179.  
  180. /**
  181. * Update subscribe.
  182. */
  183. public function update_subscribe() {
  184. update_option( $this->setting_subscribe(), true );
  185. }
  186.  
  187. /**
  188. * Check license existence.
  189. *
  190. * @param array $data The data of license.
  191. */
  192. public function check_license( $data ) {
  193. return isset( $data['item_id'] ) ? true : false;
  194. }
  195.  
  196. /**
  197. * Update license.
  198. *
  199. * @param array $data The data of license.
  200. */
  201. public function update_license( $data ) {
  202. // Update purchase code.
  203. update_option( $this->setting_purchase_code( $data ), $this->purchase_code );
  204.  
  205. // Update license data.
  206. update_option( $this->setting_license_data(), $data );
  207. }
  208.  
  209. /**
  210. * Delete data of license from database.
  211. */
  212. public function delete_license() {
  213. $this->purchase_code = null;
  214.  
  215. // Delete purchase code.
  216. delete_option( $this->setting_purchase_code() );
  217.  
  218. // Delete license data.
  219. delete_option( $this->setting_license_data() );
  220. }
  221.  
  222. /**
  223. * Remove alliances.
  224. *
  225. * @param array $haystack The haystack.
  226. * @param array $results The results.
  227. */
  228. public function remove_alliances( $haystack, $results = array() ) {
  229. if ( $haystack ) {
  230. foreach ( $haystack as $key => $item ) {
  231. if ( isset( $item['domain'] ) ) {
  232. if ( strpos( $item['domain'], 'localhost' ) || strpos( $item['domain'], '127.0.0.1' ) ) {
  233. continue;
  234. }
  235.  
  236. $parse = explode( '.', wp_parse_url( $item['domain'], PHP_URL_HOST ) );
  237.  
  238. if ( 'test' === end( $parse ) || 'dev' === end( $parse ) ) {
  239. continue;
  240. }
  241.  
  242. $haystack[ $key ]['domain'] = str_replace( array( 'https://', 'http://', 'www.' ), '', $item['domain'] );
  243.  
  244. $haystack[ $key ]['domain'] = rtrim( $haystack[ $key ]['domain'], '/' );
  245.  
  246. $map = array_map( function ( $item ) {
  247. return $item['domain'];
  248. }, $results );
  249.  
  250. if ( ! in_array( $haystack[ $key ]['domain'], $map, true ) ) {
  251. $results[] = $item;
  252. }
  253. }
  254. }
  255. }
  256.  
  257. return $results;
  258. }
  259.  
  260. /**
  261. * Render settings
  262. */
  263. public function render_settings() {
  264. ob_start();
  265.  
  266. wp_verify_nonce( null );
  267.  
  268. if ( ! current_user_can( 'manage_options' ) ) {
  269. wp_die( esc_html__( 'You do not have sufficient rights to view this page.', 'newsreader' ) );
  270. }
  271.  
  272. // Message output.
  273. if ( $this->msg ) {
  274. echo wp_kses( $this->msg, 'post' );
  275. }
  276.  
  277. // Get current status.
  278. $status = csco_get_license_data( 'status' );
  279. ?>
  280. <div class="cs-activation">
  281. <?php if ( $status && ! get_option( $this->setting_subscribe() ) && 'dev' !== csco_get_license_data( 'item_id' ) ) { ?>
  282. <div class="cs-activation-postbox">
  283.  
  284. <h2 class="hndle"><span><?php esc_html_e( 'Updates', 'newsreader' ); ?></span></h2>
  285.  
  286. <div class="inside">
  287. <p class="cs-theme-license-msg"><?php esc_html_e( 'We set a special price for all new themes for just a few days. Get notified of all introductory, flash and seasonal sales by signing up to our updates.', 'newsreader' ); ?></p>
  288.  
  289. <form method="post" class="cs-theme-license-form">
  290. <?php wp_nonce_field(); ?>
  291.  
  292. <input type="hidden" name="code" value="<?php echo esc_attr( get_option( $this->setting_purchase_code() ) ); ?>">
  293.  
  294. <table class="form-table">
  295. <!-- Email Address -->
  296. <tr>
  297. <th scope="row"><?php esc_html_e( 'Email Address', 'newsreader' ); ?></label></th>
  298. <td>
  299. <input class="regular-text" type="text" name="email" value="<?php echo esc_attr( get_bloginfo( 'admin_email' ) ); ?>">
  300. </td>
  301. </tr>
  302. <!-- Updates -->
  303. <tr>
  304. <th scope="row"></th>
  305. <td>
  306. <div class="cs-theme-license-updates">
  307. <input class="cs-theme-license-newsletter" id="newsletter" name="newsletter" type="checkbox" value="1">
  308.  
  309. <label for="newsletter">
  310. <p><?php esc_html_e( 'By checking this box you agree to our', 'newsreader' ); ?> <a href="https://codesupply.co/terms-and-conditions/" target="_blank"><?php esc_html_e( 'Terms and Conditions', 'newsreader' ); ?></a> <?php esc_html_e( 'and', 'newsreader' ); ?> <a href="https://codesupply.co/privacy-policy/" target="_blank"><?php esc_html_e( 'Privacy Policy', 'newsreader' ); ?></a>.</p>
  311.  
  312. <p class="description"><?php esc_html_e( 'You may opt-out any time by clicking the unsubscribe link in the footer of any email you receice from us, or by contacting us at', 'newsreader' ); ?> <a target="_blank" href="mailto:[email protected]"><?php esc_html_e( '[email protected]', 'newsreader' ); ?></a>.</p>
  313. </label>
  314. </div>
  315. </td>
  316. </tr>
  317. <!-- Submitbox -->
  318. <tr>
  319. <th scope="row"></th>
  320. <td>
  321. <button name="action" value="subscribe" type="submit" class="button button-primary button-large" id="publish"><?php esc_html_e( 'Subscribe', 'newsreader' ); ?></button>
  322. </td>
  323. </tr>
  324. </table>
  325. </form>
  326. </div>
  327. </div>
  328. <?php } ?>
  329.  
  330. <div class="cs-activation-postbox">
  331. <?php if ( ! $status ) { ?>
  332. <h2 class="hndle">
  333. <span>
  334. <?php esc_html_e( 'Activation', 'newsreader' ); ?>
  335. </span>
  336. </h2>
  337. <?php } ?>
  338.  
  339. <div class="inside">
  340. <!-- Active Websites -->
  341. <?php if ( $status ) { ?>
  342. <form method="post">
  343. <?php wp_nonce_field(); ?>
  344.  
  345. <table class="form-table">
  346. <tr>
  347. <th scope="row"><?php esc_html_e( 'Active Websites', 'newsreader' ); ?></label></th>
  348. <td>
  349. <?php
  350. $history = csco_get_license_data( 'license_history' );
  351.  
  352. if ( $history ) {
  353. ?>
  354. <?php
  355. foreach ( $history as $item ) {
  356. if ( 'active' === $item['status'] ) {
  357. echo sprintf( '<p><a target="_blank" href="%1$s">%1$s</a></p>', esc_url( $item['domain'] ) );
  358. }
  359. }
  360. ?>
  361. <br>
  362. <p>
  363. <input type="hidden" name="code" value="<?php echo esc_attr( get_option( $this->setting_purchase_code() ) ); ?>">
  364.  
  365. <button class="button" type="submit" name="action" value="check"><?php esc_html_e( 'Check Again', 'newsreader' ); ?></button>
  366. </p>
  367. <?php
  368. } elseif ( 'dev' === csco_get_license_data( 'item_id' ) ) {
  369. esc_html_e( 'Development Mode', 'newsreader' );
  370. }
  371. ?>
  372. </td>
  373. </tr>
  374. </table>
  375. </form>
  376. <?php } ?>
  377.  
  378. <!-- Information -->
  379. <form method="post">
  380. <?php wp_nonce_field(); ?>
  381.  
  382. <table class="form-table">
  383. <!-- Purchase Code -->
  384. <tr class="<?php echo esc_attr( $status ? 'hidden' : null ); ?>">
  385. <th scope="row"><?php esc_html_e( 'Purchase Code', 'newsreader' ); ?></label></th>
  386. <td>
  387. <input class="regular-text" type="text" name="code" value="<?php echo esc_attr( get_option( $this->setting_purchase_code() ) ); ?>">
  388. </td>
  389. </tr>
  390. <!-- Purchase Date -->
  391. <tr class="<?php echo esc_attr( ! $status ? 'hidden' : null ); ?>">
  392. <th scope="row"><?php esc_html_e( 'Purchase Date', 'newsreader' ); ?></label></th>
  393. <td>
  394. <?php $sold_at = csco_get_license_data( 'sold_at' ); ?>
  395.  
  396. <?php echo esc_html( date( 'F d, Y', strtotime( $sold_at ) ) ); ?>
  397. </td>
  398. </tr>
  399. <!-- Supported Until -->
  400. <?php
  401. $supported_until = csco_get_license_data( 'supported_until' );
  402.  
  403. if ( strtotime( $supported_until ) < strtotime( 'now' ) ) {
  404. ?>
  405. <tr class="<?php echo esc_attr( ! $status ? 'hidden' : null ); ?>">
  406. <th scope="row"><?php esc_html_e( 'Supported Until', 'newsreader' ); ?></label></th>
  407. <td>
  408. <span class="cs-theme-license-supported_until">
  409. <?php echo esc_html( date( 'F d, Y', strtotime( $supported_until ) ) ); ?>
  410.  
  411. <?php esc_html_e( ' (Expired)', 'newsreader' ); ?>
  412. </span>
  413. <p class="description"><?php esc_html_e( 'Please renew your item support for any support requests. See', 'newsreader' ); ?> <a href="https://help.market.envato.com/hc/en-us/articles/207886473-Extending-and-Renewing-Item-Support" target="_blank"><?php esc_html_e( 'this document', 'newsreader' ); ?></a> <?php esc_html_e( 'for more details.', 'newsreader' ); ?></p>
  414. </td>
  415. </tr>
  416. <?php } ?>
  417. <!-- Submitbox -->
  418. <tr>
  419. <th scope="row">
  420. <?php
  421. if ( $status ) {
  422. esc_html_e( 'Deactivation', 'newsreader' );
  423. }
  424. ?>
  425. </th>
  426. <td>
  427. <?php if ( $status ) { ?>
  428. <button name="action" value="deactivate" type="submit" class="button button-primary button-large" id="publish"><?php esc_html_e( 'Deactivate License', 'newsreader' ); ?></button>
  429. <?php } ?>
  430.  
  431. <?php if ( ! $status ) { ?>
  432. <button name="action" value="activate" type="submit" class="button button-primary button-large" id="publish"><?php esc_html_e( 'Activate License', 'newsreader' ); ?></button>
  433. <?php } ?>
  434. </td>
  435. </tr>
  436. </table>
  437. </form>
  438. </div>
  439. </div>
  440. </div>
  441. <?php
  442.  
  443. return ob_get_clean();
  444. }
  445.  
  446. /**
  447. * Trigger license.
  448. */
  449. public function trigger_license() {
  450. if ( ! isset( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'] ) ) {
  451. return;
  452. }
  453.  
  454. // Get action.
  455. if ( ! isset( $_POST['action'] ) ) {
  456. return;
  457. }
  458.  
  459. // Get purchase code.
  460. if ( isset( $_POST['code'] ) && $_POST['code'] ) {
  461. $this->purchase_code = sanitize_text_field( $_POST['code'] );
  462. } else {
  463. $this->msg = $this->set_message( esc_html__( 'Purchase code not entered.', 'newsreader' ) );
  464. return;
  465. }
  466.  
  467. $action = sanitize_text_field( $_POST['action'] );
  468.  
  469. // Simulate successful license activation.
  470. $data = array(
  471. 'status' => 'active', // License is always active
  472. 'message' => esc_html__( 'License activated.', 'newsreader' )
  473. );
  474.  
  475. // Perform actions based on the simulated activation data.
  476. switch ( $action ) {
  477. case 'activate':
  478. case 'check':
  479. $this->reset_notices();
  480. $this->update_license( $data );
  481. break;
  482.  
  483. case 'deactivate':
  484. $this->reset_notices();
  485. $this->delete_license();
  486. break;
  487.  
  488. case 'subscribe':
  489. $this->update_subscribe();
  490. break;
  491.  
  492. default:
  493. // Handle other actions or do nothing.
  494. break;
  495. }
  496.  
  497. // Set a success message.
  498. $this->msg = $this->set_message( $data['message'], 'success' );
  499. }
  500.  
  501. /**
  502. * Display a notification of license.
  503. */
  504. public function display_license_notice() {
  505. $screen = get_current_screen();
  506.  
  507. if ( ! csco_get_license_data( 'status' ) ) {
  508. return;
  509. }
  510.  
  511. // Dismissible.
  512. $dismissible = null;
  513.  
  514. if ( 'appearance_page_theme-dashboard' !== $screen->base ) {
  515. $dismissible = 'is-dismissible';
  516. }
  517.  
  518. /*
  519. * Support expired.
  520. */
  521.  
  522. // Get license data.
  523. $supported_until = csco_get_license_data( 'supported_until' );
  524.  
  525. // Date comparison.
  526. if ( strtotime( $supported_until ) < strtotime( 'now' ) ) {
  527. // Set transient name.
  528. $transient_name = sprintf( '%s_license_expired', $this->theme );
  529.  
  530. if ( ( ! get_transient( $transient_name ) && $dismissible ) || ! $dismissible ) {
  531. ?>
  532. <div class="cs-activation-notice notice notice-warning <?php echo esc_attr( $dismissible ); ?>" data-notice="<?php echo esc_attr( $transient_name ); ?>">
  533. <p><strong><?php esc_html_e( 'Support expired.', 'newsreader' ); ?></strong>
  534. <?php
  535. // Translators: theme name and link activation.
  536. echo wp_kses( sprintf( __( 'Your support for the %1$s theme has expired. Please %2$s for any support requests.', 'newsreader' ), $this->theme_name, '<a href="https://help.market.envato.com/hc/en-us/articles/207886473-Extending-and-Renewing-Item-Support" target="_blank">' . __( 'renew your support license', 'newsreader' ) . '</a>' ), 'common' );
  537. ?>
  538. </p>
  539. </div>
  540. <?php
  541. }
  542. }
  543.  
  544. /*
  545. * Multiple active websites detected.
  546. */
  547.  
  548. // Get license history.
  549. $history = (array) csco_get_license_data( 'license_history' );
  550. $count = (int) csco_get_license_data( 'purchase_count' );
  551.  
  552. // Unique history.
  553. $history_without_alliances = $this->remove_alliances( $history );
  554.  
  555. // Get actived domains.
  556. $actived = array_filter( $history_without_alliances, function( $item ) {
  557. return isset( $item['status'] ) && 'active' === $item['status'];
  558. } );
  559.  
  560. // Check the number of purchases.
  561. if ( count( $actived ) > $count ) {
  562. // Set transient name.
  563. $transient_name = sprintf( '%s_license_limit', $this->theme );
  564.  
  565. if ( ( ! get_transient( $transient_name ) && $dismissible ) || ! $dismissible ) {
  566. ?>
  567. <div class="cs-activation-notice notice notice-warning <?php echo esc_attr( $dismissible ); ?>" data-notice="<?php echo esc_attr( $transient_name ); ?>">
  568. <p><strong><?php esc_html_e( 'Multiple active websites detected.', 'newsreader' ); ?></strong> <?php esc_html_e( 'Looks like you’re using the same theme license on multiple websites. A website theme can only be customized to create one customized website according to the ThemeForest license terms. If you want to create a second website from the same theme, you’ll need to', 'newsreader' ); ?> <a href="<?php esc_url( admin_url( '/themes.php?page=theme-dashboard&tab=license' ) ); ?>"><?php esc_html_e( 'purchase another license', 'newsreader' ); ?></a>.</p>
  569. </div>
  570. <?php
  571. }
  572. }
  573. }
  574.  
  575. /**
  576. * Dismissed handler
  577. */
  578. public function dismissed_handler() {
  579. wp_verify_nonce( null );
  580.  
  581. if ( isset( $_POST['notice'] ) ) { // Input var ok; sanitization ok.
  582. set_transient( sanitize_text_field( wp_unslash( $_POST['notice'] ) ), true, 90 * DAY_IN_SECONDS ); // Input var ok.
  583. }
  584. }
  585.  
  586. /**
  587. * Enqunue Scripts
  588. *
  589. * @param string $page Current page.
  590. */
  591. public function enqueue_scripts( $page ) {
  592. wp_enqueue_script( 'jquery' );
  593.  
  594. ob_start();
  595. ?>
  596. <script>
  597. jQuery(function($) {
  598. $( document ).on( 'click', '.cs-activation-notice .notice-dismiss', function () {
  599. jQuery.post( 'ajax_url', {
  600. action: 'csco_activation_dismissed_handler',
  601. notice: $( this ).closest( '.cs-activation-notice' ).data( 'notice' ),
  602. });
  603. } );
  604. });
  605. </script>
  606. <?php
  607. $script = str_replace( 'ajax_url', admin_url( 'admin-ajax.php' ), ob_get_clean() );
  608.  
  609. wp_add_inline_script( 'jquery', str_replace( array( '<script>', '</script>' ), '', $script ) );
  610. }
  611. }
  612.  
  613. /**
  614. * Display the license notification.
  615. */
  616. function csco_license_notice() {
  617. if ( ! csco_get_license_data( 'status' ) ) {
  618. ?>
  619. <div class="notice notice-warning notice-alt">
  620. <p>
  621. <?php
  622. if ( is_customize_preview() ) {
  623. // Translators: link activation.
  624. echo wp_kses( sprintf( __( 'Please %1$s to unlock theme demos.', 'newsreader' ), '<a class="button-link" href="' . admin_url( '/themes.php?page=theme-dashboard&tab=license' ) . '">' . __( 'activate your license', 'newsreader' ) . '</a>' ), 'common' );
  625. } else {
  626. // Translators: link activation.
  627. echo wp_kses( sprintf( __( 'Please %1$s to unlock demo content.', 'newsreader' ), '<a class="button-link" href="' . admin_url( '/themes.php?page=theme-dashboard&tab=license' ) . '">' . __( 'activate your license', 'newsreader' ) . '</a>' ), 'common' );
  628. }
  629. ?>
  630. </p>
  631. </div>
  632. <?php
  633. }
  634. }
  635.  
  636. /**
  637. * Get data of license.
  638. *
  639. * @param string $param The name param.
  640. */
  641. function csco_get_license_data( $param ) {
  642. $data = get_option( sprintf( '%s_license_data', get_template() ), array() );
  643.  
  644. if ( is_array( $data ) && isset( $data[ $param ] ) ) {
  645. return $data[ $param ];
  646. }
  647. }
  648.  
  649. new CSCO_Theme_Activation();
  650. }
  651.  
Advertisement
Add Comment
Please, Sign In to add comment