=' ) && !file_exists(WP_PLUGIN_DIR.'/limit-login-attempts-reloaded') && current_user_can('install_plugins') ) { $alert = (array)get_option( 'wpchefgadget_alert', array() ); if ( empty( $alert['limit-login-attempts'] ) ) { add_action( 'admin_notices', 'widget_logic_alert'); add_action( 'network_admin_notices', 'widget_logic_alert'); add_action( 'wp_ajax_wpchefgadget_dismiss_alert', 'widget_logic_dismiss_alert' ); add_action( 'admin_enqueue_scripts', 'widget_logic_alert_scripts' ); } //enqueue admin/js/updates.js } } */ } if((!$wl_options = get_option('widget_logic')) || !is_array($wl_options) ) $wl_options = array(); if (is_admin()) { add_filter( 'in_widget_form', 'widget_logic_in_widget_form', 10, 3 ); add_filter( 'widget_update_callback', 'widget_logic_update_callback', 10, 4); add_action( 'sidebar_admin_setup', 'widget_logic_expand_control'); // before any HTML output save widget changes and add controls to each widget on the widget admin page add_action( 'sidebar_admin_page', 'widget_logic_options_control'); // add Widget Logic specific options on the widget admin page add_filter( 'plugin_action_links', 'wl_charity', 10, 2);// add my justgiving page link to the plugin admin page add_action( 'widgets_init', 'widget_logic_add_controls', 999 ); } else { $loadpoint = isset($wl_options['widget_logic-options-load_point']) ? (string)@$wl_options['widget_logic-options-load_point'] : ''; if ( 'plugins_loaded' == $loadpoint ) widget_logic_sidebars_widgets_filter_add(); else { if ( !in_array( $loadpoint, array( 'after_setup_theme', 'wp_loaded', 'wp_head' ) ) ) $loadpoint = 'parse_query'; add_action( $loadpoint, 'widget_logic_sidebars_widgets_filter_add' ); } if ( !empty($wl_options['widget_logic-options-filter']) ) add_filter( 'dynamic_sidebar_params', 'widget_logic_widget_display_callback', 10); // redirect the widget callback so the output can be buffered and filtered } function widget_logic_in_customizer() { global $wl_in_customizer; $wl_in_customizer = true; //add_filter( 'widget_display_callback', 'widget_logic_customizer_display_callback', 10, 3 ); add_action( 'dynamic_sidebar', 'widget_logic_customizer_dynamic_sidebar_callback' ); } add_action( 'customize_preview_init', 'widget_logic_in_customizer' ); function widget_logic_sidebars_widgets_filter_add() { // actually remove the widgets from the front end depending on widget logic provided add_filter( 'sidebars_widgets', 'widget_logic_filter_sidebars_widgets', 10); } // wp-admin/widgets.php explicitly checks current_user_can('edit_theme_options') // which is enough security, I believe. If you think otherwise please contact me // CALLED VIA 'widget_update_callback' FILTER (ajax update of a widget) function widget_logic_update_callback( $instance, $new_instance, $old_instance, $this_widget ) { if ( isset( $new_instance['widget_logic'] ) ) $instance['widget_logic'] = $new_instance['widget_logic']; return $instance; } // CALLED VIA 'sidebar_admin_setup' ACTION // adds in the admin control per widget, but also processes import/export function widget_logic_expand_control() { global $wp_registered_widgets, $wp_registered_widget_controls, $wl_options; // EXPORT ALL OPTIONS if (isset($_GET['wl-options-export'])) { header("Content-Disposition: attachment; filename=widget_logic_options.txt"); header('Content-Type: text/plain; charset=utf-8'); echo "[START=WIDGET LOGIC OPTIONS]\n"; foreach ($wl_options as $id => $text) echo "$id\t".json_encode($text)."\n"; echo "[STOP=WIDGET LOGIC OPTIONS]"; exit; } // IMPORT ALL OPTIONS if ( isset($_POST['wl-options-import'])) { if ($_FILES['wl-options-import-file']['tmp_name']) { $import=explode("\n",file_get_contents($_FILES['wl-options-import-file']['tmp_name'], false)); if (array_shift($import)=="[START=WIDGET LOGIC OPTIONS]" && array_pop($import)=="[STOP=WIDGET LOGIC OPTIONS]") { foreach ($import as $import_option) { list($key, $value)=explode("\t",$import_option); $wl_options[$key]=json_decode($value); } $wl_options['msg']= __('Success! Options file imported','widget-logic'); } else { $wl_options['msg']= __('Invalid options file','widget-logic'); } } else $wl_options['msg']= __('No options file provided','widget-logic'); update_option('widget_logic', $wl_options); wp_redirect( admin_url('widgets.php') ); exit; } // UPDATE OTHER WIDGET LOGIC OPTIONS // must update this to use http://codex.wordpress.org/Settings_API if ( isset($_POST['widget_logic-options-submit']) ) { $wl_options['widget_logic-options-filter'] = !empty($_POST['widget_logic-options-filter']); $wl_options['widget_logic-options-wp_reset_query'] = !empty($_POST['widget_logic-options-wp_reset_query']); $wl_options['widget_logic-options-show_errors'] = !empty($_POST['widget_logic-options-show_errors']); $wl_options['widget_logic-options-load_point']=$_POST['widget_logic-options-load_point']; } update_option('widget_logic', $wl_options); } // CALLED VIA 'sidebar_admin_page' ACTION // output extra HTML // to update using http://codex.wordpress.org/Settings_API asap function widget_logic_options_control() { global $wp_registered_widget_controls, $wl_options; if ( isset($wl_options['msg'])) { if (substr($wl_options['msg'],0,2)=="OK") echo '
'; else echo '
'; echo '

Widget Logic – '.$wl_options['msg'].'

'; unset($wl_options['msg']); update_option('widget_logic', $wl_options); } ?>

__( 'Load all WL options from a plain text config file', 'widget-logic' ) ) ); ?>

$widget ) { if ( preg_match( '/^(.+)-(\d+)$/', $id) ) continue; if ( !isset( $wp_registered_widget_controls[ $id ] ) ) { wp_register_widget_control( $id, $id, 'widget_logic_extra_control', array(), $id, null ); continue; } if ( @$wp_registered_widget_controls[ $id ]['callback'] != 'widget_logic_extra_control' ) { $wp_registered_widget_controls[$id]['params'][] = $id; $wp_registered_widget_controls[$id]['params'][] = @$wp_registered_widget_controls[$id]['callback']; $wp_registered_widget_controls[$id]['callback'] = 'widget_logic_extra_control'; $wp_registered_widget_updates[$id]['params'][] = $id; $wp_registered_widget_updates[$id]['params'][] = @$wp_registered_widget_updates[$id]['callback']; $wp_registered_widget_updates[$id]['callback'] = 'widget_logic_extra_control'; } } } function widget_logic_in_widget_form( $widget, $return, $instance ) { $logic = isset( $instance['widget_logic'] ) ? $instance['widget_logic'] : widget_logic_by_id( $widget->id ); ?>

is_preview() ): ?>
%s', esc_attr(admin_url('widgets.php')), __('widgets') ) ) ?>

'.esc_html__('Charity Donation', 'widget-logic').''); return $links; } // FRONT END FUNCTIONS... function widget_logic_by_id( $widget_id ) { global $wl_options; if ( preg_match( '/^(.+)-(\d+)$/', $widget_id, $m ) ) { $widget_class = $m[1]; $widget_i = $m[2]; $info = get_option( 'widget_'.$widget_class ); if ( empty( $info[ $widget_i ] ) ) return ''; $info = $info[ $widget_i ]; } else $info = (array)get_option( 'widget_'.$widget_id, array() ); if ( isset( $info['widget_logic'] ) ) $logic = $info['widget_logic']; elseif ( isset( $wl_options[ $widget_id ] ) ) { $logic = stripslashes( $wl_options[ $widget_id ] ); widget_logic_save( $widget_id, $logic ); unset( $wl_options[ $widget_id ] ); update_option( 'widget_logic', $wl_options ); } else $logic = ''; return $logic; } function widget_logic_save( $widget_id, $logic ) { global $wl_options; if ( preg_match( '/^(.+)-(\d+)$/', $widget_id, $m ) ) { $widget_class = $m[1]; $widget_i = $m[2]; $info = get_option( 'widget_'.$widget_class ); if ( !is_array( $info[ $widget_i ] ) ) $info[ $widget_i ] = array(); $info[ $widget_i ]['widget_logic'] = $logic; update_option( 'widget_'.$widget_class, $info ); } else { $info = (array)get_option( 'widget_'.$widget_id, array() ); $info['widget_logic'] = $logic; update_option( 'widget_'.$widget_id, $info ); } } // CALLED ON 'sidebars_widgets' FILTER function widget_logic_filter_sidebars_widgets( $sidebars_widgets ) { global $wl_options, $wl_in_customizer; if ( $wl_in_customizer ) return $sidebars_widgets; // reset any database queries done now that we're about to make decisions based on the context given in the WP query for the page if ( !empty( $wl_options['widget_logic-options-wp_reset_query'] ) ) wp_reset_query(); // loop through every widget in every sidebar (barring 'wp_inactive_widgets') checking WL for each one foreach($sidebars_widgets as $widget_area => $widget_list) { if ($widget_area=='wp_inactive_widgets' || empty($widget_list)) continue; foreach($widget_list as $pos => $widget_id) { $logic = widget_logic_by_id( $widget_id ); if ( !widget_logic_check_logic( $logic ) ) unset($sidebars_widgets[$widget_area][$pos]); } } return $sidebars_widgets; } function widget_logic_check_logic( $logic ) { $logic = @trim( (string)$logic ); $logic = apply_filters( "widget_logic_eval_override", $logic ); if ( is_bool( $logic ) ) return $logic; if ( $logic === '' ) return true; if ( stristr( $logic, "return" ) === false ) $logic = "return ( $logic );"; set_error_handler( 'widget_logic_error_handler' ); try { $show_widget = eval($logic); } catch ( Error $e ) { trigger_error( $e->getMessage(), E_USER_WARNING ); $show_widget = false; } restore_error_handler(); return $show_widget; } function widget_logic_error_handler( $errno , $errstr ) { global $wl_options; $show_errors = !empty($wl_options['widget_logic-options-show_errors']) && current_user_can('manage_options'); if ( $show_errors ) echo 'Invalid Widget Logic: '.$errstr; return true; } function widget_logic_customizer_dynamic_sidebar_callback( $widget ) { widget_logic_customizer_display( $widget['id'] ); } function widget_logic_customizer_display( $widget_id ) { if ( !preg_match( '/^(.+)-(\d+)$/', $widget_id) ) return; $logic = widget_logic_by_id( $widget_id ); global $wl_options; $show_errors = !empty($wl_options['widget_logic-options-show_errors']) && current_user_can('manage_options'); ob_start(); $show_widget = widget_logic_check_logic( $logic ); $error = ob_get_clean(); if ( $show_errors && $error ) : ?>