down to it; the print stylesheet hides each sibling off that * path, so only the article prints whatever the theme's markup is. Ctrl+P is left exactly as the * theme made it: every rule here is scoped to body.csdt-pdf-printing, which only the button sets. * * Available to every visitor, signed in or not. The button is static markup with no nonce, so a * page cache serves it unchanged, and it is hidden until the script that makes it work has run. * * @package CloudScale_Cyber_DevTools * @since 1.10.1048 */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Export to PDF button and print isolation for single posts. */ class CSDT_PDF_Export { /** Option: '1' shows the button on posts, '0' removes it and its assets. */ const OPTION = 'csdt_pdf_export_enabled'; /** * Registers the front-end hooks, only when the button is switched on. * * @return void */ public static function init(): void { if ( '1' !== (string) CSDT_Options::get( self::OPTION ) ) { return; } add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue' ) ); // Late, so the button sits above the content other filters have already built. add_filter( 'the_content', array( __CLASS__, 'add_button' ), 20 ); } /** * Post types that get the button. Posts only by default: a page is usually a landing page * or a form, not something a reader keeps. * * @return string[] */ private static function post_types(): array { /** * Filters the post types that show the Export to PDF button. * * @param string[] $post_types Default array( 'post' ). */ return (array) apply_filters( 'csdt_pdf_export_post_types', array( 'post' ) ); } /** * Whether this request is a single post view the button belongs on. * * @return bool */ private static function applies(): bool { return is_singular( self::post_types() ) && ! is_feed() && ! is_embed(); } /** * Loads the button's script and the print stylesheet on single posts. * * @return void */ public static function enqueue(): void { if ( ! self::applies() ) { return; } wp_enqueue_style( 'csdt-pdf-export', plugins_url( 'assets/cs-pdf-export.css', CSDT_PLUGIN_FILE ), array(), CSDT_DevTools::VERSION ); wp_enqueue_script( 'csdt-pdf-export', plugins_url( 'assets/cs-pdf-export.js', CSDT_PLUGIN_FILE ), array(), CSDT_DevTools::VERSION, array( 'in_footer' => true, 'strategy' => 'defer', ) ); } /** * Puts the button above the post content, and a print-only source line under it. * * Only for the main post of a single view: an excerpt, a related-posts widget or a second * the_content() call in the sidebar gets nothing. * * @param string $content Post content. * @return string */ public static function add_button( $content ) { if ( ! self::applies() || ! in_the_loop() || ! is_main_query() || get_the_ID() !== get_queried_object_id() ) { return $content; } $title = wp_strip_all_tags( get_the_title() ); $label = __( 'Export to PDF', 'cloudscale-cyber-devtools' ); $bar = '
'; // Shown only in the PDF: a printed page needs to say where it came from. $source = ''
. '' . esc_html( $title ) . ''
. esc_html( get_bloginfo( 'name' ) ) . ', ' . esc_html( get_the_date() )
. '
' . esc_html( (string) get_permalink() )
. '