Completed
Push — dev/2.2.2 ( 4bf759 )
by Sudar
01:48
created

LogListPage::render_page()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 21
nc 2
nop 0
dl 0
loc 29
ccs 0
cts 9
cp 0
crap 6
rs 8.8571
c 0
b 0
f 0
1
<?php namespace EmailLog\Core\UI\Page;
2
3
use EmailLog\Core\DB\TableManager;
4
use EmailLog\Core\UI\ListTable\LogListTable;
5
6
/**
7
 * Log List Page.
8
 *
9
 * @since 2.0
10
 */
11
class LogListPage extends BasePage {
12
	/**
13
	 * @var LogListTable
14
	 */
15
	protected $log_list_table;
16
17
	/**
18
	 * Page slug.
19
	 */
20
	const PAGE_SLUG = 'email-log';
21
22
	/**
23
	 * Nonce Field.
24
	 */
25
	const LOG_LIST_ACTION_NONCE_FIELD = 'el-log-list-nonce-field';
26
27
	/**
28
	 * Nonce name.
29
	 */
30
	const LOG_LIST_ACTION_NONCE = 'el-log-list-nonce';
31
32
	/**
33
	 * Capability to manage email logs.
34
	 *
35
	 * @since 2.1.0
36
	 */
37
	const CAPABILITY = 'manage_email_logs';
38
39
	/**
40
	 * Setup hooks.
41
	 */
42 1
	public function load() {
43 1
		parent::load();
44
45 1
		add_filter( 'set-screen-option', array( $this, 'save_screen_options' ), 10, 3 );
46
47 1
		add_action( 'admin_enqueue_scripts', array( $this, 'load_view_logs_assets' ) );
48 1
	}
49
50
	/**
51
	 * Register page.
52
	 *
53
	 * @inheritdoc
54
	 */
55
	public function register_page() {
56
		add_menu_page(
57
			__( 'Email Log', 'email-log' ),
58
			__( 'Email Log', 'email-log' ),
59
			self::CAPABILITY,
60
			self::PAGE_SLUG,
61
			array( $this, 'render_page' ),
62
			'dashicons-email-alt',
63
			26
64
		);
65
66
		$this->page = add_submenu_page(
67
			self::PAGE_SLUG,
68
			__( 'View Logs', 'email-log'),
69
			__( 'View Logs', 'email-log'),
70
			self::CAPABILITY,
71
			self::PAGE_SLUG,
72
			array( $this, 'render_page' )
73
		);
74
75
		add_action( "load-{$this->page}", array( $this, 'load_page' ) );
76
77
		/**
78
		 * Fires before loading log list page.
79
		 *
80
		 * @since 2.0
81
		 *
82
		 * @param string $page Page slug.
83
		 */
84
		do_action( 'el_load_log_list_page', $this->page );
85
	}
86
87
	/**
88
	 * Render page.
89
	 */
90
	public function render_page() {
91
		if ( ! current_user_can( self::CAPABILITY ) ) {
92
			return;
93
		}
94
95
		add_thickbox();
96
97
		$this->log_list_table->prepare_items();
98
		?>
99
		<div class="wrap">
100
			<h2><?php _e( 'Email Logs', 'email-log' ); ?></h2>
101
			<?php settings_errors(); ?>
102
103
			<form id="email-logs-search" method="get">
104
				<input type="hidden" name="page" value="<?php echo esc_attr( self::PAGE_SLUG ); ?>">
105
				<?php $this->log_list_table->search_box( __( 'Search Logs', 'email-log' ), 'search_id' ); ?>
106
			</form>
107
108
			<form id="email-logs-filter" method="get">
109
				<input type="hidden" name="page" value="<?php echo esc_attr( $_REQUEST['page'] ); ?>"/>
110
				<?php
111
				wp_nonce_field( self::LOG_LIST_ACTION_NONCE, self::LOG_LIST_ACTION_NONCE_FIELD );
112
				$this->log_list_table->display();
113
				?>
114
			</form>
115
		</div>
116
		<?php
117
		$this->render_page_footer();
118
	}
119
120
	/**
121
	 * Load page.
122
	 */
123
	public function load_page() {
124
		$this->render_help_tab();
125
126
		// Add screen options
127
		$this->get_screen()->add_option(
128
			'per_page',
129
			array(
130
				'label'   => __( 'Entries per page', 'email-log' ),
131
				'default' => 20,
132
				'option'  => 'per_page',
133
			)
134
		);
135
136
		$this->log_list_table = new LogListTable( $this );
137
	}
138
139
	/**
140
	 * Gets the per page option.
141
	 *
142
	 * @return int Number of logs a user wanted to be displayed in a page.
143
	 */
144
	public function get_per_page() {
145
		$screen = get_current_screen();
146
		$option = $screen->get_option( 'per_page', 'option' );
147
148
		$per_page = get_user_meta( get_current_user_id(), $option, true );
149
150
		if ( empty( $per_page ) || $per_page < 1 ) {
151
			$per_page = $screen->get_option( 'per_page', 'default' );
152
		}
153
154
		return $per_page;
155
	}
156
157
	/**
158
	 * Get nonce args.
159
	 *
160
	 * @return array Nonce args.
161
	 */
162
	public function get_nonce_args() {
163
		return array(
164
			self::LOG_LIST_ACTION_NONCE_FIELD => wp_create_nonce( self::LOG_LIST_ACTION_NONCE ),
165
		);
166
	}
167
168
	/**
169
	 * Get TableManager instance.
170
	 *
171
	 * @return TableManager TableManager instance.
172
	 */
173
	public function get_table_manager() {
174
		$email_log = email_log();
175
176
		return $email_log->table_manager;
177
	}
178
179
	/**
180
	 * Saves Screen options.
181
	 *
182
	 * @since Genesis
183
	 *
184
	 * @param bool|int $status Screen option value. Default false to skip.
185
	 * @param string   $option The option name.
186
	 * @param int      $value  The number of rows to use.
187
	 *
188
	 * @return bool|int
189
	 */
190
	public function save_screen_options( $status, $option, $value ) {
191
		if ( 'per_page' == $option ) {
192
			return $value;
193
		} else {
194
			return $status;
195
		}
196
	}
197
198
	/**
199
	 * Loads assets on the Log List page.
200
	 *
201
	 * @since 2.0.0
202
	 *
203
	 * @param string $hook The current admin page.
204
	 */
205
	public function load_view_logs_assets( $hook ) {
206
		// Don't load assets if not View Logs page.
207
		if ( 'toplevel_page_email-log' !== $hook ) {
208
			return;
209
		}
210
211
		$email_log      = email_log();
212
		$plugin_dir_url = plugin_dir_url( $email_log->get_plugin_file() );
213
214
		wp_enqueue_style( 'jquery-ui-css', $plugin_dir_url . 'assets/vendor/jquery-ui/themes/base/jquery-ui.min.css', array(), '1.12.1' );
215
		wp_enqueue_style( 'el-view-logs-css', $plugin_dir_url . 'assets/css/admin/view-logs.css', array( 'jquery-ui-css' ), $email_log->get_version() );
216
217
		wp_register_script( 'jquery-ui', $plugin_dir_url . 'assets/vendor/jquery-ui/jquery-ui.min.js', array( 'jquery' ), '1.12.1', true );
218
		wp_register_script( 'insertionQ', $plugin_dir_url . 'assets/vendor/insertionQuery/insQ.min.js', array( 'jquery' ), '1.0.4', true );
219
		wp_enqueue_script( 'jquery-ui-datepicker' );
220
221
		wp_enqueue_script( 'el-view-logs', $plugin_dir_url . 'assets/js/admin/view-logs.js', array( 'jquery-ui', 'jquery-ui-datepicker', 'insertionQ' ), $email_log->get_version(), true );
222
	}
223
}
224