Need help with the share content plugin and adding userpoints for sharing.

Hi all.

I've modified "addthis_share" plugin for my purpose.

mod/addthis_share/start.php:

<?php

elgg_register_event_handler('init', 'system', 'addthis_share_init');

    function addthis_share_init()
        {
        elgg_extend_view('object/elements/summary', 'addthis_share/addthis');
        }
?>

And mod/addthis_share/views/default/addthis_share/addthis.php:

 

<?php
   $full = elgg_extract('full_view', $vars, FALSE);
   $context =  elgg_get_context();

if ($context != 'thewire' && $context != 'widgets' && $context != 'blog' && $context != 'groups' && $context != 'friends' && $context != 'videos' && $context != 'bookmarks')  {
echo '<br><div align="center"><table><tr><td class="ya_share"><div align="center" class="ya-share2" data-services="vkontakte,facebook,odnoklassniki,moimir,twitter,lj,viber,whatsapp,skype,telegram" data-title="Relax Time!" data-description="Relax time!" ></div></td></tr></table></div>';
echo '<script src="//yastatic.net/share2/share.js" charset="utf-8" async="async"></script><br>';
} ?>

I want an userpoints (other plugin) will be adding to users for share content of my site.

Author of userpoints plugin are busy irl atm and can't help with that in the nearest days. But he gave me an explanation how to do it:

The main thing to get working is not the handling of userpoints as such. This could be implemented easily if there would be any kind of feedback available (as in terms of an Elgg plugin hook getting triggered) when something gets shared. The implementation of the triggering of the plugin hook is the tricky part. For this to work, the Javascript code of Addthis would have to be loaded in a different way, i.e. not just with a <script> tag but using elgg_define_js to allow for loading it in the right order together with Elgg's elgg.js library to have the Elgg JS API functions available. The Addthis code has event listeners. These can be used to trigger an Elgg plugin hook which in turn can be used to trigger the awarding of userpoints.

I'm not using exactly "AddThis service" for sharing content, it can be seen above from code.

In documentation of that service:

1. Add script block:

<script src="https://yastatic.net/share2/share.js"></script>

2. Call method "Ya_share2":

var share = Ya.share2('my-share', {
    hooks: {
        onshare: function () {
            alert('Button pressed');
        }
    }
});

Or

var myShare = document.getElementById('my-share');
var share = Ya.share2(my-share, {
    hooks: {
        onshare: function () {
            alert('Button pressed');
        }
    }
});

What and where I need to write for making it work? If I understand correctly- need to write elgg_register_plugin_hook_handler in the init function, but I don't understand how, especially with JS.

Thanks in advance.

  • Use Console in browser to inspect errors (Ctrl+Shift+I)
    

    What to do there?

    "share.js" from yastatic.net is loaded. No errors in console.

    "ya-share.js" is not loaded.

    It seems some typo somewhere... Especially in JS.

  • In

    mod/addthis_share/views/default/addthis_share/addthis.php

    Add

    elgg_require_js('ya-share.js');

    Again, read the docs.

  • It said- wrong syntax.

    Error: Script error for "ya-share.js" http://requirejs.org/docs/errors.html#scripterror

     

    cat ya-share.js

    
    define(function (require) {
            var elgg = require('elgg');
            var $ = require('jquery');
            //var Ajax = require('elgg/Ajax');
            //      var Ya.share2 = require('share.js');
    
             var myShare = document.getElementById('ya-share');
        //   var ajax = new Ajax();
            var share = Ya.share2('myShare', {
                    hooks: {
                        onready: function () {
                        alert('OK');
                                          }
                                              }
    });
    });

    I wrote "onready" hook for while, it's possible. It will call function when plugin is loaded.

    Yes, docs, I know :). Now asking for help if possible.

  • Thanks, RvR. Seems it will solve the problem. Will test tomorrow and will write here.

  • cat YanShare.js:

     

    define(function (require) {
        var elgg = require('elgg');
        var $ = require('jquery');
        var Ajax = require('elgg/Ajax');
    
        var ajax = new Ajax();
    
        var myShare = document.getElementById('YanShare');
    
        var share = Ya.share2('myShare', {
            hooks: {
                onready: function () {
                    ajax.action('points/share').done(function (output) {
                        elgg.system_message(output.system_messages.success);
                    });
                }
            }
        });
    
        var test123 = function () { alert("123321"); };
    });
    
    

    cat start.php:

    <?php
    
    elgg_register_event_handler('init', 'system', 'addthis_share_init');
    
        function addthis_share_init()
            {
            elgg_extend_view('object/elements/summary', 'addthis_share/addthis');
            elgg_register_action('points/share', __DIR__ . '/actions/points/share.php', 'logged_in');
    
            elgg_register_js('share', 'https://yastatic.net/share2/share.js');
            elgg_load_js('share');
    
            elgg_define_js('YanShare', array(
            'src' => '/mod/addthis_share/views/default/js/YanShare.js',
            ));
    
            };
    ?>

    cat addthis.php:

    <?php
        elgg_require_js('YanShare');
        $full = elgg_extract('full_view', $vars, FALSE);
        $context =  elgg_get_context();
    
    if ($context != 'thewire' && $context != 'widgets' && $context != 'blog' && $context != 'groups' && $context != 'friends' && $context != 'videos' && $context != 'bookmarks')  {
    
    echo '<br><div align="center"><table><tr><td class="ya_share"><div align="center" class="ya-share2" data-services="vkontakte,facebook,odnoklassniki,moimir,twitter,lj,viber,whatsapp,skype,telegram"></div></td></tr></table></div>';
    
    } ?>

     

    No errors in browser and i can see that YanShare.js is loaded. But nothing happened after loading page, even JS alert. Of course i gave permit to browser to not block popups.

     

  • I made another JS-file "sharescript.js" and put there define with "var YanShare = require('YanShare');" from manual that RvR gave. Also, i wrote in sharescript.js simple JS alert function.

    In "addthis.php" (in the view) I wrote "elgg_require_js(sharescript);" (from manual). And wrote in the view simple code <button onclick=name_of_function_wth_alert();>Click</button>. That's loaded without errors (in browsers console) but no alert window was openned after clicking on button. "Sharescript" was loaded (seen in console).

    I did "chown apache. *" and "chmod +x *" inside JS directory.

    Can anyone help with that functionality please?

  • Add in start.php directly:

    elgg_require_js(sharescript);

    Without any

    elgg_define_js()

  •  ReferenceError: require is not defined

  • Ok, once more.

    YanShare.js:

    define(function (require) {
            var $ = require('jquery');
            var elgg = require('elgg');
            var Ajax = require('elgg/Ajax');
            var ajax = new Ajax();
    
            var myShare = document.getElementById('yashare');
            var share = Ya.share2('yashare', {
                    hooks: {
                    onready: function () {
                    alert('Button pressed');
                    }
        }
    });
    });

    sharescript.js:

    define(function (require) {
            var $ = require('jquery');
            var YanShare = require('YanShare');
    function myFunction() {
      alert("Hello! I am an alert box!");
      }
    
    });

    addthis.php:

    <?php
        elgg_require_js('sharescript');
        $full = elgg_extract('full_view', $vars, FALSE);
        $context =  elgg_get_context();
    
    if ($context != 'thewire' && $context != 'widgets' && $context != 'blog' && $context != 'groups' && $context != 'friends' && $context != 'videos' && $context != 'bookmarks')  {
    
    echo '<button onclick="myFunction()">Button</button>';
    
    echo '<br><div align="center"><table><tr><td class="ya_share"><div align="center" class="ya-share2" data-services="vkontakte,facebook,odnoklassniki,moimir,twitter,lj,viber,whatsapp,skype,telegram"></div></td></tr></table></div>';

    start.php:

    <?php
    
    elgg_register_event_handler('init', 'system', 'addthis_share_init');
    
        function addthis_share_init()
            {
            elgg_extend_view('object/elements/summary', 'addthis_share/addthis');
            elgg_register_action('points/share', __DIR__ . '/actions/points/share.php', 'logged_in');
    
            elgg_register_js('share', 'https://yastatic.net/share2/share.js');
            elgg_load_js('share');
    
    //      elgg_register_js('YanShare', '/mod/addthis_share/views/default/js/YanShare.js');
    //      elgg_load_js('YanShare');
    
    //      elgg_require_js(sharescript);
    
            elgg_define_js('YanShare', array(
            'src' => '/mod/addthis_share/views/default/js/YanShare.js',
            ));
    
            elgg_define_js('sharescript', array(
            'src' => '/mod/addthis_share/views/default/js/sharescript.js',
            ));
    
            };
    ?>

     

    Tried with and without defining "sharescript" in start.php.