Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 59 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| wpcom_freshly_pressed_widget_init | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| WPCOM_Freshly_Pressed_Widget | |
0.00% |
0 / 56 |
|
0.00% |
0 / 7 |
110 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| widget | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
6 | |||
| update | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| form | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| get_badge_url | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| badges | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| get_badge_size | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php // phpcs:ignore Squiz.Commenting.FileComment.Missing |
| 2 | /** |
| 3 | * Freshly Pressed widget from WordPress.com. |
| 4 | */ |
| 5 | class WPCOM_Freshly_Pressed_Widget extends WP_Widget { |
| 6 | |
| 7 | /** |
| 8 | * Constructor. |
| 9 | */ |
| 10 | public function __construct() { |
| 11 | $widget_ops = array( |
| 12 | 'classname' => 'widget_freshly_pressed', |
| 13 | 'description' => __( 'Display a Freshly Pressed badge in your sidebar', 'wpcomsh' ), |
| 14 | ); |
| 15 | $control_ops = array( 'width' => 250 ); |
| 16 | parent::__construct( 'freshly_pressed', __( 'Freshly Pressed', 'wpcomsh' ), $widget_ops, $control_ops ); |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Display the widget. |
| 21 | * |
| 22 | * @param array $args Widget arguments. |
| 23 | * @param array $instance Widget instance. |
| 24 | */ |
| 25 | public function widget( $args, $instance ) { |
| 26 | $defaults = array( |
| 27 | 'title' => '', |
| 28 | 'badge' => 'rectangle', |
| 29 | ); |
| 30 | $instance = wp_parse_args( (array) $instance, $defaults ); |
| 31 | |
| 32 | echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 33 | |
| 34 | $title = apply_filters( 'widget_title', $instance['title'] ); |
| 35 | |
| 36 | if ( $title ) { |
| 37 | echo $args['before_title'] . esc_html( $title ) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 38 | } |
| 39 | |
| 40 | $badge_size = $this->get_badge_size( $instance['badge'] ); |
| 41 | |
| 42 | printf( |
| 43 | '<a href="https://wordpress.com/discover/" title="%1$s"><img src="%2$s" width="%3$dpx" height="%4$dpx" /></a>', |
| 44 | esc_attr__( 'Featured on Freshly Pressed', 'wpcomsh' ), |
| 45 | esc_url( $this->get_badge_url( $instance['badge'] ) ), |
| 46 | (int) $badge_size['width'], |
| 47 | (int) $badge_size['height'] |
| 48 | ); |
| 49 | |
| 50 | echo "\n" . $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 51 | |
| 52 | do_action( 'jetpack_stats_extra', 'widget_view', 'freshly_pressed' ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Update the widget settings. |
| 57 | * |
| 58 | * @param array $new_instance New settings. |
| 59 | * @param array $old_instance Old settings. |
| 60 | */ |
| 61 | public function update( $new_instance, $old_instance ) { |
| 62 | $instance = $old_instance; |
| 63 | |
| 64 | $instance['title'] = wp_strip_all_tags( $new_instance['title'] ); |
| 65 | $instance['badge'] = esc_attr( $new_instance['badge'] ); |
| 66 | |
| 67 | return $instance; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Display the widget settings form. |
| 72 | * |
| 73 | * @param array $instance Current settings. |
| 74 | * @return never |
| 75 | */ |
| 76 | public function form( $instance ) { |
| 77 | $defaults = array( |
| 78 | 'title' => '', |
| 79 | 'badge' => 'rectangle', |
| 80 | ); |
| 81 | $instance = wp_parse_args( (array) $instance, $defaults ); |
| 82 | |
| 83 | $title = esc_attr( $instance['title'] ); |
| 84 | $badge = esc_attr( $instance['badge'] ); |
| 85 | |
| 86 | echo '<p><label for="' . esc_attr( $this->get_field_id( 'title' ) ) . '">' . esc_html__( 'Title:', 'wpcomsh' ) . ' |
| 87 | <input class="widefat" id="' . esc_attr( $this->get_field_id( 'title' ) ) . '" name="' . esc_attr( $this->get_field_name( 'title' ) ) . '" type="text" value="' . esc_attr( $title ) . '" /> |
| 88 | </label></p> |
| 89 | |
| 90 | <p>' . esc_html__( 'Choose an image to display in your sidebar:', 'wpcomsh' ) . '<br /><br />'; |
| 91 | |
| 92 | foreach ( $this->badges() as $badge_name => $badge_ops ) { |
| 93 | echo '<input type="radio" name="' . esc_attr( $this->get_field_name( 'badge' ) ) . '" value="' . esc_attr( $badge_name ) . '" ' . checked( $badge_name, $badge, false ) . '/> <img src="' . esc_url( $this->get_badge_url( $badge_name ) ) . '" width="' . intval( $badge_ops['width'] ) . 'px" height="' . intval( $badge_ops['height'] ) . 'px" style="vertical-align: middle" /><br /><br />'; |
| 94 | } |
| 95 | |
| 96 | echo '</select></p>'; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get badge URL. |
| 101 | * |
| 102 | * @param string $badge Badge type. |
| 103 | * |
| 104 | * @return string Badge URL. |
| 105 | */ |
| 106 | public function get_badge_url( $badge ) { |
| 107 | return 'https://s0.wp.com/i/badges/freshly-pressed-' . $badge . '.png'; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Get badge types. |
| 112 | * |
| 113 | * @return array[] Badge types. |
| 114 | */ |
| 115 | public function badges() { |
| 116 | return array( |
| 117 | 'rectangle' => array( |
| 118 | 'width' => 200, |
| 119 | 'height' => 62, |
| 120 | ), |
| 121 | 'circle' => array( |
| 122 | 'width' => 150, |
| 123 | 'height' => 150, |
| 124 | ), |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Get badge type size. |
| 130 | * |
| 131 | * @param string $badge Badge type. |
| 132 | * |
| 133 | * @return array{width: int, height: int} |
| 134 | */ |
| 135 | public function get_badge_size( $badge ) { |
| 136 | $badges = $this->badges(); |
| 137 | |
| 138 | $badge = isset( $badges[ $badge ] ) ? $badge : 'rectangle'; |
| 139 | |
| 140 | return $badges[ $badge ]; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Only activate the widget for users that already have an instance of it. |
| 146 | */ |
| 147 | function wpcom_freshly_pressed_widget_init() { // phpcs:ignore Universal.Files.SeparateFunctionsFromOO.Mixed |
| 148 | if ( is_active_widget( false, false, 'freshly_pressed' ) ) { |
| 149 | register_widget( 'WPCOM_Freshly_Pressed_Widget' ); |
| 150 | } |
| 151 | } |
| 152 | add_action( 'widgets_init', 'wpcom_freshly_pressed_widget_init' ); |