Here is my code. I tried to limit it to the share() function and didn't want to modify existing tables, so I created a new table.
I didn't do any UI. If StackIdeas wants to take this, adding the UI in the category view should be trivial and to their style.
If StackIdeas doesn't take this, I'm going to make a plugin that does it outside of core code. But it sure would be great to have this in core code.
LOGIC: If there's an override here in the table, use it. If not, use the global in the autopost configuration. If an override is used, there's an option to include the default as well. Simple as that!
CREATE TABLE `chip_easyblog_facebookcat` (
`id` bigint(20) NOT NULL,
`category` bigint(20) NOT NULL,
`pages` varchar(2048) DEFAULT NULL,
`groups` varchar(2048) DEFAULT NULL,
`include_default_pages` tinyint(4) NOT NULL DEFAULT '0',
`include_default_groups` tinyint(4) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Code:
/**
* Shares the post to facebook, either personal timeline, page(s) and/or group(s)
*
* @param EasyBlogPost $post
* @param EasyBlogTableOAuth $oauth
* @param bool $system
* @param false $reposting
*
* @return bool
*
* @since 5.0
*
* NOTES:
*
* parent::api changed to $this->api as this class derives from the parent without overriding api()
* order of execution changed to note if post goes to a group or page, and if not, then goes to user's timeline
*/
public function share(EasyBlogPost $post, EasyBlogTableOAuth $oauth, $system = true, $reposting = false): bool
{
echo "share called<br />";
$oa = EB::oauth();
if (!$oa) {
return false;
}
// Get the post data
$data = $this->extractPostData($post);
$state = false;
// Construct the params that should be sent to facebook
$params = array(
'link' => $data->url,
'access_token' => $this->token
);
$isNew = $post->isNew();
$do_fb_intro = $this->config->get('integrations_facebook_introtext_message');
$do_fb_group = $this->config->get('integrations_facebook_impersonate_group');
$do_fb_page = $this->config->get('integrations_facebook_impersonate_page');
// If we have content and we want to use it for the Facebook message, set the parameter accordingly
if ($do_fb_intro && $data->content) {
$params['message'] = $data->content;
}
// if detected that this is a post update, we need to send a POST request to the Facebook crawler for
// a re-scrape in order to get the latest post content during autopost.
if (!$isNew || $reposting) {
$scrapeObj = array(
'id' => $data->url,
'access_token' => $this->token,
'scrape' => true
);
$this->api('/', 'POST', $scrapeObj);
}
$hasPostedToGroupOrPage = false;
// See if we want to post this to a particular set of facebook page(s) and/or group(s) based on the
// primary category
$db = \Joomla\CMS\Factory::getDbo();
$groupsOverride = null;
if ($db) {
$inCats = "(" . implode(",", $post->categories) . ")";
$query = 'SELECT ' . $db->quoteName('groups') . ',' . $db->quoteName('include_default_groups');
$query .= ' FROM ' . $db->quoteName('#__easyblog_facebookcat');
$query .= ' WHERE ' . $db->quoteName('category') . ' IN ' . $inCats;
$db->setQuery($query);
$groupsOverride = $db->loadObjectList();
if (!empty($groupsOverride)) {
$do_fb_group = true;
}
}
// Determines if we should auto post to facebook group(s)
if ($do_fb_group && $system) {
if (empty($groupsOverride)) {
$groups = explode(',', $this->config->get('integrations_facebook_group_id'));
} else {
$includeDefault = false;
foreach ($groupsOverride as $p) {
if ($p->include_default_groups) {
$includeDefault = true;
}
}
if ($includeDefault) {
$groups = explode(',', $this->config->get('integrations_facebook_group_id'));
} else {
$groups = array();
}
foreach ($groupsOverride as $g) {
$addGroups = explode(',', $g->groups);
foreach ($addGroups as $a) {
$groups[] = $a;
}
}
}
// Get a list of groups the user can access
$groupAccess = $this->api('/me/groups', 'GET', array('access_token' => $this->token, 'limit' => 500));
// Now we need to find the access for the particular group(s) that they want to share
if (isset($groupAccess['data']) && $groupAccess) {
// We need to ensure that the user really has access to the group
foreach ($groups as $group) {
foreach ($groupAccess['data'] as $access) {
if ($access['id'] == $group) {
$hasPostedToGroupOrPage = true;
$response = $this->api('/' . $group . '/feed', 'post', $params);
$state = isset($response['id']);
$oa->log($oauth, $post, $state, $response);
}
}
}
}
}
// Determines if we should auto post to facebook page(s)
$pagesOverride = null;
if ($db) {
$inCats = "(" . implode(",", $post->categories) . ")";
$query = 'SELECT ' . $db->quoteName('pages') . ',' . $db->quoteName('include_default_pages');
$query .= ' FROM ' . $db->quoteName('#__easyblog_facebookcat');
$query .= ' WHERE ' . $db->quoteName('category') . ' IN ' . $inCats;
$db->setQuery($query);
$pagesOverride = $db->loadObjectList();
if (!empty($pagesOverride)) {
$do_fb_page = true;
}
}
if ($do_fb_page && $system) {
if (empty($pagesOverride)) {
$pages = explode(',', $this->config->get('integrations_facebook_page_id'));
} else {
$includeDefault = false;
foreach ($pagesOverride as $p) {
if ($p->include_default_pages) {
$includeDefault = true;
}
}
if ($includeDefault) {
$pages = explode(',', $this->config->get('integrations_facebook_page_id'));
} else {
$pages = array();
}
foreach ($pagesOverride as $p) {
$addPages = explode(',', $p->pages);
foreach ($addPages as $a) {
$pages[] = $a;
}
}
}
// Get a list of pages the user can access
$pageAccess = $this->api('/me/accounts', array('access_token' => $this->token, 'limit' => 500));
foreach ($pages as $page) {
if (isset($pageAccess['data'])) {
foreach ($pageAccess['data'] as $access) {
if ($access['id'] == $page) {
// We need to set the access now to the page's access
$params['access_token'] = $access['access_token'];
$hasPostedToGroupOrPage = true;
$response = $this->api('/' . $page . '/feed', 'post', $params);
$state = isset($response['id']);
$oa->log($oauth, $post, $state, $response);
}
}
}
}
}
// If this is not a system auto posting or it wasn't posted to a group or page,
// post it to the user's own timeline
if (!$hasPostedToGroupOrPage || !$system) {
$response = $this->api('/me/feed', 'post', $params);
$state = isset($response['id']);
$oa->log($oauth, $post, $state, $response);
}
return $state;
}