PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.1
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / pages / page-mainwp-themes-handler.php

page-mainwp-themes-handler.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.1, at pages/page-mainwp-themes-handler.php

322 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This File handles the Themes SubPage.
4 * MainWP Themes Handler
5 *
6 * @package MainWP/Themes
7 */
8
9 namespace MainWP\Dashboard;
10
11 // Exit if accessed directly.
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * Class MainWP_Themes_Handler
18 *
19 * @uses MainWP_Install_Bulk
20 */
21 class MainWP_Themes_Handler { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
22
23 /**
24 * Get Class Name
25 *
26 * @return string __CLASS__
27 */
28 public static function get_class_name() {
29 return __CLASS__;
30 }
31
32 /**
33 * Method themes_search_handler()
34 *
35 * Theme Search Handler.
36 *
37 * @param mixed $data Search data.
38 * @param object $website The website object.
39 * @param object $output Search results object.
40 *
41 * @return mixed Exception|Theme
42 *
43 * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_error_message()
44 * @uses \MainWP\Dashboard\MainWP_Exception
45 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_child_response()
46 */
47 public static function themes_search_handler( $data, $website, &$output ) { // phpcs:ignore -- NOSONAR - complex.
48 if ( MainWP_Demo_Handle::get_instance()->is_demo_website( $website ) ) {
49 return;
50 }
51 if ( 0 < preg_match( '/<mainwp>(.*)<\/mainwp>/', $data, $results ) ) {
52 $result = $results[1];
53 $result_data = MainWP_System_Utility::get_child_response( base64_decode( $result ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible.
54 unset( $results );
55 if ( isset( $result_data['error'] ) ) {
56 $output->errors[ $website->id ] = MainWP_Error_Helper::get_error_message( new MainWP_Exception( $result_data['error'], $website->url ) );
57 return;
58 }
59
60 $themes = isset( $result_data['data'] ) && is_array( $result_data['data'] ) ? $result_data['data'] : array();
61
62 $not_found = true;
63 foreach ( $themes as $theme ) {
64 if ( ! isset( $theme['name'] ) ) {
65 continue;
66 }
67 $theme['websiteid'] = $website->id;
68 $theme['websiteurl'] = $website->url;
69 $theme['websitename'] = $website->name;
70 $theme['site_info'] = $website->site_info;
71
72 $output->themes[] = $theme;
73 $not_found = false;
74 }
75
76 if ( 'not_installed' === $output->status && $not_found ) {
77 $installed = isset( $result_data['installed_themes'] ) && is_array( $result_data['installed_themes'] ) ? $result_data['installed_themes'] : array();
78 foreach ( $installed as $theme ) {
79 if ( ! isset( $theme['name'] ) ) {
80 continue;
81 }
82 $theme['websiteid'] = $website->id;
83 $theme['websiteurl'] = $website->url;
84 $theme['websitename'] = $website->name;
85 $theme['site_info'] = $website->site_info;
86 $output->themes_installed[] = $theme;
87 }
88 }
89 if ( ! empty( $website->rollback_updates_data ) ) {
90 $output->roll_items[ $website->id ] = MainWP_Updates_Helper::get_roll_update_plugintheme_items( 'theme', $website->rollback_updates_data );
91 }
92 unset( $themes );
93 } else {
94 $output->errors[ $website->id ] = MainWP_Error_Helper::get_error_message( new MainWP_Exception( 'NOMAINWP', $website->url ) );
95 }
96 }
97
98 /**
99 * Activate the selected theme.
100 */
101 public static function activate_theme() {
102 $theme = isset( $_POST['theme'] ) ? sanitize_text_field( wp_unslash( $_POST['theme'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
103 static::action( 'activate', $theme );
104 die( 'SUCCESS' );
105 }
106
107 /**
108 * Delete the selected theme.
109 */
110 public static function delete_themes() {
111 $themes = isset( $_POST['themes'] ) ? wp_unslash( $_POST['themes'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
112 static::action( 'delete', implode( '||', $themes ) );
113 die( 'SUCCESS' );
114 }
115
116 /**
117 * Checks to see if Theme exists, current user can edit settings, check for any errors.
118 *
119 * @param mixed $pAction Action to perform.
120 * @param mixed $theme Theme to perform action on.
121 *
122 * @throws \MainWP_Exception Error message.
123 *
124 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
125 * @uses \MainWP\Dashboard\MainWP_Exception
126 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
127 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
128 */
129 public static function action( $pAction, $theme ) {
130 $websiteId = isset( $_POST['websiteId'] ) ? intval( $_POST['websiteId'] ) : false; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
131
132 if ( empty( $websiteId ) ) {
133 die( 'FAIL' );
134 }
135
136 $website = MainWP_DB::instance()->get_website_by_id( $websiteId );
137 if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
138 die( 'FAIL' );
139 }
140
141 if ( MainWP_System_Utility::is_suspended_site( $website ) ) {
142 die(
143 wp_json_encode(
144 array(
145 'error' => esc_html__( 'Suspended site.', 'mainwp' ),
146 'errorCode' => 'SUSPENDED_SITE',
147 )
148 )
149 );
150 }
151
152 /**
153 * Action: mainwp_before_theme_action
154 *
155 * Fires before theme activate/delete actions.
156 *
157 * @since 4.1
158 */
159 do_action( 'mainwp_before_theme_action', $pAction, $theme, $website );
160
161 try {
162 $information = MainWP_Connect::fetch_url_authed(
163 $website,
164 'theme_action',
165 array(
166 'action' => $pAction,
167 'theme' => $theme,
168 )
169 );
170 } catch ( MainWP_Exception $e ) {
171 die( 'FAIL' );
172 }
173
174 /**
175 * Action: mainwp_after_theme_action
176 *
177 * Fires after theme activate/delete actions.
178 *
179 * @since 4.1
180 */
181 do_action( 'mainwp_after_theme_action', $information, $pAction, $theme, $website );
182
183 if ( isset( $information['error'] ) ) {
184 if ( is_string( $information['error'] ) ) {
185 $information['error'] = esc_html( $information['error'] );
186 }
187 wp_send_json( $information );
188 }
189
190 if ( ! isset( $information['status'] ) || ( 'SUCCESS' !== $information['status'] ) ) {
191 die( 'FAIL' );
192 }
193
194 die( wp_json_encode( array( 'result' => true ) ) );
195 }
196
197 /**
198 * Check to see if Theme is on the Ignore List.
199 *
200 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
201 * @uses \MainWP\Dashboard\MainWP_DB::update_website_values()
202 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
203 * @uses \MainWP\Dashboard\MainWP_Utility::esc_content()
204 */
205 public static function ignore_updates() {
206 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
207 $websiteId = isset( $_POST['websiteId'] ) ? intval( $_POST['websiteId'] ) : false;
208
209 if ( empty( $websiteId ) ) {
210 die( 'FAIL' );
211 }
212
213 $website = MainWP_DB::instance()->get_website_by_id( $websiteId );
214 if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
215 die( 'FAIL' );
216 }
217
218 $themes = isset( $_POST['themes'] ) ? wp_unslash( $_POST['themes'] ) : array();
219 $names = isset( $_POST['names'] ) ? wp_unslash( $_POST['names'] ) : array();
220 // phpcs:enable
221
222 $decodedIgnoredThemes = json_decode( $website->ignored_themes, true );
223 if ( ! is_array( $decodedIgnoredThemes ) ) {
224 $decodedIgnoredThemes = array();
225 }
226
227 if ( is_array( $themes ) ) {
228 $_count = count( $themes );
229 for ( $i = 0; $i < $_count; $i++ ) {
230 $slug = $themes[ $i ];
231 $name = $names[ $i ];
232 if ( ! isset( $decodedIgnoredThemes[ $slug ] ) ) {
233 $decodedIgnoredThemes[ $slug ] = urldecode( $name );
234 }
235 }
236
237 /**
238 * Action: mainwp_before_theme_ignore
239 *
240 * Fires before theme ignore.
241 *
242 * @since 4.1
243 */
244 do_action( 'mainwp_before_theme_ignore', $website, $decodedIgnoredThemes );
245 MainWP_DB::instance()->update_website_values( $website->id, array( 'ignored_themes' => wp_json_encode( $decodedIgnoredThemes ) ) );
246
247 /**
248 * Action: mainwp_after_theme_ignore
249 *
250 * Fires after theme ignore.
251 *
252 * @since 4.1
253 */
254 do_action( 'mainwp_after_theme_ignore', $website, $decodedIgnoredThemes );
255 }
256
257 die( wp_json_encode( array( 'result' => true ) ) );
258 }
259
260 /**
261 * This is the Bulk Method to Trust A Theme.
262 *
263 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension()
264 * @uses \MainWP\Dashboard\MainWP_DB_Common::update_user_extension()
265 */
266 public static function trust_post() { // phpcs:ignore -- NOSONAR - complex.
267 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
268 $trustedThemes = json_decode( $userExtension->trusted_themes, true );
269 if ( ! is_array( $trustedThemes ) ) {
270 $trustedThemes = array();
271 }
272 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
273 $action = isset( $_POST['do'] ) ? sanitize_text_field( wp_unslash( $_POST['do'] ) ) : '';
274 $slugs = isset( $_POST['slugs'] ) && is_array( $_POST['slugs'] ) ? wp_unslash( $_POST['slugs'] ) : false;
275 // phpcs:enable
276 if ( ! is_array( $slugs ) ) {
277 return;
278 }
279 if ( 'trust' !== $action && 'untrust' !== $action ) {
280 return;
281 }
282 if ( 'trust' === $action ) {
283 foreach ( $slugs as $slug ) {
284 $idx = array_search( urldecode( $slug ), $trustedThemes );
285 if ( false === $idx ) {
286 $trustedThemes[] = urldecode( $slug );
287 }
288 }
289 } elseif ( 'untrust' === $action ) {
290 foreach ( $slugs as $slug ) {
291 if ( in_array( urldecode( $slug ), $trustedThemes ) ) {
292 $trustedThemes = array_diff( $trustedThemes, array( urldecode( $slug ) ) );
293 }
294 }
295 }
296 $userExtension->trusted_themes = wp_json_encode( $trustedThemes );
297 MainWP_DB_Common::instance()->update_user_extension( $userExtension );
298 }
299
300 /**
301 * This Method Saves a Trusted theme note.
302 *
303 * @return string Escaped note.
304 */
305 public static function save_trusted_theme_note() {
306 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
307 $slug = isset( $_POST['slug'] ) ? urldecode( sanitize_text_field( wp_unslash( $_POST['slug'] ) ) ) : '';
308 $note = isset( $_POST['note'] ) ? wp_unslash( $_POST['note'] ) : '';
309 // phpcs:enable
310 $esc_note = MainWP_Utility::esc_content( $note );
311 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
312 $trustedThemesNotes = json_decode( $userExtension->trusted_themes_notes, true );
313 if ( ! is_array( $trustedThemesNotes ) ) {
314 $trustedThemesNotes = array();
315 }
316 $trustedThemesNotes[ $slug ] = $esc_note;
317 $userExtension->trusted_themes_notes = wp_json_encode( $trustedThemesNotes );
318 MainWP_DB_Common::instance()->update_user_extension( $userExtension );
319 return $esc_note;
320 }
321 }
322