Skip to content

Commit

Permalink
Версия 1.0.1
Browse files Browse the repository at this point in the history
Исправлены мелкие ошибки.
Добавлена работа через WP_Filesystem и очистка кеша WP Super Cache при обновлении файлов скриптов.
  • Loading branch information
kagg-design committed Dec 2, 2018
1 parent 162e591 commit 93bdad4
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 49 deletions.
79 changes: 79 additions & 0 deletions includes/class-pagespeed-filesystem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

/**
* Class PageSpeed_Filesystem
*/
class PageSpeed_Filesystem {

/**
* @var WP_Filesystem_Direct
*/
private $wp_filesystem = null;

/**
* PageSpeed_Filesystem constructor.
*/
public function __construct() {
$this->init();
}

/**
* Init filesystem.
*/
private function init() {
/** @var $wp_filesystem WP_Filesystem_Direct */
global $wp_filesystem;

add_filter( 'filesystem_method', 'set_direct_fs_method', PHP_INT_MAX );

if ( ! function_exists( 'WP_Filesystem' ) ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
}

if ( ! WP_Filesystem() ) {
throw new RuntimeException( __( 'Unable to get filesystem access', 'kagg-pagespeed-optimization' ) );
}

$this->wp_filesystem = $wp_filesystem;
}

/**
* Set direct FS method.
*
* @return string
*/
public function set_direct_fs_method() {
return 'direct';
}

/**
* Read file.
*
* @param string $filename Name of the file to read.
*
* @return bool|mixed
*/
public function read( $filename ) {
if ( ! $this->wp_filesystem ) {
return false;
}

return $this->wp_filesystem->get_contents( $filename );
}

/**
* Write file.
*
* @param string $filename Name of the file to write.
* @param string $content File content.
*
* @return bool
*/
public function write( $filename, $content ) {
if ( ! $this->wp_filesystem ) {
return false;
}

return $this->wp_filesystem->put_contents( $filename, $content, FS_CHMOD_FILE );
}
}
78 changes: 31 additions & 47 deletions includes/class-pagespeed-optimization.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
<?php

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}

/**
* Class PageSpeed_Optimization.
*
* @class PageSpeed_Optimization
* version 1.0.0
* version 1.0.1
*/
class PageSpeed_Optimization {

Expand All @@ -25,7 +21,7 @@ class PageSpeed_Optimization {
/**
* @var string Plugin version.
*/
public $version = '1.0.0';
public $version = '1.0.1';

/**
* @var string Absolute plugin path.
Expand Down Expand Up @@ -141,10 +137,6 @@ private function init_hooks() {
);
}

public function test_update_pagespeed_optimization_cache_action() {
echo 'test_update_pagespeed_optimization_cache_action';
}

/**
* Add settings page to the menu.
*/
Expand Down Expand Up @@ -637,26 +629,15 @@ public function check_cron() {
}
}

/**
* Check if local files are in place. Update them if not.
*/
private function check_local_files() {
foreach ( $this->local_filenames as $local_filename ) {
if ( ! file_exists( $this->plugin_path . $local_filename ) ) {
do_action( 'update_pagespeed_optimization_cache' );

return;
}
}
}

/**
* Update scripts cache.
*/
public function update_pagespeed_optimization_cache_action() {
$filesystem = new PageSpeed_Filesystem();

$remote_file = $this->remote_filenames['ga'];
$local_file = $this->plugin_path . $this->local_filenames['ga'];
$this->update_local_files( $remote_file, $local_file );
$this->update_local_file( $filesystem, $remote_file, $local_file );

$gmap_key = $this->get_option( 'gmap_key' );
$key = '';
Expand All @@ -665,20 +646,21 @@ public function update_pagespeed_optimization_cache_action() {
}
$remote_file = $this->remote_filenames['gmap'] . $key;
$local_file = $this->plugin_path . $this->local_filenames['gmap'];
$this->update_local_files( $remote_file, $local_file );
$this->update_local_file( $filesystem, $remote_file, $local_file );

$remote_file = $this->remote_filenames['ya_metrika'];
$local_file = $this->plugin_path . $this->local_filenames['ya_metrika'];
$this->update_local_files( $remote_file, $local_file );
$this->update_local_file( $filesystem, $remote_file, $local_file );
}

/**
* Update local file.
*
* @param PageSpeed_Filesystem $filesystem Filesystem.
* @param string $remote_file Remote file url.
* @param string $local_file Local file name.
*/
private function update_local_files( $remote_file, $local_file ) {
private function update_local_file( $filesystem, $remote_file, $local_file ) {
$args = array(
'method' => 'GET',
'redirection' => 1,
Expand All @@ -688,38 +670,40 @@ private function update_local_files( $remote_file, $local_file ) {
'body' => array(),
'cookies' => array(),
);
$result = wp_remote_post( $remote_file, $args );
$result = wp_remote_get( $remote_file, $args );

if ( ! is_wp_error( $result ) ) {
$response = $result['body'];
if ( ! empty( $response ) ) {
// @codingStandardsIgnoreStart
// Save the response to the local file
if ( ! file_exists( $local_file ) ) {
// Try to create the file, if doesn't exist
$fp = fopen( $local_file, 'w' );
fclose( $fp );
}

if ( is_writable( $local_file ) ) {
$fp = fopen( $local_file, 'w' );
if ( $fp ) {
fwrite( $fp, $response );
fclose( $fp );
}
$content = $result['body'];
if ( ! empty( $content ) ) {
$local_content = $filesystem->read( $local_file );
if ( $local_content !== $content ) {
$filesystem->write( $local_file, $content );
$this->clean_cache();
}
// @codingStandardsIgnoreEnd
}
}
}

/**
* Clean cache.
*/
private function clean_cache() {

// Clean cache of WP Super Cache plugin.
if ( function_exists( 'wp_cache_clean_cache' ) ) {
global $file_prefix;
wp_cache_clean_cache( $file_prefix, true );
return;
}
}

/**
* Add event to WP-Cron and check local files.
*/
public function activate_update_pagespeed_optimization_cache() {
if ( ! wp_next_scheduled( 'update_pagespeed_optimization_cache' ) ) {
wp_schedule_event( time(), 'twicedaily', 'update_pagespeed_optimization_cache' );
$this->check_local_files();
wp_schedule_event( time(), 'hourly', 'update_pagespeed_optimization_cache' );
do_action( 'update_pagespeed_optimization_cache' );
}
}

Expand Down
5 changes: 3 additions & 2 deletions pagespeed-optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin URI:
* Description: Optimize external scripts by storing them locally
* Author: KAGG Design
* Version: 1.0.0
* Version: 1.0.1
* Author URI: https://kagg.eu/en/
* Requires at least: 4.4
* Tested up to: 5.0
Expand Down Expand Up @@ -33,8 +33,9 @@ function init_pagespeed_optimization_class() {
static $plugin;

if ( ! isset( $plugin ) ) {
// Require main class of the plugin.
// Require plugin classes.
require_once dirname( __FILE__ ) . '/includes/class-pagespeed-optimization.php';
require_once dirname( __FILE__ ) . '/includes/class-pagespeed-filesystem.php';

$plugin = new PageSpeed_Optimization();
}
Expand Down

0 comments on commit 93bdad4

Please sign in to comment.