Skip to content

Commit

Permalink
Merge pull request #178 from Automattic/add/site.plugin
Browse files Browse the repository at this point in the history
Add site plugin methods
  • Loading branch information
retrofox authored and jsnajdr committed Jan 27, 2020
1 parent bfb2489 commit 52572bf
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 21 deletions.
8 changes: 4 additions & 4 deletions packages/wpcom.js/lib/runtime/site.get.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
[
{ "name": "categoriesList", "subpath": "categories" },
{ "name": "commentsList", "subpath": "comments" },
{ "name": "domainsList", "subpath": "domains" },
{ "name": "embedsList", "subpath": "embeds" },
{ "name": "embedsList", "subpath": "embeds" },
{ "name": "domainsList", "subpath": "domains" },
{ "name": "followsList", "subpath": "follows" },
{ "name": "mediaList", "subpath": "media" },
{ "name": "pluginsList", "subpath": "plugins" },
{ "name": "pageTemplates", "subpath": "page-templates" },
{ "name": "pluginsList", "subpath": "plugins" },
{ "name": "postsList", "subpath": "posts" },
{ "name": "postTypesList", "subpath": "post-types" },
{ "name": "shortcodesList", "subpath": "shortcodes" },
{ "name": "stats", "subpath": "stats" },
{ "name": "statsClicks", "subpath": "stats/clicks" },
{ "name": "shortcodesList", "subpath": "shortcodes" },
{ "name": "statsComments", "subpath": "stats/comments" },
{ "name": "statsCommentFollowers", "subpath": "stats/comment-followers" },
{ "name": "statsComments", "subpath": "stats/comments" },
{ "name": "statsCountryViews", "subpath": "stats/country-views" },
{ "name": "statsFollowers", "subpath": "stats/followers" },
{ "name": "statsPublicize", "subpath": "stats/publicize" },
Expand Down
2 changes: 1 addition & 1 deletion packages/wpcom.js/lib/site.domain.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Module variables
* Module vars
*/
const root = '/sites';

Expand Down
17 changes: 9 additions & 8 deletions packages/wpcom.js/lib/site.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
/**
* Module dependencies.
*/
import Post from './site.post';
import Category from './site.category';
import Tag from './site.tag';
import Media from './site.media';
import Comment from './site.comment';
import SiteWordAds from './site.wordads';
import Follow from './site.follow';
import SitePlugin from './site.plugin';
import Media from './site.media';
import Post from './site.post';
import Tag from './site.tag';
import SiteDomain from './site.domain';
import SitePlugin from './site.plugin';
import SiteSettings from './site.settings';
import SiteTaxonomy from './site.taxonomy';
import SiteWordAds from './site.wordads';

import runtimeBuilder from './util/runtime-builder';
import siteGetMethods from './runtime/site.get.json';
import debugFactory from 'debug';
Expand Down Expand Up @@ -160,11 +161,11 @@ class Site {
/**
* Create a `SitePlugin` instance
*
* @param {String} id - plugin identifier
* @param {String} slug - plugin identifier
* @return {SitePlugin} SitePlugin instance
*/
plugin( id ) {
return new SitePlugin( id, this._id, this.wpcom );
plugin( slug ) {
return new SitePlugin( slug, this._id, this.wpcom );
}

/**
Expand Down
116 changes: 108 additions & 8 deletions packages/wpcom.js/lib/site.plugin.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,138 @@
/**
* Module variables
* Module vars
*/
const root = '/sites';

class SitePlugin {

/**
* `SitePlugin` constructor.
*
* @param {String} [id] - the plugin ID
* @param {String} [slug] - the plugin slug
* @param {Number|String} sid - site identifier
* @param {WPCOM} wpcom - wpcom instance
* @return {Undefined} undefined
*/
constructor( id, sid, wpcom ) {
constructor( slug, sid, wpcom ) {
if ( ! ( this instanceof SitePlugin ) ) {
return new SitePlugin( id, sid, wpcom );
return new SitePlugin( slug, sid, wpcom );
}

if ( ! slug ) {
throw new Error( '`slug` is not correctly defined' );
}

this._id = id;
this._slug = encodeURIComponent( slug );
this._sid = sid;
this.path = `${root}/${this._sid}/plugins`;
this.wpcom = wpcom;

const path = `${root}/${ this._sid }/plugins`;
this.pluginPath = `${ path }/${ this._slug }`;
}

/**
* Get informtion about the plugin
*
* @param {Object} [query] - query object parameter
* @param {Function} [fn] - callback function
* @return {Function} request handler
* @return {Promise} Promise
*/
get( query, fn ) {
return this.wpcom.req.get( `${this.path}/${this._id}`, query, fn );
return this.wpcom.req.get( this.pluginPath, query, fn );
}

/**
* config the plugin
*
* @param {Object} [query] - query object parameter
* @param {Object} config - plugin config object
* @param {Function} [fn] - callback function
* @return {Promise} Promise
*/
config( query, config, fn ) {
return this.wpcom.req.put( this.pluginPath, query, config, fn );
};

/**
* Update the plugin
*
* @param {Object} [query] - query object parameter
* @param {Function} [fn] - callback function
* @return {Promise} Promise
*/
update( query, fn ) {
return this.wpcom.req.put( `${ this.pluginPath }/update`, query, fn );
};

/**
* Install the plugin
*
* @param {Object} [query] - query object parameter
* @param {Function} [fn] - callback function
* @return {Promise} Promise
*/
install( query, fn ) {
return this.wpcom.req.put( `${ this.pluginPath }/install`, query, fn );
};

/**
* Delete the plugin
*
* @param {Object} [query] - query object parameter
* @param {Function} [fn] - callback function
* @return {Promise} Promise
*/
delete( query, fn ) {
return this.wpcom.req.put( `${ this.pluginPath }/delete`, query, fn );
};

/**
* Activate the plugin
* This method is a shorthand of config()
*
* @param {Object} [query] - query object parameter
* @param {Function} [fn] - callback function
* @return {Promise} Promise
*/
activate( query, fn ) {
return this.config( query, { active: true }, fn );
};

/**
* Deactivate the plugin
* This method is a shorthand of config()
*
* @param {Object} [query] - query object parameter
* @param {Function} [fn] - callback function
* @return {Promise} Promise
*/
deactivate( query, fn ) {
return this.config( query, { active: false }, fn );
}

/**
* Enable plugin autoupdate
* This method is a shorthand of config()
*
* @param {Object} [query] - query object parameter
* @param {Function} [fn] - callback function
* @return {Promise} Promise
*/
enableAutoupdate( query, fn ) {
return this.config( query, { autoupdate: true }, fn );
}

/**
* Disable plugin autoupdate
* This method is a shorthand of config()
*
* @param {Object} [query] - query object parameter
* @param {Function} [fn] - callback function
* @return {Promise} Promise
*/
disableAutoupdate( query, fn ) {
return this.config( query, { autoupdate: false }, fn );
};
}

/**
Expand Down
11 changes: 11 additions & 0 deletions packages/wpcom.js/lib/util/runtime-builder.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/**
* Module dependencies
*/
import debugFactory from 'debug';

/**
* Module vars
*/
const debug = debugFactory( 'wpcom:runtime' );

/**
* Build a generic method
*
Expand Down Expand Up @@ -26,6 +36,7 @@ export default function( Class, list, buildPath ) {
? methodParams
: { name: methodParams }

debug( 'Adding %o', methodParams.name );
Class.prototype[ methodParams.name ] = methodBuilder( methodParams, buildPath );
} );
};

0 comments on commit 52572bf

Please sign in to comment.