Completed
Push — master ( fec9e0...63ebd4 )
by Sudar
01:40
created

load-email-log.php ➔ load_email_log()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 49
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 25
nc 4
nop 1
dl 0
loc 49
rs 9.2258
c 0
b 0
f 0
1
<?php
2
/**
3
 * Load Email Log plugin.
4
 *
5
 * We need this load code in a separate file since it requires namespace
6
 * and using namespace in PHP 5.2 will generate a fatal error.
7
 *
8
 * @since 2.0
9
 */
10
11
/**
12
 * Load Email Log plugin.
13
 *
14
 * @since 2.0
15
 *
16
 * @param string $plugin_file Main plugin file.
17
 */
18
function load_email_log( $plugin_file ) {
19
	global $email_log;
20
21
	$plugin_dir = plugin_dir_path( $plugin_file );
22
23
	// setup autoloader.
24
	require_once 'include/EmailLogAutoloader.php';
25
26
	$loader = new \EmailLog\EmailLogAutoloader();
27
	$loader->add_namespace( 'EmailLog', $plugin_dir . 'include' );
28
29
	if ( file_exists( $plugin_dir . 'tests/' ) ) {
30
		// if tests are present, then add them.
31
		$loader->add_namespace( 'EmailLog', $plugin_dir . 'tests/wp-tests' );
32
	}
33
34
	$loader->add_file( $plugin_dir . 'include/Util/helper.php' );
35
	$loader->add_file( $plugin_dir . 'include/Addon/addon-helper.php' );
36
37
	$loader->register();
38
39
	$email_log = new \EmailLog\Core\EmailLog( $plugin_file, $loader, new \EmailLog\Core\DB\TableManager() );
40
41
	if ( \EmailLog\Util\is_admin_non_ajax_request() ) {
42
		// Loading licenser in frontend or ajax request is resulting in huge performance issues.
43
		$email_log->set_licenser( new \EmailLog\Addon\License\Licenser() );
44
45
		$email_log->add_loadie( new \EmailLog\Addon\DependencyEnforcer() );
46
		$email_log->add_loadie( new \EmailLog\Core\Request\OverridePluginAPI() );
47
	}
48
49
	$email_log->add_loadie( new \EmailLog\Core\EmailLogger() );
50
	$email_log->add_loadie( new \EmailLog\Core\UI\UILoader() );
51
52
	$email_log->add_loadie( new \EmailLog\Core\Request\NonceChecker() );
53
	$email_log->add_loadie( new \EmailLog\Core\Request\LogListAction() );
54
55
	$capability_giver = new \EmailLog\Core\AdminCapabilityGiver();
56
	$email_log->add_loadie( $capability_giver );
57
58
	// `register_activation_hook` can't be called from inside any hook.
59
	register_activation_hook( $plugin_file, array( $email_log->table_manager, 'on_activate' ) );
60
	register_activation_hook( $plugin_file, array( $capability_giver, 'add_cap_to_admin' ) );
61
62
	// Ideally the plugin should be loaded in a later event like `init` or `wp_loaded`.
63
	// But some plugins like EDD are sending emails in `init` event itself,
64
	// which won't be logged if the plugin is loaded in `wp_loaded` or `init`.
65
	add_action( 'plugins_loaded', array( $email_log, 'load' ), 101 );
66
}
67
68
/**
69
 * Return the global instance of Email Log plugin.
70
 * Eventually the EmailLog class might become singleton.
71
 *
72
 * @since 2.0
73
 *
74
 * @global \EmailLog\Core\EmailLog $email_log
75
 * @return \EmailLog\Core\EmailLog
76
 */
77
function email_log() {
78
	global $email_log;
79
80
	return $email_log;
81
}
82