PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.2.2
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 5.2.2, at pages/page-mainwp-themes-handler.php

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