Completed
Push — 137-feature/refactor-post-modu... ( 580e59 )
by Sudar
07:52
created

MetaboxPage::render_body()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Base;
4
5 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
6
7
/**
8
 * Base class for all Bulk Delete pages that will have metaboxes.
9
 *
10
 * @since 5.7.0
11
 */
12
abstract class MetaboxPage extends BasePage {
13
14
	/**
15
	 * Item Type. Possible values 'posts', 'pages', 'users' etc.
16
	 *
17
	 * @var string
18
	 */
19
	protected $item_type;
20
21
	/**
22
	 * Metaboxes registered to this page.
23
	 *
24
	 * @var \BulkWP\BulkDelete\Core\Base\BaseMetabox[]
25
	 */
26
	protected $metaboxes = array();
27
28
	/**
29
	 * Register the metaboxes after the page is registered.
30
	 */
31
	public function register() {
32
		parent::register();
33
34
		if ( $this->has_metaboxes() ) {
35
			$this->register_metaboxes();
36
		}
37
	}
38
39
	/**
40
	 * Add a metabox to page.
41
	 *
42
	 * @param \BulkWP\BulkDelete\Core\Base\BaseMetabox $metabox Metabox to add.
43
	 */
44
	public function add_metabox( $metabox ) {
45
		if ( in_array( $metabox, $this->metaboxes, true ) ) {
46
			return;
47
		}
48
49
		$this->metaboxes[] = $metabox;
50
	}
51
52
	protected function register_hooks() {
53
		parent::register_hooks();
54
55
		add_action( 'admin_print_scripts-' . $this->hook_suffix, array( $this, 'enqueue_assets' ) );
56
		add_action( "load-{$this->hook_suffix}", array( $this, 'on_load_page' ) );
57
	}
58
59
	/**
60
	 * Enqueue Scripts and Styles.
61
	 */
62
	public function enqueue_assets() {
63
		global $wp_scripts;
64
65
		/**
66
		 * Runs just before enqueuing scripts and styles in all Bulk WP admin pages.
67
		 *
68
		 * This action is primarily for registering or deregistering additional scripts or styles.
69
		 *
70
		 * @since 5.5.1
71
		 */
72
		do_action( 'bd_before_admin_enqueue_scripts' );
73
74
		wp_enqueue_script( 'jquery-ui-timepicker', $this->get_plugin_dir_url() . 'assets/js/jquery-ui-timepicker-addon.min.js', array(
75
			'jquery-ui-slider',
76
			'jquery-ui-datepicker'
77
		), '1.5.4', true );
78
		wp_enqueue_style( 'jquery-ui-timepicker', $this->get_plugin_dir_url() . 'assets/css/jquery-ui-timepicker-addon.min.css', array(), '1.5.4' );
79
80
		wp_enqueue_script( 'select2', $this->get_plugin_dir_url() . 'assets/js/select2.min.js', array( 'jquery' ), '4.0.0', true );
81
		wp_enqueue_style( 'select2', $this->get_plugin_dir_url() . 'assets/css/select2.min.css', array(), '4.0.0' );
82
83
		$postfix = ( defined( 'SCRIPT_DEBUG' ) && true === SCRIPT_DEBUG ) ? '' : '.min';
84
		wp_enqueue_script( 'bulk-delete', $this->get_plugin_dir_url() . 'assets/js/bulk-delete' . $postfix . '.js', array(
85
			'jquery-ui-timepicker',
86
			'jquery-ui-tooltip',
87
			'postbox'
88
		), \Bulk_Delete::VERSION, true );
89
		wp_enqueue_style( 'bulk-delete', $this->get_plugin_dir_url() . 'assets/css/bulk-delete' . $postfix . '.css', array( 'select2' ), \Bulk_Delete::VERSION );
90
91
		$ui  = $wp_scripts->query( 'jquery-ui-core' );
92
		$url = "//ajax.googleapis.com/ajax/libs/jqueryui/{$ui->ver}/themes/smoothness/jquery-ui.css";
93
		wp_enqueue_style( 'jquery-ui-smoothness', $url, false, $ui->ver );
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type array expected by parameter $deps of wp_enqueue_style(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

93
		wp_enqueue_style( 'jquery-ui-smoothness', $url, /** @scrutinizer ignore-type */ false, $ui->ver );
Loading history...
94
95
		/**
96
		 * Filter JavaScript array.
97
		 *
98
		 * This filter can be used to extend the array that is passed to JavaScript
99
		 *
100
		 * @since 5.4
101
		 */
102
		$translation_array = apply_filters( 'bd_javascript_array', array(
103
			'msg'            => array(),
104
			'validators'     => array(),
105
			'dt_iterators'   => array(),
106
			'pre_action_msg' => array(),
107
			'error_msg'      => array(),
108
			'pro_iterators'  => array(),
109
		) );
110
		wp_localize_script( 'bulk-delete', 'BulkWP', $translation_array ); // TODO: Change JavaScript variable to BulkWP.BulkDelete.
111
112
		/**
113
		 * Runs just after enqueuing scripts and styles in all Bulk WP admin pages.
114
		 *
115
		 * This action is primarily for registering additional scripts or styles.
116
		 *
117
		 * @since 5.5.1
118
		 */
119
		do_action( 'bd_after_admin_enqueue_scripts' );
120
	}
121
122
	/**
123
	 * Trigger the add_meta_boxes hooks to allow meta boxes to be added when the page is loaded.
124
	 */
125
	public function on_load_page() {
126
		do_action( 'add_meta_boxes_' . $this->hook_suffix, null );
127
	}
128
129
	/**
130
	 * Add additional nonce fields that are related to metaboxes.
131
	 */
132
	protected function render_nonce_fields() {
133
		parent::render_nonce_fields();
134
135
		// Used to save closed meta boxes and their order.
136
		wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
137
		wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
138
	}
139
140
	/**
141
	 * Render meta boxes in body.
142
	 */
143
	protected function render_body() {
144
		do_meta_boxes( '', 'advanced', null );
145
	}
146
147
	/**
148
	 * Render footer.
149
	 */
150
	protected function render_footer() {
151
		parent::render_footer();
152
153
		/**
154
		 * Runs just before displaying the footer text in the admin page.
155
		 *
156
		 * This action is primarily for adding extra content in the footer of admin page.
157
		 *
158
		 * @since 5.5.4
159
		 */
160
		do_action( "bd_admin_footer_for_{$this->item_type}" );
161
	}
162
163
	/**
164
	 * Does this page has metaboxes?
165
	 *
166
	 * @return bool True if page has metaboxes, False otherwise.
167
	 */
168
	protected function has_metaboxes() {
169
		return ! empty( $this->metaboxes );
170
	}
171
172
	/**
173
	 * Load all the registered metaboxes.
174
	 */
175
	protected function register_metaboxes() {
176
		foreach ( $this->metaboxes as $metabox ) {
177
			$metabox->register( $this->hook_suffix, $this->page_slug );
178
			$this->actions[] = $metabox->get_action();
179
		}
180
	}
181
}
182