Passed
Pull Request — dev/6.2.0 (#709)
by
unknown
137:18 queued 67:47
created

MultisiteAdminUIBuilder::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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