| 1 |
<?php |
| 2 |
/** |
| 3 |
* Friends Header Widget |
| 4 |
* |
| 5 |
* A widget that allows you to define the header for your own friends page. |
| 6 |
* |
| 7 |
* @package Friends |
| 8 |
* @since 0.8 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace Friends; |
| 12 |
|
| 13 |
/** |
| 14 |
* This is the class for the Friends Header Widget. |
| 15 |
* |
| 16 |
* @package Friends |
| 17 |
* @author Alex Kirk |
| 18 |
*/ |
| 19 |
class Widget_Header extends \WP_Widget { |
| 20 |
/** |
| 21 |
* Constructor |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
parent::__construct( |
| 25 |
'friends-widget-header', |
| 26 |
__( 'Friends Header', 'friends' ), |
| 27 |
array( |
| 28 |
'description' => __( 'The header for your friends page.', '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 |
$title = apply_filters( 'friends_header_widget_title', $instance['title'] ); |
| 44 |
$title = apply_filters( 'wptexturize', $title ); |
| 45 |
$title = apply_filters( 'convert_chars', $title ); |
| 46 |
|
| 47 |
echo $args['before_widget']; |
| 48 |
if ( ! empty( $title ) ) { |
| 49 |
echo $args['before_title'] . '<span class="dashicons dashicons-feedback"></span> ' . $title . $args['after_title']; |
| 50 |
} |
| 51 |
|
| 52 |
if ( $friends->frontend->author ) { |
| 53 |
?> |
| 54 |
<p> |
| 55 |
<?php |
| 56 |
echo wp_kses( |
| 57 |
sprintf( |
| 58 |
// translators: %1$s is a site name, %2$s is a URL. |
| 59 |
__( 'Visit %1$s. Back to <a href=%2$s>your friends page</a>.', 'friends' ), |
| 60 |
// translators: 1: a friend's display name, 2: a URL. |
| 61 |
'<a href="' . esc_url( $friends->frontend->author->user_url ) . '" class="auth-link" data-token="' . esc_attr( get_user_option( 'friends_out_token', $friends->frontend->author->ID ) ) . '">' . esc_html( sprintf( __( '%1$s\'s external site at %2$s', 'friends' ), $friends->frontend->author->display_name, preg_replace( '#https?://#', '', trim( $friends->frontend->author->user_url, '/' ) ) ) ) . '</a>', |
| 62 |
'"' . esc_attr( home_url( '/friends/' ) ) . '"' |
| 63 |
), |
| 64 |
array( |
| 65 |
'a' => array( |
| 66 |
'href' => array(), |
| 67 |
'class' => array(), |
| 68 |
'data-token' => array(), |
| 69 |
), |
| 70 |
) |
| 71 |
); |
| 72 |
?> |
| 73 |
</p> |
| 74 |
<?php |
| 75 |
} |
| 76 |
|
| 77 |
echo $args['after_widget']; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Widget configuration form. |
| 82 |
* |
| 83 |
* @param array $instance The current settings. |
| 84 |
*/ |
| 85 |
public function form( $instance ) { |
| 86 |
$instance = array_merge( $this->defaults(), $instance ); |
| 87 |
?> |
| 88 |
<p> |
| 89 |
<label><?php /* phpcs:ignore WordPress.WP.I18n.MissingArgDomain */ esc_html_e( 'Title' ); ?><br/> |
| 90 |
<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'] ); ?>" /> |
| 91 |
</label> |
| 92 |
</p> |
| 93 |
<?php |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Update widget configuration. |
| 98 |
* |
| 99 |
* @param array $new_instance New settings. |
| 100 |
* @param array $old_instance Old settings. |
| 101 |
* @return array Sanitized instance settings. |
| 102 |
*/ |
| 103 |
public function update( $new_instance, $old_instance ) { |
| 104 |
$instance = array(); |
| 105 |
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? wp_strip_all_tags( $new_instance['title'] ) : ''; |
| 106 |
$instance['show_post_formats'] = isset( $new_instance['show_post_formats'] ) && $new_instance['show_post_formats']; |
| 107 |
return $instance; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Return an associative array of default values |
| 112 |
* |
| 113 |
* These values are used in new widgets. |
| 114 |
* |
| 115 |
* @return array Array of default values for the Widget's options |
| 116 |
*/ |
| 117 |
public function defaults() { |
| 118 |
return array( |
| 119 |
'title' => __( 'Friends', 'friends' ), |
| 120 |
'show_post_formats' => true, |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Register this widget. |
| 126 |
*/ |
| 127 |
public static function register() { |
| 128 |
register_widget( __CLASS__ ); |
| 129 |
} |
| 130 |
} |
| 131 |
|