|
| 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' ); |
0 commit comments