| 1 |
<?php |
| 2 |
/** |
| 3 |
* Friends Header Widget |
| 4 |
* |
| 5 |
* A widget that allows you to show post formats lins on the friends sidebar. |
| 6 |
* |
| 7 |
* @package Friends |
| 8 |
* @since 1.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Friends; |
| 12 |
|
| 13 |
/** |
| 14 |
* This is the class for the Friends Post Formats Widget. |
| 15 |
* |
| 16 |
* @package Friends |
| 17 |
* @author Alex Kirk |
| 18 |
*/ |
| 19 |
class Widget_Post_Formats extends \WP_Widget { |
| 20 |
/** |
| 21 |
* Constructor |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
parent::__construct( |
| 25 |
'friends-widget-post-formats', |
| 26 |
__( 'Post Formats', 'friends' ), |
| 27 |
array( |
| 28 |
'description' => __( 'Show post formats in the sidebar.', 'friends' ), |
| 29 |
) |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Render the widget. |
| 35 |
* |
| 36 |
* @param array $args Sidebar arguments. |
| 37 |
* @param array $instance Widget instance settings. |
| 38 |
*/ |
| 39 |
public function widget( $args, $instance ) { |
| 40 |
$instance = wp_parse_args( $instance, $this->defaults() ); |
| 41 |
$friends = Friends::get_instance(); |
| 42 |
|
| 43 |
echo wp_kses( $args['before_widget'], 'post' ); |
| 44 |
?> |
| 45 |
<details class="accordion" open> |
| 46 |
<summary class="accordion-header"> |
| 47 |
<?php |
| 48 |
if ( ! empty( $instance['title'] ) ) { |
| 49 |
echo wp_kses( $args['before_title'] . $instance['title'] . $args['after_title'], 'post' ); |
| 50 |
} |
| 51 |
?> |
| 52 |
</summary> |
| 53 |
<ul class="friends-post-formats menu menu-nav accordion-body"> |
| 54 |
<li class="menu-item"><a href="<?php echo esc_attr( home_url( '/friends/' ) ); ?>"><?php _ex( 'All', 'all posts', 'friends' ); ?></a></li> |
| 55 |
<?php |
| 56 |
foreach ( get_post_format_strings() as $slug => $title ) : |
| 57 |
|
| 58 |
if ( ! isset( $instance[ 'show_post_format_' . $slug ] ) || ! $instance[ 'show_post_format_' . $slug ] ) { |
| 59 |
continue; |
| 60 |
} |
| 61 |
if ( ! isset( $instance[ 'post_format_title_' . $slug ] ) ) { |
| 62 |
$instance[ 'post_format_title_' . $slug ] = $title; |
| 63 |
} |
| 64 |
?> |
| 65 |
<li class="menu-item"><a href="<?php echo esc_attr( home_url( '/friends/type/' . $slug . '/' ) ); ?>"> <?php echo esc_attr( $instance[ 'post_format_title_' . $slug ] ); ?></a></li> |
| 66 |
<?php endforeach; ?> |
| 67 |
</ul> |
| 68 |
</details> |
| 69 |
<?php |
| 70 |
|
| 71 |
echo wp_kses( $args['after_widget'], 'post' ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Widget configuration form. |
| 76 |
* |
| 77 |
* @param array $instance The current settings. |
| 78 |
*/ |
| 79 |
public function form( $instance ) { |
| 80 |
$instance = array_merge( $this->defaults(), $instance ); |
| 81 |
?> |
| 82 |
<p> |
| 83 |
<label><?php /* phpcs:ignore WordPress.WP.I18n.MissingArgDomain */ esc_html_e( 'Title' ); ?><br/> |
| 84 |
<input class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /> |
| 85 |
</label> |
| 86 |
</p> |
| 87 |
<p><?php esc_html_e( 'Show Post Format Links:', 'friends' ); ?> |
| 88 |
<ul> |
| 89 |
<?php |
| 90 |
foreach ( get_post_format_strings() as $slug => $title ) : |
| 91 |
if ( ! isset( $instance[ 'post_format_title_' . $slug ] ) ) { |
| 92 |
$instance[ 'post_format_title_' . $slug ] = $title; |
| 93 |
} |
| 94 |
?> |
| 95 |
<li><label><input id="<?php echo esc_attr( $this->get_field_id( 'show_post_format_' . $slug ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'show_post_format_' . $slug ) ); ?>" type="checkbox" value="1"<?php checked( isset( $instance[ 'show_post_format_' . $slug ] ) && $instance[ 'show_post_format_' . $slug ] ); ?> /> <?php echo esc_html( $title ); ?></label><br/> |
| 96 |
<input id="<?php echo esc_attr( $this->get_field_id( 'post_format_title_' . $slug ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'post_format_title_' . $slug ) ); ?>" type="text" value="<?php echo esc_attr( $instance[ 'post_format_title_' . $slug ] ); ?>" placeholder="<?php echo esc_attr( $title ); ?>" /></li> |
| 97 |
<?php endforeach; ?> |
| 98 |
</ul></p> |
| 99 |
<?php |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Update widget configuration. |
| 104 |
* |
| 105 |
* @param array $new_instance New settings. |
| 106 |
* @param array $old_instance Old settings. |
| 107 |
* @return array Sanitized instance settings. |
| 108 |
*/ |
| 109 |
public function update( $new_instance, $old_instance ) { |
| 110 |
$instance = array(); |
| 111 |
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? wp_strip_all_tags( $new_instance['title'] ) : ''; |
| 112 |
$instance['title'] = apply_filters( 'wptexturize', $instance['title'] ); |
| 113 |
$instance['title'] = apply_filters( 'convert_chars', $instance['title'] ); |
| 114 |
|
| 115 |
foreach ( get_post_format_strings() as $slug => $title ) { |
| 116 |
$instance[ 'post_format_title_' . $slug ] = ( ! empty( $new_instance[ 'post_format_title_' . $slug ] ) ) ? wp_strip_all_tags( $new_instance[ 'post_format_title_' . $slug ] ) : $title; |
| 117 |
if ( $new_instance[ 'show_post_format_' . $slug ] ) { |
| 118 |
$instance[ 'show_post_format_' . $slug ] = true; |
| 119 |
} else { |
| 120 |
$instance[ 'show_post_format_' . $slug ] = false; |
| 121 |
} |
| 122 |
} |
| 123 |
return $instance; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Return an associative array of default values |
| 128 |
* |
| 129 |
* These values are used in new widgets. |
| 130 |
* |
| 131 |
* @return array Array of default values for the Widget's options |
| 132 |
*/ |
| 133 |
public function defaults() { |
| 134 |
$instance = array( |
| 135 |
'title' => __( 'Filter', 'friends' ), |
| 136 |
); |
| 137 |
|
| 138 |
foreach ( get_post_format_strings() as $slug => $title ) { |
| 139 |
$t = 'post_format_title_' . $slug; |
| 140 |
$instance[ $t ] = ( ! empty( $new_instance[ $t ] ) ) ? wp_strip_all_tags( $new_instance[ $t ] ) : $title; |
| 141 |
$instance[ 'show_post_format_' . $slug ] = in_array( $slug, array( 'standard', 'status', 'image', 'video' ) ); |
| 142 |
} |
| 143 |
|
| 144 |
return $instance; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Register this widget. |
| 149 |
*/ |
| 150 |
public static function register() { |
| 151 |
register_widget( __CLASS__ ); |
| 152 |
} |
| 153 |
} |
| 154 |
|