1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246:
<?php
if (!class_exists('CTA_Static_Widget')) {
class CTA_Static_Widget extends WP_Widget {
private $cta_templates;
public function __construct() {
$widget_ops = array( 'classname' => 'class_CTA_Static_Widget', 'description' => __('Use this widget to manually display Calls to Action in sidebars.', 'cta'));
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'id_wp_cta_static_widget' );
parent::__construct( 'id_wp_cta_static_widget', __('Call to Action Static Widget', 'inbound-pro'), $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
global $wp_query; global $post;
if (!isset($instance['cta_ids'])) {
return;
}
$obj_id = $wp_query->get_queried_object_id();
$CTA_Load_Extensions = CTA_Load_Extensions();
$this->cta_templates = $CTA_Load_Extensions->template_definitions;
$CTA_Render = CTA_Render();
$selected_ctas = $instance['cta_ids'];
if (!is_array($selected_ctas)) {
return;
}
$cta_ids = implode(",", $selected_ctas);
$count = count($selected_ctas);
$rand_key = array_rand($selected_ctas, 1);
$cta_id = $selected_ctas[$rand_key];
$this->cta_id = $cta_id;
$selected_cta = $CTA_Render->prepare_cta_dataset( array($cta_id));
if ( !isset($selected_cta['templates']) ) {
return;
}
$loaded = array();
foreach ($selected_cta['templates'] as $template) {
if ( in_array( $template['slug'], $loaded) ){
continue;
}
$loaded[] = $template['slug'];
$assets = $CTA_Render->get_template_asset_files($template);
$localized_template_id = str_replace( '-', '_', $template['slug'] );
foreach ($assets as $type => $file) {
switch ($type) {
case 'js':
foreach ($file as $js) {
wp_enqueue_script( md5($js) ,$js, array( 'jquery') , null , true );
wp_localize_script( md5($js), $localized_template_id, array( 'ajaxurl' => admin_url( 'admin-ajax.php'), 'post_id' => $obj_id ));
}
break;
case 'css':
foreach ($file as $css) {
wp_enqueue_style( md5($css), $css );
}
break;
}
}
}
foreach ($selected_cta['meta'] as $vid=>$cta) {
($vid<1) ? $suffix = '' : $suffix = '-'.$vid;
$template_slug = $selected_cta['meta'][$vid]['wp-cta-selected-template-'.$vid];
$custom_css = CTA_Variations::get_variation_custom_css ( $cta_id, $vid );
$dynamic_css = $this->cta_templates[$template_slug]['css-template'];
$dynamic_css = $CTA_Render->replace_template_variables( $selected_cta, $dynamic_css, $vid );
$css_id_preface = "#wp_cta_" . $cta_id . "_variation_" . $vid;
$dynamic_css = $CTA_Render->parse_css_template($dynamic_css, $css_id_preface);
$css_styleblock_class = apply_filters( 'wp_cta_styleblock_class', '', $cta_id, $vid );
if (!stristr($custom_css,'<style')){
$custom_css = strip_tags($custom_css);
}
echo '<style type="text/css" id="wp_cta_css_custom_'.$cta_id.'_'.$vid.'" class="wp_cta_css_'.$cta_id.' '.$css_styleblock_class.'">'.$custom_css.' '.$dynamic_css.'</style>';
$custom_js = get_post_meta( $cta_id, 'wp-cta-custom-js'.$suffix, true);
if (!stristr($custom_css,'<script') && $custom_css) {
echo '<script type="text/javascript">jQuery(document).ready(function($) {
'.$custom_js.' });</script>';
} else if ($custom_js) {
echo $custom_js;
}
}
$selected_cta['margin-top'] = $instance['cta_margin_top'];
$selected_cta['margin-bottom'] = $instance['cta_margin_bottom'];
$cta_template = $CTA_Render->build_cta_content( $selected_cta );
$cta_template = do_shortcode($cta_template);
echo $cta_template;
$this->load_variation();
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['cta_margin_top'] = $new_instance['cta_margin_top'] ? $new_instance['cta_margin_top'] : "";
$instance['cta_margin_bottom'] = $new_instance['cta_margin_bottom'] ? $new_instance['cta_margin_bottom'] : "";
$instance['cta_ids'] = $new_instance['cta_ids'] ? $new_instance['cta_ids'] : "";
return $instance;
}
function form($instance) {
wp_enqueue_style('thickbox');
wp_enqueue_script('thickbox');
$args = array('post_type' => 'wp-call-to-action', 'numberposts' => -1);
$cta_post_type = get_posts($args);
if ( !isset($instance['cta_ids']) || !is_array($instance['cta_ids']) ) {
$instance['cta_ids'] = array();
}
$width = "";
if ( isset( $instance[ 'cta_default_width' ] ) ) {
$width = $instance[ 'cta_default_width' ];
}
$height = "";
if ( isset( $instance[ 'cta_default_height' ] ) ) {
$height = $instance[ 'cta_default_height' ];
}
$margin_top = "";
if ( isset( $instance[ 'cta_margin_top' ] ) ) {
$margin_top = $instance[ 'cta_margin_top' ];
}
$margin_bottom = "";
if ( isset( $instance[ 'cta_margin_bottom' ] ) ) {
$margin_bottom = $instance[ 'cta_margin_bottom' ];
}
?>
<div class='cta-widget-p'><strong><?php _e( 'Select Calls to Action(s):', 'inbound-pro' ); ?></strong><br />
<small><?php _e('If multiple calls to action are checked, they will randomly rotate. Only 1 CTA is displayed per widget', 'inbound-pro' ); ?></small>
<div class='cta-widget-select-options'>
<?php
foreach ($cta_post_type as $cta) {
$this_id = $cta->ID;
$this_link = get_permalink( $this_id );
$this_link = preg_replace('/\?.*/', '', $this_link); ?>
<input class="checkbox" type="checkbox" <?php checked(in_array( $cta->ID, $instance['cta_ids'] ), true ); ?> value="<?php _e($cta->ID); ?>" name="<?php echo $this->get_field_name('cta_ids'); ?>[]" />
<label for=""><?php _e($cta->post_title); ?>
<a class='thickbox cta-links-hidden cta-widget-preview-links' id="cta-<?php echo $this_id;?>" href='<?php echo $this_link;?>?wp-cta-variation-id=0&inbound_popup_preview=on&post_id=<?php echo $cta->ID; ?>&TB_iframe=true&width=640&height=703'>Preview</a>
</label>
<br />
<?php
}
?>
</div>
</div>
<hr>
<h4 class='cta-advanced-section'><?php _e( 'Advanced Options', 'inbound-pro' ); ?></h4>
<div class="advanced-cta-widget-options">
<div class='cta-widget'><label for="<?php echo $this->get_field_id('cta_margin_top'); ?>">Margin Top</label>
<input class="cta-text" type="text" value="<?php echo $margin_top; ?>" id="<?php echo $this->get_field_id('cta_margin_top'); ?>" name="<?php echo $this->get_field_name('cta_margin_top'); ?>" />px</div>
<div class='cta-widget'><label for="<?php echo $this->get_field_id('cta_margin_bottom'); ?>">Margin Bottom</label>
<input class="cta-text" type="text" value="<?php echo $margin_bottom; ?>" id="<?php echo $this->get_field_id('cta_margin_bottom'); ?>" name="<?php echo $this->get_field_name('cta_margin_bottom'); ?>" />px</div>
</div>
<?php
}
function load_variation() {
$disable_ajax = get_option('wp-cta-main-disable-ajax-variation-discovery');
?>
<script>
jQuery(document).ready(function($) {
wp_cta_load_variation('<?php echo $this->cta_id; ?>', null );
});
</script>
<?php
}
}
function cta_load_static_widget() {
register_widget( 'CTA_Static_Widget' );
}
add_action( 'widgets_init', 'cta_load_static_widget' );
}