<?php
// adding styles and scripts
function brill_theme_add_editor_styles()
{
	add_editor_style('brill-editor-style.css');
}
add_action('admin_init', 'brill_theme_add_editor_styles');
function brill_enqueue_script()
{
	wp_enqueue_style('themestyle', get_template_directory_uri() . '/style.css', false, '1.1', 'all');
	wp_enqueue_script('bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array(
		'jquery'
	) , '4.1.3', true);
	wp_enqueue_script('mainjs', get_template_directory_uri() . '/js/main.js', array(
		'jquery',
		'bootstrap'
	) , '1.0', true);
}
add_action('wp_enqueue_scripts', 'brill_enqueue_script');
if (!isset($content_width)) {
	$content_width = 1200;
}
// add theme support
function brill_theme_support()
{
	add_theme_support('title-tag');
	add_theme_support('automatic-feed-links');
	add_theme_support('post-thumbnails');
	add_theme_support("custom-header");
	add_theme_support("custom-background");
	add_image_size("post-thumbnail", 365, 225, true);
	// add_image_size("banner", 1920, 550, true);
	$defaults = array(
		'height' => 75,
		'width' => 300,
		'flex-height' => true,
		'flex-width' => true,
		'header-text' => array(
			'site-title',
			'site-description'
		) ,
	);
	add_theme_support('custom-logo', $defaults);
	add_theme_support('post-formats', array(
		'aside',
		'gallery',
		'link',
		'image',
		'quote',
		'status',
		'video',
		'audio',
		'chat'
	));
	add_theme_support('woocommerce');
	add_filter('woocommerce_enqueue_styles', '__return_empty_array');
}
add_action('after_setup_theme', 'brill_theme_support');
// dashborad support widget
add_action('wp_dashboard_setup', 'brill_dashborad_widget');
function brill_dashborad_widget()
{
	wp_add_dashboard_widget('dashboard_1', 'Need Help?', 'brill_contact_widget', null, null);
}
function brill_contact_widget()
{
	echo "Contact <a href='https://www.brillinfotech.com' target='_blank'>Brill Infotech</a> for any help";
}
// register menu
function brill_register_menus()
{
	register_nav_menus(array(
		'primary-menu' => __('Primary Menu', 'brill') ,
		'footer-menu' => __('Footer Menu', 'brill') ,
		'social-menu' => __('Social Menu', 'brill')
	));
}
add_action('init', 'brill_register_menus');
 
// set copyright text
function create_copyright()
{
	echo '<div class="copy-right"><div class="container"><div class="row"><div class="col-12 col-md-6">';
	echo get_option('brill_footer_copyright_text');
	echo '</div><div class="col-12 col-md-6 footer-right">';
	echo "Theme by <a href='https://www.brillinfotech.com' target='_blank'>Brill Infotech</a>";
	echo '</div></div></div></div>';
}
//  register sidebars
function brill_widgets_init()
{
	register_sidebar(array(
		'name' => sprintf(esc_html__('Home Top Widget Area', 'brill') , 1) ,
		'id' => 'home-2',
		'before_widget' => '',
		'after_widget' => '',
		'before_title' => '',
		'after_title' => '',
	));
	register_sidebar(array(
		'name' => sprintf(esc_html__('Home Services Widget Area', 'brill') , 1) ,
		'id' => 'home-1',
		'before_widget' => '',
		'after_widget' => '',
		'before_title' => '<h2 class="widget-title">',
		'after_title' => '</h2>',
	));
	register_sidebar(array(
		'name' => sprintf(esc_html__('Blog Side bar', 'brill') , 1) ,
		'id' => 'sidebar-1',
		'before_widget' => '<section id="%1$s" class="col no-flex-mobile widget %2$s">',
		'after_widget' => '</section>',
		'before_title' => '<h2 class="widget-title">',
		'after_title' => '</h2>',
	));
	register_sidebar(array(
		'name' => sprintf(esc_html__('Footer Widget', 'brill') , 1) ,
		'id' => 'footer-1',
		'before_widget' => '<section id="%1$s" class="col col-12 col-sm-3 no-flex-mobile widget %2$s">',
		'after_widget' => '</section>',
		'before_title' => '<h5 class="widget-title">',
		'after_title' => '</h5>',
	));
}
add_action('widgets_init', 'brill_widgets_init');
// extend widgets
class Brill_Services_Widget extends WP_Widget

{
	/**
	 * Register widget with WordPress.
	 */
	function __construct()
	{
		$widget_ops = array(
			'classname' => 'brill_widget',
			'description' => 'Use thiss widget to show service section at home page',
		);
		parent::__construct('brill_widget', '<strong>Brill: </strong> Home Service', $widget_ops);
	}
	/**
	 * Front-end display of widget.
	 *
	 * @see WP_Widget::widget()
	 *
	 * @param array $args     Widget arguments.
	 * @param array $instance Saved values from database.
	 */
	public function widget($args, $instance)

	{
		$ctaPage = $instance['cta_page'] ? get_page_link($instance['cta_page']) : '';
		echo $args['before_widget'];
		echo "<div class='col col-12 col-sm-3 brill-service'><section id='' class='no-flex-mobile widget brill_widget'><a href='" . $ctaPage . "' title='" . $instance['title'] . "'>";
		if (!empty($instance['title'])) {
			echo $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title'];
		}
		if (!empty($instance['desc'])) {
			echo $instance['desc'];
		}
		echo "<br /><i class='fas fa-angle-double-right visit-page'></i></a></section></div>";
		echo $args['after_widget'];
	}
	/**
	 * Back-end widget form.
	 *
	 * @see WP_Widget::form()
	 *
	 * @param array $instance Previously saved values from database.
	 */
	public function form($instance)

	{
		$title = !empty($instance['title']) ? $instance['title'] : esc_html__('Enter title', 'brill');
		$desc = !empty($instance['desc']) ? $instance['desc'] : esc_html__('Enter description here', 'brill');
		$instance['cta_page'] =  !empty($instance['cta_page']) ? $instance['cta_page'] : "1"
?>
		<p>
		<label for="<?php
		echo esc_attr($this->get_field_id('title')); ?>"><?php
		esc_attr_e('Title:', 'brill'); ?></label> 
		<input class="widefat" id="<?php
		echo esc_attr($this->get_field_id('title')); ?>" name="<?php
		echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php
		echo esc_attr($title); ?>">
		</p>

		<p>
		<label for="<?php
		echo esc_attr($this->get_field_id('desc')); ?>"><?php
		esc_attr_e('Description:', 'brill'); ?></label> 
		<textarea rows="7" class="widefat" id="<?php
		echo esc_attr($this->get_field_id('desc')); ?>" name="<?php
		echo esc_attr($this->get_field_name('desc')); ?>"><?php
		echo esc_attr($desc); ?></textarea>
		</p>
		<label for="<?php
		echo esc_attr($this->get_field_id('cta_page')); ?>"><?php
		esc_attr_e('Linked Page:', 'brill'); ?></label> 
		<?php
		wp_dropdown_pages(array(
			'id' => $this->get_field_id('cta_page') ,
			'class' => 'widefat',
			'name' => $this->get_field_name('cta_page') ,
			'selected' => $instance['cta_page'],
			'show_option_none' => esc_html__('&mdash; Select &mdash;', 'brill') ,
		));; ?>
		<?php
	}
	/**
	 * Sanitize widget form values as they are saved.
	 *
	 * @see WP_Widget::update()
	 *
	 * @param array $new_instance Values just sent to be saved.
	 * @param array $old_instance Previously saved values from database.
	 *
	 * @return array Updated safe values to be saved.
	 */
	public function update($new_instance, $old_instance)

	{
		$instance = array();
		$instance['title'] = (!empty($new_instance['title'])) ? sanitize_text_field($new_instance['title']) : '';
		$instance['desc'] = (!empty($new_instance['desc'])) ? sanitize_text_field($new_instance['desc']) : '';
		$instance['cta_page'] = (!empty($new_instance['cta_page'])) ? sanitize_text_field($new_instance['cta_page']) : '';
		return $instance;
	}
}
function register_brill_widget()
{
	register_widget('Brill_Services_Widget');
}
add_action('widgets_init', 'register_brill_widget');
function brill_load_more_scripts()
{
	global $wp_query;
	wp_register_script('my_loadmore', get_stylesheet_directory_uri() . '/js/brill-loadmore.js', array(
		'jquery'
	));
	wp_localize_script('my_loadmore', 'brill_loadmore_params', array(
		'ajaxurl' => site_url() . '/wp-admin/admin-ajax.php', // WordPress AJAX
		'posts' => json_encode($wp_query->query_vars) , // everything about your loop is here
		'current_page' => get_query_var('paged') ? get_query_var('paged') : 1,
		'max_page' => $wp_query->max_num_pages
	));
	wp_enqueue_script('my_loadmore');
}
add_action('wp_enqueue_scripts', 'brill_load_more_scripts');
function brill_loadmore_ajax_handler()
{
	// prepare our arguments for the query
	$args = json_decode(stripslashes($_POST['query']) , true);
	$args['paged'] = $_POST['page'] + 1; // we need next page to be loaded
	$args['post_status'] = 'publish';
	// it is always better to use WP_Query but not here
	query_posts($args);
	if (have_posts()):
		// run the loop
		while (have_posts()):
			the_post();
			get_template_part('template-parts/content', get_post_format());
		endwhile;
	endif;
	die;
}
add_action('wp_ajax_loadmore', 'brill_loadmore_ajax_handler'); // wp_ajax_{action}
add_action('wp_ajax_nopriv_loadmore', 'brill_loadmore_ajax_handler'); // wp_ajax_nopriv_{action}
function brill_customize_register($wp_customize)
{
	$wp_customize->add_section('brill_home_settings', array(
		'title' => 'Home page content & settings',
		'description' => '',
		'priority' => 1,
	));
	//  =============================
	//  = Text Input                =
	//  =============================
	$wp_customize->add_setting('brill_services_heading', array(
		'default' => 'Our Services',
		'capability' => 'edit_theme_options',
		'type' => 'option',
		'sanitize_callback' => 'esc_attr'
	));
	$wp_customize->add_control('brill_service_heading_control', array(
		'label' => "Service Section Heading",
		'section' => 'brill_home_settings',
		'settings' => 'brill_services_heading',
	));
	$wp_customize->add_setting('brill_services_desc', array(
		'default' => 'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.',
		'capability' => 'edit_theme_options',
		'type' => 'option',
		'sanitize_callback' => 'esc_attr'
	));
	$wp_customize->add_control('brill_service_desc_control', array(
		'label' => "Service Section short Description",
		'section' => 'brill_home_settings',
		'settings' => 'brill_services_desc',
	));
	$wp_customize->add_setting('brill_recent_post_heading', array(
		'default' => 'Recent Post',
		'capability' => 'edit_theme_options',
		'type' => 'option',
		'sanitize_callback' => 'esc_attr'
	));
	$wp_customize->add_control('brill_recent_post_heading_control', array(
		'label' => "Recent Post section Heading",
		'section' => 'brill_home_settings',
		'settings' => 'brill_recent_post_heading',
	));
	$wp_customize->add_setting('brill_blog_btn_text', array(
		'default' => 'All Post',
		'capability' => 'edit_theme_options',
		'type' => 'option',
		'sanitize_callback' => 'esc_attr'
	));
	$wp_customize->add_control('brill_blog_btn_text_control', array(
		'label' => "Recent Post button text",
		'section' => 'brill_home_settings',
		'settings' => 'brill_blog_btn_text',
	));
	$wp_customize->add_setting('brill_footer_copyright_text', array(
		'default' => 'copyright brillinfotech.com',
		'capability' => 'edit_theme_options',
		'type' => 'option',
		'sanitize_callback' => 'esc_attr'
	));
	$wp_customize->add_control('brill_footer_copyright_text_control', array(
		'label' => "Footer Copyright Text",
		'section' => 'brill_home_settings',
		'settings' => 'brill_footer_copyright_text',
	));
}
add_action('customize_register', 'brill_customize_register');
function wp_get_attachment($attachment_id)
{
	$attachment = get_post($attachment_id);
	return array(
		'alt' => get_post_meta($attachment->ID, '_wp_attachment_image_alt', true) ,
		'caption' => $attachment->post_excerpt,
		'description' => $attachment->post_content,
		'href' => get_permalink($attachment->ID) ,
		'src' => $attachment->guid,
		'title' => $attachment->post_title
	);
}
add_filter('post_gallery', 'customFormatGallery', 10, 2);
function customFormatGallery($string, $attr)
{
	$output = "<ul class='brill-gallery row'>";
	$posts = get_posts(array(
		'include' => $attr['ids'],
		'post_type' => 'attachment'
	));
	foreach($posts as $imagePost) {
		$output.= '<li class="col-6 col-md-3"><a href="#" class="brill-gallery-thumb" title="' . wp_get_attachment($imagePost->ID) ['caption'] . '" data-brillimg="' . wp_get_attachment_image_src($imagePost->ID, 'large') [0] . '" data-toggle="modal"><img src="' . wp_get_attachment_image_src($imagePost->ID) [0] . '" class="img-responsive"></a></li>';
	}
	$output.= "</ul>";
?>
    <script type="text/javascript">		
	var modalId = "#brill<?php
	echo get_the_ID(); ?>"
	</script>

			<div class="modal fade" id="brill<?php
	echo get_the_ID(); ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
	  <div class="modal-dialog" role="document">
	    <div class="modal-content">
	      <div class="modal-header">
	        <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5>
	        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
	          <span aria-hidden="true">&times;</span>
	        </button>
	      </div>
	      <div class="modal-body">
	       

	      </div>
	      
	    </div>
	  </div>
	</div>

<?php
	return $output;
}
if (is_singular() && comments_open() && get_option('thread_comments')) {
	wp_enqueue_script('comment-reply');
}


/**
 * This file represents an example of the code that themes would use to register
 * the required plugins.
 *
 * It is expected that theme authors would copy and paste this code into their
 * functions.php file, and amend to suit.
 *
 * @see http://tgmpluginactivation.com/configuration/ for detailed documentation.
 *
 * @package    TGM-Plugin-Activation
 * @subpackage Example
 * @version    2.6.1 for parent theme Brill for publication on WordPress.org
 * @author     Thomas Griffin, Gary Jones, Juliette Reinders Folmer
 * @copyright  Copyright (c) 2011, Thomas Griffin
 * @license    http://opensource.org/licenses/gpl-2.0.php GPL v2 or later
 * @link       https://github.com/TGMPA/TGM-Plugin-Activation
 */

/**
 * Include the TGM_Plugin_Activation class.
 *
 * Depending on your implementation, you may want to change the include call:
 *
 * Parent Theme:
 * require_once get_template_directory() . '/path/to/class-tgm-plugin-activation.php';
 *
 * Child Theme:
 * require_once get_stylesheet_directory() . '/path/to/class-tgm-plugin-activation.php';
 *
 * Plugin:
 * require_once dirname( __FILE__ ) . '/path/to/class-tgm-plugin-activation.php';
 */
require_once get_template_directory() . '/plugins/class-tgm-plugin-activation.php';

add_action( 'tgmpa_register', 'brill_register_required_plugins' );

/**
 * Register the required plugins for this theme.
 *
 * In this example, we register five plugins:
 * - one included with the TGMPA library
 * - two from an external source, one from an arbitrary source, one from a GitHub repository
 * - two from the .org repo, where one demonstrates the use of the `is_callable` argument
 *
 * The variables passed to the `tgmpa()` function should be:
 * - an array of plugin arrays;
 * - optionally a configuration array.
 * If you are not changing anything in the configuration array, you can remove the array and remove the
 * variable from the function call: `tgmpa( $plugins );`.
 * In that case, the TGMPA default settings will be used.
 *
 * This function is hooked into `tgmpa_register`, which is fired on the WP `init` action on priority 10.
 */
function brill_register_required_plugins() {
	/*
	 * Array of plugin arrays. Required keys are name and slug.
	 * If the source is NOT from the .org repo, then source is also required.
	 */
	$plugins = array(

		

		// This is an example of how to include a plugin from an arbitrary external source in your theme.
		array(
			'name'         => 'Smart Slider 3', // The plugin name.
			'slug'         => 'smart-slider-3', // The plugin slug (typically the folder name).
			'source'       => 'https://downloads.wordpress.org/plugin/smart-slider-3.3.3.11.zip', // The plugin source.
			'required'     => true, // If false, the plugin is only 'recommended' instead of required.
			'force_activation'   => true, // If true, plugin is activated upon theme activation and cannot be deactivated until theme switch.
			
		),

	);

	/*
	 * Array of configuration settings. Amend each line as needed.
	 *
	 * TGMPA will start providing localized text strings soon. If you already have translations of our standard
	 * strings available, please help us make TGMPA even better by giving us access to these translations or by
	 * sending in a pull-request with .po file(s) with the translations.
	 *
	 * Only uncomment the strings in the config array if you want to customize the strings.
	 */
	$config = array(
		'id'           => 'brill',                 // Unique ID for hashing notices for multiple instances of TGMPA.
		'default_path' => '',                      // Default absolute path to bundled plugins.
		'menu'         => 'tgmpa-install-plugins', // Menu slug.
		'has_notices'  => true,                    // Show admin notices or not.
		'dismissable'  => true,                    // If false, a user cannot dismiss the nag message.
		'dismiss_msg'  => '',                      // If 'dismissable' is false, this message will be output at top of nag.
		'is_automatic' => false,                   // Automatically activate plugins after installation or not.
		'message'      => '',                      // Message to output right before the plugins table.

		
	);

	tgmpa( $plugins, $config );
}
