-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventbriteFeedImport.php
More file actions
142 lines (105 loc) · 3.7 KB
/
eventbriteFeedImport.php
File metadata and controls
142 lines (105 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/*
Plugin Name: EventBrite Feed Importer
Description: A plugin to import the RSS feed of an eventBrite event and create posts from the aquired events.
Author: JPG
Version: 1.0
*/
require 'definitions.php';
if ( ! defined( 'EB_USERID' ) ) define( 'EB_USERID' , '2447513500' );
if ( ! defined( 'FEED_URL' ) ) define( 'FEED_URL' , 'http://www.eventbrite.com/rss/organizer_list_events/' );
register_activation_hook(__FILE__, 'creatScheduledEventForEventbriteImport');
add_action('performDailyTasks', 'createNewPostForEvents');
add_action('wp', 'createNewPostForEvents'); //USING FOR TESTING FUNCTIONALITY ONLY
/**
* Creates a scheduled event for RSS feed importer functions
* @return [type] [description]
*/
function creatScheduledEventForEventbriteImport() {
if( !wp_next_scheduled( 'createNewPostForEvents' ) ) {
wp_schedule_event( time(), 'daily', 'createNewPostForEvents' );
}
}
/**
* Returns raw data from http request
* @param [URL] $feed [URL of feed]
* @param [int] $id [User ID from feed's website]
* @return [array] [Data from given URL formatted into array]
*/
function retrieveEventbriteFeedAddress( $feed = FEED_URL, $id = EB_USERID ) {
$response = wp_remote_get($feed . $id);
return $response;
}
/**
* Checks the feed address contains the appropriate content
* @param string $contentType [Type of content that is required]
* @return [boolean] [Returns true if desired content type, else returns false]
*/
function checkEventbriteFeedAddressIsValid( $contentType = 'application/rss+xml' ) {
if ( retrieveEventbriteFeedAddress()['headers']['content-type'] == $contentType ) {
return true;
}
else {
return false;
}
}
/**
* Imports RSS feed from given address
* @param [URL] $feed_address [URL of feed to import]
* @return [object] [Simplexml formatted object of feed]
*/
function importEventbriteRssFeed( $feed = FEED_URL, $id = EB_USERID ) {
if ( checkEventbriteFeedAddressIsValid() == true ) {
$event_feed = simplexml_load_file( $feed . $id, 'SimpleXMLElement', LIBXML_NOCDATA);
return $event_feed;
}
}
/**
* Check whether post already exists before creating new one
* @param [object] $event [Current object to inspect in RSS feed]
* @return [boolean] [Returns true if post already exists, else returns false]
*/
function checkIfPostExists( $event ) {
if( null == get_page_by_title( strval($event->title), 'array', 'post' ) && $event->title != 'Test Title') {
return false;
} else {
return true;
}
}
/**
* Creates new post with desired attributes
* @param [object] $event [Current object to inspect in RSS feed]
* @return [array] [Details needed for creation of new post]
*/
function createNewPost( $event ) {
$new_post = array(
'post_content' => strval($event->description), // The full text of the post.
'post_title' => strval($event->title), // The title of your post.
'post_status' => 'publish', // Default 'draft'.
'post_type' => 'post', // Default 'post'.
'post_date' => date( 'Y-m-d H:i:s', strtotime(strval($event->pubDate))), // The time post was made.
'post_category' => array(get_cat_ID('Events')) // Default empty.
);
return $new_post;
}
/**
* Inserts new post
* @param [array] $new_post [Details needed for creation of new post]
* @return [type] [description]
*/
function insertNewPost( $new_post ) {
wp_insert_post( $new_post );
}
/**
* Retrieves list of events and loops through each one
* @return [type] [description]
*/
function createNewPostForEvents() {
$events = importEventbriteRssFeed()->channel->item;
foreach ($events as $event) {
if ( checkIfPostExists( $event ) == false ) {
insertNewPost( $event );
createNewPost( $new_post );
}
}
}