-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler-functions.php
126 lines (112 loc) · 4.09 KB
/
scheduler-functions.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
require_once dirname(__FILE__) . '/vendor/autoload.php'; // Ensure the path to autoload.php is correct based on your folder structure
// Function to post to Twitter using the TwitterOAuth library
function sm_scheduler_post_to_twitter($post, $message) {
// Accessing API keys and tokens from environment variables
$consumerKey = $_ENV['TWITTER_CONSUMER_KEY'];
$consumerSecret = $_ENV['TWITTER_CONSUMER_SECRET'];
$accessToken = $_ENV['TWITTER_ACCESS_TOKEN'];
$accessTokenSecret = $_ENV['TWITTER_ACCESS_TOKEN_SECRET'];
$twitter = new Abraham\TwitterOAuth\TwitterOAuth($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret);
$status = $message . ' ' . get_permalink($post);
$result = $twitter->post("statuses/update", ["status" => $status]);
if ($twitter->getLastHttpCode() == 200) {
// Tweet posted successfully
return true;
} else {
// Handle errors here
return false;
}
}
// Function to post to LinkedIn
function sm_scheduler_post_to_linkedin($post, $message) {
$accessToken = $_ENV['LINKEDIN_ACCESS_TOKEN']; // Access token from environment variable
$url = 'https://api.linkedin.com/v2/shares';
$headers = [
'Authorization: Bearer ' . $accessToken,
'Content-Type: application/json',
'X-Restli-Protocol-Version: 2.0.0'
];
$body = json_encode([
'content' => [
'contentEntities' => [
[
'entityLocation' => get_permalink($post),
'thumbnails' => [
['resolvedUrl' => get_the_post_thumbnail_url($post)]
]
]
],
'title' => get_the_title($post)
],
'distribution' => [
'linkedInDistributionTarget' => []
],
'owner' => 'urn:li:person:cbusquets', // Replace with your actual LinkedIn Profile ID
'text' => [
'text' => $message
]
]);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
$response = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if (!$error) {
return json_decode($response, true);
} else {
return $error;
}
}
// WP Cron hook for scheduled posting
add_action('sm_scheduler_daily_event', 'sm_scheduler_post_to_social_media');
function sm_scheduler_post_to_social_media() {
$options = get_option('sm_scheduler_options');
$messages = explode('|', $options['custom_message']);
$random_message = $messages[array_rand($messages)];
// Fetch a random post that hasn't been shared recently
$post = sm_scheduler_fetch_random_post();
if ($post) {
// Attempt to post to Twitter
if (sm_scheduler_post_to_twitter($post, $random_message)) {
update_post_meta($post->ID, 'sm_last_shared_on_twitter', time());
}
// Attempt to post to LinkedIn
if (sm_scheduler_post_to_linkedin($post, $random_message)) {
update_post_meta($post->ID, 'sm_last_shared_on_linkedin', time());
}
}
}
// Function to fetch a random post
function sm_scheduler_fetch_random_post() {
$args = [
'post_type' => 'post',
'posts_per_page' => 1,
'orderby' => 'rand',
'meta_query' => [
[
'key' => 'sm_last_shared_on_twitter',
'value' => time() - WEEK_IN_SECONDS * 2, // Adjust the interval as needed
'compare' => '<',
'type' => 'NUMERIC'
],
[
'key' => 'sm_last_shared_on_linkedin',
'value' => time() - WEEK_IN_SECONDS * 2, // Adjust the interval as needed
'compare' => '<',
'type' => 'NUMERIC'
]
]
];
$query = new WP_Query($args);
if ($query->have_posts()) {
$query->the_post();
return $query->post;
} else {
return null;
}
}