Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 23.02 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Class td_js
  5.  */
  6.  
  7. define ('TD_JS_THEME_VERSION_URL', 'http://td_cake.themesafe.com/td_cake/version.php');
  8.  
  9.  
  10. class td_js {
  11.  
  12.     /**
  13.      * is running on each page load
  14.      * || td_api_features::is_enabled('require_activation') === false
  15.      */
  16.     function __construct() {
  17.         // not admin
  18.         if ( !is_admin()) {
  19.             return;
  20.         }
  21.  
  22.         $td_js_status_time = td_util::get_option_('td_cake_status_time');    // last time the status changed
  23.         $td_js_status = td_util::get_option_('td_cake_status');              // the current status time
  24.         $td_js_lp_status = td_util::get_option_('td_cake_lp_status');
  25.  
  26.         // verify if we have a status time, if we don't have one, the theme did not changed the status ever
  27.         if (!empty($td_js_status_time)) {
  28.  
  29.  
  30.             // the time since the last status change
  31.             $status_time_delta = time() - $td_js_status_time;
  32.  
  33.             // late version check after 30
  34.             if (TD_DEPLOY_MODE == 'dev') {
  35.                 $delta_max = 40;
  36.             } else {
  37.                 $delta_max = 2592000;
  38.             }
  39.  
  40.             if ($status_time_delta > $delta_max and $td_js_lp_status != 'lp_sent') {
  41.                 td_util::update_option_('td_cake_lp_status', 'lp_sent');
  42.                 $td_theme_version = @wp_remote_get(TD_JS_THEME_VERSION_URL . '?cs=' . $td_js_status . '&lp=true&v=' . TD_THEME_VERSION . '&n=' . TD_THEME_NAME, array('blocking' => false));
  43.                 return;
  44.             }
  45.  
  46.             // the theme is registered, return
  47.             if ($td_js_status == 2) {
  48.                 return;
  49.             }
  50.  
  51.             // add the menu
  52.             add_action('admin_menu', array($this, 'td_js_register_panel'), 11);
  53.  
  54.  
  55.             if (TD_DEPLOY_MODE == 'dev') {
  56.                 $delta_max = 40;
  57.             } else {
  58.                 $delta_max = 1209600; // 14 days
  59.             }
  60.             if ($status_time_delta > $delta_max) {
  61.                 add_action( 'admin_notices', array($this, 'td_js_msg_2') );
  62.                 if ($td_js_status != '4') {
  63.                     td_util::update_option_('td_cake_status', '4');
  64.                 }
  65.                 return;
  66.             }
  67.  
  68.             if (TD_DEPLOY_MODE == 'dev') {
  69.                 $delta_max = 20;
  70.             } else {
  71.                 $delta_max = 604800; // 7 days
  72.             }
  73.             if ($status_time_delta > $delta_max) {
  74.                 add_action( 'admin_notices', array($this, 'td_js_msg') );
  75.                 if ($td_js_status != '3') {
  76.                     td_util::update_option_('td_cake_status', '3');
  77.                 }
  78.                 return;
  79.             }
  80.  
  81.             // if some time passed and status is empty - do ping
  82.             if ($status_time_delta > 0 and empty($td_js_status)) {
  83.                 td_util::update_option_('td_cake_status_time', time());
  84.                 td_util::update_option_('td_cake_status', '1');
  85.                 $td_theme_version = @wp_remote_get(TD_JS_THEME_VERSION_URL . '?v=' . TD_THEME_VERSION . '&n=' . TD_THEME_NAME, array('blocking' => false)); // check for updates
  86.                 return;
  87.             }
  88.  
  89.         } else {
  90.             // update the status time first time - we do nothing
  91.             td_util::update_option_('td_cake_status_time', time());
  92.             // add the menu
  93.             add_action('admin_menu', array($this, 'td_js_register_panel'), 11);
  94.         }
  95.  
  96.     }
  97.  
  98.     function td_footer_manual_activation($text) {
  99.         //add manual activation button
  100.         $text .= '<a href="#" class="td-manual-activation-btn">Activate the theme manually</a>';
  101.         //add auto activation button
  102.         $text .= '<a href="#" class="td-auto-activation-btn" style="display: none;">Back to automatic activation</a>';
  103.         //button script
  104.         $text .= '<script type="text/javascript">
  105.                    //manual activation
  106.                    jQuery(\'.td-manual-activation-btn\').click(function(event){
  107.                        event.preventDefault();
  108.                        jQuery(\'.td-manual-activation\').css(\'display\', \'block\');
  109.                        //hide manual activation button
  110.                        jQuery(this).hide();
  111.                        //hide auto activation panel
  112.                        jQuery(\'.td-auto-activation\').hide();
  113.                        //display back to automatic activation button
  114.                        jQuery(\'.td-auto-activation-btn\').show();
  115.                    });
  116.                        
  117.                    //automatic activation
  118.                    jQuery(\'.td-auto-activation-btn\').click(function(event){
  119.                        event.preventDefault();
  120.                        jQuery(\'.td-manual-activation\').css(\'display\', \'none\');
  121.                        //hide back to automatic activation button
  122.                        jQuery(this).hide();
  123.                        //show auto activation panel
  124.                        jQuery(\'.td-auto-activation\').show();
  125.                        //display manual activation button
  126.                        jQuery(\'.td-manual-activation-btn\').show();
  127.                    });
  128.                 </script>';
  129.         echo $text;
  130.     }
  131.  
  132.     private function td_js_server_id() {
  133.         ob_start();
  134.         phpinfo(INFO_GENERAL);
  135.         echo TD_THEME_NAME;
  136.         return md5(ob_get_clean());
  137.     }
  138.  
  139.     private function syntax_check() {
  140.         $key = td_util::get_option('envato_key');
  141.         $key = preg_replace('#([a-z0-9]{8})-?([a-z0-9]{4})-?([a-z0-9]{4})-?([a-z0-9]{4})-?([a-z0-9]{12})#','$1-$2-$3-$4-$5',strtolower($key));
  142.         if (strlen($key) == 36){
  143.             return true;
  144.         }
  145.         return false;
  146.     }
  147.  
  148.  
  149.     private function td_js_manual($s_id, $e_id, $t_id) {
  150.         if (md5($s_id . $e_id) == $t_id) {
  151.             return true;
  152.         } else {
  153.             return false;
  154.         }
  155.     }
  156.  
  157.  
  158.     /**
  159.      * the cake panel t
  160.      */
  161.  
  162.     function td_js_register_panel() {
  163.         if (td_api_features::is_enabled('require_activation') === true) {
  164.             add_submenu_page( "td_theme_welcome", 'Activate theme', 'Activate theme', "edit_posts", 'td_cake_panel', array(
  165.                 $this,
  166.                 'td_js_panel'
  167.             ), null );
  168.         }
  169.  
  170.     }
  171.  
  172.     /**
  173.      * show the activate theme panel
  174.      */
  175.     function td_js_panel() {
  176.  
  177.         // add manual activation link (visible only on this page)
  178.         add_filter('admin_footer_text', array($this, 'td_footer_manual_activation'));
  179.  
  180.         ?>
  181.         <style type="text/css">
  182.             .updated, .error {
  183.                 display: none !important;
  184.             }
  185.         </style>
  186.  
  187.         <div class="td-activate-page-wrap">
  188.  
  189.             <?php require_once "wp-admin/panel/td_view_header.php"; ?>
  190.  
  191.             <div class="about-wrap td-admin-wrap">
  192.  
  193.                 <div class="td-activate-wrap">
  194.                     <!-- Auto activation -->
  195.                     <div class="td-auto-activation">
  196.  
  197.                         <!-- Step 1 - Envato Code -->
  198.                         <div class="td-activate-section td-activate-envato-code">
  199.  
  200.                             <div class="td-activate-subtitle">Activate <?php echo TD_THEME_NAME ?></div>
  201.  
  202.                             <p class="td-activate-description">
  203.                                 Please activate <?php echo TD_THEME_NAME ?> to enjoy the full benefits of the theme. We're sorry about this extra step but we built the activation system to prevent
  204.                                 mass piracy of our themes, this allows us to better serve our paying customers.
  205.                             </p>
  206.  
  207.                             <div class="td-activate-input-wrap td-envato-code">
  208.                                 <div class="td-input-title">Envato purchase code:</div>
  209.                                 <input type="text" name="td-envato-code" value="" placeholder="Your Envato code"/>
  210.                                 <span class="td-activate-input-bar"></span>
  211.                                 <span class="td-activate-err td-envato-missing" style="display:none;">Code is required</span>
  212.                                 <span class="td-activate-err td-envato-length" style="display:none;">Code is too short</span>
  213.                                 <span class="td-activate-err td-envato-invalid" style="display:none;">Code is not valid</span>
  214.                                 <span class="td-activate-err td-envato-check-error" style="display:none;">Envato API is down, please try again later or use the manual registration.</span>
  215.                             </div>
  216.  
  217.  
  218.                             <button class="td-activate-button td-envato-code-button">Activate theme</button>
  219.                             <div class="td-envato-code-info"><a href="http://forum.tagdiv.com/how-to-find-your-envato-purchase-code/" target="_blank">Find your Envato code</a></div>
  220.                         </div>
  221.  
  222.                         <!-- Step 2 - Forum Registration -->
  223.                         <div class="td-activate-section td-activate-registration" style="display: none;">
  224.  
  225.                             <div class="td-activate-subtitle">Register your theme with tagDiv</div>
  226.  
  227.                             <p class="td-activate-description">
  228.                                 Get fast support, access to our community and to the knowledge base. Our support response time is typically less than one business day.
  229.                             </p>
  230.  
  231.                             <div class="td-registration-err td-forum-connection-failed" style="display:none;">Forum connection failed, please try again.</div>
  232.  
  233.                             <!-- Username -->
  234.                             <div class="td-activate-input-wrap td-activate-username">
  235.                                 <div class="td-input-title">Username:</div>
  236.                                 <input type="text" name="td-activate-username" value="" placeholder="Username" />
  237.                                 <span class="td-activate-input-bar"></span>
  238.                                 <span class="td-activate-err td-activate-username-missing" style="display:none;">Username is required</span>
  239.                                 <span class="td-activate-err td-activate-username-used" style="display:none;">Current username is already used, try another one</span>
  240.                             </div>
  241.  
  242.                             <!-- Email -->
  243.                             <div class="td-activate-input-wrap td-activate-email">
  244.                                 <div class="td-input-title">Your Email:</div>
  245.                                 <input type="text" name="td-activate-email" value="" placeholder="Email" />
  246.                                 <span class="td-activate-input-bar"></span>
  247.                                 <span class="td-activate-err td-activate-email-missing" style="display:none;">Email is required</span>
  248.                                 <span class="td-activate-err td-activate-email-syntax" style="display:none;">Email syntax is incorrect</span>
  249.                                 <span class="td-activate-err td-activate-email-used" style="display:none;">Current email is registered with another account</span>
  250.                                 <div class="td-small-bottom">The email is private and we will not share it with anyone.</div>
  251.                             </div>
  252.  
  253.                             <!-- Password -->
  254.                             <div class="td-activate-input-wrap td-activate-password">
  255.                                 <div class="td-input-title">Password:</div>
  256.                                 <input type="password" name="td-activate-password" autocomplete="off" value="" placeholder="Password" />
  257.                                 <span class="td-activate-input-bar"></span>
  258.                                 <span class="td-activate-err td-activate-password-missing" style="display:none;">Password is required</span>
  259.                                 <span class="td-activate-err td-activate-password-length" style="display:none;">Minimum password length is 6 characters</span>
  260.                             </div>
  261.  
  262.                             <!-- Password Confirmation -->
  263.                             <div class="td-activate-input-wrap td-activate-password-confirmation">
  264.                                 <div class="td-input-title">Password confirmation:</div>
  265.                                 <input type="password" name="td-activate-password-confirmation" autocomplete="off" value="" placeholder="Password confirmation" />
  266.                                 <span class="td-activate-input-bar"></span>
  267.                                 <span class="td-activate-err td-activate-password-confirmation-missing" style="display:none;">Password confirmation is required</span>
  268.                                 <span class="td-activate-err td-activate-password-mismatch" style="display:none;">Password and password confirmation don't match</span>
  269.                             </div>
  270.  
  271.                             <button class="td-activate-button td-registration-button">Activate theme</button>
  272.                             <div class="td-activate-info"><a href="http://forum.tagdiv.com/privacy-policy-2/" target="_blank">Privacy policy</a></div>
  273.                         </div>
  274.                     </div>
  275.  
  276.  
  277.                     <!-- Manual activation -->
  278.                     <div class="td-manual-activation">
  279.                         <div class="td-activate-subtitle">Manual activation</div>
  280.  
  281.                         <div class="td-registration-err td-manual-activation-failed" style="display:none;">Manual activation failed, check each field and try again.</div>
  282.  
  283.                         <div class="td-manual-info">
  284.                             <ol>
  285.                                 <li>Go to our <a href="http://tagdiv.com/td_cake/manual.php" target="_blank">manual activation page</a></li>
  286.                                 <li>Paste your <em>Server ID</em> there and the <a href="http://forum.tagdiv.com/how-to-find-your-envato-purchase-code/" target="_blank">Envato purchase code</a></li>
  287.                                 <li>Return with the <a href="http://forum.tagdiv.com/wp-content/uploads/2017/06/activation_key.png" target="_blank">activation key</a> and paste it in this form</li>
  288.                             </ol>
  289.                         </div>
  290.  
  291.  
  292.                         <!-- Your server ID -->
  293.                         <div class="td-activate-input-wrap td-manual-server-id">
  294.                             <div class="td-input-title">Your server ID:</div>
  295.                             <input type="text" name="td-manual-server-id" value="<?php echo $this->td_js_server_id();?>" readonly/>
  296.                             <span class="td-activate-input-bar"></span>
  297.                             <div class="td-small-bottom">Copy this id and paste it in our manual activation page</div>
  298.                         </div>
  299.  
  300.                         <!-- Envato code -->
  301.                         <div class="td-activate-input-wrap td-manual-envato-code">
  302.                             <div class="td-input-title">Envato purchase code:</div>
  303.                             <input type="text" name="td-manual-envato-code" value="" placeholder="Envato purcahse code" />
  304.                             <span class="td-activate-input-bar"></span>
  305.                             <span class="td-activate-err td-manual-envato-code-missing" style="display:none;">Envato code is required</span>
  306.                         </div>
  307.  
  308.                         <!-- Activation key -->
  309.                         <div class="td-activate-input-wrap td-manual-activation-key">
  310.                             <div class="td-input-title">tagDiv activation key:</div>
  311.                             <input type="text" name="td-manual-activation-key" value="" placeholder="Activation key" />
  312.                             <span class="td-activate-input-bar"></span>
  313.                             <span class="td-activate-err td-manual-activation-key-missing" style="display:none;">Activation key is required</span>
  314.                         </div>
  315.  
  316.                         <button class="td-activate-button td-manual-activate-button">Activate</button>
  317.  
  318.                     </div>
  319.  
  320.                 </div>
  321.  
  322.  
  323.  
  324.                 <!--            <form method="post" action="admin.php?page=td_cake_panel">-->
  325.                 <!---->
  326.                 <!--                <input type="hidden" name="td_magic_token" value="--><?php //echo wp_create_nonce("td-validate-license") ?><!--"/>-->
  327.                 <!---->
  328.                 <!--                <table class="form-table">-->
  329.                 <!--                    <tr valign="top">-->
  330.                 <!--                        <th scope="row">Envato purchase code:</th>-->
  331.                 <!--                        <td>-->
  332.                 <!--                            <input style="width: 400px" type="text" name="td_envato_code" value="--><?php //echo $td_envato_code; ?><!--" />-->
  333.                 <!--                            <br/>-->
  334.                 <!--                            <div class="td-small-bottom"><a href="http://forum.tagdiv.com/how-to-find-your-envato-purchase-code/" target="_blank">Where to find your purchase code ?</a></div>-->
  335.                 <!--                        </td>-->
  336.                 <!--                    </tr>-->
  337.                 <!---->
  338.                 <!---->
  339.                 <!---->
  340.                 <!--                </table>-->
  341.                 <!---->
  342.                 <!--                <input type="hidden" name="td_active" value="auto">-->
  343.                 <!--                --><?php //submit_button('Activate theme'); ?>
  344.                 <!---->
  345.                 <!--            </form>-->
  346.  
  347.             </div>
  348.         </div>
  349.  
  350.  
  351.         <?php
  352.     }
  353.  
  354.  
  355.     // all admin pages that begin with td_ do now show the message
  356.     private function check_if_is_our_page() {
  357.         if (isset($_GET['page']) and substr($_GET['page'], 0, 3) == 'td_') {
  358.             return true;
  359.         }
  360.         return false;
  361.     }
  362.  
  363.  
  364.  
  365.     function td_js_msg() {
  366.         if ($this->check_if_is_our_page() === true || td_api_features::is_enabled('require_activation') === false) {
  367.             return;
  368.         }
  369.         ?>
  370.         <div class="error">
  371.             <p><?php echo '<strong style="color:red"> Please activate the theme! </strong> - <a href="' . wp_nonce_url( admin_url( 'admin.php?page=td_cake_panel' ) ) . '">Click here to enter your code</a> - if this is an error please contact us at contact@tagdiv.com - <a href="http://forum.tagdiv.com/how-to-activate-the-theme/">How to activate the theme</a>'; ?></p>
  372.         </div>
  373.         <?php
  374.     }
  375.  
  376.  
  377.     function td_js_msg_2() {
  378.         if ($this->check_if_is_our_page() === true || td_api_features::is_enabled('require_activation') === false) {
  379.             return;
  380.         }
  381.         ?>
  382.         <div class="error">
  383.             <p>
  384.                 Activate <?php echo TD_THEME_NAME ?> to enjoy the full benefits of the theme. We're sorry about this extra step but we built the activation system to prevent
  385.                 mass piracy of our themes, this allows us to better serve our paying customers.
  386.  
  387.                 <strong>An active theme comes with free updates, top notch support, guaranteed latest WordPress support</strong>.
  388.  
  389.             </p>
  390.             <p><?php echo '<strong style="color:red"> Please activate the theme! </strong> - <a href="' . wp_nonce_url( admin_url( 'admin.php?page=td_cake_panel' ) ) . '">Click here to enter your code</a> - if this is an error please contact us at contact@tagdiv.com - <a href="http://forum.tagdiv.com/how-to-activate-the-theme/">How to activate the theme</a>'; ?></p>
  391.         </div>
  392.         <?php
  393.     }
  394. }
  395.  
  396.  
  397.  
  398. class td_version_check {
  399.     private $cron_task_name = 'td_check_version';
  400.  
  401.  
  402.     function __construct()
  403.     {
  404.         add_action('td_wp_booster_loaded', array($this, '_compare_theme_versions'));
  405.  
  406.         add_action($this->cron_task_name, array($this, '_check_for_updates'));
  407.  
  408.         add_filter( 'cron_schedules', array($this, '_schedule_modify_add_three_days') );
  409.  
  410.         if (wp_next_scheduled($this->cron_task_name) === false) {
  411.             wp_schedule_event(time(), 'three_days', $this->cron_task_name);
  412.         }
  413.  
  414.         add_action('switch_theme', array($this, 'on_switch_theme_remove_cron'));
  415.  
  416.     }
  417.  
  418.  
  419.     /**
  420.      * connect to api server and check if a new version is available
  421.      */
  422.     function _check_for_updates() {
  423.         // default base currency is eur and it returns all rates
  424.         $api_url = 'http://td_cake.tagdiv.com/td_cake/get_current_version.php?n=' . TD_THEME_NAME . '&v=' . TD_THEME_VERSION;
  425.         $json_api_response = td_remote_http::get_page($api_url, __CLASS__);
  426.  
  427.         // check for a response
  428.         if ($json_api_response === false) {
  429.             td_log::log(__FILE__, __FUNCTION__, 'Api call failed', $api_url);
  430.         }
  431.  
  432.         // try to decode the json
  433.         $api_response = @json_decode($json_api_response, true);
  434.         if ($api_response === null and json_last_error() !== JSON_ERROR_NONE) {
  435.             td_log::log(__FILE__, __FUNCTION__, 'Error decoding the json', $api_response);
  436.         }
  437.  
  438.         //valid response
  439.         if (!empty($api_response['version']) && !empty($api_response['update_url'])) {
  440.             td_util::update_option('td_latest_version', $api_response['version']);
  441.             td_util::update_option('td_update_url', $api_response['update_url']);
  442.         }
  443.     }
  444.  
  445.  
  446.     /**
  447.      * compare current version with latest version
  448.      */
  449.     function _compare_theme_versions() {
  450.         $td_theme_version = TD_THEME_VERSION;
  451.  
  452.         //don't run on deploy
  453.         if ($td_theme_version == '__td_deploy_version__') {
  454.             return;
  455.         }
  456.  
  457.         $td_latest_version = td_util::get_option('td_latest_version');
  458.         //latest version is not set
  459.         if (empty($td_latest_version)) {
  460.             return;
  461.         }
  462.  
  463.         $td_update_url = td_util::get_option('td_update_url');
  464.         //update url is not set
  465.         if (empty($td_update_url)) {
  466.             return;
  467.         }
  468.  
  469.         //compare theme's current version with the official version
  470.         $compare_versions = version_compare($td_theme_version, $td_latest_version, '<');
  471.  
  472.         if ($compare_versions === true) {
  473.             //update is available - add variables used by td_theme_update js function
  474.             td_js_buffer::add_to_wp_admin_footer('var tdUpdateAvailable = "' . $td_latest_version . '";');
  475.             td_js_buffer::add_to_wp_admin_footer('var tdUpdateUrl = "' . $td_update_url . '";');
  476.         }
  477.     }
  478.  
  479.  
  480.     /**
  481.      * on switch theme remove wp cron task
  482.      */
  483.     function on_switch_theme_remove_cron() {
  484.         wp_clear_scheduled_hook($this->cron_task_name);
  485.     }
  486.  
  487.  
  488.  
  489.     /**
  490.      * @return mixed
  491.      */
  492. function _schedule_modify_add_three_days( $schedules ) {
  493.     $schedules['three_days'] = array(
  494.         'interval' => 259200, // 3 days in seconds
  495.         'display' => 'three_days'
  496.     );
  497.     return $schedules;
  498.  
  499. }
  500. }
  501.  
  502. //execute only if the updates flag is enabled
  503. if (td_api_features::is_enabled('check_for_updates')) {
  504.     new td_version_check();
  505. }
  506.  
  507. new td_js();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement