Skip to content

Commit

Permalink
Merge branch 'craft-4' of https://github.com/verbb/social-feeds into …
Browse files Browse the repository at this point in the history
…craft-5

# Conflicts:
#	CHANGELOG.md
#	composer.json
#	src/sources/Facebook.php
  • Loading branch information
engram-design committed Sep 14, 2024
2 parents e0a6432 + 8fb6232 commit 28b4298
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 111 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
- Update `symfony/property-access` dependency.
- Fix an error when uninstalling.

## 1.0.9 - 2024-09-14

### Removed
- Remove the ability to create feeds to a Facebook Group (no longer possible via Facebook’s API).

## 1.0.8 - 2024-05-29

### Changed
Expand Down
15 changes: 5 additions & 10 deletions docs/providers/facebook.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Your Facebook App **does not** require review and approval by Facebook to use So

## Connecting to Facebook

### Step 1. Admin Access to Facebook Page or Facebook Group
In order to fetch posts from a Facebook Page or Facebook Group, you must be an Admin for the page/group you want to access.
### Step 1. Admin Access to Facebook Page
In order to fetch posts from a Facebook Page, you must be an Admin for the page you want to access.

### Step 2: Register a Facebook App
1. Go to the <a href="https://developers.facebook.com/apps/" target="_blank">Meta for Developers</a> page.
Expand All @@ -34,10 +34,10 @@ In order to fetch posts from a Facebook Page or Facebook Group, you must be an A

### Step 4: Connect to Facebook
1. In the Social Feeds source settings, click the **Connect** button and login to Facebook.
1. Ensure you pick either the Facebook Group or Facebook Page you have admin access to.
1. Ensure you pick the Facebook Page you have admin access to.

### Step 5: Select your Facebook Page or Facebook Group
1. Select either a **Facebook Page** or a **Facebook Group** that you'd like connected to.
### Step 5: Select your Facebook Page
1. Select a **Facebook Page** that you'd like connected to.
1. Click the **Save** button for the source.

### Business Pages
Expand Down Expand Up @@ -68,8 +68,3 @@ Facebook provides the following types of content as posts.
- Photos (Photos from your Facebook Photos page)
- Videos (Videos from your Facebook Videos page)
- Events (Events from your Facebook Events page)
- Group Feed (Posts from your Facebook group)
- Photos (Photos from your Facebook Photos group)
- Videos (Videos from your Facebook Videos group)
- Events (Events from your Facebook Events group)

93 changes: 31 additions & 62 deletions src/sources/Facebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,19 @@ public static function getOAuthProviderClass(): string
public bool $enablePhotos = false;
public bool $enableVideos = false;
public bool $enableEvents = false;
public ?string $endpoint = null;
public ?string $groupId = null;
public ?string $pageId = null;


// Public Methods
// =========================================================================

public function __construct(array $config = [])
{
unset($config['endpoint'], $config['groupId']);

parent::__construct($config);
}

public function getOAuthProviderConfig(): array
{
$config = parent::getOAuthProviderConfig();
Expand All @@ -62,37 +67,19 @@ public function getDefaultScopes(): array
public function fetchSourceSettings(string $settingsKey): ?array
{
try {
if ($settingsKey === 'pageId') {
$pages = [];

$response = $this->request('GET', 'me/accounts');
$sources = $response['data'] ?? [];
$pages = [];

foreach ($sources as $source) {
$pages[] = [
'label' => $source['name'] ?? null,
'value' => $source['id'] ?? null,
];
}
$response = $this->request('GET', 'me/accounts');
$sources = $response['data'] ?? [];

return $pages;
foreach ($sources as $source) {
$pages[] = [
'label' => $source['name'] ?? null,
'value' => $source['id'] ?? null,
];
}

if ($settingsKey === 'groupId') {
$pages = [];

$response = $this->request('GET', 'me/groups');
$sources = $response['data'] ?? [];

foreach ($sources as $source) {
$pages[] = [
'label' => $source['name'] ?? null,
'value' => $source['id'] ?? null,
];
}

return $pages;
}
return $pages;
} catch (Throwable $e) {
self::apiError($this, $e);
}
Expand All @@ -107,35 +94,29 @@ public function fetchPosts(): ?array
$posts = [];

try {
if ($this->endpoint == 'page') {
// This will fail if not a page (Business or Group) so catch and continue
try {
$response = $this->request('GET', $this->pageId, [
'query' => ['fields' => 'access_token'],
]);
// This will fail if not a page (Business or Group) so catch and continue
try {
$response = $this->request('GET', $this->pageId, [
'query' => ['fields' => 'access_token'],
]);

$pageAccessToken = $response['access_token'] ?? null;
$pageAccessToken = $response['access_token'] ?? null;

// Update the token in Auth to use this from now on.
if ($pageAccessToken && $token = $this->getToken()) {
$token->accessToken = $pageAccessToken;
// Update the token in Auth to use this from now on.
if ($pageAccessToken && $token = $this->getToken()) {
$token->accessToken = $pageAccessToken;

Auth::getInstance()->getTokens()->saveToken($token);
}
} catch (Throwable $e) {
self::apiError($this, $e, false);
Auth::getInstance()->getTokens()->saveToken($token);
}
} catch (Throwable $e) {
self::apiError($this, $e, false);
}

$postType = null;
$endpoint = [];
$fields = [];

if ($this->endpoint === 'page') {
$endpoint[] = $this->pageId;
} else if ($this->endpoint === 'group') {
$endpoint[] = $this->groupId;
}
$endpoint[] = $this->pageId;

if ($this->enableProfile) {
$postType = 'post';
Expand Down Expand Up @@ -488,21 +469,9 @@ protected function defineRules(): array
{
$rules = parent::defineRules();

$rules[] = [
['endpoint'], 'required', 'when' => function($model) {
return $model->enabled;
},
];

$rules[] = [
['groupId'], 'required', 'when' => function($model) {
return $model->enabled && $model->endpoint === 'group' && $model->isConnected();
},
];

$rules[] = [
$rules[] = [
['pageId'], 'required', 'when' => function($model) {
return $model->enabled && $model->endpoint === 'page' && $model->isConnected();
return $model->enabled && $model->isConnected();
},
];

Expand Down
37 changes: 4 additions & 33 deletions src/templates/sources/_types/facebook/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,13 @@

<hr>

{% set endpoint = source.endpoint ?? 'page' %}

{{ forms.selectField({
label: 'Page or Group' | t('social-feeds', { name: source.providerName }),
instructions: 'Select whether to source content from a {name} Page or {name} Group.' | t('social-feeds', { name: source.providerName }),
name: 'endpoint',
options: [
{ label: 'Group' | t('social-feeds'), value: 'group' },
{ label: 'Page' | t('social-feeds'), value: 'page' },
],
{{ customForms.providerSettingsField(source, {
label: '{name} Page' | t('social-feeds', { name: source.providerName }),
instructions: 'The {name} Page to pull content from.' | t('social-feeds', { name: source.providerName }),
name: 'pageId',
required: true,
value: endpoint,
warning: macros.configWarning("sources.#{source.handle}.endpoint", 'social-feeds'),
errors: source.getErrors('endpoint'),
toggle: true,
targetPrefix: '.endpoint-',
}) }}

<div class="endpoint-group {% if endpoint != 'group' %}hidden{% endif %}">
{{ customForms.providerSettingsField(source, {
label: '{name} Group' | t('social-feeds', { name: source.providerName }),
instructions: 'The {name} Group to pull content from.' | t('social-feeds', { name: source.providerName }),
name: 'groupId',
required: true,
}) }}
</div>

<div class="endpoint-page {% if endpoint != 'page' %}hidden{% endif %}">
{{ customForms.providerSettingsField(source, {
label: '{name} Page' | t('social-feeds', { name: source.providerName }),
instructions: 'The {name} Page to pull content from.' | t('social-feeds', { name: source.providerName }),
name: 'pageId',
required: true,
}) }}
</div>

{% set inputs %}
{% set inputKey = random() %}

Expand Down
6 changes: 0 additions & 6 deletions src/translations/en/social-feeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
'Feed Preview' => 'Feed Preview',
'Feeds' => 'Feeds',
'Feed saved.' => 'Feed saved.',
'Group' => 'Group',
'Hashtag' => 'Hashtag',
'Hashtags' => 'Hashtags',
'How to query results by.' => 'How to query results by.',
Expand All @@ -58,8 +57,6 @@
'No source exists with the ID “{id}”.' => 'No source exists with the ID “{id}”.',
'Not Connected' => 'Not Connected',
'Order By' => 'Order By',
'Page' => 'Page',
'Page or Group' => 'Page or Group',
'Photos' => 'Photos',
'Photos from your Facebook Photos page.' => 'Photos from your Facebook Photos page.',
'Playlist' => 'Playlist',
Expand All @@ -84,7 +81,6 @@
'Search Terms' => 'Search Terms',
'See what your posts look like for your feed.' => 'See what your posts look like for your feed.',
'Select what types of content this source should provide.' => 'Select what types of content this source should provide.',
'Select whether to source content from a {name} Page or {name} Group.' => 'Select whether to source content from a {name} Page or {name} Group.',
'Select which [Sources]({url}) are available for this feed.' => 'Select which [Sources]({url}) are available for this feed.',
'Set the cache duration. Accepts a [Date Interval](https://www.php.net/manual/en/dateinterval.construct.php) or a number of seconds.' => 'Set the cache duration. Accepts a [Date Interval](https://www.php.net/manual/en/dateinterval.construct.php) or a number of seconds.',
'Settings' => 'Settings',
Expand All @@ -99,7 +95,6 @@
'Tagged Posts' => 'Tagged Posts',
'The {name} Business Account to pull content from.' => 'The {name} Business Account to pull content from.',
'The {name} Channel to pull content from.' => 'The {name} Channel to pull content from.',
'The {name} Group to pull content from.' => 'The {name} Group to pull content from.',
'The {name} Page to pull content from.' => 'The {name} Page to pull content from.',
'The {name} Playlist to pull content from.' => 'The {name} Playlist to pull content from.',
'The {name} User to pull content from.' => 'The {name} User to pull content from.',
Expand Down Expand Up @@ -131,7 +126,6 @@
'Whether to proxy the redirect URI through Verbb‘s servers. This should **only** be used for local testing. See [docs](https://verbb.io/craft-plugins/social-feeds/docs/feature-tour/providers#local-testing-proxy) for more.' => 'Whether to proxy the redirect URI through Verbb‘s servers. This should **only** be used for local testing. See [docs](https://verbb.io/craft-plugins/social-feeds/docs/feature-tour/providers#local-testing-proxy) for more.',
'{name} Business Account' => '{name} Business Account',
'{name} Channel' => '{name} Channel',
'{name} Group' => '{name} Group',
'{name} Page' => '{name} Page',
'{name} Playlist' => '{name} Playlist',
'{name} User' => '{name} User',
Expand Down

0 comments on commit 28b4298

Please sign in to comment.