<?php
/*
 * Plugin Name: APS Ads Manager
 * Plugin URI: https://www.webstudio55.com/plugins/arena/
 * Description: This is an addon plugin for APS products to provide easy to use advertisement management solution to get your APS Store monitized without any code editing.
 * Version: 1.3
 * Text Domain: aps-ads
 * Domain Path: /langs
 * Author: Shahzad Anjum
 * Author URI: https://www.codecanyon.net/user/Anjum
*/
	
	// define common constants
	define( 'APS_ADS_VER', '1.3' );
	define( 'APS_ADS_NAME', 'APS Ads Manager' );
	define( 'APS_ADS_URL', WP_PLUGIN_URL .'/' .str_replace(basename( __FILE__), '', plugin_basename(__FILE__)) );
	define( 'APS_ADS_DIR', WP_PLUGIN_DIR .'/' .str_replace(basename( __FILE__), '', plugin_basename(__FILE__)) );
	
	// register activation hook
	register_activation_hook( __FILE__, 'aps_ads_plugin_activate' );
	
	// register deactivation hook
	register_deactivation_hook( __FILE__, 'aps_ads_plugin_deactivate' );
	
	// get APS Products version
	$aps_version = get_option('aps_version', 0);
	
	if ($aps_version > 0) {
		if (version_compare($aps_version, '3.0', '>=')) {
			
			// include ads post type
			include (APS_ADS_DIR .'/inc/ads-post.php');
			
			// include ads settings
			include (APS_ADS_DIR .'/inc/ads-configuration.php');
			
			if (is_admin()) {
				// include APS Ads manager
				include (APS_ADS_DIR .'/inc/ads-manager.php');
				include (APS_ADS_DIR .'/inc/ads-settings.php');
			} else {
				// include ads display
				include (APS_ADS_DIR .'/inc/ads-display.php');
				include (APS_ADS_DIR .'/inc/mobile-detect.php');
			}
			
			// add menu page
			add_action('admin_menu', 'register_aps_ads_menu_pages');

			function register_aps_ads_menu_pages() {
				add_menu_page(__('APS Ads', 'aps-ads'), __('APS Ads Manager', 'aps-ads'), 'publish_aps_products', 'aps-ads', '', 'dashicons-layout', 7);
				$ads_manager = add_submenu_page('aps-ads', __('Ads Placements', 'aps-ads'), __('Ads Placements', 'aps-ads'), 'aps_admin', 'aps-ads', 'aps_build_ads_manager_page');
				$ads_settings = add_submenu_page('aps-ads', __('Settings', 'aps-ads'), __('Settings', 'aps-ads'), 'aps_admin', 'aps-ads-settings', 'aps_build_ads_settings_page');
				
				// enqueue scripts and styles for pages
				add_action( 'load-' .$ads_manager, 'aps_ads_load_manage_page' );
				add_action( 'load-' .$ads_settings, 'aps_ads_load_settings_page' );
			}
			
			function aps_ads_load_manage_page() {
				// enqueue scripts and styles for manage page
				add_action( 'admin_enqueue_scripts', 'aps_ads_add_scripts_to_manage_page' );
			}
			
			function aps_ads_load_settings_page() {
				// enqueue scripts and styles for settings page
				add_action( 'admin_enqueue_scripts', 'aps_ads_add_scripts_to_settings_page' );
			}
			
			// add scripts and styles for ads manager page
			function aps_ads_add_scripts_to_manage_page() {
				// enqueue aps admin css styles
				wp_enqueue_style( 'aps-admin-styles' );
				
				// enqueue aps ads admin styles
				wp_enqueue_style( 'aps-ads-css' );
				
				// enqueue jquery select2
				wp_enqueue_script( 'aps-select2' );
				
				// enqueue jquery ui sortable
				wp_enqueue_script( 'jquery-ui-sortable' );
			}
			
			// add scripts and styles for ads settings page
			function aps_ads_add_scripts_to_settings_page() {
				// enqueue aps admin css styles
				wp_enqueue_style( 'aps-admin-styles' );
				
				// enqueue aps ads admin styles
				wp_enqueue_style( 'aps-ads-css' );
			}
		} else {
			// add notification for admin
			add_action('admin_notices', 'aps_ads_products_require_update_notice');
		}
		
	} else {
		// add notification for admin
		add_action('admin_notices', 'aps_ads_products_not_installed_notice');
	}
	
	// APS plugin not installed/active notification	
	function aps_ads_products_not_installed_notice() { ?>
		<div class="error notice is-dismissible">
			<p><?php esc_html_e('Arena Products Store plugin is required for APS Ads Manager, please install and activate Arena Products Store plugin', 'aps-ads'); ?></p>
		</div>
		<?php
	}
	
	// APS plugin require update notification
	function aps_ads_products_require_update_notice() { ?>
		<div class="error notice is-dismissible">
			<p><?php esc_html_e('Please update Arena Products Store plugin to use APS Ads Manager.', 'aps-ads'); ?></p>
		</div>
		<?php
	}
	
	// add an option on activation
	function aps_ads_plugin_activate() {
		$status = get_option('aps_ads_status', 0);
		
		if (!$status) {
			update_option('aps_ads_status', APS_ADS_VER);
		}
		
		// get APS Products version
		$aps_version = get_option('aps_version', 0);
		
		if ($aps_version > 0) {
			// regenerate APS CSS styles
			$design = aps_get_design_settings();
			$typo = aps_get_typo_settings();
			aps_generate_styles($design, $typo);
		}
	}
	
	// deactivation hook
	function aps_ads_plugin_deactivate() {
		// just deactivate the plugin
		update_option('aps_ads_status', 0);
	}
	
	// initialize APS Ads plugin
	add_action('plugins_loaded', 'aps_ads_load_text_domain');
	
	function aps_ads_load_text_domain() {
		// load plugin text domain language files
		load_plugin_textdomain( 'aps-ads', false, basename(dirname( __FILE__ )) .'/langs/' );
	}
	
	// register javascripts and styles
	add_action( 'admin_enqueue_scripts', 'aps_ads_register_scripts' );
	
	// add scripts and styles for ads manager page
	function aps_ads_register_scripts() {
		// enqueue aps ads admin styles
		$css = (is_rtl()) ? 'styles-rtl.css': 'styles.css';
		wp_register_style( 'aps-ads-css', APS_ADS_URL .'css/' .$css, false, APS_ADS_VER );
		
		$screen = get_current_screen();
		
		if ($screen->id == 'dashboard') {
			// enqueue aps ads admin styles
			wp_enqueue_style( 'aps-ads-css' );
		}
	}
	