diff --git a/README.md b/README.md index 01df2e7..3ab86d5 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +# Docs update in progress ... + # Annotator plugin for Wordpress Adds inline annotations to Wordpress using the amazing @@ -14,7 +16,7 @@ Adds inline annotations to Wordpress using the amazing ## Requirements -PHP >= 5.\*.\* +PHP >= 5.3.\* ## Install @@ -22,8 +24,7 @@ Just `git clone` this project into the `wp-content/plugins/` directory, then activate the plugin through the Wordpress administration panel accessible at `http:///wp-admin/plugins.php`. You will also need to sign up at [AnnotateIt](http://annotateti.org) and -fill your _Account ID_ and _Auth Token_ details in the Plugin settings -form. +fill your _key_ and _secret_ details in the Plugin settings form. ## Demo diff --git a/lib/okfn-annot-factory.php b/lib/okfn-annot-factory.php index 67f4af2..4b358c1 100644 --- a/lib/okfn-annot-factory.php +++ b/lib/okfn-annot-factory.php @@ -1,55 +1,39 @@ settings = $settings; - $this->annotator_content = $settings->get_option('annotator_content'); - - //todo: add load from search - $this->uri = $this->get_current_uri(); } - /* - * + /** * Fetches the URI of the currently visited blog page - * * returns a uri string - * */ private function get_current_uri(){ return OkfnUtils::current_url(); } - /* + /** * Wrapper method calling the Mustache template engine. - * * (Needed mostly for testing and mocking...) - * - * - * */ function render_template($template_vars, $template='annotator-instance.js') { @@ -60,15 +44,35 @@ function render_template($template_vars, $template='annotator-instance.js') { private function prepare_variables(){ - $template_vars = array( - 'annotator_content' => $this->annotator_content, - 'uri' => $this->uri, + $template_vars = $this->settings->get_options_values(); + + $CONSUMER_TTL = 86400; + + $secret = $template_vars["annotateit_secret"]; + $objDateTime = new DateTime('NOW'); + $issuedAt = $objDateTime->format(DateTime::ISO8601)."Z"; + + $user = true ? "" : wp_get_current_user()->user_login; //client 1.2 seems to be buggy, waiting for v2 + + $token = array( + 'consumerKey'=> $template_vars["annotateit_key"], + 'userId'=> $user, + 'issuedAt'=> $issuedAt, + 'ttl' => $CONSUMER_TTL ); + $template_vars["token"] = JWT::encode($token, $secret); + $template_vars["user"] = $user; + + //todo: add load from search + $template_vars['uri'] = $this->get_current_uri(); + + $template_vars['annotateit_secret'] = ""; //should never be published + return $template_vars; } - /* + /** * Generates a code snippet containing a customised Javascript instantiation * code for the Annotator. * @@ -79,4 +83,4 @@ function create_snippet() { return $this->render_template($this->prepare_variables()); } } -?> +?> \ No newline at end of file diff --git a/lib/okfn-annot-injector.php b/lib/okfn-annot-injector.php index 7a6627a..95b981e 100644 --- a/lib/okfn-annot-injector.php +++ b/lib/okfn-annot-injector.php @@ -16,12 +16,14 @@ class OkfnAnnotInjector extends OkfnBase { // of the annotator (A more fine grained control through the settings can be implemented later). 'javascripts' => array( - array( +/* todo deniso import jquery properly + * array( 'file' => 'javascripts/jquery.min.js' ), +*/ array( 'file' => 'javascripts/annotator/annotator-full.min.js', - 'dependencies' => array('json2','jquery'), + 'dependencies' => array('json2',/*'jquery'*/), ), ), @@ -50,7 +52,10 @@ function __construct($factory,$content_policy){ function load_javascript_dependencies(){ wp_enqueue_script('json2'); //deregister the javascript version used by wordpress - wp_deregister_script('jquery'); +/* + * todo deniso import jquery properly + * wp_deregister_script('jquery'); +*/ } /* diff --git a/lib/okfn-annot-settings.php b/lib/okfn-annot-settings.php index 05f888c..1269331 100644 --- a/lib/okfn-annot-settings.php +++ b/lib/okfn-annot-settings.php @@ -16,6 +16,9 @@ class OkfnAnnotSettings extends OkfnBase { 'default_options' => array( 'annotator_content' => '.entry-content', 'url_pattern' => '.*', + 'annotateit_key' => '', + 'annotateit_secret' => '', + 'logged_in-only' => 'on', ) ); @@ -93,17 +96,33 @@ function process_settings_form($params){ $this->render_settings_form($is_submit=true); } - /* - * + /** + * Returns values for current plugin options - stored or default + * is_prefix - boolean flag indicating add prefix before nme or not + * returns array with name-value options + */ + function get_options_values() { + $prefix = $this->conf->forminput_prefix; + $options = array(); + + foreach($this->conf->default_options as $optname => $value) { + $stored_value = get_option("{$prefix}-{$optname}"); + $options[$optname] = empty($stored_value) ? $value : $stored_value ; + } + + //unescape backslashes automatically added by php for string sanitation + $options['url_pattern'] = stripslashes($options['url_pattern']); + + $options['logged_in-only'] = $options['logged_in-only'] == 'on' ? 'on' : ''; + + return $options; + } + + /** * Renders the HTML for the Annotator settings options page. - * * is_submit - boolean flag indicating whether data has been submitted - * - * * returns a rendered form template - * */ - function render_settings_form($is_submit=false) { $prefix = $this->conf->forminput_prefix; @@ -116,28 +135,17 @@ function render_settings_form($is_submit=false) { 'form_submitted' => $is_submit, ); - foreach($this->conf->default_options as $optname => $value) { - $stored_value = get_option("{$prefix}-{$optname}"); - $options[$optname] = empty($stored_value) ? $value : $stored_value ; - } - - //unescape backslashes automatically added by php for string sanitation - $options['url_pattern'] = stripslashes($options['url_pattern']); + $options = array_merge($options, $this->get_options_values()); $mustache = new Mustache; $template = OkfnUtils::get_template('settings.html'); print $mustache->render($template, $options); } - /* + /** * Public: - * * Logical switch determining whether to render or to process the setting form. - * - * * params - the request parameters (defaults to $_POST if not provided). - * - * * returns nothing */ diff --git a/okfn-annotator.php b/okfn-annotator.php index dc2a77c..64d8464 100644 --- a/okfn-annotator.php +++ b/okfn-annotator.php @@ -20,25 +20,36 @@ License: GPLv2 or later */ - -foreach(array( - 'lib/wp-pluggable', - 'vendor/Mustache', - 'lib/okfn-utils', - 'lib/okfn-base', - 'lib/okfn-annot-settings', - 'lib/okfn-annot-content-policy', - 'lib/okfn-annot-injector', - 'lib/okfn-annot-factory', -) as $lib) require_once("${lib}.php"); +foreach (array( + 'lib/wp-pluggable', + 'vendor/Mustache', + 'vendor/php-jwt/src/JWT', + 'lib/okfn-utils', + 'lib/okfn-base', + 'lib/okfn-annot-settings', + 'lib/okfn-annot-content-policy', + 'lib/okfn-annot-injector', + 'lib/okfn-annot-factory', + ) as $lib) require_once("${lib}.php"); $settings = new OkfnAnnotSettings; +//todo make configurable +$factory = new OkfnAnnotFactory($settings); +$content_policy = new OkfnAnnotContentPolicy($settings); + +$init_factory = function () use ($factory, $content_policy, $settings){ + $options_values = $settings->get_options_values(); + $logged_in_only = $options_values["logged_in-only"] == "on"; + + if (!$logged_in_only || is_user_logged_in()) { + $injector = new OkfnAnnotInjector($factory, $content_policy); + $injector->inject(); + } +}; + +//todo deniso make is_user_logged_in configurable if (!is_admin()) { - $factory = new OkfnAnnotFactory($settings); - $content_policy = new OkfnAnnotContentPolicy($settings); - $injector = new OkfnAnnotInjector($factory, $content_policy); - $injector->inject(); + add_action('init', $init_factory); } - ?> diff --git a/readme.txt b/readme.txt index a9df011..3d57a12 100644 --- a/readme.txt +++ b/readme.txt @@ -1,8 +1,8 @@ === Annotator === -Contributors: a-fiore +Contributors: a-fiore, denis.obydennykh Tags: annotation, okf, annotate.it Requires at least: 3.2.0 -Tested up to: 3.2.0 +Tested up to: 4.3.1 Stable tag: 0.4 Adds inline annotations to Wordpress using the Open Knowledge diff --git a/templates/annotator-instance.js b/templates/annotator-instance.js index a22b14d..2488994 100644 --- a/templates/annotator-instance.js +++ b/templates/annotator-instance.js @@ -1,16 +1,25 @@ jQuery(function ($) { - var element= $('{{annotator_content}}'); - - if (element) { - element.annotator() - .annotator('setupPlugins', null, { - Store: { - annotationData: {uri: '{{uri}}'}, - loadFromSearch: {uri: '{{uri}}', limit: 200} - } - }); - } else { - throw new Error("OkfnAnnotator: Unable to find a DOM element for selector '{{annotator_content}}'; cannot instantiate the Annotator"); - } + var element = $('{{annotator_content}}'); + + if (element) { + var annotatorInstance = element.annotator(); + annotatorInstance + .annotator('setupPlugins', null, { + Store: { + annotationData: {uri: '{{uri}}'}, + loadFromSearch: {uri: '{{uri}}', limit: 200} + } + , Auth: { + token: '{{token}}' + } +/* + , Permissions: { + user: '{{user}}' + } +*/ + }) + } else { + throw new Error("OkfnAnnotator: Unable to find a DOM element for selector '{{annotator_content}}'; cannot instantiate the Annotator"); + } }); diff --git a/templates/settings.html b/templates/settings.html index 6ca3428..5c14a35 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -1,63 +1,109 @@
-

-

Annotator

- - {{#form_submitted}} -
-

Settings saved.

-
- {{/form_submitted}} - -
- -

Embedding options

- - - - - - - - - - - - -
- - - -
- - - -
- -
URL pattern examples
- -

- - -

-
+

+

Annotator

+ + {{#form_submitted}} +
+

Settings saved.

+
+ {{/form_submitted}} + +
+ +

Embedding options

+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + +
+ + + +
+ + + +
+ + + +
+ + + + +
+ +
URL pattern examples
+ +

+ + +

+
diff --git a/vendor/javascripts/annotator/annotator-full.min.js b/vendor/javascripts/annotator/annotator-full.min.js index 2562883..08deead 100644 --- a/vendor/javascripts/annotator/annotator-full.min.js +++ b/vendor/javascripts/annotator/annotator-full.min.js @@ -1,2 +1,20 @@ -((function(){var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v=Array.prototype.slice,w=Object.prototype.hasOwnProperty,x=function(a,b){return function(){return a.apply(b,arguments)}},y=function(a,b){function d(){this.constructor=a}for(var c in b)w.call(b,c)&&(a[c]=b[c]);return d.prototype=b.prototype,a.prototype=new d,a.__super__=b.prototype,a},z=Array.prototype.indexOf||function(a){for(var b=0,c=this.length;b1?"["+d+"]":"",e="/"+c.tagName.toLowerCase()+d+e,c=c.parentNode;return e}),c.get()},a.escape=function(a){return a.replace(/&(?!\w+;)/g,"&").replace(//g,">").replace(/"/g,""")},a.fn.escape=function(b){return arguments.length?this.html(a.escape(b)):this.html()},j=["log","debug","info","warn","exception","assert","dir","dirxml","trace","group","groupEnd","groupCollapsed","time","timeEnd","profile","profileEnd","count","clear","table","error","notifyFirebug","firebug","userObjects"];if(typeof console!="undefined"&&console!==null){console.group==null&&(console.group=function(a){return console.log("GROUP: ",a)}),console.groupCollapsed==null&&(console.groupCollapsed=console.group);for(p=0,r=j.length;p0?h.start.splitText(h.startOffset):h.start,h.start===h.end?(h.endOffset-h.startOffsetr;0<=r?g++:g--){if(l[g]!==f[g])break;d.push(l[g])}c=j+d.join("/"),k.commonAncestorContainer=this._nodeFromXPath(c);if(!k.commonAncestorContainer)return console.error(u("Error deserializing range: can't find XPath '")+c+u("'. Is this the right document?")),null;s=["start","end"];for(n=0,p=s.length;n=this[i+"Offset"]){k[i+"Container"]=m,k[i+"Offset"]=this[i+"Offset"]-h;break}h+=m.nodeValue.length}}return(new e.BrowserRange(k)).normalize(b)},b.prototype.serialize=function(a,b){return this.normalize(a).serialize(a,b)},b.prototype.toObject=function(){return{start:this.start,startOffset:this.startOffset,end:this.end,endOffset:this.endOffset}},b}(),m={uuid:function(){var a;return a=0,function(){return a++}}(),getGlobal:function(){return function(){return this}()},mousePosition:function(b,c){var d;return d=a(c).offset(),{top:b.pageY-d.top,left:b.pageX-d.left}},preventEventDefault:function(a){return a!=null?typeof a.preventDefault=="function"?a.preventDefault():void 0:void 0}},n=this.Annotator,b=function(b){function c(b,d){this.onDeleteAnnotation=x(this.onDeleteAnnotation,this),this.onEditAnnotation=x(this.onEditAnnotation,this),this.onAdderClick=x(this.onAdderClick,this),this.onAdderMousedown=x(this.onAdderMousedown,this),this.onHighlightMouseover=x(this.onHighlightMouseover,this),this.checkForEndSelection=x(this.checkForEndSelection,this),this.checkForStartSelection=x(this.checkForStartSelection,this),this.clearViewerHideTimer=x(this.clearViewerHideTimer,this),this.startViewerHideTimer=x(this.startViewerHideTimer,this),this.showViewer=x(this.showViewer,this),this.onEditorSubmit=x(this.onEditorSubmit,this),this.onEditorHide=x(this.onEditorHide,this),this.showEditor=x(this.showEditor,this);var e,f,g;c.__super__.constructor.apply(this,arguments),this.plugins={};if(!c.supported())return this;this.options.readOnly||this._setupDocumentEvents(),this._setupWrapper()._setupViewer()._setupEditor(),g=this.html;for(e in g)f=g[e],e!=="wrapper"&&(this[e]=a(f).appendTo(this.wrapper).hide())}return y(c,b),c.prototype.events={".annotator-adder button click":"onAdderClick",".annotator-adder button mousedown":"onAdderMousedown",".annotator-hl mouseover":"onHighlightMouseover",".annotator-hl mouseout":"startViewerHideTimer"},c.prototype.html={hl:'',adder:'
",wrapper:'
'},c.prototype.options={readOnly:!1},c.prototype.plugins={},c.prototype.editor=null,c.prototype.viewer=null,c.prototype.selectedRanges=null,c.prototype.mouseIsDown=!1,c.prototype.ignoreMouseup=!1,c.prototype.viewerHideTimer=null,c.prototype._setupWrapper=function(){return this.wrapper=a(this.html.wrapper),this.element.find("script").remove(),this.element.wrapInner(this.wrapper),this.wrapper=this.element.find(".annotator-wrapper"),this},c.prototype._setupViewer=function(){var b=this;return this.viewer=new c.Viewer({readOnly:this.options.readOnly}),this.viewer.hide().on("edit",this.onEditAnnotation).on("delete",this.onDeleteAnnotation).addField({load:function(c,d){return d.text?a(c).escape(d.text):a(c).html(""+u("No Comment")+""),b.publish("annotationViewerTextField",[c,d])}}).element.appendTo(this.wrapper).bind({mouseover:this.clearViewerHideTimer,mouseout:this.startViewerHideTimer}),this},c.prototype._setupEditor=function(){return this.editor=new c.Editor,this.editor.hide().on("hide",this.onEditorHide).on("save",this.onEditorSubmit).addField({type:"textarea",label:u("Comments")+"…",load:function(b,c){return a(b).find("textarea").val(c.text||"")},submit:function(b,c){return c.text=a(b).find("textarea").val()}}),this.editor.element.appendTo(this.wrapper),this},c.prototype._setupDocumentEvents=function(){return a(document).bind({mouseup:this.checkForEndSelection,mousedown:this.checkForStartSelection}),this},c.prototype.getSelectedRanges=function(){var b,c,d,f,g,h,i,j,k;i=m.getGlobal().getSelection(),g=[],h=[],i.isCollapsed||(g=function(){var a,g;g=[];for(c=0,a=i.rangeCount;0<=a?ca;0<=a?c++:c--)f=i.getRangeAt(c),b=new e.BrowserRange(f),d=b.normalize().limit(this.wrapper[0]),d===null&&h.push(f),g.push(d);return g}.call(this),i.removeAllRanges());for(j=0,k=h.length;j0?setTimeout(function(){return c(a)},1):d.publish("annotationsLoaded",[b])},b=a.slice(),a.length&&c(a),this},c.prototype.dumpAnnotations=function(){return this.plugins.Store?this.plugins.Store.dumpAnnotations():console.warn(u("Can't dump annotations without Store plugin."))},c.prototype.highlightRange=function(b){var c,d,e,f,g,h;d=/^\s*$/,g=b.textNodes(),h=[];for(e=0,f=g.length;e tag?"))),this},c.prototype.showEditor=function(a,b){return this.editor.element.css(b),this.editor.load(a),this},c.prototype.onEditorHide=function(){return this.publish("annotationEditorHidden",[this.editor]),this.ignoreMouseup=!1},c.prototype.onEditorSubmit=function(a){return this.publish("annotationEditorSubmit",[this.editor,a]),a.ranges===void 0?this.setupAnnotation(a):this.updateAnnotation(a)},c.prototype.showViewer=function(a,b){return this.viewer.element.css(b),this.viewer.load(a),this.publish("annotationViewerShown",[this.viewer,a])},c.prototype.startViewerHideTimer=function(){if(!this.viewerHideTimer)return this.viewerHideTimer=setTimeout(this.viewer.hide,250)},c.prototype.clearViewerHideTimer=function(){return clearTimeout(this.viewerHideTimer),this.viewerHideTimer=!1},c.prototype.checkForStartSelection=function(a){if(!a||!this.isAnnotator(a.target))return this.startViewerHideTimer(),this.mouseIsDown=!0},c.prototype.checkForEndSelection=function(a){var b,c,d,e,f;this.mouseIsDown=!1;if(this.ignoreMouseup)return;this.selectedRanges=this.getSelectedRanges(),f=this.selectedRanges;for(d=0,e=f.length;d0&&this.invertX(),this},d.prototype.resetOrientation=function(){return this.element.removeClass(this.classes.invert.x).removeClass(this.classes.invert.y),this},d.prototype.invertX=function(){return this.element.addClass(this.classes.invert.x),this},d.prototype.invertY=function(){return this.element.addClass(this.classes.invert.y),this},d.prototype.isInvertedY=function(){return this.element.hasClass(this.classes.invert.y)},d.prototype.isInvertedX=function(){return this.element.hasClass(this.classes.invert.x)},d}(c),b.Editor=function(b){function c(b){this.onCancelButtonMouseover=x(this.onCancelButtonMouseover,this),this.processKeypress=x(this.processKeypress,this),this.submit=x(this.submit,this),this.load=x(this.load,this),this.hide=x(this.hide,this),this.show=x(this.show,this),c.__super__.constructor.call(this,a(this.html)[0],b),this.fields=[],this.annotation={}}return y(c,b),c.prototype.events={"form submit":"submit",".annotator-save click":"submit",".annotator-cancel click":"hide",".annotator-cancel mouseover":"onCancelButtonMouseover","textarea keydown":"processKeypress"},c.prototype.classes={hide:"annotator-hide",focus:"annotator-focus"},c.prototype.html='
\n
\n
    \n \n
    \n
    ",c.prototype.options={},c.prototype.show=function(a){return m.preventEventDefault(a),this.element.removeClass(this.classes.hide),this.element.find(".annotator-save").addClass(this.classes.focus),this.checkOrientation(),this.element.find(":input:first").focus(),this.setupDraggables(),this.publish("show")},c.prototype.hide=function(a){return m.preventEventDefault(a),this.element.addClass(this.classes.hide),this.publish("hide")},c.prototype.load=function(a){var b,c,d,e;this.annotation=a,this.publish("load",[this.annotation]),e=this.fields;for(c=0,d=e.length;c'),d.element=c[0];switch(d.type){case"textarea":e=a("