<?php
/*
Plugin Name: Visitors contributions
Plugin URI: http://codecanyon.net/user/Leavy
Description: A wordpress plugin that gives your visitors the possiblity to develop your website content by submiting new versions to your articles.
Version: 1.11
Author: Leavy
Author URI: http://codecanyon.net/user/Leavy
*/
class visitors_edits{
	public function __construct() {
		register_activation_hook( __FILE__, array($this,'install'));
		add_action( 'admin_menu', array( $this, 'admin_pages' ) );
		add_filter('the_content', array($this,'contentFilter'));

		add_filter( 'query_vars', array($this,'query_vars') );
		add_action( 'init',array($this,'init'));
		add_action( 'parse_request',array($this,'parse_request'));

		add_filter( 'mce_buttons', array($this,'tinymce_btns') );
		add_filter( 'mce_external_plugins', array($this,'tinymce_scripts') );

		add_action('admin_head', array($this,'admin_head') );
		add_action('admin_init', array($this,'admin_init') );

	}
	public function admin_pages() {
		add_menu_page('Visitors Contributions | Pending reviews','Visitors contributions','read','visitors_edits_main',array($this , 'main'),plugins_url("img/contribute.png",__FILE__),87);
		add_submenu_page(null,"Visitors Contributions | Review","Review a contribution","read",'visitors_edits_approve',array($this,'approve'));
		add_submenu_page("visitors_edits_main","Visitors Contributions | Settings","Settings","read",'visitors_edits_settings',array($this,'settings'));
	}
	public function admin_head(){
		if(isset($_GET["page"]) && ($_GET["page"]==="visitors_edits_approve" || $_GET["page"]==="visitors_edits_main" || $_GET["page"]==="visitors_edits_settings")){
		?>
			<link rel="stylesheet" type="text/css" href="<?php echo plugins_url( '/css/app.css',__FILE__ );?>">
		<?php
		}
	}
	public function admin_init(){
		add_editor_style(plugins_url( './css/tinymce.css',__FILE__ ));
	}
	public function main(){
		require "inc/main.php";
	}
	public function approve(){
		require 'inc/approve.php';
	}
	public function settings(){
		require 'inc/settings.php';
	}
	public function contentFilter($content){
		if(is_single()) {
			$options=get_option( "visitors_edits_options", [
				"propose_edit_link"=>"<p><a href='#post_link#'>Propose an edit</a></p>"
			]);
			global $post;
			$new_content = str_replace("#post_link#", get_site_url().'/'.$post->post_name.'/suggestions', stripcslashes($options["propose_edit_link"]));
			$content .= $new_content;	
		}
		return $content;
	}
	public function install(){
		//General Settings
		delete_option("visitors_edits_options");
		//Database Settings
		global $wpdb;
		$wpdb->show_errors();

		$charset_collate = $wpdb->get_charset_collate();
		$table_name = $wpdb->prefix . 'visitors_edits';

		$sql = "CREATE TABLE $table_name (
			edit_id bigint(20) NOT NULL AUTO_INCREMENT,
			edit_time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
			visitor_name text DEFAULT '',
			visitor_email text DEFAULT '',
			visitor_comment longtext DEFAULT '',
			edit_content longtext DEFAULT '',
			post_id bigint(20),
			post_content longtext DEFAULT '',
			UNIQUE KEY (edit_id)
		) $charset_collate;";

		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
		dbDelta( $sql );
	}
	public function query_vars($query_vars){
		$query_vars[] = 'visitors_edits_post_name';
    	return $query_vars;
	}
	public function init(){
		add_rewrite_rule(
	        '(.*)?/suggestions/?$',
	        'index.php?visitors_edits_post_name=$matches[1]',
	        'top'
	    );
	    flush_rewrite_rules(true);
	}
	public function parse_request($request){
		if( isset( $request->query_vars['visitors_edits_post_name'] ) ):
	        include( plugin_dir_path(__FILE__) . "/inc/editor.php" );
	        exit();
	    endif;
	    return $request;
	}
	public function tinymce_btns($buttons){
		if(isset($_GET["page"]) && $_GET["page"]==="visitors_edits_approve"){
			array_push($buttons,'|','visitors_edits_approve','visitors_edits_reject','visitors_edits_clean');
		}
   		return $buttons;
	}
	public function tinymce_scripts($plugin_array){
		$plugin_array['visitors_edits'] = plugins_url( 'js/approve.js',__FILE__ );
   		return $plugin_array;
	}
	static function scriptUrl($script){
		return plugins_url( 'js/'.$script.'.js',__FILE__);
	}
}
new visitors_edits();

use Caxy\HtmlDiff\HtmlDiff;
use PHPHtmlParser\Dom;

function visitors_editsDIFF($oldHtml,$newHtml){
	require "vendor/autoload.php";
	$htmlDiff = new HtmlDiff($oldHtml, $newHtml);
	$htmlDiff->getConfig()->setGroupDiffs(false);
	
	return $htmlDiff->build();
}
function visitors_editsDOM(){
	return new Dom;
}
?>