<?php
/*
Plugin Name: UX Shop Shortcodes by Develoved
Plugin URI: themes.develoved.com
Description: This is a plugin that is meant to add implement all the shortcodes used in a compatible theme by Develoved.
Version: 1.2.4
Author: develoved
Author URI: themes.develoved.com
Text Domain: dvl-uxshop-shortcodes
*/

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Load plugin textdomain.
 */
function shrk_uxshop_shortcodes_load_textdomain() {
	load_plugin_textdomain( 'dvl-uxshop-shortcodes', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); 
}
add_action( 'init', 'shrk_uxshop_shortcodes_load_textdomain' );

/*
 * Setup visual composer shortcodes
 */
define('GALLERY_DEFAULT_MAX_ITEMS', 9);

require_once ( 'vc_extend/vc_extend.php' );
require_once ( 'woocommerce/shortcodes.php' );

/**
 * Register shortcodes
 */
foreach (scandir( dirname(__FILE__) . '/inc' ) as $f) {
    $path = dirname(__FILE__) . '/inc/' . $f;
    if (is_file($path)) {
        require_once( $path );
    }
}


function shrk_get_postids_of_query_obj( $queryobj = false ) {
	
	$ret_id = array();
	
	if ( !$queryobj ) { return false; }
	
	foreach ( $queryobj->posts as $post_obj ) {
		$ret_id[] = $post_obj->ID;
	}
	
	if ( !empty( $ret_id ) ) {
		return $ret_id;
	}
	
	return false;
	
}

function shrk_getCSSAnimation($css_animation) {
	$output = '';
	if ( $css_animation != '' ) {
		wp_enqueue_script( 'waypoints' );
		$output = ' wpb_animate_when_almost_visible wpb_'.$css_animation;
	}
	return $output;
}


/**
 * Shortcode Wrapper
 * used for wrapping woo stock shortcodes
 * 
 * @param mixed $function
 * @param array $atts (default: array())
 * @return string
 */
function shrk_shortcode_wrapper( 
	$function, 
	$atts = array(), 
	$wrapper = array(
		'class'  => '',
		'before' => null,
		'after'  => null
		)
	) {

	if ( !is_callable( $function ) ) {
		if ( is_array( $function ) && !empty( $function[0] && $function[0] == 'WC_Shortcodes' ) ) {
			return '<div>' . esc_html__('Your theme does not support this shortcode or WooCommerce is not active.', 'dvl-uxshop-shortcodes') . '</div>';
		} else {
			return '<div>' . esc_html__('Your theme does not support this shortcode.', 'dvl-uxshop-shortcodes') . '</div>';
		}
	}

	do_action( 'shrk_swiper_sc_wrapper_before' );
	ob_start();

	$before = empty( $wrapper['before'] ) ? '<div class="' . esc_attr( $wrapper['class'] ) . '" ' . shrk_shortcode_extract_atts( $atts, 'swiper_' ) . '>' : $wrapper['before'];
	$after  = empty( $wrapper['after'] ) ? '</div>' : $wrapper['after'];
	$output = call_user_func( $function, $atts );

	echo $before;
	echo $output;
	echo $after;

	do_action( 'shrk_swiper_sc_wrapper_after' );
	return ob_get_clean();
}

/**
 * Extract specific attributes 
 * and print them as element data attributes
 */
function shrk_shortcode_extract_atts( $atts = array(), $att_prefix = 'swpr' ) {
	$data_atts = array();

	if ( empty( $atts ) ) {
		return '';
	}

	foreach ($atts as $att => $value) {
		if ( substr( $att, 0, strlen($att_prefix) ) === $att_prefix ) {
			$data_atts[] = 'data-' . str_replace('_', '-', $att) . '="' . esc_attr( $value ) . '"';
		}
	}

	return implode(' ',$data_atts);
}

function shrk_shortcodes_init() {

	if ( !function_exists( 'WC' ) || !function_exists( 'woo_shrk_slides_image_grid_enable' ) ) {
		return;
	}

	// Disable slides output for product-loop
	add_action( 'shrk_swiper_sc_wrapper_before', 'woo_shrk_slides_image_grid_disable' );

	// Enable slides output for product-loop
	add_action( 'shrk_swiper_sc_wrapper_after', 'woo_shrk_slides_image_grid_enable' );

	// Remove notices from shortcodes output.
	add_action( 'shrk_swiper_sc_wrapper_before', function () {
		remove_action( 'woocommerce_shortcode_before_product_cat_loop', 'wc_print_notices', 10 ); // specific to product loop shortcode
	} );

	// Add back notices from shortcodes output.
	add_action( 'shrk_swiper_sc_wrapper_after', function () {
		add_action( 'woocommerce_shortcode_before_product_cat_loop', 'wc_print_notices', 10 ); // specific to product loop shortcode
	} );

}
add_action( 'init', 'shrk_shortcodes_init' );

?>