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:
<?php
class Landing_Pages_Sidebars {
public function __construct() {
self::add_hooks();
}
public static function add_hooks() {
add_action('init', array( __CLASS__ , 'register_sidebars' ) , 100 );
add_action('wp_head', array( __CLASS__ , 'replace_sidebars' ) );
}
public static function register_sidebars() {
if (function_exists('register_sidebar')) {
register_sidebar( array(
'id' => 'lp_sidebar',
'name' => __('Landing Pages Sidebar', 'landing-pages'),
'description' => __('Landing Pages Sidebar Area: For default and native theme templates only.', 'landing-pages'),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
'priority' => 10
));
}
}
public static function replace_sidebars() {
global $_wp_sidebars_widgets, $post, $wp_registered_sidebars, $wp_registered_widgets;
if ( !isset($post) || $post->post_type != 'landing-page') {
return;
}
$whitelist = array('sidebar-1','sidebar-left','primary','default','blog','sidebar-right','blog-sidebar','left-sidebar','right-sidebar');
$registered_widget_id = 'id_lp_conversion_area_widget-1';
foreach ($wp_registered_widgets as $key => $array ) {
if (strstr($key , 'id_lp_conversion_area_widget')) {
$registered_widget_id = $key;
break;
}
}
if (!is_active_sidebar('lp_sidebar')) {
$active_widgets = get_option('sidebars_widgets');
if (!isset($active_widgets['lp_sidebar']) || !$active_widgets['lp_sidebar'] ) {
$active_widgets['lp_sidebar'] = array($registered_widget_id);
$_wp_sidebars_widgets['lp_sidebar'] = $active_widgets['lp_sidebar'];
update_option('sidebars_widgets', $active_widgets);
}
}
$count = 0;
$found = 0;
foreach ($_wp_sidebars_widgets as $key => $val) {
foreach ($whitelist as $item) {
if (strpos($key, $item) !== FALSE || $key == 'sidebar') {
$_wp_sidebars_widgets['wp_inactive_widgets'] = array();
$_wp_sidebars_widgets[$key] = $_wp_sidebars_widgets['lp_sidebar'];
$found = 1;
}
}
if (!$found && $count===0) {
$_wp_sidebars_widgets[$key] = $_wp_sidebars_widgets['lp_sidebar'];
}
$count++;
}
}
}
new Landing_Pages_Sidebars;