We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1841
    • 141 Posts
    Hi there, I am attempting to create a plugin that will send a ping to "ping o matic" whenever a page is published in Evo. Please bear in mind I have no PHP knowledge really and have never created a plugin for modx (although I have created a number of snippets).

    The issue is that I don’t know how to put placeholders and Template variables into a plugin, snippets and chunks are easy but plugins seem a different story.

    The code I have is:
    /*
    --------------------------------------------
     $title contains the title of the page you're sending
     $url is the url of the page
     $debug true print out the debug and show xml call and answer
    --------------------------------------------
     the output is an array with two elements:
     status: ok / ko
     msg: the text response from pingomatic
    --------------------------------------------
    */
    function pingomatic($title,$url,$debug=false) {
    global $tmplvars;
        $content='<?xml version="1.0"?>'.
            '<methodCall>'.
            ' <methodName>weblogUpdates.ping</methodName>'.
            '  <params>'.
            '   <param>'.
            '    <value>[+pagetitle+]'</value>'.
            '   </param>'.
            '  <param>'.
            '   <value>[~[+id+]~]</value>'.
            '  </param>'.
            ' </params>'.
            '</methodCall>';
        $headers="POST / HTTP/1.0\r\n".
        "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1) Gecko/20090624 Firefox/3.5 (.NET CLR 3.5.30729)\r\n".
        "Host: rpc.pingomatic.com\r\n".
        "Content-Type: text/xml\r\n".
        "Content-length: ".strlen($content);
        if ($debug) nl2br($headers);
        $request=$headers."\r\n\r\n".$content;
        $response = "";
        $fs=fsockopen('rpc.pingomatic.com',80, $errno, $errstr);
        if ($fs) {
            fwrite ($fs, $request);
            while (!feof($fs)) $response .= fgets($fs);
            if ($debug) echo "<xmp>".$response."</xmp>";
            fclose ($fs);
            preg_match_all("/<(name|value|boolean|string)>(.*)<\/(name|value|boolean|string)>/U",$response,$ar, PREG_PATTERN_ORDER);
            for($i=0;$i<count($ar[2]);$i++) $ar[2][$i]= strip_tags($ar[2][$i]);
            return array('status'=> ( $ar[2][1]==1 ? 'ko' : 'ok' ), 'msg'=>$ar[2][3] );
        } else {
            if ($debug) echo "<xmp>".$errstr." (".$errno.")</xmp>";
            return array('status'=>'ko', 'msg'=>$errstr." (".$errno.")");
        }
    }
    
    


    The "pagetitle" should be the page being published, the "url" the actual page URL. This has been set to function on the System event: "OnDocPublished"

    This code does work outside of Modx but It’s almost impossible to test as I can’t get a responce.

    If we could get this working it would give modx a great edge as pingomatic is the reason Wordpress gets articles published so fast in Google.

    Thanks for your Time.

      • 28042 ☆ A M B ☆
      • 24,524 Posts
      For plugins, you need to use API functions and values instead of the snippet/TV tags. For example,
      <value>$modx->makeUrl($modx->documentIdentifier)</value>

      and
      $modx->documentObject['pagetitle']


      Placeholders [+...+] will not exist unless they have first been set
      $modx->setPlaceholder('name', 'value');

      either in a snippet such as Wayfinder or Ditto. To access a placeholder in code, use
      $modx->getPlaceholder('name');


      The reason why using MODx tags in snippet output can (sometimes) work is that the tags are part of the snippet’s output, and MODx runs the page through the parser several times until it either stops changing or reaches its limit of how many times to parse the page. Since it is possible that the page won’t be parsed again, it’s always a better idea to use API functions in snippet or plugin code.

        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 3749
        • 24,544 Posts
        First, you need to call your function -- it won’t execute unless called.

        You’ll want to connect the plugin to the OnDocPublished event.

        At the end of your code (outside the function), you need to get the arguments, and then call the function -- something like this:

        $url = $modx->makeUrl($docid, '', '', 'full');
        $title=$modx->documentObject['pagetitle'];
        pingomatic($title,$url,$debug=false)


        $docid should be set for you.

        [Update] I see that, once again, Sottwell and I have submitted simultaneous posts. We have to stop meeting like this. smiley
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 1841
          • 141 Posts
          Thankyou both for your help, I have added as you have suggested, just need to figure out how I can tell if it has worked or not.

          If it does work it would be a great feature to add to Modx, it’s how wordpress get’s pages indexed in Google so fast.