<?php
/**
 * Plugin Name: WP Simple HTML Sitemap
 * Plugin URI: http://wordpress.org/plugins/wp-simple-html-sitemap/
 * Description: Using Simple HTML Sitemap plugin, you can add HTML Sitemap anywhere on the website using Shortcode.
 * Author: Ashish Ajani
 * Version: 3.8
 * Author URI: http://freelancer-coder.com/
 * License: GPLv2 or later
*/ 
 

/* Security: Considered blocking direct access to PHP files by adding the following line. */
defined('ABSPATH') or die("No script kiddies please!");

require_once( ABSPATH . '/wp-includes/shortcodes.php' );

/* Define plugin constants */
define('WSHS_PLUGIN_FILE', plugin_dir_path(__FILE__));
define('WSHS_POST_LIST_VERSION', '1.1');
define('WSHS_DATABASE_VERSION', '1.1');
define('WSHS_PLUGIN_PATH', plugin_dir_path(__FILE__));
define("WSHS_PLUGIN_URL", plugins_url('', __FILE__));
define("WSHS_PLUGIN_CSS", WSHS_PLUGIN_URL . '/css/');
define("WSHS_PLUGIN_JS", WSHS_PLUGIN_URL . '/js/');
define('WSHS_SAVED_CODE_TABLE', 'wshs_saved_code');

/* Plugin activation process */
register_activation_hook(__FILE__, 'wshs_plugin_install');
function wshs_plugin_install() {}

function wshs_update_db_check() {
    
    if (get_site_option( 'wshs_db_version' ) != WSHS_DATABASE_VERSION) {
        wshs_create_saved_code_table();
    }
}
add_action( 'plugins_loaded', 'wshs_update_db_check' );

/* Plugin deactivation process */
register_deactivation_hook(__FILE__, 'wshs_plugin_deactivate');
function wshs_plugin_deactivate() {
    
    // Silence is golden
}

/* Add menu to sidebar menu. */

function wshs_admin_menu() {
    
    add_menu_page('Simple HTML Sitemap',esc_html__('Simple HTML Sitemap', 'wp-simple-html-sitemap') , 'manage_options', 'wshs_page_list', 'wshs_page_list', plugins_url('/images/sitemap.png', __FILE__ ));
    add_submenu_page('wshs_page_list', 'Simple HTML Sitemap - Pages', esc_html__('Pages','wp-simple-html-sitemap'), 'manage_options', 'wshs_page_list', 'wshs_page_list');
    add_submenu_page('wshs_page_list', 'Simple HTML Sitemap - Posts', esc_html__('Posts','wp-simple-html-sitemap'), 'manage_options', 'wshs_post_list', 'wshs_post_list');
    add_submenu_page('wshs_page_list', 'Simple HTML Sitemap - Saved Shortcodes', esc_html__('Saved Shortcodes','wp-simple-html-sitemap'), 'manage_options', 'wshs_saved', 'wshs_saved');
    add_submenu_page('wshs_page_list', 'Simple HTML Sitemap - Documentation', esc_html__('Documentation','wp-simple-html-sitemap'), 'manage_options', 'wshs_documentation', 'wshs_documentation');
}

add_action('admin_menu', 'wshs_admin_menu');

/* Enqueue admin script */
function wshs_admin_assets($hook) {
    
    // JS
    wp_enqueue_script('wshs_main_script', WSHS_PLUGIN_JS . 'wshs_script.js',array(),@filemtime(WSHS_PLUGIN_JS . 'wshs_script.js'), true);
    wp_localize_script( 'wshs_main_script', 'wshs_ajax_object',
		array( 
            'ajax_nonce' => wp_create_nonce('ajax-nonce'),
			'placeholder_image' => plugin_dir_url('').'/wp-simple-html-sitemap/',
			'ajax_url' => admin_url('admin-ajax.php')
		)
	);

    //CSS
    wp_enqueue_style('wshs_admin_css', WSHS_PLUGIN_CSS . 'wshs_style.css',array(),@filemtime(WSHS_PLUGIN_CSS . 'wshs_style.css'), false);
}

add_action('admin_enqueue_scripts', 'wshs_admin_assets');

/* Plugins setting hooks */
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'wshs_add_action_links');

function wshs_add_action_links($links) {
    
    $mylinks = array(
        '<a href="admin.php?page=wshs_page_list">'.esc_html__("Settings", 'wp-simple-html-sitemap').'</a>',
    );
    return array_merge($links, $mylinks);
}

// Plugins meta hooks
add_filter('plugin_row_meta', 'wshs_plugin_row_meta', 10, 2);

function wshs_plugin_row_meta($links, $file) {
    
    if (plugin_basename(__FILE__) == $file) {
        $row_meta = array(
            'visitpage' => '<a href="' . esc_url('https://wordpress.org/plugins/wp-simple-html-sitemap/') . '" target="_blank" aria-label="' . esc_attr__('Visit WordPress.org page', 'wp-simple-html-sitemap') . '" >' . esc_html__('Visit WordPress.org page', 'wp-simple-html-sitemap') . '</a>',
            'rate' => '<a href="' . esc_url('https://wordpress.org/support/plugin/wp-simple-html-sitemap/reviews/') . '" target="_blank" aria-label="' . esc_attr__('Rate this plugin', 'wp-simple-html-sitemap') . '" >' . esc_html__('Rate this plugin', 'wp-simple-html-sitemap') . '</a>'
        );

        return array_merge($links, $row_meta);
    }
    return (array) $links;
}

add_filter('plugin_action_links', 'wshs_details_link', 10, 3);

function wshs_details_link($links, $plugin_file, $plugin_data) {
    
    if (isset($plugin_data['PluginURI']) && false !== strpos($plugin_data['PluginURI'], 'http://wordpress.org/extend/plugins/')) {
        $slug = basename($plugin_data['PluginURI']);
        $links[] = sprintf(
            '<a href="%s" class="thickbox" title="%s">%s</a>',
            self_admin_url(
                'plugin-install.php?tab=plugin-information&amp;plugin=' . $slug . '&amp;TB_iframe=true&amp;width=600&amp;height=550'
            ),
            esc_attr(
                sprintf(
                    /* translators: %s is the plugin name. */
                    esc_html__( 'More information about %s', 'wp-simple-html-sitemap' ),
                    $plugin_data['Name']
                )
            ),
            __( 'Details', 'wp-simple-html-sitemap' )
        );
    }
    return $links;
}

if(!function_exists('wshs_create_saved_code_table')){
    function wshs_create_saved_code_table(){
        
        global $wpdb;
        $charset_collate = $wpdb->get_charset_collate();
        $sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->base_prefix}".WSHS_SAVED_CODE_TABLE."` (
        id bigint(50) NOT NULL AUTO_INCREMENT,
        title varchar(100),
        code_type varchar(100),
        user_id bigint(20),
        attributes text(2000),
        created_at timestamp,
        updated_at timestamp,
        PRIMARY KEY (id)
        ) $charset_collate;";
        require_once ABSPATH . 'wp-admin/includes/upgrade.php';
        dbDelta( $sql );
        $is_error = empty( $wpdb->last_error );
        if($is_error){
            update_option('wshs_db_version', WSHS_DATABASE_VERSION);
        }
    }
}

if(!function_exists('wshs_is_admin_page_active')){
    function wshs_is_admin_page_active( $page_slug, $parent_slug = '' ) {
        $screen = get_current_screen();

        if ( ! $screen ) {
            return false;
        }

        // Match based on screen ID
        if ( $parent_slug ) {
            return $screen->id === $parent_slug . '_page_' . $page_slug;
        }

        return $screen->id === $page_slug;
    }
}

/* Include other files */
require_once WSHS_PLUGIN_PATH . '/inc/wshs_admin_view.php';
require_once WSHS_PLUGIN_PATH . '/inc/wshs_front_view.php';
require_once WSHS_PLUGIN_PATH . '/inc/wshs_page_list.php';
require_once WSHS_PLUGIN_PATH . '/inc/wshs_post_list.php';
require_once WSHS_PLUGIN_PATH . '/inc/wshs_saved.php';
require_once WSHS_PLUGIN_PATH . '/inc/wshs_documentation.php';
