<?php

/**
 * Sanitize the post array for the options page. Loop through the post array,
 * matching and filtering whitelisted elements and returning the clean array.
 *
 * @param  array  $data 	This should be the $_POST array to sanitize.
 * @param  array  $filters 	The array containing the filters with matching keys.
 * @return array			The clean array.
 * @since 1.3.3
 */
function ow_sanitize_options_post_array( $data, $filters ) {
	$sanitized = [];
	foreach ( $filters as $key => $value ) {
		$sanitized[$key] = isset( $data[$key] ) ? filter_var( $data[$key], $filters[$key] ) : null;
	}
	return $sanitized;
}			

/**
 * Compile the database query based on an array with keys and values.
 *
 * @param  array  $data 	The array with keys and values that correspond to 
 *							the database table.
 * @return string			The database query string.
 * @since 1.3.3
 */			
function ow_create_options_sql_query( $array ) {
    $query = [];
    
    foreach ( $array as $key => $value ) {
        $query[] = "('$key','$value')";
    }
    return 'REPLACE INTO ow_global_configuration (gc_option,gc_value) VALUES ' . implode( ',', $query );
}










function ow_head() {
	
	return '<a href="' . PRODUCT_WEBSITE . '" target="_blank"><img width="221px" height="60px" src="' . plugins_url( '../img/iris-webportal-logo.svg', __FILE__ ) . '" alt="Powered by ' . PRODUCT_NAME . '" id="powered-by-openwebsite-admin"/></a>';
}



// ADD IRIS LOGO ON EACH ADMIN PAGE
function iris_meta_box() {
	
	echo '<p>' . ow_head() . '</p> '; 
}



function add_iris_meta_box() {
	
	add_meta_box( 'iris-logo-meta-box', 'Contact ' . PRODUCT_NAME, 'iris_meta_box', '', 'side', 'high', null );
}
add_action( 'add_meta_boxes', 'add_iris_meta_box' );



function admin_footer_powered_by( $content ) {
	
    return '<i>Powered by <a href="' . PRODUCT_WEBSITE . '" target="_blank">' . PRODUCT_NAME . '</a></i>';
}
add_filter( 'admin_footer_text', 'admin_footer_powered_by', 11 );



function admin_footer_version( $content ) {
	
    return 'Version ' . OW_VERSION . ' | ' . get_bloginfo( 'version' );
}
add_filter( 'update_footer', 'admin_footer_version', 11 );



// INCLUDE JS ON EACH ADMIN PAGE
function add_iris_plugin_js_admin() {
	
	global $ow_dashboard;

	if ( is_admin() ) {

		wp_enqueue_script( 'admin-ajax', plugins_url( '../js/ajax.js', __FILE__ ), array( 'jquery' ), null, false );
		wp_enqueue_script( 'admin-scripts', plugins_url( '../js/script.js', __FILE__ ), array( 'jquery' ), null, false );
		wp_localize_script( 'admin-ajax', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
	}
}
add_action( 'admin_enqueue_scripts', 'add_iris_plugin_js_admin' );



function search_iris_wordpress_help() {
	
	global $iris_db;
	
	$query = $_POST['sq'];
	$help_array = $iris_db->get_results( "SELECT * FROM iris_wordpress_help WHERE MATCH(title) AGAINST('*$query*' IN BOOLEAN MODE) OR MATCH(description) AGAINST ('*$query*' IN BOOLEAN MODE) OR MATCH(category) AGAINST ('*$query*' IN BOOLEAN MODE) LIMIT 5" );
	
	foreach ( $help_array as $help_item ) {
		
		$result[] = array( 'title' => $help_item->title, 'description' => wpautop( $help_item->description ), 'categories' => $help_item->category );
	}
	echo json_encode( $result );
	die();
}
add_action( 'wp_ajax_search_iris_wordpress_help', 'search_iris_wordpress_help' );
add_action( 'wp_ajax_nopriv_search_iris_wordpress_help', 'search_iris_wordpress_help' );



function iris_wordpress_help( $category = '' ) {
	
	global $iris_db;
	
	$help_array = $iris_db->get_results( "SELECT * FROM iris_wordpress_help WHERE MATCH(title) AGAINST('*$category*' IN BOOLEAN MODE) OR MATCH(description) AGAINST ('*$category*' IN BOOLEAN MODE) OR MATCH(category) AGAINST ('*$category*' IN BOOLEAN MODE)" );
	
	echo '<table class="striped fixed widefat"><thead><tr><th width="25%">Title</th><th>Description</th><th width="25%">Categories</th></tr></thead><tbody>';
	
	foreach ( $help_array as $help_item ) {
		
		echo '<tr><td class="row-title"><strong>' . $help_item->title . '</strong></td><td>' . wpautop( $help_item->description ) . '</td><td>' . $help_item->category . '</td></tr>';
	}
	echo '</tbody></table>';
}



function additional_admin_color_schemes() {

	wp_admin_css_color( 'iris', __( PRODUCT_NAME ),	plugin_dir_url( dirname( __FILE__ ) ) . 'css/admin-theme.css', array(
		'#66BBEB',
		'#1B2737',
		'#ffffff',
		'#D80C27',
	) );
}
add_action( 'admin_init', 'additional_admin_color_schemes' );



function change_admin_color() {
	
    return 'iris';
}
add_filter( 'get_user_option_admin_color','change_admin_color' );
remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' );



function menu_bubble() {
	
	$count = 0;
	
	if ( ! website_live() ) {	
	
		$count++;
	}
	if ( update_available() ) {	
	
		$count++;
	}
	if ( $count != 0 ) {
		
		$bubble = "<span class='update-plugins count-$count'><span class='plugin-count'>$count</span></span>";
		return $bubble;
	}
	return false;
}



// ADD ADMIN MENU
function add_iris_plugin_menu() {

	global $ow_dashboard;
	
	$ow_dashboard = add_menu_page( PRODUCT_NAME, PRODUCT_NAME . ' ' . menu_bubble(), 'edit_posts', 'iris-dashboard', 'main_page', 'dashicons-iris', 0 );
	add_submenu_page( 'iris-dashboard', 'Dashboard', 'Dashboard', 'edit_posts', 'iris-dashboard', 'main_page' );
	add_submenu_page( 'iris-dashboard', 'Options', 'Options', 'edit_posts', 'iris-options', 'options_page' );

	if ( has_newsletter() ) {
		add_submenu_page( 'iris-dashboard', 'newsletter', 'Newsletter', 'edit_posts', 'post.php?post=397&action=edit', '' );
	}
	add_submenu_page( 'iris-dashboard', 'pages', 'Pages', 'edit_posts', 'iris-tabs', 'tabs_panel' );
	add_submenu_page( 'iris-dashboard', 'knowledge-base', 'Knowledge Base & Support', 'edit_posts', 'iris-knowledge-base', 'knowledge_base' );	
}
add_action( 'admin_menu', 'add_iris_plugin_menu' );



// ADD ADMIN MENU PAGES
function main_page() {
	
	require_once plugin_dir_path( __FILE__ ) . 'page-dashboard.php'; 
}



function options_page() {
	
	require_once plugin_dir_path( __FILE__ ) . 'page-options.php'; 
}



function knowledge_base() {
	
	require_once plugin_dir_path( __FILE__ ) . 'page-knowledge-base.php'; 
}



function tabs_panel() {
	
	require_once plugin_dir_path( __FILE__ ) . 'page-tabs.php'; 
}



function ow_admin_bar( $wp_admin_bar ) {
	
	$wp_admin_bar->add_node( array(
		'id'    => 'iris_logo',
		'title' => '<span class="dashicons-before dashicons-iris-color"></span>',
		'href'  => '',
	) );
	$wp_admin_bar->add_node( array(
		'id'    => 'website_package_type',
		'title' => '<span>' . PRODUCT_NAME . ' ' . ow_global_var('website_package') . ' Package</span>',
		'href'  => '',
	) );
	$wp_admin_bar->add_node( array(
		'id'    => 'website_name',
		'title' => '<span class="dashicons-before dashicons-admin-site" style="transform:translateY(6px);display:inline-block;padding:0 6px 0 0;"></span><span>'.ow_global_var( 'accountant_website' ) . '</span>',
		'href' => site_url(),
		'meta'  => array(
			'target'=> '_blank',
			'rel' => 'external',
	) ) );
	
	if ( ! ow_global_var( 'in_production' ) ) {
		$wp_admin_bar->add_node( array(
			'id'    => 'website_status',
			'title' => 'WEBSITE NOT LIVE'
		) );
	}
	if ( update_available() ) {
		$wp_admin_bar->add_node( array(
			'id'    => 'update_status',
			'title' => 'UPDATE AVAILABLE',
			'href'  => site_url( 'wp-admin/update-core.php' )
		) );
	}
	$wp_admin_bar->remove_node( 'updates' );
	$wp_admin_bar->remove_node( 'wp-logo' );
	$wp_admin_bar->remove_node( 'comments' );
	$wp_admin_bar->remove_node( 'new-content' );
	$wp_admin_bar->remove_node( 'site-name' );
	$wp_admin_bar->remove_node( 'view' );
}
add_action( 'admin_bar_menu', 'ow_admin_bar', 999 );



function custom_menu_order( $menu_ord ) {
	
    if ( ! $menu_ord ) {
		return true;
	}
    return array(
        'index.php', // Dashboard
        'separator1', // First separator
        'edit.php?post_type=page', // Pages
        'edit.php?post_type=team', // Team Profiles
        'edit.php?post_type=testimonials', // testimonials
        'edit.php?post_type=vacancies-posts', // vacancies
        'edit.php?post_type=practice-news-posts', // Practice News
        'edit.php', // Blog
		'separator2', // Second separator
        'upload.php', // Media
        'link-manager.php', // Links
        'edit-comments.php', // Comments
        'themes.php', // Appearance
        'plugins.php', // Plugins
        'users.php', // Users
        'tools.php', // Tools
        'options-general.php', // Settings
        'separator-last', // Last separator
    );
}
add_filter( 'custom_menu_order', 'custom_menu_order' ); // Activate custom_menu_order
add_filter( 'menu_order', 'custom_menu_order' );



// REMOVE WORDPRESS MENU ITEMS FROM ADMIN AREA
function my_admin_menu() {
	
	global $menu;
	global $wp_post_types;
    
	$menu[5][0] = 'Blog'; // Change posts to Blog
	$menu[10][0] = 'Media Hub'; // Change Media to Images	
    $labels = $wp_post_types['post']->labels;
    $labels->name = 'Blog';
	
	if ( ! current_user_can( 'administrator' ) ) {
		remove_menu_page( 'index.php' );
		remove_menu_page( 'link-manager.php' );
		remove_menu_page( 'tools.php' );
		remove_menu_page( 'edit-comments.php' );
	}
}
add_action( 'admin_menu', 'my_admin_menu' );



// REMOVE 'DEACTIVATE' LINK AND ADD 'SETTINGS' LINK TO PLUGIN MENU
function plugin_settings_link( $links,$file ) {
	
	$plugin = plugin_basename('ow-plugin/ow-plugin.php');
	
	if ( $file == $plugin ) {
		
		if ( array_key_exists( 'deactivate', $links ) ) {
			
			unset( $links['deactivate'] );
		}
		$url = self_admin_url( 'admin.php?page=iris-settings' );
		return array_merge( $links, array( '<a href="' . $url . '">Settings</a>' ) );
	}
	return $links;
}
add_filter( 'plugin_action_links', 'plugin_settings_link', 10, 2 );



// ADD 'VIEW DETAILS' LINK TO PLUGIN MENU
function plugin_test_link( $links, $file ) {
	
	$plugin = plugin_basename('ow-plugin/ow-plugin.php');
	
	if ( $file == $plugin ) {
		
		if ( array_key_exists( 2, $links ) ) {
			
			unset( $links[2] );
		}
		$url = self_admin_url( 'plugin-install.php?tab=plugin-information&plugin=ow-plugin&TB_iframe=true&width=772&height=930' );
		return array_merge( $links, array( '<a href="' . $url . '" class="thickbox open-plugin-details-modal">View Details</a>' ) );
	}
	return $links;
}
add_filter( 'plugin_row_meta', 'plugin_test_link', 10, 2 );



// REMOVES DRAFTS FROM MAIN MENU
function remove_draft_from_menu( $post ) {
	
   _wp_delete_post_menu_item( $post->ID );
}
add_action( 'publish_to_draft', 'remove_draft_from_menu' );



// ADD/REMOVE CAPABILITIES FOR EDITOR
function wps_editor_capabilities() {
	
    global $wp_roles;
	
    $wp_roles->add_cap( 'editor', 'edit_theme_options' );
	$wp_roles->add_cap( 'editor', 'update_plugins' );	
	
}
add_action( 'init', 'wps_editor_capabilities' );



// RESTRICT BASIC AND STANDARD PACKAGES FROM UPLOADING VIDEO & scripts/applications etc
function restrict_mime( $mimes ) {
	
	$mimes = array(
		'jpg|jpeg' => 'image/jpeg',
		'png' 	   => 'image/png',
		'svg' 	   => 'image/svg+xml',
		'gif' 	   => 'image/gif',
		'pdf' 	   => 'application/pdf',
		'docx' 	   => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
		'doc' 	   => 'application/msword',
		'xls' 	   => 'application/vnd.ms-excel',
		'xlsx' 	   => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		'ppt' 	   => 'application/vnd.ms-powerpoint',
		'pptx' 	   => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
		'zip' 	   => 'application/zip',
	);
	return $mimes;
}
add_filter( 'upload_mimes', 'restrict_mime' );



function my_default_editor() {
	
	return 'tinymce';
}
add_filter( 'wp_default_editor', 'my_default_editor' );



// check if the website is live
function website_live() {
	if ( ow_global_var( 'in_production' ) ) {
		return true;
	}
	return false;
}



function website_not_live_notice() {
	
	if ( ! website_live() && ! ow_global_var( 'website_live_request' ) ) {
		
		echo '<div class="notice notice-error"><p><strong>Website is not live. To make your site live, please get in touch via your <a href="' . site_url( 'wp-admin/admin.php?page=iris-dashboard' ) . '">' . PRODUCT_NAME . ' Dashboard.</a></strong></p></div>';
	}
	if ( ! website_live() && ow_global_var( 'website_live_request' ) ) {
		
		echo '<div class="notice notice-info"><p><strong>Website live request is pending. A member of our team will be in touch shortly. In the meantime, why not browse our <a href="'.site_url( 'wp-admin/admin.php?page=iris-dashboard' ).'">Knowledge Base.</a></strong></p></div>';
	}
}
add_action( 'admin_notices', 'website_not_live_notice' );



function plugin_update_warning_notice() {
	
	if ( update_available() ) {	
	
		echo '<div class="notice notice-warning"><p><strong>An update is available. <a href="' . site_url( 'wp-admin/update-core.php' ) . '">Update to ' . PRODUCT_NAME . ' ' . UPDATE_VERSION . '</a></strong></p></div>';
	}
}
add_action('admin_notices', 'plugin_update_warning_notice');