Skip to content

Instantly share code, notes, and snippets.

@shameemreza
Last active August 18, 2021 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shameemreza/340e8447a7bc25d02e3c73b764713e58 to your computer and use it in GitHub Desktop.
Save shameemreza/340e8447a7bc25d02e3c73b764713e58 to your computer and use it in GitHub Desktop.
<?php
register_activation_hook( __FILE__, 'ga_register_cron' );
function ga_register_cron() {
// Si no existe el evento, lo registramos
if ( ! wp_next_scheduled( 'ga_daily_cron' ) ) {
wp_schedule_event( current_time( 'timestamp' ), 'daily', 'ga_daily_cron' );
}
}
function update_ga_js() {
$remoteFile = 'https://www.google-analytics.com/analytics.js';
$localFilePath = ABSPATH . 'ga/';
$localFile = $localFilePath . 'local-analytics.js';
if ( ! file_exists( $localFile ) ) {
wp_mkdir_p( $localFilePath );
}
if ( ! is_writable( $localFile ) ) {
$stat = @ stat( dirname( $localFile ) );
$perms = $stat['mode'] & 0007777;
$perms = $perms & 0000666;
chmod( $localFile, $perms );
clearstatcache();
}
$response = wp_safe_remote_get(
$remoteFile,
array(
'timeout' => 10,
'stream' => true,
'filename' => $localFile,
)
);
}
add_action( 'ga_daily_cron', 'update_ga_js' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment