Passed
Push — master ( e448bf...51995c )
by Paul
07:24 queued 04:56
created

DisablePosts::isAdmin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
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
		add_action( 'init',               [$this, 'disable'] );
29
		add_action( 'wp_dashboard_setup', [$this, 'modifyDashboardWidgets'] );
30
		add_action( 'admin_bar_menu',     [$this, 'removeFromAdminBar'], 999 );
31
		add_action( 'admin_menu',         [$this, 'removeFromAdminMenu'] );
32
		add_action( 'wp_dashboard_setup', [$this, 'unregisterDashboardWidgets'] );
33
		add_action( 'widgets_init',       [$this, 'unregisterWidgets'], 1 );
34
		add_filter( 'posts_results',      [$this, 'filterPostQuery'] );
35
		add_filter( 'pre_get_posts',      [$this, 'filterSearchQuery'] );
36
	}
37
38
	/**
39
	 * @return void
40
	 * @action init
41
	 */
42
	public function disable()
43
	{
44
		if( !in_array(( new Helper )->getCurrentScreen()->pagenow, [
45
			'edit.php', 'edit-tags.php', 'post-new.php',
46
		]))return;
47
48
		if( !filter_input_array( INPUT_GET, [
49
			'post_type' => FILTER_DEFAULT,
50
			'taxonomy' => FILTER_DEFAULT,
51
		])) {
52
			wp_safe_redirect( get_admin_url(), 301 );
53
			exit;
54
		}
55
	}
56
57
	/**
58
	 * http://localhost/?m=2013     - yearly archives
59
	 * http://localhost/?m=201303   - monthly archives
60
	 * http://localhost/?m=20130327 - daily archives
61
	 * http://localhost/?cat=1      - category archives
62
	 * http://localhost/?tag=foobar - tag archives
63
	 * http://localhost/?p=1        - single post
64
	 *
65
	 * @param array $posts
66
	 * @return array
67
	 * @filter posts_results
68
	 */
69
	public function filterPostQuery( $posts = [] )
70
	{
71
		global $wp_query;
72
		return $this->isAdmin() || strpos( $wp_query->request, "wp_posts.post_type = 'post'" ) === false
73
			? $posts
74
			: [];
75
	}
76
77
	/**
78
	 * @return WP_Query
79
	 * @filter pre_get_posts
80
	 */
81
	public function filterSearchQuery( WP_Query $query )
82
	{
83
		if( $this->isAdmin() || !$query->is_main_query() || !is_search() ) {
84
			return $query;
85
		}
86
		$post_types = get_post_types( ['exclude_from_search' => false ] );
87
		unset( $post_types['post'] );
88
		$query->set( 'post_type', array_values( $post_types ) );
89
		return $query;
90
	}
91
92
	/**
93
	 * @return void
94
	 * @action wp_dashboard_setup
95
	 */
96
	public function modifyDashboardWidgets()
97
	{
98
		if( !is_blog_admin() || !current_user_can( 'edit_posts' ))return;
99
100
		global $wp_meta_boxes;
101
		$widgets = &$wp_meta_boxes['dashboard']['normal']['core'];
102
		if( !isset( $widgets['dashboard_right_now']['callback'] ))return;
103
		$widgets['dashboard_right_now']['callback'] = function() {
104
			ob_start();
105
			wp_dashboard_right_now();
106
			echo preg_replace( '/<li class="post-count">(.*?)<\/li>/', '', ob_get_clean() );
107
		};
108
	}
109
110
	/**
111
	 * @return void
112
	 * @action admin_bar_menu
113
	 */
114
	public function removeFromAdminBar()
115
	{
116
		global $wp_admin_bar;
117
		$wp_admin_bar->remove_node( 'new-post' );
118
	}
119
120
	/**
121
	 * @return void
122
	 * @action admin_menu
123
	 */
124
	public function removeFromAdminMenu()
125
	{
126
		remove_menu_page( 'edit.php' );
127
	}
128
129
	/**
130
	 * @return void
131
	 * @action wp_dashboard_setup
132
	 */
133
	public function unregisterDashboardWidgets()
134
	{
135
		remove_meta_box( 'dashboard_activity', 'dashboard', 'normal' );
136
		remove_meta_box( 'dashboard_quick_press', 'dashboard', 'normal' );
137
	}
138
139
	/**
140
	 * @return void
141
	 * @action widgets_init
142
	 */
143
	public function unregisterWidgets()
144
	{
145
		unregister_widget( 'WP_Widget_Archives' );
146
		unregister_widget( 'WP_Widget_Calendar' );
147
		unregister_widget( 'WP_Widget_Recent_Posts' );
148
	}
149
150
	/**
151
	 * @return bool
152
	 */
153
	protected function isAdmin()
154
	{
155
		return is_admin() || ( new Helper )->getCurrentScreen()->pagenow == 'wp-login.php';
156
	}
157
}
158