Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace WPCT_ERP_FORMS\Abstract;
use WPCT_ERP_FORMS\Menu;
use WPCT_ERP_FORMS\Settings;
abstract class Plugin extends Singleton
{
protected $name;
protected $index;
protected $textdomain;
private $menu;
protected $dependencies = [];
abstract public function init();
abstract public static function activate();
abstract public static function deactivate();
public function __construct()
{
if (empty($this->name) || empty($this->textdomain)) {
throw new \Exception('Bad plugin initialization');
}
if (empty($this->index)) {
$this->index = sanitize_title($this->name);
}
$this->index = dirname(__FILE__, 2) . '/' . $this->index;
$this->check_dependencies();
$settings = Settings::get_instance($this->textdomain);
$this->menu = Menu::get_instance($this->name, $settings);
add_action('init', [$this, 'init'], 10);
add_action('init', function () {
$this->load_textdomain();
}, 5);
add_filter('load_textdomain_mofile', function ($mofile, $domain) {
return $this->load_mofile($mofile, $domain);
}, 10, 2);
}
public function get_menu()
{
return $this->menu;
}
public function get_name()
{
return $this->name;
}
public function get_index()
{
return $this->index;
}
public function get_textdomain()
{
return $this->textdomain;
}
public function get_data()
{
return apply_filters('wpct_dc_plugin_data', null, $this->index);
}
private function check_dependencies()
{
add_filter('wpct_dc_dependencies', function ($dependencies) {
foreach ($this->dependencies as $label => $url) {
$dependencies[$label] = $url;
}
return $dependencies;
});
}
private function load_textdomain()
{
$data = $this->get_data();
$domain_path = isset($data['DomainPath']) && !empty($data['DomainPath']) ? $data['DomainPath'] : '/languages';
load_plugin_textdomain(
$this->textdomain,
false,
dirname($this->index) . $domain_path,
);
}
private function load_mofile($mofile, $domain)
{
$data = $this->get_data();
$domain_path = isset($data['DomainPath']) && !empty($data['DomainPath']) ? $data['DomainPath'] : '/languages';
if ($domain === $this->textdomain && strpos($mofile, WP_LANG_DIR . '/plugins/') === false) {
$locale = apply_filters('plugin_locale', determine_locale(), $domain);
$mofile = dirname($this->index) . $domain_path . '/' . $domain . '-' . $locale . '.mo';
}
return $mofile;
}
}