url='https://api.activedemand.com/v1/smart_blocks/show_all';
$options = get_option(PREFIX.'_options_field');
$show = get_option(PREFIX.'_server_showpopups');
$this->show_popups=(is_array($options) && array_key_exists(PREFIX.'_appkey', $options) && $show);
$this->server_side=TRUE;
$this->has_fired=FALSE;
}
public function reset(){
$this->has_fired=FALSE;
$blocks=array();
$forms=array();
}
private function add_shortcode($id, $slug){
$div='activedemand_'.$slug.'_'.count($this->$slug);
$this->$slug[$div]=$id;
return $div;
}
public function has_content(){
return (count($this->blocks) + count($this->forms)>0) || $this->show_popups;
}
public function add_block($id){
$div='activedemand_blocks_'.count($this->blocks);
$this->blocks[$div]=$id;
return $div;
}
public function add_form($id){
$div='activedemand_forms_'.count($this->forms);
$this->forms[$div]=$id;
return $div;
}
public function make_args(){
$options = get_option(PREFIX.'_options_field');
$activedemand_ignore_block_style = false;
if (array_key_exists(PREFIX.'_ignore_block_style', $options)) {
$activedemand_ignore_block_style = $options[PREFIX.'_ignore_block_style'];
}
return array(
'exclude_css'=>$activedemand_ignore_block_style,
'shortcodes'=> $this->get_codes(),
PREFIX.'_session_guid' => activedemand_get_cookie_value()
);
}
public function post_codes(){
if(!$this->server_side){
throw new \Exception('Method must be Server Side for ShortCodeCollector to POST');
}
$args= $this->make_args();
$timeout=10;
$response= activedemand_postHTML($this->url, $args, $timeout);
$this->has_fired=TRUE;
$this->reply=$response;
return $response;
}
public function get_reply(){
if(!$this->has_fired) $this->post_codes();
return $this->reply;
}
public function get_codes(){
return json_encode((object) array('forms'=> (object) $this->forms,
'popups'=> $this->show_popups,
'blocks'=> (object) $this->blocks
));
}
}
add_shortcode(PREFIX.'_block', __NAMESPACE__.'\activedemand_process_block_shortcode');
function activedemand_process_block_shortcode($atts, $content = null){
$id = "";
//$id exists after this call.
extract(shortcode_atts(array('id' => ''), $atts));
$collector= ShortCodeCollector::get_instance();
$div_id=$collector->add_block($id);
$html= '';
return "
$html
";
}
add_shortcode(PREFIX.'_form', __NAMESPACE__.'\activedemand_process_form_shortcode');
function activedemand_process_form_shortcode($atts, $content = null){
$id = "";
//$id exists after this call.
extract(shortcode_atts(array('id' => ''), $atts));
$collector= ShortCodeCollector::get_instance();
$div_id=$collector->add_form($id);
$html= '';
return "";
}
//enqueue jQuery for popup purposes
add_action('wp_enqueue_scripts', __NAMESPACE__.'\activedemand_scripts');
function activedemand_scripts(){
wp_enqueue_script('jquery');
}
function activedemand_prefilter_post($post){
$content=$post->post_content;
$collector= ShortCodeCollector::get_instance();
$id=$post->ID;
if(array_key_exists($id, $collector->posts_processed)){
$post->post_content=$collector->posts_processed[$id];
}
else{
$post->post_content= prefilter_content($content);
$collector->posts_processed[$id]=$post->post_content;
}
return $post;
}
function match_replacement($matches){
switch($matches[1]){
case PREFIX.'_block':
$function=__NAMESPACE__.'\activedemand_process_block_shortcode';
break;
case PREFIX.'_form':
$function=__NAMESPACE__.'\activedemand_process_form_shortcode';
break;
default:
return;
}
$args="array('id'=>$matches[3])";
return ""
. '';
}
function prefilter_content($content){
if(!defined('W3TC_DYNAMIC_SECURITY') || !function_exists('w3tc_fragmentcache_start')){
return do_shortcode($content);
}
else{
$shortcodes = array(PREFIX.'_form', PREFIX.'_block', 'fakecode');
foreach ($shortcodes as $sc) {
$pattern="/\[($sc).*?id=('|\")(\d+)('|\").*\]/";
$content= preg_replace_callback($pattern, __NAMESPACE__.'\match_replacement', $content);
}
return do_shortcode($content);
}
}
function sc_content_check_and_filter($content){
global $post;
if(!is_object($post)){
return prefilter_content($content);
}
$id=$post->ID;
$collector= ShortCodeCollector::get_instance();
if(isset($collector->posts_processed[$id])){
$content=$collector->posts_processed[$id];
} else{
$post= activedemand_prefilter_post($post);
$content=$post->post_content;
}
return $content;
}
add_filter('the_content', __NAMESPACE__.'\sc_content_check_and_filter',1);
remove_filter('the_content', 'wpautop');
add_filter('widget_text', __NAMESPACE__.'\prefilter_content');
function footer_script(){
if(!defined('W3TC_DYNAMIC_SECURITY') || !function_exists('w3tc_fragmentcache_start')){
$process_code=process_shortcodes_script();
} else{
$process_code=''
. '';
}
echo $process_code;
}
add_action('wp_footer', __NAMESPACE__.'\footer_script', 900);
function process_shortcodes_script(){
$collector= ShortCodeCollector::get_instance();
$server_side=$collector->server_side;
$script=<<
function cycleAndReplace(obj){
for(var property in obj){
if(!obj.hasOwnProperty(property) || property=="popup") continue;
var id="#"+property;
jQuery(id).html(obj[property]);
}
}
function prefixThePopup(popup){
jQuery(document).ready(function(){
jQuery("body").prepend(popup);
});
}
SCRIPTTOP;
if($server_side){
$arr=json_decode($collector->get_reply(), TRUE);
$json= json_encode($arr, JSON_HEX_TAG || JSON_HEX_QUOT);
$name=PREFIX.'_shortcodes';
$script.=<<
SCRIPTEND;
return $script;
}
function get_collector_content($div_id){
$collector= ShortCodeCollector::get_instance();
return $collector->get_content($div_id);
}
function add_client_rider(){
$collector= ShortCodeCollector::get_instance();
$args=$collector->make_args();
$data= activedemand_field_string($args);
$url= $collector->url;
$script=<<