<?php
/**
 * Theme functions and definitions
 *
 * @package Charity Foundation
 */ 

if ( ! function_exists( 'charity_foundation_enqueue_styles' ) ) :
	/**
	 * Load assets.
	 *
	 * @since 1.0.0
	 */
	function charity_foundation_enqueue_styles() {
		wp_enqueue_style( 'ngo-charity-donation-style-parent', get_template_directory_uri() . '/style.css' );
		wp_enqueue_style( 'charity-foundation-style', get_stylesheet_directory_uri() . '/style.css', array( 'ngo-charity-donation-style-parent' ), '1.0.0' );

		// Theme Customize CSS.
		require get_parent_theme_file_path( 'inc/extra_customization.php' );
		wp_add_inline_style( 'charity-foundation-style',$ngo_charity_donation_custom_style );
	}
endif;
add_action( 'wp_enqueue_scripts', 'charity_foundation_enqueue_styles', 99 );

function charity_foundation_setup() {
	add_theme_support( 'align-wide' );
	add_theme_support( 'automatic-feed-links' );
	add_theme_support( "responsive-embeds" );
	add_theme_support( "wp-block-styles" );
	add_theme_support( 'post-thumbnails' );
	add_theme_support( 'title-tag' );
	add_theme_support('custom-background',array(
		'default-color' => 'ffffff',
	));
	add_image_size( 'charity-foundation-featured-image', 2000, 1200, true );
	add_image_size( 'charity-foundation-thumbnail-avatar', 100, 100, true );

	$GLOBALS['content_width'] = 525;
	register_nav_menus( array(
		'primary' => __( 'Primary Menu', 'charity-foundation' ),
	) );

	add_theme_support( 'html5', array(
		'comment-form',
		'comment-list',
		'gallery',
		'caption',
	) );

	// Add theme support for Custom Logo.
	add_theme_support( 'custom-logo', array(
		'width'       => 250,
		'height'      => 250,
		'flex-width'  => true,
	) );

	/*
	* This theme styles the visual editor to resemble the theme style,
	* specifically font, colors, and column width.
	*/
	add_editor_style( array( 'assets/css/editor-style.css' ) );
}
add_action( 'after_setup_theme', 'charity_foundation_setup' );

function charity_foundation_widgets_init() {
	register_sidebar( array(
		'name'          => __( 'Sidebar', 'charity-foundation' ),
		'id'            => 'sidebar-1',
		'description'   => __( 'Add widgets here to appear in your sidebar on blog posts and archive pages.', 'charity-foundation' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<div class="widget_container"><h3 class="widget-title">',
		'after_title'   => '</h3></div>',
	) );

	register_sidebar( array(
		'name'          => __( 'Page Sidebar', 'charity-foundation' ),
		'id'            => 'sidebar-2',
		'description'   => __( 'Add widgets here to appear in your pages and posts', 'charity-foundation' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<div class="widget_container"><h3 class="widget-title">',
		'after_title'   => '</h3></div>',
	) );

	register_sidebar( array(
		'name'          => __( 'Footer 1', 'charity-foundation' ),
		'id'            => 'footer-1',
		'description'   => __( 'Add widgets here to appear in your footer.', 'charity-foundation' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => __( 'Footer 2', 'charity-foundation' ),
		'id'            => 'footer-2',
		'description'   => __( 'Add widgets here to appear in your footer.', 'charity-foundation' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => __( 'Footer 3', 'charity-foundation' ),
		'id'            => 'footer-3',
		'description'   => __( 'Add widgets here to appear in your footer.', 'charity-foundation' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );

	register_sidebar( array(
		'name'          => __( 'Footer 4', 'charity-foundation' ),
		'id'            => 'footer-4',
		'description'   => __( 'Add widgets here to appear in your footer.', 'charity-foundation' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h3 class="widget-title">',
		'after_title'   => '</h3>',
	) );
}
add_action( 'widgets_init', 'charity_foundation_widgets_init' );

function charity_foundation_customize_register() {
  	global $wp_customize;

  	$wp_customize->remove_section( 'ngo_charity_donation_pro' );
}
add_action( 'customize_register', 'charity_foundation_customize_register', 11 );

function charity_foundation_sanitize_dropdown_pages( $page_id, $setting ) {
	$page_id = absint( $page_id );
	return ( 'publish' == get_post_status( $page_id ) ? $page_id : $setting->default );
}

function charity_foundation_string_limit_words($string, $word_limit) {
	$words = explode(' ', $string, ($word_limit + 1));
	if(count($words) > $word_limit)
	array_pop($words);
	return implode(' ', $words);
}

function charity_foundation_customize( $wp_customize ) {

	wp_enqueue_style('customizercustom_css', esc_url( get_stylesheet_directory_uri() ). '/assets/css/customizer.css');

	$wp_customize->add_section('charity_foundation_pro', array(
		'title'    => __('UPGRADE CHARITY PREMIUM', 'charity-foundation'),
		'priority' => 1,
	));

	$wp_customize->add_setting('charity_foundation_pro', array(
		'default'           => null,
		'sanitize_callback' => 'sanitize_text_field',
	));
	$wp_customize->add_control(new Charity_Foundation_Pro_Control($wp_customize, 'charity_foundation_pro', array(
		'label'    => __('CHARITY PREMIUM', 'charity-foundation'),
		'section'  => 'charity_foundation_pro',
		'settings' => 'charity_foundation_pro',
		'priority' => 1,
	)));

	//Our Causes Section
	$wp_customize->add_section('charity_foundation_causes_section',array(
		'title'	=> __('Our Causes Section','charity-foundation'),
		'priority' => 5,
	));

	$wp_customize->add_setting('charity_foundation_causes_section_title',array(
		'default'	=> '',
		'sanitize_callback'	=> 'sanitize_text_field'
	));	
	$wp_customize->add_control('charity_foundation_causes_section_title',array(
		'label'	=> __('Section Heading Title','charity-foundation'),
		'section' => 'charity_foundation_causes_section',
		'type'	 => 'text'
	));

	$categories = get_categories();
		$cat_posts = array();
			$i = 0;
			$cat_posts[]='Select';	
		foreach($categories as $category){
			if($i==0){
			$default = $category->slug;
			$i++;
		}
		$cat_posts[$category->slug] = $category->name;
	}

	$wp_customize->add_setting('charity_foundation_causes_section_category',array(
		'default'	=> 'select',
		'sanitize_callback' => 'ngo_charity_donation_sanitize_choices',
	));
	$wp_customize->add_control('charity_foundation_causes_section_category',array(
		'type'    => 'select',
		'choices' => $cat_posts,
		'label' => __('Select Category to display Latest Post','charity-foundation'),
		'section' => 'charity_foundation_causes_section',
	));
}
add_action( 'customize_register', 'charity_foundation_customize' );

function charity_foundation_enqueue_comments_reply() {
  if( is_singular() && comments_open() && ( get_option( 'thread_comments' ) == 1) ) {
    // Load comment-reply.js (into footer)
    wp_enqueue_script( 'comment-reply', '/wp-includes/js/comment-reply.min.js', array(), false, true );
  }
}
add_action(  'wp_enqueue_scripts', 'charity_foundation_enqueue_comments_reply' );


function charity_foundation_custom_goal_raised() {
  add_meta_box( 'bn_meta', __( 'Donation Meta Feilds', 'charity-foundation' ), 'charity_foundation_meta_goal_raised_callback', 'post', 'normal', 'high' );
}
if (is_admin()){
  add_action('admin_menu', 'charity_foundation_custom_goal_raised');
}

function charity_foundation_meta_goal_raised_callback( $post ) {
  wp_nonce_field( basename( __FILE__ ), 'charity_foundation_goal_raised_meta' );
  $bn_stored_meta = get_post_meta( $post->ID );
  $charity_foundation_raised = get_post_meta( $post->ID, 'charity_foundation_raised', true );
  $charity_foundation_goal = get_post_meta( $post->ID, 'charity_foundation_goal', true );
  ?>
  <div id="custom_meta_feilds">
    <table id="list">
      <tbody id="the-list" data-wp-lists="list:meta">
        <tr id="meta-8">
          <td class="left">
            <?php esc_html_e( 'Raised Price', 'charity-foundation' )?>
          </td>
          <td class="left">
            <input type="number" name="charity_foundation_raised" id="charity_foundation_raised" value="<?php echo esc_attr($charity_foundation_raised); ?>" />
          </td>
        </tr>
        <tr id="meta-8">
          <td class="left">
            <?php esc_html_e( 'Goal Price', 'charity-foundation' )?>
          </td>
          <td class="left">
            <input type="number" name="charity_foundation_goal" id="charity_foundation_goal" value="<?php echo esc_attr($charity_foundation_goal); ?>" />
          </td>
        </tr>
      </tbody>
    </table>
  </div>
  <?php
}

function charity_foundation_save( $post_id ) {
	if (!isset($_POST['charity_foundation_goal_raised_meta']) || !wp_verify_nonce( strip_tags( wp_unslash( $_POST['charity_foundation_goal_raised_meta']) ), basename(__FILE__))) {
  		return;
  	}

	if (!current_user_can('edit_post', $post_id)) {
		return;
	}
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
		return;
	}

	if( isset( $_POST[ 'charity_foundation_raised' ] ) ) {
	update_post_meta( $post_id, 'charity_foundation_raised', strip_tags( wp_unslash( $_POST[ 'charity_foundation_raised' ]) ) );
	}
	if( isset( $_POST[ 'charity_foundation_goal' ] ) ) {
	update_post_meta( $post_id, 'charity_foundation_goal', strip_tags( wp_unslash( $_POST[ 'charity_foundation_goal' ]) ) );
	}
}
add_action( 'save_post', 'charity_foundation_save' );

define('CHARITY_FOUNDATION_PRO_LINK',__('https://www.ovationthemes.com/wordpress/charity-donation-wordpress-theme/','charity-foundation'));

/* Pro control */
if (class_exists('WP_Customize_Control') && !class_exists('Charity_Foundation_Pro_Control')):
    class Charity_Foundation_Pro_Control extends WP_Customize_Control{

    public function render_content(){?>
        <label style="overflow: hidden; zoom: 1;">
            <div class="col-md upsell-btn">
                <a href="<?php echo esc_url( CHARITY_FOUNDATION_PRO_LINK ); ?>" target="blank" class="btn btn-success btn"><?php esc_html_e('UPGRADE CHARITY PREMIUM','charity-foundation');?> </a>
            </div>
            <div class="col-md">
                <img class="charity_foundation_img_responsive " src="<?php echo esc_url( get_stylesheet_directory_uri() ); ?>/screenshot.png">
            </div>
            <div class="col-md">
                <h3 style="margin-top:10px; margin-left: 20px; text-decoration:underline; color:#333;"><?php esc_html_e('CHARITY PREMIUM - Features', 'charity-foundation'); ?></h3>
                <ul style="padding-top:10px">
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('Responsive Design', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('Boxed or fullwidth layout', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('Shortcode Support', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('Demo Importer', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('Section Reordering', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('Contact Page Template', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('Multiple Blog Layouts', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('Unlimited Color Options', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('Designed with HTML5 and CSS3', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('Customizable Design & Code', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('Cross Browser Support', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('Detailed Documentation Included', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('Stylish Custom Widgets', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('Patterns Background', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('WPML Compatible (Translation Ready)', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('Woo-commerce Compatible', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('Full Support', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('10+ Sections', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('Live Customizer', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('AMP Ready', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('Clean Code', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('SEO Friendly', 'charity-foundation');?> </li>
                    <li class="upsell-charity_foundation"> <div class="dashicons dashicons-yes"></div> <?php esc_html_e('Supper Fast', 'charity-foundation');?> </li>                    
                </ul>
            </div>
            <div class="col-md upsell-btn upsell-btn-bottom">
                <a href="<?php echo esc_url( CHARITY_FOUNDATION_PRO_LINK ); ?>" target="blank" class="btn btn-success btn"><?php esc_html_e('UPGRADE CHARITY PREMIUM','charity-foundation');?> </a>
            </div>
        </label>
    <?php } }
endif;

if ( ! defined( 'NGO_CHARITY_DONATION_SUPPORT' ) ) {
define('NGO_CHARITY_DONATION_SUPPORT',__('https://wordpress.org/support/theme/charity-foundation','charity-foundation'));
}
if ( ! defined( 'NGO_CHARITY_DONATION_REVIEW' ) ) {
define('NGO_CHARITY_DONATION_REVIEW',__('https://wordpress.org/support/theme/charity-foundation/reviews/#new-post','charity-foundation'));
}
if ( ! defined( 'NGO_CHARITY_DONATION_LIVE_DEMO' ) ) {
define('NGO_CHARITY_DONATION_LIVE_DEMO',__('https://www.ovationthemes.com/demos/charity-foundation/','charity-foundation'));
}
if ( ! defined( 'NGO_CHARITY_DONATION_BUY_PRO' ) ) {
define('NGO_CHARITY_DONATION_BUY_PRO',__('https://www.ovationthemes.com/wordpress/charity-donation-wordpress-theme/','charity-foundation'));
}
if ( ! defined( 'NGO_CHARITY_DONATION_PRO_DOC' ) ) {
define('NGO_CHARITY_DONATION_PRO_DOC',__('https://www.ovationthemes.com/docs/ot-charity-foundation-pro-doc/','charity-foundation'));
}
if ( ! defined( 'NGO_CHARITY_DONATION_THEME_NAME' ) ) {
define('NGO_CHARITY_DONATION_THEME_NAME',__('Premium Donation Theme','charity-foundation'));
}