-
-
Save shameemreza/340e8447a7bc25d02e3c73b764713e58 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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