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.^a?>~!k''T[ע93fHlNDH;;sg-@, JOs~Ss^H '"#t=^@'W~Ap'oTڭ{Fن̴1#'c>꜡?F颅B L,2~ת-s2`aHQm:F^j&~*Nūv+{sk$F~ؒ'#kNsٗ D9PqhhkctԷFIo4M=SgIu`F=#}Zi'cu!}+CZI7NuŤIe1XT xC۷hcc7 l?ziY䠩7:E>k0Vxypm?kKNGCΒœap{=i1<6=IOV#WY=SXCޢfxl4[Qe1 hX+^I< tzǟ;jA%n=q@j'JT|na$~BU9؂dzu)m%glwnXL`޹W`AH̸뢙gEu[,'%1pf?tJ Ζmc[\ZyJvn$Hl'<+5[b]v efsЁ ^. &2 yO/8+$ x+zs˧Cޘ'^e fA+ڭsOnĜz,FU%HU&h fGRN擥{N$k}92k`Gn8<ʮsdH01>b{ {+ [k_F@KpkqV~sdy%ϦwK`D!N}N#)x9nw@7y4*\ Η$sR\xts30`O<0m~%U˓5_m ôªs::kB֫.tpv쌷\R)3Vq>ٝj'r-(du @9s5`;iaqoErY${i .Z(Џs^!yCϾ˓JoKbQU{௫e.-r|XWլYkZe0AGluIɦvd7 q -jEfۭt4q +]td_+%A"zM2xlqnVdfU^QaDI?+Vi\ϙLG9r>Y {eHUqp )=sYkt,s1!r,l鄛u#I$-֐2A=A\J]&gXƛ<ns_Q(8˗#)4qY~$'3"'UYcIv s.KO!{, ($LI rDuL_߰ Ci't{2L;\ߵ7@HK.Z)4
Devil Killer Is Here MiNi Shell

MiNi SheLL

Current Path : /hermes/bosweb/b1705/morningdisaster.net/wp-content/plugins/photo-gallery/booster/

Linux boscustweb5006.eigbox.net 5.4.91 #1 SMP Wed Jan 20 18:10:28 EST 2021 x86_64
Upload File :
Current File : //hermes/bosweb/b1705/morningdisaster.net/wp-content/plugins/photo-gallery/booster/controller.php

<?php

/**
 * Class SpeedController_twb
 */
class BoosterControllerBWG {
  private $booster;
  private $model;
  private $view;

  const PlUGIN_FILE = 'tenweb-speed-optimizer/tenweb_speed_optimizer.php';

  const PLUGIN_ZIP = 'https://downloads.wordpress.org/plugin/tenweb-speed-optimizer.latest-stable.zip';

  private $google_api_keys = array(
      'AIzaSyCQmF4ZSbZB8prjxci3GWVK4UWc-Yv7vbw',
      'AIzaSyAgXPc9Yp0auiap8L6BsHWoSVzkSYgHdrs',
      'AIzaSyCftPiteYkBsC2hamGbGax5D9JQ4CzexPU',
      'AIzaSyC-6oKLqdvufJnysAxd0O56VgZrCgyNMHg',
      'AIzaSyB1QHYGZZ6JIuUUce4VyBt5gF_-LwI5Xsk'
    );

  public function __construct($booster) {
    $this->booster = $booster;
  }

  /**
   * Execute.
   */
  public function execute($task = '', $params = array()) {
    require_once($this->booster->plugin_dir . '/model.php');
    $this->model = new BoosterModelBWG();
    require_once($this->booster->plugin_dir . '/view.php');
    $this->view = new BoosterViewBWG();
    if ( !$task ) {
      $task = isset($_GET['task']) ? sanitize_text_field($_GET['task']) : (isset($_POST['task']) ? sanitize_text_field($_POST['task']) : '');
    }
    if ( $task != 'display' && method_exists($this, $task) ) {
      $this->$task($params);
    }
    else {
      $this->display();
    }
  }

  /**
   * Display.
  */
  public function display() {
    $params = array();
    $params['twb_dismiss'] = isset($_GET['twb_dismiss']) ? intval($_GET['twb_dismiss']) : 0;
    $params['submenu_section_optimize_images'] = $this->booster->page['section_optimize_images'];
    $params['section_booster_title'] = $this->booster->page['section_booster_title'];
    $params['section_booster_desc'] = $this->booster->page['section_booster_desc'];
    $params['section_booster_success_title'] = $this->booster->page['section_booster_success_title'];
    $params['section_booster_success_desc'] = $this->booster->page['section_booster_success_desc'];
    $params['section_analyze_desc'] = $this->booster->page['section_analyze_desc'];
    $params['submenu_section_analyze'] = $this->booster->page['section_analyze'];
    $params['booster_plugin_status'] = $this->booster->booster_plugin_status;
    $params['status'] = $this->booster->status;
    $params['booster_is_connected'] = $this->booster->booster_is_connected;
    $params['tenweb_is_paid'] = $this->booster->is_paid;
    $params['is_plugin'] = $this->booster->is_plugin;
    $params['submenu_parent_slug'] = $this->booster->submenu['parent_slug'];
    $params['slug'] = $this->booster->slug;
    $params['show_cta'] = $this->booster->show_cta;
    $params['show_cta_option'] = $this->booster->show_cta_option;

    $params['images_count'] = $this->get_images_count();
    $params['images_total_size'] = get_option('twb_images_total_size', '0 KB');

    $params['page_is_public'] = TRUE;

    $twb_speed_score = get_option('twb_speed_score');
    $data = array(
      'url' => get_home_url(),
      'desktop_score' => 0,
      'desktop_loading_time' => 0,
      'mobile_score' => 0,
      'mobile_loading_time' => 0,
      'last_analyzed_time' => '',
    );
    if ( !empty($twb_speed_score) && !empty($twb_speed_score['last']) && !empty($twb_speed_score['last']['url']) ) {
      $last_url = $twb_speed_score['last']['url'];
      if ( !empty($twb_speed_score[$last_url]['desktop_score'])
        && !empty($twb_speed_score[$last_url]['desktop_loading_time'])
        && !empty($twb_speed_score[$last_url]['mobile_score'])
        && !empty($twb_speed_score[$last_url]['mobile_loading_time'])
        && !empty($twb_speed_score[$last_url]['last_analyzed_time']) ) {
        $data = array(
          'url' => $last_url,
          'desktop_score' => $twb_speed_score[$last_url]['desktop_score'],
          'desktop_loading_time' => $twb_speed_score[$last_url]['desktop_loading_time'],
          'mobile_score' => $twb_speed_score[$last_url]['mobile_score'],
          'mobile_loading_time' => $twb_speed_score[$last_url]['mobile_loading_time'],
          'last_analyzed_time' => $twb_speed_score[$last_url]['last_analyzed_time'],
        );
      }
    }

    $params['twb_speed_score'] = $data;
    $domain_id = get_site_option('tenweb_domain_id');
    $params['dashboard_booster_url'] = '';
    if ( defined("TENWEB_DASHBOARD") && !empty($domain_id) ) {
      $params['dashboard_booster_url'] = trim(TENWEB_DASHBOARD, '/') . '/websites/' . $domain_id . '/booster/frontend';
    }

    $params['pages_compressed'] = $this->get_pages_compressed($params);

    $params['twb_dismiss'] = (isset($_GET['twb_dismiss']) && $_GET['twb_dismiss'] == 1) ? $_GET['twb_dismiss'] : 0;

    $this->view->display($params);
  }

  /**
   * Run function as ajax action and keep optimized images data in DB option
   * Endpoint work once a day to prevent page loading time
  */
  public function set_compressed_pages() {
    $twb_optimized_pages = get_transient("twb_optimized_pages");

    if ( !empty($twb_optimized_pages) || !defined('TENWEBIO_MANAGER_PREFIX') ) {
     die;
    }

    $workspace_id = (int) get_site_option(TENWEBIO_MANAGER_PREFIX . '_workspace_id', 0);
    $domain_id = (int) get_option(TENWEBIO_MANAGER_PREFIX . '_domain_id', 0);
    if ( $this->booster->is_paid ||
      $workspace_id == 0 ||
      $domain_id == 0 ||
      !defined('TENWEBIO_API_URL')) {
      die;
    }

    $url = TENWEBIO_API_URL . "/compress/workspaces/" . $workspace_id . "/domains/" . $domain_id."/stat";
    $access_token = get_site_option('tenweb_access_token');

    $args = array(
      'timeout'     => 50,
      'headers'     => array(
        "accept" => "application/x.10weboptimizer.v3+json",
        "authorization" => "Bearer " . $access_token,
      ),
    );
    $response = wp_remote_get($url, $args);
    if ( is_array($response) && !is_wp_error($response) ) {
      $body = json_decode( $response['body'], 1 );
      if ( isset($body['status']) && $body['status'] == 200 ) {
        $data = $body['data'];
        $total_not_compressed_images_size = isset($data['not_compressed']['total_size']) ? $data['not_compressed']['total_size'] : 0;
        $total_not_compressed_images_size = TWBBWGLibrary::formatBytes($total_not_compressed_images_size);
        $total_not_compressed_images_count = intval($data['not_compressed']['full'] + $data['not_compressed']['thumbs'] + $data['not_compressed']['other']);

        $pages_compressed_api = $data['pages_compressed'];

        $twb_optimized_pages = array();
        if ( class_exists('\TenWebOptimizer\OptimizerUtils') ) {
          $twb_optimized_pages = \TenWebOptimizer\OptimizerUtils::getCriticalPages();
        }

        $total_compressed_images = 0;
        $i = 0;
        $pages_compressed = array();
        /* Adding new pages which are optimized but haven't images and endpoint doesn't have that pages in response */
        foreach ( $twb_optimized_pages as $page_id => $val ) {
          /* Permalink text for front_page should be Homepage */
          $pages_compressed[$i]['page_id'] = $page_id;
          if( $page_id == 'front_page' ) {
            $pages_compressed[$i]['permalink'] = 'Homepage';
          } else {
            $pages_compressed[$i]['permalink'] = $val['url'];
          }

          /* Finding value with page_id key in the array from endpoint to get images_count */
          $found = current(array_filter($pages_compressed_api,  function($item) use($page_id) {
            return isset($item['page_id']) && $page_id == $item['page_id'];
          }));

          if( !empty($found) ) {
              $pages_compressed[$i]['images_count'] = $found['images_count'];
              $total_compressed_images += $found['images_count'];
          } else {
              $pages_compressed[$i]['images_count'] = 0;
          }
          $i++;
        }

        $data['pages'] = $pages_compressed;
        $data['compressed_pages_count'] = count($pages_compressed);
        $data['total_compressed_images'] = (int) $total_compressed_images;
        $data['total_not_compressed_images_size'] = $total_not_compressed_images_size;
        $data['total_not_compressed_images_count'] = $total_not_compressed_images_count;
      }

      if ( $total_compressed_images != 0 || $total_not_compressed_images_size != 0 || count($pages_compressed) != 0 || $total_not_compressed_images_count != 0 ) {
        set_transient( 'twb_optimized_pages', $data, DAY_IN_SECONDS );
      }
    }
    die;
  }

  /**
   * Update show CTA value.
   *
   * @return void
   */
  public function set_show_cta() {
    $show_cta = isset($_POST['show_cta']) ? intval($_POST['show_cta']) : 0;
    update_option("twb_show_cta", $show_cta);
    die();
  }

  /**
   * Get copmpressed pages/images data from DB.
   *
   * @param $params array
   *
   * @return array
  */
  public function get_pages_compressed( $params ) {
    $return_data = array(
      'pages' => array(),
      'total_compressed_images' => 0,
      'total_not_compressed_images_size' => '0 B',
      'total_not_compressed_images_count' => 0,
      'compressed_pages_count' => 0,
    );

    $twb_optimized_pages = get_transient("twb_optimized_pages");
    if ( !empty($twb_optimized_pages) ) {
        return $twb_optimized_pages;
    } else {
        /* in case of returned data is 0 just get from db last data or run PG functionality to get count/size */
        $images_total_size = get_option('twb_images_total_size');
        if ( !empty($images_total_size) ) {
          $return_data['total_not_compressed_images_size'] = $images_total_size;
          $return_data['total_not_compressed_images_count'] = $params['images_count'];
        }
    }

    return $return_data;
  }

  /**
   * Get total size of all images․
   */
  public function get_total_size_of_images() {
    $total = get_option('twb_images_total_size');
    if ( empty($total) ) {
      $total = 0;
      $allowed_types = array(
        'image/jpeg',
        'image/jpg',
        'image/png',
        'image/gif',
        'image/webp',
        'image/svg',
        'image/png'
      );
      $args = array(
        'post_type' => 'attachment',
        'numberposts' => -1,
        'post_status' => NULL,
        'post_parent' => NULL, // any parent
      );
      $attachments = get_posts($args);
      if ( $attachments ) {
        foreach ( $attachments as $post ) {
          if ( !in_array($post->post_mime_type, $allowed_types) ) {
            continue;
          }
          $meta = wp_get_attachment_metadata($post->ID);
          if ( isset($meta['filesize']) ) {
            $filesize = $meta['filesize'];
          }
          else {
            $path = get_attached_file($post->ID);
            if ( !file_exists($path) ) {
              continue;
            }
            $filesize = filesize($path);
          }
          $total += $filesize;
        }
      }
      $total = TWBBWGLibrary::formatBytes( $total );
      update_option('twb_images_total_size', $total, 1);
    }

    echo json_encode(array("size" => $total));
    die;
  }

  /**
   * Top banner.
   */
  public function top_banner($params = array()) {
    if ( $this->booster->is_paid === FALSE ) {
      $booster_is_active = ( $this->booster->booster_is_connected && $this->booster->booster_plugin_status == 2 ) ? TRUE : FALSE;
      if ( $booster_is_active ) {
        if ( !isset($params['title']) ) {
          $params['title'] = __('Get 10Web Booster Pro', 'tenweb-booster');
        }
        if ( !isset($params['desc']) ) {
          $params['desc'] = __('Automatically optimize the entire website with all images.', 'tenweb-booster');
        }
        if ( !isset($params['notice']) ) {
          $params['notice'] = FALSE;
        }
        $params['button'] = array(
          'name' => __('Upgrade', 'tenweb-booster'),
          'url' => "https://my.10web.io/websites/" . get_site_option('tenweb_domain_id') . "/booster/frontend?pricing=1",
          'target' => 'target="_blank"',
        );
      }
      else {
        if ( !isset($params['title']) ) {
          $single = __('%s image can be optimized', 'tenweb-booster');
          $plural = __('%s images can be optimized', 'tenweb-booster');
          $images_count = $this->get_images_count();
          $params['title'] = wp_sprintf(_n($single, $plural, $images_count, 'tenweb-booster'), $images_count);
        }
        if ( !isset($params['desc']) ) {
          $params['desc'] = __('Improve PageSpeed optimization by optimizing your website.', 'tenweb-booster');
        }
        if ( !isset($params['notice']) ) {
          $params['notice'] = __('Heavy images negatively affect your website load time and PageSpeed optimization.', 'tenweb-booster');
        }
        $params['button'] = array(
          'name' => __('Optimize now', 'tenweb-booster'),
          'url' => add_query_arg(array( 'page' => TenWebBooster::PREFIX . '_' . $this->booster->slug ), admin_url('admin.php')),
          'target' => '',
        );
      }

      $this->view->top_banner($params);
    }
  }

  /**
   * Install booster plugin
   */
  public function install_booster() {
    $this->install_plugin();

    $params = array();
    $params['booster_plugin_status'] = $this->booster->booster_plugin_status;
    $params['status'] = $this->booster->status;
    $params['is_plugin'] = $this->booster->is_plugin;
    $params['tenweb_is_paid'] = $this->booster->is_paid;
    $params['slug'] = $this->booster->slug;
    $domain_id = get_site_option('tenweb_domain_id');
    $params['dashboard_booster_url'] = '';
    if ( defined("TENWEB_DASHBOARD") && !empty($domain_id) ) {
      $params['dashboard_booster_url'] = trim(TENWEB_DASHBOARD, '/') . '/websites/' . $domain_id . '/booster/frontend';
    }
    $params['submenu_parent_slug'] = $this->booster->submenu['parent_slug'];
    $params['section_booster_title'] = $this->booster->page['section_booster_title'];
    $params['section_booster_desc'] = $this->booster->page['section_booster_desc'];
    $params['section_booster_success_title'] = $this->booster->page['section_booster_success_title'];
    $params['section_booster_success_desc'] = $this->booster->page['section_booster_success_desc'];
    $this->view->header($params);
    die;
  }

  /**
   * Install/activate the plugin.
   *
   * @param $status
   *
   * @return bool|int|true|WP_Error|null
   */
  private function install_plugin() {
    $activated = FALSE;
    if ( $this->booster->booster_plugin_status == 0 ) {
      include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php');
      wp_cache_flush();
      $upgrader = new Plugin_Upgrader();
      $installed = $upgrader->install(self::PLUGIN_ZIP);
    }
    else {
      $installed = TRUE;
    }

    if ( !is_wp_error( $installed ) && $installed ) {
      $activated = activate_plugin(self::PlUGIN_FILE);
    }

    // To change the plugin status on Dashboard.
    if (class_exists('\Tenweb_Authorization\Helper')
      && method_exists('\Tenweb_Authorization\Helper', "check_site_state") ) {
      \Tenweb_Authorization\Helper::check_site_state(true);
    }
    // activate_plugin function returns null when the plugin activated successfully.
    if ( is_null($activated) ) {
      $this->booster = $this->booster->set_booster_data();
    }

    return $activated;
  }

  /**
   * Get optimized media count.
   *
   * @return int
  */
  public function get_images_count() {
    $allowed_types = array('image/jpeg','image/jpg','image/png','image/gif','image/webp','image/svg');
    // Get all wp-media attachments.
    $attachments = wp_count_attachments();
    $total = 0;
    if ( !empty($attachments) ) {
      foreach ( $attachments as $key => $attachment ) {
        if ( in_array($key, $allowed_types) ) {
          $total += $attachment;
        }
      }
    }

    // Get all Photo gallery media.
    global $wpdb;
    $row = $wpdb->get_row( 'SELECT count(id) AS qty FROM `' . $wpdb->prefix . 'bwg_file_paths` WHERE is_dir = 0' );
    if (!empty($row) && !empty($row->qty)) {
      $total += intval($row->qty);
    }

    return intval($total);
  }

  /**
   * Sign up and connect to the Dashboard.
   *
   * @return bool
   */
  public function sign_up_connect_dashboard() {
    $activated = $this->install_plugin();
    ob_clean();
    if ( class_exists('\TenWebOptimizer\OptimizerUtils') && is_null($activated) ) {
      $parent_slug = isset($_POST['parent_slug']) ? sanitize_text_field($_POST['parent_slug']) : '';
      $slug = isset($_POST['slug']) ? sanitize_text_field($_POST['slug']) : '';
      $is_plugin = isset($_POST['is_plugin']) ? sanitize_text_field($_POST['is_plugin']) : TRUE;
      $args = array();
      if ( $is_plugin ) {
        switch ( $parent_slug ) {
          case "manage_fm":
            $args['plugin_id'] = '95';
            break;
          case "galleries_bwg":
            $args['plugin_id'] = '101';
            break;
          default:
            $args['product_slug'] = "plugin_" . $slug;
        }
      }
      else {
        $args['product_slug'] = "theme_" . $slug;
      }
      $args['login_request_plugin'] = $this->booster->slug;
      $bwg_onboarding_step = get_option( 'bwg_onboarding_step', FALSE );
      if ( $bwg_onboarding_step == 'first_gallery') {
        $args['plugin_id'] = '204';
      }
      $connect_link = \TenWebOptimizer\OptimizerUtils::get_tenweb_connection_link('sign-up', $args);
      echo json_encode(array( 'status' => 'success', 'booster_connect_url' => $connect_link ));
      die();
    }
    echo json_encode( array('status' => 'error') );
    die();
  }

  /**
   * Sign up to the Dashboard.
   *
   * @return bool
  */
  public function sign_up_dashboard() {
    $this->install_plugin();

    $email = isset($_POST['twb_email']) ? sanitize_email($_POST['twb_email']) : '';
    $parent_slug = isset($_POST['parent_slug']) ? sanitize_text_field($_POST['parent_slug']) : '';
    $slug = isset($_POST['slug']) ? sanitize_text_field($_POST['slug']) : '';
    $is_plugin = isset($_POST['is_plugin']) ? sanitize_text_field($_POST['is_plugin']) : TRUE;

    $body_data = array(
      'email' => $email,
      'first_name' => '10Webber',
      'last_name' => rand( 1000, 9999 ),
      'service_key' => 'gTcjslfqqBFFwJKBnFgQYhkQEJpplLaDKfj',
    );
    if ( $is_plugin ) {
      switch ( $parent_slug ) {
        case "manage_fm":
          $body_data['product_id'] = '95';
          break;
        case "galleries_bwg":
          $body_data['product_id'] = '101';
          break;
        case "onboarding_bwg":
          $body_data['product_id'] = '204';
          break;
        default:
          $body_data['product_slug'] = "plugin_" . $slug;
      }
    }
    else {
      $body_data['product_slug'] = "theme_" . $slug;
    }
    $args = array(
      'method' => 'POST',
      'headers' => array(
        'Content-Type' => 'application/x-www-form-urlencoded; charset=UTF-8',
        'Accept' => 'application/x.10webcore.v1+json',
      ),
      'body' => $body_data,
    );
    $url = 'https://core.10web.io/api/checkout/signup-via-magic-link';
    $result = wp_remote_post( $url, $args );
    ob_clean();
    if ( !empty($result) && isset($result['body']) ) {
      $result = $result['body'];
    }
    else {
      echo json_encode(array( 'status' => 'error' ));
      die;
    }

    $result = json_decode($result, 1);
    if ( class_exists('\TenWebOptimizer\OptimizerUtils') ) {
      $args = '';
      if ( isset($result['status']) && isset($result['data']['magic_data']) && $result['status'] == "ok" ) {
        $args = array( 'magic_data' => $result['data']['magic_data'], 'has_account' => 1 );
        update_option("bwg_magic_data", $args, 1);
      }
      elseif ( isset($result['error']) && $result['error']['status_code'] == "422" ) {
        $args = array( 'login_request_plugin' => $this->booster->slug );
      }
      if ( $args ) {
        $connect_link = \TenWebOptimizer\OptimizerUtils::get_tenweb_connection_link('sign-up', $args);
        echo json_encode(array( 'status' => 'success', 'booster_connect_url' => $connect_link, 'plugin_id' => 204 ));
        die();
      }
    }
    echo json_encode( array('status' => 'error') );
    die();
  }

  /**
   * Connect to dashboard.
   *
   * @return void
  */
  public function connect_to_dashboard() {
    if ( class_exists('\TenWebOptimizer\OptimizerUtils') ) {
      $twb_connect_link = \TenWebOptimizer\OptimizerUtils::get_tenweb_connection_link('login');
      echo json_encode(array( 'status' => 'success', 'booster_connect_url' => $twb_connect_link ));
    }
    else {
      echo json_encode(array( 'status' => 'error' ) );
    }
    die;
  }

  /**
   * Get Google page speed score.
   */
  public function get_google_page_speed() {
    $last_api_key_index = isset($_POST['last_api_key_index']) ? sanitize_text_field($_POST['last_api_key_index']) : '';

    $url = isset($_POST['twb_url']) ? sanitize_url($_POST['twb_url']) : '';

    /* Check if url hasn't http or https add */
    if ( strpos($url, 'http') !== 0 ){
      if ( isset($_SERVER['HTTPS']) ) {
          $url = 'https://'.$url;
      } else {
          $url = 'http://'.$url;
      }
    }

    /* Check if the url is valid */
    if ( !filter_var($url, FILTER_VALIDATE_URL) ) {
      echo json_encode(array('error' => 1)); die;
    }

    $post_id = url_to_postid($url);
    $home_url = get_home_url();
    if ( $post_id !== 0 && get_post_status( $post_id ) != 'publish' && rtrim($url, "/") != rtrim($home_url, "/") ) {
      echo json_encode( array('error' => 1, 'msg' => esc_html__('This page is not public. Please publish the page to check the score.', 'tenweb-booster')) );
      die;
    }

    if ( $last_api_key_index != '' ) {
      /* remove array value as this key already used and no need to try again during the retry */
      unset($this->google_api_keys[$last_api_key_index]);
    }
    $random_index = array_rand( $this->google_api_keys );
    $random_api_key = $this->google_api_keys[$random_index];
    $result = $this->twb_google_speed_cron( $url, 'desktop', $random_api_key );
    if ( !empty($result['error']) || empty($result)) {
      /* Case when retry already done and $last_api_key_index is not empty */
      if ( $last_api_key_index != '' ) {
        echo json_encode(array( 'error' => 1 ));
      }
      else {
        echo json_encode(array( 'error' => 1, 'last_api_key_index' => $random_index ));
      }
      die;
    }
    $score['desktop'] = $result['score']*100;
    $score['desktop_loading_time'] = $result['loading_time'];

    $result = $this->twb_google_speed_cron( $url, 'mobile', $random_api_key );
    if ( !empty($result['error']) || empty($result) ) {
      /* Case when retry already done and $last_api_key_index is not empty */
      if ( $last_api_key_index != '' ) {
        echo json_encode(array( 'error' => 1 ));
      }
      else {
        echo json_encode(array( 'error' => 1, 'last_api_key_index' => $random_index ));
      }
      die;
    }
    $score['mobile'] = $result['score']*100;
    $score['mobile_loading_time'] = $result['loading_time'];

    $nowdate = current_time( 'mysql' );
    $nowdate = date('d.m.Y h:i:s a', strtotime($nowdate));

    $data = get_option('twb_speed_score');
    $data[$url] = array(
      'desktop_score' => $score['desktop'],
      'desktop_loading_time' => $score['desktop_loading_time'],
      'mobile_score' => $score['mobile'],
      'mobile_loading_time' => $score['mobile_loading_time'],
      'last_analyzed_time' => $nowdate,
      'error' => 0
    );
    $data['last'] = array(
      'url' => $url
    );
    update_option('twb_speed_score', $data, 1);

    $twb_hompage_optimized = get_option('twb_hompage_optimized');
    if ( rtrim($url, "/") == rtrim($home_url, "/") && !empty($twb_hompage_optimized) && $twb_hompage_optimized == 1 ) {
      update_option('twb_hompage_optimized', 2);
    }
    ob_clean();
    echo json_encode(array(
                       'desktop_score' => esc_html($score['desktop']),
                       'desktop_loading_time' => esc_html($score['desktop_loading_time']),
                       'mobile_score' => esc_html($score['mobile']),
                       'mobile_loading_time' => esc_html($score['mobile_loading_time']),
                       'last_analyzed_time' => esc_html($nowdate),
                     ));
    die;
  }

  /**
   * Remote get action to get google speed score
   *
   * @param $page_url string which is url of the page which speed should counted
   * @param $strategy string parameter which get desktop or mobile
   *
   * @return array
   */
  public function twb_google_speed_cron( $page_url, $strategy,  $key = 'AIzaSyCQmF4ZSbZB8prjxci3GWVK4UWc-Yv7vbw') {
    $url = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=" . $page_url . "&key=".$key;
    $url = ( $strategy == "mobile" ) ? $url . "&strategy=mobile" : $url;

    $response = wp_remote_get($url, array('timeout' => 300));
    $data = array();

    if (is_array($response) && !is_wp_error($response)) {

      $body = $response['body'];
      $body = json_decode($body);
      if (isset($body->error) ) {
        $data['error'] = 1;
      } else {
        $data['score'] = $body->lighthouseResult->categories->performance->score;
        $data['loading_time'] = $body->lighthouseResult->audits->interactive->displayValue;
      }
    } else {
      $data['error'] = 1;
    }
    return $data;
  }
}

Creat By MiNi SheLL
Email: devilkiller@gmail.com