danpros / htmly

Simple and fast databaseless PHP blogging platform, and Flat-File CMS
https://www.htmly.com
GNU General Public License v2.0
1.07k stars 261 forks source link

[FEATURE REQUEST] Disable comments per-post #794

Open mithat opened 3 weeks ago

mithat commented 3 weeks ago

Comments may not be desired on all blog posts.

Assuming you have enabled comments in HTMLy, it would be great if you could disable them for some posts. Disqus (and possibly other comment providers) lets you close comments per-post, but visible code injected by Disqus remains.

Many blogging platforms have a simple checkbox on a post's editing page that lets you disable comments for that post.

mithat commented 2 weeks ago

I have begun to implement a way to disable comments per post using the theme rather than do a core hack.

The approach is similar to how a table of contents is implemented, but rather than add:

<!--toc-->

to a post on which you want a table of contents, you add:

<!--no-comments-->

to posts you don't want to have comments.

You then need to modify the appropriate code in your theme. In twenty-sixteen's post.html.php, I changed:

    <?php if (disqus()): ?>
        <?php echo disqus($p->title, $p->url) ?>
    <?php endif; ?>

    <?php if (disqus_count()): ?>
        <?php echo disqus_count() ?>
    <?php endif; ?>

    <?php if (facebook() || disqus()): ?>
        <div class="comments-area" id="comments">

            <h2 class="comments-title"><?php echo i18n('Comments');?> “<?php echo $p->title;?>”</h2>

            <?php if (facebook()): ?>
                <div class="fb-comments" data-href="<?php echo $p->url ?>" data-numposts="<?php echo config('fb.num') ?>" data-colorscheme="<?php echo config('fb.color') ?>"></div>
            <?php endif; ?>

            <?php if (disqus()): ?>
                <div id="disqus_thread"></div>
            <?php endif; ?>

        </div>
    <?php endif; ?>

to:

    <?php $no_comments = explode('<!--no-comments-->', $post->body); ?>
    <?php if (!isset($no_comments['1'])): ?>
        <?php if (disqus()): ?>
            <?php echo disqus($p->title, $p->url) ?>
        <?php endif; ?>

        <?php if (disqus_count()): ?>
            <?php echo disqus_count() ?>
        <?php endif; ?>

        <?php if (facebook() || disqus()): ?>
            <div class="comments-area" id="comments">

                <h2 class="comments-title"><?php echo i18n('Comments');?> “<?php echo $p->title;?>”</h2>

                <?php if (facebook()): ?>
                    <div class="fb-comments" data-href="<?php echo $p->url ?>" data-numposts="<?php echo config('fb.num') ?>" data-colorscheme="<?php echo config('fb.color') ?>"></div>
                <?php endif; ?>

                <?php if (disqus()): ?>
                    <div id="disqus_thread"></div>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    <?php endif; ?>

Basically, I wrapped the comment insertion code between:

<?php $no_comments = explode('<!--no-comments-->', $post->body); ?>
<?php if (!isset($no_comments['1'])): ?>

and

<?php endif; ?>

So far, this seems to be working fine.

I have yet to address how I want to handle comment counts in the post info and whether I want to add a "Comments have been disabled for this post" notification where comments would normally go. But overall this seems to be a workable approach until (if ever!) the feature is added to the core.

Joduai commented 1 week ago

I'm a bit late to propose some kind of a workaround, but was doing other things with htmly, as you could see in other "issues".

Basically htmly lacks adding some sort of custom fields for content management in admin panel, although such functionality is available for templates. But didn't you consider to base on tags rather than loading whole page's content to check whether it contains a string ? Checking if page's tags contain a #nocomments using get_tag() funct you could either display comments or load 3rd party script, or show a custom notification. Assuming you don't enter a list of tags longer than page content 😅 Of course displaying tags in content would have to be changed not to display certain tags you'd use for functional behavior, but you get the point.

Would be great if @danpros enabled discussions here, or implemented a simple forum on htmly's website to discuss things related to htmly rather than spamming issues section.

danpros commented 1 week ago

@Joduai I have other personal projects that really needs attention so I've been pretty inactive here lately. Already enable the discussion.

KuJoe commented 1 hour ago

I liked the <!--no-comments-> tag idea so I wrote up something quick to add that to the markdown file and added it to the add/edit post pages in the admin area, does something like this work per post or should I change it to a checkbox or other input type?

image

mithat commented 1 hour ago

@KuJoe Since this is something that impacts the content text, I'm wondering whether it might be better to add a "No comments" icon to the text editor toolbar instead. A dropdown or checkbox is nice, but it could lead to the following scenario: User disables comments, which adds the <!--no-comments--> tag to the content. User then later enables comments using the UI -- but the <!--no-comments--> isn't removed from the content (unless you also want to add the code needed to parse the content and remove it).

Putting it in the toolbar makes it behave like the TOC button, which while not ideal is arguably more consistent.

The bigger question is whether the <!--no-comments--> hack should creep toward permanence with any UI enhancements at all. The effort to do it IMHO the right way, with an additional state variable attached to the post, probably isn't that much greater than the hack offered here. I just did it this way so I didn't have to modify the core -- at least until I have a better understanding of how HTMLy works.