From 1b2ff16d0d9f189a409fd4826d83f5a0cb717abd Mon Sep 17 00:00:00 2001 From: Miguel Lezama Date: Wed, 16 Dec 2015 17:59:20 -0300 Subject: [PATCH 1/3] Jetpack Sites: Use new list of active modules on /me/sites endpoint --- client/lib/site/jetpack.js | 97 +++++-------------- client/lib/sites-list/list.js | 2 +- client/my-sites/menus/main.jsx | 2 +- client/my-sites/plugins/controller.js | 9 -- client/my-sites/plugins/index.js | 6 +- client/my-sites/plugins/main.jsx | 4 +- client/my-sites/plugins/plugin.jsx | 4 +- .../plugins/plugins-browser/index.jsx | 2 +- .../my-sites/plugins/plugins-manage-mixin.js | 8 +- client/my-sites/site-settings/form-base.js | 2 +- .../site-settings/section-security.jsx | 2 +- .../editor-sharing/publicize-options.jsx | 2 - client/post-editor/media-modal/index.jsx | 12 +-- shared/my-sites/themes/main.jsx | 2 +- 14 files changed, 40 insertions(+), 114 deletions(-) diff --git a/client/lib/site/jetpack.js b/client/lib/site/jetpack.js index f6934cf75de37..5af39b53d8cb8 100644 --- a/client/lib/site/jetpack.js +++ b/client/lib/site/jetpack.js @@ -1,8 +1,7 @@ /** * External dependencies */ -var debug = require( 'debug' )( 'calypso:site:jetpack' ), - find = require( 'lodash/collection/find' ); +var debug = require( 'debug' )( 'calypso:site:jetpack' ); /** * Internal dependencies @@ -27,6 +26,10 @@ function JetpackSite( attributes ) { JetpackSite.prototype.updateComputedAttributes = function() { JetpackSite.super_.prototype.updateComputedAttributes.apply( this ); + if ( this.jetpack_modules ) { + this.modules = this.jetpack_modules; + delete this.jetpack_modules; + } this.hasMinimumJetpackVersion = versionCompare( this.options.jetpack_version, config( 'jetpack_min_version' ) ) >= 0; // Only sites that have the minimum Jetpack Version and siteurl matches the main_network_site can update plugins // unmapped_url is more likely to equal main_network_site because they should both be set to siteurl option @@ -46,7 +49,7 @@ JetpackSite.prototype.canManage = function() { // for versions 3.4 and higher, canManage can be determined by the state of the Manage Module if ( this.versionCompare( '3.4', '>=' ) ) { // if we haven't fetched the modules yet, we default to true - if ( this.modulesFetched ) { + if ( this.modules ) { return this.isModuleActive( 'manage' ); } return true; @@ -72,32 +75,6 @@ JetpackSite.prototype.fetchAvailableUpdates = function() { }.bind( this ) ); }; -JetpackSite.prototype.fetchModules = function() { - if ( ! this.hasMinimumJetpackVersion || this.fetchingModules ) { - return; - } - - this.fetchingModules = true; - wpcom.undocumented().jetpackModules( this.ID, function( error, data ) { - if ( error || ! data.modules ) { - debug( 'error fetching Modules data from api', error ); - return; - } - - this.fetchingModules = false; - this.modulesFetched = true; - this.set( { modules: data.modules } ); - this.emit( 'change' ); - }.bind( this ) ); -}; - -JetpackSite.prototype.getModule = function( moduleId ) { - if ( this.modulesFetched ) { - return find( this.modules, { id: moduleId } ); - } - this.fetchModules(); -}; - JetpackSite.prototype.isSecondaryNetworkSite = function() { return this.options.is_multi_site && this.options.unmapped_url !== this.options.main_network_site; @@ -109,13 +86,7 @@ JetpackSite.prototype.isMainNetworkSite = function() { }; JetpackSite.prototype.isModuleActive = function( moduleId ) { - var module = this.getModule( moduleId ); - - if ( module ) { - return module.active; - } - - return false; + return this.modules.includes( moduleId ); }; JetpackSite.prototype.verifyModulesActive = function( moduleIds, callback ) { @@ -125,12 +96,6 @@ JetpackSite.prototype.verifyModulesActive = function( moduleIds, callback ) { moduleIds = [ moduleIds ]; } - if ( ! this.modulesFetched ) { - this.fetchModules(); - this.once( 'change', this.verifyModulesActive.bind( this, moduleIds, callback ) ); - return; - } - modulesActive = moduleIds.every( function( moduleId ) { return this.isModuleActive( moduleId ); }, this ); @@ -152,8 +117,8 @@ JetpackSite.prototype.handleError = function( error, action, plugin, module ) { return; } - if ( module && module.name ) { - moduleTranslationArgs = { args: { module: module.name, site: this.domain } }; + if ( module ) { + moduleTranslationArgs = { args: { module: module, site: this.domain } }; } remoteManagementUrl = this.getRemoteManagementURL(); @@ -249,63 +214,53 @@ JetpackSite.prototype.callHome = function() { }; JetpackSite.prototype.activateModule = function( moduleId, callback ) { - var module = this.getModule( moduleId ); debug( 'activate module', moduleId ); - if ( ! module.id ) { + if ( ! moduleId ) { callback && callback( new Error( 'No id' ) ); return; } - if ( module.active ) { + if ( this.isModuleActive( moduleId ) ) { // Nothing to do callback(); return; } - this.toggleModule( module, callback ); + this.toggleModule( moduleId, callback ); }; JetpackSite.prototype.deactivateModule = function( moduleId, callback ) { - var module = this.getModule( moduleId ); debug( 'deactivate module', moduleId ); - if ( ! module.id ) { - callback && callback( new Error( 'No id' ) ); - return; - } - - if ( ! module.active ) { + if ( ! this.isModuleActive( moduleId ) ) { // Nothing to do callback(); return; } - this.toggleModule( module, callback ); + this.toggleModule( moduleId, callback ); }; -JetpackSite.prototype.toggleModule = function( module, callback ) { - var method; +JetpackSite.prototype.toggleModule = function( moduleId, callback ) { + const isActive = this.isModuleActive( moduleId ), + method = isActive ? 'jetpackModulesDeactivate' : 'jetpackModulesActivate', + prevActiveModules = Object.assign( {}, this.modules ); - if ( typeof module === 'string' ) { - module = this.getModule( module ); + if ( isActive ) { + this.modules = this.modules.filter( module => module !== moduleId ); + } else { + this.modules = [ ...this.modules, moduleId ]; } - - method = module.active ? 'jetpackModulesDeactivate' : 'jetpackModulesActivate'; - - wpcom.undocumented()[ method ]( this.ID, module.id, function( error, data ) { - debug( 'module toggled', this.URL, module.id, data ); + wpcom.undocumented()[ method ]( this.ID, moduleId, function( error, data ) { + debug( 'module toggled', this.URL, moduleId, data ); if ( error ) { debug( 'error toggling module from api', error ); + this.modules = prevActiveModules; this.emit( 'change' ); - callback && callback( error ); - return; } - - module.active = ! module.active; - this.emit( 'change' ); - callback && callback( null, data ); + callback && callback( error, data ); }.bind( this ) ); this.emit( 'change' ); diff --git a/client/lib/sites-list/list.js b/client/lib/sites-list/list.js index 5c3d2e4f8286a..ab1db855f160b 100644 --- a/client/lib/sites-list/list.js +++ b/client/lib/sites-list/list.js @@ -459,7 +459,7 @@ SitesList.prototype.getSelectedOrAllJetpackCanManage = function() { return site.jetpack && site.capabilities && site.capabilities.manage_options && - ( ! site.modules || site.canManage() ); + site.canManage(); } ); }; diff --git a/client/my-sites/menus/main.jsx b/client/my-sites/menus/main.jsx index 90f147ab8b38c..93d644471b7db 100644 --- a/client/my-sites/menus/main.jsx +++ b/client/my-sites/menus/main.jsx @@ -220,7 +220,7 @@ var Menus = React.createClass( { site = this.props.site, menu; - if ( site && site.jetpack && site.modulesFetched && ! site.canManage() ) { + if ( site && site.jetpack && ! site.canManage() ) { return this.renderJetpackManageDisabledMessage( site ); } diff --git a/client/my-sites/plugins/controller.js b/client/my-sites/plugins/controller.js index b73cf22d697a5..75255507e4006 100644 --- a/client/my-sites/plugins/controller.js +++ b/client/my-sites/plugins/controller.js @@ -182,15 +182,6 @@ controller = { renderPluginsBrowser( context, siteUrl ); }, - jetpackManageActive: function( context, next ) { - sites.getSelectedOrAll().forEach( function( site ) { - if ( site.jetpack ) { - site.fetchModules(); - } - } ); - next(); - }, - jetpackCanUpdate: function( filter, context, next ) { let redirectToPlugins = false, selectedSites = sites.getSelectedOrAllWithPlugins(); diff --git a/client/my-sites/plugins/index.js b/client/my-sites/plugins/index.js index 7cf52032fe29a..770b28b19033c 100644 --- a/client/my-sites/plugins/index.js +++ b/client/my-sites/plugins/index.js @@ -17,10 +17,10 @@ module.exports = function() { } if ( config.isEnabled( 'manage/plugins' ) ) { - page( '/plugins', controller.siteSelection, controller.navigation, pluginsController.jetpackManageActive, pluginsController.plugins.bind( null, 'all' ) ); + page( '/plugins', controller.siteSelection, controller.navigation, pluginsController.plugins.bind( null, 'all' ) ); [ 'active', 'inactive', 'updates' ].forEach( function( filter ) { - page( '/plugins/' + filter + '/:site_id?', controller.siteSelection, controller.navigation, pluginsController.jetpackManageActive, pluginsController.jetpackCanUpdate.bind( null, filter ), pluginsController.plugins.bind( null, filter ) ); + page( '/plugins/' + filter + '/:site_id?', controller.siteSelection, controller.navigation, pluginsController.jetpackCanUpdate.bind( null, filter ), pluginsController.plugins.bind( null, filter ) ); } ); - page( '/plugins/:plugin/:business_plugin?/:site_id?', controller.siteSelection, controller.navigation, pluginsController.jetpackManageActive, pluginsController.plugin ); + page( '/plugins/:plugin/:business_plugin?/:site_id?', controller.siteSelection, controller.navigation, pluginsController.plugin ); } }; diff --git a/client/my-sites/plugins/main.jsx b/client/my-sites/plugins/main.jsx index f2066b53da5b4..77d395da6399d 100644 --- a/client/my-sites/plugins/main.jsx +++ b/client/my-sites/plugins/main.jsx @@ -775,9 +775,7 @@ export default React.createClass( { ); } - if ( selectedSite && - selectedSite.modulesFetched && - ! selectedSite.canManage() ) { + if ( selectedSite && ! selectedSite.canManage() ) { return (
; } - if ( isJetpack && jetpackEnabled && site.modulesFetched && ! site.canManage() ) { + if ( isJetpack && jetpackEnabled && ! site.canManage() ) { return ; } From 68f9027f1a369bb3e35b531e4543278fe6ed77b3 Mon Sep 17 00:00:00 2001 From: Miguel Lezama Date: Wed, 16 Dec 2015 20:24:06 -0300 Subject: [PATCH 2/3] =?UTF-8?q?Jetpack=20Site:=20check=20if=20`this.module?= =?UTF-8?q?s`=20is=20defined,=20just=20in=20case=20=C2=AF\=5F(=E3=83=84)?= =?UTF-8?q?=5F/=C2=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/lib/site/jetpack.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/lib/site/jetpack.js b/client/lib/site/jetpack.js index 5af39b53d8cb8..cef43724be603 100644 --- a/client/lib/site/jetpack.js +++ b/client/lib/site/jetpack.js @@ -86,7 +86,7 @@ JetpackSite.prototype.isMainNetworkSite = function() { }; JetpackSite.prototype.isModuleActive = function( moduleId ) { - return this.modules.includes( moduleId ); + return this.modules && this.modules.includes( moduleId ); }; JetpackSite.prototype.verifyModulesActive = function( moduleIds, callback ) { From 17c4e89a6cb1690fb88a1d301bfafc894dc42931 Mon Sep 17 00:00:00 2001 From: Miguel Lezama Date: Thu, 17 Dec 2015 11:00:37 -0300 Subject: [PATCH 3/3] Jetpack Site: `includes` is only supported on Chrome 47 --- client/lib/site/jetpack.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/lib/site/jetpack.js b/client/lib/site/jetpack.js index cef43724be603..980302e227b75 100644 --- a/client/lib/site/jetpack.js +++ b/client/lib/site/jetpack.js @@ -86,7 +86,7 @@ JetpackSite.prototype.isMainNetworkSite = function() { }; JetpackSite.prototype.isModuleActive = function( moduleId ) { - return this.modules && this.modules.includes( moduleId ); + return this.modules && this.modules.indexOf( moduleId ) > -1; }; JetpackSite.prototype.verifyModulesActive = function( moduleIds, callback ) {