DisablePosts::modifyWelcomePanel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
namespace GeminiLabs\Pollux\PostType;
4
5
use GeminiLabs\Pollux\Application;
6
use GeminiLabs\Pollux\Helper;
7
use WP_Query;
8
9
class DisablePosts
10
{
11
	/**
12
	 * @var Application
13
	 */
14
	protected $app;
15
16
	public function __construct( Application $app )
17
	{
18
		$this->app = $app;
19
	}
20
21
	/**
22
	 * @return void
23
	 */
24
	public function init()
25
	{
26
		if( !$this->app->config->disable_posts )return;
27
28
		remove_action( 'welcome_panel', 'wp_welcome_panel' );
29
30
		add_action( 'init',               [$this, 'disable'] );
31
		add_action( 'wp_dashboard_setup', [$this, 'modifyDashboardWidgets'] );
32
		add_action( 'welcome_panel',      [$this, 'modifyWelcomePanel'] );
33
		add_action( 'admin_bar_menu',     [$this, 'removeFromAdminBar'], 999 );
34
		add_action( 'admin_menu',         [$this, 'removeFromAdminMenu'] );
35
		add_action( 'admin_init',         [$this, 'unregisterDashboardWidgets'] );
36
		add_action( 'widgets_init',       [$this, 'unregisterWidgets'], 1 );
37
		add_filter( 'posts_results',      [$this, 'filterPostQuery'] );
38
		add_filter( 'pre_get_posts',      [$this, 'filterSearchQuery'] );
39
	}
40
41
	/**
42
	 * @return void
43
	 * @action init
44
	 */
45
	public function disable()
46
	{
47
		if( !in_array( Helper::getCurrentScreen()->pagenow, [
0 ignored issues
show
Bug introduced by
The property pagenow does not seem to exist on WP_Screen.
Loading history...
48
			'edit.php', 'edit-tags.php', 'post-new.php',
49
		]))return;
50
51
		if( !filter_input_array( INPUT_GET, [
52
			'post_type' => FILTER_DEFAULT,
53
			'taxonomy' => FILTER_DEFAULT,
54
		])) {
55
			wp_safe_redirect( get_admin_url(), 301 );
56
			exit;
57
		}
58
	}
59
60
	/**
61
	 * http://localhost/?m=2013     - yearly archives
62
	 * http://localhost/?m=201303   - monthly archives
63
	 * http://localhost/?m=20130327 - daily archives
64
	 * http://localhost/?cat=1      - category archives
65
	 * http://localhost/?tag=foobar - tag archives
66
	 * http://localhost/?p=1        - single post
67
	 *
68
	 * @param array $posts
69
	 * @return array
70
	 * @filter posts_results
71
	 */
72
	public function filterPostQuery( $posts = [] )
73
	{
74
		global $wp_query;
75
		return $this->isAdmin() || strpos( $wp_query->request, "wp_posts.post_type = 'post'" ) === false
76
			? $posts
77
			: [];
78
	}
79
80
	/**
81
	 * @return WP_Query
82
	 * @filter pre_get_posts
83
	 */
84
	public function filterSearchQuery( WP_Query $query )
85
	{
86
		if( $this->isAdmin() || !$query->is_main_query() || !is_search() ) {
87
			return $query;
88
		}
89
		$post_types = get_post_types( ['exclude_from_search' => false ] );
90
		unset( $post_types['post'] );
91
		$query->set( 'post_type', array_values( $post_types ) );
92
		return $query;
93
	}
94
95
	/**
96
	 * @return void
97
	 * @action wp_dashboard_setup
98
	 */
99
	public function modifyDashboardWidgets()
100
	{
101
		if( !is_blog_admin() || !current_user_can( 'edit_posts' ))return;
102
103
		global $wp_meta_boxes;
104
		$widgets = &$wp_meta_boxes['dashboard']['normal']['core'];
105
		if( !isset( $widgets['dashboard_right_now']['callback'] ))return;
106
		$widgets['dashboard_right_now']['callback'] = function() {
107
			ob_start();
108
			wp_dashboard_right_now();
109
			echo preg_replace( '/<li class="post-count">(.*?)<\/li>/', '', ob_get_clean() );
110
		};
111
	}
112
113
	/**
114
	 * @return void
115
	 * @action welcome_panel
116
	 */
117
	public function modifyWelcomePanel()
118
	{
119
		ob_start();
120
		wp_welcome_panel();
121
		echo preg_replace( '/(<li><a href="(.*?)" class="welcome-icon welcome-write-blog">(.*?)<\/li>)/', '', ob_get_clean() );
122
	}
123
124
	/**
125
	 * @return void
126
	 * @action admin_bar_menu
127
	 */
128
	public function removeFromAdminBar()
129
	{
130
		global $wp_admin_bar;
131
		$wp_admin_bar->remove_node( 'new-post' );
132
	}
133
134
	/**
135
	 * @return void
136
	 * @action admin_menu
137
	 */
138
	public function removeFromAdminMenu()
139
	{
140
		remove_menu_page( 'edit.php' );
141
	}
142
143
	/**
144
	 * @return void
145
	 * @action wp_dashboard_setup
146
	 */
147
	public function unregisterDashboardWidgets()
148
	{
149
		remove_meta_box( 'dashboard_activity', 'dashboard', 'normal' );
150
		remove_meta_box( 'dashboard_quick_press', 'dashboard', 'normal' );
151
	}
152
153
	/**
154
	 * @return void
155
	 * @action widgets_init
156
	 */
157
	public function unregisterWidgets()
158
	{
159
		unregister_widget( 'WP_Widget_Archives' );
160
		unregister_widget( 'WP_Widget_Calendar' );
161
		unregister_widget( 'WP_Widget_Recent_Posts' );
162
	}
163
164
	/**
165
	 * @return bool
166
	 */
167
	protected function isAdmin()
168
	{
169
		return is_admin() || Helper::getCurrentScreen()->pagenow == 'wp-login.php';
0 ignored issues
show
Bug introduced by
The property pagenow does not seem to exist on WP_Screen.
Loading history...
170
	}
171
}
172