<?php

/**
 * The main file that handles the entire output, content filters, etc., for this plugin.
 *
 * @package Sticky Genesis Topbar
 * @author RainaStudio
 * @version 1.0.5
 */
 
// Check to make sure Genesis Framework is active.
register_activation_hook(__FILE__, 'sticky_genesis_topbar_require_genesis');
function sticky_genesis_topbar_require_genesis() {
		
    if( basename( get_template_directory() ) != 'genesis' ) {
	    deactivate_plugins(plugin_basename(__FILE__));
        wp_die('Sorry, you can\'t use this plugin unless a <a href="http://my.studiopress.com/themes/" target="_blank" rel="nofollow">Genesis</a> theme is active. <a href="/wp-admin/plugins.php">Go Back</a>');

	}

}

// Hook function to 'genesis_setup'.
add_action( 'genesis_setup', 'sticky_genesis_topbar_setup', 15 );
function sticky_genesis_topbar_setup() {
	
	// Register metabox.
	add_action('genesis_theme_settings_metaboxes', 'sticky_genesis_topbar_settings');
	
	function sticky_genesis_topbar_settings( $_genesis_theme_settings_pagehook ) {
		
		add_meta_box( 'sticky_genesis_topbar-genesis-settings', __( 'Sticky Topbar', 'sticky_genesis_topbar' ), 'custom_topbar_box', $_genesis_theme_settings_pagehook, 'main', 'high' );
		
	}
	
	// Create topbar metabox.
	function custom_topbar_box() {
		?>
		<p><?php _e("<em>Enter your topbar text, button URL & text, including HTML if desired and social media sites' URLs.</em>", 'sticky_genesis_topbar'); ?></p>
		<table class="form-table"><tbody><tr valign="top">
			<th scope="row"><?php esc_html_e( 'Topbar Text:', 'sticky_genesis_topbar' ); ?></th>
			<td>
				<p><textarea name="<?php echo GENESIS_SETTINGS_FIELD; ?>[sticky_genesis_topbar_text]" class="large-text" id="sticky_genesis_topbar_text" cols="40" rows="5" maxlength="180"><?php echo htmlspecialchars( genesis_get_option('sticky_genesis_topbar_text') ); ?></textarea></p>
				<p><span class="description">ie. Get 30% OFF on StudioPlayer Genesis WordPress theme</span><code>Limit 180 characters</code></p>
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><?php esc_html_e( 'Topbar Button Text:', 'sticky_genesis_topbar' ); ?></th>
			<td>
				<p><input type="text" name="<?php echo GENESIS_SETTINGS_FIELD; ?>[sticky_genesis_topbar_btn_text]" class="regular-text" id="sticky_genesis_topbar_btn_text" value="<?php echo htmlspecialchars( genesis_get_option('sticky_genesis_topbar_btn_text') ); ?>"/></p>
				<p><span class="description">ie. Subscribe, Buy Now, Get Now, Check Out, 30% OFF, Best Deal, Learn More, Live Demo</span></p>
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><?php esc_html_e( 'Topbar Button URL:', 'sticky_genesis_topbar' ); ?></th>
			<td>
				<p><input type="url" name="<?php echo GENESIS_SETTINGS_FIELD; ?>[sticky_genesis_topbar_url]" class="large-text" id="sticky_genesis_topbar_url"placeholder="http://" size="40" value="<?php echo htmlspecialchars( genesis_get_option('sticky_genesis_topbar_url') ); ?>"/></p>
				<p><span class="description">ie.<code>https://rainastudio.com/themes/studioplayer</code></span></p>
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><?php esc_html_e( 'Facebook URL:', 'sticky_genesis_topbar' ); ?></th>
			<td>
				<p><input type="url" name="<?php echo GENESIS_SETTINGS_FIELD; ?>[sticky_genesis_topbar_facebook]" class="large-text" id="sticky_genesis_topbar_facebook" value="<?php echo htmlspecialchars( genesis_get_option('sticky_genesis_topbar_facebook') ); ?>"/></p>
				<p><span class="description">ie.<code>https://www.facebook.com/rainastudio/</code></span></p>
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><?php esc_html_e( 'Twitter URL:', 'sticky_genesis_topbar' ); ?></th>
			<td>
				<p><input type="url" name="<?php echo GENESIS_SETTINGS_FIELD; ?>[sticky_genesis_topbar_twitter]" class="large-text" id="sticky_genesis_topbar_twitter" value="<?php echo htmlspecialchars( genesis_get_option('sticky_genesis_topbar_twitter') ); ?>"/></p>
				<p><span class="description">ie.<code>https://twitter.com/rainastudio/</code></span></p>
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><?php esc_html_e( 'LinkedIn URL:', 'sticky_genesis_topbar' ); ?></th>
			<td>
				<p><input type="url" name="<?php echo GENESIS_SETTINGS_FIELD; ?>[sticky_genesis_topbar_linkedIn]" class="large-text" id="sticky_genesis_topbar_linkedIn" value="<?php echo htmlspecialchars( genesis_get_option('sticky_genesis_topbar_linkedIn') ); ?>"/></p>
				<p><span class="description">ie.<code>https://www.linkedin.com/in/a3ashif/</code></span></p>
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><?php esc_html_e( 'Pinterest URL:', 'sticky_genesis_topbar' ); ?></th>
			<td>
				<p><input type="url" name="<?php echo GENESIS_SETTINGS_FIELD; ?>[sticky_genesis_topbar_pinterest]" class="large-text" id="sticky_genesis_topbar_pinterest" value="<?php echo htmlspecialchars( genesis_get_option('sticky_genesis_topbar_pinterest') ); ?>"/></p>
				<p><span class="description">ie.<code>https://www.pinterest.com/rainastudio/</code></span></p>
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><?php esc_html_e( 'Instagram URL:', 'sticky_genesis_topbar' ); ?></th>
			<td>
				<p><input type="url" name="<?php echo GENESIS_SETTINGS_FIELD; ?>[sticky_genesis_topbar_instagram]" class="large-text" id="sticky_genesis_topbar_instagram" value="<?php echo htmlspecialchars( genesis_get_option('sticky_genesis_topbar_instagram') ); ?>"/></p>
				<p><span class="description">ie.<code>https://www.instagram.com/ashif_devs/</code></span></p>
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><?php esc_html_e( 'YouTube URL:', 'sticky_genesis_topbar' ); ?></th>
			<td>
				<p><input type="url" name="<?php echo GENESIS_SETTINGS_FIELD; ?>[sticky_genesis_topbar_youtube]" class="large-text" id="sticky_genesis_topbar_youtube" value="<?php echo htmlspecialchars( genesis_get_option('sticky_genesis_topbar_youtube') ); ?>"/></p>
				<p><span class="description">ie.<code>https://youtube.com/rainastudio/</code></span></p>
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><?php esc_html_e( 'Dribbble URL:', 'sticky_genesis_topbar' ); ?></th>
			<td>
				<p><input type="url" name="<?php echo GENESIS_SETTINGS_FIELD; ?>[sticky_genesis_topbar_dribbble]" class="large-text" id="sticky_genesis_topbar_dribbble" value="<?php echo htmlspecialchars( genesis_get_option('sticky_genesis_topbar_dribbble') ); ?>"/></p>
				<p><span class="description">ie.<code>https://dribbble.com/ashif/</code></span></p>
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><?php esc_html_e( 'Google+ URL:', 'sticky_genesis_topbar' ); ?></th>
			<td>
				<p><input type="url" name="<?php echo GENESIS_SETTINGS_FIELD; ?>[sticky_genesis_topbar_googleplus]" class="large-text" id="sticky_genesis_topbar_googleplus" value="<?php echo htmlspecialchars( genesis_get_option('sticky_genesis_topbar_googleplus') ); ?>"/></p>
				<p><span class="description">ie.<code>https://plus.google.com/108700156371679837101/</code></span></p>
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><?php esc_html_e( 'Medium URL:', 'sticky_genesis_topbar' ); ?></th>
			<td>
				<p><input type="url" name="<?php echo GENESIS_SETTINGS_FIELD; ?>[sticky_genesis_topbar_medium]" class="large-text" id="sticky_genesis_topbar_medium" value="<?php echo htmlspecialchars( genesis_get_option('sticky_genesis_topbar_medium') ); ?>"/></p>
				<p><span class="description">ie.<code>https://medium.com/@anwerashif/</code></span></p>
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><?php esc_html_e( 'Tumblr URL:', 'sticky_genesis_topbar' ); ?></th>
			<td>
				<p><input type="url" name="<?php echo GENESIS_SETTINGS_FIELD; ?>[sticky_genesis_topbar_tumblr]" class="large-text" id="sticky_genesis_topbar_tumblr" value="<?php echo htmlspecialchars( genesis_get_option('sticky_genesis_topbar_tumblr') ); ?>"/></p>
				<p><span class="description">ie.<code>https://tumblr.com/rainastudio/</code></span></p>
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><?php esc_html_e( 'Vimeo URL:', 'sticky_genesis_topbar' ); ?></th>
			<td>
				<p><input type="url" name="<?php echo GENESIS_SETTINGS_FIELD; ?>[sticky_genesis_topbar_vimeo]" class="large-text" id="sticky_genesis_topbar_vimeo" value="<?php echo htmlspecialchars( genesis_get_option('sticky_genesis_topbar_vimeo') ); ?>"/></p>
				<p><span class="description">ie.<code>https://vimeo.com/rainastudio/</code></span></p>
			</td>
		</tr>
		<tr valign="top">
		<th scope="row"><?php esc_html_e( 'Remove Topbar:', 'sticky_genesis_topbar' ); ?></th>
		<td>
			<fieldset>
			<legend class="screen-reader-text"><?php esc_html_e( 'Remove Topbar', 'sticky_genesis_topbar' ); ?></legend>
			
					<p><label for="sticky_genesis_topbar_on_remove"><input type="checkbox" name="genesis-settings[sticky_genesis_topbar_on_remove]" id="<?php echo GENESIS_SETTINGS_FIELD; ?>[sticky_genesis_topbar_on_remove]" value="1"<?php checked( 1, genesis_get_option( 'sticky_genesis_topbar_on_remove', GENESIS_SETTINGS_FIELD ) ); ?>/></p>

			</fieldset>
		</td>
	</tr>
		</tbody></table>
		<?php
	}
	
	// Register defaults.
	add_filter( 'genesis_theme_settings_defaults', 'sticky_genesis_topbar_settings_defaults' );
	function sticky_genesis_topbar_settings_defaults( $defaults ) {
		
		$defaults['sticky_genesis_topbar_text'] = 'Go to Genesis > Theme Settings to set topbar';
		
		$defaults['sticky_genesis_topbar_url'] = admin_url() . 'admin.php?page=genesis';
		
		$defaults['sticky_genesis_topbar_btn_text'] = 'Set Now';
	 
		return $defaults;
	}
	
	// Sanitization.
	add_action( 'genesis_settings_sanitizer_init', 'sticky_genesis_topbar_sanitization_filters' );
	
	function sticky_genesis_topbar_sanitization_filters() {
		
		genesis_add_option_filter( 'safe_html', GENESIS_SETTINGS_FIELD, array( 'sticky_genesis_topbar_text' ) );
		genesis_add_option_filter( 'safe_html', GENESIS_SETTINGS_FIELD, array( 'sticky_genesis_topbar_url' ) );
		genesis_add_option_filter( 'safe_html', GENESIS_SETTINGS_FIELD, array( 'sticky_genesis_topbar_btn_text' ) );
		genesis_add_option_filter( 'safe_html', GENESIS_SETTINGS_FIELD, array( 'sticky_genesis_topbar_facebook' ) );
		genesis_add_option_filter( 'safe_html', GENESIS_SETTINGS_FIELD, array( 'sticky_genesis_topbar_twitter' ) );
		genesis_add_option_filter( 'safe_html', GENESIS_SETTINGS_FIELD, array( 'sticky_genesis_topbar_linkedIn' ) );
		genesis_add_option_filter( 'safe_html', GENESIS_SETTINGS_FIELD, array( 'sticky_genesis_topbar_pinterest' ) );
		genesis_add_option_filter( 'safe_html', GENESIS_SETTINGS_FIELD, array( 'sticky_genesis_topbar_instagram' ) );
		genesis_add_option_filter( 'safe_html', GENESIS_SETTINGS_FIELD, array( 'sticky_genesis_topbar_youtube' ) );
		genesis_add_option_filter( 'safe_html', GENESIS_SETTINGS_FIELD, array( 'sticky_genesis_topbar_dribbble' ) );
		genesis_add_option_filter( 'safe_html', GENESIS_SETTINGS_FIELD, array( 'sticky_genesis_topbar_googleplus' ) );
		genesis_add_option_filter( 'safe_html', GENESIS_SETTINGS_FIELD, array( 'sticky_genesis_topbar_medium' ) );
		genesis_add_option_filter( 'safe_html', GENESIS_SETTINGS_FIELD, array( 'sticky_genesis_topbar_tumblr' ) );
		genesis_add_option_filter( 'safe_html', GENESIS_SETTINGS_FIELD, array( 'sticky_genesis_topbar_vimeo' ) );
		genesis_add_option_filter( 'one_zero', GENESIS_SETTINGS_FIELD, array( 'sticky_genesis_topbar_on_remove' ) );
	}
	
	// Remove topbar if checkbox on
	$topbar 		= genesis_get_option( 'sticky_genesis_topbar_on_remove', GENESIS_SETTINGS_FIELD);
	if ( $topbar == 1 ) {
		remove_action( 'genesis_before_header', 'sticky_genesis_topbar_before_header' );
	} else {
		// Register 'topbar' before genesis_before_header
		add_action( 'genesis_before_header', 'sticky_genesis_topbar_before_header' );
		
		function sticky_genesis_topbar_before_header() {
				$facebookurl 	= genesis_get_option('sticky_genesis_topbar_facebook');
				$twitterurl		= genesis_get_option( 'sticky_genesis_topbar_twitter');
				$linkedinurl	= genesis_get_option( 'sticky_genesis_topbar_linkedIn');
				$pinteresturl	= genesis_get_option( 'sticky_genesis_topbar_pinterest');
				$instagramurl	= genesis_get_option( 'sticky_genesis_topbar_instagram');
				$youtubeurl		= genesis_get_option( 'sticky_genesis_topbar_youtube');
				$dribbbleurl	= genesis_get_option( 'sticky_genesis_topbar_dribbble');
				$googleplusurl	= genesis_get_option( 'sticky_genesis_topbar_googleplus');
				$mediumurl		= genesis_get_option( 'sticky_genesis_topbar_medium');
				$tumblrurl		= genesis_get_option( 'sticky_genesis_topbar_tumblr');
				$vimeourl		= genesis_get_option( 'sticky_genesis_topbar_vimeo');
			?><div class="topbar">
				<div class="wrap">
					<div class="promo-topbar">
						<p><?php echo genesis_get_option('sticky_genesis_topbar_text'); ?></p>
						<a class="button promo" href="<?php echo genesis_get_option('sticky_genesis_topbar_url'); ?>"><?php echo genesis_get_option('sticky_genesis_topbar_btn_text'); ?></a>
					</div>
					<div class="social-icons">
						<?php if (!empty($facebookurl)) {?>
							<a href="<?php echo $facebookurl; ?>" target="_blank" class="sm-icons facebook"><i class="fa fa-facebook"></i></a> <?php } ?>
						<?php if (!empty($twitterurl)) {?>
							<a href="<?php echo $twitterurl; ?>" target="_blank" class="sm-icons twitter"><i class="fa fa-twitter"></i></a> <?php } ?>
						<?php if (!empty($linkedinurl)) {?>
							<a href="<?php echo $linkedinurl; ?>" target="_blank" class="sm-icons linkedin"><i class="fa fa-linkedin"></i></a> <?php } ?>
						<?php if (!empty($pinteresturl)) {?>
							<a href="<?php echo $pinteresturl; ?>" target="_blank" class="sm-icons pinterest"><i class="fa fa-pinterest"></i></a> <?php } ?>
						<?php if (!empty($instagramurl)) {?>
							<a href="<?php echo $instagramurl; ?>" target="_blank" class="sm-icons instagram"><i class="fa fa-instagram"></i></a> <?php } ?>
						<?php if (!empty($youtubeurl)) {?>
							<a href="<?php echo $youtubeurl; ?>" target="_blank" class="sm-icons youtube"><i class="fa fa-youtube"></i></a> <?php } ?>
						<?php if (!empty($dribbbleurl)) {?>
							<a href="<?php echo $dribbbleurl; ?>" target="_blank" class="sm-icons dribbble"><i class="fa fa-dribbble"></i></a> <?php } ?>
						<?php if (!empty($googleplusurl)) {?>
							<a href="<?php echo $googleplusurl; ?>" target="_blank" class="sm-icons googleplus"><i class="fa fa-google-plus"></i></a> <?php } ?>
						<?php if (!empty($mediumurl)) {?>
							<a href="<?php echo $mediumurl; ?>" target="_blank" class="sm-icons medium"><i class="fa fa-medium"></i></a> <?php } ?>
						<?php if (!empty($tumblrurl)) {?>
							<a href="<?php echo $tumblrurl; ?>" target="_blank" class="sm-icons tumblr"><i class="fa fa-tumblr"></i></a> <?php } ?>
						<?php if (!empty($vimeourl)) {?>
							<a href="<?php echo $vimeourl; ?>" target="_blank" class="sm-icons vimeo"><i class="fa fa-vimeo"></i></a> <?php } ?>
					</div>
				</div>
			</div><?php
		}
	}
	
	// Call plugin's scripts & stylesheet.
	add_action( 'wp_enqueue_scripts', 'sticky_genesis_topbar_scripts' );
	function sticky_genesis_topbar_scripts() {
		if ( !is_admin() ) {
		
			wp_enqueue_style( 'app-topbar-css', sticky_genesis_topbar_css . 'style.css' );
			wp_enqueue_style( 'app-topbar-fontawesome-css', sticky_genesis_topbar_css . 'font-awesome.css' );
			wp_register_script( 'app-topbar-js', sticky_genesis_topbar_js . 'topbar.js', array( 'jquery' ), sticky_genesis_topbar_version, true );
			wp_enqueue_script( 'app-topbar-js' );
		
		}
	}
	
}