Passed
Pull Request — dev/6.1.0 (#673)
by Sudar
10:21 queued 04:55
created

MultisiteAdminUIBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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