Passed
Push — develop ( e6e477...3b1736 )
by Paul
03:05
created

Controller::filterPluginLinks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace GeminiLabs\Pollux;
4
5
use GeminiLabs\Pollux\Application;
6
use GeminiLabs\Pollux\Helper;
7
use GeminiLabs\Pollux\PostType\Archive;
8
use GeminiLabs\Pollux\Settings\Settings;
9
use WP_Screen;
10
11
class Controller
12
{
13
	/**
14
	 * @var string
15
	 */
16
	public $hook;
17
18
	/**
19
	 * @var Application
20
	 */
21
	protected $app;
22
23
	public function __construct( Application $app )
24
	{
25
		$this->app = $app;
26
	}
27
28
	/**
29
	 * @return array
30
	 * @filter plugin_action_links_pollux/pollux.php
31
	 */
32
	public function filterPluginLinks( array $links )
33
	{
34
		$settings_url = admin_url( sprintf( 'options-general.php?page=%s', $this->app->id ));
35
		$links[] = sprintf( '<a href="%s">%s</a>', $settings_url, __( 'Settings', 'pollux' ));
36
		return $links;
37
	}
38
39
	/**
40
	 * @return void
41
	 * @filter admin_footer_text
42
	 */
43
	public function filterWordPressFooter( $text )
44
	{
45
		if( $this->app->config['remove_wordpress_footer'] )return;
46
		return $text;
47
	}
48
49
	/**
50
	 * @return void
51
	 * @action admin_enqueue_scripts
52
	 */
53
	public function registerAssets()
54
	{
55
		$screen = ( new Helper )->getCurrentScreen();
56
57
		$this->registerArchiveAssets( $screen );
58
		$this->registerCodemirrorAssets( $screen );
59
		$this->registerSettingsAssets( $screen );
60
61
		wp_enqueue_style( 'pollux/main.css',
62
			$this->app->url( 'assets/main.css' ),
63
			apply_filters( 'pollux/enqueue/css/deps', [] ),
64
			$this->app->version
65
		);
66
		wp_enqueue_script( 'pollux/main.js',
67
			$this->app->url( 'assets/main.js' ),
68
			apply_filters( 'pollux/enqueue/js/deps', [] ),
69
			$this->app->version
70
		);
71
		wp_localize_script( 'pollux/main.js',
72
			apply_filters( 'pollux/enqueue/js/localize/name', $this->app->id ),
73
			apply_filters( 'pollux/enqueue/js/localize/variables', [] )
74
		);
75
	}
76
77
	/**
78
	 * @return void
79
	 * @action admin_menu
80
	 */
81
	public function registerPage()
82
	{
83
		$this->hook = add_submenu_page(
84
			'options-general.php',
85
			__( 'Pollux', 'pollux' ),
86
			__( 'Pollux', 'pollux' ),
87
			'manage_options',
88
			$this->app->id,
89
			[$this, 'renderPage']
90
		);
91
	}
92
93
	/**
94
	 * @return void
95
	 * @action admin_init
96
	 */
97
	public function removeDashboardWidgets()
98
	{
99
		if( !$this->app->config['remove_dashboard_widgets'] )return;
100
		$widgets = apply_filters( 'pollux/dashoard/widgets', [
101
			'dashboard_quick_press',
102
		]);
103
		foreach( $widgets as $widget ) {
104
			remove_meta_box( $widget, 'dashboard', 'normal' );
105
		}
106
	}
107
108
	/**
109
	 * @return void
110
	 * @action wp_before_admin_bar_render
111
	 */
112
	public function removeWordPressMenu()
113
	{
114
		if( !$this->app->config['remove_wordpress_menu'] )return;
115
		global $wp_admin_bar;
116
		$wp_admin_bar->remove_menu( 'wp-logo' );
117
	}
118
119
	/**
120
	 * @return void
121
	 * @callback add_submenu_page
122
	 */
123
	public function renderPage()
124
	{
125
		$this->app->render( 'index', [
126
			'heading' => __( 'Pollux Settings', 'pollux' ),
127
			'id' => $this->hook,
128
		]);
129
	}
130
131
	/**
132
	 * @return void
133
	 */
134
	protected function registerArchiveAssets( WP_Screen $screen )
135
	{
136
		if(( new Helper )->endsWith( '_archive', $screen->id ) && $screen->pagenow == 'edit.php' ) {
137
			wp_enqueue_script( 'common' );
138
			wp_enqueue_script( 'editor-expand' );
139
			wp_enqueue_script( 'post' );
140
			wp_enqueue_script( 'postbox' );
141
			wp_enqueue_script( 'wp-lists' );
142
			if( wp_is_mobile() ) {
143
				wp_enqueue_script( 'jquery-touch-punch' );
144
			}
145
		}
146
	}
147
148
	/**
149
	 * @return void
150
	 */
151
	protected function registerCodemirrorAssets( WP_Screen $screen )
152
	{
153
		if( $screen->id == 'settings_page_pollux' && $screen->pagenow == 'options-general.php' ) {
154
			wp_enqueue_style( 'pollux/codemirror.css',
155
				$this->app->url( 'assets/codemirror.css' ),
156
				[],
157
				$this->app->version
158
			);
159
			wp_enqueue_script( 'pollux/codemirror.js',
160
				$this->app->url( 'assets/codemirror.js' ),
161
				['pollux/main.js'],
162
				$this->app->version
163
			);
164
		}
165
	}
166
167
	/**
168
	 * @return void
169
	 */
170
	protected function registerSettingsAssets( WP_Screen $screen )
171
	{
172
		if( $screen->id == sprintf( 'toplevel_page_%s', Settings::id() )) {
173
			wp_enqueue_script( 'common' );
174
			wp_enqueue_script( 'postbox' );
175
			wp_enqueue_script( 'wp-lists' );
176
		}
177
	}
178
}
179