public function wp_enqueue_scripts() { // Note Widget Styles (only enqueue styles if this widget is active) if (function_exists('Note_Widget')) { // Grab the Note Widget instance $note_widget = Note_Widget(); if (is_active_widget(false, false, $note_widget->id_base, true)) { // Note Widget wp_enqueue_style('note-widget', Note::plugin_url() . '/assets/css/widgets/note-widget.css', false, Note::$version); } } }
/** * This function adds CSS classes to Note widgets. */ function dynamic_sidebar_params($params) { global $wp_registered_widgets; // Bail if Note doesn't exist if (!function_exists('Note_Widget')) { return $params; } // Note Widget instance $note_widget = Note_Widget(); // Only on Note Widgets if ($params[0]['widget_name'] === $note_widget->name) { $the_note_widget = $wp_registered_widgets[$params[0]['widget_id']]; // Determine if we can get the instance if (is_array($the_note_widget['callback']) && is_a($the_note_widget['callback'][0], 'Note_Widget')) { $instance = $the_note_widget['callback'][0]->get_settings(); // Get all Note widget settings if (array_key_exists($params[1]['number'], $instance)) { $instance = $instance[$params[1]['number']]; } // If we have an instance if (!empty($instance) && isset($instance['css_class']) && $instance['css_class']) { $css_classes = explode(' ', $instance['css_class']); // Append container suffix to end of classes foreach ($css_classes as &$css_class) { $css_class .= '-container'; } $css_classes = implode(' ', $css_classes); // Add classes to $before_widget $params[0]['before_widget'] = str_replace('class="', 'class="' . $css_classes . ' ', $params[0]['before_widget']); } } } return $params; }
/** * This function adjusts the sidebar parameters for widgets. */ public function dynamic_sidebar_params($params) { // Bail if we're not on the front-end if (is_admin()) { return $params; } // If Note exists if (function_exists('Note_Widget')) { // Grab the Note Widget instance $note_widget = Note_Widget(); // Only on Note Widgets if (_get_widget_id_base($params[0]['widget_id']) === $note_widget->id_base) { // Store a reference to the widget settings (all Note Widgets) $note_widget_settings = $note_widget->get_settings(); // Determine if this is a valid Note widget if (array_key_exists($params[1]['number'], $note_widget_settings)) { // Grab widget settings $instance = $note_widget_settings[$params[1]['number']]; // If we have a template if (property_exists($note_widget, 'templates') && isset($instance['template']) && !empty($instance['template']) && array_key_exists($instance['template'], $note_widget->templates)) { // Grab the template details for this widget $template = $note_widget->templates[$instance['template']]; // CSS Classes $css_classes = array(); // Check the template type first if (isset($template['type'])) { $css_classes[] = sanitize_html_class($template['type'] . '-widget'); } // Then check the template if (empty($css_classes) && isset($template['template'])) { $css_classes[] = sanitize_html_class($template['template'] . '-widget'); } // Otherwise fallback to the name if (empty($css_classes)) { $css_classes[] = sanitize_html_class($instance['template'] . '-widget'); } // Adjust the before_widget parameter (only replacing once to ensure only the outer most wrapper element gets the CSS class adjustment) $params[0]['before_widget'] = preg_replace('/class="/', 'class="' . esc_attr(implode(' ', $css_classes)) . ' ', $params[0]['before_widget'], 1); } } } } // If Conductor exists if (function_exists('Conduct_Widget')) { // Grab the Conductor Widget instance $conductor_widget = Conduct_Widget(); // Only on Conductor Widgets if (_get_widget_id_base($params[0]['widget_id']) === $conductor_widget->id_base) { // Store a reference to the widget settings (all Conductor Widgets) $conductor_widget_settings = $conductor_widget->get_settings(); // Determine if this is a valid Conductor widget if (array_key_exists($params[1]['number'], $conductor_widget_settings)) { // Grab widget settings $instance = $conductor_widget_settings[$params[1]['number']]; // If we have flexbox display in the Front Page Sidebar if ($params[0]['id'] === 'front-page-sidebar' && $this->conductor_has_flexbox_display()) { // Grab this widget's display configuration $widget_display_config = isset($instance['widget_size']) && isset($conductor_widget->displays[$instance['widget_size']]) ? $conductor_widget->displays[$instance['widget_size']] : false; // Verify that the widget size supports columns //isset( $instance['widget_size'] ) && $instance['widget_size'] === 'flexbox' if (!empty($widget_display_config) && $conductor_widget->widget_display_supports_customize_property($widget_display_config, 'columns')) { // Adjust the before_widget parameter (only replacing once to ensure only the outer most wrapper element gets the CSS class adjustment) $params[0]['before_widget'] = preg_replace('/<div class="in front-page-widget-in cf">/', '', $params[0]['before_widget'], 1); // Adjust the after_widget parameter (only replacing once to ensure only the outer most wrapper element gets the CSS class adjustment) $params[0]['after_widget'] = preg_replace('/<\\/div>/', '', $params[0]['after_widget'], 1); } } } } } return $params; }
/** * This function prepends input elements to Note widgets * for use in the Previewer JS scripts. */ function dynamic_sidebar_params($params) { $note_widget = Note_Widget(); // Only on Note Widgets if ($params[0]['widget_name'] === $note_widget->name) { $widget_after = '<input type="hidden" name="widget_number" class="widget-number" value="' . esc_attr($params[1]['number']) . '" />'; // Widget Number $widget_after .= '<input type="hidden" name="widget_id" class="widget-id" value="' . esc_attr($params[0]['widget_id']) . '" />'; // Widget ID $widget_after .= '<input type="hidden" name="sidebar_name" class="sidebar-name" value="' . esc_attr($params[0]['name']) . '" />'; // Sidebar Name $widget_after .= '<input type="hidden" name="sidebar_id" class="sidebar-id" value="' . esc_attr($params[0]['id']) . '" />'; // Sidebar ID // Modify the 'after_widget' param to include data we'll send to Customizer $params[0]['after_widget'] = $widget_after . $params[0]['after_widget']; } return $params; }
/** * This function sets up Note Widget localize data. */ public function setup_note_widget_localize_data() { global $wp_registered_widgets; $note_widget = Note_Widget(); // Note Widget instance // Re-add the widgets key if it doesn't exist (due to filtering above) if (!isset($this->note_localize['widgets'])) { $this->note_localize['widgets'] = array('defaults' => array(), 'settings' => array(), 'templates' => array()); } // Setup the defaults data $this->note_localize['widgets']['defaults'] = $note_widget->defaults; // Setup the template data $this->note_localize['widgets']['templates'] = $note_widget->templates; // Setup the background image CSS $this->note_localize['widgets']['background_image_css'] = $note_widget->background_image_css; // Find Note Widgets in sidebars // TODO: The following logic will fetch data for all Note Widgets in all sidebars, can we just output data for displayed widgets? $sidebars_widgets = wp_get_sidebars_widgets(); $note_widget_settings = array(); if (is_array($sidebars_widgets)) { // Loop through sidebars foreach ($sidebars_widgets as $sidebar => $widgets) { // Ignore inactive or orphaned if ($sidebar !== 'wp_inactive_widgets' && substr($sidebar, 0, 16) !== 'orphaned_widgets' && is_array($widgets)) { // Loop through widgets foreach ($widgets as $widget) { // Verify that this is a Note Widget if ($note_widget->id_base === _get_widget_id_base($widget)) { // Make sure this widget has a callback if (isset($wp_registered_widgets[$widget])) { // Store a reference to this widget object $wp_widget = $wp_registered_widgets[$widget]; $widget_number = $wp_widget['params'][0]['number']; // Store a reference to the widget settings (all Note Widgets) if (empty($note_widget_settings)) { $note_widget_settings = $note_widget->get_settings(); } // Find this widget in settings if (array_key_exists($widget_number, $note_widget_settings)) { // Widget settings (parse with Note Widget defaults to prevent PHP warnings and missing setting values) $this->note_localize['widgets']['settings'][$widget_number] = wp_parse_args((array) $note_widget_settings[$widget_number], $note_widget->defaults); // Store a reference to the widget number $this->note_localize['widgets']['settings'][$widget_number]['widget_number'] = $widget_number; // Store a reference to the widget ID $this->note_localize['widgets']['settings'][$widget_number]['widget_id'] = $widget; // Store a reference to the sidebar ID $this->note_localize['widgets']['settings'][$widget_number]['sidebar_id'] = $sidebar; } } } } } } } // Allow for filtering of localization widget data $this->note_localize['widgets'] = apply_filters('note_localize_widgets', $this->note_localize['widgets'], $this); }
$note_options = Note_Options::get_options(); // Remove Note data upon uninstall if ($note_options['uninstall']['data']) { // Widgets grouped by sidebar $sidebars_widgets = wp_get_sidebars_widgets(); if (empty($sidebars_widgets)) { $sidebars_widgets = wp_get_widget_defaults(); } // Unregister Note Sidebars if (is_array($note_options['sidebars'])) { // Loop through posts foreach ($note_options['sidebars'] as $post_id => $note_sidebar_ids) { // Loop through Note Sidebar IDs foreach ($note_sidebar_ids as $sidebar_id) { // Find the Note Sidebar ID for this sidebar $note_sidebar_id = Note_Sidebars::get_sidebar_id($sidebar_id, $post_id); // Remove this sidebar if it was found in sidebars widgets if (isset($sidebars_widgets[$note_sidebar_id])) { unset($sidebars_widgets[$note_sidebar_id]); } } } // Update the sidebars/widgets wp_set_sidebars_widgets($sidebars_widgets); } // Grab an instance of the Note Widget and remove the settings $note_widget = Note_Widget(); delete_option($note_widget->option_name); // Delete the Note option delete_option(Note_Options::$option_name); }