Completed
Push — master ( e85e3d...0703ae )
by Sudar
03:41
created

EmailLog   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 355
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 17.34%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 355
ccs 17
cts 98
cp 0.1734
rs 10
wmc 20
lcom 2
cbo 1

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B load() 0 26 2
A add_plugin_links() 0 11 2
A register_settings_page() 0 6 1
B display_logs() 0 45 4
B create_settings_panel() 0 41 1
A display_content_callback() 0 15 2
A save_screen_options() 0 7 2
A get_per_page() 0 12 3
A add_action_links() 0 6 1
A add_footer_links() 0 4 1
1
<?php namespace EmailLog\Core;
2
3
use EmailLog\Core\DB\TableManager;
4
5
/**
6
 * The main plugin class.
7
 *
8
 * @since Genesis
9
 */
10
class EmailLog {
11
12
	/**
13
	 * Version number.
14
	 *
15
	 * @since Genesis
16
	 * @var const VERSION
17
	 */
18
	const VERSION = '1.9.1';
19
20
	/**
21
	 * Flag to track if the plugin is loaded.
22
	 *
23
	 * @since 2.0
24
	 * @var bool
25
	 */
26
	private $loaded;
27
28
	/**
29
	 * @var string Plugin file path.
30
	 *
31
	 * @since 2.0
32
	 */
33
	private $plugin_file;
34
35
	/**
36
	 * Filesystem directory path where translations are stored.
37
	 *
38
	 * @since 2.0
39
	 * @var string $translations_path
40
	 */
41
	public $translations_path;
42
43
	/**
44
	 * @var object TableManager.
45
	 *
46
	 * @since 2.0
47
	 */
48
	public $table_manager;
49
50
	/**
51
	 * @var object EmailLogger
52
	 *
53
	 * @since 2.0
54
	 */
55
	public $logger;
56
57
	/**
58
	 * Admin screen object.
59
	 *
60
	 * @since Genesis
61
	 * @access private
62
	 * @var string $include_path
63
	 */
64
	private $admin_screen;
65
66
	/**
67
	 * Page slug to be used in admin dashboard hyperlinks.
68
	 *
69
	 * @since Genesis
70
	 * @var const PAGE_SLUG
71
	 */
72
	const PAGE_SLUG                = 'email-log';
73
74
	/**
75
	 * String value to generate nonce.
76
	 *
77
	 * @since Genesis
78
	 * @var const DELETE_LOG_NONCE_FIELD
79
	 */
80
	const DELETE_LOG_NONCE_FIELD   = 'sm-delete-email-log-nonce';
81
82
	/**
83
	 * String value to generate nonce.
84
	 *
85
	 * @since Genesis
86
	 * @var const DELETE_LOG_ACTION
87
	 */
88
	const DELETE_LOG_ACTION        = 'sm-delete-email-log';
89
90
	// JS Stuff
91
	const JS_HANDLE                = 'email-log';
92
93
	//hooks
94
	const HOOK_LOG_COLUMNS         = 'email_log_manage_log_columns';
95
	const HOOK_LOG_DISPLAY_COLUMNS = 'email_log_display_log_columns';
96
97
	/**
98
	 * Initialize the plugin.
99
	 */
100 2
	public function __construct( $file ) {
101 2
		$this->plugin_file = $file;
102 2
		$this->translations_path = dirname( plugin_basename( $this->plugin_file ) ) . '/languages/' ;
103 2
	}
104
105
	/**
106
	 * Load the plugin.
107
	 */
108 2
	public function load() {
109 2
		if ( $this->loaded ) {
110
			return;
111
		}
112
113
		// Load localization domain.
114 2
		load_plugin_textdomain( 'email-log', false, $this->translations_path );
115
116
		// Register hooks.
117 2
		add_action( 'admin_menu', array( $this, 'register_settings_page' ) );
118
119
		// Register Filter.
120 2
		add_filter( 'set-screen-option', array( $this, 'save_screen_options' ), 10, 3 );
121 2
		add_filter( 'plugin_row_meta', array( $this, 'add_plugin_links' ), 10, 2 );
122
123 2
		$plugin = plugin_basename( $this->plugin_file );
124 2
		add_filter( "plugin_action_links_$plugin", array( $this, 'add_action_links' ) );
125
126
		// Add our ajax call.
127 2
		add_action( 'wp_ajax_display_content', array( $this, 'display_content_callback' ) );
128
129 2
		$this->table_manager->load();
130 2
		$this->logger->load();
131
132 2
		$this->loaded = true;
133 2
	}
134
135
	/**
136
	 * Adds additional links in the plugin listing page.
137
	 * TODO: Move to UI namespace
138
	 *
139
	 * @since Genesis
140
	 *
141
	 * @see Additional links in the Plugin listing is based on
142
	 * @link http://zourbuth.com/archives/751/creating-additional-wordpress-plugin-links-row-meta/
143
	 *
144
	 * @param array $links Array with default links to display in plugins page.
145
	 * @param string $file The name of the plugin file.
146
	 * @return array Array with links to display in plugins page.
147
	 */
148
	public function add_plugin_links( $links, $file ) {
149
		$plugin = plugin_basename( $this->plugin_file );
150
151
		if ( $file == $plugin ) {
152
			// only for this plugin
153
			return array_merge( $links,
154
				array( '<a href="http://sudarmuthu.com/wordpress/email-log/pro-addons" target="_blank">' . __( 'Buy Addons', 'email-log' ) . '</a>' )
155
			);
156
		}
157
		return $links;
158
	}
159
160
	/**
161
	 * Registers the settings page.
162
	 * TODO: Move to UI namespace
163
	 *
164
	 * @since Genesis
165
	 */
166
	public function register_settings_page() {
167
		// Save the handle to your admin page - you'll need it to create a WP_Screen object
168
		$this->admin_page = add_submenu_page( 'tools.php', __( 'Email Log', 'email-log' ), __( 'Email Log', 'email-log' ), 'manage_options', self::PAGE_SLUG , array( $this, 'display_logs' ) );
0 ignored issues
show
Bug introduced by
The property admin_page does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
169
170
		add_action( "load-{$this->admin_page}", array( $this, 'create_settings_panel' ) );
171
	}
172
173
	/**
174
	 * Displays the stored email log.
175
	 * TODO: Move to UI namespace
176
	 *
177
	 * @since Genesis
178
	 */
179
	public function display_logs() {
180
		add_thickbox();
181
182
		$this->logs_table->prepare_items( $this->get_per_page() );
0 ignored issues
show
Bug introduced by
The property logs_table does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
183
		?>
184
		<div class="wrap">
185
			<h2><?php _e( 'Email Logs', 'email-log' );?></h2>
186
			<?php
187
			if ( isset( $this->logs_deleted ) && $this->logs_deleted != '' ) {
188
				$logs_deleted = intval( $this->logs_deleted );
0 ignored issues
show
Bug introduced by
The property logs_deleted does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
189
190
				if ( $logs_deleted > 0 ) {
191
					echo '<div class="updated"><p>' . sprintf( _n( '1 email log deleted.', '%s email logs deleted', $logs_deleted, 'email-log' ), $logs_deleted ) . '</p></div>';
192
				} else {
193
					echo '<div class="updated"><p>' . __( 'There was some problem in deleting the email logs' , 'email-log' ) . '</p></div>';
194
				}
195
				unset( $this->logs_deleted );
196
			}
197
			?>
198
			<form id="email-logs-search" method="get">
199
				<input type="hidden" name="page" value="<?php echo self::PAGE_SLUG; ?>" >
200
				<?php
201
				$this->logs_table->search_box( __( 'Search Logs', 'email-log' ), 'search_id' );
202
				?>
203
			</form>
204
205
			<form id="email-logs-filter" method="get">
206
				<input type="hidden" name="page" value="<?php echo $_REQUEST['page'] ?>" />
207
				<?php
208
				wp_nonce_field( self::DELETE_LOG_ACTION, self::DELETE_LOG_NONCE_FIELD );
209
				$this->logs_table->display();
210
				?>
211
			</form>
212
		</div>
213
		<?php
214
		/**
215
		 * Action to add additional content to email log admin footer.
216
		 *
217
		 * @since 1.8
218
		 */
219
		do_action( 'el_admin_footer' );
220
221
		// Display credits in Footer
222
		add_action( 'in_admin_footer', array( $this, 'add_footer_links' ) );
223
	}
224
225
	/**
226
	 * Adds settings panel for the plugin.
227
	 * TODO: Move to UI namespace
228
	 *
229
	 * @since Genesis
230
	 */
231
	public function create_settings_panel() {
232
233
		/**
234
		 * Create the WP_Screen object against your admin page handle
235
		 * This ensures we're working with the right admin page
236
		 */
237
		$this->admin_screen = \WP_Screen::get( $this->admin_page );
238
239
		/**
240
		 * Content specified inline
241
		 */
242
		$this->admin_screen->add_help_tab(
243
			array(
244
				'title'    => __( 'About Plugin', 'email-log' ),
245
				'id'       => 'about_tab',
246
				'content'  => '<p>' . __( 'Email Log WordPress Plugin, allows you to log all emails that are sent through WordPress.', 'email-log' ) . '</p>',
247
				'callback' => false,
248
			)
249
		);
250
251
		// Add help sidebar
252
		$this->admin_screen->set_help_sidebar(
253
			'<p><strong>' . __( 'More information', 'email-log' ) . '</strong></p>' .
254
			'<p><a href = "http://sudarmuthu.com/wordpress/email-log">' . __( 'Plugin Homepage/support', 'email-log' ) . '</a></p>' .
255
			'<p><a href = "http://sudarmuthu.com/blog">' . __( "Plugin author's blog", 'email-log' ) . '</a></p>' .
256
			'<p><a href = "http://sudarmuthu.com/wordpress/">' . __( "Other Plugin's by Author", 'email-log' ) . '</a></p>'
257
		);
258
259
		// Add screen options
260
		$this->admin_screen->add_option(
261
			'per_page',
262
			array(
263
				'label' => __( 'Entries per page', 'email-log' ),
264
				'default' => 20,
265
				'option' => 'per_page',
266
			)
267
		);
268
269
		//Prepare Table of elements
270
		$this->logs_table = new UI\LogListTable();
271
	}
272
273
	/**
274
	 * AJAX callback for displaying email content.
275
	 * TODO: Move to UI namespace
276
	 *
277
	 * @since 1.6
278
	 */
279
	public function display_content_callback() {
280
		global $wpdb;
281
282
		if ( current_user_can( 'manage_options' ) ) {
283
			$table_name = $wpdb->prefix . TableManager::LOG_TABLE_NAME;
284
			$email_id   = absint( $_GET['email_id'] );
285
286
			$query   = $wpdb->prepare( 'SELECT * FROM ' . $table_name . ' WHERE id = %d', $email_id );
287
			$content = $wpdb->get_results( $query );
288
289
			echo wpautop( $content[0]->message );
290
		}
291
292
		die(); // this is required to return a proper result
293
	}
294
295
	/**
296
	 * Saves Screen options.
297
	 * TODO: Move to UI namespace
298
	 *
299
	 * @since Genesis
300
	 *
301
	 * @param bool|int $status Screen option value. Default false to skip.
302
	 * @param string   $option The option name.
303
	 * @param int      $value  The number of rows to use.
304
	 * @return bool|int
305
	 */
306
	function save_screen_options( $status, $option, $value ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
307
		if ( 'per_page' == $option ) {
308
			return $value;
309
		} else {
310
			return $status;
311
		}
312
	}
313
314
	/**
315
	 * Gets the per page option.
316
	 * TODO: Move to UI namespace
317
	 *
318
	 * @since Genesis
319
	 *
320
	 * @return int Number of logs a user wanted to be displayed in a page.
321
	 */
322
	public static function get_per_page() {
323
		$screen = get_current_screen();
324
		$option = $screen->get_option( 'per_page', 'option' );
325
326
		$per_page = get_user_meta( get_current_user_id(), $option, true );
327
328
		if ( empty( $per_page ) || $per_page < 1 ) {
329
			$per_page = $screen->get_option( 'per_page', 'default' );
330
		}
331
332
		return $per_page;
333
	}
334
335
	/**
336
	 * Adds additional links.
337
	 * TODO: Move to UI namespace
338
	 *
339
	 * @since Genesis
340
	 *
341
	 * @param array $links
342
	 * @return array
343
	 */
344
	public function add_action_links( $links ) {
345
		// Add a link to this plugin's settings page
346
		$settings_link = '<a href="tools.php?page=email-log">' . __( 'Log', 'email-log' ) . '</a>';
347
		array_unshift( $links, $settings_link );
348
		return $links;
349
	}
350
351
	/**
352
	 * Adds Footer links.
353
	 * TODO: Move to UI namespace
354
	 *
355
	 * @since Genesis
356
	 *
357
	 * @see Function relied on
358
	 * @link http://striderweb.com/nerdaphernalia/2008/06/give-your-wordpress-plugin-credit/
359
	 */
360
	public function add_footer_links() {
361
		$plugin_data = get_plugin_data( $this->plugin_file );
362
		printf( '%1$s ' . __( 'plugin', 'email-log' ) . ' | ' . __( 'Version', 'email-log' ) . ' %2$s | ' . __( 'by', 'email-log' ) . ' %3$s<br />', $plugin_data['Title'], $plugin_data['Version'], $plugin_data['Author'] );
363
	}
364
}
365