-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Site Editor on WP.com: Initial explorations, take two #44928
Draft
ockham
wants to merge
1
commit into
trunk
Choose a base branch
from
add/custom-edit-post-take-three
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+745
−9
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import config from '../../config'; | ||
import { initialize } from '@wordpress/edit-site'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import './utils'; | ||
|
||
window.AppBoot = () => { | ||
if ( ! config.isEnabled( 'custom-editor' ) ) { | ||
window.location.href = '/'; | ||
} else { | ||
initialize( 'wpcom', {} ); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const CUSTOM_EDITOR_SECTION_DEFINITION = { | ||
name: 'custom-editor', | ||
paths: [ '/custom-editor' ], | ||
module: 'custom-editor', | ||
secondary: false, | ||
group: 'custom-editor', | ||
enableLoggedOut: true, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"extends": "@automattic/calypso-build/tsconfig", | ||
"compilerOptions": { | ||
// Disallow features that require cross-file information for emit. | ||
// Must be used with babel typescript | ||
"isolatedModules": true, | ||
|
||
"baseUrl": ".", | ||
"paths": { | ||
"*": [ "*", "../../*" ] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import wpcomRequest from 'wpcom-proxy-request'; | ||
|
||
/** | ||
* A lot of the following is copied from D37093-code. | ||
* Maybe we can move this code into Calypso `packages/`, parametrize it (e.g. apiFetch) | ||
* and move into Calypso's `packages/wpcom-proxy-request`, or publish as a new npm for use on WP.com. | ||
*/ | ||
|
||
// FIXME -- quick'n'dirty setting of a few params | ||
const _currentSiteId = 89417913; // @ockham's test site, replace with your own | ||
|
||
const wpApiSettings = { | ||
root: 'https://public-api.wordpress.com/', | ||
}; | ||
|
||
/** | ||
* Normalizes the path for requests to the public API. | ||
*/ | ||
function wpcomFetchNormalizePath( options ) { | ||
if ( options.url && options.url.indexOf( wpApiSettings.root ) !== -1 ) { | ||
options.path = options.url.replace( wpApiSettings.root, '' ); | ||
delete options.url; | ||
} | ||
|
||
if ( options.path ) { | ||
// Ensures path starts with a slash. | ||
if ( ! options.path.startsWith( '/' ) ) { | ||
options.path = '/' + options.path; | ||
} | ||
|
||
// Removes namespace from path. | ||
if ( options.apiNamespace ) { | ||
options.path = options.path.replace( '/' + options.apiNamespace, '' ); | ||
} | ||
} | ||
|
||
return options; | ||
} | ||
|
||
/** | ||
* Creates a fetch-like response. | ||
*/ | ||
function wpcomFetchCreateFetchResponse( data, status, headers ) { | ||
var fetchResponse; | ||
var normalizedHeaders = {}; | ||
for ( var header in headers ) { | ||
normalizedHeaders[ header.toLowerCase() ] = headers[ header ]; | ||
} | ||
if ( Array.isArray( data ) ) { | ||
fetchResponse = data.slice( 0 ); | ||
} else { | ||
fetchResponse = Object.assign( {}, data ); | ||
} | ||
fetchResponse.status = status; | ||
fetchResponse.json = function() { | ||
return Promise.resolve( data ); | ||
}; | ||
fetchResponse.headers = { | ||
get: function( name ) { | ||
return normalizedHeaders[ name && name.toLowerCase() ]; | ||
}, | ||
}; | ||
return fetchResponse; | ||
} | ||
|
||
/** | ||
* Determines the namespace from the request path. | ||
*/ | ||
function wpcomFetchSetNamespace( options, next ) { | ||
if ( options.path && ! options.namespace ) { | ||
var namespace = options.path.match( /^\/([a-z]+\/v?[0-9.]+)\// ); | ||
if ( namespace ) { | ||
options.apiNamespace = namespace[ 1 ]; | ||
options.path = options.path.replace( '/' + options.apiNamespace, '' ); | ||
} else { | ||
// Defaults all requests to the wp/v2 namespace. | ||
options.apiNamespace = 'wp/v2'; | ||
} | ||
} | ||
console.log( 'post-norm options', options ); | ||
return next( options, next ); | ||
} | ||
|
||
/** | ||
* Prefixes any non-global request endpoint to the site specific endpoint. | ||
*/ | ||
function wpcomFetchAddSitePrefix( options, next ) { | ||
if ( options.path && ! options.global && options.path.indexOf( '/sites/' ) === -1 ) { | ||
options.path = '/sites/' + _currentSiteId + options.path; | ||
} | ||
return next( options, next ); | ||
} | ||
|
||
// Register middlewares (last-registered runs first). | ||
[ | ||
wpcomFetchAddSitePrefix, | ||
wpcomFetchSetNamespace, | ||
function( options, next ) { | ||
// Path needs to be normalized first. | ||
return next( wpcomFetchNormalizePath( options ), next ); | ||
}, | ||
].forEach( function( middleware ) { | ||
apiFetch.use( middleware ); | ||
} ); | ||
|
||
apiFetch.setFetchHandler( wpcomRequest ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole module is very interesting. Any ideas how we would be able to get this to work server-side? Where would the middleware be able to get the current site ID from? Is this already solved elsewhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you elaborate? We could probably implement those endpoints in PHP and pass through to the 'actual' ones. I'm not quite sure why we would do that, since it seems it would require us to keep a much more explicit list of existing endpoints (which would mean higher maintenance burden etc) 🤔
I lifted this stuff mostly from the
gutenberg-wpcom
plugin, and that's using a_currentSiteId
global that's attached towindow
somehow by another plugin (I think).Alternatively, I think that on the client side, we should be able to store the current site ID somewhere in Redux/
@wordpress/data
state and have the middleware pull it from there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By "server side" I meant the Calypso Node server, in the case of SSR, for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, sorry for misreading. I haven't really thought about that yet TBH. It's true that a lot of
@wordpress/
packages don't work particularly well on the server side, but we might be able to make them. (Here's an old exploration by a Team Calypso alum: WordPress/gutenberg#16227.)