-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcronjob.php
112 lines (89 loc) · 2.65 KB
/
cronjob.php
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
<?php
// /*
// This script is a cron job. It checks the Smugmug site and calls an XML-RPC function if there are any new
// albums on the specified user's account.
//
// NEEDS A DB SET UP WITH THE TABLE "ALBUMS" WITH AN INT COLUMN CALLED "ID" AND ALL VARIABLES SET BENEATH
// */
require_once "configcron.php";
require_once "phpSmug/phpSmug.php";
//$apiKey = "apikey";
$AppNameVersion = "kvarteret/1.0";
$domain = "http://www.kvarteret.no";
//$mailAdress = "mail@domain.com";
//$password = "password";
//$dbName = "smugTemp";
//$dbUser = "root";
//$dbPass = "pass";
//error_log("before getting values");
$smugObject = new phpSmug( "APIKey=" . $apiKey, "AppName=" . $AppNameVersion . "(" . $domain . ")" );
$smugObject->login( "EmailAddress=" . $mailAdress, "Password=" .$password );
//error message in cache function from phpSmug. No Cache is used.
//$smugObject->enableCache();
$albums = $smugObject->albums_get();
$db_albums = array();
//get all album id's from database
try {
$dbh = new PDO('mysql:host=localhost;dbname=' . $dbName, $dbUser, $dbPass);
foreach($dbh->query('SELECT id from albums') as $row) {
array_push($db_albums, $row);
}
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
error_log(count($albums) . "\n");
//compare to albums in $albums array
foreach ($albums as $album) {
$isInDb = false;
foreach($db_albums as $db_album) {
if($db_album['id'] == $album['id']) {
$isInDb = true;
}
}
if(!$isInDb) {
addNewAlbum($album, $dbh);
}
}
$dbh = null;
//Function for adding new album to the db and creating a draft in wp.
function addNewAlbum($album, $dbh) {
global $dbName;
$albumIsPosted = false;
error_log("inside add new album");
//TODO make the draft
$draftMade = TRUE;
//if it succeded add the id to the database
if($draftMade) {
$statement = "INSERT INTO `albums` (`id`) VALUES ('" . $album['id'] . "')";
error_log($statement . "\n");
try {
$sth = $dbh->prepare($statement);
$sth->execute();
$albumIsPosted=true;
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
}
if($albumIsPosted)
{
$new_post = array(
'post_title' => 'New Album',
'post_content' => "[DakSmugInsert album_id={$album['id']} album_key={$album['Key']}]",
'post_status' => 'draft',
'post_date' => date('Y-m-d H:i:s'),
//'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(0)
);
//echo "creating posts";
$post_id = wp_insert_post($new_post,true);
if ( is_wp_error( $post_id ) ) {
$error_string = $post_id->get_error_message();
echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
}
}
}
?>