PluginProbe ʕ •ᴥ•ʔ
Starter Sites & Templates by Neve / 1.4.1
Starter Sites & Templates by Neve v1.4.1
1.4.1 1.4.0 1.3.0 1.2.29 1.2.28 1.2.6 1.2.7 1.2.8 1.2.9 trunk 1.0.10 1.0.11 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.27 1.1.28 1.1.29 1.1.3 1.1.30 1.1.31 1.1.32 1.1.33 1.1.34 1.1.35 1.1.36 1.1.37 1.1.38 1.1.39 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.3 1.2.4 1.2.5
templates-patterns-collection / includes / Importers / Cleanup / Manager.php
templates-patterns-collection / includes / Importers / Cleanup Last commit date
Active_State.php 1 year ago Manager.php 3 months ago
Manager.php
302 lines
1 <?php
2 /**
3 * The cleanup manager.
4 *
5 * Used to manage all cleanup actions.
6 *
7 * @package templates-patterns-collection
8 */
9 namespace TIOB\Importers\Cleanup;
10
11 use TIOB\Importers\Helpers\Slug_Mapping;
12 use TIOB\Importers\Plugin_Importer;
13
14 /**
15 * Class Manager
16 * @package TIOB\Importers\Cleanup
17 */
18 class Manager {
19
20 /**
21 * Main
22 *
23 * @var Manager
24 */
25 protected static $instance = null;
26
27 /**
28 * Instantiate the class.
29 *
30 * @static
31 * @return Manager
32 * @since 1.0.0
33 * @access public
34 */
35 final public static function instance() {
36 if ( is_null( self::$instance ) ) {
37 self::$instance = new self();
38 self::$instance->init();
39 }
40
41 return self::$instance;
42 }
43
44 /**
45 * Do init actions.
46 */
47 private function init() {}
48
49 /**
50 * Uninstall Plugin method.
51 *
52 * @param string $plugin The plugin file path.
53 * @return bool
54 */
55 private function uninstall_plugin( $plugin ) {
56 require_once( ABSPATH . '/wp-admin/includes/file.php' );
57 global $wp_filesystem;
58 WP_Filesystem();
59 if ( is_plugin_active( $plugin ) ) {
60 deactivate_plugins( $plugin, true );
61 }
62
63 if ( is_uninstallable_plugin( $plugin ) ) {
64 uninstall_plugin( $plugin );
65 }
66 $plugins_dir = $wp_filesystem->wp_plugins_dir();
67 $plugins_dir = trailingslashit( $plugins_dir );
68 $this_plugin_dir = trailingslashit( dirname( $plugins_dir . $plugin ) );
69
70 if ( strpos( $plugin, '/' ) && $this_plugin_dir != $plugins_dir ) {
71 $deleted = $wp_filesystem->delete( $this_plugin_dir, true );
72 } else {
73 $deleted = $wp_filesystem->delete( $plugins_dir . $plugin );
74 }
75 return $deleted;
76 }
77
78 /**
79 * Returns plugin key from slug.
80 *
81 * @param string $plugin_slug The plugin slug.
82 * @param array $plugin_list The plugin list.
83 * @return string
84 */
85 private function get_plugin_key_by_slug( $plugin_slug, $plugin_list ) {
86 foreach ( $plugin_list as $key => $data ) {
87 $parts = explode( '/', $key );
88
89 if ( isset( $parts[0] ) && $parts[0] === $plugin_slug ) {
90 return $key;
91 }
92 }
93 return '';
94 }
95
96 /**
97 * Handles plugin cleanup.
98 * @param array $state The cleanup state.
99 */
100 private function cleanup_plugins( $state ) {
101 if ( isset( $state[ Active_State::PLUGINS_NSP ] ) ) {
102 $plugin_list = get_plugins();
103 foreach ( $state[ Active_State::PLUGINS_NSP ] as $plugin_slug => $info ) {
104 if ( $plugin_slug === Plugin_Importer::OPTIMOLE_SLUG ) {
105 return;
106 }
107
108 $plugin = $this->get_plugin_key_by_slug( $plugin_slug, $plugin_list );
109 if ( empty( $plugin ) ) {
110 continue;
111 }
112 $this->uninstall_plugin( $plugin );
113 if ( $plugin_slug === 'woocommerce' ) {
114 // Remove WooCommerce Pages.
115 wp_delete_post( get_option( 'woocommerce_shop_page_id' ), true );
116 wp_delete_post( get_option( 'woocommerce_cart_page_id' ), true );
117 wp_delete_post( get_option( 'woocommerce_checkout_page_id' ), true );
118 wp_delete_post( get_option( 'woocommerce_myaccount_page_id' ), true );
119 wp_delete_post( get_option( 'woocommerce_edit_address_page_id' ), true );
120 wp_delete_post( get_option( 'woocommerce_view_order_page_id' ), true );
121 wp_delete_post( get_option( 'woocommerce_change_password_page_id' ), true );
122 wp_delete_post( get_option( 'woocommerce_logout_page_id' ), true );
123 }
124 }
125 }
126 }
127
128 /**
129 * Handles category cleanup.
130 * @param array $state The cleanup state.
131 */
132 private function cleanup_category( $state ) {
133 if ( isset( $state[ Active_State::CATEGORY_NSP ] ) ) {
134 foreach ( $state[ Active_State::CATEGORY_NSP ] as $category_id ) {
135 wp_delete_category( $category_id );
136 }
137 }
138 }
139
140 /**
141 * Handles terms cleanup.
142 * @param array $state The cleanup state.
143 */
144 private function cleanup_terms( $state ) {
145 if ( isset( $state[ Active_State::TERMS_NSP ] ) ) {
146 foreach ( $state[ Active_State::TERMS_NSP ] as $term_data ) {
147 wp_delete_term( $term_data['id'], $term_data['taxonomy'] );
148 }
149 }
150 if ( isset( $state[ Active_State::TAGS_NSP ] ) ) {
151 foreach ( $state[ Active_State::TAGS_NSP ] as $id ) {
152 wp_delete_term( $id, 'post_tag' );
153 }
154 }
155 }
156
157 /**
158 * Handles posts cleanup.
159 * @param array $state The cleanup state.
160 */
161 private function cleanup_posts( $state ) {
162 if ( isset( $state[ Active_State::POSTS_NSP ] ) ) {
163 foreach ( $state[ Active_State::POSTS_NSP ] as $post_id ) {
164 wp_delete_post( $post_id, true );
165 }
166 }
167 if ( isset( $state[ Active_State::COMMENTS_NSP ] ) ) {
168 foreach ( $state[ Active_State::COMMENTS_NSP ] as $comment_id ) {
169 wp_delete_comment( $comment_id, true );
170 }
171 }
172 }
173
174 /**
175 * Handles attachments cleanup.
176 * @param array $state The cleanup state.
177 */
178 private function cleanup_attachments( $state ) {
179 if ( isset( $state[ Active_State::ATTACHMENT_NSP ] ) ) {
180 foreach ( $state[ Active_State::ATTACHMENT_NSP ] as $post_id ) {
181 wp_delete_attachment( $post_id, true );
182 }
183 }
184 }
185
186 /**
187 * Handles theme mods cleanup.
188 * @param array $state The cleanup state.
189 */
190 private function cleanup_theme_mods( $state ) {
191 if ( isset( $state[ Active_State::THEME_MODS_NSP ] ) ) {
192 foreach ( $state[ Active_State::THEME_MODS_NSP ] as $theme_mod ) {
193 if ( empty( $theme_mod['value'] ) ) {
194 remove_theme_mod( $theme_mod['mod'] );
195 continue;
196 }
197 set_theme_mod( $theme_mod['mod'], $theme_mod['value'] );
198 }
199 }
200 }
201
202 /**
203 * Handles menu cleanup.
204 * @param array $state The cleanup state.
205 */
206 private function cleanup_menus( $state ) {
207 if ( isset( $state[ Active_State::MENUS_NSP ] ) ) {
208 set_theme_mod( 'nav_menu_locations', $state[ Active_State::MENUS_NSP ] );
209 }
210 }
211
212 /**
213 * Handles widget cleanup.
214 * @param array $state The cleanup state.
215 */
216 private function cleanup_widgets( $state ) {
217 if ( isset( $state[ Active_State::WIDGETS_NSP ] ) ) {
218 foreach ( $state[ Active_State::WIDGETS_NSP ] as $widget ) {
219 if ( empty( $widget['value'] ) ) {
220 delete_option( $widget['id'] );
221 continue;
222 }
223 update_option( $widget['id'], $widget['value'] );
224 }
225 }
226 }
227
228 /**
229 * Handles other options cleanup.
230 * @param array $state The cleanup state.
231 */
232 private function cleanup_options( $namespace, $state ) {
233 if ( isset( $state[ $namespace ] ) ) {
234 foreach ( $state[ $namespace ] as $option => $value ) {
235 if ( empty( $value ) ) {
236 delete_option( $option );
237 continue;
238 }
239 update_option( $option, $value );
240 }
241 }
242 }
243
244 /**
245 * Handles form cleanup.
246 * @param array $state The cleanup state.
247 */
248 private function cleanup_forms( $namespace, $state ) {
249 if ( ! class_exists( 'MM_WPFS_Database' ) ) {
250 return;
251 }
252
253 $db = new \MM_WPFS_Database();
254
255 if ( isset( $state[ $namespace ] ) ) {
256 foreach ( $state[ $namespace ] as $name => $form ) {
257 $get = 'get' . ucfirst( $form['layout'] ) . ucfirst( $form['type'] ) . 'FormByName';
258 $method = 'delete' . ucfirst( $form['layout'] ) . ucfirst( $form['type'] ) . 'Form';
259 $id = null;
260
261 if ( method_exists( 'MM_WPFS_Database', $get ) ) {
262 $item = $db->$get( $name );
263 foreach ( $item as $key => $value ) {
264 if ( strpos( $key, 'FormID' ) !== false ) {
265 $id = $value;
266 break;
267 }
268 }
269 }
270
271 if ( empty( $id ) ) {
272 continue;
273 }
274
275 if ( method_exists( 'MM_WPFS_Database', $method ) ) {
276 $db->$method( $id );
277 }
278 }
279 }
280 }
281
282 final public function do_cleanup() {
283 $active_state = new Active_State();
284 $state = $active_state->get();
285
286 $this->cleanup_theme_mods( $state );
287 $this->cleanup_menus( $state );
288 $this->cleanup_category( $state );
289 $this->cleanup_terms( $state );
290 $this->cleanup_options( Active_State::FRONT_PAGE_NSP, $state );
291 $this->cleanup_options( Active_State::SHOP_PAGE_NSP, $state );
292 $this->cleanup_forms( Active_State::PAYMENT_FORM_NSP, $state );
293 $this->cleanup_posts( $state );
294 $this->cleanup_attachments( $state );
295 $this->cleanup_widgets( $state );
296 $this->cleanup_plugins( $state );
297 Slug_Mapping::clear();
298
299 return delete_transient( Active_State::STATE_NAME );
300 }
301 }
302