Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 79 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
InstallerListener | |
0.00% |
0 / 79 |
|
0.00% |
0 / 5 |
272 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
assets | |
0.00% |
0 / 46 |
|
0.00% |
0 / 1 |
30 | |||
listen_for_premium_plugin_activation | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
42 | |||
load_text_domain | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
load_script_translation_file | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace NewfoldLabs\WP\Module\Installer\WPAdmin\Listeners; |
4 | |
5 | use NewfoldLabs\WP\Module\Installer\Services\PluginInstaller; |
6 | use NewfoldLabs\WP\Module\PLS\Utilities\PLSUtility; |
7 | |
8 | /** |
9 | * Manages all the installer enqueue related functionalities for the module. |
10 | */ |
11 | class InstallerListener { |
12 | /** |
13 | * Identifier for main script handle (the Modal). |
14 | * |
15 | * @var string |
16 | */ |
17 | public static $installer_handle = 'nfd-installer'; |
18 | |
19 | /** |
20 | * Identifier for the listener script. |
21 | * |
22 | * @var string |
23 | */ |
24 | public static $listener_handle = 'nfd-installer-listener'; |
25 | |
26 | /** |
27 | * Pages/screen->ids the installer script should load. |
28 | * |
29 | * @var array |
30 | */ |
31 | public static $screens = array( |
32 | 'plugin-install', |
33 | ); |
34 | |
35 | /** |
36 | * Constructor for the Installer class. |
37 | */ |
38 | public function __construct() { |
39 | // Hook to enqueue installer scripts |
40 | \add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) ); |
41 | \add_action( 'init', array( __CLASS__, 'load_text_domain' ), 100 ); |
42 | \add_filter( 'load_script_translation_file', array( $this, 'load_script_translation_file' ), 10, 3 ); |
43 | |
44 | // Hook to listen to premium plugin activation |
45 | $this->listen_for_premium_plugin_activation(); |
46 | } |
47 | |
48 | /** |
49 | * Register installer assets |
50 | * |
51 | * @return void |
52 | */ |
53 | public function assets() { |
54 | // Installer listener script |
55 | $listener_asset_file = NFD_INSTALLER_BUILD_DIR . '/dataAttrListener.asset.php'; |
56 | // Installer Modal script |
57 | $asset_file = NFD_INSTALLER_BUILD_DIR . '/installer.asset.php'; |
58 | |
59 | if ( \is_readable( $listener_asset_file ) ) { |
60 | $listener_asset = include $listener_asset_file; |
61 | |
62 | // The listener script is passed as a dependency to the installer script |
63 | \wp_register_script( |
64 | self::$listener_handle, |
65 | NFD_INSTALLER_BUILD_URL . '/dataAttrListener.js', |
66 | array_merge( $listener_asset['dependencies'] ), |
67 | $listener_asset['version'], |
68 | true |
69 | ); |
70 | } |
71 | |
72 | if ( \is_readable( $asset_file ) ) { |
73 | $asset = include $asset_file; |
74 | |
75 | \wp_register_script( |
76 | self::$installer_handle, |
77 | NFD_INSTALLER_BUILD_URL . '/installer.js', |
78 | array_merge( $asset['dependencies'], array( self::$listener_handle ) ), |
79 | $asset['version'], |
80 | true |
81 | ); |
82 | |
83 | \wp_set_script_translations( |
84 | self::$installer_handle, |
85 | 'wp-module-installer', |
86 | NFD_INSTALLER_DIR . '/languages' |
87 | ); |
88 | |
89 | \wp_register_style( |
90 | self::$installer_handle, |
91 | NFD_INSTALLER_BUILD_URL . '/installer.css', |
92 | array(), |
93 | $asset['version'] |
94 | ); |
95 | |
96 | \wp_add_inline_script( |
97 | self::$installer_handle, |
98 | 'var nfdInstaller = ' . \wp_json_encode( |
99 | array( |
100 | 'restUrl' => \get_home_url() . '/index.php?rest_route=', |
101 | 'pluginInstallHash' => PluginInstaller::rest_get_plugin_install_hash(), |
102 | ) |
103 | ) . ';', |
104 | 'before' |
105 | ); |
106 | |
107 | // installer assets may be enqueued as a dependency of any enqueued scripts |
108 | // they are also enqueued on whitelisted screens |
109 | $screen = get_current_screen(); |
110 | if ( isset( $screen->id ) ) { |
111 | if ( in_array( $screen->id, self::$screens, true ) ) { |
112 | \wp_enqueue_script( self::$installer_handle ); |
113 | \wp_enqueue_style( self::$installer_handle ); |
114 | } |
115 | } |
116 | } |
117 | } |
118 | |
119 | /** |
120 | * Listens for premium plugin activation using activated_plugin hook. |
121 | * |
122 | * @return void |
123 | */ |
124 | private function listen_for_premium_plugin_activation() { |
125 | $pls_utility = new PLSUtility(); |
126 | |
127 | // Retrieve the license data (decrypted) from the option |
128 | $license_data_store = $pls_utility->retrieve_license_storage_map(); |
129 | |
130 | if ( ! $license_data_store || empty( $license_data_store ) ) { |
131 | return; |
132 | } |
133 | |
134 | // Hook into activated_plugin action to trigger license activation after plugin activation |
135 | \add_action( |
136 | 'activated_plugin', |
137 | function ( $plugin, $network_wide ) use ( $pls_utility, $license_data_store ) { |
138 | foreach ( $license_data_store as $plugin_slug => $license_data ) { |
139 | if ( isset( $license_data['basename'] ) && $license_data['basename'] === $plugin ) { |
140 | $pls_utility->activate_license( $plugin_slug ); |
141 | break; |
142 | } |
143 | } |
144 | }, |
145 | 10, |
146 | 2 |
147 | ); |
148 | } |
149 | |
150 | /** |
151 | * Load text domain for Module |
152 | * |
153 | * @return void |
154 | */ |
155 | public static function load_text_domain() { |
156 | |
157 | \load_plugin_textdomain( |
158 | 'wp-module-installer', |
159 | false, |
160 | NFD_INSTALLER_DIR . '/languages' |
161 | ); |
162 | } |
163 | |
164 | /** |
165 | * Filters the file path for the JS translation JSON. |
166 | * |
167 | * If the script handle matches the module's handle, builds a custom path using |
168 | * the languages directory, current locale, text domain, and a hash of the script. |
169 | * |
170 | * @param string $file Default translation file path. |
171 | * @param string $handle Script handle. |
172 | * @param string $domain Text domain. |
173 | * @return string Modified file path for the translation JSON. |
174 | */ |
175 | public function load_script_translation_file( $file, $handle, $domain ) { |
176 | |
177 | if ( $handle === self::$installer_handle ) { |
178 | $path = NFD_INSTALLER_DIR . '/languages/'; |
179 | $locale = \determine_locale(); |
180 | |
181 | $file_base = 'default' === $domain |
182 | ? $locale |
183 | : $domain . '-' . $locale; |
184 | $file = $path . $file_base . '-' . md5( 'build/' . NFD_INSTALLER_VERSION . 'installer.js' ) |
185 | . '.json'; |
186 | |
187 | } |
188 | |
189 | return $file; |
190 | } |
191 | } |