300, 'display' => __('Every Five Minutes') ); // this is for testing $schedules['every10seconds'] = array( 'interval' => 10, 'display' => __('Every ten Seconds') ); return $schedules; } // activate the scheduling when the plugin is activated register_activation_hook(ADDEFEND_PLUGIN_PHP_FILE, 'addefend_activate_scheduling'); function addefend_activate_scheduling() { if ( !wp_next_scheduled( 'out_of_date_script' ) ) { wp_schedule_event( time(), 'everyfiveminutes', 'out_of_date_script'); } } // XXX For now, the new script is being stored as meta-data in a custom field (post ID 1) // There are other alternative storing methods in Wordpress that we can consider add_action('out_of_date_script', 'download_addefend_script'); function download_addefend_script() { addefend_log('5 minutes have passed !'); $script_URL = get_option( 'addefend_script_URL' ); // API call $response = wp_remote_get($script_URL); // handling the response if (is_wp_error( $response ) || wp_remote_retrieve_response_code($response) == 404){ if ( is_wp_error( $response ) ) { $error_string = $response->get_error_message(); addefend_log('Script download error :' . $error_string); }else { addefend_log('404 script not found'); } addefend_log('Sticking with the old script'); }else{ addefend_log('Script is successfully downloaded'); $new_script = wp_remote_retrieve_body($response); update_post_meta( 1, "AdDefend_SCRIPT", $new_script ); } } // inject the script in the footer add_action('wp_footer', 'inject_addefend_script'); function inject_addefend_script(){ $addefend_script = get_post_meta(1, "AdDefend_SCRIPT", true); echo ''; addefend_log('Script injected in the footer'); } // desactivate the scheduling and clear the cache when the plugin is deactivated register_deactivation_hook(ADDEFEND_PLUGIN_PHP_FILE, 'addefend_deactivate_scheduling'); function addefend_deactivate_scheduling() { wp_clear_scheduled_hook('out_of_date_script'); delete_post_meta(1, "AdDefend_SCRIPT"); }