|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace cw\wp\admin; |
| 4 | + |
| 5 | +class Dashboard{ |
| 6 | + public function addMainWidget($id, $name, $callback, $priotirty='core'){ |
| 7 | + return $this->addWidget($id, $name, $callback, 'normal', $priotirty); |
| 8 | + } |
| 9 | + |
| 10 | + public function addSideWidget($id, $name, $callback, $priotirty='core'){ |
| 11 | + return $this->addWidget($id, $name, $callback, 'side', $priotirty); |
| 12 | + } |
| 13 | + |
| 14 | + public function addWidget($id, $name, $callback, $where='normal', $priotirty='core'){ |
| 15 | + add_action('wp_dashboard_setup', function() use($id, $name, $callback, $where, $priotirty){ |
| 16 | + add_meta_box($id, $name, $callback, 'dashboard', $where, $priotirty); |
| 17 | + }, 1000); |
| 18 | + return $this; |
| 19 | + } |
| 20 | + |
| 21 | + public function removeWidgets(){ |
| 22 | + add_action('wp_dashboard_setup', function(){ |
| 23 | + global $wp_meta_boxes; |
| 24 | + $wp_meta_boxes['dashboard']['side'] = ['core' => []]; |
| 25 | + $wp_meta_boxes['dashboard']['normal'] = ['core' => []]; |
| 26 | + }, 1000); |
| 27 | + return $this; |
| 28 | + } |
| 29 | + |
| 30 | + public function replaceWelcomePanel($input){ |
| 31 | + add_action( 'admin_footer', function() use($input){ |
| 32 | + if(is_callable($input)) |
| 33 | + $input = $input(); |
| 34 | + |
| 35 | + $input = str_replace(array("\r", "\n"), '', $input); |
| 36 | + $input = addcslashes($input, '"'); |
| 37 | + |
| 38 | + echo '<script>(function($){$("#welcome-panel").html("'.$input.'");})(jQuery);</script>'; |
| 39 | + }, 1000); |
| 40 | + return $this; |
| 41 | + } |
| 42 | + |
| 43 | + public function appendToWelcomePanel($input){ |
| 44 | + add_action( 'admin_footer', function() use($input){ |
| 45 | + if(is_callable($input)) |
| 46 | + $input = $input(); |
| 47 | + |
| 48 | + $input = str_replace(array("\r", "\n"), '', $input); |
| 49 | + $input = addcslashes($input, '"'); |
| 50 | + |
| 51 | + echo '<script>(function($){$("#welcome-panel").append("'.$input.'");})(jQuery);</script>'; |
| 52 | + }, 1000); |
| 53 | + return $this; |
| 54 | + } |
| 55 | + |
| 56 | + public function hideWidgetArea(){ |
| 57 | + add_action( 'admin_footer', function(){ |
| 58 | + echo '<script>(function($){$("#dashboard-widgets-wrap").html("");})(jQuery);</script>'; |
| 59 | + }, 101 ); |
| 60 | + return $this; |
| 61 | + } |
| 62 | +} |
0 commit comments