PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.5.2
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.5.2
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / vendor / team-updraft / lib-onboarding-wizard / composer-plugin-extras / plugin.php

plugin.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.5.2, at vendor/team-updraft/lib-onboarding-wizard/composer-plugin-extras/plugin.php

173 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use Composer\Composer;
4 use Composer\IO\IOInterface;
5 use Composer\Plugin\PluginInterface;
6
7 /**
8 * LibOnboardingWizard Composer Plugin.
9 *
10 * When the lib-onboarding-wizard package is installed, this plugin:
11 * - Replaces all the Updraftplus\Wp_Optimize with the extracted namespace from composer package.
12 * - Deletes itself and composer-plugin-extras folder after execution.
13 */
14 class LibOnboardingWizard implements PluginInterface {
15 private static $namespace_placeholder = 'Updraftplus\Wp_Optimize';
16
17 private static $textdomain_placeholder = 'wp-optimize';
18
19 /**
20 * This function converts the package name from composer.json in to valid php namespace.
21 * @param $package_name string The package name obtained from composer.json
22 * @return string
23 */
24 private static function get_php_namespace($package_name) {
25 $parts = explode('/', $package_name); // Split vendor and package
26 $namespaceParts = array_map(function ($part) {
27 $clean = preg_replace('/[^a-zA-Z0-9_]/', '_', $part); // Remove everything except letters, numbers, and underscores
28 $segments = preg_split('/_+/', $clean); // Split by underscores
29 $segments = array_map('ucfirst', $segments); // Capitalize each segment
30 return implode('_', $segments); // Join back with underscore
31 }, $parts);
32 return implode('\\', $namespaceParts); // Combine with backslash to form final namespace
33 }
34
35 /**
36 * Activates the Composer plugin.
37 *
38 * This method is called by Composer when the plugin is first activated.
39 * It can be used to perform initialization, register event subscribers,
40 * or modify the Composer environment dynamically.
41 *
42 * @param Composer $composer The Composer instance giving access to configuration, repositories, and more.
43 * @param IOInterface $io The IO interface for input/output operations, allowing interaction with the user.
44 *
45 * @return void
46 */
47 public function activate(Composer $composer, IOInterface $io) {
48 $this->remove_tests_directory($composer, $io);
49 $this->replace_namespace($composer, $io);
50 $this->replace_textdomain($composer, $io);
51 }
52 /**
53 * Deactivates the Composer plugin.
54 *
55 * This method is called when the plugin is deactivated during the Composer lifecycle.
56 * Although this implementation does nothing, the method must be defined to fulfill
57 * the interface or abstract class requirements.
58 *
59 * @param Composer $composer The Composer instance.
60 * @param IOInterface $io The IO interface for input/output operations.
61 *
62 * @return void
63 */
64 public function deactivate(Composer $composer, IOInterface $io) {
65 // do nothing, empty body needed to extend the abstract methods.
66 }
67
68 /**
69 * Uninstalls the Composer plugin.
70 *
71 * This method is called when the plugin is removed from the Composer environment.
72 * It provides an opportunity to clean up any configurations or state.
73 * In this implementation, no action is performed.
74 *
75 * @param Composer $composer The Composer instance.
76 * @param IOInterface $io The IO interface for input/output operations.
77 *
78 * @return void
79 */
80 public function uninstall(Composer $composer, IOInterface $io) {
81 // do nothing, empty body needed to extend the abstract methods.
82 }
83
84 /**
85 * This method replaces the php namespace placeholder
86 * present in onboading wizard source with valid php namespace.
87 *
88 * @param Composer $composer The Composer instance.
89 * @param IOInterface $io The IO interface for input/output operations.
90 * @return void
91 */
92 public function replace_namespace(Composer $composer, IOInterface $io): void {
93 $php_namespace = self::get_php_namespace($composer->getPackage()->getName());
94 $file_recursive_iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(dirname(__DIR__)));
95 foreach ($file_recursive_iterator as $file) {
96 if (!$file->isFile() || $file->getExtension() !== 'php') continue;
97 $path = $file->getRealPath();
98 $content = file_get_contents($path);
99 if (strpos($content, self::$namespace_placeholder) !== false) {
100 file_put_contents($path, str_replace(self::$namespace_placeholder, $php_namespace, $content));
101 $io->write(sprintf('Replaced %s namespace placeholder with %s in file: %s', self::$namespace_placeholder, $php_namespace, $path));
102 }
103 }
104 }
105
106 /**
107 * This method replaces the textdomain placeholder
108 * present in onboading wizard source with valid textdomain.
109 *
110 * @param Composer $composer The Composer instance.
111 * @param IOInterface $io The IO interface for input/output operations.
112 * @return void
113 */
114 private function replace_textdomain(Composer $composer, IOInterface $io) {
115 $package_name = $composer->getPackage()->getName();
116 // Use the root package name, for example if its team-updraft/updraftplus
117 // then the text domain would be replaced with `updraftplus`.
118 $parts = explode('/', $package_name);
119 $replacement = $parts[1];
120 $file_recursive_iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(dirname(__DIR__)));
121 foreach ($file_recursive_iterator as $file) {
122 if (!$file->isFile() || !in_array($file->getExtension(), array('php', 'js'))) continue;
123 $path = $file->getRealPath();
124 $content = file_get_contents($path);
125 if (strpos($content, self::$textdomain_placeholder) !== false) {
126 file_put_contents($path, str_replace(self::$textdomain_placeholder, $replacement, $content));
127 $io->write(sprintf('Replaced %s namespace placeholder with %s in file: %s', self::$textdomain_placeholder, $replacement, $path));
128 }
129 }
130 }
131
132 /**
133 * Remove the tests directory from the package.
134 *
135 * @param Composer $composer The Composer instance.
136 * @param IOInterface $io The IO interface for input/output operations.
137 * @return void
138 */
139 private function remove_tests_directory(Composer $composer, IOInterface $io): void {
140 $rootDir = dirname(__DIR__);
141 $testsDir = $rootDir . DIRECTORY_SEPARATOR . 'tests';
142
143 if (is_dir($testsDir)) {
144 $this->delete_directory($testsDir);
145 $io->write(sprintf('Removed tests directory: %s', $testsDir));
146 }
147 }
148
149 /**
150 * Recursively delete a directory and its contents.
151 *
152 * @param string $dir Path to the directory to delete.
153 * @return void
154 */
155 private function delete_directory(string $dir): void {
156 $iterator = new \RecursiveIteratorIterator(
157 new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS),
158 \RecursiveIteratorIterator::CHILD_FIRST
159 );
160
161 foreach ($iterator as $file) {
162 if ($file->isDir()) {
163 rmdir($file->getPathname());
164 } else {
165 unlink($file->getPathname());
166 }
167 }
168 rmdir($dir);
169 }
170
171
172 }
173