Index: config/schema/url_embed.schema.yml IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/config/schema/url_embed.schema.yml b/config/schema/url_embed.schema.yml new file mode 100644 --- /dev/null (revision 515020552861edaf2f104b03eb7db26f9276ef21) +++ b/config/schema/url_embed.schema.yml (revision 515020552861edaf2f104b03eb7db26f9276ef21) @@ -0,0 +1,10 @@ +url_embed.settings: + type: config_object + label: 'Url Embed config.' + mapping: + facebook_app_id: + type: string + label: 'Facebook App ID' + facebook_app_secret: + type: string + label: 'Facebook App Secret' Index: src/Form/UrlEmbedAdminSettingsForm.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/src/Form/UrlEmbedAdminSettingsForm.php b/src/Form/UrlEmbedAdminSettingsForm.php new file mode 100644 --- /dev/null (revision 515020552861edaf2f104b03eb7db26f9276ef21) +++ b/src/Form/UrlEmbedAdminSettingsForm.php (revision 515020552861edaf2f104b03eb7db26f9276ef21) @@ -0,0 +1,88 @@ +config('url_embed.settings'); + + $form['facebook_app'] = [ + '#type' => 'details', + '#open' => TRUE, + '#title' => $this->t('Facebook / Instagram App'), + '#description' => $this->t('As of Oct 24 2020, Facebook and Instagram require you to create a live facebook app with an oEmbed product enabled to embed posts.', [ + '@doc_url' => 'https://developers.facebook.com/docs/plugins/oembed', + ]), + ]; + + // If we have Facebook credentials in the config, check that it is a valid app and show a message to the user. + if (!empty($config->get('facebook_app_id')) && !empty($config->get('facebook_app_id'))) { + $debug = url_embed_debug_facebook_access_token($config->get('facebook_app_id') . '|' . $config->get('facebook_app_secret')); + if (!empty($debug['is_valid'])) { + $form['facebook_app']['facebook_app_status']['#markup'] = '
'; + } + else { + $form['facebook_app']['facebook_app_status']['#markup'] = ''; + } + } + + $form['facebook_app']['facebook_app_id'] = [ + '#type' => 'textfield', + '#title' => $this->t('App ID'), + '#default_value' => $config->get('facebook_app_id'), + ]; + + $form['facebook_app']['facebook_app_secret'] = [ + '#type' => 'textfield', + '#title' => $this->t('App Secret'), + '#default_value' => $config->get('facebook_app_secret'), + ]; + + return parent::buildForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state) { + parent::validateForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + $config = $this->config('url_embed.settings'); + $config + ->set('facebook_app_id', $form_state->getValue('facebook_app_id')) + ->set('facebook_app_secret', $form_state->getValue('facebook_app_secret')) + ->save(); + + parent::submitForm($form, $form_state); + } + +} Index: src/UrlEmbed.php IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/src/UrlEmbed.php b/src/UrlEmbed.php --- a/src/UrlEmbed.php (revision c3469227d337723507b5d1409e2a483efb7daadf) +++ b/src/UrlEmbed.php (revision 515020552861edaf2f104b03eb7db26f9276ef21) @@ -46,13 +46,24 @@ public $config; /** - * @{inheritdoc} + * Constructs a UrlEmbed object. + * + * @param array $config + * (optional) The options passed to the adapter. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * (optional) The config factory. */ public function __construct(CacheBackendInterface $cache_backend, TimeInterface $time, ConfigFactoryInterface $config_factory, array $config = []) { - $this->config = $config; $this->cacheBackend = $cache_backend; $this->time = $time; $this->configFactory = $config_factory; + $global_config = $config_factory->get('url_embed.settings'); + $defaults = []; + + if ($global_config->get('facebook_app_id') && $global_config->get('facebook_app_secret')) { + $defaults['facebook']['key'] = $global_config->get('facebook_app_id') . '|' . $global_config->get('facebook_app_secret'); + } + $this->config = array_replace_recursive($defaults, $config); } /** @@ -73,7 +84,7 @@ * @{inheritdoc} */ public function getEmbed($request, array $config = []) { - return Embed::create($request, $config ?: $this->config); + return Embed::create($request, array_replace_recursive($this->config, $config)); } /** Index: url_embed.install IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/url_embed.install b/url_embed.install --- a/url_embed.install (revision c3469227d337723507b5d1409e2a483efb7daadf) +++ b/url_embed.install (revision 515020552861edaf2f104b03eb7db26f9276ef21) @@ -24,6 +24,28 @@ ]; } } + elseif ($phase == 'runtime') { + $requirements['url_embed']['title'] = t('Url Embed: Facebook/Instagram App'); + $config = \Drupal::service('config.factory')->get('url_embed.settings'); + if (empty($config->get('facebook_app_id')) || empty($config->get('facebook_app_secret'))) { + $requirements['url_embed']['value'] = t('Missing Facebook app credentials.'); + $requirements['url_embed']['description'] = t('In order to embed Facebook and Instagram posts you must create a live facebook app with oEmbed enabled and add the app ID and Secret to the Url Embed settings page.', + [ + '@facebook_url' => 'https://developers.facebook.com/docs/plugins/oembed', + '@settings_url' => '/admin/config/media/url_embed', + ]); + $requirements['url_embed']['severity'] = REQUIREMENT_WARNING; + } + else { + $access_token = url_embed_debug_facebook_access_token($config->get('facebook_app_id') . '|' . $config->get('facebook_app_secret')); + $is_valid = !empty($access_token['is_valid']); + $requirements['url_embed']['value'] = $is_valid ? t('Active') : t('Invalid Facebook app credentials provided.'); + $requirements['url_embed']['severity'] = $is_valid ? REQUIREMENT_INFO : REQUIREMENT_ERROR; + if (!$is_valid) { + $requirements['url_embed']['description'] = t('Check that you have entered your app credentials correctly and that it is live. Edit settings', ['@settings_url' => '/admin/config/media/url_embed']); + } + } + } return $requirements; } Index: url_embed.module IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/url_embed.module b/url_embed.module --- a/url_embed.module (revision c3469227d337723507b5d1409e2a483efb7daadf) +++ b/url_embed.module (revision 515020552861edaf2f104b03eb7db26f9276ef21) @@ -1,1 +1,41 @@ get($url); + $code = $response->getStatusCode(); + + if ($code == 200) { + $data = $response->getBody()->getContents(); + $decoded = Json::decode($data); + return isset($decoded['data']) ? $decoded['data'] : FALSE; + } + else { + return FALSE; + } + } + catch (Exception $e) { + } + + return FALSE; +} Index: url_embed.routing.yml IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/url_embed.routing.yml b/url_embed.routing.yml --- a/url_embed.routing.yml (revision c3469227d337723507b5d1409e2a483efb7daadf) +++ b/url_embed.routing.yml (revision 515020552861edaf2f104b03eb7db26f9276ef21) @@ -5,3 +5,10 @@ _title: 'Embed URL' requirements: _embed_button_editor_access: 'TRUE' +url_embed.admin: + path: '/admin/config/media/url_embed' + defaults: + _form: '\Drupal\url_embed\Form\UrlEmbedAdminSettingsForm' + _title: 'Url Embed' + requirements: + _permission: 'administer url_embed' Index: url_embed.services.yml IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/url_embed.services.yml b/url_embed.services.yml --- a/url_embed.services.yml (revision c3469227d337723507b5d1409e2a483efb7daadf) +++ b/url_embed.services.yml (revision 515020552861edaf2f104b03eb7db26f9276ef21) @@ -1,4 +1,4 @@ services: url_embed: class: Drupal\url_embed\UrlEmbed - arguments: ['@cache.data', '@datetime.time', '@config.factory'] + arguments: ['@cache.data', '@datetime.time', '@?config.factory', ['config']]