Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function start_next_ical_update() {
- //fetch ical scheduled imports and get the ones that have not been manually imported for at least a day
- $args = array(
- 'post_type' => 'tribe-ea-record',
- 'post_status' => 'tribe-ea-schedule',
- 'ping_status' => 'schedule',
- 'post_mime_type' => 'ea/ical',
- 'posts_per_page' => -1,
- 'meta_query' => array(
- 'relation' => 'OR',
- array(
- 'key' => '_tribe_ics_hack',
- 'value' => time()-86400,
- 'compare' => ' '_tribe_ics_hack',
- 'compare' => 'NOT EXISTS'
- ) ),
- 'orderby' => '_tribe_ics_hack',
- 'order' => 'ASC');
- $query = new WP_Query( $args );
- //start the update for the one that has not been updated the longest
- if($query->have_posts()) {
- $item = $query->posts[0];
- $meta = get_post_meta($item->ID);
- $url = $meta['_tribe_aggregator_source'][0];
- error_log('started '.$url);
- update_ical_events($url);
- update_post_meta( $item->ID, '_tribe_ics_hack', time());
- }
- }
- //use the tribe cron every 15 minutes to start the update function
- add_action('tribe_aggregator_cron', 'start_next_ical_update', 10, 0);
- //during debugging use the json api to start the update manually
- //use curl http://your-domain.com/wp-json/pd/v1/updateevents
- /*add_action( 'rest_api_init', function () {
- register_rest_route( 'pd/v1', '/updateevents', array(
- 'methods' => 'GET',
- 'callback' => 'start_next_ical_update',
- ) );
- } );*/
- function update_ical_events($url) {
- //get ics contents from url
- ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0)');
- try {
- $ics = get_file_contents($url);
- if ($ics === false) {
- return new WP_Error( 'import_error', 'File was empty.');
- }
- } catch (Exception $e) {
- return new WP_Error( 'import_error', 'URL could not be reached.');
- }
- //get wp upload dir and a filename for our ics
- $upload_dir = wp_upload_dir();
- /*preg_match('/[A-Za-z0-9]+\.?[A-Za-z0-9]*$/', $url, $name); */
- $basename = wp_unique_filename($upload_dir['path'], 'manual.ics');
- $file_abs = $upload_dir['path'].'/'.$basename;
- $file_url = $upload_dir['url'] . '/' . $basename;
- //save the ics content to a local file
- $f = fopen($file_abs, "w") or die("Unable to open file!");
- if ( !$f ) {
- return new WP_Error( 'import_error', 'File could not be written.');
- }
- fwrite($f, $ics);
- fclose($f);
- //set permissions
- $stat = stat( dirname( $file_abs ));
- $perms = $stat['mode'] & 0000666;
- chmod( $file_abs, $perms );
- //and add attachement metadata to the file to get it into wp media
- $type = wp_check_filetype( basename( $basename ), null );
- $attachment = array(
- 'guid' => $file_url,
- 'post_mime_type' => $type['type'],
- 'post_title' => preg_replace( '/\.[^.]+$/', '', $basename ),
- 'post_content' => '',
- 'post_status' => 'inherit',
- 'post_author' => 1
- );
- $id = wp_insert_attachment( $attachment, $file_abs);
- require_once( ABSPATH . 'wp-admin/includes/image.php' );
- // Generate the metadata for the attachment, and update the database record.
- $attach_data = wp_generate_attachment_metadata( $id, $file_url );
- wp_update_attachment_metadata( $id, $attach_data );
- //prepare settings for event aggregator
- $meta = Array(
- 'origin' => 'ics',
- 'file' => $id,
- 'type' => 'manual',
- 'limit_type' => 'no_limit'
- );
- $args = array_merge($meta, Array(
- 'allow_multiple_organizers' => true,
- 'preview' => true
- ));
- //create new events aggregator import and get the data from the aggregator service
- $import = new Tribe__Events__Aggregator__API__Import();
- $response = $import->create($args);
- $response_data = $response->data;
- $meta['import_id'] = $response_data->import_id;
- //create and save new aggregator record
- $record = Tribe__Events__Aggregator__Records::instance()->get_by_origin( $meta['origin'] );
- $post = $record->create($meta['type'], $args, $meta);
- $record->set_status_as_pending();
- $record->update_meta( 'category', null);
- $record->update_meta( 'post_status', 'publish' );
- $record->finalize();
- //seems to be necessary for larger ics files, gets down failure rate
- sleep(5);
- //process the events and add them
- $result = $record->process_posts();
- $result->record = $record;
- return new WP_REST_Response( $result );
- }
- function get_file_contents($url, $post = null) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- if(!empty($post)) {
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
- }
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement