Skip to content

Commit

Permalink
Add JS to make the preview link work
Browse files Browse the repository at this point in the history
  • Loading branch information
pcraig3 committed Feb 14, 2022
1 parent ee77609 commit fc9cd8d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 29 deletions.
58 changes: 29 additions & 29 deletions wordpress/wp-content/themes/cds-redirector/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,38 @@ public function addActions()
add_action('admin_menu', [$this, 'addAdminMenu']);
add_action('admin_init', [$this, 'registerSettings']);

add_action(
'admin_footer',
function () {
global $post;

if (!$post) {
return;
}

$text = __('Preview', 'cds-snc');
$url =
Utils::addHttp(
cds_get_theme_option('redirect_url'),
) .
'/preview?id=' .
$post->ID .
'&lang=' .
cds_get_active_language();
echo "<script>
jQuery(document).ready(function( $ ) {
setTimeout(function(){
$('.block-editor-post-preview__dropdown').replaceWith('<a class=\"components-button is-tertiary\" href=\"$url\">$text</a>');
}, 1000);
});
</script>";
},
200,
10,
);
add_action('admin_enqueue_scripts', [$this, 'redirector_scripts']);
}
}



/**
* Enqueue scripts.
*/
public function redirector_scripts(): void
{
global $post;

if (!$post) {
return;
}

wp_enqueue_script('redirector-main', get_template_directory_uri() . '/js/main.js', ['jquery']);

$url = Utils::addHttp(
cds_get_theme_option('redirect_url')
) .
'/preview?id=' .
$post->ID .
'&lang=' .
cds_get_active_language();

$params = array('url' => $url);
wp_localize_script('redirector-main', 'OBJECT', $params );
}


/**
* Returns all theme options
*
Expand Down
19 changes: 19 additions & 0 deletions wordpress/wp-content/themes/cds-redirector/js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
console.log('redirector!');
console.log(OBJECT.url);

jQuery(function () {
const checkPreviewInterval = setTimeout(checkPreview, 1000);

function checkPreview() {
const previewButton = jQuery(".block-editor-post-preview__button-toggle").first();
if (previewButton.length) {
// remove event handlers
previewButton.off();

// add our own event handler to open preview link
previewButton.on("click", function() {
window.open(OBJECT.url, "_blank");
});
}
}
});

0 comments on commit fc9cd8d

Please sign in to comment.