Passed
Push — 671-feature/add-mutisite-suppo... ( 7c9bd5...e10db6 )
by Sudar
05:24
created

MultisiteAdminUIBuilder   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
dl 0
loc 196
ccs 0
cts 59
cp 0
rs 10
c 1
b 0
f 0
wmc 16

10 Methods

Rating   Name   Duplication   Size   Complexity  
A get_plugin_file() 0 2 1
A get_network_module() 0 8 2
A load_primary_network_pages() 0 13 1
A on_network_admin_menu() 0 35 3
A __construct() 0 2 1
A get_primary_network_pages() 0 6 2
A get_network_page() 0 8 2
A get_secondary_network_pages() 0 2 1
A get_delete_users_network_admin_page() 0 25 1
A load() 0 6 2
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Multisite;
4
5
use BulkWP\BulkDelete\Core\Base\BasePage;
6
use BulkWP\BulkDelete\Core\Users\DeleteUsersInMultisitePage;
7
use BulkWP\BulkDelete\Core\Users\DeleteUsersPage;
8
use BulkWP\BulkDelete\Core\Users\Modules\DeleteUsersByUserMetaInMultisiteModule;
9
use BulkWP\BulkDelete\Core\Users\Modules\DeleteUsersByUserRoleInMultisiteModule;
10
11
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
12
13
/**
14
 * Builds UI in Multisite.
15
 *
16
 * @since 6.1.0
17
 */
18
class MultisiteAdminUIBuilder {
19
20
	/**
21
	 * Path to the main plugin file.
22
	 *
23
	 * @var string
24
	 */
25
	protected $plugin_file;
26
27
	/**
28
	 * List of Primary Network Admin pages.
29
	 *
30
	 * @var \BulkWP\BulkDelete\Core\Base\BaseDeletePage[]
31
	 */
32
	protected $network_primary_pages = array();
33
34
	/**
35
	 * MultisiteAdminUIBuilder constructor.
36
	 *
37
	 * @param string $plugin_file Path to the main plugin file.
38
	 */
39
	public function __construct( $plugin_file ) {
40
		$this->plugin_file = $plugin_file;
41
	}
42
43
	/**
44
	 * Setup actions and load the UI.
45
	 */
46
	public function load() {
47
		if ( ! is_multisite() ) {
48
			return;
49
		}
50
51
		add_action( 'network_admin_menu', array( $this, 'on_network_admin_menu' ) );
52
	}
53
54
	/**
55
	 * Triggered when the `network_admin_menu` hook is fired.
56
	 *
57
	 * Register all multisite admin pages.
58
	 */
59
	public function on_network_admin_menu() {
60
		foreach ( $this->get_primary_network_pages() as $page ) {
61
			$page->register();
62
		}
63
64
		/**
65
		 * Runs just after adding all *delete* menu items to Bulk WP Network main menu.
66
		 *
67
		 * This action is primarily for adding extra *delete* menu items to the Bulk WP main menu.
68
		 *
69
		 * @since 6.1.0
70
		 */
71
		do_action( 'bd_after_primary_network_menus' );
72
73
		/**
74
		 * Runs just before adding non-action menu items to Bulk WP Network main menu.
75
		 *
76
		 * This action is primarily for adding extra menu items before non-action menu items to the Bulk WP Network main menu.
77
		 *
78
		 * @since 6.1.0
79
		 */
80
		do_action( 'bd_before_secondary_network_menus' );
81
82
		foreach ( $this->get_secondary_network_pages() as $page ) {
83
			$page->register();
84
		}
85
86
		/**
87
		 * Runs just after adding all menu items to Bulk WP Network main menu.
88
		 *
89
		 * This action is primarily for adding extra menu items to the Bulk WP Network main menu.
90
		 *
91
		 * @since 6.1.0
92
		 */
93
		do_action( 'bd_after_all_network_menus' );
94
	}
95
96
	/**
97
	 * Get the list of registered network admin pages.
98
	 *
99
	 * @return \BulkWP\BulkDelete\Core\Base\BaseDeletePage[] List of Primary Admin pages.
100
	 */
101
	protected function get_primary_network_pages() {
102
		if ( empty( $this->network_primary_pages ) ) {
103
			$this->load_primary_network_pages();
104
		}
105
106
		return $this->network_primary_pages;
107
	}
108
109
	/**
110
	 * Load Primary network admin pages.
111
	 *
112
	 * The pages need to be loaded in `init` hook, since the association between page and modules is needed in cron requests.
113
	 */
114
	protected function load_primary_network_pages() {
115
		$users_page = $this->get_delete_users_network_admin_page();
116
117
		$this->network_primary_pages[ $users_page->get_page_slug() ] = $users_page;
118
119
		/**
120
		 * List of primary network admin pages.
121
		 *
122
		 * @since 6.1.0
123
		 *
124
		 * @param \BulkWP\BulkDelete\Core\Base\BaseDeletePage[] List of Admin pages.
125
		 */
126
		$this->network_primary_pages = apply_filters( 'bd_primary_network_pages', $this->network_primary_pages );
127
	}
128
129
	/**
130
	 * Get the Delete Users in Multisite page.
131
	 *
132
	 * @return \BulkWP\BulkDelete\Core\Users\DeleteUsersInMultisitePage
133
	 */
134
	protected function get_delete_users_network_admin_page() {
135
		$users_page = new DeleteUsersInMultisitePage( $this->get_plugin_file() );
136
137
		$users_page->add_module( new DeleteUsersByUserRoleInMultisiteModule() );
138
		$users_page->add_module( new DeleteUsersByUserMetaInMultisiteModule() );
139
140
		/**
141
		 * After the modules are registered in the delete users page.
142
		 *
143
		 * @since 6.1.0
144
		 *
145
		 * @param DeleteUsersPage $users_page The page in which the modules are registered.
146
		 */
147
		do_action( "bd_after_network_modules_{$users_page->get_page_slug()}", $users_page );
148
149
		/**
150
		 * After the modules are registered in a delete page.
151
		 *
152
		 * @since 6.1.0
153
		 *
154
		 * @param BasePage $users_page The page in which the modules are registered.
155
		 */
156
		do_action( 'bd_after_network_modules', $users_page );
157
158
		return $users_page;
159
	}
160
161
	/**
162
	 * Used for getting modules for multisite network admin.
163
	 * Get the module object instance by page slug and module class name.
164
	 *
165
	 * @param string $page_slug         Page Slug.
166
	 * @param string $module_class_name Module class name.
167
	 *
168
	 * @return \BulkWP\BulkDelete\Core\Base\BaseModule|null Module object instance or null if no match found.
169
	 */
170
	public function get_network_module( $page_slug, $module_class_name ) {
171
		$page = $this->get_network_page( $page_slug );
172
173
		if ( is_null( $page ) ) {
174
			return null;
175
		}
176
		error_log( var_export( $page->get_module( $module_class_name ), true ) );
177
		return $page->get_module( $module_class_name );
178
	}
179
180
	/**
181
	 * Used for multisite network admin dashboard.
182
	 * Get the page object instance by page slug.
183
	 *
184
	 * @param string $page_slug Page slug.
185
	 *
186
	 * @return \BulkWP\BulkDelete\Core\Base\BaseDeletePage|null Page object instance or null if no match found.
187
	 */
188
	public function get_network_page( $page_slug ) {
189
		$pages = $this->get_primary_network_pages();
190
191
		if ( ! isset( $pages[ $page_slug ] ) ) {
192
			return null;
193
		}
194
195
		return $pages[ $page_slug ];
196
	}
197
198
	/**
199
	 * Get the secondary list of network pages.
200
	 *
201
	 * @return array
202
	 */
203
	protected function get_secondary_network_pages() {
204
		return array();
205
	}
206
207
	/**
208
	 * Get Plugin file.
209
	 *
210
	 * @return string Plugin File.
211
	 */
212
	protected function get_plugin_file() {
213
		return $this->plugin_file;
214
	}
215
}
216