<?php
namespace ERROPiX\AIOW;

// don't load directly
if ( ! defined( 'ABSPATH' ) ) die( '-1' );

/**
 * Plugin Controller
*/
class Main {
	static $instance;
	
	static function getInstance() {
		if( !self::$instance ) {
			self::$instance = new self;
		}
		return self::$instance;
	}
	
	private function __construct() {
		add_action( 'widgets_init', array($this,'widgets_init') );
		add_filter( 'vc_shortcode_output', array($this,'vc_shortcode_output'), 5, 3 );
	}
	
	function widgets_init() {
		register_widget( __namespace__.'\All_In_One_Widget' );
	}
	
	function vc_shortcode_output( $output, $instance=null, $atts=array() ){
		$id = get_the_ID();
		
		if( is_a($instance, 'WPBakeryShortCode_VC_Basic_Grid') ) {
			$marker = '<!-- VC-GRID-FIXED -->';
			if( strpos($output, $marker) === false ) {
				$output = preg_replace( '/&quot;page_id&quot;:\d+/i', '&quot;page_id&quot;:'.$id, $output);
				$output = preg_replace( '/post-id="\d+"/i', 'post-id="'.$id.'"', $output);
				$output.= $marker;
			}
		}
		
		return $output;
	}
	
	static function enabled_vc() {
		return defined('WPB_VC_VERSION') && function_exists('vc_disable_frontend');
	}
}