Widget.php
246 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPressPopularPosts\Widget; |
| 4 | |
| 5 | use WordPressPopularPosts\{ Helper, Image, Output, Themer, Translate }; |
| 6 | use WordPressPopularPosts\Traits\QueriesPosts; |
| 7 | |
| 8 | class Widget extends \WP_Widget { |
| 9 | |
| 10 | use QueriesPosts; |
| 11 | |
| 12 | /** |
| 13 | * Default options. |
| 14 | * |
| 15 | * @since 5.0.0 |
| 16 | * @var array |
| 17 | */ |
| 18 | private $defaults = []; |
| 19 | |
| 20 | /** |
| 21 | * Administrative settings. |
| 22 | * |
| 23 | * @since 2.3.3 |
| 24 | * @var array |
| 25 | */ |
| 26 | private $config = []; |
| 27 | |
| 28 | /** |
| 29 | * Image object. |
| 30 | * |
| 31 | * @since 5.0.0 |
| 32 | * @var WordPressPopularPosts\Image |
| 33 | */ |
| 34 | private $thumbnail; |
| 35 | |
| 36 | /** |
| 37 | * Output object. |
| 38 | * |
| 39 | * @var \WordPressPopularPosts\Output |
| 40 | * @access private |
| 41 | */ |
| 42 | private $output; |
| 43 | |
| 44 | /** |
| 45 | * Translate object. |
| 46 | * |
| 47 | * @var \WordPressPopularPosts\Translate $translate |
| 48 | * @access private |
| 49 | */ |
| 50 | private $translate; |
| 51 | |
| 52 | /** |
| 53 | * Themer object. |
| 54 | * |
| 55 | * @var \WordPressPopularPosts\Themer $themer |
| 56 | * @access private |
| 57 | */ |
| 58 | private $themer; |
| 59 | |
| 60 | /** |
| 61 | * Construct. |
| 62 | * |
| 63 | * @since 1.0.0 |
| 64 | * @param array $options |
| 65 | * @param array $config |
| 66 | * @param \WordPressPopularPosts\Output $output |
| 67 | * @param \WordPressPopularPosts\Image $image |
| 68 | * @param \WordPressPopularPosts\Translate $translate |
| 69 | * @param \WordPressPopularPosts\Themer $themer |
| 70 | */ |
| 71 | public function __construct(array $options, array $config, Output $output, Image $thumbnail, Translate $translate, Themer $themer) |
| 72 | { |
| 73 | // Create the widget |
| 74 | parent::__construct( |
| 75 | 'wpp', |
| 76 | 'WP Popular Posts', |
| 77 | [ |
| 78 | 'classname' => 'popular-posts', |
| 79 | 'description' => 'The most Popular Posts on your blog.' |
| 80 | ] |
| 81 | ); |
| 82 | |
| 83 | $this->defaults = $options; |
| 84 | $this->config = $config; |
| 85 | $this->output = $output; |
| 86 | $this->thumbnail = $thumbnail; |
| 87 | $this->translate = $translate; |
| 88 | $this->themer = $themer; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Widget hooks. |
| 93 | * |
| 94 | * @since 5.0.0 |
| 95 | */ |
| 96 | public function hooks() |
| 97 | { |
| 98 | // Register the widget |
| 99 | add_action('widgets_init', [$this, 'register']); |
| 100 | // Remove widget from Legacy Widget block |
| 101 | add_filter('widget_types_to_hide_from_legacy_widget_block', [$this, 'remove_from_legacy_widget_block']); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Registers the widget. |
| 106 | * |
| 107 | * @since 5.0.0 |
| 108 | */ |
| 109 | public function register() |
| 110 | { |
| 111 | register_widget($this); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Outputs the content of the widget. |
| 116 | * |
| 117 | * @since 1.0.0 |
| 118 | * @param array $args The array of form elements. |
| 119 | * @param array $instance The current instance of the widget. |
| 120 | */ |
| 121 | public function widget($args, $instance) |
| 122 | { |
| 123 | /** |
| 124 | * @var string $name |
| 125 | * @var string $id |
| 126 | * @var string $description |
| 127 | * @var string $class |
| 128 | * @var string $before_widget |
| 129 | * @var string $after_widget |
| 130 | * @var string $before_title |
| 131 | * @var string $after_title |
| 132 | * @var string $widget_id |
| 133 | * @var string $widget_name |
| 134 | */ |
| 135 | extract($args, EXTR_SKIP); |
| 136 | |
| 137 | echo "\n" . $before_widget . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 138 | |
| 139 | $notice = ''; |
| 140 | |
| 141 | if ( is_user_logged_in() && current_user_can('manage_options') ) { |
| 142 | ob_start(); |
| 143 | ?> |
| 144 | <style> |
| 145 | .wpp-notice { |
| 146 | margin: 0 0 22px; |
| 147 | padding: 18px 22px; |
| 148 | background: #fcfcf7; |
| 149 | border: #ffff63 4px solid; |
| 150 | } |
| 151 | |
| 152 | .wpp-notice p { |
| 153 | color: #000 !important; |
| 154 | } |
| 155 | |
| 156 | .wpp-notice p:nth-child(2n) { |
| 157 | margin: 0; |
| 158 | font-size: 0.85em; |
| 159 | } |
| 160 | </style> |
| 161 | <div class="wpp-notice"> |
| 162 | <p><strong>Important notice for administrators:</strong> The WP Popular Posts "classic" widget has been removed.</p> |
| 163 | <p>This widget has reached end-of-life as of version 7.0. Please go to <strong>Appearance > Widgets > [Your Sidebar] > WP Popular Posts</strong> for instructions on migrating your popular posts list to either the WP Popular Posts block or the wpp shortcode.</p> |
| 164 | </div> |
| 165 | <?php |
| 166 | $notice = ob_get_clean() . "\n"; |
| 167 | } |
| 168 | |
| 169 | echo $notice; |
| 170 | |
| 171 | echo "\n" . $after_widget . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Generates the administration form for the widget. |
| 176 | * |
| 177 | * @since 1.0.0 |
| 178 | * @param array $instance The array of keys and values for the widget. |
| 179 | */ |
| 180 | public function form($instance) |
| 181 | { |
| 182 | $instance = Helper::merge_array_r( |
| 183 | $this->defaults, |
| 184 | (array) $instance |
| 185 | ); |
| 186 | require plugin_dir_path(__FILE__) . '/form.php'; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Processes the widget's options to be saved. |
| 191 | * |
| 192 | * @since 1.0.0 |
| 193 | * @param array $new_instance The previous instance of values before the update. |
| 194 | * @param array $old_instance The new instance of values to be generated via the update. |
| 195 | * @return array $instance Updated instance. |
| 196 | */ |
| 197 | public function update($new_instance, $old_instance) |
| 198 | { |
| 199 | if ( empty($old_instance) ) { |
| 200 | $old_instance = $this->defaults; |
| 201 | } else { |
| 202 | $old_instance = Helper::merge_array_r( |
| 203 | $this->defaults, |
| 204 | (array) $old_instance |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | $instance = $old_instance; |
| 209 | |
| 210 | return $instance; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Returns data on the current sidebar. |
| 215 | * |
| 216 | * @since 5.0.0 |
| 217 | * @access private |
| 218 | * @return array|null |
| 219 | */ |
| 220 | private function get_sidebar_data() |
| 221 | { |
| 222 | global $wp_registered_sidebars; |
| 223 | $sidebars = wp_get_sidebars_widgets(); |
| 224 | |
| 225 | foreach ( (array) $sidebars as $sidebar_id => $sidebar ) { |
| 226 | if ( in_array($this->id, (array) $sidebar, true ) ) { |
| 227 | return $wp_registered_sidebars[$sidebar_id]; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | return null; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Removes the standard widget from the Legacy Widget block. |
| 236 | * |
| 237 | * @param array |
| 238 | * @return array |
| 239 | */ |
| 240 | public function remove_from_legacy_widget_block(array $widget_types) |
| 241 | { |
| 242 | $widget_types[] = 'wpp'; |
| 243 | return $widget_types; |
| 244 | } |
| 245 | } |
| 246 |