Skip to content

Commit 5976b7a

Browse files
authored
Merge pull request #204 from PRX/feat/186-previews-iframe-refactor
186: Previews iframe refactor
2 parents 00e129d + c4aaafc commit 5976b7a

File tree

114 files changed

+23823
-7268
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

114 files changed

+23823
-7268
lines changed

.lando.yml

+24
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,30 @@ services:
1616
build:
1717
- npm install
1818
- chmod +x scripts/*
19+
appserver_nginx:
20+
scanner:
21+
retry: 10
22+
okCodes:
23+
- 302
24+
- 401
25+
- 402
26+
- 403
27+
edge:
28+
scanner:
29+
retry: 10
30+
okCodes:
31+
- 302
32+
- 401
33+
- 402
34+
- 403
35+
edge_ssl:
36+
scanner:
37+
retry: 10
38+
okCodes:
39+
- 302
40+
- 401
41+
- 402
42+
- 403
1943
mailhog:
2044
type: mailhog
2145
portforward: false

node_modules/.package-lock.json

-7
This file was deleted.

wp-content/mu-plugins/the-world-site-config/configs/global/global-plugins.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
'tw-media/tw-media.php',
3434
'tw-menus/tw-menus.php',
3535
'tw-newsletters/tw-newsletters.php',
36+
'tw-previews/tw-previews.php',
3637
'tw-programs/tw-programs.php',
3738
'tw-qa-block/tw-qa-block.php',
3839
'tw-datawrapper-block/tw-datawrapper-block.php',
@@ -51,7 +52,6 @@
5152
'svg-block/svg-block.php',
5253
'wp-graphql/wp-graphql.php',
5354
'wp-graphql-acf/wp-graphql-acf.php',
54-
'wp-graphql-jwt-authentication/wp-graphql-jwt-authentication.php',
5555
'add-wpgraphql-seo/wp-graphql-yoast-seo.php',
5656
'custom-post-type-permalinks/custom-post-type-permalinks.php',
5757
'faustwp/faustwp.php',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Preview template.
4+
*
5+
* @package tw_previews
6+
*/
7+
8+
?>
9+
<!DOCTYPE html>
10+
<html lang="en">
11+
<head>
12+
<meta charset="UTF-8">
13+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
14+
<title>Preview Window</title>
15+
<?php
16+
wp_print_scripts();
17+
?>
18+
</head>
19+
<body>
20+
<div id="<?php echo esc_attr( TW_PREVIEWS_APP_CONTAINER_ID ); ?>"></div>
21+
</body>
22+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
/**
3+
* Setup admin page UI.
4+
*
5+
* @package tw_previews
6+
*/
7+
8+
define( 'TW_PREVIEWS_APP_CONTAINER_ID', 'tw-episode-importer-app' );
9+
10+
if ( ! function_exists( 'tw_previews_faust_exclude_redirect_preview_route' ) ) {
11+
/**
12+
* Prevent Faust from redirecting preview route to the front-end domain.
13+
*
14+
* @param array $excluded Array of excluded paths.
15+
* @return array Array of excluded paths.
16+
*
17+
* @package tw_previews
18+
*/
19+
function tw_previews_faust_exclude_redirect_preview_route( $excluded ) {
20+
21+
$route = add_query_arg( null, null );
22+
23+
if ( strpos( $route, 'preview/' ) > -1 ) {
24+
// Faust uses `basename` on the current URL path to compare with exclusion list.
25+
// `basename` is meant for use with file paths, and will only grab the last segment of the path, including query string.
26+
// Since the last segment of our preview URL is dynamic, and we have no idea if query string params have been added,
27+
// we have to add our exclude route dynamically using the same URL parsing Faust uses.
28+
// TODO: Keep an eye on this when Faust is updated. They may improve or alter their logic for path exlcusion.
29+
$excluded = array_merge(
30+
$excluded,
31+
array(
32+
basename( $route ),
33+
)
34+
);
35+
}
36+
37+
return $excluded;
38+
}
39+
}
40+
add_filter(
41+
'faustwp_exclude_from_public_redirect',
42+
'tw_previews_faust_exclude_redirect_preview_route',
43+
20,
44+
1
45+
);
46+
47+
if ( ! function_exists( 'tw_previews_template_include_preview' ) ) {
48+
/**
49+
* Use custom template for previews.
50+
*
51+
* @param string $template Current template path.
52+
* @return string
53+
*/
54+
function tw_previews_template_include_preview( $template ) {
55+
$route = add_query_arg( null, null );
56+
57+
if ( strpos( $route, 'preview/' ) > -1 ) {
58+
if ( ! is_user_logged_in() ) {
59+
header( 'Status: 403 Forbidden', true, 403 );
60+
exit();
61+
}
62+
return TW_PREVIEWS_PATH . '/preview/preview.php';
63+
}
64+
65+
return $template;
66+
}
67+
}
68+
add_action( 'template_include', 'tw_previews_template_include_preview' );
69+
70+
if ( ! function_exists( 'tw_previews_preview_post_link' ) ) {
71+
/**
72+
* Update preview post link.
73+
*
74+
* @param string $preview_link Current preview link.
75+
* @param WP_Post $post Post object preview link is being created for.
76+
*
77+
* @return string New post preview link URL.
78+
*/
79+
function tw_previews_preview_post_link( $preview_link, $post ) {
80+
// We will work with a global ID that WPGraphQL uses. Will make queries easier down the line.
81+
$post_id = base64_encode( "post:{$post->ID}" );
82+
$preview_path = "/preview/{$post_id}";
83+
84+
return $preview_path;
85+
}
86+
}
87+
add_filter( 'preview_post_link', 'tw_previews_preview_post_link', 100000, 2 );
88+
89+
/**
90+
* Render HTML and enqueue scripts for admin page.
91+
*
92+
* @return void
93+
*/
94+
function tw_previews_preview_page_html() {
95+
96+
global $wp;
97+
98+
$not_preview = ! preg_match( '~^preview/~', $wp->request );
99+
100+
if ( ! $wp->request || $not_preview ) {
101+
return;
102+
}
103+
104+
list(, $post_id) = explode( '/', $wp->request );
105+
list(, $post_database_id) = explode( ':', base64_decode( $post_id ) );
106+
$post = get_post( $post_database_id );
107+
108+
if ( in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
109+
$post->filter = 'sample';
110+
}
111+
112+
$permalink = get_permalink( $post, true );
113+
$preview_url = preg_replace( '~(?<=/)[^/]+$~', "preview/{$post_id}", $permalink );
114+
115+
if ( function_exists( 'WPE\FaustWP\Replacement\equivalent_frontend_url' ) ) {
116+
$preview_url = WPE\FaustWP\Replacement\equivalent_frontend_url( $preview_url );
117+
}
118+
119+
wp_enqueue_style( 'tw-previews-preview-ui', TW_PREVIEWS_URL . 'preview/ui/ui.css', array(), wp_rand() );
120+
wp_enqueue_script( 'tw-previews-preview-ui', TW_PREVIEWS_URL . 'preview/ui/dist/bundle.js', array(), wp_rand(), array( 'strategy' => 'defer' ) );
121+
wp_localize_script(
122+
'tw-previews-preview-ui',
123+
'appLocalizer',
124+
array(
125+
'appContainerId' => TW_PREVIEWS_APP_CONTAINER_ID,
126+
'restUrl' => home_url( '/wp-json/wp/v2/' ),
127+
'gqlUrl' => trailingslashit( site_url() ) . 'index.php?' . \WPGraphQL\Router::$route,
128+
'nonce' => wp_create_nonce( 'wp_rest' ),
129+
'id' => $post_id,
130+
'previewUrl' => $preview_url,
131+
)
132+
);
133+
}
134+
add_action( 'wp_print_scripts', 'tw_previews_preview_page_html' );
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
extends: [
3+
"plugin:react/recommended",
4+
"plugin:react/jsx-runtime",
5+
"eslint-config-prettier",
6+
],
7+
parserOptions: {
8+
ecmaFeatures: {
9+
jsx: true,
10+
},
11+
ecmaVersion: "latest",
12+
sourceType: "module",
13+
},
14+
rules: {
15+
"react/prop-types": 0,
16+
},
17+
plugins: ["react", "@typescript-eslint"],
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Ignore generated graphql types
2+
src/types/api/graphql.ts
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"presets": [
3+
[
4+
"@babel/preset-env",
5+
{
6+
"targets": "defaults"
7+
}
8+
],
9+
"@babel/preset-react"
10+
]
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { CodegenConfig } from '@graphql-codegen/cli';
2+
3+
const config: CodegenConfig = {
4+
overwrite: true,
5+
schema: 'http://the-world-wp.lndo.site/graphql',
6+
generates: {
7+
'src/types/api/graphql.ts': {
8+
plugins: ['typescript']
9+
}
10+
}
11+
};
12+
13+
export default config;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"$schema": "https://ui.shadcn.com/schema.json",
3+
"style": "default",
4+
"rsc": false,
5+
"tsx": true,
6+
"tailwind": {
7+
"config": "./tailwind.config.js",
8+
"css": "./src/styles/global.scss",
9+
"baseColor": "slate",
10+
"cssVariables": true
11+
},
12+
"aliases": {
13+
"components": "@/components",
14+
"utils": "@/lib/utils"
15+
}
16+
}

wp-content/plugins/tw-previews/preview/ui/dist/bundle.js

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @license React
3+
* react-dom.production.min.js
4+
*
5+
* Copyright (c) Facebook, Inc. and its affiliates.
6+
*
7+
* This source code is licensed under the MIT license found in the
8+
* LICENSE file in the root directory of this source tree.
9+
*/
10+
11+
/**
12+
* @license React
13+
* react.production.min.js
14+
*
15+
* Copyright (c) Facebook, Inc. and its affiliates.
16+
*
17+
* This source code is licensed under the MIT license found in the
18+
* LICENSE file in the root directory of this source tree.
19+
*/
20+
21+
/**
22+
* @license React
23+
* scheduler.production.min.js
24+
*
25+
* Copyright (c) Facebook, Inc. and its affiliates.
26+
*
27+
* This source code is licensed under the MIT license found in the
28+
* LICENSE file in the root directory of this source tree.
29+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"paths": {
4+
"@/*": ["./src/*"]
5+
}
6+
}
7+
}

0 commit comments

Comments
 (0)