From 0f475a780a489aa3192fe468209d6d35c4e96a65 Mon Sep 17 00:00:00 2001 From: apeatling Date: Thu, 19 Mar 2020 11:28:10 -0700 Subject: [PATCH 01/32] Initial addition of send a message block into Jetpack block scaffolding --- .../blocks/send-a-message/attributes.js | 31 +++ .../blocks/send-a-message/countrycodes.js | 241 +++++++++++++++++ extensions/blocks/send-a-message/edit.js | 250 ++++++++++++++++++ extensions/blocks/send-a-message/editor.js | 7 + extensions/blocks/send-a-message/editor.scss | 44 +++ extensions/blocks/send-a-message/icon.js | 10 + extensions/blocks/send-a-message/index.js | 62 +++++ extensions/blocks/send-a-message/save.js | 44 +++ .../blocks/send-a-message/send-a-message.php | 43 +++ extensions/blocks/send-a-message/view.js | 4 + extensions/blocks/send-a-message/view.scss | 37 +++ extensions/index.json | 2 +- 12 files changed, 774 insertions(+), 1 deletion(-) create mode 100644 extensions/blocks/send-a-message/attributes.js create mode 100644 extensions/blocks/send-a-message/countrycodes.js create mode 100644 extensions/blocks/send-a-message/edit.js create mode 100644 extensions/blocks/send-a-message/editor.js create mode 100644 extensions/blocks/send-a-message/editor.scss create mode 100644 extensions/blocks/send-a-message/icon.js create mode 100644 extensions/blocks/send-a-message/index.js create mode 100644 extensions/blocks/send-a-message/save.js create mode 100644 extensions/blocks/send-a-message/send-a-message.php create mode 100644 extensions/blocks/send-a-message/view.js create mode 100644 extensions/blocks/send-a-message/view.scss diff --git a/extensions/blocks/send-a-message/attributes.js b/extensions/blocks/send-a-message/attributes.js new file mode 100644 index 0000000000000..7c2a4657ff7eb --- /dev/null +++ b/extensions/blocks/send-a-message/attributes.js @@ -0,0 +1,31 @@ +/** + * External dependencies + */ +import { __ } from '@wordpress/i18n'; + +export default { + countryCode: { + type: 'string', + }, + phoneNumber: { + type: 'string', + }, + firstMessage: { + type: 'string', + default: __( 'Hi, I got your WhatsApp information from your website.', 'jetpack' ), + }, + buttonText: { + type: 'array', + source: 'children', + selector: 'a.whatsapp-block__button', + default: __( 'Chat on WhatsApp', 'jetpack' ), + }, + backgroundColor: { + type: 'string', + default: '#25D366', + }, + colorClass: { + type: 'string', + default: 'dark', + }, +}; diff --git a/extensions/blocks/send-a-message/countrycodes.js b/extensions/blocks/send-a-message/countrycodes.js new file mode 100644 index 0000000000000..9f268dd057d5f --- /dev/null +++ b/extensions/blocks/send-a-message/countrycodes.js @@ -0,0 +1,241 @@ +export const countryCodes = [ + { code: 'US', label: '\ud83c\uddfa\ud83c\uddf8 +1', value: '1US' }, + { code: 'CA', label: '\ud83c\udde8\ud83c\udde6 +1', value: '1CA' }, + { code: 'BS', label: '\ud83c\udde7\ud83c\uddf8 +1242', value: '1242BS' }, + { code: 'BB', label: '\ud83c\udde7\ud83c\udde7 +1246', value: '1246BB' }, + { code: 'AI', label: '\ud83c\udde6\ud83c\uddee +1264', value: '1264AI' }, + { code: 'AG', label: '\ud83c\udde6\ud83c\uddec +1268', value: '1268AG' }, + { code: 'VG', label: '\ud83c\uddfb\ud83c\uddec +1284', value: '1284VG' }, + { code: 'VI', label: '\ud83c\uddfb\ud83c\uddee +1340', value: '1340VI' }, + { code: 'BM', label: '\ud83c\udde7\ud83c\uddf2 +1441', value: '1441BM' }, + { code: 'GD', label: '\ud83c\uddec\ud83c\udde9 +1473', value: '1473GD' }, + { code: 'TC', label: '\ud83c\uddf9\ud83c\udde8 +1649', value: '1649TC' }, + { code: 'MS', label: '\ud83c\uddf2\ud83c\uddf8 +1664', value: '1664MS' }, + { code: 'MP', label: '\ud83c\uddf2\ud83c\uddf5 +1670', value: '1670MP' }, + { code: 'GU', label: '\ud83c\uddec\ud83c\uddfa +1671', value: '1671GU' }, + { code: 'AS', label: '\ud83c\udde6\ud83c\uddf8 +1684', value: '1684AS' }, + { code: 'LC', label: '\ud83c\uddf1\ud83c\udde8 +1758', value: '1758LC' }, + { code: 'DM', label: '\ud83c\udde9\ud83c\uddf2 +1767', value: '1767DM' }, + { code: 'VC', label: '\ud83c\uddfb\ud83c\udde8 +1784', value: '1784VC' }, + { code: 'DO', label: '\ud83c\udde9\ud83c\uddf4 +1849', value: '1849DO' }, + { code: 'TT', label: '\ud83c\uddf9\ud83c\uddf9 +1868', value: '1868TT' }, + { code: 'KN', label: '\ud83c\uddf0\ud83c\uddf3 +1869', value: '1869KN' }, + { code: 'JM', label: '\ud83c\uddef\ud83c\uddf2 +1876', value: '1876JM' }, + { code: 'PR', label: '\ud83c\uddf5\ud83c\uddf7 +1939', value: '1939PR' }, + { code: 'EG', label: '\ud83c\uddea\ud83c\uddec +20', value: '20EG' }, + { code: 'MA', label: '\ud83c\uddf2\ud83c\udde6 +212', value: '212MA' }, + { code: 'DZ', label: '\ud83c\udde9\ud83c\uddff +213', value: '213DZ' }, + { code: 'TN', label: '\ud83c\uddf9\ud83c\uddf3 +216', value: '216TN' }, + { code: 'LY', label: '\ud83c\uddf1\ud83c\uddfe +218', value: '218LY' }, + { code: 'GM', label: '\ud83c\uddec\ud83c\uddf2 +220', value: '220GM' }, + { code: 'SN', label: '\ud83c\uddf8\ud83c\uddf3 +221', value: '221SN' }, + { code: 'MR', label: '\ud83c\uddf2\ud83c\uddf7 +222', value: '222MR' }, + { code: 'ML', label: '\ud83c\uddf2\ud83c\uddf1 +223', value: '223ML' }, + { code: 'GN', label: '\ud83c\uddec\ud83c\uddf3 +224', value: '224GN' }, + { code: 'CI', label: '\ud83c\udde8\ud83c\uddee +225', value: '225CI' }, + { code: 'BF', label: '\ud83c\udde7\ud83c\uddeb +226', value: '226BF' }, + { code: 'NE', label: '\ud83c\uddf3\ud83c\uddea +227', value: '227NE' }, + { code: 'TG', label: '\ud83c\uddf9\ud83c\uddec +228', value: '228TG' }, + { code: 'BJ', label: '\ud83c\udde7\ud83c\uddef +229', value: '229BJ' }, + { code: 'MU', label: '\ud83c\uddf2\ud83c\uddfa +230', value: '230MU' }, + { code: 'LR', label: '\ud83c\uddf1\ud83c\uddf7 +231', value: '231LR' }, + { code: 'SL', label: '\ud83c\uddf8\ud83c\uddf1 +232', value: '232SL' }, + { code: 'GH', label: '\ud83c\uddec\ud83c\udded +233', value: '233GH' }, + { code: 'NG', label: '\ud83c\uddf3\ud83c\uddec +234', value: '234NG' }, + { code: 'TD', label: '\ud83c\uddf9\ud83c\udde9 +235', value: '235TD' }, + { code: 'CF', label: '\ud83c\udde8\ud83c\uddeb +236', value: '236CF' }, + { code: 'CM', label: '\ud83c\udde8\ud83c\uddf2 +237', value: '237CM' }, + { code: 'CV', label: '\ud83c\udde8\ud83c\uddfb +238', value: '238CV' }, + { code: 'ST', label: '\ud83c\uddf8\ud83c\uddf9 +239', value: '239ST' }, + { code: 'GQ', label: '\ud83c\uddec\ud83c\uddf6 +240', value: '240GQ' }, + { code: 'GA', label: '\ud83c\uddec\ud83c\udde6 +241', value: '241GA' }, + { code: 'CG', label: '\ud83c\udde8\ud83c\uddec +242', value: '242CG' }, + { code: 'CD', label: '\ud83c\udde8\ud83c\udde9 +243', value: '243CD' }, + { code: 'AO', label: '\ud83c\udde6\ud83c\uddf4 +244', value: '244AO' }, + { code: 'GW', label: '\ud83c\uddec\ud83c\uddfc +245', value: '245GW' }, + { code: 'IO', label: '\ud83c\uddee\ud83c\uddf4 +246', value: '246IO' }, + { code: 'SC', label: '\ud83c\uddf8\ud83c\udde8 +248', value: '248SC' }, + { code: 'SD', label: '\ud83c\uddf8\ud83c\udde9 +249', value: '249SD' }, + { code: 'RW', label: '\ud83c\uddf7\ud83c\uddfc +250', value: '250RW' }, + { code: 'ET', label: '\ud83c\uddea\ud83c\uddf9 +251', value: '251ET' }, + { code: 'SO', label: '\ud83c\uddf8\ud83c\uddf4 +252', value: '252SO' }, + { code: 'DJ', label: '\ud83c\udde9\ud83c\uddef +253', value: '253DJ' }, + { code: 'KE', label: '\ud83c\uddf0\ud83c\uddea +254', value: '254KE' }, + { code: 'TZ', label: '\ud83c\uddf9\ud83c\uddff +255', value: '255TZ' }, + { code: 'UG', label: '\ud83c\uddfa\ud83c\uddec +256', value: '256UG' }, + { code: 'BI', label: '\ud83c\udde7\ud83c\uddee +257', value: '257BI' }, + { code: 'MZ', label: '\ud83c\uddf2\ud83c\uddff +258', value: '258MZ' }, + { code: 'ZM', label: '\ud83c\uddff\ud83c\uddf2 +260', value: '260ZM' }, + { code: 'MG', label: '\ud83c\uddf2\ud83c\uddec +261', value: '261MG' }, + { code: 'YT', label: '\ud83c\uddfe\ud83c\uddf9 +262', value: '262YT' }, + { code: 'RE', label: '\ud83c\uddf7\ud83c\uddea +262', value: '262RE' }, + { code: 'ZW', label: '\ud83c\uddff\ud83c\uddfc +263', value: '263ZW' }, + { code: 'NA', label: '\ud83c\uddf3\ud83c\udde6 +264', value: '264NA' }, + { code: 'MW', label: '\ud83c\uddf2\ud83c\uddfc +265', value: '265MW' }, + { code: 'LS', label: '\ud83c\uddf1\ud83c\uddf8 +266', value: '266LS' }, + { code: 'BW', label: '\ud83c\udde7\ud83c\uddfc +267', value: '267BW' }, + { code: 'SZ', label: '\ud83c\uddf8\ud83c\uddff +268', value: '268SZ' }, + { code: 'KM', label: '\ud83c\uddf0\ud83c\uddf2 +269', value: '269KM' }, + { code: 'ZA', label: '\ud83c\uddff\ud83c\udde6 +27', value: '27ZA' }, + { code: 'SH', label: '\ud83c\uddf8\ud83c\udded +290', value: '290SH' }, + { code: 'ER', label: '\ud83c\uddea\ud83c\uddf7 +291', value: '291ER' }, + { code: 'AW', label: '\ud83c\udde6\ud83c\uddfc +297', value: '297AW' }, + { code: 'FO', label: '\ud83c\uddeb\ud83c\uddf4 +298', value: '298FO' }, + { code: 'GL', label: '\ud83c\uddec\ud83c\uddf1 +299', value: '299GL' }, + { code: 'GR', label: '\ud83c\uddec\ud83c\uddf7 +30', value: '30GR' }, + { code: 'NL', label: '\ud83c\uddf3\ud83c\uddf1 +31', value: '31NL' }, + { code: 'BE', label: '\ud83c\udde7\ud83c\uddea +32', value: '32BE' }, + { code: 'FR', label: '\ud83c\uddeb\ud83c\uddf7 +33', value: '33FR' }, + { code: 'ES', label: '\ud83c\uddea\ud83c\uddf8 +34', value: '34ES' }, + { code: 'KY', label: '\ud83c\uddf0\ud83c\uddfe +345', value: '345KY' }, + { code: 'GI', label: '\ud83c\uddec\ud83c\uddee +350', value: '350GI' }, + { code: 'PT', label: '\ud83c\uddf5\ud83c\uddf9 +351', value: '351PT' }, + { code: 'LU', label: '\ud83c\uddf1\ud83c\uddfa +352', value: '352LU' }, + { code: 'IE', label: '\ud83c\uddee\ud83c\uddea +353', value: '353IE' }, + { code: 'IS', label: '\ud83c\uddee\ud83c\uddf8 +354', value: '354IS' }, + { code: 'AL', label: '\ud83c\udde6\ud83c\uddf1 +355', value: '355AL' }, + { code: 'MT', label: '\ud83c\uddf2\ud83c\uddf9 +356', value: '356MT' }, + { code: 'FI', label: '\ud83c\uddeb\ud83c\uddee +358', value: '358FI' }, + { code: 'BG', label: '\ud83c\udde7\ud83c\uddec +359', value: '359BG' }, + { code: 'HU', label: '\ud83c\udded\ud83c\uddfa +36', value: '36HU' }, + { code: 'LT', label: '\ud83c\uddf1\ud83c\uddf9 +370', value: '370LT' }, + { code: 'LV', label: '\ud83c\uddf1\ud83c\uddfb +371', value: '371LV' }, + { code: 'EE', label: '\ud83c\uddea\ud83c\uddea +372', value: '372EE' }, + { code: 'MD', label: '\ud83c\uddf2\ud83c\udde9 +373', value: '373MD' }, + { code: 'AM', label: '\ud83c\udde6\ud83c\uddf2 +374', value: '374AM' }, + { code: 'BY', label: '\ud83c\udde7\ud83c\uddfe +375', value: '375BY' }, + { code: 'AD', label: '\ud83c\udde6\ud83c\udde9 +376', value: '376AD' }, + { code: 'MC', label: '\ud83c\uddf2\ud83c\udde8 +377', value: '377MC' }, + { code: 'SM', label: '\ud83c\uddf8\ud83c\uddf2 +378', value: '378SM' }, + { code: 'VA', label: '\ud83c\uddfb\ud83c\udde6 +379', value: '379VA' }, + { code: 'UA', label: '\ud83c\uddfa\ud83c\udde6 +380', value: '380UA' }, + { code: 'RS', label: '\ud83c\uddf7\ud83c\uddf8 +381', value: '381RS' }, + { code: 'ME', label: '\ud83c\uddf2\ud83c\uddea +382', value: '382ME' }, + { code: 'XK', label: '\ud83c\uddfd\ud83c\uddf0 +383', value: '383XK' }, + { code: 'HR', label: '\ud83c\udded\ud83c\uddf7 +385', value: '385HR' }, + { code: 'SI', label: '\ud83c\uddf8\ud83c\uddee +386', value: '386SI' }, + { code: 'BA', label: '\ud83c\udde7\ud83c\udde6 +387', value: '387BA' }, + { code: 'MK', label: '\ud83c\uddf2\ud83c\uddf0 +389', value: '389MK' }, + { code: 'IT', label: '\ud83c\uddee\ud83c\uddf9 +39', value: '39IT' }, + { code: 'RO', label: '\ud83c\uddf7\ud83c\uddf4 +40', value: '40RO' }, + { code: 'CH', label: '\ud83c\udde8\ud83c\udded +41', value: '41CH' }, + { code: 'CZ', label: '\ud83c\udde8\ud83c\uddff +420', value: '420CZ' }, + { code: 'SK', label: '\ud83c\uddf8\ud83c\uddf0 +421', value: '421SK' }, + { code: 'LI', label: '\ud83c\uddf1\ud83c\uddee +423', value: '423LI' }, + { code: 'AT', label: '\ud83c\udde6\ud83c\uddf9 +43', value: '43AT' }, + { code: 'IM', label: '\ud83c\uddee\ud83c\uddf2 +44', value: '44IM' }, + { code: 'GG', label: '\ud83c\uddec\ud83c\uddec +44', value: '44GG' }, + { code: 'JE', label: '\ud83c\uddef\ud83c\uddea +44', value: '44JE' }, + { code: 'GB', label: '\ud83c\uddec\ud83c\udde7 +44', value: '44GB' }, + { code: 'DK', label: '\ud83c\udde9\ud83c\uddf0 +45', value: '45DK' }, + { code: 'SE', label: '\ud83c\uddf8\ud83c\uddea +46', value: '46SE' }, + { code: 'NO', label: '\ud83c\uddf3\ud83c\uddf4 +47', value: '47NO' }, + { code: 'SJ', label: '\ud83c\uddf8\ud83c\uddef +47', value: '47SJ' }, + { code: 'PL', label: '\ud83c\uddf5\ud83c\uddf1 +48', value: '48PL' }, + { code: 'DE', label: '\ud83c\udde9\ud83c\uddea +49', value: '49DE' }, + { code: 'FK', label: '\ud83c\uddeb\ud83c\uddf0 +500', value: '500FK' }, + { code: 'GS', label: '\ud83c\uddec\ud83c\uddf8 +500', value: '500GS' }, + { code: 'BZ', label: '\ud83c\udde7\ud83c\uddff +501', value: '501BZ' }, + { code: 'GT', label: '\ud83c\uddec\ud83c\uddf9 +502', value: '502GT' }, + { code: 'SV', label: '\ud83c\uddf8\ud83c\uddfb +503', value: '503SV' }, + { code: 'HN', label: '\ud83c\udded\ud83c\uddf3 +504', value: '504HN' }, + { code: 'NI', label: '\ud83c\uddf3\ud83c\uddee +505', value: '505NI' }, + { code: 'CR', label: '\ud83c\udde8\ud83c\uddf7 +506', value: '506CR' }, + { code: 'PA', label: '\ud83c\uddf5\ud83c\udde6 +507', value: '507PA' }, + { code: 'PM', label: '\ud83c\uddf5\ud83c\uddf2 +508', value: '508PM' }, + { code: 'HT', label: '\ud83c\udded\ud83c\uddf9 +509', value: '509HT' }, + { code: 'PE', label: '\ud83c\uddf5\ud83c\uddea +51', value: '51PE' }, + { code: 'MX', label: '\ud83c\uddf2\ud83c\uddfd +52', value: '52MX' }, + { code: 'CU', label: '\ud83c\udde8\ud83c\uddfa +53', value: '53CU' }, + { code: 'CY', label: '\ud83c\udde8\ud83c\uddfe +537', value: '537CY' }, + { code: 'AR', label: '\ud83c\udde6\ud83c\uddf7 +54', value: '54AR' }, + { code: 'BR', label: '\ud83c\udde7\ud83c\uddf7 +55', value: '55BR' }, + { code: 'CL', label: '\ud83c\udde8\ud83c\uddf1 +56', value: '56CL' }, + { code: 'CO', label: '\ud83c\udde8\ud83c\uddf4 +57', value: '57CO' }, + { code: 'VE', label: '\ud83c\uddfb\ud83c\uddea +58', value: '58VE' }, + { code: 'BL', label: '\ud83c\udde7\ud83c\uddf1 +590', value: '590BL' }, + { code: 'MF', label: '\ud83c\uddf2\ud83c\uddeb +590', value: '590MF' }, + { code: 'GP', label: '\ud83c\uddec\ud83c\uddf5 +590', value: '590GP' }, + { code: 'BO', label: '\ud83c\udde7\ud83c\uddf4 +591', value: '591BO' }, + { code: 'EC', label: '\ud83c\uddea\ud83c\udde8 +593', value: '593EC' }, + { code: 'GF', label: '\ud83c\uddec\ud83c\uddeb +594', value: '594GF' }, + { code: 'GY', label: '\ud83c\uddec\ud83c\uddfe +595', value: '595GY' }, + { code: 'PY', label: '\ud83c\uddf5\ud83c\uddfe +595', value: '595PY' }, + { code: 'MQ', label: '\ud83c\uddf2\ud83c\uddf6 +596', value: '596MQ' }, + { code: 'SR', label: '\ud83c\uddf8\ud83c\uddf7 +597', value: '597SR' }, + { code: 'UY', label: '\ud83c\uddfa\ud83c\uddfe +598', value: '598UY' }, + { code: 'MY', label: '\ud83c\uddf2\ud83c\uddfe +60', value: '60MY' }, + { code: 'CC', label: '\ud83c\udde8\ud83c\udde8 +61', value: '61CC' }, + { code: 'AU', label: '\ud83c\udde6\ud83c\uddfa +61', value: '61AU' }, + { code: 'CX', label: '\ud83c\udde8\ud83c\uddfd +61', value: '61CX' }, + { code: 'ID', label: '\ud83c\uddee\ud83c\udde9 +62', value: '62ID' }, + { code: 'PH', label: '\ud83c\uddf5\ud83c\udded +63', value: '63PH' }, + { code: 'NZ', label: '\ud83c\uddf3\ud83c\uddff +64', value: '64NZ' }, + { code: 'SG', label: '\ud83c\uddf8\ud83c\uddec +65', value: '65SG' }, + { code: 'TH', label: '\ud83c\uddf9\ud83c\udded +66', value: '66TH' }, + { code: 'TL', label: '\ud83c\uddf9\ud83c\uddf1 +670', value: '670TL' }, + { code: 'NF', label: '\ud83c\uddf3\ud83c\uddeb +672', value: '672NF' }, + { code: 'BN', label: '\ud83c\udde7\ud83c\uddf3 +673', value: '673BN' }, + { code: 'NR', label: '\ud83c\uddf3\ud83c\uddf7 +674', value: '674NR' }, + { code: 'PG', label: '\ud83c\uddf5\ud83c\uddec +675', value: '675PG' }, + { code: 'TO', label: '\ud83c\uddf9\ud83c\uddf4 +676', value: '676TO' }, + { code: 'SB', label: '\ud83c\uddf8\ud83c\udde7 +677', value: '677SB' }, + { code: 'VU', label: '\ud83c\uddfb\ud83c\uddfa +678', value: '678VU' }, + { code: 'FJ', label: '\ud83c\uddeb\ud83c\uddef +679', value: '679FJ' }, + { code: 'PW', label: '\ud83c\uddf5\ud83c\uddfc +680', value: '680PW' }, + { code: 'WF', label: '\ud83c\uddfc\ud83c\uddeb +681', value: '681WF' }, + { code: 'CK', label: '\ud83c\udde8\ud83c\uddf0 +682', value: '682CK' }, + { code: 'NU', label: '\ud83c\uddf3\ud83c\uddfa +683', value: '683NU' }, + { code: 'WS', label: '\ud83c\uddfc\ud83c\uddf8 +685', value: '685WS' }, + { code: 'KI', label: '\ud83c\uddf0\ud83c\uddee +686', value: '686KI' }, + { code: 'NC', label: '\ud83c\uddf3\ud83c\udde8 +687', value: '687NC' }, + { code: 'TV', label: '\ud83c\uddf9\ud83c\uddfb +688', value: '688TV' }, + { code: 'PF', label: '\ud83c\uddf5\ud83c\uddeb +689', value: '689PF' }, + { code: 'TK', label: '\ud83c\uddf9\ud83c\uddf0 +690', value: '690TK' }, + { code: 'FM', label: '\ud83c\uddeb\ud83c\uddf2 +691', value: '691FM' }, + { code: 'MH', label: '\ud83c\uddf2\ud83c\udded +692', value: '692MH' }, + { code: 'RU', label: '\ud83c\uddf7\ud83c\uddfa +7', value: '7RU' }, + { code: 'KZ', label: '\ud83c\uddf0\ud83c\uddff +77', value: '77KZ' }, + { code: 'JP', label: '\ud83c\uddef\ud83c\uddf5 +81', value: '81JP' }, + { code: 'KR', label: '\ud83c\uddf0\ud83c\uddf7 +82', value: '82KR' }, + { code: 'VN', label: '\ud83c\uddfb\ud83c\uddf3 +84', value: '84VN' }, + { code: 'KP', label: '\ud83c\uddf0\ud83c\uddf5 +850', value: '850KP' }, + { code: 'HK', label: '\ud83c\udded\ud83c\uddf0 +852', value: '852HK' }, + { code: 'MO', label: '\ud83c\uddf2\ud83c\uddf4 +853', value: '853MO' }, + { code: 'KH', label: '\ud83c\uddf0\ud83c\udded +855', value: '855KH' }, + { code: 'LA', label: '\ud83c\uddf1\ud83c\udde6 +856', value: '856LA' }, + { code: 'CN', label: '\ud83c\udde8\ud83c\uddf3 +86', value: '86CN' }, + { code: 'PN', label: '\ud83c\uddf5\ud83c\uddf3 +872', value: '872PN' }, + { code: 'BD', label: '\ud83c\udde7\ud83c\udde9 +880', value: '880BD' }, + { code: 'TW', label: '\ud83c\uddf9\ud83c\uddfc +886', value: '886TW' }, + { code: 'TR', label: '\ud83c\uddf9\ud83c\uddf7 +90', value: '90TR' }, + { code: 'IN', label: '\ud83c\uddee\ud83c\uddf3 +91', value: '91IN' }, + { code: 'PK', label: '\ud83c\uddf5\ud83c\uddf0 +92', value: '92PK' }, + { code: 'AF', label: '\ud83c\udde6\ud83c\uddeb +93', value: '93AF' }, + { code: 'LK', label: '\ud83c\uddf1\ud83c\uddf0 +94', value: '94LK' }, + { code: 'MM', label: '\ud83c\uddf2\ud83c\uddf2 +95', value: '95MM' }, + { code: 'MV', label: '\ud83c\uddf2\ud83c\uddfb +960', value: '960MV' }, + { code: 'LB', label: '\ud83c\uddf1\ud83c\udde7 +961', value: '961LB' }, + { code: 'JO', label: '\ud83c\uddef\ud83c\uddf4 +962', value: '962JO' }, + { code: 'SY', label: '\ud83c\uddf8\ud83c\uddfe +963', value: '963SY' }, + { code: 'IQ', label: '\ud83c\uddee\ud83c\uddf6 +964', value: '964IQ' }, + { code: 'KW', label: '\ud83c\uddf0\ud83c\uddfc +965', value: '965KW' }, + { code: 'SA', label: '\ud83c\uddf8\ud83c\udde6 +966', value: '966SA' }, + { code: 'YE', label: '\ud83c\uddfe\ud83c\uddea +967', value: '967YE' }, + { code: 'OM', label: '\ud83c\uddf4\ud83c\uddf2 +968', value: '968OM' }, + { code: 'PS', label: '\ud83c\uddf5\ud83c\uddf8 +970', value: '970PS' }, + { code: 'AE', label: '\ud83c\udde6\ud83c\uddea +971', value: '971AE' }, + { code: 'IL', label: '\ud83c\uddee\ud83c\uddf1 +972', value: '972IL' }, + { code: 'BH', label: '\ud83c\udde7\ud83c\udded +973', value: '973BH' }, + { code: 'QA', label: '\ud83c\uddf6\ud83c\udde6 +974', value: '974QA' }, + { code: 'BT', label: '\ud83c\udde7\ud83c\uddf9 +975', value: '975BT' }, + { code: 'MN', label: '\ud83c\uddf2\ud83c\uddf3 +976', value: '976MN' }, + { code: 'NP', label: '\ud83c\uddf3\ud83c\uddf5 +977', value: '977NP' }, + { code: 'IR', label: '\ud83c\uddee\ud83c\uddf7 +98', value: '98IR' }, + { code: 'TJ', label: '\ud83c\uddf9\ud83c\uddef +992', value: '992TJ' }, + { code: 'TM', label: '\ud83c\uddf9\ud83c\uddf2 +993', value: '993TM' }, + { code: 'AZ', label: '\ud83c\udde6\ud83c\uddff +994', value: '994AZ' }, + { code: 'GE', label: '\ud83c\uddec\ud83c\uddea +995', value: '995GE' }, + { code: 'KG', label: '\ud83c\uddf0\ud83c\uddec +996', value: '996KG' }, + { code: 'UZ', label: '\ud83c\uddfa\ud83c\uddff +998', value: '998UZ' }, +]; diff --git a/extensions/blocks/send-a-message/edit.js b/extensions/blocks/send-a-message/edit.js new file mode 100644 index 0000000000000..2e39482b92523 --- /dev/null +++ b/extensions/blocks/send-a-message/edit.js @@ -0,0 +1,250 @@ +/** + * External dependencies + */ + +import { Component } from '@wordpress/element'; +import { __, _x } from '@wordpress/i18n'; +import { + Button, + Placeholder, + TextControl, + TextareaControl, + SelectControl, + Toolbar, + Popover, + Icon, + PanelBody, + PanelRow, +} from '@wordpress/components'; +import { + BlockControls, + InspectorControls, + RichText, + PanelColorSettings, + ContrastChecker, +} from '@wordpress/block-editor'; + +/** + * Internal dependencies + */ + +import { countryCodes } from './countrycodes.js'; +import { WhatsAppIcon } from './icon.js'; +import { title } from './index'; +import './view.scss'; + +export default class SendAMessageEdit extends Component { + constructor() { + super( ...arguments ); + + this.setDefaultCountryCode(); + + const isValidPhoneNumber = this.isValidPhoneNumber(); + this.state = { + editing: ! isValidPhoneNumber, + isValidPhoneNumber: isValidPhoneNumber, + }; + + this.onSubmitURL = this.onSubmitURL.bind( this ); + } + + async setDefaultCountryCode() { + const { countryCode } = this.props.attributes; + const { setAttributes } = this.props; + + if ( undefined === countryCode ) { + setAttributes( { countryCode: '1' } ); + + const geoFetch = await fetch( 'http://ip-api.com/json/?fields=countryCode' ) + .then( response => { + if ( ! response.ok ) { + return false; + } + + return response; + } ) + .catch( () => { + return false; + } ); + + if ( geoFetch ) { + const geo = await geoFetch.json(); + + countryCodes.forEach( item => { + if ( item.code === geo.countryCode ) { + setAttributes( { countryCode: item.value } ); + } + } ); + } + } + } + + onSubmitURL( e ) { + e.preventDefault(); + + if ( this.isValidPhoneNumber() ) { + this.setState( { + editing: false, + isValidPhoneNumber: true, + } ); + } else { + this.setState( { + isValidPhoneNumber: false, + } ); + } + } + + isValidPhoneNumber() { + const { countryCode, phoneNumber } = this.props.attributes; + const phoneNumberRegEx = RegExp( /^[+]?[\s./0-9]*[(]?[0-9]{1,4}[)]?[-\s./0-9]*$/, 'g' ); + + if ( undefined === phoneNumber || phoneNumber.length < 1 ) { + return false; + } + + return phoneNumberRegEx.test( countryCode.replace( /\D/g, '' ) + phoneNumber ); + } + + render() { + const { + countryCode, + phoneNumber, + buttonText, + firstMessage, + colorClass, + backgroundColor, + } = this.props.attributes; + + const { setAttributes, className } = this.props; + + const onFocusPhoneNumber = () => { + this.setState( { isValidPhoneNumber: true } ); + }; + + if ( this.state.editing ) { + return ( + +
+ setAttributes( { countryCode: value } ) } + options={ countryCodes } + /> + setAttributes( { phoneNumber: value } ) } + onFocus={ onFocusPhoneNumber } + value={ phoneNumber } + /> + { ! this.state.isValidPhoneNumber && ( + + + { __( 'Please enter a valid phone number', 'jetpack' ) } + + ) } + + +
+ ); + } + + const toolbarControls = [ + { + icon: 'edit', + title: __( 'Edit WhatsApp phone number', 'jetpack' ), + onClick: () => this.setState( { editing: true } ), + }, + ]; + + const setBackgroundColor = color => { + setAttributes( { backgroundColor: color } ); + + if ( color === undefined || color === '#25D366' || color === '#465B64' ) { + return setAttributes( { colorClass: 'dark' } ); + } + + setAttributes( { colorClass: 'light' } ); + }; + + return ( +
+ + + + + + + + setAttributes( { firstMessage: text } ) } + /> + + + setBackgroundColor( color ), + label: __( 'Background Color', 'jetpack' ), + disableCustomColors: true, + colors: [ + { + name: _x( 'WhatsApp Green', 'background color name', 'jetpack' ), + slug: 'whatsapp-green', + color: '#25D366', + }, + { + name: _x( 'WhatsApp Dark', 'background color name', 'jetpack' ), + slug: 'whatsapp-dark', + color: '#465B64', + }, + { + name: _x( 'WhatsApp Light', 'background color name', 'jetpack' ), + slug: 'whatsapp-light', + color: '#F4F4F4', + }, + { + name: _x( 'White', 'background color name', 'jetpack' ), + slug: 'whatsapp-white', + color: '#FFFFFF', + }, + ], + }, + ] } + > + + + + + setAttributes( { buttonText: value } ) } + withoutInteractiveFormatting + allowedFormats={ [] } + className="whatsapp-block__button" + tagName="a" + style={ { + backgroundColor: backgroundColor, + } } + /> +
+ ); + } +} diff --git a/extensions/blocks/send-a-message/editor.js b/extensions/blocks/send-a-message/editor.js new file mode 100644 index 0000000000000..d05f403942058 --- /dev/null +++ b/extensions/blocks/send-a-message/editor.js @@ -0,0 +1,7 @@ +/** + * Internal dependencies + */ +import registerJetpackBlock from '../../shared/register-jetpack-block'; +import { name, settings } from '.'; + +registerJetpackBlock( name, settings ); diff --git a/extensions/blocks/send-a-message/editor.scss b/extensions/blocks/send-a-message/editor.scss new file mode 100644 index 0000000000000..6595852dd0bf9 --- /dev/null +++ b/extensions/blocks/send-a-message/editor.scss @@ -0,0 +1,44 @@ +/** + * Editor styles for Send A Message + */ + +.wp-block-jetpack-send-a-message { + input.components-text-control__input { + padding: 5px 8px; + margin-left: 3px; + } + + select.components-select-control__input { + width: 95px; + min-height: 30px; + padding-left: 10px; + } + + button.components-button.is-large { + margin-left: 7px; + } + + .components-placeholder__label svg { + margin-right: 6px; + } +} + +.whatsapp-phonenumber-invalid { + .components-popover__content { + background: #B40000; + color: white; + padding: 6px 10px; + border-radius: 6px; + border: none; + } + + &:not(.is-without-arrow):not(.is-mobile):after { + border-color: #B40000; + } + + svg.dashicon { + vertical-align: top; + margin-right: 4px; + margin-top: -1px; + } +} diff --git a/extensions/blocks/send-a-message/icon.js b/extensions/blocks/send-a-message/icon.js new file mode 100644 index 0000000000000..9eb3771376f0a --- /dev/null +++ b/extensions/blocks/send-a-message/icon.js @@ -0,0 +1,10 @@ +/** + * External dependencies + */ +import { SVG, Path } from '@wordpress/components'; + +export default ( + + + +); diff --git a/extensions/blocks/send-a-message/index.js b/extensions/blocks/send-a-message/index.js new file mode 100644 index 0000000000000..5afed0da96b65 --- /dev/null +++ b/extensions/blocks/send-a-message/index.js @@ -0,0 +1,62 @@ +/** + * External dependencies + */ +import { __, _x } from '@wordpress/i18n'; +import { Fragment } from '@wordpress/element'; + +/** + * Internal dependencies + */ +import attributes from './attributes'; +import edit from './edit'; +import save from './save'; +import icon from './icon'; + +/** + * Style dependencies + */ +import './editor.scss'; + +export const name = 'send-a-message'; +export const title = __( 'Send A Message', 'jetpack' ); +export const whatsAppURL = 'https://api.whatsapp.com/send?phone='; +export const defaultButtonText = __( 'Chat on WhatsApp', 'jetpack' ); +export const defaultFirstMessage = __( + 'Hi, I got your WhatsApp information from your website.', + 'jetpack' +); + +export const settings = { + title, + description: ( + +

+ { __( + 'Let your visitors to send you a message on WhatsApp with the tap of a button.', + 'jetpack' + ) } +

+
+ ), + icon, + category: 'jetpack', + keywords: [ + _x( 'whatsapp', 'keyword', 'jetpack' ), + _x( 'messenger', 'keyword', 'jetpack' ), + _x( 'contact', 'keyword', 'jetpack' ), + _x( 'support', 'keyword', 'jetpack' ), + ], + supports: { + align: [ 'left', 'center', 'right' ], + html: false, + }, + attributes, + edit, + save: save, + example: { + attributes: { + countryCode: '1', + phoneNumber: '555-123-4567', + }, + }, +}; diff --git a/extensions/blocks/send-a-message/save.js b/extensions/blocks/send-a-message/save.js new file mode 100644 index 0000000000000..5fe53058cbf02 --- /dev/null +++ b/extensions/blocks/send-a-message/save.js @@ -0,0 +1,44 @@ +/** + * External dependencies + */ +import { RichText } from '@wordpress/block-editor'; + +/** + * Internal dependencies + */ +import { whatsAppURL } from './index'; + +export default function SendAMessageSave( props ) { + const { + countryCode, + phoneNumber, + firstMessage, + buttonText, + backgroundColor, + colorClass, + } = props.attributes; + + const fullPhoneNumber = countryCode.replace( /\D+/g, '' ) + phoneNumber.replace( /\D+/g, '' ); + + const getWhatsAppUrl = () => { + let url = whatsAppURL + fullPhoneNumber; + + if ( '' !== firstMessage ) { + url += '&text=' + encodeURIComponent( firstMessage ); + } + + return url; + }; + + return ( +
+ + + +
+ ); +} diff --git a/extensions/blocks/send-a-message/send-a-message.php b/extensions/blocks/send-a-message/send-a-message.php new file mode 100644 index 0000000000000..c23016c291af9 --- /dev/null +++ b/extensions/blocks/send-a-message/send-a-message.php @@ -0,0 +1,43 @@ + __NAMESPACE__ . '\load_assets' ) + ); +} +add_action( 'init', __NAMESPACE__ . '\register_block' ); + +/** + * Send A Message block registration/dependency declaration. + * + * @param array $attr Array containing the Send A Message block attributes. + * @param string $content String containing the Send A Message block content. + * + * @return string + */ +function load_assets( $attr, $content ) { + /* + * Enqueue necessary scripts and styles. + */ + \Jetpack_Gutenberg::load_assets_as_required( 'send-a-message' ); + + return $content; +} diff --git a/extensions/blocks/send-a-message/view.js b/extensions/blocks/send-a-message/view.js new file mode 100644 index 0000000000000..741c50d6a0ff9 --- /dev/null +++ b/extensions/blocks/send-a-message/view.js @@ -0,0 +1,4 @@ +/** + * Internal dependencies + */ +import './view.scss'; diff --git a/extensions/blocks/send-a-message/view.scss b/extensions/blocks/send-a-message/view.scss new file mode 100644 index 0000000000000..5aaf2cc1942d9 --- /dev/null +++ b/extensions/blocks/send-a-message/view.scss @@ -0,0 +1,37 @@ +div.wp-block-jetpack-send-a-message { + a.whatsapp-block__button { + background: #25D366; + color: #fff; + display: inline-block; + padding: 8px 16px 8px 56px; + border-radius: 8px; + text-decoration: none; + + font-family: "Helvetica Neue", Helvetica, Arials, sans-serif; + font-weight: 500; + + background-image: url("data:image/svg+xml;utf8,"); + background-position: 16px center; + background-repeat: no-repeat; + background-size: 32px 32px; + } + + &.is-color-light { + a.whatsapp-block__button { + color: #465B64; + background-image: url("data:image/svg+xml;utf8,"); + } + } + + &.aligncenter { + text-align: center; + } + + &.alignright { + text-align: right; + } + + &:hover { + opacity: 0.9; + } +} diff --git a/extensions/index.json b/extensions/index.json index 428c8e7b138d5..91c2777a82d48 100644 --- a/extensions/index.json +++ b/extensions/index.json @@ -32,7 +32,7 @@ "videopress", "wordads" ], - "beta": [ "amazon" ], + "beta": [ "amazon", "send-a-message" ], "experimental": [ "seo" ], "no-post-editor": [ "business-hours", From 46cefae6e08e1404b4de764526379f83a4d6714a Mon Sep 17 00:00:00 2001 From: apeatling Date: Thu, 21 May 2020 10:43:01 -0700 Subject: [PATCH 02/32] Switch to innerblocks for supporting WhatsApp block within main Send a Message block parent. This will allow us to support other services within the same block in the future, while providing service variations so that users can directly insert the buttons they want from the inserter. --- extensions/blocks/send-a-message/edit.js | 252 +----------------- extensions/blocks/send-a-message/editor.js | 11 +- extensions/blocks/send-a-message/editor.scss | 41 --- extensions/blocks/send-a-message/index.js | 43 +-- .../whatsapp}/attributes.js | 0 .../service-blocks/whatsapp/edit.js | 250 +++++++++++++++++ .../service-blocks/whatsapp/editor.scss | 44 +++ .../{ => service-blocks/whatsapp}/icon.js | 0 .../service-blocks/whatsapp/index.js | 54 ++++ .../{ => service-blocks/whatsapp}/save.js | 0 .../{ => service-blocks/whatsapp}/view.js | 0 .../{ => service-blocks/whatsapp}/view.scss | 0 .../{ => shared}/countrycodes.js | 0 .../blocks/send-a-message/variations.js | 25 ++ 14 files changed, 403 insertions(+), 317 deletions(-) rename extensions/blocks/send-a-message/{ => service-blocks/whatsapp}/attributes.js (100%) create mode 100644 extensions/blocks/send-a-message/service-blocks/whatsapp/edit.js create mode 100644 extensions/blocks/send-a-message/service-blocks/whatsapp/editor.scss rename extensions/blocks/send-a-message/{ => service-blocks/whatsapp}/icon.js (100%) create mode 100644 extensions/blocks/send-a-message/service-blocks/whatsapp/index.js rename extensions/blocks/send-a-message/{ => service-blocks/whatsapp}/save.js (100%) rename extensions/blocks/send-a-message/{ => service-blocks/whatsapp}/view.js (100%) rename extensions/blocks/send-a-message/{ => service-blocks/whatsapp}/view.scss (100%) rename extensions/blocks/send-a-message/{ => shared}/countrycodes.js (100%) create mode 100644 extensions/blocks/send-a-message/variations.js diff --git a/extensions/blocks/send-a-message/edit.js b/extensions/blocks/send-a-message/edit.js index 2e39482b92523..5c8777d8acf35 100644 --- a/extensions/blocks/send-a-message/edit.js +++ b/extensions/blocks/send-a-message/edit.js @@ -1,250 +1,16 @@ /** * External dependencies */ +import { InnerBlocks } from '@wordpress/block-editor'; -import { Component } from '@wordpress/element'; -import { __, _x } from '@wordpress/i18n'; -import { - Button, - Placeholder, - TextControl, - TextareaControl, - SelectControl, - Toolbar, - Popover, - Icon, - PanelBody, - PanelRow, -} from '@wordpress/components'; -import { - BlockControls, - InspectorControls, - RichText, - PanelColorSettings, - ContrastChecker, -} from '@wordpress/block-editor'; +export default function SendAMessageEdit( props ) { + const { className } = props; -/** - * Internal dependencies - */ - -import { countryCodes } from './countrycodes.js'; -import { WhatsAppIcon } from './icon.js'; -import { title } from './index'; -import './view.scss'; - -export default class SendAMessageEdit extends Component { - constructor() { - super( ...arguments ); - - this.setDefaultCountryCode(); - - const isValidPhoneNumber = this.isValidPhoneNumber(); - this.state = { - editing: ! isValidPhoneNumber, - isValidPhoneNumber: isValidPhoneNumber, - }; - - this.onSubmitURL = this.onSubmitURL.bind( this ); - } - - async setDefaultCountryCode() { - const { countryCode } = this.props.attributes; - const { setAttributes } = this.props; - - if ( undefined === countryCode ) { - setAttributes( { countryCode: '1' } ); - - const geoFetch = await fetch( 'http://ip-api.com/json/?fields=countryCode' ) - .then( response => { - if ( ! response.ok ) { - return false; - } - - return response; - } ) - .catch( () => { - return false; - } ); - - if ( geoFetch ) { - const geo = await geoFetch.json(); - - countryCodes.forEach( item => { - if ( item.code === geo.countryCode ) { - setAttributes( { countryCode: item.value } ); - } - } ); - } - } - } - - onSubmitURL( e ) { - e.preventDefault(); - - if ( this.isValidPhoneNumber() ) { - this.setState( { - editing: false, - isValidPhoneNumber: true, - } ); - } else { - this.setState( { - isValidPhoneNumber: false, - } ); - } - } - - isValidPhoneNumber() { - const { countryCode, phoneNumber } = this.props.attributes; - const phoneNumberRegEx = RegExp( /^[+]?[\s./0-9]*[(]?[0-9]{1,4}[)]?[-\s./0-9]*$/, 'g' ); - - if ( undefined === phoneNumber || phoneNumber.length < 1 ) { - return false; - } - - return phoneNumberRegEx.test( countryCode.replace( /\D/g, '' ) + phoneNumber ); - } - - render() { - const { - countryCode, - phoneNumber, - buttonText, - firstMessage, - colorClass, - backgroundColor, - } = this.props.attributes; - - const { setAttributes, className } = this.props; - - const onFocusPhoneNumber = () => { - this.setState( { isValidPhoneNumber: true } ); - }; - - if ( this.state.editing ) { - return ( - -
- setAttributes( { countryCode: value } ) } - options={ countryCodes } - /> - setAttributes( { phoneNumber: value } ) } - onFocus={ onFocusPhoneNumber } - value={ phoneNumber } - /> - { ! this.state.isValidPhoneNumber && ( - - - { __( 'Please enter a valid phone number', 'jetpack' ) } - - ) } - - -
- ); - } - - const toolbarControls = [ - { - icon: 'edit', - title: __( 'Edit WhatsApp phone number', 'jetpack' ), - onClick: () => this.setState( { editing: true } ), - }, - ]; - - const setBackgroundColor = color => { - setAttributes( { backgroundColor: color } ); - - if ( color === undefined || color === '#25D366' || color === '#465B64' ) { - return setAttributes( { colorClass: 'dark' } ); - } - - setAttributes( { colorClass: 'light' } ); - }; - - return ( -
- - - - - - - - setAttributes( { firstMessage: text } ) } - /> - - - setBackgroundColor( color ), - label: __( 'Background Color', 'jetpack' ), - disableCustomColors: true, - colors: [ - { - name: _x( 'WhatsApp Green', 'background color name', 'jetpack' ), - slug: 'whatsapp-green', - color: '#25D366', - }, - { - name: _x( 'WhatsApp Dark', 'background color name', 'jetpack' ), - slug: 'whatsapp-dark', - color: '#465B64', - }, - { - name: _x( 'WhatsApp Light', 'background color name', 'jetpack' ), - slug: 'whatsapp-light', - color: '#F4F4F4', - }, - { - name: _x( 'White', 'background color name', 'jetpack' ), - slug: 'whatsapp-white', - color: '#FFFFFF', - }, - ], - }, - ] } - > - - - + const ALLOWED_BLOCKS = [ 'jetpack/whatsapp-button' ]; - setAttributes( { buttonText: value } ) } - withoutInteractiveFormatting - allowedFormats={ [] } - className="whatsapp-block__button" - tagName="a" - style={ { - backgroundColor: backgroundColor, - } } - /> -
- ); - } + return ( +
+ +
+ ); } diff --git a/extensions/blocks/send-a-message/editor.js b/extensions/blocks/send-a-message/editor.js index d05f403942058..8543749f7ff5f 100644 --- a/extensions/blocks/send-a-message/editor.js +++ b/extensions/blocks/send-a-message/editor.js @@ -3,5 +3,14 @@ */ import registerJetpackBlock from '../../shared/register-jetpack-block'; import { name, settings } from '.'; +import { + name as whatsAppBlockName, + settings as whatsAppBlockSettings, +} from './service-blocks/whatsapp'; -registerJetpackBlock( name, settings ); +registerJetpackBlock( name, settings, [ + { + name: whatsAppBlockName, + settings: whatsAppBlockSettings, + }, +] ); diff --git a/extensions/blocks/send-a-message/editor.scss b/extensions/blocks/send-a-message/editor.scss index 6595852dd0bf9..97360cbf36bec 100644 --- a/extensions/blocks/send-a-message/editor.scss +++ b/extensions/blocks/send-a-message/editor.scss @@ -1,44 +1,3 @@ -/** - * Editor styles for Send A Message - */ - .wp-block-jetpack-send-a-message { - input.components-text-control__input { - padding: 5px 8px; - margin-left: 3px; - } - - select.components-select-control__input { - width: 95px; - min-height: 30px; - padding-left: 10px; - } - - button.components-button.is-large { - margin-left: 7px; - } - - .components-placeholder__label svg { - margin-right: 6px; - } -} - -.whatsapp-phonenumber-invalid { - .components-popover__content { - background: #B40000; - color: white; - padding: 6px 10px; - border-radius: 6px; - border: none; - } - - &:not(.is-without-arrow):not(.is-mobile):after { - border-color: #B40000; - } - svg.dashicon { - vertical-align: top; - margin-right: 4px; - margin-top: -1px; - } } diff --git a/extensions/blocks/send-a-message/index.js b/extensions/blocks/send-a-message/index.js index 5afed0da96b65..91720c4cf8939 100644 --- a/extensions/blocks/send-a-message/index.js +++ b/extensions/blocks/send-a-message/index.js @@ -2,15 +2,15 @@ * External dependencies */ import { __, _x } from '@wordpress/i18n'; -import { Fragment } from '@wordpress/element'; +import { InnerBlocks } from '@wordpress/block-editor'; +import { Path } from '@wordpress/components'; /** * Internal dependencies */ -import attributes from './attributes'; import edit from './edit'; -import save from './save'; -import icon from './icon'; +import variations from './variations'; +import renderMaterialIcon from '../../shared/render-material-icon'; /** * Style dependencies @@ -19,26 +19,13 @@ import './editor.scss'; export const name = 'send-a-message'; export const title = __( 'Send A Message', 'jetpack' ); -export const whatsAppURL = 'https://api.whatsapp.com/send?phone='; -export const defaultButtonText = __( 'Chat on WhatsApp', 'jetpack' ); -export const defaultFirstMessage = __( - 'Hi, I got your WhatsApp information from your website.', - 'jetpack' -); export const settings = { title, - description: ( - -

- { __( - 'Let your visitors to send you a message on WhatsApp with the tap of a button.', - 'jetpack' - ) } -

-
+ description: __( 'Let your visitors to send you messages with the tap of a button.', 'jetpack' ), + icon: renderMaterialIcon( + ), - icon, category: 'jetpack', keywords: [ _x( 'whatsapp', 'keyword', 'jetpack' ), @@ -46,17 +33,9 @@ export const settings = { _x( 'contact', 'keyword', 'jetpack' ), _x( 'support', 'keyword', 'jetpack' ), ], - supports: { - align: [ 'left', 'center', 'right' ], - html: false, - }, - attributes, + attributes: {}, edit, - save: save, - example: { - attributes: { - countryCode: '1', - phoneNumber: '555-123-4567', - }, - }, + save: InnerBlocks.Content, + variations, + example: {}, }; diff --git a/extensions/blocks/send-a-message/attributes.js b/extensions/blocks/send-a-message/service-blocks/whatsapp/attributes.js similarity index 100% rename from extensions/blocks/send-a-message/attributes.js rename to extensions/blocks/send-a-message/service-blocks/whatsapp/attributes.js diff --git a/extensions/blocks/send-a-message/service-blocks/whatsapp/edit.js b/extensions/blocks/send-a-message/service-blocks/whatsapp/edit.js new file mode 100644 index 0000000000000..b0630af7d098e --- /dev/null +++ b/extensions/blocks/send-a-message/service-blocks/whatsapp/edit.js @@ -0,0 +1,250 @@ +/** + * External dependencies + */ + +import { Component } from '@wordpress/element'; +import { __, _x } from '@wordpress/i18n'; +import { + Button, + Placeholder, + TextControl, + TextareaControl, + SelectControl, + Toolbar, + Popover, + Icon, + PanelBody, + PanelRow, +} from '@wordpress/components'; +import { + BlockControls, + InspectorControls, + RichText, + PanelColorSettings, + ContrastChecker, +} from '@wordpress/block-editor'; + +/** + * Internal dependencies + */ + +import { countryCodes } from '../../shared/countrycodes.js'; +import { WhatsAppIcon } from './icon.js'; +import { title } from './index'; +import './view.scss'; + +export default class SendAMessageEdit extends Component { + constructor() { + super( ...arguments ); + + this.setDefaultCountryCode(); + + const isValidPhoneNumber = this.isValidPhoneNumber(); + this.state = { + editing: ! isValidPhoneNumber, + isValidPhoneNumber: isValidPhoneNumber, + }; + + this.onSubmitURL = this.onSubmitURL.bind( this ); + } + + async setDefaultCountryCode() { + const { countryCode } = this.props.attributes; + const { setAttributes } = this.props; + + if ( undefined === countryCode ) { + setAttributes( { countryCode: '1' } ); + + const geoFetch = await fetch( 'http://ip-api.com/json/?fields=countryCode' ) + .then( response => { + if ( ! response.ok ) { + return false; + } + + return response; + } ) + .catch( () => { + return false; + } ); + + if ( geoFetch ) { + const geo = await geoFetch.json(); + + countryCodes.forEach( item => { + if ( item.code === geo.countryCode ) { + setAttributes( { countryCode: item.value } ); + } + } ); + } + } + } + + onSubmitURL( e ) { + e.preventDefault(); + + if ( this.isValidPhoneNumber() ) { + this.setState( { + editing: false, + isValidPhoneNumber: true, + } ); + } else { + this.setState( { + isValidPhoneNumber: false, + } ); + } + } + + isValidPhoneNumber() { + const { countryCode, phoneNumber } = this.props.attributes; + const phoneNumberRegEx = RegExp( /^[+]?[\s./0-9]*[(]?[0-9]{1,4}[)]?[-\s./0-9]*$/, 'g' ); + + if ( undefined === phoneNumber || phoneNumber.length < 1 ) { + return false; + } + + return phoneNumberRegEx.test( countryCode.replace( /\D/g, '' ) + phoneNumber ); + } + + render() { + const { + countryCode, + phoneNumber, + buttonText, + firstMessage, + colorClass, + backgroundColor, + } = this.props.attributes; + + const { setAttributes, className } = this.props; + + const onFocusPhoneNumber = () => { + this.setState( { isValidPhoneNumber: true } ); + }; + + if ( this.state.editing ) { + return ( + +
+ setAttributes( { countryCode: value } ) } + options={ countryCodes } + /> + setAttributes( { phoneNumber: value } ) } + onFocus={ onFocusPhoneNumber } + value={ phoneNumber } + /> + { ! this.state.isValidPhoneNumber && ( + + + { __( 'Please enter a valid phone number', 'jetpack' ) } + + ) } + + +
+ ); + } + + const toolbarControls = [ + { + icon: 'edit', + title: __( 'Edit WhatsApp phone number', 'jetpack' ), + onClick: () => this.setState( { editing: true } ), + }, + ]; + + const setBackgroundColor = color => { + setAttributes( { backgroundColor: color } ); + + if ( color === undefined || color === '#25D366' || color === '#465B64' ) { + return setAttributes( { colorClass: 'dark' } ); + } + + setAttributes( { colorClass: 'light' } ); + }; + + return ( +
+ + + + + + + + setAttributes( { firstMessage: text } ) } + /> + + + setBackgroundColor( color ), + label: __( 'Background Color', 'jetpack' ), + disableCustomColors: true, + colors: [ + { + name: _x( 'WhatsApp Green', 'background color name', 'jetpack' ), + slug: 'whatsapp-green', + color: '#25D366', + }, + { + name: _x( 'WhatsApp Dark', 'background color name', 'jetpack' ), + slug: 'whatsapp-dark', + color: '#465B64', + }, + { + name: _x( 'WhatsApp Light', 'background color name', 'jetpack' ), + slug: 'whatsapp-light', + color: '#F4F4F4', + }, + { + name: _x( 'White', 'background color name', 'jetpack' ), + slug: 'whatsapp-white', + color: '#FFFFFF', + }, + ], + }, + ] } + > + + + + + setAttributes( { buttonText: value } ) } + withoutInteractiveFormatting + allowedFormats={ [] } + className="whatsapp-block__button" + tagName="a" + style={ { + backgroundColor: backgroundColor, + } } + /> +
+ ); + } +} diff --git a/extensions/blocks/send-a-message/service-blocks/whatsapp/editor.scss b/extensions/blocks/send-a-message/service-blocks/whatsapp/editor.scss new file mode 100644 index 0000000000000..6595852dd0bf9 --- /dev/null +++ b/extensions/blocks/send-a-message/service-blocks/whatsapp/editor.scss @@ -0,0 +1,44 @@ +/** + * Editor styles for Send A Message + */ + +.wp-block-jetpack-send-a-message { + input.components-text-control__input { + padding: 5px 8px; + margin-left: 3px; + } + + select.components-select-control__input { + width: 95px; + min-height: 30px; + padding-left: 10px; + } + + button.components-button.is-large { + margin-left: 7px; + } + + .components-placeholder__label svg { + margin-right: 6px; + } +} + +.whatsapp-phonenumber-invalid { + .components-popover__content { + background: #B40000; + color: white; + padding: 6px 10px; + border-radius: 6px; + border: none; + } + + &:not(.is-without-arrow):not(.is-mobile):after { + border-color: #B40000; + } + + svg.dashicon { + vertical-align: top; + margin-right: 4px; + margin-top: -1px; + } +} diff --git a/extensions/blocks/send-a-message/icon.js b/extensions/blocks/send-a-message/service-blocks/whatsapp/icon.js similarity index 100% rename from extensions/blocks/send-a-message/icon.js rename to extensions/blocks/send-a-message/service-blocks/whatsapp/icon.js diff --git a/extensions/blocks/send-a-message/service-blocks/whatsapp/index.js b/extensions/blocks/send-a-message/service-blocks/whatsapp/index.js new file mode 100644 index 0000000000000..7c7993352f43a --- /dev/null +++ b/extensions/blocks/send-a-message/service-blocks/whatsapp/index.js @@ -0,0 +1,54 @@ +/** + * External dependencies + */ +import { __, _x } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import attributes from './attributes'; +import edit from './edit'; +import save from './save'; +import icon from './icon'; +import { supportsCollections } from '../../../../shared/block-category'; +import './editor.scss'; + +export const name = 'whatsapp-button'; +export const title = __( 'WhatsApp Button', 'jetpack' ); +export const whatsAppURL = 'https://api.whatsapp.com/send?phone='; +export const defaultButtonText = __( 'Chat on WhatsApp', 'jetpack' ); +export const defaultFirstMessage = __( + 'Hi, I got your WhatsApp information from your website.', + 'jetpack' +); + +export const settings = { + title, + description: __( + 'Let your visitors to send you a message on WhatsApp with the tap of a button.', + 'jetpack' + ), + icon, + category: supportsCollections() ? 'grow' : 'jetpack', + parent: [ 'jetpack/send-a-message' ], + keywords: [ + _x( 'whatsapp', 'keyword', 'jetpack' ), + _x( 'messenger', 'keyword', 'jetpack' ), + _x( 'contact', 'keyword', 'jetpack' ), + _x( 'support', 'keyword', 'jetpack' ), + ], + supports: { + align: [ 'left', 'center', 'right' ], + html: false, + reusable: false, + }, + attributes, + edit, + save: save, + example: { + attributes: { + countryCode: '1', + phoneNumber: '555-123-4567', + }, + }, +}; diff --git a/extensions/blocks/send-a-message/save.js b/extensions/blocks/send-a-message/service-blocks/whatsapp/save.js similarity index 100% rename from extensions/blocks/send-a-message/save.js rename to extensions/blocks/send-a-message/service-blocks/whatsapp/save.js diff --git a/extensions/blocks/send-a-message/view.js b/extensions/blocks/send-a-message/service-blocks/whatsapp/view.js similarity index 100% rename from extensions/blocks/send-a-message/view.js rename to extensions/blocks/send-a-message/service-blocks/whatsapp/view.js diff --git a/extensions/blocks/send-a-message/view.scss b/extensions/blocks/send-a-message/service-blocks/whatsapp/view.scss similarity index 100% rename from extensions/blocks/send-a-message/view.scss rename to extensions/blocks/send-a-message/service-blocks/whatsapp/view.scss diff --git a/extensions/blocks/send-a-message/countrycodes.js b/extensions/blocks/send-a-message/shared/countrycodes.js similarity index 100% rename from extensions/blocks/send-a-message/countrycodes.js rename to extensions/blocks/send-a-message/shared/countrycodes.js diff --git a/extensions/blocks/send-a-message/variations.js b/extensions/blocks/send-a-message/variations.js new file mode 100644 index 0000000000000..81ac994943d2a --- /dev/null +++ b/extensions/blocks/send-a-message/variations.js @@ -0,0 +1,25 @@ +/** + * External dependencies + */ +import { __ } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import whatsAppIcon from './service-blocks/whatsapp/icon'; + +const variations = [ + { + isDefault: true, + name: 'whatsapp-button', + title: __( 'WhatsApp Button', 'jetpack' ), + description: __( + 'Let your visitors to send you messages on WhatsApp with the tap of a button.', + 'jetpack' + ), + icon: whatsAppIcon, + innerBlocks: [ [ 'jetpack/whatsapp-button' ] ], + }, +]; + +export default variations; From 4164f71ad8122704a91aeef122c16f0e29b0472d Mon Sep 17 00:00:00 2001 From: apeatling Date: Thu, 21 May 2020 11:17:42 -0700 Subject: [PATCH 03/32] Make sure that innerblocks are registered and can load front end styles --- extensions/blocks/send-a-message/editor.js | 10 +- .../blocks/send-a-message/send-a-message.php | 26 +- .../service-blocks/whatsapp/attributes.js | 31 --- .../service-blocks/whatsapp/edit.js | 250 ------------------ .../service-blocks/whatsapp/editor.scss | 44 --- .../service-blocks/whatsapp/icon.js | 10 - .../service-blocks/whatsapp/index.js | 54 ---- .../service-blocks/whatsapp/save.js | 44 --- .../service-blocks/whatsapp/view.js | 4 - .../service-blocks/whatsapp/view.scss | 37 --- .../blocks/send-a-message/variations.js | 2 +- extensions/index.json | 2 +- 12 files changed, 20 insertions(+), 494 deletions(-) delete mode 100644 extensions/blocks/send-a-message/service-blocks/whatsapp/attributes.js delete mode 100644 extensions/blocks/send-a-message/service-blocks/whatsapp/edit.js delete mode 100644 extensions/blocks/send-a-message/service-blocks/whatsapp/editor.scss delete mode 100644 extensions/blocks/send-a-message/service-blocks/whatsapp/icon.js delete mode 100644 extensions/blocks/send-a-message/service-blocks/whatsapp/index.js delete mode 100644 extensions/blocks/send-a-message/service-blocks/whatsapp/save.js delete mode 100644 extensions/blocks/send-a-message/service-blocks/whatsapp/view.js delete mode 100644 extensions/blocks/send-a-message/service-blocks/whatsapp/view.scss diff --git a/extensions/blocks/send-a-message/editor.js b/extensions/blocks/send-a-message/editor.js index 8543749f7ff5f..85233e379c424 100644 --- a/extensions/blocks/send-a-message/editor.js +++ b/extensions/blocks/send-a-message/editor.js @@ -4,13 +4,13 @@ import registerJetpackBlock from '../../shared/register-jetpack-block'; import { name, settings } from '.'; import { - name as whatsAppBlockName, - settings as whatsAppBlockSettings, -} from './service-blocks/whatsapp'; + name as whatsAppButtonBlockName, + settings as whatsAppButtonBlockSettings, +} from './whatsapp-button'; registerJetpackBlock( name, settings, [ { - name: whatsAppBlockName, - settings: whatsAppBlockSettings, + name: whatsAppButtonBlockName, + settings: whatsAppButtonBlockSettings, }, ] ); diff --git a/extensions/blocks/send-a-message/send-a-message.php b/extensions/blocks/send-a-message/send-a-message.php index c23016c291af9..bae3389523e1a 100644 --- a/extensions/blocks/send-a-message/send-a-message.php +++ b/extensions/blocks/send-a-message/send-a-message.php @@ -1,13 +1,16 @@ __NAMESPACE__ . '\load_assets' ) + array( 'render_callback' => __NAMESPACE__ . '\render_block' ) ); } add_action( 'init', __NAMESPACE__ . '\register_block' ); /** - * Send A Message block registration/dependency declaration. + * Render callback. * - * @param array $attr Array containing the Send A Message block attributes. - * @param string $content String containing the Send A Message block content. + * @param array $attributes Array containing the block attributes. + * @param string $content String containing the block content. * * @return string */ -function load_assets( $attr, $content ) { - /* - * Enqueue necessary scripts and styles. - */ - \Jetpack_Gutenberg::load_assets_as_required( 'send-a-message' ); +function render_block( $attributes, $content ) { + Jetpack_Gutenberg::load_styles_as_required( FEATURE_NAME ); return $content; } diff --git a/extensions/blocks/send-a-message/service-blocks/whatsapp/attributes.js b/extensions/blocks/send-a-message/service-blocks/whatsapp/attributes.js deleted file mode 100644 index 7c2a4657ff7eb..0000000000000 --- a/extensions/blocks/send-a-message/service-blocks/whatsapp/attributes.js +++ /dev/null @@ -1,31 +0,0 @@ -/** - * External dependencies - */ -import { __ } from '@wordpress/i18n'; - -export default { - countryCode: { - type: 'string', - }, - phoneNumber: { - type: 'string', - }, - firstMessage: { - type: 'string', - default: __( 'Hi, I got your WhatsApp information from your website.', 'jetpack' ), - }, - buttonText: { - type: 'array', - source: 'children', - selector: 'a.whatsapp-block__button', - default: __( 'Chat on WhatsApp', 'jetpack' ), - }, - backgroundColor: { - type: 'string', - default: '#25D366', - }, - colorClass: { - type: 'string', - default: 'dark', - }, -}; diff --git a/extensions/blocks/send-a-message/service-blocks/whatsapp/edit.js b/extensions/blocks/send-a-message/service-blocks/whatsapp/edit.js deleted file mode 100644 index b0630af7d098e..0000000000000 --- a/extensions/blocks/send-a-message/service-blocks/whatsapp/edit.js +++ /dev/null @@ -1,250 +0,0 @@ -/** - * External dependencies - */ - -import { Component } from '@wordpress/element'; -import { __, _x } from '@wordpress/i18n'; -import { - Button, - Placeholder, - TextControl, - TextareaControl, - SelectControl, - Toolbar, - Popover, - Icon, - PanelBody, - PanelRow, -} from '@wordpress/components'; -import { - BlockControls, - InspectorControls, - RichText, - PanelColorSettings, - ContrastChecker, -} from '@wordpress/block-editor'; - -/** - * Internal dependencies - */ - -import { countryCodes } from '../../shared/countrycodes.js'; -import { WhatsAppIcon } from './icon.js'; -import { title } from './index'; -import './view.scss'; - -export default class SendAMessageEdit extends Component { - constructor() { - super( ...arguments ); - - this.setDefaultCountryCode(); - - const isValidPhoneNumber = this.isValidPhoneNumber(); - this.state = { - editing: ! isValidPhoneNumber, - isValidPhoneNumber: isValidPhoneNumber, - }; - - this.onSubmitURL = this.onSubmitURL.bind( this ); - } - - async setDefaultCountryCode() { - const { countryCode } = this.props.attributes; - const { setAttributes } = this.props; - - if ( undefined === countryCode ) { - setAttributes( { countryCode: '1' } ); - - const geoFetch = await fetch( 'http://ip-api.com/json/?fields=countryCode' ) - .then( response => { - if ( ! response.ok ) { - return false; - } - - return response; - } ) - .catch( () => { - return false; - } ); - - if ( geoFetch ) { - const geo = await geoFetch.json(); - - countryCodes.forEach( item => { - if ( item.code === geo.countryCode ) { - setAttributes( { countryCode: item.value } ); - } - } ); - } - } - } - - onSubmitURL( e ) { - e.preventDefault(); - - if ( this.isValidPhoneNumber() ) { - this.setState( { - editing: false, - isValidPhoneNumber: true, - } ); - } else { - this.setState( { - isValidPhoneNumber: false, - } ); - } - } - - isValidPhoneNumber() { - const { countryCode, phoneNumber } = this.props.attributes; - const phoneNumberRegEx = RegExp( /^[+]?[\s./0-9]*[(]?[0-9]{1,4}[)]?[-\s./0-9]*$/, 'g' ); - - if ( undefined === phoneNumber || phoneNumber.length < 1 ) { - return false; - } - - return phoneNumberRegEx.test( countryCode.replace( /\D/g, '' ) + phoneNumber ); - } - - render() { - const { - countryCode, - phoneNumber, - buttonText, - firstMessage, - colorClass, - backgroundColor, - } = this.props.attributes; - - const { setAttributes, className } = this.props; - - const onFocusPhoneNumber = () => { - this.setState( { isValidPhoneNumber: true } ); - }; - - if ( this.state.editing ) { - return ( - -
- setAttributes( { countryCode: value } ) } - options={ countryCodes } - /> - setAttributes( { phoneNumber: value } ) } - onFocus={ onFocusPhoneNumber } - value={ phoneNumber } - /> - { ! this.state.isValidPhoneNumber && ( - - - { __( 'Please enter a valid phone number', 'jetpack' ) } - - ) } - - -
- ); - } - - const toolbarControls = [ - { - icon: 'edit', - title: __( 'Edit WhatsApp phone number', 'jetpack' ), - onClick: () => this.setState( { editing: true } ), - }, - ]; - - const setBackgroundColor = color => { - setAttributes( { backgroundColor: color } ); - - if ( color === undefined || color === '#25D366' || color === '#465B64' ) { - return setAttributes( { colorClass: 'dark' } ); - } - - setAttributes( { colorClass: 'light' } ); - }; - - return ( -
- - - - - - - - setAttributes( { firstMessage: text } ) } - /> - - - setBackgroundColor( color ), - label: __( 'Background Color', 'jetpack' ), - disableCustomColors: true, - colors: [ - { - name: _x( 'WhatsApp Green', 'background color name', 'jetpack' ), - slug: 'whatsapp-green', - color: '#25D366', - }, - { - name: _x( 'WhatsApp Dark', 'background color name', 'jetpack' ), - slug: 'whatsapp-dark', - color: '#465B64', - }, - { - name: _x( 'WhatsApp Light', 'background color name', 'jetpack' ), - slug: 'whatsapp-light', - color: '#F4F4F4', - }, - { - name: _x( 'White', 'background color name', 'jetpack' ), - slug: 'whatsapp-white', - color: '#FFFFFF', - }, - ], - }, - ] } - > - - - - - setAttributes( { buttonText: value } ) } - withoutInteractiveFormatting - allowedFormats={ [] } - className="whatsapp-block__button" - tagName="a" - style={ { - backgroundColor: backgroundColor, - } } - /> -
- ); - } -} diff --git a/extensions/blocks/send-a-message/service-blocks/whatsapp/editor.scss b/extensions/blocks/send-a-message/service-blocks/whatsapp/editor.scss deleted file mode 100644 index 6595852dd0bf9..0000000000000 --- a/extensions/blocks/send-a-message/service-blocks/whatsapp/editor.scss +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Editor styles for Send A Message - */ - -.wp-block-jetpack-send-a-message { - input.components-text-control__input { - padding: 5px 8px; - margin-left: 3px; - } - - select.components-select-control__input { - width: 95px; - min-height: 30px; - padding-left: 10px; - } - - button.components-button.is-large { - margin-left: 7px; - } - - .components-placeholder__label svg { - margin-right: 6px; - } -} - -.whatsapp-phonenumber-invalid { - .components-popover__content { - background: #B40000; - color: white; - padding: 6px 10px; - border-radius: 6px; - border: none; - } - - &:not(.is-without-arrow):not(.is-mobile):after { - border-color: #B40000; - } - - svg.dashicon { - vertical-align: top; - margin-right: 4px; - margin-top: -1px; - } -} diff --git a/extensions/blocks/send-a-message/service-blocks/whatsapp/icon.js b/extensions/blocks/send-a-message/service-blocks/whatsapp/icon.js deleted file mode 100644 index 9eb3771376f0a..0000000000000 --- a/extensions/blocks/send-a-message/service-blocks/whatsapp/icon.js +++ /dev/null @@ -1,10 +0,0 @@ -/** - * External dependencies - */ -import { SVG, Path } from '@wordpress/components'; - -export default ( - - - -); diff --git a/extensions/blocks/send-a-message/service-blocks/whatsapp/index.js b/extensions/blocks/send-a-message/service-blocks/whatsapp/index.js deleted file mode 100644 index 7c7993352f43a..0000000000000 --- a/extensions/blocks/send-a-message/service-blocks/whatsapp/index.js +++ /dev/null @@ -1,54 +0,0 @@ -/** - * External dependencies - */ -import { __, _x } from '@wordpress/i18n'; - -/** - * Internal dependencies - */ -import attributes from './attributes'; -import edit from './edit'; -import save from './save'; -import icon from './icon'; -import { supportsCollections } from '../../../../shared/block-category'; -import './editor.scss'; - -export const name = 'whatsapp-button'; -export const title = __( 'WhatsApp Button', 'jetpack' ); -export const whatsAppURL = 'https://api.whatsapp.com/send?phone='; -export const defaultButtonText = __( 'Chat on WhatsApp', 'jetpack' ); -export const defaultFirstMessage = __( - 'Hi, I got your WhatsApp information from your website.', - 'jetpack' -); - -export const settings = { - title, - description: __( - 'Let your visitors to send you a message on WhatsApp with the tap of a button.', - 'jetpack' - ), - icon, - category: supportsCollections() ? 'grow' : 'jetpack', - parent: [ 'jetpack/send-a-message' ], - keywords: [ - _x( 'whatsapp', 'keyword', 'jetpack' ), - _x( 'messenger', 'keyword', 'jetpack' ), - _x( 'contact', 'keyword', 'jetpack' ), - _x( 'support', 'keyword', 'jetpack' ), - ], - supports: { - align: [ 'left', 'center', 'right' ], - html: false, - reusable: false, - }, - attributes, - edit, - save: save, - example: { - attributes: { - countryCode: '1', - phoneNumber: '555-123-4567', - }, - }, -}; diff --git a/extensions/blocks/send-a-message/service-blocks/whatsapp/save.js b/extensions/blocks/send-a-message/service-blocks/whatsapp/save.js deleted file mode 100644 index 5fe53058cbf02..0000000000000 --- a/extensions/blocks/send-a-message/service-blocks/whatsapp/save.js +++ /dev/null @@ -1,44 +0,0 @@ -/** - * External dependencies - */ -import { RichText } from '@wordpress/block-editor'; - -/** - * Internal dependencies - */ -import { whatsAppURL } from './index'; - -export default function SendAMessageSave( props ) { - const { - countryCode, - phoneNumber, - firstMessage, - buttonText, - backgroundColor, - colorClass, - } = props.attributes; - - const fullPhoneNumber = countryCode.replace( /\D+/g, '' ) + phoneNumber.replace( /\D+/g, '' ); - - const getWhatsAppUrl = () => { - let url = whatsAppURL + fullPhoneNumber; - - if ( '' !== firstMessage ) { - url += '&text=' + encodeURIComponent( firstMessage ); - } - - return url; - }; - - return ( -
- - - -
- ); -} diff --git a/extensions/blocks/send-a-message/service-blocks/whatsapp/view.js b/extensions/blocks/send-a-message/service-blocks/whatsapp/view.js deleted file mode 100644 index 741c50d6a0ff9..0000000000000 --- a/extensions/blocks/send-a-message/service-blocks/whatsapp/view.js +++ /dev/null @@ -1,4 +0,0 @@ -/** - * Internal dependencies - */ -import './view.scss'; diff --git a/extensions/blocks/send-a-message/service-blocks/whatsapp/view.scss b/extensions/blocks/send-a-message/service-blocks/whatsapp/view.scss deleted file mode 100644 index 5aaf2cc1942d9..0000000000000 --- a/extensions/blocks/send-a-message/service-blocks/whatsapp/view.scss +++ /dev/null @@ -1,37 +0,0 @@ -div.wp-block-jetpack-send-a-message { - a.whatsapp-block__button { - background: #25D366; - color: #fff; - display: inline-block; - padding: 8px 16px 8px 56px; - border-radius: 8px; - text-decoration: none; - - font-family: "Helvetica Neue", Helvetica, Arials, sans-serif; - font-weight: 500; - - background-image: url("data:image/svg+xml;utf8,"); - background-position: 16px center; - background-repeat: no-repeat; - background-size: 32px 32px; - } - - &.is-color-light { - a.whatsapp-block__button { - color: #465B64; - background-image: url("data:image/svg+xml;utf8,"); - } - } - - &.aligncenter { - text-align: center; - } - - &.alignright { - text-align: right; - } - - &:hover { - opacity: 0.9; - } -} diff --git a/extensions/blocks/send-a-message/variations.js b/extensions/blocks/send-a-message/variations.js index 81ac994943d2a..4db3c861f1b4d 100644 --- a/extensions/blocks/send-a-message/variations.js +++ b/extensions/blocks/send-a-message/variations.js @@ -6,7 +6,7 @@ import { __ } from '@wordpress/i18n'; /** * Internal dependencies */ -import whatsAppIcon from './service-blocks/whatsapp/icon'; +import whatsAppIcon from './whatsapp-button/icon'; const variations = [ { diff --git a/extensions/index.json b/extensions/index.json index 91c2777a82d48..8c46dd039ecdc 100644 --- a/extensions/index.json +++ b/extensions/index.json @@ -32,7 +32,7 @@ "videopress", "wordads" ], - "beta": [ "amazon", "send-a-message" ], + "beta": [ "amazon", "send-a-message", "send-a-message/whatsapp-button" ], "experimental": [ "seo" ], "no-post-editor": [ "business-hours", From 1548ec7de4d02bf2824ff4bbdd3700a4bcf27930 Mon Sep 17 00:00:00 2001 From: apeatling Date: Thu, 21 May 2020 14:42:30 -0700 Subject: [PATCH 04/32] [not verified] Add WhatsApp button innerblock. --- extensions/blocks/send-a-message/view.js | 4 + extensions/blocks/send-a-message/view.scss | 0 .../whatsapp-button/attributes.js | 31 +++ .../send-a-message/whatsapp-button/edit.js | 251 ++++++++++++++++++ .../whatsapp-button/editor.scss | 44 +++ .../send-a-message/whatsapp-button/icon.js | 10 + .../send-a-message/whatsapp-button/index.js | 54 ++++ .../send-a-message/whatsapp-button/save.js | 47 ++++ .../send-a-message/whatsapp-button/view.js | 4 + .../send-a-message/whatsapp-button/view.scss | 37 +++ .../whatsapp-button/whatsapp-button.php | 42 +++ 11 files changed, 524 insertions(+) create mode 100644 extensions/blocks/send-a-message/view.js create mode 100644 extensions/blocks/send-a-message/view.scss create mode 100644 extensions/blocks/send-a-message/whatsapp-button/attributes.js create mode 100644 extensions/blocks/send-a-message/whatsapp-button/edit.js create mode 100644 extensions/blocks/send-a-message/whatsapp-button/editor.scss create mode 100644 extensions/blocks/send-a-message/whatsapp-button/icon.js create mode 100644 extensions/blocks/send-a-message/whatsapp-button/index.js create mode 100644 extensions/blocks/send-a-message/whatsapp-button/save.js create mode 100644 extensions/blocks/send-a-message/whatsapp-button/view.js create mode 100644 extensions/blocks/send-a-message/whatsapp-button/view.scss create mode 100644 extensions/blocks/send-a-message/whatsapp-button/whatsapp-button.php diff --git a/extensions/blocks/send-a-message/view.js b/extensions/blocks/send-a-message/view.js new file mode 100644 index 0000000000000..741c50d6a0ff9 --- /dev/null +++ b/extensions/blocks/send-a-message/view.js @@ -0,0 +1,4 @@ +/** + * Internal dependencies + */ +import './view.scss'; diff --git a/extensions/blocks/send-a-message/view.scss b/extensions/blocks/send-a-message/view.scss new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/extensions/blocks/send-a-message/whatsapp-button/attributes.js b/extensions/blocks/send-a-message/whatsapp-button/attributes.js new file mode 100644 index 0000000000000..7c2a4657ff7eb --- /dev/null +++ b/extensions/blocks/send-a-message/whatsapp-button/attributes.js @@ -0,0 +1,31 @@ +/** + * External dependencies + */ +import { __ } from '@wordpress/i18n'; + +export default { + countryCode: { + type: 'string', + }, + phoneNumber: { + type: 'string', + }, + firstMessage: { + type: 'string', + default: __( 'Hi, I got your WhatsApp information from your website.', 'jetpack' ), + }, + buttonText: { + type: 'array', + source: 'children', + selector: 'a.whatsapp-block__button', + default: __( 'Chat on WhatsApp', 'jetpack' ), + }, + backgroundColor: { + type: 'string', + default: '#25D366', + }, + colorClass: { + type: 'string', + default: 'dark', + }, +}; diff --git a/extensions/blocks/send-a-message/whatsapp-button/edit.js b/extensions/blocks/send-a-message/whatsapp-button/edit.js new file mode 100644 index 0000000000000..2ca276a434b46 --- /dev/null +++ b/extensions/blocks/send-a-message/whatsapp-button/edit.js @@ -0,0 +1,251 @@ +/** + * External dependencies + */ + +import { __, _x } from '@wordpress/i18n'; +import { useEffect, useState } from '@wordpress/element'; +import { + Button, + Placeholder, + BaseControl, + TextControl, + TextareaControl, + SelectControl, + Toolbar, + Popover, + Icon, + PanelBody, + PanelRow, +} from '@wordpress/components'; +import { + BlockControls, + InspectorControls, + RichText, + PanelColorSettings, +} from '@wordpress/block-editor'; + +/** + * Internal dependencies + */ + +import { countryCodes } from '../shared/countrycodes.js'; +import './view.scss'; + +export default function WhatsAppButtonEdit( { attributes, setAttributes, className } ) { + const { + countryCode, + phoneNumber, + buttonText, + firstMessage, + colorClass, + backgroundColor, + } = attributes; + + const [ isValidPhoneNumber, setIsValidPhoneNumber ] = useState( false ); + + const getCountryCode = async () => { + if ( undefined !== countryCode ) { + return; + } + + setAttributes( { countryCode: '1' } ); + + const geoFetch = await fetch( 'http://ip-api.com/json/?fields=countryCode' ) + .then( response => { + if ( ! response.ok ) { + return false; + } + + return response; + } ) + .catch( () => { + return false; + } ); + + if ( geoFetch ) { + const geo = await geoFetch.json(); + + countryCodes.forEach( item => { + if ( item.code === geo.countryCode ) { + setAttributes( { countryCode: item.value } ); + } + } ); + } + }; + + useEffect( () => { + getCountryCode(); + } ); + + const validatePhoneNumber = newPhoneNumber => { + const phoneNumberRegEx = RegExp( /^[+]?[\s./0-9]*[(]?[0-9]{1,4}[)]?[-\s./0-9]*$/, 'g' ); + + if ( undefined === newPhoneNumber || newPhoneNumber.length < 1 ) { + return false; + } + + return phoneNumberRegEx.test( countryCode.replace( /\D/g, '' ) + newPhoneNumber ); + }; + + const onBlurPhoneNumber = e => { + setIsValidPhoneNumber( validatePhoneNumber( '' ) ); + }; + + const toolbarControls = [ + { + icon: 'edit', + title: __( 'Edit WhatsApp phone number', 'jetpack' ), + onClick: () => this.setState( { editing: true } ), + }, + ]; + + const setBackgroundColor = color => { + setAttributes( { backgroundColor: color } ); + + if ( color === undefined || color === '#25D366' || color === '#465B64' ) { + return setAttributes( { colorClass: 'dark' } ); + } + + setAttributes( { colorClass: 'light' } ); + }; + + return ( +
+ + + + + + + + setAttributes( { countryCode: value } ) } + options={ countryCodes } + /> + onBlurPhoneNumber( newPhoneNumber ) } + value={ phoneNumber } + /> + + + setAttributes( { firstMessage: text } ) } + /> + + + setBackgroundColor( color ), + label: __( 'Background Color', 'jetpack' ), + disableCustomColors: true, + colors: [ + { + name: _x( 'WhatsApp Green', 'background color name', 'jetpack' ), + slug: 'whatsapp-green', + color: '#25D366', + }, + { + name: _x( 'WhatsApp Dark', 'background color name', 'jetpack' ), + slug: 'whatsapp-dark', + color: '#465B64', + }, + { + name: _x( 'WhatsApp Light', 'background color name', 'jetpack' ), + slug: 'whatsapp-light', + color: '#F4F4F4', + }, + { + name: _x( 'White', 'background color name', 'jetpack' ), + slug: 'whatsapp-white', + color: '#FFFFFF', + }, + ], + }, + ] } + > + + + setAttributes( { buttonText: value } ) } + withoutInteractiveFormatting + allowedFormats={ [] } + className="whatsapp-block__button" + tagName="a" + style={ { + backgroundColor: backgroundColor, + } } + /> +
+ ); +} + +// export default class SendAMessageEdit extends Component { +// render() { +// const { +// countryCode, +// phoneNumber, +// buttonText, +// firstMessage, +// colorClass, +// backgroundColor, +// } = this.props.attributes; + +// const { setAttributes, className } = this.props; + +// if ( this.state.editing ) { +// return ( +// +//
+// setAttributes( { countryCode: value } ) } +// options={ countryCodes } +// /> +// setAttributes( { phoneNumber: value } ) } +// onFocus={ onFocusPhoneNumber } +// value={ phoneNumber } +// /> +// { ! this.state.isValidPhoneNumber && ( +// +// +// { __( 'Please enter a valid phone number', 'jetpack' ) } +// +// ) } +// +// +//
+// ); +// } + +// } +// } diff --git a/extensions/blocks/send-a-message/whatsapp-button/editor.scss b/extensions/blocks/send-a-message/whatsapp-button/editor.scss new file mode 100644 index 0000000000000..6595852dd0bf9 --- /dev/null +++ b/extensions/blocks/send-a-message/whatsapp-button/editor.scss @@ -0,0 +1,44 @@ +/** + * Editor styles for Send A Message + */ + +.wp-block-jetpack-send-a-message { + input.components-text-control__input { + padding: 5px 8px; + margin-left: 3px; + } + + select.components-select-control__input { + width: 95px; + min-height: 30px; + padding-left: 10px; + } + + button.components-button.is-large { + margin-left: 7px; + } + + .components-placeholder__label svg { + margin-right: 6px; + } +} + +.whatsapp-phonenumber-invalid { + .components-popover__content { + background: #B40000; + color: white; + padding: 6px 10px; + border-radius: 6px; + border: none; + } + + &:not(.is-without-arrow):not(.is-mobile):after { + border-color: #B40000; + } + + svg.dashicon { + vertical-align: top; + margin-right: 4px; + margin-top: -1px; + } +} diff --git a/extensions/blocks/send-a-message/whatsapp-button/icon.js b/extensions/blocks/send-a-message/whatsapp-button/icon.js new file mode 100644 index 0000000000000..9eb3771376f0a --- /dev/null +++ b/extensions/blocks/send-a-message/whatsapp-button/icon.js @@ -0,0 +1,10 @@ +/** + * External dependencies + */ +import { SVG, Path } from '@wordpress/components'; + +export default ( + + + +); diff --git a/extensions/blocks/send-a-message/whatsapp-button/index.js b/extensions/blocks/send-a-message/whatsapp-button/index.js new file mode 100644 index 0000000000000..2205e3c0682d1 --- /dev/null +++ b/extensions/blocks/send-a-message/whatsapp-button/index.js @@ -0,0 +1,54 @@ +/** + * External dependencies + */ +import { __, _x } from '@wordpress/i18n'; + +/** + * Internal dependencies + */ +import attributes from './attributes'; +import edit from './edit'; +import save from './save'; +import icon from './icon'; +import { supportsCollections } from '../../../shared/block-category'; +import './editor.scss'; + +export const name = 'whatsapp-button'; +export const title = __( 'WhatsApp Button', 'jetpack' ); +export const whatsAppURL = 'https://api.whatsapp.com/send?phone='; +export const defaultButtonText = __( 'Chat on WhatsApp', 'jetpack' ); +export const defaultFirstMessage = __( + 'Hi, I got your WhatsApp information from your website.', + 'jetpack' +); + +export const settings = { + title, + description: __( + 'Let your visitors to send you a message on WhatsApp with the tap of a button.', + 'jetpack' + ), + icon, + category: supportsCollections() ? 'grow' : 'jetpack', + parent: [ 'jetpack/send-a-message' ], + keywords: [ + _x( 'whatsapp', 'keyword', 'jetpack' ), + _x( 'messenger', 'keyword', 'jetpack' ), + _x( 'contact', 'keyword', 'jetpack' ), + _x( 'support', 'keyword', 'jetpack' ), + ], + supports: { + align: [ 'left', 'center', 'right' ], + html: false, + reusable: false, + }, + attributes, + edit, + save: save, + example: { + attributes: { + countryCode: '1', + phoneNumber: '555-123-4567', + }, + }, +}; diff --git a/extensions/blocks/send-a-message/whatsapp-button/save.js b/extensions/blocks/send-a-message/whatsapp-button/save.js new file mode 100644 index 0000000000000..132912677bbac --- /dev/null +++ b/extensions/blocks/send-a-message/whatsapp-button/save.js @@ -0,0 +1,47 @@ +/** + * External dependencies + */ +import { RichText } from '@wordpress/block-editor'; + +/** + * Internal dependencies + */ +import { whatsAppURL } from './index'; + +export default function SendAMessageSave( { attributes, className } ) { + const { + countryCode, + phoneNumber, + firstMessage, + buttonText, + backgroundColor, + colorClass, + } = attributes; + + const fullPhoneNumber = + countryCode && phoneNumber + ? countryCode.replace( /\D+/g, '' ) + phoneNumber.replace( /\D+/g, '' ) + : ''; + + const getWhatsAppUrl = () => { + let url = whatsAppURL + fullPhoneNumber; + + if ( '' !== firstMessage ) { + url += '&text=' + encodeURIComponent( firstMessage ); + } + + return url; + }; + + return ( +
+ + + +
+ ); +} diff --git a/extensions/blocks/send-a-message/whatsapp-button/view.js b/extensions/blocks/send-a-message/whatsapp-button/view.js new file mode 100644 index 0000000000000..741c50d6a0ff9 --- /dev/null +++ b/extensions/blocks/send-a-message/whatsapp-button/view.js @@ -0,0 +1,4 @@ +/** + * Internal dependencies + */ +import './view.scss'; diff --git a/extensions/blocks/send-a-message/whatsapp-button/view.scss b/extensions/blocks/send-a-message/whatsapp-button/view.scss new file mode 100644 index 0000000000000..c1d2cb68de336 --- /dev/null +++ b/extensions/blocks/send-a-message/whatsapp-button/view.scss @@ -0,0 +1,37 @@ +div.wp-block-jetpack-whatsapp-button { + a.whatsapp-block__button { + background: #25D366; + color: #fff; + display: inline-block; + padding: 8px 16px 8px 56px; + border-radius: 8px; + text-decoration: none; + + font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; + font-weight: 500; + + background-image: url("data:image/svg+xml;utf8,"); + background-position: 16px center; + background-repeat: no-repeat; + background-size: 32px 32px; + } + + &.is-color-light { + a.whatsapp-block__button { + color: #465B64; + background-image: url("data:image/svg+xml;utf8,"); + } + } + + &.aligncenter { + text-align: center; + } + + &.alignright { + text-align: right; + } + + &:hover { + opacity: 0.9; + } +} diff --git a/extensions/blocks/send-a-message/whatsapp-button/whatsapp-button.php b/extensions/blocks/send-a-message/whatsapp-button/whatsapp-button.php new file mode 100644 index 0000000000000..c594cee8c8649 --- /dev/null +++ b/extensions/blocks/send-a-message/whatsapp-button/whatsapp-button.php @@ -0,0 +1,42 @@ + __NAMESPACE__ . '\render_block' ) + ); +} +add_action( 'init', __NAMESPACE__ . '\register_block' ); + +/** + * Render callback. + * + * @param array $attributes Array containing the block attributes. + * @param string $content String containing the block content. + * + * @return string + */ +function render_block( $attributes, $content ) { + Jetpack_Gutenberg::load_styles_as_required( PARENT_NAME . '/' . FEATURE_NAME ); + + return $content; +} From 7b98d9beb64f15935f7c34c81323719457275232 Mon Sep 17 00:00:00 2001 From: apeatling Date: Thu, 21 May 2020 14:53:35 -0700 Subject: [PATCH 05/32] [not verified] Remove commented out old class component from WhatsApp block edit. --- .../send-a-message/whatsapp-button/edit.js | 50 ------------------- 1 file changed, 50 deletions(-) diff --git a/extensions/blocks/send-a-message/whatsapp-button/edit.js b/extensions/blocks/send-a-message/whatsapp-button/edit.js index 2ca276a434b46..944ab1404946c 100644 --- a/extensions/blocks/send-a-message/whatsapp-button/edit.js +++ b/extensions/blocks/send-a-message/whatsapp-button/edit.js @@ -199,53 +199,3 @@ export default function WhatsAppButtonEdit( { attributes, setAttributes, classNa ); } - -// export default class SendAMessageEdit extends Component { -// render() { -// const { -// countryCode, -// phoneNumber, -// buttonText, -// firstMessage, -// colorClass, -// backgroundColor, -// } = this.props.attributes; - -// const { setAttributes, className } = this.props; - -// if ( this.state.editing ) { -// return ( -// -//
-// setAttributes( { countryCode: value } ) } -// options={ countryCodes } -// /> -// setAttributes( { phoneNumber: value } ) } -// onFocus={ onFocusPhoneNumber } -// value={ phoneNumber } -// /> -// { ! this.state.isValidPhoneNumber && ( -// -// -// { __( 'Please enter a valid phone number', 'jetpack' ) } -// -// ) } -// -// -//
-// ); -// } - -// } -// } From 7b18026dc122d146b2816b1fe01059646ab01082 Mon Sep 17 00:00:00 2001 From: apeatling Date: Fri, 22 May 2020 11:33:47 -0700 Subject: [PATCH 06/32] Move phone number and message settings to the block toolbar as well as the sidebar. --- extensions/blocks/send-a-message/index.js | 3 +- .../send-a-message/whatsapp-button/edit.js | 116 +++++++++++------- .../whatsapp-button/editor.scss | 15 ++- 3 files changed, 82 insertions(+), 52 deletions(-) diff --git a/extensions/blocks/send-a-message/index.js b/extensions/blocks/send-a-message/index.js index 91720c4cf8939..b0715283c95a9 100644 --- a/extensions/blocks/send-a-message/index.js +++ b/extensions/blocks/send-a-message/index.js @@ -11,6 +11,7 @@ import { Path } from '@wordpress/components'; import edit from './edit'; import variations from './variations'; import renderMaterialIcon from '../../shared/render-material-icon'; +import { supportsCollections } from '../../shared/block-category'; /** * Style dependencies @@ -26,7 +27,7 @@ export const settings = { icon: renderMaterialIcon( ), - category: 'jetpack', + category: supportsCollections() ? 'grow' : 'jetpack', keywords: [ _x( 'whatsapp', 'keyword', 'jetpack' ), _x( 'messenger', 'keyword', 'jetpack' ), diff --git a/extensions/blocks/send-a-message/whatsapp-button/edit.js b/extensions/blocks/send-a-message/whatsapp-button/edit.js index 944ab1404946c..b0b10bec1212f 100644 --- a/extensions/blocks/send-a-message/whatsapp-button/edit.js +++ b/extensions/blocks/send-a-message/whatsapp-button/edit.js @@ -6,16 +6,14 @@ import { __, _x } from '@wordpress/i18n'; import { useEffect, useState } from '@wordpress/element'; import { Button, - Placeholder, BaseControl, TextControl, TextareaControl, SelectControl, - Toolbar, - Popover, Icon, PanelBody, - PanelRow, + ToolbarGroup, + Dropdown, } from '@wordpress/components'; import { BlockControls, @@ -23,6 +21,7 @@ import { RichText, PanelColorSettings, } from '@wordpress/block-editor'; +import { DOWN } from '@wordpress/keycodes'; /** * Internal dependencies @@ -91,14 +90,6 @@ export default function WhatsAppButtonEdit( { attributes, setAttributes, classNa setIsValidPhoneNumber( validatePhoneNumber( '' ) ); }; - const toolbarControls = [ - { - icon: 'edit', - title: __( 'Edit WhatsApp phone number', 'jetpack' ), - onClick: () => this.setState( { editing: true } ), - }, - ]; - const setBackgroundColor = color => { setAttributes( { backgroundColor: color } ); @@ -109,42 +100,81 @@ export default function WhatsAppButtonEdit( { attributes, setAttributes, classNa setAttributes( { colorClass: 'light' } ); }; + const renderSettingsToggle = ( isOpen, onToggle ) => { + const openOnArrowDown = event => { + if ( ! isOpen && event.keyCode === DOWN ) { + event.preventDefault(); + event.stopPropagation(); + onToggle(); + } + }; + + return ( +