/** * fetches the settings, transformed into shortcode parameters, for a specific instance of a widget * * @param array $atts Shortcode attributes * @return array New parameters for the shortcode (atts and content), or false if error or not found */ public function widget_fetch_settings($atts) { global $wp_registered_widgets, $shortcode_tags; if (empty($atts) || !is_array($atts) || !array_key_exists('widget', $atts)) { return false; } //the id must be numeric (and positive!) and I need the widget class and shortcode tags... if (!empty($atts['widget']) && is_numeric($atts['widget']) && class_exists(self::$widget_class) && !empty($shortcode_tags)) { $id = intval($atts['widget']); if ($id > 0) { //make the full id... $widget_id = 'custom-menu-wizard-' . intval($id); //get the widget instances for all sidebars... $sidebars_widgets = wp_get_sidebars_widgets(); if (is_array($sidebars_widgets)) { //by default, DON'T look in orphaned sidebars - but can be turned on... $orphaned = array_key_exists('orphaned', $atts) && !empty($atts['orphaned']); //by default, DO look in WP's inactive sidebar - but can be turned off... $inactive = !array_key_exists('inactive', $atts) || !empty($atts['inactive']); //search through all widget instances for one with our full id... foreach ($sidebars_widgets as $sidebar => $widgets) { //check whether we should be including inactive/orphaned sidebars in our search... if (($inactive || $sidebar !== 'wp_inactive_widgets') && ($orphaned || substr($sidebar, 0, 16) !== 'orphaned_widgets') && is_array($widgets) && in_array($widget_id, $widgets) && isset($wp_registered_widgets[$widget_id])) { //found it : get its option settings... $settings = get_option($wp_registered_widgets[$widget_id]['callback'][0]->option_name); if (!empty($settings) && array_key_exists($id, $settings) && !empty($settings[$id]) && is_array($settings[$id])) { //seems we have a widget! : //...create a regex pattern using WP's standard but replacing all shortcodes with just ours... $repl = array_keys($shortcode_tags); $repl = join('|', array_map('preg_quote', $repl)); $pattern = str_replace("({$repl})", '(cmwizard)', get_shortcode_regex()); //...sanitize the settings, create the equivalent shortcode, then match // against the modified pattern to get attributes and content... if (preg_match("/{$pattern}/s", Custom_Menu_Wizard_Widget::cmw_shortcode(Custom_Menu_Wizard_Widget::cmw_settings($settings[$id])), $m) > 0) { //...and return them return array('atts' => shortcode_parse_atts(trim($m[3])), 'content' => isset($m[5]) ? $m[5] : null); } } else { //seems there are no valid option settings for this widget instance!... break; } } } } } } return false; }
/** * hooked into custom_menu_wizard_encode_shortcode filter : converts a cmwizard shortcode into instance settings fit for * the widget() method of Custom_Menu_Wizard_Widget * * it's important to note that a shortcode processed this way does *NOT* hit the filters that a cmwizard shortcode would * normally hit, namely custom_menu_wizard_shortcode_attributes & shortcode_atts_cmwizard * * @param string $shortcode A full [cmwizard .../] shortcode * @return array|boolean Instance settings, or false if error */ public function encode_shortcode($shortcode = '') { if (class_exists(self::$widget_class) && preg_match('/^cmwizard\\s?(.*)$/', rtrim(ltrim($shortcode, '[ '), '] /'), $m) > 0) { $instance = $this->shortcode_instance(shortcode_parse_atts(trim($m[1])), 'cmwizard'); if (!empty($instance)) { $instance['cmwv'] = self::$version; $instance = Custom_Menu_Wizard_Widget::cmw_settings($instance, false, 'widget'); } } return empty($instance) ? false : $instance; }