LogListPage::get_per_page()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 11
ccs 0
cts 7
cp 0
crap 12
rs 10
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(
0 ignored issues
show
Documentation Bug introduced by
It seems like add_submenu_page(self::P...($this, 'render_page')) can also be of type false. However, the property $page is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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-list" 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
107
				<?php
108
				// Disable the output of referrer hidden field, since it will be generated by the log list table.
109
				wp_nonce_field( self::LOG_LIST_ACTION_NONCE, self::LOG_LIST_ACTION_NONCE_FIELD, false );
110
				$this->log_list_table->display();
111
				?>
112
			</form>
113
		</div>
114
		<?php
115
		$this->render_page_footer();
116
	}
117
118
	/**
119
	 * Load page.
120
	 */
121
	public function load_page() {
122
		$this->render_help_tab();
123
124
		// Add screen options
125
		$this->get_screen()->add_option(
126
			'per_page',
127
			array(
128
				'label'   => __( 'Entries per page', 'email-log' ),
129
				'default' => 20,
130
				'option'  => 'per_page',
131
			)
132
		);
133
134
		$this->log_list_table = new LogListTable( $this );
135
	}
136
137
	/**
138
	 * Gets the per page option.
139
	 *
140
	 * @return int Number of logs a user wanted to be displayed in a page.
141
	 */
142
	public function get_per_page() {
143
		$screen = get_current_screen();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $screen is correct as get_current_screen() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
144
		$option = $screen->get_option( 'per_page', 'option' );
145
146
		$per_page = get_user_meta( get_current_user_id(), $option, true );
147
148
		if ( empty( $per_page ) || $per_page < 1 ) {
149
			$per_page = $screen->get_option( 'per_page', 'default' );
150
		}
151
152
		return $per_page;
153
	}
154
155
	/**
156
	 * Get nonce args.
157
	 *
158
	 * @return array Nonce args.
159
	 */
160
	public function get_nonce_args() {
161
		return array(
162
			self::LOG_LIST_ACTION_NONCE_FIELD => wp_create_nonce( self::LOG_LIST_ACTION_NONCE ),
163
		);
164
	}
165
166
	/**
167
	 * Get TableManager instance.
168
	 *
169
	 * @return TableManager TableManager instance.
170
	 */
171
	public function get_table_manager() {
172
		$email_log = email_log();
173
174
		return $email_log->table_manager;
175
	}
176
177
	/**
178
	 * Saves Screen options.
179
	 *
180
	 * @since Genesis
181
	 *
182
	 * @param bool|int $status Screen option value. Default false to skip.
183
	 * @param string   $option The option name.
184
	 * @param int      $value  The number of rows to use.
185
	 *
186
	 * @return bool|int
187
	 */
188
	public function save_screen_options( $status, $option, $value ) {
189
		if ( 'per_page' == $option ) {
190
			return $value;
191
		} else {
192
			return $status;
193
		}
194
	}
195
196
	/**
197
	 * Loads assets on the Log List page.
198
	 *
199
	 * @since 2.0.0
200
	 *
201
	 * @param string $hook The current admin page.
202
	 */
203
	public function load_view_logs_assets( $hook ) {
204
		// Don't load assets if not View Logs page.
205
		if ( 'toplevel_page_email-log' !== $hook ) {
206
			return;
207
		}
208
209
		$email_log      = email_log();
210
		$plugin_dir_url = plugin_dir_url( $email_log->get_plugin_file() );
211
212
		wp_register_style( 'jquery-ui-css', $plugin_dir_url . 'assets/vendor/jquery-ui/themes/base/jquery-ui.min.css', array(), '1.12.1' );
213
		wp_enqueue_style( 'el-view-logs-css', $plugin_dir_url . 'assets/css/admin/view-logs.css', array( 'jquery-ui-css' ), $email_log->get_version() );
214
215
		wp_register_script( 'insertionQ', $plugin_dir_url . 'assets/vendor/insertion-query/insQ.min.js', array( 'jquery' ), '1.0.4', true );
216
217
		wp_enqueue_script( 'el-view-logs', $plugin_dir_url . 'assets/js/admin/view-logs.js', array( 'insertionQ', 'jquery-ui-core', 'jquery-ui-datepicker', 'jquery-ui-tooltip', 'jquery-ui-tabs' ), $email_log->get_version(), true );
218
	}
219
}
220