*/ private static $shortcode_posts = array(); /** * Registers the front-end hooks. The automatic line is hooked only when the * settings turn it on, so a site that only uses the shortcode pays nothing * on every `the_content` call. */ public function __construct() { add_shortcode( self::SHORTCODE, array( $this, 'shortcode' ) ); if ( is_admin() ) { return; } add_action( 'wp_enqueue_scripts', array( $this, 'maybe_enqueue_style_early' ) ); $settings = DevLab_LU_Settings::get(); if ( $settings['auto'] && ! empty( $settings['post_types'] ) ) { add_filter( 'the_content', array( $this, 'filter_content' ), self::CONTENT_PRIORITY ); } } /** * Loads the stylesheet. WordPress prints it in the footer when this runs * after the page head was sent. * * @return void */ private function enqueue_style() { $settings = DevLab_LU_Settings::get(); if ( ! $settings['load_css'] ) { return; } wp_enqueue_style( self::STYLE_HANDLE, DEVLAB_LU_URL . 'assets/css/frontend.css', array(), DEVLAB_LU_VERSION ); } /** * On single pages that will show the line, loads the stylesheet in the * head, so the line never appears unstyled for a moment. * * @return void */ public function maybe_enqueue_style_early() { if ( ! is_singular() ) { return; } $post = get_queried_object(); if ( ! $post instanceof WP_Post ) { return; } if ( $this->auto_line_applies( $post ) || has_shortcode( $post->post_content, self::SHORTCODE ) ) { $this->enqueue_style(); } } /** * Whether the automatic line is meant for this post: the setting is on, the * post type was picked, and the post was edited more than five minutes * after it was published. * * @param WP_Post $post Post object. * @return bool */ private function auto_line_applies( $post ) { $settings = DevLab_LU_Settings::get(); return $settings['auto'] && in_array( $post->post_type, $settings['post_types'], true ) && ! post_password_required( $post ) && DevLab_LU_Line::is_updated( $post ); } /** * Adds the line before or after the content of the main post on a single * page. * * It stays out of feeds, archives, excerpts, REST responses and any other * post that happens to be printed on the page (related-post lists, for * example). * * @param string $content Post content. * @return string */ public function filter_content( $content ) { if ( ! is_string( $content ) || ! is_singular() || is_feed() || ! in_the_loop() || ! is_main_query() || doing_filter( 'get_the_excerpt' ) ) { return $content; } $post = get_post(); if ( ! $post instanceof WP_Post || (int) get_queried_object_id() !== (int) $post->ID || isset( self::$shortcode_posts[ $post->ID ] ) || ! $this->auto_line_applies( $post ) ) { return $content; } $settings = DevLab_LU_Settings::get(); $position = $settings['position']; $line = DevLab_LU_Line::render( $post, array( 'tag' => 'p', 'modifier' => $position, ) ); if ( '' === $line ) { return $content; } $this->enqueue_style(); return DevLab_LU_Settings::POSITION_AFTER === $position ? $content . "\n" . $line : $line . "\n" . $content; } /** * Finds the post a shortcode is about. * * Without an `id`, it is the post being shown: the one whose content is * being filtered, or the single post or page being viewed (elsewhere it * matches nothing). With an `id`, only a * published, public, unprotected post is accepted, so a shortcode written * by one author cannot reveal the dates of someone else's private content. * An `id` that is not a positive whole number matches nothing, instead of * quietly falling back to the current post. * * @param mixed $raw_id The `id` attribute as written, or '' when absent. * @return WP_Post|null */ private function resolve_post( $raw_id ) { $raw_id = trim( (string) $raw_id ); if ( '' !== $raw_id ) { $id = ctype_digit( $raw_id ) ? (int) $raw_id : 0; if ( $id < 1 ) { return null; } $post = get_post( $id ); if ( ! $post instanceof WP_Post || 'publish' !== $post->post_status || ! is_post_type_viewable( $post->post_type ) || post_password_required( $post ) ) { return null; } return $post; } // Without an id the line belongs to the post being shown: the one whose // content is being filtered, or the single post or page being viewed. // Elsewhere (a widget on the home page or an archive) the global post is // whatever the last loop left behind, so print nothing. if ( ! doing_filter( 'the_content' ) && ! is_singular() ) { return null; } $post = get_post(); return $post instanceof WP_Post ? $post : null; } /** * Renders [devlab_last_updated]. * * Attributes (all optional): * - label: text before the date; label="" prints the date alone. * - format: a PHP date format for this one line. * - id: the post to read the date from (default: the current post). * - only_if_updated: "yes" prints nothing unless the post was edited more * than five minutes after it was published. * * @param array|string $atts Shortcode attributes. * @return string Escaped HTML, or '' when there is nothing to show. */ public function shortcode( $atts ) { $atts = shortcode_atts( array( 'label' => false, 'format' => '', 'id' => '', 'only_if_updated' => 'no', ), $atts, self::SHORTCODE ); $post = $this->resolve_post( $atts['id'] ); if ( ! $post ) { return ''; } if ( DevLab_LU_Settings::to_flag( $atts['only_if_updated'] ) && ! DevLab_LU_Line::is_updated( $post ) ) { return ''; } $format = sanitize_text_field( (string) $atts['format'] ); $format = mb_substr( $format, 0, DevLab_LU_Settings::FORMAT_MAX ); $line = DevLab_LU_Line::render( $post, array( 'tag' => 'span', 'label' => false === $atts['label'] ? null : sanitize_text_field( (string) $atts['label'] ), 'format' => $format, ) ); if ( '' === $line ) { return ''; } // Remember it only when the line belongs to the post being displayed, // which is the one the automatic line would repeat. $current = get_post(); if ( $current instanceof WP_Post && (int) $current->ID === (int) $post->ID ) { self::$shortcode_posts[ $post->ID ] = true; } $this->enqueue_style(); return $line; } }