'', 'post_types' => array( 'post' ), ) ); } /** * Public post types that support comments, as name => label. * * @return array */ function livere_comments_available_post_types() { $types = array(); foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $type ) { if ( post_type_supports( $type->name, 'comments' ) ) { $types[ $type->name ] = $type->labels->singular_name; } } return $types; } /** * Whether the LiveRe widget replaces the comments of this post. * * @param WP_Post|int|null $post Post to check. Defaults to the current post. * @return bool */ function livere_comments_is_enabled_for( $post = null ) { $post = get_post( $post ); if ( ! $post ) { return false; } $settings = livere_comments_get_settings(); $enabled = '' !== $settings['client_id'] && in_array( $post->post_type, (array) $settings['post_types'], true ) && ! post_password_required( $post ) && '1' !== get_post_meta( $post->ID, LIVERE_COMMENTS_DISABLED_META, true ); /** * Filters whether LiveRe replaces the comments of a post. * * @param bool $enabled Whether LiveRe is shown. * @param WP_Post $post The post. */ return (bool) apply_filters( 'livere_comments_enabled', $enabled, $post ); } /** * Article key for a post. LiveRe stores comments per key; by default the widget uses the * page path and query, so the permalink path keeps comments with the post and matches the * key the LiveRe comment-count script derives from links to the post. * * @param WP_Post $post The post. * @return string */ function livere_comments_article_key( $post ) { $key = wp_make_link_relative( get_permalink( $post ) ); /** * Filters the article key sent to LiveRe for a post. * * @param string $key Article key. Defaults to the permalink path. * @param WP_Post $post The post. */ return (string) apply_filters( 'livere_comments_article_key', $key, $post ); } /** * Prints the widget for the current post. */ function livere_comments_render() { $post = get_post(); if ( ! $post || ! livere_comments_is_enabled_for( $post ) ) { return; } $settings = livere_comments_get_settings(); // The widget reads window.livereOptions.refer when it starts; module scripts run after // parsing, so setting it here (before the closing body tag) is early enough. wp_print_inline_script_tag( 'window.livereOptions = Object.assign(window.livereOptions || {}, ' . wp_json_encode( array( 'refer' => livere_comments_article_key( $post ) ) ) . ');' ); printf( '
', esc_attr( $settings['client_id'] ) ); } /** * Same as livere_comments_render(), returned as a string. * * @return string */ function livere_comments_get_markup() { ob_start(); livere_comments_render(); return (string) ob_get_clean(); } // Classic themes: swap the theme's comments.php for ours. add_filter( 'comments_template', function ( $template ) { if ( livere_comments_is_enabled_for() ) { return plugin_dir_path( __FILE__ ) . 'templates/comments.php'; } return $template; }, 100 ); // Block themes: replace the Comments block (and the older Post Comments block). add_filter( 'render_block', function ( $block_content, $block ) { $replaced = array( 'core/comments', 'core/post-comments' ); if ( isset( $block['blockName'] ) && in_array( $block['blockName'], $replaced, true ) && livere_comments_is_enabled_for() ) { return livere_comments_get_markup(); } return $block_content; }, 10, 2 ); add_action( 'wp_enqueue_scripts', function () { if ( ! is_singular() || ! livere_comments_is_enabled_for( get_queried_object_id() ) ) { return; } /** * Filters the LiveRe widget script URL. * * @param string $url Script URL. */ $src = apply_filters( 'livere_comments_script_url', LIVERE_COMMENTS_SCRIPT_URL ); wp_enqueue_script_module( 'livere-comments-widget', esc_url_raw( $src ), array(), null ); } ); if ( is_admin() ) { require_once plugin_dir_path( __FILE__ ) . 'includes/admin.php'; }