Skip to content

Instantly share code, notes, and snippets.

@GLWalker
Created March 4, 2022 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GLWalker/e8d875b7cd83242de4b18405ab508dbb to your computer and use it in GitHub Desktop.
Save GLWalker/e8d875b7cd83242de4b18405ab508dbb to your computer and use it in GitHub Desktop.
<?php
/**
* Putting this here for reference in case others need to accomplish similar. This creates
a new archives widget and adds some settings for bootstrap classes then apply's them
with a couple filters.
*/
/**
* Widget API: WP_Widget_Archives class
*
* @package WordPress
* @subpackage Widgets
* @since 4.4.0
*/
/**
* Core class used to implement the Archives widget.
*
* @since 2.8.0
*
* @see WP_Widget
*/
/**
* QSQ Archives Widget with additional settings and filters
*
*/
class OSQ_Widget_Archives extends WP_Widget
{
/**
* Link filtering function
*
* adds classes to list items
* @param [type] $link_html
* @return void
*/
public function osq_archives_list($link_html)
{
global $wp;
static $current_url;
$link_html = preg_replace('/(<[^\s>]+)/', '\1 class="list-group-item list-group-item-action ' . $this->list_color . '"', $link_html, 1);
if (empty($current_url)) {
$current_url = add_query_arg($_SERVER['QUERY_STRING'], '', home_url($wp->request));
}
if (stristr($link_html, $current_url)) {
$link_html = preg_replace('/(<[^\s>]+)/', '\1 class="list-group-item list-group-item-action ' . $this->list_color . ' active"', $link_html, 1);
}
return $link_html;
}
/**
* Count filtering function
*
* moves adds span with classes to $count items
* @param [type] $link_html
* @return void
*/
public function osq_archives_count($link_html)
{
$link_html = str_replace('</a>&nbsp;(', '<span class="badge ' . $this->bdg_style . ' ' . $this->bdg_color . ' ' . $this->bdg_text . ' float-end">', $link_html);
$link_html = str_replace(')', '</span></a>', $link_html);
return $link_html;
}
/**
* Sets up a new Archives widget instance.
*
* @since 2.8.0
*/
public function __construct()
{
$widget_ops = array(
'classname' => 'widget_osq_archives',
'description' => __('A monthly archive of your site&#8217;s Posts.'),
'customize_selective_refresh' => true,
'show_instance_in_rest' => true,
);
parent::__construct('archives_osq', __('Archives OSQ'), $widget_ops);
}
/**
* Outputs the content for the current Archives widget instance.
*
* @since 2.8.0
*
* @param array $args Display arguments including 'before_title', 'after_title',
* 'before_widget', and 'after_widget'.
* @param array $instance Settings for the current Archives widget instance.
*/
public function widget($args, $instance)
{
$default_title = __('Archives');
$title = !empty($instance['title']) ? $instance['title'] : $default_title;
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters('widget_title', $title, $instance, $this->id_base);
$count = !empty($instance['count']) ? '1' : '0';
$dropdown = !empty($instance['dropdown']) ? '1' : '0';
/* User classes per instance */
$ulclass = !empty($instance['make-flush']) ? ' list-group-flush' : '';
$this->list_color = !empty($instance['lg-color']) ? esc_attr($instance['lg-color']) : osq_get_option('widget_listgroup_color');
$this->bdg_color = !empty($instance['bdg-color']) ? esc_attr($instance['bdg-color']) : osq_get_option('widget_badge_bg_color');
$this->bdg_text = !empty($instance['bdg-text']) ? esc_attr($instance['bdg-text']) : osq_get_option('widget_badge_text_color');
// univeral badge style
$this->bdg_style = osq_get_option('badge_style');
echo $args['before_widget'];
if ($title) {
echo $args['before_title'] . $title . $args['after_title'];
}
// The title may be filtered: Strip out HTML and make sure the aria-label is never empty and so dropdown works correctly.
$title = trim(strip_tags($title));
if ($dropdown) {
$dropdown_id = "{$this->id_base}-dropdown-{$this->number}";
?>
<label class="screen-reader-text" for="<?php echo esc_attr($dropdown_id); ?>"><?php echo $title; ?></label>
<select id="<?php echo esc_attr($dropdown_id); ?>" name="archive-dropdown" class="form-select">
<?php
/**
* Filters the arguments for the Archives widget drop-down.
*
* @since 2.8.0
* @since 4.9.0 Added the `$instance` parameter.
*
* @see wp_get_archives()
*
* @param array $args An array of Archives widget drop-down arguments.
* @param array $instance Settings for the current Archives widget instance.
*/
$dropdown_args = apply_filters(
'widget_archives_dropdown_args',
array(
'type' => 'monthly',
'format' => 'option',
'show_post_count' => $count,
),
$instance
);
switch ($dropdown_args['type']) {
case 'yearly':
$label = __('Select Year');
break;
case 'monthly':
$label = __('Select Month');
break;
case 'daily':
$label = __('Select Day');
break;
case 'weekly':
$label = __('Select Week');
break;
default:
$label = __('Select Post');
break;
}
?>
<option value=""><?php echo esc_html($label); ?></option>
<?php wp_get_archives($dropdown_args); ?>
</select>
<script>
/*
<![CDATA[ */
(function() {
var dropdown = document.getElementById("<?php echo esc_js($dropdown_id); ?>");
function onSelectChange() {
if (dropdown.options[dropdown.selectedIndex].value !== '') {
document.location.href = this.options[this.selectedIndex].value;
}
}
dropdown.onchange = onSelectChange;
})();
/* ]]> */
</script>
<?php
} else {
$aria_label = $title ? $title : $default_title;
add_filter('get_archives_link', array($this, 'osq_archives_list'));
if ($count) {
add_filter('get_archives_link', array($this, 'osq_archives_count'));
}
echo '<nav role="navigation" aria-label="' . esc_attr($aria_label) . '">';
?>
<ul class="list-group<?php echo $ulclass; ?>">
<?php
wp_get_archives(
/**
* Filters the arguments for the Archives widget.
*
* @since 2.8.0
* @since 4.9.0 Added the `$instance` parameter.
*
* @see wp_get_archives()
*
* @param array $args An array of Archives option arguments.
* @param array $instance Array of settings for the current widget.
*/
apply_filters(
'widget_archives_args',
array(
'type' => 'monthly',
'show_post_count' => $count,
),
$instance
)
);
?>
</ul>
</nav>
<?php
}
echo $args['after_widget'];
}
/**
* Handles updating settings for the current Archives widget instance.
*
* @since 2.8.0
*
* @param array $new_instance New settings for this instance as input by the user via
* WP_Widget_Archives::form().
* @param array $old_instance Old settings for this instance.
* @return array Updated settings to save.
*/
public function update($new_instance, $old_instance)
{
$instance = $old_instance;
$instance['title'] = sanitize_text_field($new_instance['title']);
$instance['count'] = !empty($new_instance['count']) ? 1 : 0;
$instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0;
$instance['make-flush'] = !empty($new_instance['make-flush']) ? 1 : 0;
if (is_array($new_instance['lg-color'])) {
$new_instance['lg-color'] = array_filter($new_instance['lg-color']);
$new_instance['lg-color'] = implode(' ', $new_instance['lg-color']);
}
$instance['lg-color'] = esc_attr($new_instance['lg-color']);
if (is_array($new_instance['bdg-color'])) {
$new_instance['bdg-color'] = array_filter($new_instance['bdg-color']);
$new_instance['bdg-color'] = implode(' ', $new_instance['bdg-color']);
}
$instance['bdg-color'] = esc_attr($new_instance['bdg-color']);
if (is_array($new_instance['bdg-text'])) {
$new_instance['bdg-text'] = array_filter($new_instance['bdg-text']);
$new_instance['bdg-text'] = implode(' ', $new_instance['bdg-text']);
}
$instance['bdg-text'] = esc_attr($new_instance['bdg-text']);
return $instance;
}
/**
* Outputs the settings form for the Archives widget.
*
* @since 2.8.0
*
* @param array $instance Current settings.
*/
public function form($instance)
{
// Defaults.
$instance = wp_parse_args((array) $instance, array('title' => ''));
$count = isset($instance['count']) ? (bool) $instance['count'] : false;
$dropdown = isset($instance['dropdown']) ? (bool) $instance['dropdown'] : false;
$makeflush = isset($instance['make-flush']) ? (bool) $instance['make-flush'] : false;
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($instance['title']); ?>" />
</p>
<p>
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>" <?php checked($dropdown); ?> />
<label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e('Display as dropdown'); ?></label>
<br />
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" <?php checked($count); ?> />
<label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Show post counts'); ?></label>
</p>
<p class="osq-option-row">
<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('make-flush'); ?>" name="<?php echo $this->get_field_name('make-flush'); ?>" <?php checked($makeflush); ?> />
<label for="<?php echo $this->get_field_id('make-flush'); ?>"><?php _e('Render Flush'); ?></label>
</p>
<?php
$row = '';
/* List Group Background */
$field_lgc_name = $this->get_field_name('lg-color');
$field_lgc_id = $this->get_field_id('lg-color');
$row .= '<p class="osq-option-row">';
$row .= '<label class="osq-select-label" for="' . $this->get_field_id('lg-color') . '">' . __('List Color', 'osquantum') . ': &nbsp;</label>';
$row_lgc_column = '';
$lgc = $this->get_widget_option('list_group_color');
$instance['lg-color'] = explode(' ', $instance['lg-color']);
foreach ($lgc as $class_lgc_name) {
if (!empty($class_lgc_name)) {
$class_lgc_label = $class_lgc_name;
$selected_lgc = in_array($class_lgc_name, $instance['lg-color'], true);
$row_lgc_column .= '<option value="' . $class_lgc_name . '" ' . selected($selected_lgc, true, false) . '>' . $class_lgc_label . '</option>';
}
}
$row .= '<select name="' . $field_lgc_name . '" id="' . $field_lgc_id . '">';
$row .= '<option value="">- ' . __('default') . ' -</option>';
$row .= $row_lgc_column;
$row .= '</select> &nbsp;';
$row .= '</p>';
if ('' != $count) {
/* Badge Background */
$field_bdgc_name = $this->get_field_name('bdg-color');
$field_bdgc_id = $this->get_field_id('bdg-color');
$row .= '<p class="osq-option-row">';
$row .= '<label class="osq-select-label" for="' . $this->get_field_id('bdg-color') . '">' . __('Badge Color', 'osquantum') . ': &nbsp;</label>';
$row_bdgc_column = '';
$bdgc = $this->get_widget_option('bg_color');
$instance['bdg-color'] = explode(' ', $instance['bdg-color']);
foreach ($bdgc as $class_bdgc_name) {
if (!empty($class_bdgc_name)) {
$class_bdgc_label = $class_bdgc_name;
$selected_bdgc = in_array($class_bdgc_name, $instance['bdg-color'], true);
$row_bdgc_column .= '<option value="' . $class_bdgc_name . '" ' . selected($selected_bdgc, true, false) . '>' . $class_bdgc_label . '</option>';
}
}
$row .= '<select name="' . $field_bdgc_name . '" id="' . $field_bdgc_id . '">';
$row .= '<option value="">- ' . __('default') . ' -</option>';
$row .= $row_bdgc_column;
$row .= '</select> &nbsp;';
/* Badge Text */
$field_bdgt_name = $this->get_field_name('bdg-text');
$field_bdgt_id = $this->get_field_id('bdg-text');
$row .= '<label class="osq-select-label" for="' . $this->get_field_id('bdg-text') . '">' . __('Badge Text', 'osquantum') . ': &nbsp;</label>';
$row_bdgt_column = '';
$bdgt = $this->get_widget_option('text_color');
$instance['bdg-text'] = explode(' ', $instance['bdg-text']);
foreach ($bdgt as $class_bdgt_name) {
if (!empty($class_bdgt_name)) {
$class_bdgt_label = $class_bdgt_name;
$selected_bdgt = in_array($class_bdgt_name, $instance['bdg-text'], true);
$row_bdgt_column .= '<option value="' . $class_bdgt_name . '" ' . selected($selected_bdgt, true, false) . '>' . $class_bdgt_label . '</option>';
}
}
$row .= '<select name="' . $field_bdgt_name . '" id="' . $field_bdgt_id . '">';
$row .= '<option value="">- ' . __('default') . ' -</option>';
$row .= $row_bdgt_column;
$row .= '</select> &nbsp;';
$row .= '</p>';
}
echo $row;
return $instance;
}
/**
* Get the available array of options.
*
* @since 1.1.1
* @access public
* @return array
*/
public function get_widget_option($option = '')
{
if (!isset($option)) {
return;
}
static $done = false;
if ($done) {
return $this->$option;
}
$done = true;
return $this->$option;
}
/**
* Arrays' of BS classes.
*
* @since 1
* @var array
*/
private $bg_color = array(
'bg-primary' => 'bg-primary',
'bg-secondary' => 'bg-secondary',
'bg-success' => 'bg-success',
'bg-danger' => 'bg-danger',
'bg-warning' => 'bg-warning',
'bg-info' => 'bg-info',
'bg-light' => 'bg-light',
'bg-dark' => 'bg-dark',
'bg-white' => 'bg-white',
'bg-transparent' => 'bg-transparent',
'bg-user' => 'bg-user',
);
private $text_color = array(
'text-primary' => 'text-primary',
'text-secondary' => 'text-secondary',
'text-success' => 'text-success',
'text-danger' => 'text-danger',
'text-warning' => 'text-warning',
'text-info' => 'text-info',
'text-light' => 'text-light',
'text-dark' => 'text-dark',
'text-body' => 'text-body',
'text-muted' => 'text-muted',
'text-white' => 'text-white',
'text-black-50' => 'text-black-50',
'text-white-50' => 'text-white-50',
'text-user' => 'text-user',
);
private $list_group_color = array(
'list-group-item-primary' => 'list-group-item-primary',
'list-group-item-secondary' => 'list-group-item-secondary',
'list-group-item-success' => 'list-group-item-success',
'list-group-item-danger' => 'list-group-item-danger',
'list-group-item-warning' => 'list-group-item-warning',
'list-group-item-info' => 'list-group-item-info',
'list-group-item-light' => 'list-group-item-light',
'list-group-item-dark' => 'list-group-item-dark',
'list-group-item-user' => 'list-group-item-user',
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment