0

I'm building a plugin that requires a database file which is updated monthly. I would like to write a function that automatically replaces the old file once every 30 days.

Here's what I have so far. Am I on the right track?

add_action( 'plugins_loaded', 'update_some_file' );

function update_some_file() {

    $remotefile = curl_init('http://www.somesite.com/somefile.dat');
    curl_setopt($remotefile, CURLOPT_NOBODY, true);
    curl_setopt($remotefile, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($remotefile, CURLOPT_FILETIME, true);
    $result = curl_exec($remotefile);

    if ($result === false) {
        die (curl_error($remotefile)); 
    }

    $timestamp_r = curl_getinfo($remotefile, CURLINFO_FILETIME);

    $localfile = '/path/to/somefile.dat';

    if ( file_exists( $localfile ) && ( date("Y-m-d H:i:s", $timestamp_r) - date("Y-m-d H:i:s", filemtime($localfile)) >= 30 days ) ) { // obviously need some help here.
        rename('/path/to/somefile.dat', '/path/to/old_somefile.dat');
        $newfile = file_get_contents('http://www.somesite.com/somefile.dat');
        file_put_contents('/path/to/somefile.dat', $newfile);
        unlink('/path/to/old_somefile.dat')
    }
}

Or is there a simpler/better way to do this?

UPDATE:

I verified the file manipulation part is working:

add_action( 'plugins_loaded', 'update_some_file' );
function update_some_file() {
    $localfile = dirname( __FILE__ ) . '/includes/localfile.txt';
    if ( file_exists( $localfile ) ) {
        rename(dirname( __FILE__ ) . '/includes/localfile.txt', dirname( __FILE__ ) . '/includes/old/localfile.txt');
        $newfile = file_get_contents('http://somesite.com/localfile.txt');
        file_put_contents(dirname( __FILE__ ) . '/includes/localfile.txt', $newfile);
        unlink(dirname( __FILE__ ) . '/includes/old/localfile.txt');
    }
}

UPDATE:

Here's the the final working code thanks to fischi's help. (I set the schedule to 5 mins to test it)

add_filter( 'cron_schedules', 'lsmicron_add_intervals'); 
function lsmicron_add_intervals( $schedules ) {
    $schedules['fiveminutes'] = array(
        'interval' => 300,
        'display' => __('5 Minutes')
    );
    return $schedules;
}

add_action('lsmicron_fiveminutes_event', 'lsmi_geoip_update');
if ( !wp_next_scheduled( 'lsmicron_fiveminutes_event' ) ) {
    wp_schedule_event( time(), 'fiveminutes', 'lsmicron_fiveminutes_event' );
}

function lsmi_geoip_update() {
    $localfile = dirname( __FILE__ ) . '/includes/localfile.txt';
    if ( file_exists( $localfile ) ) {
        rename(dirname( __FILE__ ) . '/includes/localfile.txt', dirname( __FILE__ ) . '/includes/old/localfile.txt');
        $newfile = file_get_contents('http://example.com/localfile.txt');
        file_put_contents(dirname( __FILE__ ) . '/includes/localfile.txt', $newfile);
        unlink(dirname( __FILE__ ) . '/includes/old/localfile.txt');
    }
}

1 Answer 1

2

First things first, I suppose that your function already works - I did not debug it, but wanted to point you in the right direction of WordPress scheduling of events.

You should use wp-cron for that. It let's you schedule events, and let them be handled by WordPress.

Please keep in mind that this is not a real cronjob, and depends on your site being called. If you need the file to be updated wether or not visitors are on your site, consider calling wp-cron by a real cronjob.

At first, you need to register the monthly (or in this case 30 days). You can define any interval there, whatever you need, just pass the interval in seconds.

function f711_add_intervals($schedules) {
    $schedules['thirtydays'] = array(
        'interval' => ( 30 * 24 * 60 * 60 ), // time in seconds.
        'display' => __('30 Days')
    );
    return $schedules;
}
add_filter( 'cron_schedules', 'f711_add_intervals'); 

Now you can register a scheduled event, but only after you checked if it isn't already registered:

// function to register the event
function f711_activation() {

    // check if event is already scheduled
    if ( !wp_next_scheduled( 'f711_thirtydays_event' ) ) {
        // schedule event with the schedule 'thirtydays', calling the action hook
        wp_schedule_event( current_time( 'timestamp', 'thirtydays', 'f711_thirtydays_event' );
    }

}

// define the hook, calling your function
add_action('f711_thirtydays_event', 'update_some_file');

You can look at the schedule using the wp_get_schedule() function, to check if everything works out correctly.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.