-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #204 from PRX/feat/186-previews-iframe-refactor
186: Previews iframe refactor
- Loading branch information
Showing
114 changed files
with
23,823 additions
and
7,268 deletions.
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 was deleted.
Oops, something went wrong.
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,22 @@ | ||
<?php | ||
/** | ||
* Preview template. | ||
* | ||
* @package tw_previews | ||
*/ | ||
|
||
?> | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Preview Window</title> | ||
<?php | ||
wp_print_scripts(); | ||
?> | ||
</head> | ||
<body> | ||
<div id="<?php echo esc_attr( TW_PREVIEWS_APP_CONTAINER_ID ); ?>"></div> | ||
</body> | ||
</html> |
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,134 @@ | ||
<?php | ||
/** | ||
* Setup admin page UI. | ||
* | ||
* @package tw_previews | ||
*/ | ||
|
||
define( 'TW_PREVIEWS_APP_CONTAINER_ID', 'tw-episode-importer-app' ); | ||
|
||
if ( ! function_exists( 'tw_previews_faust_exclude_redirect_preview_route' ) ) { | ||
/** | ||
* Prevent Faust from redirecting preview route to the front-end domain. | ||
* | ||
* @param array $excluded Array of excluded paths. | ||
* @return array Array of excluded paths. | ||
* | ||
* @package tw_previews | ||
*/ | ||
function tw_previews_faust_exclude_redirect_preview_route( $excluded ) { | ||
|
||
$route = add_query_arg( null, null ); | ||
|
||
if ( strpos( $route, 'preview/' ) > -1 ) { | ||
// Faust uses `basename` on the current URL path to compare with exclusion list. | ||
// `basename` is meant for use with file paths, and will only grab the last segment of the path, including query string. | ||
// Since the last segment of our preview URL is dynamic, and we have no idea if query string params have been added, | ||
// we have to add our exclude route dynamically using the same URL parsing Faust uses. | ||
// TODO: Keep an eye on this when Faust is updated. They may improve or alter their logic for path exlcusion. | ||
$excluded = array_merge( | ||
$excluded, | ||
array( | ||
basename( $route ), | ||
) | ||
); | ||
} | ||
|
||
return $excluded; | ||
} | ||
} | ||
add_filter( | ||
'faustwp_exclude_from_public_redirect', | ||
'tw_previews_faust_exclude_redirect_preview_route', | ||
20, | ||
1 | ||
); | ||
|
||
if ( ! function_exists( 'tw_previews_template_include_preview' ) ) { | ||
/** | ||
* Use custom template for previews. | ||
* | ||
* @param string $template Current template path. | ||
* @return string | ||
*/ | ||
function tw_previews_template_include_preview( $template ) { | ||
$route = add_query_arg( null, null ); | ||
|
||
if ( strpos( $route, 'preview/' ) > -1 ) { | ||
if ( ! is_user_logged_in() ) { | ||
header( 'Status: 403 Forbidden', true, 403 ); | ||
exit(); | ||
} | ||
return TW_PREVIEWS_PATH . '/preview/preview.php'; | ||
} | ||
|
||
return $template; | ||
} | ||
} | ||
add_action( 'template_include', 'tw_previews_template_include_preview' ); | ||
|
||
if ( ! function_exists( 'tw_previews_preview_post_link' ) ) { | ||
/** | ||
* Update preview post link. | ||
* | ||
* @param string $preview_link Current preview link. | ||
* @param WP_Post $post Post object preview link is being created for. | ||
* | ||
* @return string New post preview link URL. | ||
*/ | ||
function tw_previews_preview_post_link( $preview_link, $post ) { | ||
// We will work with a global ID that WPGraphQL uses. Will make queries easier down the line. | ||
$post_id = base64_encode( "post:{$post->ID}" ); | ||
$preview_path = "/preview/{$post_id}"; | ||
|
||
return $preview_path; | ||
} | ||
} | ||
add_filter( 'preview_post_link', 'tw_previews_preview_post_link', 100000, 2 ); | ||
|
||
/** | ||
* Render HTML and enqueue scripts for admin page. | ||
* | ||
* @return void | ||
*/ | ||
function tw_previews_preview_page_html() { | ||
|
||
global $wp; | ||
|
||
$not_preview = ! preg_match( '~^preview/~', $wp->request ); | ||
|
||
if ( ! $wp->request || $not_preview ) { | ||
return; | ||
} | ||
|
||
list(, $post_id) = explode( '/', $wp->request ); | ||
list(, $post_database_id) = explode( ':', base64_decode( $post_id ) ); | ||
$post = get_post( $post_database_id ); | ||
|
||
if ( in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) { | ||
$post->filter = 'sample'; | ||
} | ||
|
||
$permalink = get_permalink( $post, true ); | ||
$preview_url = preg_replace( '~(?<=/)[^/]+$~', "preview/{$post_id}", $permalink ); | ||
|
||
if ( function_exists( 'WPE\FaustWP\Replacement\equivalent_frontend_url' ) ) { | ||
$preview_url = WPE\FaustWP\Replacement\equivalent_frontend_url( $preview_url ); | ||
} | ||
|
||
wp_enqueue_style( 'tw-previews-preview-ui', TW_PREVIEWS_URL . 'preview/ui/ui.css', array(), wp_rand() ); | ||
wp_enqueue_script( 'tw-previews-preview-ui', TW_PREVIEWS_URL . 'preview/ui/dist/bundle.js', array(), wp_rand(), array( 'strategy' => 'defer' ) ); | ||
wp_localize_script( | ||
'tw-previews-preview-ui', | ||
'appLocalizer', | ||
array( | ||
'appContainerId' => TW_PREVIEWS_APP_CONTAINER_ID, | ||
'restUrl' => home_url( '/wp-json/wp/v2/' ), | ||
'gqlUrl' => trailingslashit( site_url() ) . 'index.php?' . \WPGraphQL\Router::$route, | ||
'nonce' => wp_create_nonce( 'wp_rest' ), | ||
'id' => $post_id, | ||
'previewUrl' => $preview_url, | ||
) | ||
); | ||
} | ||
add_action( 'wp_print_scripts', 'tw_previews_preview_page_html' ); |
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 @@ | ||
module.exports = { | ||
extends: [ | ||
"plugin:react/recommended", | ||
"plugin:react/jsx-runtime", | ||
"eslint-config-prettier", | ||
], | ||
parserOptions: { | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
ecmaVersion: "latest", | ||
sourceType: "module", | ||
}, | ||
rules: { | ||
"react/prop-types": 0, | ||
}, | ||
plugins: ["react", "@typescript-eslint"], | ||
}; |
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,2 @@ | ||
# Ignore generated graphql types | ||
src/types/api/graphql.ts |
11 changes: 11 additions & 0 deletions
11
wp-content/plugins/tw-previews/preview/ui/babel.config.json
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,11 @@ | ||
{ | ||
"presets": [ | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
"targets": "defaults" | ||
} | ||
], | ||
"@babel/preset-react" | ||
] | ||
} |
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 @@ | ||
import type { CodegenConfig } from '@graphql-codegen/cli'; | ||
|
||
const config: CodegenConfig = { | ||
overwrite: true, | ||
schema: 'http://the-world-wp.lndo.site/graphql', | ||
generates: { | ||
'src/types/api/graphql.ts': { | ||
plugins: ['typescript'] | ||
} | ||
} | ||
}; | ||
|
||
export default config; |
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,16 @@ | ||
{ | ||
"$schema": "https://ui.shadcn.com/schema.json", | ||
"style": "default", | ||
"rsc": false, | ||
"tsx": true, | ||
"tailwind": { | ||
"config": "./tailwind.config.js", | ||
"css": "./src/styles/global.scss", | ||
"baseColor": "slate", | ||
"cssVariables": true | ||
}, | ||
"aliases": { | ||
"components": "@/components", | ||
"utils": "@/lib/utils" | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
wp-content/plugins/tw-previews/preview/ui/dist/bundle.js.LICENSE.txt
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,29 @@ | ||
/** | ||
* @license React | ||
* react-dom.production.min.js | ||
* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
/** | ||
* @license React | ||
* react.production.min.js | ||
* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
/** | ||
* @license React | ||
* scheduler.production.min.js | ||
* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ |
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,7 @@ | ||
{ | ||
"compilerOptions": { | ||
"paths": { | ||
"@/*": ["./src/*"] | ||
} | ||
} | ||
} |
Oops, something went wrong.