DashboardWidget.php
3 weeks ago
ParsiDateArchiveWidget.php
3 weeks ago
ParsiDateCalendarWidget.php
3 weeks ago
Widget.php
3 weeks ago
DashboardWidget.php
416 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Dashboard Widget |
| 4 | * |
| 5 | * Add plugin dashboard widget to WP |
| 6 | */ |
| 7 | |
| 8 | namespace WPParsidate\Widget; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use WPParsidate\Helper\{Assets, Cache, JSON, Param, Templates}; |
| 13 | |
| 14 | class DashboardWidget { |
| 15 | public function __construct() { |
| 16 | add_filter( 'dashboard_primary_link', [ $this, 'changeDashboardLink' ], 999 ); |
| 17 | add_filter( 'dashboard_primary_feed', [ $this, 'changeDashboardFeedUrl' ], 999 ); |
| 18 | add_filter( 'dashboard_secondary_link', [ $this, 'changeDashboardSecondaryLink' ], 999 ); |
| 19 | add_filter( 'dashboard_secondary_feed', [ $this, 'changeDashboardSecondaryFeedUrl' ], 999 ); |
| 20 | add_action( 'wp_dashboard_setup', [ $this, 'addDashboardWidget' ], 1 ); |
| 21 | add_action( 'wp_ajax_wpp-dashboard-widgets', [ $this, 'ajaxDashboardWidgets' ] ); |
| 22 | add_action( 'admin_enqueue_scripts', [ $this, 'enqueueScripts' ] ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Handles dashboard widgets via AJAX. |
| 27 | * |
| 28 | * @since 5.1.0 |
| 29 | */ |
| 30 | public function ajaxDashboardWidgets(): void { |
| 31 | require_once ABSPATH . 'wp-admin/includes/dashboard.php'; |
| 32 | |
| 33 | $pagenow = Param::get( 'pagenow' ); |
| 34 | if ( 'dashboard-user' === $pagenow || 'dashboard-network' === $pagenow || 'dashboard' === $pagenow ) { |
| 35 | set_current_screen( $pagenow ); |
| 36 | } |
| 37 | |
| 38 | switch ( Param::get( 'widget' ) ) { |
| 39 | case 'wpp_dashboard_primary': |
| 40 | $this->printDashboardWidgetPrimary(); |
| 41 | break; |
| 42 | } |
| 43 | wp_die(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Add Parsi-Date events and news widget |
| 48 | * |
| 49 | * @return void |
| 50 | * @author HamidReza Yazdani |
| 51 | * @sicne 5.1.0 |
| 52 | */ |
| 53 | public function addDashboardWidget(): void { |
| 54 | add_meta_box( |
| 55 | 'wpp_dashboard_primary', |
| 56 | esc_html__( 'WP Parsi', 'wp-parsidate' ), |
| 57 | [ $this, 'dashboardWidgetContent' ], |
| 58 | 'dashboard', |
| 59 | 'normal', |
| 60 | 'high' |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Put our content into the new widget |
| 66 | * |
| 67 | * @return void |
| 68 | * @author HamidReza Yazdani |
| 69 | * @sicne 5.1.0 |
| 70 | * |
| 71 | */ |
| 72 | public function dashboardWidgetContent(): void { |
| 73 | ?> |
| 74 | <div id="wpp-sponsorship-guide"> |
| 75 | <div class="question"> |
| 76 | <span class="dashicons dashicons-info-outline"></span> |
| 77 | <span><?php esc_html_e( 'What is this?', 'wp-parsidate' ); ?></span> |
| 78 | </div> |
| 79 | <ul> |
| 80 | <li> |
| 81 | <a href="https://wp-parsidate.ir/donate" target="_blank"> |
| 82 | <span class="dashicons dashicons-external"></span> |
| 83 | <?php esc_html_e( 'Why are you showing me this?', 'wp-parsidate' ); ?> |
| 84 | </a> |
| 85 | </li> |
| 86 | <li> |
| 87 | <a href="https://wp-parsidate.ir/sponser" target="_blank"> |
| 88 | <span class="dashicons dashicons-external"></span> |
| 89 | <?php esc_html_e( 'How can I become a sponsor?', 'wp-parsidate' ); ?> |
| 90 | </a> |
| 91 | </li> |
| 92 | </ul> |
| 93 | </div> |
| 94 | <div id="wpp_sponsorship_placeholder"> |
| 95 | <img src="<?php echo esc_url_raw( WP_PARSI_URL ); ?>assets/images/logo.svg" |
| 96 | alt="<?php esc_html_e( 'Loading Sponsors', 'wp-parsidate' ); ?>"> |
| 97 | </div> |
| 98 | <div id="wpp_sponsorship" class="keen-slider"></div> |
| 99 | <?php |
| 100 | $this->dashboardEventsNews(); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Renders the Events and News dashboard widget. |
| 105 | * |
| 106 | * @since 4.8.0 |
| 107 | */ |
| 108 | public function dashboardEventsNews(): void { |
| 109 | ?> |
| 110 | |
| 111 | <div class="wordpress-news hide-if-no-js"> |
| 112 | <?php $this->printDashboardWidgetPrimary(); ?> |
| 113 | </div> |
| 114 | |
| 115 | <p class="community-events-footer"> |
| 116 | <?php |
| 117 | printf( |
| 118 | '<a href="%1$s" target="_blank">%2$s <span class="screen-reader-text"> %3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a>', |
| 119 | 'https://wp-parsi.com/', |
| 120 | esc_html__( 'WordPress Parsi', 'wp-parsidate' ), |
| 121 | /* translators: Hidden accessibility text. */ |
| 122 | esc_html__( '(opens in a new tab)', 'wp-parsidate' ) |
| 123 | ); |
| 124 | ?> |
| 125 | |
| 126 | | |
| 127 | |
| 128 | <?php |
| 129 | printf( |
| 130 | '<a href="%1$s" target="_blank">%2$s <span class="screen-reader-text"> %3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a>', |
| 131 | 'https://wp-parsi.com/about/', |
| 132 | esc_html__( 'About', 'wp-parsidate' ), |
| 133 | /* translators: Hidden accessibility text. */ |
| 134 | esc_html__( '(opens in a new tab)', 'wp-parsidate' ) |
| 135 | ); |
| 136 | ?> |
| 137 | |
| 138 | | |
| 139 | |
| 140 | <?php |
| 141 | printf( |
| 142 | '<a href="%1$s" target="_blank">%2$s <span class="screen-reader-text"> %3$s</span><span aria-hidden="true" class="dashicons dashicons-external"></span></a>', |
| 143 | /* translators: If a Rosetta site exists (e.g. https://es.wordpress.org/news/), then use that. Otherwise, leave untranslated. */ |
| 144 | esc_url_raw( _x( 'https://wp-parsi.com/sponser/', 'Sponsorship plans', 'wp-parsidate' ) ), |
| 145 | esc_html__( 'Sponsor', 'wp-parsidate' ), |
| 146 | /* translators: Hidden accessibility text. */ |
| 147 | esc_html__( '(opens in a new tab)', 'wp-parsidate' ) |
| 148 | ); |
| 149 | ?> |
| 150 | </p> |
| 151 | <?php |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * 'WordPress Events and News' dashboard widget. |
| 156 | * |
| 157 | * @since 2.7.0 |
| 158 | * @since 4.8.0 Removed popular plugins feed. |
| 159 | */ |
| 160 | public function printDashboardWidgetPrimary(): void { |
| 161 | $feeds = array( |
| 162 | 'news' => array( |
| 163 | |
| 164 | /** |
| 165 | * Filters the primary link URL for the 'WordPress Events and News' dashboard widget. |
| 166 | * |
| 167 | * @param string $link The widget's primary link URL. |
| 168 | * |
| 169 | * @since 2.5.0 |
| 170 | * |
| 171 | */ |
| 172 | 'link' => 'https://wp-parsi.com/', |
| 173 | |
| 174 | /** |
| 175 | * Filters the primary feed URL for the 'WordPress Events and News' dashboard widget. |
| 176 | * |
| 177 | * @param string $url The widget's primary feed URL. |
| 178 | * |
| 179 | * @since 2.3.0 |
| 180 | * |
| 181 | */ |
| 182 | 'url' => 'https://wp-parsi.com/parsidate/feed/', |
| 183 | |
| 184 | /** |
| 185 | * Filters the primary link title for the 'WordPress Events and News' dashboard widget. |
| 186 | * |
| 187 | * @param string $title Title attribute for the widget's primary link. |
| 188 | * |
| 189 | * @since 2.3.0 |
| 190 | * |
| 191 | */ |
| 192 | 'title' => apply_filters( 'dashboard_primary_title', esc_html__( 'ParsiDate', 'wp-parsidate' ) ), |
| 193 | 'items' => 5, |
| 194 | 'show_summary' => 0, |
| 195 | 'show_author' => 0, |
| 196 | 'show_date' => 0, |
| 197 | ), |
| 198 | //'planet' => array( |
| 199 | |
| 200 | /** |
| 201 | * Filters the secondary link URL for the 'WordPress Events and News' dashboard widget. |
| 202 | * |
| 203 | * @param string $link The widget's secondary link URL. |
| 204 | * |
| 205 | * @since 2.3.0 |
| 206 | * |
| 207 | */ |
| 208 | //'link' => esc_html__( 'https://wp-planet.ir/' ), |
| 209 | |
| 210 | /** |
| 211 | * Filters the secondary feed URL for the 'WordPress Events and News' dashboard widget. |
| 212 | * |
| 213 | * @param string $url The widget's secondary feed URL. |
| 214 | * |
| 215 | * @since 2.3.0 |
| 216 | * |
| 217 | */ |
| 218 | //'url' => esc_html__( 'https://wp-planet.ir/feed' ), |
| 219 | |
| 220 | /** |
| 221 | * Filters the secondary link title for the 'WordPress Events and News' dashboard widget. |
| 222 | * |
| 223 | * @param string $title Title attribute for the widget's secondary link. |
| 224 | * |
| 225 | * @since 2.3.0 |
| 226 | * |
| 227 | */ |
| 228 | //'title' => apply_filters( 'dashboard_secondary_title', esc_html__( 'Other WordPress News' ) ), |
| 229 | |
| 230 | /** |
| 231 | * Filters the number of secondary link items for the 'WordPress Events and News' dashboard widget. |
| 232 | * |
| 233 | * @param string $items How many items to show in the secondary feed. |
| 234 | * |
| 235 | * @since 4.4.0 |
| 236 | * |
| 237 | */ |
| 238 | // 'items' => apply_filters( 'dashboard_secondary_items', 3 ), |
| 239 | // 'show_summary' => 0, |
| 240 | // 'show_author' => 0, |
| 241 | // 'show_date' => 0, |
| 242 | //), |
| 243 | ); |
| 244 | |
| 245 | wp_dashboard_cached_rss_widget( 'wpp_dashboard_primary', |
| 246 | [ $this, 'printDashboardWidgetPrimaryOutput' ], $feeds ); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Displays the WordPress events and news feeds. |
| 251 | * |
| 252 | * @param string $widget_id Widget ID. |
| 253 | * @param array $feeds Array of RSS feeds. |
| 254 | * |
| 255 | * @since 3.8.0 |
| 256 | * @since 4.8.0 Removed popular plugins feed. |
| 257 | * |
| 258 | */ |
| 259 | public function printDashboardWidgetPrimaryOutput( $widget_id, $feeds ): void { |
| 260 | foreach ( $feeds as $type => $args ) { |
| 261 | $args['type'] = $type; |
| 262 | echo '<div class="rss-widget">'; |
| 263 | wp_widget_rss_output( $args['url'], $args ); |
| 264 | echo '</div>'; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Enqueue our assets to WP admin dashboard |
| 270 | * |
| 271 | * @param $hook |
| 272 | * |
| 273 | * @return void |
| 274 | * @author HamidReza Yazdani |
| 275 | * @sicne 5.1.0 |
| 276 | * |
| 277 | */ |
| 278 | public function enqueueScripts( $hook ): void { |
| 279 | // Check if we are on the admin dashboard page |
| 280 | if ( $hook !== 'index.php' ) { |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | $pluginVersion = Assets::getVersion(); |
| 285 | $debugName = WP_PARSI_DEBUG_MODE ? '' : '.min'; |
| 286 | |
| 287 | wp_enqueue_style( 'keen-slider', WP_PARSI_URL . "assets/css-admin/keen-slider$debugName.css", false, '6.8.6' ); |
| 288 | wp_enqueue_style( 'wpp_dashboard', WP_PARSI_URL . "assets/css-admin/dashboard$debugName.css", false, |
| 289 | $pluginVersion ); |
| 290 | wp_enqueue_script( 'keen-slider', WP_PARSI_URL . "assets/js-admin/keen-slider.min.js", array(), '6.8.6', true ); |
| 291 | wp_enqueue_script( 'wpp_dashboard', WP_PARSI_URL . "assets/js-admin/dashboard$debugName.js", |
| 292 | array( 'jquery', 'keen-slider' ), WP_PARSI_VER, true ); |
| 293 | |
| 294 | wp_localize_script( 'wpp_dashboard', 'wpp_dashboard', [ 'sponsors' => $this->getMockedSponsors() ] ); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Get mocked sponsors |
| 299 | * |
| 300 | * @return array |
| 301 | * @author Mohammad Zarei |
| 302 | * @sicne 5.1.3 |
| 303 | * |
| 304 | */ |
| 305 | private function getMockedSponsors(): array { |
| 306 | $sponsors = array(); |
| 307 | $allSponsors = $this->getRemoteSponsors(); |
| 308 | if ( empty( $allSponsors ) ) { |
| 309 | $allSponsors = $this->getDataSponsors(); |
| 310 | } |
| 311 | /*$all_sponsors = array( |
| 312 | array( |
| 313 | 'image_url' => WP_PARSI_URL . 'assets/images/sponsors/files-ir.jpg', |
| 314 | 'image_alt' => 'Files.ir', |
| 315 | 'link' => 'https://files.ir/?utm_source=wp-persian&utm_medium=WordPress&utm_campaign=Sponsored-Banner', |
| 316 | 'end_date' => '2026-11-21', |
| 317 | ), |
| 318 | array( |
| 319 | 'image_url' => WP_PARSI_URL . 'assets/images/sponsors/hostiran-1.png', |
| 320 | 'image_alt' => 'HostIran', |
| 321 | 'link' => 'https://hostiran.net/host/?utm_source=parsidate&utm_medium=banner&utm_campaign=awareness', |
| 322 | 'end_date' => '2026-11-21', |
| 323 | ) |
| 324 | );*/ |
| 325 | $today = date( 'Y-m-d' ); |
| 326 | foreach ( $allSponsors as $sponsor ) { |
| 327 | if ( strtotime( $sponsor['end_date'] ) > strtotime( $today ) ) { |
| 328 | $sponsors[] = $sponsor; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | return $sponsors; |
| 333 | } |
| 334 | |
| 335 | private function getRemoteSponsors(): array { |
| 336 | $cache = Cache::get( 'remote_dashboard_sponsors' ); |
| 337 | if ( is_array( $cache ) ) { |
| 338 | return $cache; |
| 339 | } |
| 340 | |
| 341 | $sponsors = []; |
| 342 | $urls = [ |
| 343 | 'https://raw.githubusercontent.com/wordpress-parsi/wp-parsidate/refs/heads/master/inc/Data/sponsors.json' |
| 344 | ]; |
| 345 | |
| 346 | foreach ( $urls as $url ) { |
| 347 | $response = wp_remote_get( $url, [ 'timeout' => 3 ] ); |
| 348 | |
| 349 | if ( ! is_wp_error( $response ) ) { |
| 350 | $data = JSON::decode( wp_remote_retrieve_body( $response ), true ); |
| 351 | $data = is_array( $data ) ? $data : []; |
| 352 | |
| 353 | if ( ! empty( $data ) ) { |
| 354 | $sponsors = $data; |
| 355 | break; |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | Cache::set( 'remote_dashboard_sponsors', $sponsors, DAY_IN_SECONDS ); |
| 361 | |
| 362 | return $sponsors; |
| 363 | } |
| 364 | |
| 365 | private function getDataSponsors(): array { |
| 366 | $path = Templates::pathCorrection( WP_PARSI_DIR . '/inc/Data/sponsors.json' ); |
| 367 | |
| 368 | if ( ! file_exists( $path ) ) { |
| 369 | return []; |
| 370 | } |
| 371 | |
| 372 | $sponsors = JSON::decode( file_get_contents( $path ), true ); |
| 373 | |
| 374 | return is_array( $sponsors ) ? $sponsors : []; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Widget secondary feed |
| 379 | * |
| 380 | * @return string |
| 381 | * @author Morteza Geransayeh |
| 382 | */ |
| 383 | public function changeDashboardSecondaryFeedUrl(): string { |
| 384 | return 'https://wp-planet.ir/feed'; |
| 385 | } |
| 386 | |
| 387 | /** |
| 388 | * Widget secondary link |
| 389 | * |
| 390 | * @return string |
| 391 | * @author Morteza Geransayeh |
| 392 | */ |
| 393 | public function changeDashboardSecondaryLink(): string { |
| 394 | return 'https://wp-planet.ir/'; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Widget primary feed |
| 399 | * |
| 400 | * @return string |
| 401 | * @author Morteza Geransayeh |
| 402 | */ |
| 403 | public function changeDashboardFeedUrl(): string { |
| 404 | return 'https://wp-parsi.com/feed/'; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Change login header url in wp-login.php & Widget primary link |
| 409 | * |
| 410 | * @return string |
| 411 | */ |
| 412 | public function changeDashboardLink(): string { |
| 413 | return 'https://wp-parsi.com'; |
| 414 | } |
| 415 | } |
| 416 |