How can I properly extend Xenforo_Model_Post::canEditPost

AndyB

Well-known member
My default edit time limit for regular members is 24 hours. What I would like to do is create an add-on which will allow me to override this default value on a per post basis.

If I hack the following code like this it works perfect:

PHP:
    public function canEditPost(array $post, array $thread, array $forum, &$errorPhraseKey = '', array $nodePermissions = null, array $viewingUser = null)
    {
        $this->standardizeViewingUserReferenceForNode($thread['node_id'], $viewingUser, $nodePermissions);
      
        // start hack
      
        if ($viewingUser['user_id'] == 5528 AND $post['post_id'] == 1894195)
        {
            return true;
        }      
          
        // end hack

<snip>

However when I try to extend Xenforo_Model_Post::canEditPost the Edit link appears properly below the post, but clicking the link produces a "You do not have permission to view this page or perform this action." error message.

Here is my add-on code which only partially works:

PHP:
<?php

class Andy_CanEditPost_Model_Post extends XFCP_Andy_CanEditPost_Model_Post
{
    public function canEditPost(array $post, array $thread, array $forum, &$errorPhraseKey = '', array $nodePermissions = null, array $viewingUser = null)
    {      
        // get parent
        $parent = parent::canEditPost($post, $thread, $forum, $errorPhraseKey, $nodePermissions, $viewingUser);
      
        if ($viewingUser['user_id'] == 5528 AND $post['post_id'] == 1894195)
        {
            return true;
        }                 
      
        // return parent
        return $parent;
      
    }
}

?>

Thank you.
 
You must ensure that the viewingUser array is populated.

PHP:
$canEditPost = parent::canEditPost($post, $thread, $forum, $errorPhraseKey, $nodePermissions, $viewingUser);

if (!$canEditPost)
{
    $this->standardizeViewingUserReference($viewingUser);
   
    if ($viewingUser['user_id'] == 5528 AND $post['post_id'] == 1894195)
    {
        $canEditPost = true;
    }
}

return $canEditPost;
 
I know it doesn't make any difference, but I prefer to use the variable $parent instead.

PHP:
<?php

class Andy_CanEditPost_Model_Post extends XFCP_Andy_CanEditPost_Model_Post
{
	public function canEditPost(array $post, array $thread, array $forum, &$errorPhraseKey = '', array $nodePermissions = null, array $viewingUser = null)
	{		
		// get parent
		$parent = parent::canEditPost($post, $thread, $forum, $errorPhraseKey, $nodePermissions, $viewingUser);

		if (!$parent)
		{
			$this->standardizeViewingUserReference($viewingUser);
				
			if ($viewingUser['user_id'] == 5528 AND $post['post_id'] == 1894195)
			{
				$parent = true;
			}			
		}
		
		// return parent
		return $parent;
	}
}

?>
 
Top Bottom