<?php
/**
 * Articled's functions and definitions
 *
 * @package Articled
 * @since Articled 1.0
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
  exit( '<div style="font-family: monospace;color: red;display: flex;justify-content: center;
  align-items: center;width: 100%;height: 100%;font-size: 40px;font-weight: 600;">Direct script access denied.</div>' );
}


// Const for Getting current file name
define('ABU_CURRENT_FILE', false);
defined( 'ABU_DIR' ) or define( 'ABU_DIR', get_template_directory() );
defined( 'ABU_DEVMODE' ) or define( 'ABU_DEVMODE', true );

if( ! function_exists('get_min') ) {
  function get_min( $t = 'css' ) {
    if( ! ABU_DEVMODE ) {
      return '.min.' . $t;
    }
    return '.' . $t;
  }
}

// Articled theme setup
function after_articled_setup() {
   // Roohani supports
    $_logo_defaults = array(
        'height'      => 150,
        'width'       => 200,
        'flex-height' => true,
        'flex-width'  => true,
        'header-text' => array( 'site-title', 'site-description' ),
    );
    add_theme_support( 'custom-logo', $_logo_defaults );
    add_theme_support( 'customize-selective-refresh-widgets' );
    add_theme_support( 'title-tag' ); // Enable support for title tags.
    add_theme_support( 'post-thumbnails' ); // Enable support for post thumbnails and featured images.
    add_theme_support( 'post-formats', array(
                      'status', 'quote', 'gallery',   // Enable support for the following post formats:
                      'image' , 'video', 'audio',    // aside, gallery, quote, image, and video
                      'link'  , 'aside', 'chat'
                      ) );
    add_theme_support( 'html5', array(
                        'comment-list',
                        'commwwent-form',
                        'search-form',
                        'gallery', 'caption'));
    add_theme_support( 'automatic-feed-links' );
    add_editor_style( 'editor-style.css' );

  	add_image_size('articled_masonry_thumbnail', 455, 310, true);
  	add_image_size( 'articled_60x60', 60, 60, true );

  	add_image_size('articled_full_thumbnail', 900, 420, true);

  	add_image_size('articled_list_thumbnail', 250, 250, false);
    add_image_size('articled_list_thumbnail_srcset', 50, 50, false);

    add_image_size( 'articled_thumbnail_very_low', 44, 22, false );
    add_image_size( 'articled_related-thumbnail', 280, 193, array( 'center', 'center' ) );
    add_image_size( 'articled_related_thumbnail_srcset', 100, 65, false );

    add_image_size('articled_data_src_small', 350, 165, false);
    add_image_size('articled_data_src_medium', 445, 225, false);
    add_image_size('articled_data_src_large', 625, 293, false);



}
add_action( 'after_setup_theme', 'after_articled_setup' );


$default_set_theme_mod = array(
  "articled_blog_post_layout"=> 2,
  "articled_verified" => 1
);
if( get_theme_mod('articled_verified', 0) == 0 ){
  foreach ($default_set_theme_mod as $key => $value) {
    if( get_theme_mod($key, 0) ) continue;
    set_theme_mod($key, $value);
  }
}

// articled wp website contents width
if( !isset($content_width) ) {
  $content_width = 900;
}

// Enqueues every themes css file
if( ! function_exists('articled_enqueue_style') ) {
  function articled_enqueue_style() {

    wp_enqueue_style( 'articled-google-font', '//fonts.googleapis.com/css?family=' . implode( '|', articled_google_fonts() ), array(), '1.0.3', 'all');

    wp_enqueue_style('bootstrap-grid', get_template_directory_uri() . '/inc/third_party/bootstrap/bootstrap-grid' . get_min(), '', '4.0.0', 'all');
    wp_enqueue_style('bootstrap-reboot', get_template_directory_uri() . '/inc/third_party/bootstrap/bootstrap-reboot' . get_min(), '', '4.0.0', 'all');
    wp_enqueue_style('fontawesome', get_template_directory_uri() . '/inc/third_party/fontawesome/css/all' . get_min(), '', '5.6.0', 'all');
    wp_enqueue_style('font-LORA', '//fonts.googleapis.com/css?family=Lora', array(), '1.0.3', 'all');
    wp_enqueue_style('main-style',  get_stylesheet_uri(), array(), '1.0.3', 'all');
    wp_enqueue_style('articled-animation-style',  get_template_directory_uri() . '/assets/css/articled-animation' . get_min(), '', '1.0.3', 'all');
    wp_enqueue_style('articled-mobile',  get_template_directory_uri() . '/assets/css/responsive' . get_min(), '', '1.0.3', 'all');
  }
  add_action('wp_enqueue_scripts', 'articled_enqueue_style');
}


// Enqueues every themes js file
if( ! function_exists('articled_enqueue_sripts') ) {
 function articled_enqueue_sripts() {
   wp_enqueue_script('jquery');
   wp_enqueue_script('lazy-loading', get_template_directory_uri() . '/assets/js/lazyload' . get_min('js'), array(), '1.8.2', false);
	 wp_enqueue_script('main-js', get_template_directory_uri() . '/assets/js/main-js.js', array(), '1.0.1', false );
	 wp_enqueue_script('articled-animations-js',  get_template_directory_uri() . '/assets/js/articled-animations' . get_min('js'), array('jquery'), '1.0.1', true);
 }
 add_action('wp_enqueue_scripts', 'articled_enqueue_sripts');
}

// Enable widgetable sidebar
// You might need to tweak theme files, more info here - http://codex.wordpress.org/Widgetizing_Themes
if( ! function_exists('articled_side_bar_widgets') ) {
  function articled_side_bar_widgets() {
    register_sidebar( array(
      'name'          => __( 'Main Sidebar', 'articled' ),
      'id'            => 'articled-main-sidebar',
      'description'   => __( 'Widgets Appear in Sidebar', 'articled' ),
      'before_widget' => '<div id="%1$s" class="widget %2$s">',
      'after_widget'  => '</div>',
      'before_title'  => '<h3 class="widget-titles"><span class="screen-text">',
      'after_title'   => '</span></h3>',
    ) );

    register_sidebar(array(
      'name' => __('Slider', 'articled'),
      'id' => 'articled-slider-sidebar',
      'description' => 'You can add widget for displaying on slider.',
      'before_widget' => '<div id="%1$s" class="widget %2$s">',
      'after_widget' => "</div>",
      'before_title' => '<h4 class="slider-titles"><span class="screen-text">',
      'after_title' => '</span></h4>',
    ));

  	register_sidebar(array(
      'name' => __('404 Sidebar', 'articled'),
      'id' => 'articled-sidebar-404',
      'description' => __('Add widgets to 404 Error Page'  , 'articled'),
      'before_widget' => '<div id="%1$s" class="widget %2$s">',
      'after_widget' => "</div>",
      'before_title' => '<h3 class="widget-title-404"><span class="screen-text">',
      'after_title' => '</span></h3>',
  	));

  	register_sidebar(array(
      'name' => __('Footer Widgets 1', 'articled'),
      'id' => 'articled-footer-1',
      'description' => '',
      'before_widget' => '<div id="%1$s" class="widget %2$s">',
      'after_widget' => "</div>",
      'before_title' => '<h4 class="widget-titles"><span class="screen-text">',
      'after_title' => '</span></h4>',
  	));

  	register_sidebar(array(
      'name' => __('Footer Widgets 2', 'articled'),
      'id' => 'articled-footer-2',
      'description' => '',
      'before_widget' => '<div id="%1$s" class="widget %2$s">',
      'after_widget' => "</div>",
      'before_title' => '<h4 class="widget-titles"><span class="screen-text">',
      'after_title' => '</span></h4>',
  	));

  }
  add_action( 'widgets_init', 'articled_side_bar_widgets' );
}


$articled_font_size = array('12px'  => '12px','13px'  => '13px','14px'  => '14px','15px'  => '15px','16px'  => '16px',
          '17px'  => '17px','18px'  => '18px','19px'  => '19px','20px'  => '20px',)	;


// Include Some PHP files

require_once ABU_DIR . '/inc/functions/short-functions.php';
require_once ABU_DIR . '/inc/functions/long-functions.php';
require_once get_template_directory() . '/inc/conditional-files.php';
require_once get_template_directory() . '/inc/comment-options.php';

// Include Some Customizer PHP files
require_once get_template_directory() . '/inc/options/class-articled-sections.php';
require_once get_template_directory() . '/inc/options/theme-options.php';
require_once get_template_directory() . '/inc/options/class-articled-sanitize.php';
require_once get_template_directory() . '/inc/options/blog/class-articled-blog.php';
require_once get_template_directory() . '/inc/options/topbar/class-articled-top-bar.php';
require_once get_template_directory() . '/inc/options/footer/class-articled-footer.php';
require_once get_template_directory() . '/inc/options/class-articled-partials.php';
require_once get_template_directory() . '/inc/options/class-articled-controls.php';
require_once get_template_directory() . '/inc/options/class-articled-onoffs.php';
require_once get_template_directory() . '/inc/options/class-articled-selects.php';
require_once get_template_directory() . '/inc/options/class-articled-fields.php';
require_once get_template_directory() . '/inc/options/class-articled-headings.php';
require_once get_template_directory() . '/inc/options/class-articled-ranges.php';

// wp_die( var_dump( glob( get_template_directory() . '/inc/classes/*.php', GLOB_NOSORT ) ) );
foreach ( glob( get_template_directory() . '/inc/classes/*.php', GLOB_NOSORT ) as $key => $value) {
  require_once $value;
}


$notice_text = '<code>' . sprintf ( __( '"<a href="%1$s" target="_blank">Articled Booster</a>" plugin is recommended by Author to add more powerfull feature in theme.', 'articled' ), esc_url( 'http://www.abusufiyan.com/plugin/articled-booster?muftki=ha' ) ) . '</code>';
articledNotice::create( $notice_text, true, 'boosterrecommended' );


// Register Navigations
function articled_register_menus () {
   register_nav_menus( array(
    'top-menu'   => __( 'Top Bar Menus', 'articled'),
    'main-menu' => __( 'Main Menus', 'articled')
   ));
};
add_action('init', 'articled_register_menus');

// enqueue files to customizer
if( ! function_exists('articled_customize_enqueues') ) {
  function articled_customize_enqueues() {
    wp_enqueue_script( 'jquery-ui-sortable' );
    wp_enqueue_script('jQuery-chosen-js', get_template_directory_uri() . '/inc/third_party/chosen/chosen.jquery.js', array( 'jquery'), '1.8.7');
    wp_enqueue_style( 'jQuery-chosen-css', get_template_directory_uri() . '/inc/third_party/chosen/chosen.css', null, '1.8.7');
    wp_enqueue_style( 'articled-customizer-css', get_template_directory_uri() . '/assets/css/customizer-css.css', null, '1.0', 'all' );
    wp_enqueue_script('articled-customizer-js', get_template_directory_uri() . '/assets/js/customizer.js', array( 'jquery', 'jquery-ui-sortable', 'customize-controls' ), '1.0');
  }
  add_action( 'customize_controls_enqueue_scripts', 'articled_customize_enqueues' );
}

function articled_theme_customize_css_output() {
    function articled_theme_customize_css(){
      $articled_mod = 'get_theme_mod';
      require_once( get_template_directory() . '/assets/css/style-customizer.php');
    }
    echo str_replace(' ', '', articled_theme_customize_css() );
}
add_action( 'wp_head' , 'articled_theme_customize_css_output' );


function articled_theme_customize_js_output() {
    require_once( get_template_directory() . '/assets/js/script-customizer.php' );
}
add_action( 'wp_footer' , 'articled_theme_customize_js_output' );


/**
 * Replaces "[...]" (appended to automatically generated excerpts) with ... and
 * a 'Continue reading' link.
 *
 * @since Articled 1.0
 *
 * @param string $link Link to single post/page.
 * @return string 'Continue reading' link prepended with an ellipsis.
 */

if( !function_exists('articled_excerpt_more') || !function_exists('articled_excerpt_length') ){

	function articled_excerpt_more( $excerpt_text ) {

		 	if ( is_admin() ) { return; }

			if ( get_theme_mod('articled_excerpt_more_text', '[...]') == '[...]'  ) {
					$excerpt_append = '[...]';
			} else {
					$excerpt_append = get_theme_mod('articled_excerpt_more_text', '[...]');
			}

		$excerpt_text = sprintf( '<a href="%1$s" class="read-more">%2$s</a>', esc_url( get_permalink( get_the_ID() ) ), $excerpt_append );

		return $excerpt_text;
	}

	add_filter( 'excerpt_more', 'articled_excerpt_more');

	function articled_excerpt_length( $length ) {
		if( get_theme_mod('articled_excerpt_more_length', 50) == 50 ){
				$length =  50;
			} else {
				$length = get_theme_mod('articled_excerpt_more_length', 50);
			}
		return $length;
	}
	add_filter( 'excerpt_length', 'articled_excerpt_length', 999 );
}
