Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling Unsupported Block Editor for Jetpack sites with SSO enabled. #12606

Merged
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
ac3e043
Enabling Unsupported Block Editor for Jetpack sites with SSO enabled.
marecar3 Aug 5, 2020
48f9ecc
Fix lint error
marecar3 Aug 5, 2020
07e3c59
Fix lint errors
marecar3 Aug 6, 2020
705e574
Send update capabilities events from Android native to JS
marecar3 Aug 8, 2020
e8c87ba
Merge branch 'develop' into gutenberg/unsupported-block-editor-jetpac…
marecar3 Aug 8, 2020
66fa9f9
Small fix
marecar3 Aug 8, 2020
b87dffd
Remove empty lines
marecar3 Aug 8, 2020
cdf8899
Fix lint errors
marecar3 Aug 8, 2020
8678b8d
Update gb-mobile reference
marecar3 Aug 8, 2020
ee3a0e7
Update gb-mobile reference
marecar3 Aug 8, 2020
b227a08
Update gb-mobile reference
marecar3 Aug 11, 2020
b356af7
Merge
marecar3 Aug 11, 2020
9fa7b6e
Revert "Merge"
marecar3 Aug 11, 2020
0c9b6ec
Update gb-mobile reference
marecar3 Aug 11, 2020
b846c13
update gb-mobile reference
marecar3 Aug 11, 2020
7bab8c1
Merge branch 'develop' into gutenberg/unsupported-block-editor-jetpac…
marecar3 Aug 13, 2020
5c95d1f
Merge
marecar3 Aug 14, 2020
eb57550
Merge branch 'develop' into gutenberg/unsupported-block-editor-jetpac…
marecar3 Aug 17, 2020
a970616
Update gb-mobile submodule
marecar3 Aug 18, 2020
8161844
Update gb-mobile submodule
marecar3 Aug 18, 2020
75fd10c
Merge branch 'develop' into gutenberg/unsupported-block-editor-jetpac…
marecar3 Aug 19, 2020
d3e1826
Update gb-mobile reference
marecar3 Aug 19, 2020
a90d430
Update gb-mobile ref
marecar3 Aug 19, 2020
1ceca41
Merge branch 'develop' into gutenberg/unsupported-block-editor-jetpac…
marecar3 Aug 20, 2020
c511db9
Revert stories-android submodule ref change
marecar3 Aug 20, 2020
5f64960
Implement jetpackSsoEnabled
marecar3 Aug 24, 2020
9df0804
Merge
marecar3 Aug 24, 2020
ed1155f
Revert "Merge"
marecar3 Aug 24, 2020
fb71f66
Merge branch 'develop' into gutenberg/unsupported-block-editor-jetpac…
marecar3 Aug 25, 2020
ef326a0
Revert stories-android ref to develop version
marecar3 Aug 25, 2020
2e3400e
Refresh capabilities after Save action
marecar3 Aug 25, 2020
a238a1c
Pass userId to WebView logic
marecar3 Aug 25, 2020
b73e4ef
fix lint errors
marecar3 Aug 25, 2020
5c359ca
Move jetpack/sso logic to WPAndroid
marecar3 Aug 26, 2020
246e73d
exclude WpCom atomic sites
marecar3 Aug 27, 2020
4598b61
Remove unused line of code
marecar3 Sep 1, 2020
251f348
Update gb mobile reference
marecar3 Sep 2, 2020
5755582
Merge branch 'develop' into gutenberg/unsupported-block-editor-jetpac…
marecar3 Sep 4, 2020
254d962
Update gb-mobile ref
marecar3 Sep 4, 2020
96672fc
Merge branch 'develop' into gutenberg/unsupported-block-editor-jetpac…
marecar3 Sep 4, 2020
a32721b
Update gb-mobile reference
marecar3 Sep 9, 2020
4de9f08
Merge branch 'develop' into gutenberg/unsupported-block-editor-jetpac…
marecar3 Sep 9, 2020
7c4de6b
Merge branch 'develop' into gutenberg/unsupported-block-editor-jetpac…
marecar3 Sep 10, 2020
020c882
Update gb-mobile reference
marecar3 Sep 10, 2020
9269fd5
Rename isSitePrivate to isSelfHosted
marecar3 Sep 10, 2020
a63df79
Check in one place if arguments are not null
marecar3 Sep 10, 2020
86206be
Update gb-mobile reference
marecar3 Sep 11, 2020
6c6e037
Merge branch 'develop' into gutenberg/unsupported-block-editor-jetpac…
marecar3 Sep 11, 2020
9128215
Update gb-mobile reference to point to develop
marecar3 Sep 12, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.wordpress.android.editor.ImageSettingsDialogFragment;
import org.wordpress.android.editor.gutenberg.GutenbergEditorFragment;
import org.wordpress.android.editor.gutenberg.GutenbergPropsBuilder;
import org.wordpress.android.editor.gutenberg.GutenbergWebViewAuthorizationData;
import org.wordpress.android.fluxc.Dispatcher;
import org.wordpress.android.fluxc.action.AccountAction;
import org.wordpress.android.fluxc.generated.AccountActionBuilder;
Expand Down Expand Up @@ -152,6 +153,7 @@
import org.wordpress.android.ui.posts.services.AztecImageLoader;
import org.wordpress.android.ui.posts.services.AztecVideoLoader;
import org.wordpress.android.ui.prefs.AppPrefs;
import org.wordpress.android.ui.prefs.SiteSettingsInterface;
import org.wordpress.android.ui.reader.utils.ReaderUtilsWrapper;
import org.wordpress.android.ui.stockmedia.StockMediaPickerActivity;
import org.wordpress.android.ui.uploads.PostEvents;
Expand Down Expand Up @@ -239,7 +241,8 @@ public class EditPostActivity extends LocaleAwareActivity implements
EditPostSettingsCallback,
PrepublishingBottomSheetListener,
PrivateAtCookieProgressDialogOnDismissListener,
ExceptionLogger {
ExceptionLogger,
SiteSettingsInterface.SiteSettingsListener {
public static final String ACTION_REBLOG = "reblogAction";
public static final String EXTRA_POST_LOCAL_ID = "postModelLocalId";
public static final String EXTRA_LOAD_AUTO_SAVE_REVISION = "loadAutosaveRevision";
Expand Down Expand Up @@ -373,6 +376,8 @@ enum RestartEditorOptions {
private StorePostViewModel mViewModel;

private SiteModel mSite;
private SiteSettingsInterface mSiteSettings;
private boolean mIsJetpackSsoEnabled;

public static boolean checkToRestart(@NonNull Intent data) {
return data.hasExtra(EditPostActivity.EXTRA_RESTART_EDITOR)
Expand Down Expand Up @@ -435,6 +440,10 @@ protected void onCreate(Bundle savedInstanceState) {
if (refreshedSite != null) {
mSite.setMobileEditor(refreshedSite.getMobileEditor());
}

mSiteSettings = SiteSettingsInterface.getInterface(this, mSite, this);
// initialize settings with locally cached values, fetch remote on first pass
mSiteSettings.init(true);
}

// Check whether to show the visual editor
Expand Down Expand Up @@ -635,6 +644,32 @@ public void onCookieProgressDialogCancelled() {
setupViewPager();
}

// SiteSettingsListener
@Override
public void onSaveError(Exception error) { }

@Override
public void onFetchError(Exception error) { }

@Override
public void onSettingsUpdated() {
// Let's hold the value in local variable as listener is too noisy
boolean isJetpackSsoEnabled = mSite.isJetpackConnected() && mSiteSettings.isJetpackSsoEnabled();
if (mIsJetpackSsoEnabled != isJetpackSsoEnabled) {
mIsJetpackSsoEnabled = isJetpackSsoEnabled;
if (mEditorFragment instanceof GutenbergEditorFragment) {
((GutenbergEditorFragment) mEditorFragment)
.updateCapabilities(mIsJetpackSsoEnabled, getGutenbergPropsBuilder());
}
}
}

@Override
public void onSettingsSaved() { }

@Override
public void onCredentialsValidated(Exception error) { }

private void setupViewPager() {
// Set up the ViewPager with the sections adapter.
mViewPager = findViewById(R.id.pager);
Expand Down Expand Up @@ -1970,45 +2005,30 @@ public Fragment getItem(int position) {
// Enable gutenberg on the site & show the informative popup upon opening
// the GB editor the first time when the remote setting value is still null
setGutenbergEnabledIfNeeded();
String postType = mIsPage ? "page" : "post";
String languageString = LocaleManager.getLanguage(EditPostActivity.this);
String wpcomLocaleSlug = languageString.replace("_", "-").toLowerCase(Locale.ENGLISH);
boolean isWpCom = getSite().isWPCom() || mSite.isPrivateWPComAtomic() || mSite.isWPComAtomic();

EditorTheme editorTheme = mEditorThemeStore.getEditorThemeForSite(mSite);
Bundle themeBundle = (editorTheme != null) ? editorTheme.getThemeSupport().toBundle() : null;

// The Unsupported Block Editor is disabled for all self-hosted sites
// even the one that are connected via Jetpack to a WP.com account.
// The option is disabled on Self-hosted sites because they can have their web editor
// to be set to classic and then the fallback will not work.
// We disable in Jetpack site because we don't have the self-hosted site's credentials
// which are required for us to be able to fetch the site's authentication cookie.
boolean isUnsupportedBlockEditorEnabled = isWpCom && "gutenberg".equals(mSite.getWebEditor());

boolean isSiteUsingWpComRestApi = mSite.isUsingWpComRestApi();
boolean enableMentions = isSiteUsingWpComRestApi && mGutenbergMentionsFeatureConfig.isEnabled();
GutenbergPropsBuilder gutenbergPropsBuilder = new GutenbergPropsBuilder(
enableMentions,
isUnsupportedBlockEditorEnabled,
mModalLayoutPickerFeatureConfig.isEnabled(),
wpcomLocaleSlug,
postType,
themeBundle
);
boolean isWpCom = getSite().isWPCom() || mSite.isPrivateWPComAtomic() || mSite.isWPComAtomic();
GutenbergPropsBuilder gutenbergPropsBuilder = getGutenbergPropsBuilder();

GutenbergWebViewAuthorizationData gutenbergWebViewAuthorizationData =
new GutenbergWebViewAuthorizationData(
mSite.getUrl(),
isWpCom,
mAccountStore.getAccount().getUserId(),
mAccountStore.getAccount().getUserName(),
mAccountStore.getAccessToken(),
mSite.getSelfHostedSiteId(),
mSite.getUsername(),
mSite.getPassword(),
mSite.isUsingWpComRestApi(),
mSite.getWebEditor(),
WordPress.getUserAgent(),
mIsJetpackSsoEnabled);

return GutenbergEditorFragment.newInstance(
"",
"",
mIsNewPost,
mSite.getUrl(),
!isWpCom,
isWpCom ? mAccountStore.getAccount().getUserId() : mSite.getSelfHostedSiteId(),
isWpCom ? mAccountStore.getAccount().getUserName() : mSite.getUsername(),
isWpCom ? "" : mSite.getPassword(),
mAccountStore.getAccessToken(),
isSiteUsingWpComRestApi,
WordPress.getUserAgent(),
gutenbergWebViewAuthorizationData,
mTenorFeatureConfig.isEnabled(),
gutenbergPropsBuilder
);
Expand Down Expand Up @@ -2061,6 +2081,30 @@ public int getCount() {
}
}

private GutenbergPropsBuilder getGutenbergPropsBuilder() {
String postType = mIsPage ? "page" : "post";
String languageString = LocaleManager.getLanguage(EditPostActivity.this);
String wpcomLocaleSlug = languageString.replace("_", "-").toLowerCase(Locale.ENGLISH);

boolean isSiteUsingWpComRestApi = mSite.isUsingWpComRestApi();
boolean enableMentions = isSiteUsingWpComRestApi && mGutenbergMentionsFeatureConfig.isEnabled();

EditorTheme editorTheme = mEditorThemeStore.getEditorThemeForSite(mSite);
Bundle themeBundle = (editorTheme != null) ? editorTheme.getThemeSupport().toBundle() : null;

boolean isUnsupportedBlockEditorEnabled = (mSite.isWPCom() || mIsJetpackSsoEnabled)
&& "gutenberg".equals(mSite.getWebEditor());

return new GutenbergPropsBuilder(
enableMentions,
isUnsupportedBlockEditorEnabled,
mModalLayoutPickerFeatureConfig.isEnabled(),
wpcomLocaleSlug,
postType,
themeBundle
);
}

// Moved from EditPostContentFragment
public static final String NEW_MEDIA_POST = "NEW_MEDIA_POST";
public static final String NEW_MEDIA_POST_EXTRA_IDS = "NEW_MEDIA_POST_EXTRA_IDS";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;

import org.wordpress.android.editor.gutenberg.GutenbergWebViewAuthorizationData;
import org.wordpress.android.util.AppLog;
import org.wordpress.android.util.StringUtils;
import org.wordpress.mobile.ReactNativeGutenbergBridge.GutenbergWebViewActivity;
Expand All @@ -15,37 +18,52 @@ public class WPGutenbergWebViewActivity extends GutenbergWebViewActivity {
public static final String ENCODING_UTF8 = "UTF-8";
public static final String WPCOM_LOGIN_URL = "https://wordpress.com/wp-login.php";

public static final String ARG_USER_ID = "authenticated_user_id";
public static final String ARG_AUTHENTICATION_USER = "authenticated_user";
public static final String ARG_AUTHENTICATION_PASSWD = "authenticated_passwd";
public static final String ARG_AUTHENTICATION_TOKEN = "authenticated_token";
public static final String ARG_URL_TO_LOAD = "url_to_load";
public static final String ARG_IS_SITE_PRIVATE = "is_site_private";
public static final String ARG_USER_AGENT = "user_agent";
public static final String ARG_GUTENBERG_WEB_VIEW_AUTH_DATA = "param_gutenberg_web_view_auth_data";

public static final String ARG_BLOCK_ID = "block_id";
public static final String ARG_BLOCK_NAME = "block_name";
public static final String ARG_BLOCK_CONTENT = "block_content";

private boolean mIsJetpackSsoEnabled;
private boolean mIsJetpackSsoRedirected;
private String mUrlToLoad;
private long mUserId;

@Override
protected void loadUrl() {
if (getIntent() != null
&& getIntent().getExtras() != null) {
Bundle bundle = getIntent().getExtras();

String siteUrl = bundle.getString(ARG_URL_TO_LOAD);
GutenbergWebViewAuthorizationData gutenbergWebViewAuthorizationData =
bundle.getParcelable(ARG_GUTENBERG_WEB_VIEW_AUTH_DATA);

String siteUrl = gutenbergWebViewAuthorizationData.getSiteUrl();
String urlToLoad = siteUrl + "/wp-admin/post-new.php";
String username = bundle.getString(ARG_AUTHENTICATION_USER);
String password = bundle.getString(ARG_AUTHENTICATION_PASSWD);
String token = bundle.getString(ARG_AUTHENTICATION_TOKEN);
boolean isSitePrivate = bundle.getBoolean(ARG_IS_SITE_PRIVATE, false);

boolean isSitePrivate = !gutenbergWebViewAuthorizationData.isWPCom();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is isSitePrivate an accurate name for this variable? Would something like isSelfHosted or !isWpCom be more precise?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing this one. You are right. isSitePrivate isn't an accurate name.
Changed to isSelfHosted .

String authenticationUrl = isSitePrivate ? siteUrl + "/wp-login.php" : WPCOM_LOGIN_URL;
String userAgent = bundle.getString(ARG_USER_AGENT);
String userAgent = gutenbergWebViewAuthorizationData.getWordPressUserAgent();

// Request to login.php needs to carry the “User-Agent” along with the headers.
// If this is not the case, this request might be blocked by the backend.
mWebView.getSettings().setUserAgentString(userAgent);

String username = gutenbergWebViewAuthorizationData.getWPComAccountUsername();
String password = gutenbergWebViewAuthorizationData.getWPComAccountPassword();
String token = gutenbergWebViewAuthorizationData.getWPComAccountToken();

mUserId = isSitePrivate ? gutenbergWebViewAuthorizationData.getSelfHostedSiteId()
: gutenbergWebViewAuthorizationData.getWPComAccountUserId();

if (isSitePrivate && gutenbergWebViewAuthorizationData.isJetpackSsoEnabled()) {
mIsJetpackSsoEnabled = true;
mUrlToLoad = urlToLoad;
mUserId = gutenbergWebViewAuthorizationData.getWPComAccountUserId();
loadAuthenticatedUrl(WPCOM_LOGIN_URL, "", username, password, token);
return;
}

loadAuthenticatedUrl(authenticationUrl, urlToLoad, username, password, token);
}
}
Expand All @@ -72,7 +90,7 @@ protected void saveContent(String content) {
/**
* Login to the WordPress.com and load the specified URL.
*/
protected void loadAuthenticatedUrl(String authenticationURL,
private void loadAuthenticatedUrl(String authenticationURL,
String urlToLoad,
String username,
String password,
Expand All @@ -83,7 +101,7 @@ protected void loadAuthenticatedUrl(String authenticationURL,
mWebView.postUrl(authenticationURL, postData.getBytes());
}

public static String getAuthenticationPostData(String authenticationUrl,
private String getAuthenticationPostData(String authenticationUrl,
String urlToLoad,
String username,
String password,
Expand All @@ -94,12 +112,16 @@ public static String getAuthenticationPostData(String authenticationUrl,

try {
String postData = String.format(
"log=%s&pwd=%s&redirect_to=%s",
"log=%s&pwd=%s",
URLEncoder.encode(StringUtils.notNullStr(username), ENCODING_UTF8),
URLEncoder.encode(StringUtils.notNullStr(password), ENCODING_UTF8),
URLEncoder.encode(StringUtils.notNullStr(urlToLoad), ENCODING_UTF8)
URLEncoder.encode(StringUtils.notNullStr(password), ENCODING_UTF8)
);

// Add url to load if needed
if (!TextUtils.isEmpty(urlToLoad)) {
postData += "&redirect_to=" + URLEncoder.encode(urlToLoad, ENCODING_UTF8);
}

// Add token authorization when signing in to WP.com
if (authenticationUrl.contains("wordpress.com/wp-login.php") && !TextUtils.isEmpty(token)) {
postData += "&authorization=Bearer " + URLEncoder.encode(token, ENCODING_UTF8);
Expand All @@ -112,4 +134,28 @@ public static String getAuthenticationPostData(String authenticationUrl,

return "";
}

@Override
protected boolean isUrlOverridden(WebView view, String url) {
if (mIsJetpackSsoEnabled) {
if (!mIsJetpackSsoRedirected) {
mIsJetpackSsoRedirected = true;
view.loadUrl(mUrlToLoad);
return true;
}

if (url.contains(mUrlToLoad)) {
mForegroundView.setVisibility(View.VISIBLE);
mIsRedirected = true;
}

return false;
} else {
return super.isUrlOverridden(view, url);
}
}

@Override public long getUserId() {
return mUserId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.wordpress.android.editor.ExceptionLogger;
import org.wordpress.android.editor.R;
import org.wordpress.mobile.WPAndroidGlue.AddMentionUtil;
import org.wordpress.mobile.WPAndroidGlue.GutenbergProps;
import org.wordpress.mobile.WPAndroidGlue.RequestExecutor;
import org.wordpress.mobile.WPAndroidGlue.Media;
import org.wordpress.mobile.WPAndroidGlue.WPAndroidGlueCode;
Expand Down Expand Up @@ -205,4 +206,9 @@ public void replaceUnsupportedBlock(String content, String blockId) {
public void updateTheme(Bundle editorTheme) {
mWPAndroidGlueCode.updateTheme(editorTheme);
}

public void updateCapabilities(GutenbergPropsBuilder gutenbergPropsBuilder) {
GutenbergProps gutenbergProps = gutenbergPropsBuilder.build(getActivity(), mHtmlModeEnabled);
mWPAndroidGlueCode.updateCapabilities(gutenbergProps);
}
}
Loading