我想在本地为zopim聊天插件托管一个js文件

时间:2019-07-11 16:33:40

标签: javascript wordpress

所以我使用了zopim插件,它的功能很棒,但是它加载的某些代码加载速度非常慢,我想在本地托管它,以便它使用我的快速CDN,并最终加快了加载时间。

因此它加载的js在这里:https://v2.zopim.com/bin/v/widget_v2.303.js

它从插件中的php文件加载(请参见下面的代码)

我尝试将js文件上传到我的网站,并更改源代码以指向它,但它不起作用。

<?php

class Zopim_Widget
{
  // We need some CSS to position the paragraph
  public static function zopimme()
  {
    $subdomain = get_option( Zopim_Options::ZENDESK_OPTION_SUBDOMAIN );

    if ($subdomain) {
      echo Zopim_Widget::get_widget_code_using_subdomain($subdomain);
      return;
    }

    $current_user = wp_get_current_user();

    $code = get_option( Zopim_Options::ZOPIM_OPTION_CODE );

    if ( ( $code == "" || $code == "zopim" ) && !( isset( $_GET[ 'page' ] ) && preg_match( "/zopim/", $_GET[ 'page' ] ) ) && ( !preg_match( "/zopim/", $_SERVER[ "SERVER_NAME" ] ) ) ) {
      return;
    }

    echo "<!--Embed from Zendesk Chat Chat Wordpress Plugin v" . VERSION_NUMBER . "-->
    <!--Start of Zopim Live Chat Script-->
    <script type=\"text/javascript\">
    window.\$zopim||(function(d,s){var z=\$zopim=function(c){z._.push(c)},$=z.s=
    d.createElement(s),e=d.getElementsByTagName(s)[0];z.set=function(o){z.set.
      _.push(o)};z._=[];z.set._=[];$.async=!0;$.setAttribute('charset','utf-8');
      $.src='//v2.zopim.com/?" . $code . "';z.t=+new Date;$.
      type='text/javascript';e.parentNode.insertBefore($,e)})(document,'script');
      </script>";

      echo '<script>';
      if ( isset( $current_user ) ):
        $firstname = $current_user->display_name;
        $useremail = $current_user->user_email;
        if ( $firstname != "" && $useremail != "" ):
          echo "\$zopim(function(){\$zopim.livechat.set({name: '$firstname', email: '$useremail'}); });";
        endif;
      endif;

      echo Zopim_Options::get_widget_options();
      echo '</script>';
      echo "<!--End of Zendesk Chat Script-->";
    }

    public static function get_widget_code_using_subdomain( $subdomain )
    {
      $url = "https://ekr.zdassets.com/snippets/web_widget/" . $subdomain .".zendesk.com?dynamic_snippet=true";
      $response = wp_remote_get($url, array());


      if ( is_wp_error( $response ) ) {
        $error = array( 'wp_error' => $response->get_error_message() );
        return json_encode( $error );
      }

      return $response['body'];
    }
  }

0 个答案:

没有答案