diff --git a/addons/website/static/src/snippets/s_facebook_page/000.js b/addons/website/static/src/snippets/s_facebook_page/000.js index 159a5196ff2..c2a973e2ed4 100644 --- a/addons/website/static/src/snippets/s_facebook_page/000.js +++ b/addons/website/static/src/snippets/s_facebook_page/000.js @@ -16,10 +16,14 @@ const FacebookPageWidget = publicWidget.Widget.extend({ this.options.wysiwyg && this.options.wysiwyg.odooEditor.observerUnactive(); - var params = pick(this.$el[0].dataset, 'href', 'height', 'tabs', 'small_header', 'hide_cover'); + const params = pick(this.$el[0].dataset, 'href', 'id', 'height', 'tabs', 'small_header', 'hide_cover'); if (!params.href) { return def; } + if (params.id) { + params.href = `https://www.facebook.com/${params.id}`; + } + delete params.id; params.width = utils.confine(Math.floor(this.$el.width()), 180, 500); var src = $.param.querystring('https://www.facebook.com/plugins/page.php', params); diff --git a/addons/website/static/src/snippets/s_facebook_page/options.js b/addons/website/static/src/snippets/s_facebook_page/options.js index beae6e31734..a4ec7d8dda4 100644 --- a/addons/website/static/src/snippets/s_facebook_page/options.js +++ b/addons/website/static/src/snippets/s_facebook_page/options.js @@ -14,6 +14,7 @@ options.registry.facebookPage = options.Class.extend({ var defaults = { href: '', + id: '', height: 215, width: 350, tabs: '', @@ -135,18 +136,33 @@ options.registry.facebookPage = options.Class.extend({ */ _checkURL: function () { const defaultURL = 'https://www.facebook.com/Odoo'; - const match = this.fbData.href.match(/^(?:https?:\/\/)?(?:www\.)?(?:fb|facebook)\.com\/(?:([\w.]+)|[^/?#]+-([0-9]{15,16}))(?:$|[/?# ])/); + // Patterns matched by the regex (all relate to existing pages, + // in spite of the URLs containing "profile.php" or "people"): + // - https://www.facebook.com/ + // - http://www.facebook.com/ + // - www.facebook.com/ + // - facebook.com/profile.php?id= + // - www.facebook.com/- - NB: the name doesn't matter + // - www.fb.com/people// - same + // - m.facebook.com/p/- - same + // The regex is kept as a huge one-liner for performance as it is + // compiled once on script load. The only way to split it on several + // lines is with the RegExp constructor, which is compiled on runtime. + const match = this.fbData.href.match(/^(https?:\/\/)?((www\.)?(fb|facebook)|(m\.)?facebook)\.com\/(((profile\.php\?id=|people\/[^/?#]+\/|(p\/)?[^/?#]+-)(?[0-9]{15,16}))|(?[\w.]+))($|[/?# ])/); if (match) { // Check if the page exists on Facebook or not - return new Promise((resolve, reject) => $.ajax({ - url: 'https://graph.facebook.com/' + (match[2] || match[1]) + '/picture', - success: () => resolve(), - error: () => { + const pageId = match.groups.nameid || match.groups.id; + return fetch(`https://graph.facebook.com/${pageId}/picture`) + .then((res) => { + if (res.ok) { + this.fbData.id = pageId; + } else { + this.fbData.id = ""; this.fbData.href = defaultURL; - resolve(); - }, - })); + } + }); } + this.fbData.id = ""; this.fbData.href = defaultURL; return Promise.resolve(); },