Comments Plugin for Kirby v3

#1

Hey there!

I used https://github.com/Addpixel/KirbyComments, the plugin of Florian in many websites until today. The new Kirby v3 looks gorgeous to me and I installed it. After a not-that-easy process, i got it working - besides the Comments Plugin. It not just doesn’t work, it even breaks the translation stuff.

Does anybody have an idea what’s the main problem of that plugin or can provide me with an actually working Comments Plugin for Kirby v3? Maybe @florianpircher can help me here? :slight_smile:

Greetings,
RastaTux

#2

The way plugins work for Kirby 3 is completely different to Kirby 2, so plugins need updating before they will work. You would need to wait for Florian to update it, or have a go your self, but it is a complex plugin so that may take a while.

#3

I have look into updating the plugin for Kirby 3, but had to conclude that updating the plugin would be more work than I am willing to put into the project. The plugin API has changed significantly with Kirby 3. Updating the plugin with all of its functions and options for the new API would require a complete rewrite of the software and its documentation.

Updating just the core functionality would actually be doable within a reasonable timespan, but then I would have to remove many features and options that are more difficult to translate to the new Kirby 3 architecture. If you are familiar with PHP and the new plugin API, you can look at comments.php of the plugin and try to update only the parts that you need and delete the rest. I am happy to help if you have any questions about the internals of the plugin.

Because comments are saved as subpages, you can write your own snippet for rendering comments. That way you don’t loose the existing comments on your page.

#4

@florianpircher Ugh, yeah I thought so… I’m familiar with PHP, but neither with the new plugin API, nor the old one… But maybe i’ll try to play with it a bit… Thanks for offering your help!

#5

Hey! To get my comments working in Kirby v3 I changed the code by @florianpircher, so the basics work for me now in v3. Feel free to commit merges!

#6

I have a few questions about getting this to work on Kirby 3.

  1. plugins/kirbycomments: The instructions say to add this into the plugins directory, but Kirby 3 doesn’t natively come with a plugins directory. Will adding a directory /site/plugins actually work when adding the ‘kirbycomments’ directory into ‘/site/plugins’?
  2. Where should the following files be placed in a Kirby 3 install: index.php, package.json

I think that is about it for now.

#7

Yes that will work. Kirby does not ship with any plugins, but it’s fine to manually create the folder and manually install plugins into it. The best way is to use composer, but this particualar plugin does not seem to be up for that, yet.

According to the read me, the downloaded files should go in…

site/plugins/kirbycomments/

So that everything goes in that folder, don’t put it in a sub folder.

site/plugins/kirbycomments/index.php
site/plugins/kirbycomments/package.json
... etc

Hope that helps.

#8

Thanks @jimbobrjames – this does get the field to render, and the “preview” to render, but when submitting the comment I get an error. The result of which is here – It seems to be unable to create the comment file. I am wondering if it has to do with one of two issues:

  1. It’s unable to access the YML file because it’s currently in the “plugins” directory (see screengrab).
  2. There is some inability to the plugin to write to the appropriate content directory? Perhaps I have to update the path reference or give the plugin some write permission?

Thanks of course for any help.

Screengrab of my directory structure and of the error.

charles

#9

Hm, looks like $comments_page is not created in the first step. The yaml file is not important in this context. Can the Panel write to the content folder?

#10

Hi @texnixe – if by asking “Can the Panel write to the content folder?” meaning does the panel allow me to publish, it does. You can see where I am with things here if that’s helpful – http://lostarts.xyz/craighton-berman/critique-1

This is the URL that produces the error in the example above, after fully submitting the comment.

#11

Hm, I don’t see how that error message is related to the comments page? The error I see there is caused by calling the avatar() method on a user when there is no user… There should be two if statements here:

<?php if($user = $kirby->user('someuser')) && $avatar = $user->avatar()): ?>
<!-- rest of code -->
<?php endif ?>
#12

Hi @texnixe thanks for digging in. I started investigating before you added the code block above. Is that code block in reference to the comments page ($comments_page) or is this related to the page causing the error “critique.php” (the template for this page)?

btw: I commented out much of the code in my critique.php, including my previous call to $avatar and user, and I’m still recieving the same error… hence the clarifying question above.

Thanks again!

#13

As indicated by the error message, that code comes from the critique.php template.

You can’t use HTML comments to comment out PHP, they have no effect at all… and the error is therefore still there.

#14

Oy! #learning :slight_smile:

Thanks @texnixe I’ll keep trying to figure this out…

#15

You are welcome :slightly_smiling_face:. We are here…

#16

So I’ve gone back and done a couple of things:

Per @texnixe comments I’ve gone through the resulting error (same as the screenshot above), as well as completely deleted majority of the contents of my critique.php template so the only thing that renders is the Header and Footer snippet along with the snippet for Comments.

Unfortunately I get the same error, and so it leads me to wonder, @texnixe are you seeing a different error than what I shared above?

Also in that screenshot error message it’s indicating the error is coming from:
/nfs/c01/h06/mnt/6643/domains/lostarts.xyz/html/site/plugins/kirbycomments/plugin/Comments.php

So perhaps we’re seeing different issues?

#17

No, after the form was online I filled in the form and tried to submit, which then resulted in the same error message your originally posted above.

Somehow the parent folder is not created (the plugin should actually make sure there is a $comments_page before calling the createChild() method, but doesn’t. That would at least prevent the error.

Edit:

Ok, I had a closer look at the plugin code and there are at least three issues:

  1. It doesn’t authenticate before trying to create the $comments_page page.
  2. The code for creating the page is not correct, either (that is still Kirby 2 code, I think)
  3. It doesn’t check if the parent page was successfully created before trying to create the comment children
1 Like
#18

Ha! I might have stumbled (though much more clumbsily) into a similar realization. I manually created a “comments” directory in the ‘content//’ directory and the comments stuck and I didn’t get the error (which I believe pertains to your points #1 and #2 above).

@RastaTux any thoughts based on your updating of the plugin?

Or any suggestions for another commenting option? Ideally something as simple as this one :slight_smile:

#19

Well, those errors above can be fixed like this:

			// No comment was posted on `$this->page` yet; create comments page
			try {
				kirby()->impersonate('kirby');
				$dirname = Comments::option('pages.comments.dirname');
				$template = Comments::option('pages.comments.template');
				$comments_page = $this->page->createChild([
					$dirname,
					$template,
					'content' => [
						'title' => Comments::invokeHook('decide-comments-page-title', array($this->page)),
						'date'  => $now->format('Y-m-d H:i:s')
					]
				]);
			} catch (Exception $e) {
				$this->status = new CommentsStatus(200, $e);
				return $this->status;
			}

And then before calling the second createChild() method for the subpages, add an if statement if the parent exists.

			// Save comment as page
				if ($comments_page) {
						kirby()->impersonate('kirby');
						$comment_page = $comments_page->createChild([
					  	    'slug' => $dirname,
						    'template' => $template,
						    'content' => $contents
						]);
						$comment_page->publish();
				}
#20

I see what you’re doing here… cool. So I’ve gotten this input as you’ve suggested, but a new error has unfolded on line 495 below. Error and code…

Error message:

Parse error: syntax error, unexpected 'public' (T_PUBLIC) in /nfs/c01/h06/mnt/6643/domains/lostarts.xyz/html/site/plugins/kirbycomments/plugin/Comments.php on line 495

Code below:

/**
 * `true` iff no comment is managed by this `Comments` instance.
 *
 * @return bool
 */
public function isEmpty()
{
	return count($this->comments) === 0;
}

I went through to see if all ‘{}’ and ‘[]’ and ‘()’ are properly closed. Perhaps I’m missing one? This error seems to break both the Panel and the site itself.