<?php
/**
 * Plugin Name: Hercules Core 
 * Plugin URI: http://www.hercules-design.com
 * Description: This plugin adds Gallery, FAQ custom post types, shortcodes and demo importer.
 * Version: 1.9
 * Author: HerculesDesign
 * Author URI: 
 * License: GPL2
 */
 // Blocking direct access
if( ! function_exists( 'hercules_block_direct_access' ) ) {
	function hercules_block_direct_access() {
		if( ! defined( 'ABSPATH' ) ) {
			exit( 'Direct access denied.' );
		}
	}
}
require 'plugin-update-checker/plugin-update-checker.php';
$myUpdateChecker = Puc_v4_Factory::buildUpdateChecker(
    'http://novablog-plugins.hercules-design.com/metadata.json',
    __FILE__,
    'hercules-core'
);

global $novablog_options;
//Loading widgets
function novablog_register_widgets() {
  $novablog_widgets = array(
    'hs-recent-popular-tab-widget'         => 'hs_recent_popular_tab_widget',
    'novablog_aboutme'      => 'novablog_aboutmebox',
    'novablog-125-banner'       => 'novablog_Ad_125_125_Widget',
    'novablog-banner'         => 'novablog_banner_widget',
    'novablog-comment-widget'        => 'novablog_CommentWidget',
    'novablog-facebook-widget'     => 'novablog_Facebook_Widget',
	'novablog-flickr-widget'     => 'novablog_FlickrWidget',
	'novablog-instagram-widget'     => 'Novablog_InstagramWidget',
	'novablog-social-widget'     => 'novablog_SocialNetworksWidget',
	'novablog-twitter-class-widget'     => 'Novablog_Twitter',
	'novablog-recent-news-widget'     => 'novablog_RecentNewsWidget',
  );
  foreach ( $novablog_widgets as $key => $value ) {
    require_once plugin_dir_path( __FILE__ ) . '/widgets/'. sanitize_key( $key ) .'.php';
    register_widget( $value );
  }
}

add_action( 'widgets_init', 'novablog_register_widgets' );

//Shortcodes
	require_once plugin_dir_path( __FILE__ ) . '/theme_shortcodes/columns.php';
	require_once plugin_dir_path( __FILE__ ) . '/theme_shortcodes/shortcodes.php';
	require_once plugin_dir_path( __FILE__ ) . '/theme_shortcodes/posts_grid.php';
	require_once plugin_dir_path( __FILE__ ) . '/theme_shortcodes/tabs.php';
	require_once plugin_dir_path( __FILE__ ) . '/theme_shortcodes/toggle.php';
	require_once plugin_dir_path( __FILE__ ) . '/theme_shortcodes/html.php';
	require_once plugin_dir_path( __FILE__ ) . '/theme_shortcodes/progressbar.php';
	require_once plugin_dir_path( __FILE__ ) . '/theme_shortcodes/skills.php';
	require_once plugin_dir_path( __FILE__ ) . '/theme_shortcodes/table.php';
//tinyMCE includes
	include(plugin_dir_path( __FILE__ ) . '/theme_shortcodes/tinymce/tinymce_shortcodes.php');
/* Gallery */
function novablog_post_type_gallery() {
$labels = array(
		'name'               => esc_html__( 'Gallery', 'post type general name', 'novablog' ),
		'singular_name'      => esc_html__( 'Gallery', 'post type singular name', 'novablog' ),
		'menu_name'          => esc_html__( 'Gallery', 'admin menu', 'novablog' ),
		'name_admin_bar'     => esc_html__( 'Gallery', 'add new on admin bar', 'novablog' ),
		'add_new'            => esc_html__( 'Add New Image', 'image', 'novablog' ),
		'add_new_item'       => esc_html__( 'Add New Image', 'novablog' ),
		'new_item'           => esc_html__( 'New Image', 'novablog' ),
		'edit_item'          => esc_html__( 'Edit Image', 'novablog' ),
		'view_item'          => esc_html__( 'View Image', 'novablog' ),
		'all_items'          => esc_html__( 'All Images', 'novablog' ),
		'search_items'       => esc_html__( 'Search Images', 'novablog' ),
		'parent_item_colon'  => esc_html__( 'Parent Images:', 'novablog' ),
		'not_found'          => esc_html__( 'No image found.', 'novablog' ),
		'not_found_in_trash' => esc_html__( 'No images found in Trash.', 'novablog' ),
		 'featured_image' => esc_html__( 'Image', 'novablog' ),
'set_featured_image' => esc_html__( 'Set image', 'novablog' ),
'remove_featured_image' => esc_html__( 'Remove image', 'novablog' ),
'use_featured_image' => esc_html__( 'Use image', 'novablog' )
	);
	register_post_type( 'gallery',
		array( 
				'labels'             => $labels,
				'singular_label'    => esc_html__( 'Gallery', 'novablog' ),
				'public'             => true,
		'publicly_queryable' => true,
		'show_ui'            => true,
		'show_in_menu'        => true,
		'show_in_nav_menus'   => true,
	'show_in_admin_bar'   => true,
		'query_var'          => true,
		'has_archive'        => true,
		'show_admin_column' => true,
		'hierarchical'       => true,
		'menu_position'      => null,
		'register_meta_box_cb' => 'novablog_add_box_gallery',
				'capability_type'   => 'post',
				'rewrite'           => array(
					'slug'       => 'gallery-view',
					'pages' => true,
				),
				'supports' => array(
						'title',
						'editor',
						'thumbnail', 'comments')
					) 
				);
	register_taxonomy('gallery_categories', 'gallery', array('hierarchical' => true, 'labels' => array(
                'name' => esc_html__( 'Gallery Categories', 'novablog' ),
                'add_new_item' => esc_html__( 'Add New Gallery Category', 'novablog' ),
                'new_item_name' => esc_html__( 'New Gallery Category', 'novablog' ),
            ), 'rewrite' => true, 'query_var' => true, 'show_admin_column' => true));
	
}
add_action('init', 'novablog_post_type_gallery');
/* Featured Image position */


function novablog_move_meta_box(){
   remove_meta_box( 'postimagediv', 'gallery', 'side' );
    add_meta_box('postimagediv', esc_html__( 'Image', 'novablog' ), 'post_thumbnail_meta_box', 'gallery', 'normal', 'high');
	}
add_action('do_meta_boxes', 'novablog_move_meta_box');
/* FAQs */
function novablog_post_type_faq() {
	register_post_type('faq', 
				array(
				'label' => esc_html__('FAQs', 'novablog'),
				'singular_label' => esc_html__('FAQ', 'novablog'),
				'public' => true,
				'show_ui' => true,
				'_builtin' => false, // It's a custom post type, not built in
				'_edit_link' => 'post.php?post=%d',
				'capability_type' => 'post',
				'hierarchical' => false,
				'show_admin_column' => true,
				'rewrite' => array("slug" => "faq"), // Permalinks
				'query_var' => "faq", // This goes to the WP_Query schema
				'supports' => array('title','author','editor'),
				'menu_position' => 5,
				'publicly_queryable' => true,
				'exclude_from_search' => false,
				));
	register_taxonomy(
        'faq_categories',
        'faq',
        array(
            'labels' => array(
                'name' => 'FAQ Categories',
                'add_new_item' => esc_html__('Add New FAQ Category', 'novablog' ),
                'new_item_name' => esc_html__('New FAQ Category', 'novablog' )
            ),
            'show_ui' => true,
            'hierarchical' => true,
            'hasArchive' => true,
			'show_admin_column' => true
        )
    );
	
}
add_action('init', 'novablog_post_type_faq');

/*-----------------------------------------------------------------------------------
	Metaboxes for gallery
-----------------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------------*/
/*	Define Metabox Fields
/*-----------------------------------------------------------------------------------*/

$hr_prefix = 'novablog_';
 
$hercules_meta_box_gallery_page = array(
	'id' => 'my-meta-box-gallery',
	'title' =>  esc_html__('Options', 'novablog'),
	'page' => 'gallery',
	'context' => 'normal',
	'priority' => 'high',
	'fields' => array(
		array(
    	   'name' => esc_html__('Our role', 'novablog'),
    	   'desc' => '',
    	   'id' => $hr_prefix . 'gallery_role',
    	   'type' => 'text',
    	   'std' => ''
    	),
		array(
    	   'name' => esc_html__('Year', 'novablog'),
    	   'desc' => '',
    	   'id' => $hr_prefix . 'gallery_year',
    	   'type' => 'text',
    	   'std' => ''
    	),	
		array(
    	   'name' => esc_html__('Client', 'novablog'),
    	   'desc' => '',
    	   'id' => $hr_prefix . 'gallery_client',
    	   'type' => 'text',
    	   'std' => ''
    	),
				array(
    	   'name' => esc_html__('Technology', 'novablog'),
    	   'desc' => '',
    	   'id' => $hr_prefix . 'gallery_technology',
    	   'type' => 'text',
    	   'std' => ''
    	),
		array(
    	   'name' => esc_html__('URL', 'novablog'),
    	   'desc' => '',
    	   'id' => $hr_prefix . 'gallery_url',
    	   'type' => 'text',
    	   'std' => ''
    	),
		array( "name" => esc_html__('URL target','novablog'),
				"desc" => '',
				"id" => $hr_prefix . 'gallery_target',
				"type" => "select",
				"std" => esc_html__('blank','novablog'),
				"options" => array(esc_html__('blank','novablog'),esc_html__('self','novablog'))
			),
		array( "name" => esc_html__('When you click the thumbnail image, do you want to url led to a separate page?','novablog'),
				"desc" => '',
				"id" => $hr_prefix . 'url_separate_window',
				"type" => "select",
				"std" => esc_html__('no','novablog'),
				"options" => array(esc_html__('no','novablog'),esc_html__('yes','novablog'))
			)				
	)
);

add_action('admin_menu', 'novablog_add_box_gallery');


/*-----------------------------------------------------------------------------------*/
/*	Add metabox to edit page
/*-----------------------------------------------------------------------------------*/
 
function novablog_add_box_gallery() {
	global $hercules_meta_box_gallery_page;
	add_meta_box($hercules_meta_box_gallery_page['id'], $hercules_meta_box_gallery_page['title'], 'novablog_show_box_gallery_core', $hercules_meta_box_gallery_page['page'], $hercules_meta_box_gallery_page['context'], $hercules_meta_box_gallery_page['priority']);

}

/*-----------------------------------------------------------------------------------*/
/*	Callback function to show fields in meta box
/*-----------------------------------------------------------------------------------*/

function novablog_show_box_gallery_core() {
	global $hercules_meta_box_gallery_page, $post;
 	
	echo '<p style="padding:10px 0 0 0;">'.esc_html__('Please fill additional fields. ', 'novablog').'</p>';
	// Use nonce for verification
	echo '<input type="hidden" name="novablog_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
 
	echo '<table class="form-table">';
 
	foreach ($hercules_meta_box_gallery_page['fields'] as $field) {
		// get current post meta data
		$meta = get_post_meta($post->ID, $field['id'], true);
		switch ($field['type']) {
 
			//If Text		
			case 'text':
			
			echo '<tr style="border-top:1px solid #eeeeee;">',
				'<th style="width:25%"><label for="', $field['id'], '"><strong>', $field['name'], '</strong><span style=" display:block; color:#999; margin:5px 0 0 0; line-height: 18px;">'. $field['desc'].'</span></label></th>',
				'<td>';
			echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : stripslashes(htmlspecialchars(( $field['std']), ENT_QUOTES)), '" size="30" style="width:75%; margin-right: 20px; float:left;" />';
			
			break;
			
			//If textarea		
			case 'textarea':
			
			echo '<tr style="border-top:1px solid #eeeeee;">',
				'<th style="width:25%"><label for="', $field['id'], '"><strong>', $field['name'], '</strong><span style="line-height:18px; display:block; color:#999; margin:5px 0 0 0;">'. $field['desc'].'</span></label></th>',
				'<td>';
			echo '<textarea name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : stripslashes(htmlspecialchars(( $field['std']), ENT_QUOTES)), '" rows="8" cols="5" style="width:100%; margin-right: 20px; float:left;">', $meta ? $meta : stripslashes(htmlspecialchars(( $field['std']), ENT_QUOTES)), '</textarea>';
			
			break;
			
			//If Select	
			case 'select':
			
				echo '<tr>',
				'<th style="width:25%"><label for="', $field['id'], '"><strong>', $field['name'], '</strong><span style=" display:block; color:#999; margin:5px 0 0 0; line-height: 18px;">'. $field['desc'].'</span></label></th>',
				'<td>';
			
				echo'<select id="' . $field['id'] . '" name="'.$field['id'].'">';
			
				foreach ($field['options'] as $option) {
					
					echo'<option';
					if ($meta == $option ) { 
						echo ' selected="selected"'; 
					}
					echo'>'. $option .'</option>';
				
				} 
				
				echo'</select>';
			
			break; 
			
		}

	}
 
	echo '</table>';
}

 
add_action('save_post', 'novablog_save_data_gallery');

/*-----------------------------------------------------------------------------------*/
/*	Save data when post is edited
/*-----------------------------------------------------------------------------------*/
 
function novablog_save_data_gallery($post_id) {
	global $hercules_meta_box_gallery_page;
 
	// verify nonce
	if (!isset($_POST['novablog_meta_box_nonce']) || !wp_verify_nonce($_POST['novablog_meta_box_nonce'], basename(__FILE__))) {
		return $post_id;
	}
 
	// check autosave
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
		return $post_id;
	}
 
	// check permissions
	if ('page' == $_POST['post_type']) {
		if (!current_user_can('edit_page', $post_id)) {
			return $post_id;
		}
	} elseif (!current_user_can('edit_post', $post_id)) {
		return $post_id;
	}
 
	foreach ($hercules_meta_box_gallery_page['fields'] as $field) {
		$old = get_post_meta($post_id, $field['id'], true);
		$new = $_POST[$field['id']];
 
		if ($new && $new != $old) {
			update_post_meta($post_id, $field['id'], stripslashes(htmlspecialchars($new)));
		} elseif ('' == $new && $old) {
			delete_post_meta($post_id, $field['id'], $old);
		}
	}
	
}

 // Load Redux extensions
   if(!function_exists('novablog_redux_register_custom_extension_loader')) :
	function novablog_redux_register_custom_extension_loader($ReduxFramework) {
		$path = plugin_dir_path( __FILE__ ) . '/extensions/';
		$folders = scandir( $path, 1 );		   
		foreach($folders as $folder) {
			if ($folder === '.' or $folder === '..' or !is_dir($path . $folder) ) {
				continue;	
			} 
			$extension_class = 'ReduxFramework_Extension_' . $folder;
			if( !class_exists( $extension_class ) ) {
				// In case you wanted override your override, hah.
				$class_file = $path . $folder . '/extension_' . $folder . '.php';
				$class_file = apply_filters( 'redux/extension/'.$ReduxFramework->args['opt_name'].'/'.$folder, $class_file );
				if( $class_file ) {
					require_once( $class_file );
					$extension = new $extension_class( $ReduxFramework );
				}
			}
		}
	}
	// Modify {$redux_opt_name} to match your opt_name
	add_action("redux/extensions/novablog_options/before", 'novablog_redux_register_custom_extension_loader', 0);
endif;
add_filter('the_excerpt', 'do_shortcode');
add_filter('widget_text','do_shortcode');
add_filter('the_content', 'novablog_shortcode_empty_paragraph_fix');
function novablog_shortcode_empty_paragraph_fix($content) {
if( is_singular() && is_main_query() ) {
	$array = array (
			'<p>['    => '[', 
			']</p>'   => ']', 
			']<br />' => ']'
	);
	$content = strtr($content, $array);
	}
	return $content;
}
// AUTHOR SOCIAL LINKS
function novablog_contactmethods( $contactmethods ) {

	$contactmethods['twitter']   = esc_html__('Twitter URL', 'novablog');
	$contactmethods['facebook']  = esc_html__('Facebook URL', 'novablog');
	$contactmethods['google']    = esc_html__('Google Plus URL', 'novablog');
	$contactmethods['tumblr']    = esc_html__('Tumblr URL', 'novablog');
	$contactmethods['instagram'] = esc_html__('Instagram URL', 'novablog');
	$contactmethods['pinterest'] = esc_html__('Pinterest URL', 'novablog');
	$contactmethods['snapchat'] = esc_html__('Snapchat URL', 'novablog');

	return $contactmethods;
}
add_filter('user_contactmethods','novablog_contactmethods',10,1);

//google analytics

function novablog_google_analytics() {
global $novablog_options;
if(isset($novablog_options['google_analytics'])){	
    echo $novablog_options['google_analytics']; 
}
}
add_action('wp_head', 'novablog_google_analytics');



add_filter('the_content', 'novablog_ads_before_post_content');
function novablog_ads_before_post_content($content) {
global $novablog_options;
if(is_single() && 'gallery' != get_post_type() && !is_home() && !has_post_format( array('quote','link'))) {


if(isset($novablog_options['custom_ads'])){
$custom_ads = '<div class="custom-ads">'.$novablog_options['custom_ads'].'</div>';
$content = $custom_ads . $content;
}

}
return $content;
}


//category images
require_once plugin_dir_path( __FILE__ ) . '/category-images/class-hercules-category-image.php';
include_once plugin_dir_path( __FILE__ ) . '/widgets-visibility/widgets-visibility.php';
require_once plugin_dir_path( __FILE__ ) . '/slideshow/slideshow.php';
require_once plugin_dir_path( __FILE__ ) . '/custom-login-scripts/login.php';
require_once plugin_dir_path( __FILE__ ) . '/translate/translate-panel.php';


if( ! class_exists( 'Hercules_Likes' ) && $novablog_options['post_likes'] !='no' ) {
require_once plugin_dir_path( __FILE__ ) . '/hercules-likes/class-hercules-likes.php';
}

add_filter('upload_mimes', 'novablog_custom_upload_mimes');
function novablog_custom_upload_mimes ( $existing_mimes=array() ) {
    // add your extension to the mimes array as below
    $existing_mimes['zip'] = 'application/zip';
    $existing_mimes['gz'] = 'application/x-gzip';
	$existing_mimes['otf'] = 'application/x-font-otf';
  	$existing_mimes['woff'] = 'application/x-font-woff';
  	$existing_mimes['ttf'] = 'application/x-font-ttf';
  	$existing_mimes['svg'] = 'image/svg+xml';
  	$existing_mimes['eot'] = 'application/vnd.ms-fontobject';
    return $existing_mimes;
}

if ( !function_exists( 'wbc_extended_example' ) ) {
	function wbc_extended_example( $demo_active_import , $demo_directory_path ) {

		reset( $demo_active_import );
		$current_key = key( $demo_active_import );



		/************************************************************************
		* Setting Menus
		*************************************************************************/

		// If it's demo1 - demo6
		$wbc_menu_array = array( 'lifestyle', 'fashion' );

		//if ( isset( $demo_active_import[$current_key]['directory'] ) && !empty( $demo_active_import[$current_key]['directory'] ) && in_array( $demo_active_import[$current_key]['directory'], $wbc_menu_array ) ) {
			$top_menu = get_term_by( 'name', 'Hercules', 'nav_menu' );

			if ( isset( $top_menu->term_id ) ) {
				set_theme_mod( 'nav_menu_locations', array(
						'primary-menu' => $top_menu->term_id,
						'mobile_menu' => $top_menu->term_id
					)
				);
			}

		//}

// array of demos/homepages to check/select from
		$wbc_home_pages = array(
			'sophisticated' => 'Homepage',
			'feminine' => 'Homepage',
		);
		if ( isset( $demo_active_import[$current_key]['directory'] ) && !empty( $demo_active_import[$current_key]['directory'] ) && array_key_exists( $demo_active_import[$current_key]['directory'], $wbc_home_pages ) ) {
			$page = get_page_by_title( $wbc_home_pages[$demo_active_import[$current_key]['directory']] );
			if ( isset( $page->ID ) ) {
				update_option( 'page_on_front', $page->ID );
				update_option( 'show_on_front', 'page' );
			}
		}

	}


	
	add_action( 'wbc_importer_after_content_import', 'wbc_extended_example', 11, 3 );
}
?>