<?php
/*
Plugin Name:Review API
Description:This plugin is used to view API reviews
Version: 2.12
*/
register_activation_hook(__FILE__,'reputation_management_activeFunction');
register_deactivation_hook( __FILE__, 'reputation_management_deActiveFunction');
require_once("reputation_management_macros.php");
/************************* Code for admin section ****************/
add_action( 'admin_menu', 'reputation_management_menu_contributor' );

function reputation_management_menu_contributor() {
	global $wpdb;
	add_menu_page('Review API Settings', __('Review API Settings','contr'), 'add_users', 'reputation_management/adminPanel.php');
}

if(!empty($_REQUEST['reputation_management-ajax'])) {
    require_once(plugin_dir_path(__FILE__) . "reviews_ajax.php");
}

/********************** Code for reputation bug widget ****************/
class PagesLink extends WP_Widget {
    function __construct() {
        $widget_ops = array('classname' => 'PagesLink', 'description' => 'Displays API Reviews On Sidebar' );
        return parent::__construct('PagesLink', 'Display API Reviews On Sidebar', $widget_ops);
    }
    function form($instance) {
        $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
        $title = $instance['title'];
        $number = isset($instance['count']) ? $instance['count'] : 0;
        ?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
        <?php
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        return $instance;
    }
    function widget($args, $instance) {
        extract($args, EXTR_SKIP);
        echo $before_widget;
        $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
        if (!empty($title))
            echo $before_title . $title . $after_title;
        // WIDGET CODE GOES HERE
        ?>
        <script>
            var TEXT_LENGTH_REP = <?php echo TEXT_LENGTH_REP?>;
            var READ_MORE = '<?php echo READ_MORE?>';
            var WP_PLUGIN_URL = '<?php echo WP_PLUGIN_URL?>';
            var WP_URL = '<?php echo get_site_url()?>';
            var BEHAVIOR_ON_ERROR = '<?php echo get_option('behavior_on_error')?>';
            var READ_MORE_URL = '<?php echo get_permalink(get_option("reviews_all_id")); ?>';
        </script>
        <div id="reputation_management-reviews" class="dot-tmpl">
            {{?it.length}}
            <div id="rb_list">
                <div class="slider">
                    {{~ it :p}}
                    <div class='slide'>
                        <div class='logo-block'>
                            <label class='review_logo'><img src='{{=WP_PLUGIN_URL}}/reputation_management/images/{{=p.review_source}}logo.png'></label>
                        </div>
                        <div class='dec-block'>
                            <label class='cust-name'>{{=p.customer_name}} {{=p.customer_last_name}}</label>
                            <label class='date'>{{=p.date_of_submission}}</label>
                            {{ if(p.review_source != 'citysearch') { }}
                                <label class='rating'><img src='{{=WP_PLUGIN_URL}}/reputation_management/images/{{=p.rating}}star.png'></label>
                            {{ } }}
                            <label class='dec'>
                                {{=p.description_cut}}
                            </label>
                        </div>
                    </div>
                    {{~}}
                </div>
            </div>
            <a class='view-more' href="<?php echo get_permalink(get_option("reviews_all_id"))?>">View More</a>
            {{?}}
        </div>
        <?php
		wp_enqueue_style('reputation_management_widget_css',plugins_url( '/css/reputation_management_widget.css' , __FILE__ ));
		wp_enqueue_script('slide_js',plugins_url( '/js/reputation_management_init.min.js' , __FILE__ ));
		wp_enqueue_script( 'reputation_management_widget',plugins_url( '/js/reputation_management_widget.js' , __FILE__ ));
		
        echo $after_widget;
    }
}
add_action( 'widgets_init', create_function('', 'return register_widget("PagesLink");') );
///* Front end page creation starts from here*/
function create_pages($page_title,$page_code){
    global $wpdb;
    $the_page_title = $page_title;
    $the_page_name = $page_title;
    // the menu entry...
    delete_option($page_code."_title");
    add_option($page_code."_title", $the_page_title, '', 'yes');
    // the slug...
    delete_option($page_code."_name");
    add_option($page_code."_name", $the_page_name, '', 'yes');
    // the id...
    delete_option($page_code."_id");
    add_option($page_code."_id", '0', '', 'yes');
    $the_page = get_page_by_title( $the_page_title );
    if ( ! $the_page ) {
        // Create post object
        $_p = array();
        $_p['post_title'] = $the_page_title;
        $_p['post_content'] = "[" . $page_code . "]";
        $_p['post_status'] = 'publish';
        $_p['post_type'] = 'page';
        $_p['comment_status'] = 'closed';
        $_p['ping_status'] = 'closed';
        $_p['post_category'] = array(1); // the default 'Uncatrgorised'
        // Insert the post into the database
        $the_page_id = wp_insert_post( $_p );
    } else {
        // the plugin may have been previously active and the page may just be trashed...
        $the_page_id = $the_page->ID;
        //make sure the page is not trashed...
        $the_page->post_status = 'publish';
        $the_page_id = wp_update_post( $the_page );
    }
    delete_option( $page_code."_id" );
    add_option($page_code."_id", $the_page_id );
}

function remove_pages($page_code) {
    global $wpdb;
    $the_page_title = get_option( $page_code."_title" );
    $the_page_name = get_option( $page_code."_name" );
    //  the id of our page...
    $the_page_id = get_option( $page_code.'_id' );   /*get the page id*/
    if( $the_page_id ) {
        wp_delete_post( $the_page_id ); // this will trash, not delete
    }
    delete_option($page_code."_title");
    delete_option($page_code."_name");
    delete_option($page_code."_id");
}

/*function artist_activate
 *This function is hooked to the plugin activation hook
 */
function reputation_management_activeFunction() {

    global $wpdb;
	create_pages('Reviews','reviews_all');
    add_option( 'rb_api_key', '' );
    add_option( 'rb_scroll_limit', 5 );
    add_option( 'internal_review', 1 );
	add_option( 'yelp', 1 );
	add_option( 'google_plus', 1 );
	add_option( 'rb_no_of_reviews', 5 );
	add_option( 'behavior_on_error', 'cache' );
}

/*function rb_deactivate
 *This function is hooked to the plugin deactivation hook
 */
function reputation_management_deActiveFunction() {
	global $wpdb;
	remove_pages('reviews_all');
    delete_option( 'rb_api_key' );
    delete_option( 'rb_scroll_limit' );
	
    delete_option( 'internal_review' );
	delete_option( 'yelp' );
	delete_option( 'google_plus' );

	delete_option( 'rb_no_of_reviews' );
	delete_option( 'behavior_on_error' );
}


function reviews_all_shortcode(){
	require_once(plugin_dir_path(__FILE__) . "reviews_all.php");
}
add_shortcode("reviews_all", "reviews_all_shortcode");

// Load the auto-update class
add_action('init', 'check_update_action');
function check_update_action()
{
    require_once('wp_autoupdate.php');
    $wptuts_plugin_current_version = '2.12';
    $wptuts_plugin_remote_path = PLUGIN_UPDATE_REMOTE_PATH.'/update.php' ;
    $wptuts_plugin_slug = plugin_basename(__FILE__);
    new wp_auto_update ($wptuts_plugin_current_version, $wptuts_plugin_remote_path, $wptuts_plugin_slug);
}

/*add action for */
add_action( 'plugins_loaded', 'my_plugin_override' );

function my_plugin_override() {
    add_option( 'internal_review', 1 );
	add_option( 'yelp', 1 );
	add_option( 'google_plus', 1 );
}
