Sunday, May 24, 2020

Wordpress Pdf Annotation Plugin | PHP API for annotation.js | API for Insert,Update and Delete annotation

I have search for annotation plugin in Wordpress plugin center but not able to found a good one so i developed a annotation plugin with the help of http://annotatorjs.org/
This plugin will provided you full control over data as it save it in your wordpress database itself.
With the plugin 
Plugin for annotation
Viewer page template for viewing all pages in pdf file with scroll
Manage annotation page for edit the text used for annotation and for deleting the annotation

<?php 
    /*
    Plugin Name: G-annotator
    Plugin URI: http://php-you.blogspot.com
    Description: Plugin for inline annotation in post
    Author:GRS
    Version: 1.0
    Author URI:http://php-you.blogspot.com
    */


  add_action('init','gannotator_init');
  add_action('wp','gannotator_wp');
  function gannotator_init()
  {
  if(is_user_logged_in() )
 
     add_action('parse_request', 'custom_requests');
 
  }
  
  }
   function gannotator_wp()
  {
  if(is_user_logged_in())
  {
    add_action('wp_enqueue_scripts', 'g_annatator_enqueue_script');
 
  
 
  }
  
  }
  
  function g_annatator_enqueue_script() { 
    wp_enqueue_style( 'annatator_css', plugins_url( 'js/annotator.min.css', __FILE__ ) );  
    wp_enqueue_script( 'annatator_script', plugin_dir_url( __FILE__ ) . 'js/annotator-full.min.js', array('jquery'), '', true );
    wp_enqueue_script( 'g_annatator_script', plugin_dir_url( __FILE__ ) . 'js/g-annotator.js', array('jquery'), '', true );
}

function custom_requests ( $wp ) { 

global $current_user; 


  $valid_actions = array('api', 'action2');
  
  if(
    !empty($wp->query_vars['myroute']) &&
    in_array($wp->query_vars['myroute'], $valid_actions) 
  ) {
  
     
  /***************Insert and Update annotation************************/ 
  $method = $_SERVER['REQUEST_METHOD'];
if ('PUT' === $method || 'POST' === $method ) {
   $jsondata=file_get_contents('php://input');
    
    $jsondecode=json_decode($jsondata);
    
    if($jsondecode->id=='')
    {
   
               $order   = array("\r\n", "\n", "\r");
                  $replace = '<br/>';
                $jsondecode->text=str_replace($order, $replace,$jsondecode->text);
               
        $my_cptpost_args = array(

         'post_title'    => $jsondecode->quote,

         'post_content'  => $jsondecode->text,

          'post_status'   => 'private',

         'post_type' => 'document_tags'

       );
       
                

      // insert the post into the database

     $cpt_id = wp_insert_post( $my_cptpost_args, $wp_error);

    add_post_meta($cpt_id, 'uri', $jsondecode->uri);
    add_post_meta($cpt_id, 'ranges', json_encode($jsondecode->ranges));
    add_post_meta($cpt_id, 'rawdata', json_encode($jsondecode));
    $jsondecode->id=$cpt_id;

    }
    else
    {
    $my_post = array(
      'ID'           => $jsondecode->id,
      'post_title'   => $jsondecode->quote,
      'post_content' => $jsondecode->text,
  );
                 $order   = array("\r\n", "\n", "\r");
                  $replace = '<br/>';
                $jsondecode->text=str_replace($order, $replace,$jsondecode->text);
 
// Update the post into the database
  wp_update_post( $my_post );
  update_post_meta( $jsondecode->id, 'ranges', json_encode($jsondecode->ranges));  
  update_post_meta( $jsondecode->id, 'rawdata', json_encode($jsondecode));  
    }
    
    
     $order   = array("<br/>");
   $replace = "\n";
   $jsondecode->text=str_replace($order, $replace,$jsondecode->text);
    echo json_encode($jsondecode);
       
    die(0);
}
 /***************Insert and Update annotation************************/ 
  /***************Delete annotation************************/ 
if ('DELETE' === $method)
{
$jsondata=file_get_contents('php://input');
    
 $jsondecode=json_decode($jsondata);
    
    if($jsondecode->id!=='')
    {
      wp_delete_post($jsondecode->id);
       //wp_trash_post($jsondecode->id);
      
    }
}
 
  /***************Delete annotation************************/   
  /************ listing annotation as per uri **********************/ 
  $current_url = $_GET['uri'];
  if($current_url!=='')
  {
    $mydata=array();
    $masterdata=array();
    $posts = get_posts(array(
'post_type' => 'document_tags',
'meta_key' => 'uri',
'meta_value' => $current_url,
 'post_status' => 'private',
 'author'        =>  $current_user->ID,
  ));

  
  foreach($posts as $pdata)
  {
  $order   = array("<br/>");
   $replace = '\n';
    $rawdata=str_replace($order, $replace,get_post_meta($pdata->ID, 'rawdata',false));
    $dat_ass_arr=json_decode($rawdata[0],true);
    $dat_ass_arr['id']=$pdata->ID;
  
   array_push($mydata,$dat_ass_arr);
  
  }
  
  $masterdata['total']=count($mydata);
   $masterdata['rows']= $mydata;
  
  echo json_encode($masterdata);
 
    
  }
 /************ listing annotation as per uri **********************/

exit();
    // do something here

  }//checking gannotation/api && myroute is api

}//close function

add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_filter( 'query_vars','my_insert_query_vars' );
add_action( 'wp_loaded','my_flush_rules' );

// flush_rules() if our rules are not yet included
function my_flush_rules(){
    $rules = get_option( 'rewrite_rules' );

    if ( ! isset( $rules['gannotation/(.*?)/(.+?)'] ) ) {
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
    }
}

// Adding a new rule
function my_insert_rewrite_rules( $rules )
{
    $newrules = array();
    $newrules['gannotation/(.*?)/(.+?)'] = 'index.php?myroute=$matches[1]&myargument=$matches[2]';
       return $newrules + $rules;
}

// Adding the id var so that WP recognizes it
function my_insert_query_vars( $vars )
{
     array_push($vars, 'myroute', 'myargument');
     return $vars;
}
add_action('init', function() {

     add_rewrite_rule(
        'gannotation/(.*?)/(.+?)/?',
        'index.php?myroute=$matches[1]&myargument=$matches[2]',
        'top'
    );

});




?>