Passed
Push — analysis-XZJdrn ( 7c4e7b )
by Sudar
69:49 queued 32s
created

FeatureAddon::enqueue_style()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
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 assets that should be loaded for pages.
27
	 *
28
	 * Typically this is used only for built-in pages. Custom pages might load assets themselves.
29
	 *
30
	 * @since 6.0.1
31
	 *
32
	 * @var array
33
	 */
34
	protected $page_assets = array();
35
36
	/**
37
	 * List of modules that are registered by this add-on.
38
	 *
39
	 * This is an associate array, where the key is the item type and value is the array of modules.
40
	 * Eg: $modules['item_type'] = array( $module1, $module2 );
41
	 *
42
	 * @var array
43
	 */
44
	protected $modules = array();
45
46
	/**
47
	 * List of schedulers that are registered by this add-on.
48
	 *
49
	 * @var \BulkWP\BulkDelete\Core\Base\BaseScheduler[]
50
	 */
51
	protected $schedulers = array();
52
53
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
54
	public function register() {
55
		foreach ( $this->pages as $page ) {
56
			$page->for_addon( $this->addon_info );
57
		}
58
59
		if ( ! empty( $this->pages ) ) {
60
			add_filter( 'bd_primary_pages', array( $this, 'register_pages' ) );
61
		}
62
63
		foreach ( array_keys( $this->page_assets ) as $page_slug ) {
64
			add_action( "bd_after_enqueue_page_assets_for_{$page_slug}", array( $this, 'register_page_assets' ) );
65
		}
66
67
		foreach ( array_keys( $this->modules ) as $page_slug ) {
68
			add_action( "bd_after_modules_{$page_slug}", array( $this, 'register_modules_in_page' ) );
69
		}
70
71
		foreach ( $this->schedulers as $scheduler ) {
72
			$scheduler->register();
73
		}
74
	}
75
76
	/**
77
	 * Register pages.
78
	 *
79
	 * @param \BulkWP\BulkDelete\Core\Base\BaseDeletePage[] $primary_pages List of registered Primary pages.
80
	 *
81
	 * @return \BulkWP\BulkDelete\Core\Base\BaseDeletePage[] Modified list of primary pages.
82
	 */
83
	public function register_pages( $primary_pages ) {
84
		foreach ( $this->pages as $page ) {
85
			/**
86
			 * After the modules are registered in the delete posts page.
87
			 *
88
			 * @since 6.0.0
89
			 *
90
			 * @param \BulkWP\BulkDelete\Core\Base\BaseDeletePage $page The page in which the modules are registered.
91
			 */
92
			do_action( "bd_after_modules_{$page->get_page_slug()}", $page );
93
94
			/**
95
			 * After the modules are registered in a delete page.
96
			 *
97
			 * @since 6.0.0
98
			 *
99
			 * @param BasePage $posts_page The page in which the modules are registered.
100
			 */
101
			do_action( 'bd_after_modules', $page );
102
103
			$primary_pages[ $page->get_page_slug() ] = $page;
104
		}
105
106
		return $primary_pages;
107
	}
108
109
	/**
110
	 * Register page assets.
111
	 *
112
	 * @since 6.0.1
113
	 *
114
	 * @param \BulkWP\BulkDelete\Core\Base\BaseDeletePage $page Page.
115
	 */
116
	public function register_page_assets( $page ) {
117
		$assets = $this->page_assets[ $page->get_page_slug() ];
118
119
		foreach ( $assets as $asset ) {
120
			$this->enqueue_asset( $asset );
121
		}
122
	}
123
124
	/**
125
	 * Enqueue page assets.
126
	 *
127
	 * @since 6.0.1
128
	 *
129
	 * @param array $asset Asset details.
130
	 */
131
	protected function enqueue_asset( $asset ) {
132
		if ( 'script' === $asset['type'] ) {
133
			$this->enqueue_script( $asset );
134
		}
135
136
		if ( 'style' === $asset['type'] ) {
137
			$this->enqueue_style( $asset );
138
		}
139
	}
140
141
	/**
142
	 * Enqueue Script.
143
	 *
144
	 * @since 6.0.1
145
	 *
146
	 * @param array $asset Asset details.
147
	 */
148
	protected function enqueue_script( $asset ) {
149
		wp_enqueue_script(
150
			$asset['handle'],
151
			$asset['file'],
152
			$asset['dependencies'],
153
			$this->addon_info->get_version(),
154
			true
155
		);
156
	}
157
158
	/**
159
	 * Enqueue Style.
160
	 *
161
	 * @since 6.0.1
162
	 *
163
	 * @param array $asset Asset details.
164
	 */
165
	protected function enqueue_style( $asset ) {
166
		wp_enqueue_style(
167
			$asset['handle'],
168
			$asset['file'],
169
			$asset['dependencies'],
170
			$this->addon_info->get_version()
171
		);
172
	}
173
174
	/**
175
	 * Register modules for a page.
176
	 *
177
	 * @param \BulkWP\BulkDelete\Core\Base\BaseDeletePage $page Page.
178
	 */
179
	public function register_modules_in_page( $page ) {
180
		$modules = $this->modules[ $page->get_page_slug() ];
181
182
		foreach ( $modules as $module ) {
183
			$page->add_module( $module );
184
		}
185
	}
186
}
187