Changed it so you can only nest on comments with an ID, which means old
comments can't be replied to. OH, I reverted the change from using CompiledYamlFile because I don't like how the RocketTheme Yaml dump is setup, it breaks comments up onto multiple lines. Thus, I also removed the reply button on those comments. You can still attempt to reply to a comment if you're hacking around the frontend, but as long as the comment doesn't have an ID it shouldn't work. TODO: Make the backend comment panel work again.
This commit is contained in:
		
							parent
							
								
									be0eca0ff5
								
							
						
					
					
						commit
						4698549b88
					
				
					 2 changed files with 48 additions and 59 deletions
				
			
		
							
								
								
									
										105
									
								
								comments.php
									
										
									
									
									
								
							
							
						
						
									
										105
									
								
								comments.php
									
										
									
									
									
								
							| 
						 | 
					@ -3,7 +3,7 @@ namespace Grav\Plugin;
 | 
				
			||||||
use Grav\Common\Blueprint;
 | 
					use Grav\Common\Blueprint;
 | 
				
			||||||
use Grav\Common\Blueprints;
 | 
					use Grav\Common\Blueprints;
 | 
				
			||||||
use Grav\Common\BlueprintSchema;
 | 
					use Grav\Common\BlueprintSchema;
 | 
				
			||||||
use Grav\Common\File\CompiledYamlFile;
 | 
					//use Grav\Common\File\CompiledYamlFile;
 | 
				
			||||||
use Grav\Common\Filesystem\Folder;
 | 
					use Grav\Common\Filesystem\Folder;
 | 
				
			||||||
use Grav\Common\Filesystem\RecursiveFolderFilterIterator;
 | 
					use Grav\Common\Filesystem\RecursiveFolderFilterIterator;
 | 
				
			||||||
use Grav\Common\GPM\GPM;
 | 
					use Grav\Common\GPM\GPM;
 | 
				
			||||||
| 
						 | 
					@ -12,6 +12,7 @@ use Grav\Common\Page\Page;
 | 
				
			||||||
use Grav\Common\Page\Pages;
 | 
					use Grav\Common\Page\Pages;
 | 
				
			||||||
use Grav\Common\Plugin;
 | 
					use Grav\Common\Plugin;
 | 
				
			||||||
use Grav\Common\Utils;
 | 
					use Grav\Common\Utils;
 | 
				
			||||||
 | 
					use RocketTheme\Toolbox\File\File;
 | 
				
			||||||
use RocketTheme\Toolbox\Event\Event;
 | 
					use RocketTheme\Toolbox\Event\Event;
 | 
				
			||||||
use Symfony\Component\Yaml\Yaml;
 | 
					use Symfony\Component\Yaml\Yaml;
 | 
				
			||||||
use Twig_SimpleFunction;
 | 
					use Twig_SimpleFunction;
 | 
				
			||||||
| 
						 | 
					@ -284,9 +285,11 @@ class CommentsPlugin extends Plugin {
 | 
				
			||||||
        /** store comments with page **/
 | 
					        /** store comments with page **/
 | 
				
			||||||
        /******************************/
 | 
					        /******************************/
 | 
				
			||||||
        $localfilename = $path . '/comments.yaml';
 | 
					        $localfilename = $path . '/comments.yaml';
 | 
				
			||||||
        $localfile = CompiledYamlFile::instance($localfilename);
 | 
					        //$localfile = CompiledYamlFile::instance($localfilename);
 | 
				
			||||||
 | 
					        $localfile = File::instance($localfilename);
 | 
				
			||||||
        if (file_exists($localfilename)) {
 | 
					        if (file_exists($localfilename)) {
 | 
				
			||||||
            $data = $localfile->content();
 | 
					            //$data = $localfile->content();
 | 
				
			||||||
 | 
					            $data = Yaml::parse($localfile->content());
 | 
				
			||||||
            if (isset($data['comments']) && is_array($data['comments'])) {
 | 
					            if (isset($data['comments']) && is_array($data['comments'])) {
 | 
				
			||||||
                foreach ($data['comments'] as $key => $comment) {
 | 
					                foreach ($data['comments'] as $key => $comment) {
 | 
				
			||||||
                    if (!empty($comment['parent']) && $comment['parent'] == $id) {
 | 
					                    if (!empty($comment['parent']) && $comment['parent'] == $id) {
 | 
				
			||||||
| 
						 | 
					@ -300,7 +303,8 @@ class CommentsPlugin extends Plugin {
 | 
				
			||||||
                        //reason: could be possible that "deleted" already exists (e.g. false or '') in $comment which would overwrite the first (newly added) occurence
 | 
					                        //reason: could be possible that "deleted" already exists (e.g. false or '') in $comment which would overwrite the first (newly added) occurence
 | 
				
			||||||
                        $data['comments'][$key]['deleted'] = $date;
 | 
					                        $data['comments'][$key]['deleted'] = $date;
 | 
				
			||||||
                        //no need to look further as ids are supposed to be unique.
 | 
					                        //no need to look further as ids are supposed to be unique.
 | 
				
			||||||
                        $localfile->save($data);
 | 
					                        //$localfile->save($data);
 | 
				
			||||||
 | 
					                        $localfile->save(Yaml::dump($data));
 | 
				
			||||||
                        $entry_removed = false;
 | 
					                        $entry_removed = false;
 | 
				
			||||||
                        $reply_id = empty($comment['id']) ? '' : $comment['id'];
 | 
					                        $reply_id = empty($comment['id']) ? '' : $comment['id'];
 | 
				
			||||||
                        $message = "Found active reply ($reply_id) for selected comment ($id).";
 | 
					                        $message = "Found active reply ($reply_id) for selected comment ($id).";
 | 
				
			||||||
| 
						 | 
					@ -316,7 +320,8 @@ class CommentsPlugin extends Plugin {
 | 
				
			||||||
                        //reason: could be possible that "deleted" already exists (e.g. false or '') in $comment which would overwrite the first (newly added) occurence
 | 
					                        //reason: could be possible that "deleted" already exists (e.g. false or '') in $comment which would overwrite the first (newly added) occurence
 | 
				
			||||||
                        $data['comments'][$key]['deleted'] = $date;
 | 
					                        $data['comments'][$key]['deleted'] = $date;
 | 
				
			||||||
                        //no need to look further as ids are supposed to be unique.
 | 
					                        //no need to look further as ids are supposed to be unique.
 | 
				
			||||||
                        $localfile->save($data);
 | 
					                        //$localfile->save($data);
 | 
				
			||||||
 | 
					                        $localfile->save(Yaml::dump($data));
 | 
				
			||||||
                        $entry_removed = true;
 | 
					                        $entry_removed = true;
 | 
				
			||||||
                        $message = "Deleted comment ($id) via path ($path)";
 | 
					                        $message = "Deleted comment ($id) via path ($path)";
 | 
				
			||||||
                        break;
 | 
					                        break;
 | 
				
			||||||
| 
						 | 
					@ -326,31 +331,6 @@ class CommentsPlugin extends Plugin {
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            //nothing
 | 
					            //nothing
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        /**********************************/
 | 
					 | 
				
			||||||
        /** store comments in index file **/
 | 
					 | 
				
			||||||
        /**********************************/
 | 
					 | 
				
			||||||
        $indexfilename = DATA_DIR . 'comments/index.yaml';
 | 
					 | 
				
			||||||
        $indexfile = CompiledYamlFile::instance($indexfilename);
 | 
					 | 
				
			||||||
        if (file_exists($indexfilename)) {
 | 
					 | 
				
			||||||
            $dataIndex = $indexfile->content();
 | 
					 | 
				
			||||||
            if (isset($dataIndex['comments']) && is_array($dataIndex['comments'])) {
 | 
					 | 
				
			||||||
                foreach ($dataIndex['comments'] as $key => $comment) {
 | 
					 | 
				
			||||||
                    if (!empty($comment['page']) && !empty($comment['id']) && $comment['page'] == $route && $comment['id'] == $id) {
 | 
					 | 
				
			||||||
                        //add deleted as first item in array (better readability in file)
 | 
					 | 
				
			||||||
                        $dataIndex['comments'][$key] = array_merge(array('deleted' => ''), $comment);
 | 
					 | 
				
			||||||
                        //set date after merge
 | 
					 | 
				
			||||||
                        //reason: could be possible that "deleted" already exists (e.g. false or '') in $comment which would overwrite the first (newly added) occurence
 | 
					 | 
				
			||||||
                        $dataIndex['comments'][$key]['deleted'] = $date;
 | 
					 | 
				
			||||||
                        //no need to look further as ids are supposed to be unique.
 | 
					 | 
				
			||||||
                        $indexfile->save($dataIndex);
 | 
					 | 
				
			||||||
                        break;
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            //nothing
 | 
					 | 
				
			||||||
            
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        //clear cache
 | 
					        //clear cache
 | 
				
			||||||
        $this->grav['cache']->delete($this->comments_cache_id);
 | 
					        $this->grav['cache']->delete($this->comments_cache_id);
 | 
				
			||||||
| 
						 | 
					@ -392,29 +372,25 @@ class CommentsPlugin extends Plugin {
 | 
				
			||||||
        /** store comments with page **/
 | 
					        /** store comments with page **/
 | 
				
			||||||
        /******************************/
 | 
					        /******************************/
 | 
				
			||||||
        $localfilename = $path . '/comments.yaml';
 | 
					        $localfilename = $path . '/comments.yaml';
 | 
				
			||||||
        $localfile = CompiledYamlFile::instance($localfilename);
 | 
					        //$localfile = CompiledYamlFile::instance($localfilename);
 | 
				
			||||||
 | 
					        $localfile = File::instance($localfilename);
 | 
				
			||||||
        if (file_exists($localfilename)) {
 | 
					        if (file_exists($localfilename)) {
 | 
				
			||||||
            $data = $localfile->content();
 | 
					            //$data = $localfile->content();
 | 
				
			||||||
            $data['autoincrement']++;
 | 
					            $data = Yaml::parse($localfile->content());
 | 
				
			||||||
 | 
					            if (isset($data['autoincrement'])) {
 | 
				
			||||||
 | 
					                $data['autoincrement']++;
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                $data['autoincrement'] = max( sizeof($data['comments']), 1 );
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            $data = array('autoincrement' => 1, 'comments' => array());
 | 
					            $data = array('autoincrement' => 1, 'comments' => array());
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        $localid = $data['autoincrement'];
 | 
					        $localid = $data['autoincrement'];
 | 
				
			||||||
        $newComment = ['id' => $data['autoincrement'], 'ip' => $ip, 'parent' => $parent, 'lang' => $lang, 'text' => $text, 'date' => $date, 'author' => $name, 'email' => $email, 'site' => $site, 'user' => $user, 'approved' => $approved, 'isAdmin' => !empty($isAdmin), ];
 | 
					        $newComment = ['id' => $data['autoincrement'], 'ip' => $ip, 'parent' => $parent, 'lang' => $lang, 'text' => $text, 'date' => $date, 'author' => $name, 'email' => $email, 'site' => $site, 'user' => $user, 'approved' => $approved, 'isAdmin' => !empty($isAdmin) ];
 | 
				
			||||||
        $data['comments'][] = $newComment;
 | 
					        $data['comments'][] = $newComment;
 | 
				
			||||||
        $localfile->save($data);
 | 
					        //$localfile->save($data);
 | 
				
			||||||
        /**********************************/
 | 
					        $localfile->save(Yaml::dump($data));
 | 
				
			||||||
        /** store comments in index file **/
 | 
					 | 
				
			||||||
        /**********************************/
 | 
					 | 
				
			||||||
        $indexfilename = DATA_DIR . 'comments/index.yaml';
 | 
					 | 
				
			||||||
        $indexfile = CompiledYamlFile::instance($indexfilename);
 | 
					 | 
				
			||||||
        if (file_exists($indexfilename)) {
 | 
					 | 
				
			||||||
            $data = $indexfile->content();
 | 
					 | 
				
			||||||
        } else {
 | 
					 | 
				
			||||||
            $data = array('comments' => array());
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        $data['comments'][] = ['page' => $route, 'id' => $localid, 'parent' => $parent, 'lang' => $lang, 'text' => $text, 'date' => $date, 'author' => $name, 'email' => $email, 'site' => $site, 'approved' => $approved, ];
 | 
					 | 
				
			||||||
        $indexfile->save($data);
 | 
					 | 
				
			||||||
        //clear cache, don't let incoming spam thrash the cache.
 | 
					        //clear cache, don't let incoming spam thrash the cache.
 | 
				
			||||||
        if ($approved == 'true') {
 | 
					        if ($approved == 'true') {
 | 
				
			||||||
            $this->grav['cache']->delete($this->comments_cache_id);
 | 
					            $this->grav['cache']->delete($this->comments_cache_id);
 | 
				
			||||||
| 
						 | 
					@ -479,8 +455,10 @@ class CommentsPlugin extends Plugin {
 | 
				
			||||||
                if (!empty($paths[str_replace('/', '\\', $fileFolder) ])) $route = $paths[str_replace('/', '\\', $fileFolder) ];
 | 
					                if (!empty($paths[str_replace('/', '\\', $fileFolder) ])) $route = $paths[str_replace('/', '\\', $fileFolder) ];
 | 
				
			||||||
                if (!empty($paths[str_replace('\\', '/', $fileFolder) ])) $route = $paths[str_replace('\\', '/', $fileFolder) ];
 | 
					                if (!empty($paths[str_replace('\\', '/', $fileFolder) ])) $route = $paths[str_replace('\\', '/', $fileFolder) ];
 | 
				
			||||||
                $page_stats[$filepath] = array('active_entries' => 0, 'deleted_entries' => 0, 'active_comments' => 0, 'deleted_comments' => 0, 'active_replies' => 0, 'deleted_replies' => 0, 'latest_active_entry' => 0, 'route' => $route,);
 | 
					                $page_stats[$filepath] = array('active_entries' => 0, 'deleted_entries' => 0, 'active_comments' => 0, 'deleted_comments' => 0, 'active_replies' => 0, 'deleted_replies' => 0, 'latest_active_entry' => 0, 'route' => $route,);
 | 
				
			||||||
                $localfile = CompiledYamlFile::instance($filepath);
 | 
					                //$localfile = CompiledYamlFile::instance($filepath);
 | 
				
			||||||
                $localcomments = $localfile->content();
 | 
					                $localfile = File::instance($filepath);
 | 
				
			||||||
 | 
					                //$localcomments = $localfile->content();
 | 
				
			||||||
 | 
					                $localcomments = Yaml::parse($localfile->content());
 | 
				
			||||||
                if (!empty($localcomments['comments']) && is_array($localcomments['comments'])) {
 | 
					                if (!empty($localcomments['comments']) && is_array($localcomments['comments'])) {
 | 
				
			||||||
                    foreach ($localcomments['comments'] as $comment) {
 | 
					                    foreach ($localcomments['comments'] as $comment) {
 | 
				
			||||||
                        if (!empty($comment['deleted'])) {
 | 
					                        if (!empty($comment['deleted'])) {
 | 
				
			||||||
| 
						 | 
					@ -538,7 +516,8 @@ class CommentsPlugin extends Plugin {
 | 
				
			||||||
    private function getFilesOrderedByModifiedDate($path = '') {
 | 
					    private function getFilesOrderedByModifiedDate($path = '') {
 | 
				
			||||||
        $files = [];
 | 
					        $files = [];
 | 
				
			||||||
        if (!$path) {
 | 
					        if (!$path) {
 | 
				
			||||||
            $path = DATA_DIR . 'comments';
 | 
					            //$path = DATA_DIR . 'comments';
 | 
				
			||||||
 | 
					            $path = $this->grav['page']->path() . '/comments.yaml';
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        if (!file_exists($path)) {
 | 
					        if (!file_exists($path)) {
 | 
				
			||||||
            Folder::mkdir($path);
 | 
					            Folder::mkdir($path);
 | 
				
			||||||
| 
						 | 
					@ -605,9 +584,10 @@ class CommentsPlugin extends Plugin {
 | 
				
			||||||
            return $comments;
 | 
					            return $comments;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        $lang = $this->grav['language']->getLanguage();
 | 
					        $lang = $this->grav['language']->getLanguage();
 | 
				
			||||||
        $filename = $lang ? '/' . $lang : '';
 | 
					        //$filename = $lang ? '/' . $lang : '';
 | 
				
			||||||
        $filename.= $this->grav['uri']->path() . '.yaml';
 | 
					        //$filename.= $this->grav['uri']->path() . '.yaml';
 | 
				
			||||||
        $comments = $this->getDataFromFilename($filename) ['comments'];
 | 
					        $filename = $this->grav['page']->path() . '/comments.yaml';
 | 
				
			||||||
 | 
					        $comments = $this->getDataFromFilename($filename)['comments'];
 | 
				
			||||||
        $comments = $this->setCommentLevels($comments);
 | 
					        $comments = $this->setCommentLevels($comments);
 | 
				
			||||||
        //save to cache if enabled
 | 
					        //save to cache if enabled
 | 
				
			||||||
        $cache->save($this->comments_cache_id, $comments);
 | 
					        $cache->save($this->comments_cache_id, $comments);
 | 
				
			||||||
| 
						 | 
					@ -621,15 +601,22 @@ class CommentsPlugin extends Plugin {
 | 
				
			||||||
            return $comments;
 | 
					            return $comments;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        $levelsflat = array();
 | 
					        $levelsflat = array();
 | 
				
			||||||
 | 
					        $commentId = 0;
 | 
				
			||||||
        foreach ($comments as $key => $comment) {
 | 
					        foreach ($comments as $key => $comment) {
 | 
				
			||||||
 | 
					            $commentId += 1;
 | 
				
			||||||
            if (!empty($comment['deleted'])) {
 | 
					            if (!empty($comment['deleted'])) {
 | 
				
			||||||
                //if field "deleted" exists and is filled with a true value then ignore the comment completely.
 | 
					                //if field "deleted" exists and is filled with a true value then ignore the comment completely.
 | 
				
			||||||
                //TODO: This only works on this position as long as it is forbidden to delete comments that have active replies (children).
 | 
					                //TODO: This only works on this position as long as it is forbidden to delete comments that have active replies (children).
 | 
				
			||||||
                //      Otherwise implement that children get the deleted flag recursively or are ignored via Comment class.
 | 
					                //      Otherwise implement that children get the deleted flag recursively or are ignored via Comment class.
 | 
				
			||||||
                
 | 
					                
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                $levelsflat[$comment['id']]['parent'] = $comment['parent'];
 | 
					                if (isset($comment['id'])) {
 | 
				
			||||||
                $levelsflat[$comment['id']]['class'] = new Comment($comment['id'], $comments[$key]);
 | 
					                    $levelsflat[$comment['id']]['parent'] = $comment['parent'];
 | 
				
			||||||
 | 
					                    $levelsflat[$comment['id']]['class'] = new Comment($comment['id'], $comments[$key]);
 | 
				
			||||||
 | 
					                } else {
 | 
				
			||||||
 | 
					                    $levelsflat[$commentId]['parent'] = $comment['parent'];
 | 
				
			||||||
 | 
					                    $levelsflat[$commentId]['class'] = new Comment($commentId, $comments[$key]);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        //get starting points (entries without valid parent = root element)
 | 
					        //get starting points (entries without valid parent = root element)
 | 
				
			||||||
| 
						 | 
					@ -667,7 +654,7 @@ class CommentsPlugin extends Plugin {
 | 
				
			||||||
        $lang = $this->grav['language']->getLanguage();
 | 
					        $lang = $this->grav['language']->getLanguage();
 | 
				
			||||||
        $filename = $lang ? '/' . $lang : '';
 | 
					        $filename = $lang ? '/' . $lang : '';
 | 
				
			||||||
        $filename.= $this->grav['uri']->path() . '.yaml';
 | 
					        $filename.= $this->grav['uri']->path() . '.yaml';
 | 
				
			||||||
        $pingbacks = $this->getDataFromFilenameOld($filename) ['pingbacks'];
 | 
					        $pingbacks = $this->getDataFromFilenameOld($filename)['pingbacks'];
 | 
				
			||||||
        //save to cache if enabled
 | 
					        //save to cache if enabled
 | 
				
			||||||
        $cache->save($this->pingbacks_cache_id, $pingbacks);
 | 
					        $cache->save($this->pingbacks_cache_id, $pingbacks);
 | 
				
			||||||
        return $pingbacks;
 | 
					        return $pingbacks;
 | 
				
			||||||
| 
						 | 
					@ -691,12 +678,14 @@ class CommentsPlugin extends Plugin {
 | 
				
			||||||
        //Single item details
 | 
					        //Single item details
 | 
				
			||||||
        //$fileInstance = CompiledYamlFile::instance(DATA_DIR . 'comments/' . $fileRoute);
 | 
					        //$fileInstance = CompiledYamlFile::instance(DATA_DIR . 'comments/' . $fileRoute);
 | 
				
			||||||
        //Use comment file in page folder
 | 
					        //Use comment file in page folder
 | 
				
			||||||
        $fileInstance = CompiledYamlFile::instance($this->grav['page']->path() . '/comments.yaml');
 | 
					        //$fileInstance = CompiledYamlFile::instance($this->grav['page']->path() . '/comments.yaml');
 | 
				
			||||||
 | 
					        $fileInstance = File::instance($this->grav['page']->path() . '/comments.yaml');
 | 
				
			||||||
        if (!$fileInstance->content()) {
 | 
					        if (!$fileInstance->content()) {
 | 
				
			||||||
            //Item not found
 | 
					            //Item not found
 | 
				
			||||||
            return;
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return $fileInstance->content();
 | 
					        //return $fileInstance->content();
 | 
				
			||||||
 | 
					        return Yaml::parse($fileInstance->content());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    private function getDataFromFilenameOld($fileRoute) {
 | 
					    private function getDataFromFilenameOld($fileRoute) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -48,7 +48,7 @@
 | 
				
			||||||
									{{nested}}
 | 
														{{nested}}
 | 
				
			||||||
									<div class="comment-footer">
 | 
														<div class="comment-footer">
 | 
				
			||||||
										<span class="comment-reply">
 | 
															<span class="comment-reply">
 | 
				
			||||||
											{% if grav.twig.commenting_enabled %}
 | 
																{% if grav.twig.commenting_enabled and comment.id %}
 | 
				
			||||||
												<a class="comment-add-reply" href="#"><i class="fa fa-reply" title="{{'PLUGIN_COMMENTS.ADD_REPLY'|t}}"></i> {{'PLUGIN_COMMENTS.REPLY'|t}}</a>
 | 
																	<a class="comment-add-reply" href="#"><i class="fa fa-reply" title="{{'PLUGIN_COMMENTS.ADD_REPLY'|t}}"></i> {{'PLUGIN_COMMENTS.REPLY'|t}}</a>
 | 
				
			||||||
											{% endif %}
 | 
																{% endif %}
 | 
				
			||||||
											{% if grav.user.access.admin.super %}
 | 
																{% if grav.user.access.admin.super %}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue