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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
<?php /** * Handles setting up the library. * * @since 1.0.0 * * @package StellarWP\Telemetry */
namespace StellarWP\Telemetry;
use StellarWP\ContainerContract\ContainerInterface; use StellarWP\Telemetry\Admin\Admin_Subscriber; use StellarWP\Telemetry\Admin\Resources; use StellarWP\Telemetry\Contracts\Data_Provider; use StellarWP\Telemetry\Data_Providers\Debug_Data; use StellarWP\Telemetry\Events\Event_Subscriber; use StellarWP\Telemetry\Exit_Interview\Exit_Interview_Subscriber; use StellarWP\Telemetry\Exit_Interview\Template; use StellarWP\Telemetry\Last_Send\Last_Send; use StellarWP\Telemetry\Last_Send\Last_Send_Subscriber; use StellarWP\Telemetry\Opt_In\Opt_In_Subscriber; use StellarWP\Telemetry\Opt_In\Opt_In_Template; use StellarWP\Telemetry\Opt_In\Status; use StellarWP\Telemetry\Telemetry\Telemetry; use StellarWP\Telemetry\Telemetry\Telemetry_Subscriber;
/** * The core class of the library. * * @since 1.0.0 * * @package StellarWP\Telemetry */ class Core { const PLUGIN_BASENAME = 'plugin.basename'; const PLUGIN_FILE = 'plugin.file'; const SITE_PLUGIN_DIR = 'site.plugin_dir';
/** * The subscriber class names that should be registered in the container. * * @since 1.0.0 * * @var string[] */ private $subscribers = [ Admin_Subscriber::class, Exit_Interview_Subscriber::class, Event_Subscriber::class, Last_Send_Subscriber::class, Opt_In_Subscriber::class, Telemetry_Subscriber::class, ];
/** * The container that should be used for loading library resources. * * @since 1.0.0 * * @var ContainerInterface */ private $container;
/** * The current instance of the library. * * @since 1.0.0 * * @var self */ private static $instance = null;
/** * Returns the current instance or creates one to return. * * @since 1.0.0 * * @return self */ public static function instance() { if ( null === self::$instance ) { self::$instance = new self(); }
return self::$instance; }
/** * Initializes the library. * * @since 1.0.0 * * @param string $plugin_path The path to the main plugin file. * * @throws \RuntimeException Throws exception if container is not set. * * @return void */ public function init( string $plugin_path ) { if ( ! Config::has_container() ) { throw new \RuntimeException( 'You must call StellarWP\Telemetry\Config::set_container() before calling StellarWP\Telemetry::init().' ); }
$this->init_container( $plugin_path ); }
/** * Gets the container. * * @since 1.0.0 * * @return \StellarWP\ContainerContract\ContainerInterface */ public function container() { return $this->container; }
/** * Initializes the container with library resources. * * @since 1.0.0 * * @param string $plugin_path The path of the plugin. * * @return void */ private function init_container( string $plugin_path ) { $container = Config::get_container();
// For all registered stellar slugs, use the plugin basename for those that do not have a wp_slug set. foreach ( Config::get_all_stellar_slugs() as $stellar_slug => $wp_slug ) { if ( '' !== $wp_slug ) { continue; }
Config::add_stellar_slug( $stellar_slug, plugin_basename( $plugin_path ) ); }
$container->bind( self::PLUGIN_BASENAME, plugin_basename( $plugin_path ) ); $container->bind( self::PLUGIN_FILE, $plugin_path ); $container->bind( self::SITE_PLUGIN_DIR, dirname( plugin_dir_path( $plugin_path ) ) ); $container->bind( Data_Provider::class, Debug_Data::class ); $container->bind( Status::class, Status::class ); $container->bind( Last_Send::class, Last_Send::class ); $container->bind( Opt_In_Template::class, static function () use ( $container ) { return new Opt_In_Template( $container->get( Status::class ) ); } ); $container->bind( Template::class, static function () use ( $container ) { return new Template( $container ); } ); $container->bind( Telemetry::class, static function () use ( $container ) { return new Telemetry( $container->get( Data_Provider::class ), $container->get( Status::class ) ); } ); $container->bind( Resources::class, static function () { return new Resources(); } );
// Store the container for later use. $this->container = $container;
foreach ( $this->subscribers as $subscriber_class ) { $this->container->bind( $subscriber_class, new $subscriber_class( $this->container ) ); $this->container->get( $subscriber_class )->register(); } } }
|