Users running a non-download version of Vanilla (pulled from github), on branch release/2019.016 or master from the last 2 weeks should upgrade to release/2019.017 or latest master for security reasons. Downloaded official open sources releases are not affected.
Please upgrade here. These earlier versions are no longer being updated and have security issues.

I can't get PremHide to work

This discussion is related to the PremHide addon.
24

Comments

  • peregrineperegrine MVP
    edited June 2013

    @adriansolnine

    try this hidden with [prem][/prem] shortcodes.',

    <?php 
    
    $PluginInfo['PremHide'] = array(
       'Description' => 'This plugin allows you to insert Premium Content which will be hidden with [prem][/prem] shortcodes.',
       'Version' => '1.0.1',
       'Author' => "Adrian Speyer",
       'AuthorUrl' => 'http://www.adrianspeyer.com'
    );
    
    class PremHidePlugin extends Gdn_Plugin {
    
    
    public function DiscussionController_AfterCommentFormat_Handler($Sender) {
    
         $Session = Gdn::Session();
        if (!($Session->IsValid())) {
          $LoginStr = "<p> Please <a href='".SignInUrl($this->SelfUrl)."'>Login </a>To See Premium Content </p>";
        $pattern = '#\[prem\](.*?)\[\/prem\]#si';
        $body = $Sender->EventArguments['Object']->Body;
        $context = preg_replace($pattern, $LoginStr, $body);   
        $Sender->EventArguments['Object']->FormatBody = $context;
        }
    }
    public function PostController_AfterCommentFormat_Handler($Sender) {
            $this->DiscussionController_AfterCommentFormat_Handler($Sender);
        }
    
    public function Setup() {
    
        }   
    }
    

    the next logical step would be to add role based hiding. e.g. if not a certain role remove it.

    note there is no &$Sender - use $Sender as mentioned previously
    also don't close your php scripts at the bottom ?> it is not needed and not necessary (x00 previous tip to me) and can cause problems with rss feeds. etc.

    internationalized

          $LoginStr = "<p>" . T("Please") .  " <a href='" . SignInUrl($this->SelfUrl) . "'> " . T("Login") .  "</a> " . T("To See Premium Content") . "</p>";
    
    

    then you can use

    $Definition['Login'] = 'Login in language';
    $Definition['Please'] = 'Bitte';
    $Definition['To See Premium Content'] = 'To See Hidden Content';

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

    Adrianvrijvlinder
  • AdrianAdrian Wandering Spirit Montreal Vanilla Staff

    @Peregrine looks awesome. I kept instead of [prem] so users that had it before can still use it. I also don't like the shortcode showing on the front end. It looks cleaner to me. Thanks a million for your time and effort. I think this add-on is very helpful for all. It would not have been possible without your help, or the ideas and effort from @x00 and @businessdad

    thanks all. a real learning experience.

    Sharing is caring

    peregrinevrijvlinder
  • businessdadbusinessdad Stealth contributor MVP

    @x00 said:
    you are lucky it worked at all

    /<prem>(.*?</prem>/ is not a valid pattern.

    The original pattern in the plugin version I tested was /<prem>/. The missing bracket is due to a typo from my side, when I wrote my suggestion. Sorry about that.

    Adrian
  • AdrianAdrian Wandering Spirit Montreal Vanilla Staff

    looks to be good now! thanks all

    Sharing is caring

    peregrinevrijvlinder
  • peregrineperegrine MVP
    edited June 2013

    @adriansonline said:
    Peregrine looks awesome. I kept instead of [prem] so users that had it before can still use it. I also don't like the shortcode showing on the front end. It looks cleaner to me. Thanks a million for your time and effort. I think this add-on is very helpful for all. It would not have been possible without your help, or the ideas and effort from x00 and businessdad

    thanks all. a real learning experience.

    you did leave [prem] in the plugin description - not sure if you intended to.

    @x00's ideas and @businessdad's gave me ideas that I wouldn't have thought of.

    The power of many :).

    The reason you probably got the "extra effort" from me is because you are a productive member of the community who wants to learn and wants to share and its a pleasure helping people like you

    .nice idea for a plugin,

    I'm sure it will be of value to many.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

    Adrianvrijvlinder
  • AdrianAdrian Wandering Spirit Montreal Vanilla Staff

    @peregrine thanks for catching my typo and the kind words! It was a team effort which is nice. It also helps me now that I am actually working on an active Vanilla Forum. My earlier forum fell to death due to lack of interest and spammers.

    Sharing is caring

  • peregrineperegrine MVP
    edited June 2013

    one more idea if you want.... then you could add some css to the class if you wanted.

     $context = "";
        $Info = "";
        $body = $Sender->EventArguments['Object']->Body;
        $context = preg_replace($pattern, $LoginStr, $body);   
        if ($body != $context){
        $Info = '<p class ="premhide">' . T("Parts of this discussion are not viewable if you are not logged in") . '</p>'; 
        }
        $Sender->EventArguments['Object']->FormatBody = $Info . $context;
    

    or even making the Info line be the url line and making the replacement pattern just

     "<p>" . T("Premium content")
    

    that way if there were tags like

           $context = "this is a test <PREM>hidden xxx  </PREM> shown xxxx <prem> hidden yyyy </prem> shown yyyy";
    

    then the login url would only show once at the top, and the Premium content would show where the prem tags were.

    If you get my drift.

    like so ---

        
    public function DiscussionController_AfterCommentFormat_Handler($Sender) {
        
         $Session = Gdn::Session();
        if (!($Session->IsValid())) {
        $LoginStr = "<p>" . T("Please") .  " <a href='" . SignInUrl($this->SelfUrl) . "'> " . T("Login") .  "</a> " . T("To See Premium Content") . "</p>";
         $pattern = '#\(.*?)\<\/prem\>#si';
        $context = "";
        $replacement = '<p class= "info">' . T("Premium Content deleted") . "</p>";
        $Info = "";
        $body = $Sender->EventArguments['Object']->Body;
        $context = preg_replace($pattern, $replacement, $body);   
        if ($body != $context){
        $Info = $LoginStr;
        }
        $Sender->EventArguments['Object']->FormatBody = $Info . $context;
        }
    }
    
    

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

    vrijvlinder
  • I won't bother you any more with features :).

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • supaloopasupaloopa New
    edited June 2013

    i think i got it to work..

  • It seems to work now. But when you log in to see the hidden text, i can still the tags. For example it looks like this: [prem]Test[/prem]

  • you probably had the wrong pattern or only changed the first < without changing the closing >

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • supaloopasupaloopa New
    edited June 2013

    Theres another problem with the coding. If I enable the plugin, all the text is in one single line.

    For example if I were to list something like:

    1

    2

    3

    4

    5

    By enabling the pluggin, it would like this: 12345

    It doesnt matter if I put the prem tag around it or not..


    Weird, I just tested it again on my forum and it appears if your a Guest, the text is all in 1 line, but when you log in, the original formatting looks fine but you can still the tag code. And yes I did close the tag.

  • peregrineperegrine MVP
    edited June 2013

    @supaloopa said:
    It seems to work now. But when you log in to see the hidden text, i can still the tags. For example it looks like this: [prem]Test[/prem]

    for brackets

       
        public function DiscussionController_AfterCommentFormat_Handler($Sender) {
    
        
         $Session = Gdn::Session();
        if (!($Session->IsValid())) {
        $LoginStr = "<p>" . T("Please") .  " <a href='" . SignInUrl($this->SelfUrl) . "'> " . T("Login") .  "</a> " . T("To See Premium Content") . "</p>";
        $pattern = '#\[prem\](.*?)\[\/prem\]#si';
        $context = "";
        $replacement = '<p class= "info">' . T("Premium Content deleted") . "</p>";
        $Info = "";
        $body = $Sender->EventArguments['Object']->Body;
        $context = preg_replace($pattern, $replacement, $body);   
        if ($body != $context){
        $Info = $LoginStr;
        }
        $Sender->EventArguments['Object']->FormatBody = $Info . $context;
        } else {
         $body = $Sender->EventArguments['Object']->Body;
         $pattern = '#\[.?prem\]#si';
         $context = preg_replace($pattern, "", $body); 
         $Sender->EventArguments['Object']->FormatBody = $Info . $context; 
       }
    }
    

    html tags don't need to be hidden.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • x00x00 MVP
    edited June 2013

    the idea of this:

    $context = preg_replace($pattern, '\1', $context);

    is it strips the tags

    #<prem>(.*?)</prem>#is

    The first parenthesis matches .*?, so \1 will be the result of the first parenthesis

    you could replace it with <div class="PremiumContent">\1</div> that surrounds the premium content with a div.

    grep is your friend.

    peregrine
  • @supaloopa said:
    Weird, I just tested it again on my forum and it appears if your a Guest, the text is all in 1 line, but when you log in, the original formatting looks fine but you can still the tag code. And yes I did close the tag.

    perhaps I'm missing developments, but the point of the original plugin is it was supposed to restrict that content from guests.

    grep is your friend.

  • ^
    ^
    Right, guests can see only "Please login To See Premium Content" so they would have to sign up in order to see the premium content.

  • x00x00 MVP
    edited June 2013

    @peregrine

    It would be dryer if you just use the same pattern, and just change the replace.

    best way to think about it. the deciding factor is $Session->IsValid() all the input are the same, and the result is different based on the deciding factor. So in order to be dry all you need to do is to supply the input data to match the same pattern, and replace based on the session.

    rather than overcomplicated.

    grep is your friend.

  • peregrineperegrine MVP
    edited June 2013

    @x00
    agreed. I know exactly what you mean. I have to run now and can't test... :)

    In any event. the code posted in
    @supaloopa
    http://vanillaforums.org/discussion/comment/184723/#Comment_184723 does indeed work. does not show brackets and hidden text when logged in, and shows link and hides text when not logged in.

    the only other change I had to make was

    href='" . "entry/signin"
    since the signin link did not work for me.

    I will leave the updating and tightening of code to any one who wants to make it tighter by removing duplications of

    $pattern =
    $Sender->EventArguments['Object']->FormatBody = $Info . $context;
    $body = $Sender->EventArguments['Object']->Body;

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • Hi,

    this plugin seems to be working but each posts in my forum turns into a long run on sentence with no return spaces. Can someone fix? Thank you :)

  • ShadowdareShadowdare Moderator

    @shootingtars, would you post a screenshot and does it only turn posts with the prem tags into what you are describing?

    Add Pages to Vanilla with the Basic Pages app

Sign In or Register to comment.