-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSmpEventJob.php
More file actions
89 lines (75 loc) · 3.25 KB
/
Copy pathSmpEventJob.php
File metadata and controls
89 lines (75 loc) · 3.25 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
<?php
/**
* Event handler for new jobs (qa_job_post).
* Posts to enabled social media platforms.
*/
class SmpEventJob
{
private string $directory;
function load_module($directory, $urltoroot)
{
$this->directory = $directory;
}
function process_event($event, $userid, $handle, $cookieid, $params)
{
if ($event !== 'qa_job_post') {
return;
}
require_once $this->directory . 'SmpConstants.php';
require_once $this->directory . 'SmpPoster.php';
$poster = new SmpPoster($this->directory);
// Check if any accounts are configured for jobs
$accounts = $poster->getAccountsForPosting(SmpConstants::CONTENT_JOB);
// Get user's personal accounts (if configured)
$userAccounts = [];
if ($userid) {
$userAccounts = $poster->getUserAccountsForPosting((int)$userid);
}
if (empty($accounts) && empty($userAccounts)) {
return;
}
$title = $params['title'] ?? '';
$postId = $params['postid'] ?? $params['jobid'] ?? 0;
// Build URL using job path helper if available
if (function_exists('qa_job_request')) {
$url = qa_path_absolute(qa_job_request($postId, $title));
} else {
$url = qa_opt('site_url') . 'job/' . $postId . '/' . qa_slug($title);
}
// Generate message using OpenAI
$generatedMessage = $poster->openaiGenerateMessage($title, 'job posting');
$message = $generatedMessage . "\n\nApply here: " . $url;
// Generate image if Instagram auto-image or YouTube auto-video is enabled
$imageUrl = null;
$allAccounts = $accounts + $userAccounts;
$accountPlatforms = array_column($allAccounts, '_platform');
$needsImage = in_array(SmpConstants::PLATFORM_INSTAGRAM, $accountPlatforms)
|| (in_array(SmpConstants::PLATFORM_YOUTUBE, $accountPlatforms)
&& qa_opt(SmpConstants::OPT_YOUTUBE_AUTO_VIDEO));
if ($needsImage) {
require_once $this->directory . 'SmpImageGenerator.php';
$imageGen = new SmpImageGenerator();
$imageUrl = $imageGen->generateJobImage($title, $postId);
}
// Post to all enabled accounts
$results = $poster->postToAll(SmpConstants::CONTENT_JOB, $message, $imageUrl, ['title' => $title, '_manual_accounts' => $userAccounts]);
// Report failures
foreach ($results as $accountId => $result) {
if (empty($result['success'])) {
$accountName = $result['account_name'] ?? $accountId;
$platform = $result['platform'] ?? 'unknown';
$usedImageUrl = $result['image_url'] ?? $imageUrl ?? 'none';
$videoUrl = $result['video_url'] ?? 'none';
$poster->reportFailure(
'Job post failed on ' . $platform . ' (' . $accountName . ')',
'Job ID: ' . $postId
. "\nTitle: " . $title
. "\nImage URL: " . $usedImageUrl
. "\nVideo URL: " . $videoUrl
. "\nError: " . ($result['error'] ?? 'Unknown')
. "\n\n--- Message ---\n" . $message
);
}
}
}
}