class-base.php
1 year ago
class-dashboard-widget.php
1 year ago
class-rest-request.php
1 year ago
index.php
3 years ago
class-dashboard-widget.php
127 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WBCR\Factory_Adverts_159; |
| 4 | |
| 5 | // Exit if accessed directly |
| 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | exit; |
| 8 | } |
| 9 | |
| 10 | /** |
| 11 | * Adverts Dashboard Widget. |
| 12 | * |
| 13 | * Adds a widget with a banner or a list of news. |
| 14 | * |
| 15 | * @author Alexander Vitkalov <nechin.va@gmail.com> |
| 16 | * @author Alexander Kovalev <alex.kovalevv@gmail.com>, Github: https://github.com/alexkovalevv |
| 17 | * |
| 18 | * @since 1.0.0 Added |
| 19 | * @package factory-adverts |
| 20 | * @copyright (c) 2019 Webcraftic Ltd |
| 21 | */ |
| 22 | class Dashboard_Widget { |
| 23 | |
| 24 | /** |
| 25 | * Контент, который должен быть напечатан внутри дашбоард виджета |
| 26 | * |
| 27 | * @author Alexander Kovalev <alex.kovalevv@gmail.com> |
| 28 | * @since 1.0.1 |
| 29 | * @var string |
| 30 | */ |
| 31 | private $content; |
| 32 | |
| 33 | /** |
| 34 | * Экзепляр плагина с которым взаимодействует этот модуль |
| 35 | * |
| 36 | * @author Alexander Kovalev <alex.kovalevv@gmail.com> |
| 37 | * @since 1.0.1 |
| 38 | * @var \Wbcr_Factory480_Plugin |
| 39 | */ |
| 40 | private $plugin; |
| 41 | |
| 42 | /** |
| 43 | * Dashboard_Widget constructor. |
| 44 | * |
| 45 | * Call parent constructor. Registration hooks. |
| 46 | * |
| 47 | * @since 1.0.0 Added |
| 48 | * |
| 49 | * @param \Wbcr_Factory480_Plugin $plugin |
| 50 | * @param string $content |
| 51 | */ |
| 52 | public function __construct( \Wbcr_Factory480_Plugin $plugin, $content ) { |
| 53 | |
| 54 | $this->plugin = $plugin; |
| 55 | $this->content = $content; |
| 56 | |
| 57 | if ( ! empty( $this->content ) ) { |
| 58 | if ( $this->plugin->isNetworkActive() && $this->plugin->isNetworkAdmin() ) { |
| 59 | add_action( 'wp_network_dashboard_setup', [ $this, 'add_dashboard_widgets' ], 999 ); |
| 60 | |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | add_action( 'wp_dashboard_setup', [ $this, 'add_dashboard_widgets' ], 999 ); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Add the News widget to the dashboard. |
| 70 | * |
| 71 | * @since 1.0.0 Added |
| 72 | */ |
| 73 | public function add_dashboard_widgets() { |
| 74 | $widget_id = 'wbcr-factory-adverts-widget'; |
| 75 | |
| 76 | wp_add_dashboard_widget( $widget_id, $this->plugin->getPluginTitle() . ' News', [ |
| 77 | $this, |
| 78 | 'print_widget_content' |
| 79 | ] ); |
| 80 | |
| 81 | $this->sort_dashboard_widgets( $widget_id ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Create the function to output the contents of the Dashboard Widget. |
| 86 | * |
| 87 | * @since 1.0.0 Added |
| 88 | */ |
| 89 | public function print_widget_content() { |
| 90 | ?> |
| 91 | <div class="wordpress-news hide-if-no-js"> |
| 92 | <div class="rss-widget"> |
| 93 | <?php echo $this->content; ?> |
| 94 | </div> |
| 95 | </div> |
| 96 | <?php |
| 97 | |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Сортируем виджеты на странице дашбоард |
| 102 | * |
| 103 | * Виджеты должны быть в таком порядке, чтобы наш виджет был выше все� |
| 104 | . |
| 105 | * |
| 106 | * @author Alexander Kovalev <alex.kovalevv@gmail.com> |
| 107 | * @author Alexander Vitkalov <nechin.va@gmail.com> |
| 108 | * |
| 109 | * @since 1.0.2 Добавлена поддержка мультисайтов |
| 110 | * @since 1.0.0 Добавлен |
| 111 | * |
| 112 | * @param string $widget_id ID нашего виджета |
| 113 | */ |
| 114 | private function sort_dashboard_widgets( $widget_id ) { |
| 115 | global $wp_meta_boxes; |
| 116 | |
| 117 | $location = $this->plugin->isNetworkAdmin() ? 'dashboard-network' : 'dashboard'; |
| 118 | |
| 119 | $normal_core = $wp_meta_boxes[ $location ]['normal']['core']; |
| 120 | $widget_backup = [ $widget_id => $normal_core[ $widget_id ] ]; |
| 121 | unset( $normal_core[ $widget_id ] ); |
| 122 | $sorted_core = array_merge( $widget_backup, $normal_core ); |
| 123 | |
| 124 | $wp_meta_boxes['dashboard']['normal']['core'] = $sorted_core; |
| 125 | } |
| 126 | } |
| 127 |