Passed
Push — 189-fix/delete-posts-by-post-s... ( 468a0e...78e3f0 )
by Sudar
53:13 queued 41:17
created

BaseDeletePage::add_module()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 0
cts 5
cp 0
crap 6
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Base;
4
5
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
6
7
/**
8
 * Base class for all Bulk Delete pages that will have modules.
9
 *
10
 * @since 6.0.0
11
 */
12
abstract class BaseDeletePage extends BasePage {
13
	/**
14
	 * Item Type. Possible values 'posts', 'pages', 'users' etc.
15
	 *
16
	 * @var string
17
	 */
18
	protected $item_type;
19
20
	/**
21
	 * Modules registered to this page.
22
	 *
23
	 * @var \BulkWP\BulkDelete\Core\Base\BaseModule[]
24
	 */
25
	protected $modules = array();
26
27
	/**
28
	 * Register the modules after the page is registered.
29
	 */
30
	public function register() {
31
		parent::register();
32
33
		if ( $this->has_modules() ) {
34
			$this->register_modules();
35
		}
36
	}
37
38
	/**
39
	 * Add a module to the page.
40
	 *
41
	 * @param \BulkWP\BulkDelete\Core\Base\BaseModule $module Module to add.
42
	 */
43
	public function add_module( $module ) {
44
		if ( in_array( $module, $this->modules, true ) ) {
45
			return;
46
		}
47
48
		$this->modules[] = $module;
49
	}
50
51
	protected function register_hooks() {
52
		parent::register_hooks();
53
54
		add_action( 'admin_print_scripts-' . $this->hook_suffix, array( $this, 'enqueue_assets' ) );
55
		add_action( "load-{$this->hook_suffix}", array( $this, 'on_load_page' ) );
56
	}
57
58
	/**
59
	 * Enqueue Scripts and Styles.
60
	 */
61
	public function enqueue_assets() {
62
		global $wp_scripts;
63
64
		/**
65
		 * Runs just before enqueuing scripts and styles in all Bulk WP admin pages.
66
		 *
67
		 * This action is primarily for registering or deregistering additional scripts or styles.
68
		 *
69
		 * @since 5.5.1
70
		 */
71
		do_action( 'bd_before_admin_enqueue_scripts' );
72
73
		wp_enqueue_script(
74
			'jquery-ui-timepicker-addon',
75
			$this->get_plugin_dir_url() . 'assets/js/jquery-ui-timepicker-addon.min.js',
76
			array( 'jquery-ui-slider', 'jquery-ui-datepicker' ),
77
			'1.6.3',
78
			true
79
		);
80
		wp_enqueue_style( 'jquery-ui-timepicker', $this->get_plugin_dir_url() . 'assets/css/jquery-ui-timepicker-addon.min.css', array(), '1.6.3' );
81
82
		wp_enqueue_script( 'select2', $this->get_plugin_dir_url() . 'assets/js/select2.min.js', array( 'jquery' ), '4.0.5', true );
83
		wp_enqueue_style( 'select2', $this->get_plugin_dir_url() . 'assets/css/select2.min.css', array(), '4.0.5' );
84
85
		$postfix = ( defined( 'SCRIPT_DEBUG' ) && true === SCRIPT_DEBUG ) ? '' : '.min';
86
		wp_enqueue_script(
87
			'bulk-delete',
88
			$this->get_plugin_dir_url() . 'assets/js/bulk-delete' . $postfix . '.js',
89
			array( 'jquery-ui-timepicker-addon', 'jquery-ui-tooltip', 'postbox' ),
90
			\Bulk_Delete::VERSION,
91
			true
92
		);
93
		wp_enqueue_style( 'bulk-delete', $this->get_plugin_dir_url() . 'assets/css/bulk-delete' . $postfix . '.css', array( 'select2' ), \Bulk_Delete::VERSION );
94
95
		$ui  = $wp_scripts->query( 'jquery-ui-core' );
96
		$url = "//ajax.googleapis.com/ajax/libs/jqueryui/{$ui->ver}/themes/smoothness/jquery-ui.css";
97
		wp_enqueue_style( 'jquery-ui-smoothness', $url, array(), $ui->ver );
98
99
		/**
100
		 * Filter JavaScript array.
101
		 *
102
		 * This filter can be used to extend the array that is passed to JavaScript
103
		 *
104
		 * @since 5.4
105
		 */
106
		$translation_array = apply_filters( 'bd_javascript_array', array(
107
			'msg'            => array(),
108
			'validators'     => array(),
109
			'dt_iterators'   => array(),
110
			'pre_action_msg' => array(),
111
			'error_msg'      => array(),
112
			'pro_iterators'  => array(),
113
		) );
114
		wp_localize_script( 'bulk-delete', 'BulkWP', $translation_array ); // TODO: Change JavaScript variable to BulkWP.BulkDelete.
115
116
		/**
117
		 * Runs just after enqueuing scripts and styles in all Bulk WP admin pages.
118
		 *
119
		 * This action is primarily for registering additional scripts or styles.
120
		 *
121
		 * @since 5.5.1
122
		 */
123
		do_action( 'bd_after_admin_enqueue_scripts' );
124
	}
125
126
	/**
127
	 * Trigger the add_meta_boxes hooks to allow modules to be added when the page is loaded.
128
	 */
129
	public function on_load_page() {
130
		do_action( 'add_meta_boxes_' . $this->hook_suffix, null );
131
	}
132
133
	/**
134
	 * Add additional nonce fields that are related to modules.
135
	 */
136
	protected function render_nonce_fields() {
137
		parent::render_nonce_fields();
138
139
		// Used to save closed meta boxes and their order.
140
		wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
141
		wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
142
	}
143
144
	/**
145
	 * Render meta boxes in body.
146
	 */
147
	protected function render_body() {
148
		do_meta_boxes( '', 'advanced', null );
149
	}
150
151
	/**
152
	 * Render footer.
153
	 */
154
	protected function render_footer() {
155
		parent::render_footer();
156
157
		/**
158
		 * Runs just before displaying the footer text in the admin page.
159
		 *
160
		 * This action is primarily for adding extra content in the footer of admin page.
161
		 *
162
		 * @since 5.5.4
163
		 */
164
		do_action( "bd_admin_footer_for_{$this->item_type}" );
165
	}
166
167
	/**
168
	 * Does this page have any modules?
169
	 *
170
	 * @return bool True if page has modules, False otherwise.
171
	 */
172
	protected function has_modules() {
173
		return ! empty( $this->modules );
174
	}
175
176
	/**
177
	 * Load all the registered modules.
178
	 */
179
	protected function register_modules() {
180
		foreach ( $this->modules as $module ) {
181
			$module->register( $this->hook_suffix, $this->page_slug );
182
			$this->actions[] = $module->get_action();
183
		}
184
	}
185
186
	/**
187
	 * Get the item type of the page.
188
	 *
189
	 * @return string Item type of the page.
190
	 */
191
	public function get_item_type() {
192
		return $this->item_type;
193
	}
194
}
195