Passed
Push — master ( e95607...5b55be )
by Sudar
15:04
created

FeatureAddon   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 87
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A register_pages() 0 24 2
A register() 0 15 5
A register_modules_in_page() 0 5 2
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Addon;
4
5
use BulkWP\BulkDelete\Core\Base\BasePage;
6
7
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * A Feature Add-on.
11
 *
12
 * All Feature Add-ons will extend this class.
13
 * A Feature Add-on contains a bunch of modules and may also have Schedulers.
14
 *
15
 * @since 6.0.0
16
 */
17
abstract class FeatureAddon extends BaseAddon {
18
	/**
19
	 * List of pages that are registered by this add-on.
20
	 *
21
	 * @var \BulkWP\BulkDelete\Core\Base\BaseAddonPage[]
22
	 */
23
	protected $pages = array();
24
25
	/**
26
	 * List of modules that are registered by this add-on.
27
	 *
28
	 * This is an associate array, where the key is the item type and value is the array of modules.
29
	 * Eg: $modules['item_type'] = array( $module1, $module2 );
30
	 *
31
	 * @var array
32
	 */
33
	protected $modules = array();
34
35
	/**
36
	 * List of schedulers that are registered by this add-on.
37
	 *
38
	 * @var \BulkWP\BulkDelete\Core\Base\BaseScheduler[]
39
	 */
40
	protected $schedulers = array();
41
42
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
43
	public function register() {
44
		foreach ( $this->pages as $page ) {
45
			$page->for_addon( $this->addon_info );
46
		}
47
48
		if ( ! empty( $this->pages ) ) {
49
			add_filter( 'bd_primary_pages', array( $this, 'register_pages' ) );
50
		}
51
52
		foreach ( array_keys( $this->modules ) as $page_slug ) {
53
			add_action( "bd_after_modules_{$page_slug}", array( $this, 'register_modules_in_page' ) );
54
		}
55
56
		foreach ( $this->schedulers as $scheduler ) {
57
			$scheduler->register();
58
		}
59
	}
60
61
	/**
62
	 * Register pages.
63
	 *
64
	 * @param \BulkWP\BulkDelete\Core\Base\BaseDeletePage[] $primary_pages List of registered Primary pages.
65
	 *
66
	 * @return \BulkWP\BulkDelete\Core\Base\BaseDeletePage[] Modified list of primary pages.
67
	 */
68
	public function register_pages( $primary_pages ) {
69
		foreach ( $this->pages as $page ) {
70
			/**
71
			 * After the modules are registered in the delete posts page.
72
			 *
73
			 * @since 6.0.0
74
			 *
75
			 * @param \BulkWP\BulkDelete\Core\Base\BaseDeletePage $page The page in which the modules are registered.
76
			 */
77
			do_action( "bd_after_modules_{$page->get_page_slug()}", $page );
78
79
			/**
80
			 * After the modules are registered in a delete page.
81
			 *
82
			 * @since 6.0.0
83
			 *
84
			 * @param BasePage $posts_page The page in which the modules are registered.
85
			 */
86
			do_action( 'bd_after_modules', $page );
87
88
			$primary_pages[ $page->get_page_slug() ] = $page;
89
		}
90
91
		return $primary_pages;
92
	}
93
94
	/**
95
	 * Register modules for a page.
96
	 *
97
	 * @param \BulkWP\BulkDelete\Core\Base\BaseDeletePage $page Page.
98
	 */
99
	public function register_modules_in_page( $page ) {
100
		$modules = $this->modules[ $page->get_page_slug() ];
101
102
		foreach ( $modules as $module ) {
103
			$page->add_module( $module );
104
		}
105
	}
106
}
107