| @@ -11,352 +11,330 @@ | ||
| 11 | 11 | * Class MainWP MainWP_Plugins_Handler |
| 12 | 12 | * |
| 13 | 13 | * @uses MainWP_Install_Bulk() |
| 14 | 14 | */ |
| 15 | -class MainWP_Plugins_Handler { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR. | |
| 16 | - // phpcs:disable Generic.Metrics.CyclomaticComplexity -- This is the only way to achieve desired results, pull request solutions appreciated. | |
| 15 | +class MainWP_Plugins_Handler { | |
| 16 | + // phpcs:disable Generic.Metrics.CyclomaticComplexity -- This is the only way to achieve desired results, pull request solutions appreciated. | |
| 17 | 17 | |
| 18 | - /** | |
| 19 | - * Get Class Name. | |
| 20 | - * | |
| 21 | - * @return string __CLASS__ | |
| 22 | - */ | |
| 23 | - public static function get_class_name() { | |
| 24 | - return __CLASS__; | |
| 25 | - } | |
| 18 | + /** | |
| 19 | + * Get Class Name. | |
| 20 | + * | |
| 21 | + * @return string __CLASS__ | |
| 22 | + */ | |
| 23 | + public static function get_class_name() { | |
| 24 | + return __CLASS__; | |
| 25 | + } | |
| 26 | 26 | |
| 27 | - /** | |
| 28 | - * Plugins Search Handler. | |
| 29 | - * | |
| 30 | - * @param mixed $data Search Data. | |
| 31 | - * @param object $website Child Sites. | |
| 32 | - * @param mixed $output Output. | |
| 33 | - * | |
| 34 | - * @return mixed error|array. | |
| 35 | - * | |
| 36 | - * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_error_message() | |
| 37 | - * @uses \MainWP\Dashboard\MainWP_System_Utility::get_child_response() | |
| 38 | - * @uses \MainWP\Dashboard\MainWP_Exception | |
| 39 | - * @uses \MainWP\Dashboard\MainWP_System_Utility::get_child_response() | |
| 40 | - */ | |
| 41 | - public static function plugins_search_handler( $data, $website, &$output ) { // phpcs:ignore -- NOSONAR - complex. | |
| 42 | - if ( MainWP_Demo_Handle::get_instance()->is_demo_website( $website ) ) { | |
| 43 | - return; | |
| 44 | - } | |
| 45 | - if ( 0 < preg_match( '/<mainwp>(.*)<\/mainwp>/', $data, $results ) ) { | |
| 46 | - $result = $results[1]; | |
| 47 | - $result_data = MainWP_System_Utility::get_child_response( base64_decode( $result ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible. | |
| 48 | - unset( $results ); | |
| 49 | - if ( isset( $result_data['error'] ) ) { | |
| 50 | - $output->errors[ $website->id ] = MainWP_Error_Helper::get_error_message( new MainWP_Exception( $result_data['error'], $website->url ) ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 51 | - return; | |
| 52 | - } | |
| 27 | + /** | |
| 28 | + * Plugins Search Handler. | |
| 29 | + * | |
| 30 | + * @param mixed $data Search Data. | |
| 31 | + * @param object $website Child Sites. | |
| 32 | + * @param mixed $output Output. | |
| 33 | + * | |
| 34 | + * @return mixed error|array. | |
| 35 | + * | |
| 36 | + * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_error_message() | |
| 37 | + * @uses \MainWP\Dashboard\MainWP_System_Utility::get_child_response() | |
| 38 | + * @uses \MainWP\Dashboard\MainWP_Exception | |
| 39 | + * @uses \MainWP\Dashboard\MainWP_System_Utility::get_child_response() | |
| 40 | + */ | |
| 41 | + public static function plugins_search_handler( $data, $website, &$output ) { | |
| 42 | + if ( 0 < preg_match( '/<mainwp>(.*)<\/mainwp>/', $data, $results ) ) { | |
| 43 | + $result = $results[1]; | |
| 44 | + $plugins = MainWP_System_Utility::get_child_response( base64_decode( $result ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- base64_encode used for http encoding compatible. | |
| 45 | + unset( $results ); | |
| 46 | + if ( isset( $plugins['error'] ) ) { | |
| 47 | + $output->errors[ $website->id ] = MainWP_Error_Helper::get_error_message( new MainWP_Exception( $plugins['error'], $website->url ) ); | |
| 48 | + return; | |
| 49 | + } | |
| 53 | 50 | |
| 54 | - $plugins = isset( $result_data['data'] ) && is_array( $result_data['data'] ) ? $result_data['data'] : array(); | |
| 51 | + if ( isset( $plugins['not_criteria_plugins'] ) && is_array( $plugins['not_criteria_plugins'] ) ) { | |
| 52 | + foreach ( $plugins['not_criteria_plugins'] as $plugin ) { | |
| 53 | + if ( ! isset( $plugin['name'] ) ) { | |
| 54 | + continue; | |
| 55 | + } | |
| 56 | + $plugin['websiteid'] = $website->id; | |
| 57 | + $plugin['websiteurl'] = $website->url; | |
| 58 | + $plugin['websitename'] = $website->name; | |
| 59 | + $output->not_criteria_plugins[] = $plugin; | |
| 60 | + } | |
| 61 | + } else { | |
| 62 | + foreach ( $plugins as $plugin ) { | |
| 63 | + if ( ! isset( $plugin['name'] ) ) { | |
| 64 | + continue; | |
| 65 | + } | |
| 66 | + $plugin['websiteid'] = $website->id; | |
| 67 | + $plugin['websiteurl'] = $website->url; | |
| 68 | + $plugin['websitename'] = $website->name; | |
| 55 | 69 | |
| 56 | - $not_found = true; | |
| 70 | + $output->plugins[] = $plugin; | |
| 71 | + } | |
| 72 | + } | |
| 73 | + unset( $plugins ); | |
| 74 | + } else { | |
| 75 | + $output->errors[ $website->id ] = MainWP_Error_Helper::get_error_message( new MainWP_Exception( 'NOMAINWP', $website->url ) ); | |
| 76 | + } | |
| 77 | + } | |
| 57 | 78 | |
| 58 | - foreach ( $plugins as $plugin ) { | |
| 59 | - if ( ! isset( $plugin['name'] ) ) { | |
| 60 | - continue; | |
| 61 | - } | |
| 62 | - $plugin['websiteid'] = $website->id; | |
| 63 | - $plugin['websiteurl'] = $website->url; | |
| 64 | - $plugin['websitename'] = $website->name; | |
| 79 | + /** Activate Plugin. */ | |
| 80 | + public static function activate_plugins() { | |
| 81 | + self::action( 'activate' ); | |
| 82 | + } | |
| 65 | 83 | |
| 66 | - $output->plugins[] = $plugin; | |
| 67 | - $not_found = false; | |
| 68 | - } | |
| 84 | + /** Deactivate Plugin */ | |
| 85 | + public static function deactivate_plugins() { | |
| 86 | + self::action( 'deactivate' ); | |
| 87 | + } | |
| 69 | 88 | |
| 70 | - if ( 'not_installed' === $output->status && $not_found ) { | |
| 71 | - $installed = isset( $result_data['installed_plugins'] ) && is_array( $result_data['installed_plugins'] ) ? $result_data['installed_plugins'] : array(); | |
| 72 | - foreach ( $installed as $plugin ) { | |
| 73 | - if ( ! isset( $plugin['name'] ) ) { | |
| 74 | - continue; | |
| 75 | - } | |
| 76 | - $plugin['websiteid'] = $website->id; | |
| 77 | - $plugin['websiteurl'] = $website->url; | |
| 78 | - $plugin['websitename'] = $website->name; | |
| 79 | - $output->plugins_installed[] = $plugin; | |
| 80 | - } | |
| 81 | - } | |
| 89 | + /** Delete Plugin. */ | |
| 90 | + public static function delete_plugins() { | |
| 91 | + self::action( 'delete' ); | |
| 92 | + } | |
| 82 | 93 | |
| 83 | - if ( ! empty( $website->rollback_updates_data ) ) { | |
| 84 | - $output->roll_items[ $website->id ] = MainWP_Updates_Helper::get_roll_update_plugintheme_items( 'plugin', $website->rollback_updates_data ); | |
| 85 | - } | |
| 94 | + /** | |
| 95 | + * Ignore Plugin. | |
| 96 | + * | |
| 97 | + * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id() | |
| 98 | + * @uses \MainWP\Dashboard\MainWP_DB::update_website_values() | |
| 99 | + * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website() | |
| 100 | + */ | |
| 101 | + public static function ignore_updates() { | |
| 102 | + $websiteId = isset( $_POST['websiteId'] ) ? intval( $_POST['websiteId'] ) : false; | |
| 86 | 103 | |
| 87 | - unset( $plugins ); | |
| 88 | - } else { | |
| 89 | - $output->errors[ $website->id ] = MainWP_Error_Helper::get_error_message( new MainWP_Exception( 'NOMAINWP', $website->url ) ); | |
| 90 | - } | |
| 91 | - } | |
| 104 | + if ( empty( $websiteId ) ) { | |
| 105 | + die( wp_json_encode( array( 'error' => esc_html__( 'Site ID not found. Please reload the page and try again.', 'mainwp' ) ) ) ); | |
| 106 | + } | |
| 92 | 107 | |
| 93 | - /** Activate Plugin. */ | |
| 94 | - public static function activate_plugins() { | |
| 95 | - static::action( 'activate' ); | |
| 96 | - } | |
| 108 | + $website = MainWP_DB::instance()->get_website_by_id( $websiteId ); | |
| 97 | 109 | |
| 98 | - /** Deactivate Plugin */ | |
| 99 | - public static function deactivate_plugins() { | |
| 100 | - static::action( 'deactivate' ); | |
| 101 | - } | |
| 110 | + if ( ! MainWP_System_Utility::can_edit_website( $website ) ) { | |
| 111 | + die( wp_json_encode( array( 'error' => esc_html__( 'You are not allowed to edit this website.', 'mainwp' ) ) ) ); | |
| 112 | + } | |
| 102 | 113 | |
| 103 | - /** Delete Plugin. */ | |
| 104 | - public static function delete_plugins() { | |
| 105 | - static::action( 'delete' ); | |
| 106 | - } | |
| 114 | + $plugins = isset( $_POST['plugins'] ) ? wp_unslash( $_POST['plugins'] ) : false; | |
| 115 | + $names = isset( $_POST['names'] ) ? wp_unslash( $_POST['names'] ) : array(); | |
| 107 | 116 | |
| 108 | - /** | |
| 109 | - * Ignore Plugin. | |
| 110 | - * | |
| 111 | - * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id() | |
| 112 | - * @uses \MainWP\Dashboard\MainWP_DB::update_website_values() | |
| 113 | - * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website() | |
| 114 | - */ | |
| 115 | - public static function ignore_updates() { | |
| 116 | - // phpcs:disable WordPress.Security.NonceVerification | |
| 117 | - $websiteId = isset( $_POST['websiteId'] ) ? intval( $_POST['websiteId'] ) : false; | |
| 117 | + $decodedIgnoredPlugins = json_decode( $website->ignored_plugins, true ); | |
| 118 | 118 | |
| 119 | - if ( empty( $websiteId ) ) { | |
| 120 | - die( wp_json_encode( array( 'error' => esc_html__( 'Site ID not found. Please reload the page and try again.', 'mainwp' ) ) ) ); | |
| 121 | - } | |
| 119 | + if ( ! is_array( $decodedIgnoredPlugins ) ) { | |
| 120 | + $decodedIgnoredPlugins = array(); | |
| 121 | + } | |
| 122 | 122 | |
| 123 | - $website = MainWP_DB::instance()->get_website_by_id( $websiteId ); | |
| 123 | + if ( is_array( $plugins ) ) { | |
| 124 | + $_count = count( $plugins ); | |
| 125 | + for ( $i = 0; $i < $_count; $i ++ ) { | |
| 126 | + $slug = $plugins[ $i ]; | |
| 127 | + $name = $names[ $i ]; | |
| 128 | + if ( ! isset( $decodedIgnoredPlugins[ $slug ] ) ) { | |
| 129 | + $decodedIgnoredPlugins[ $slug ] = urldecode( $name ); | |
| 130 | + } | |
| 131 | + } | |
| 124 | 132 | |
| 125 | - if ( ! MainWP_System_Utility::can_edit_website( $website ) ) { | |
| 126 | - die( wp_json_encode( array( 'error' => esc_html__( 'You are not allowed to edit this website.', 'mainwp' ) ) ) ); | |
| 127 | - } | |
| 133 | + /** | |
| 134 | + * Action: mainwp_before_plugin_ignore | |
| 135 | + * | |
| 136 | + * Fires before plugin ignore. | |
| 137 | + * | |
| 138 | + * @since 4.1 | |
| 139 | + */ | |
| 140 | + do_action( 'mainwp_before_plugin_ignore', $decodedIgnoredPlugins, $website ); | |
| 128 | 141 | |
| 129 | - $plugins = isset( $_POST['plugins'] ) ? wp_unslash( $_POST['plugins'] ) : false; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 130 | - $names = isset( $_POST['names'] ) ? wp_unslash( $_POST['names'] ) : array(); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 131 | - // phpcs:enable | |
| 142 | + MainWP_DB::instance()->update_website_values( $website->id, array( 'ignored_plugins' => wp_json_encode( $decodedIgnoredPlugins ) ) ); | |
| 132 | 143 | |
| 133 | - $decodedIgnoredPlugins = json_decode( $website->ignored_plugins, true ); | |
| 144 | + /** | |
| 145 | + * Action: mainwp_after_plugin_ignore | |
| 146 | + * | |
| 147 | + * Fires after plugin ignore. | |
| 148 | + * | |
| 149 | + * @since 4.1 | |
| 150 | + */ | |
| 151 | + do_action( 'mainwp_after_plugin_ignore', $decodedIgnoredPlugins, $website ); | |
| 134 | 152 | |
| 135 | - if ( ! is_array( $decodedIgnoredPlugins ) ) { | |
| 136 | - $decodedIgnoredPlugins = array(); | |
| 137 | - } | |
| 153 | + } | |
| 138 | 154 | |
| 139 | - if ( is_array( $plugins ) ) { | |
| 140 | - $_count = count( $plugins ); | |
| 141 | - for ( $i = 0; $i < $_count; $i++ ) { | |
| 142 | - $slug = $plugins[ $i ]; | |
| 143 | - $name = $names[ $i ]; | |
| 144 | - if ( ! isset( $decodedIgnoredPlugins[ $slug ] ) ) { | |
| 145 | - $decodedIgnoredPlugins[ $slug ] = array( 'name' => urldecode( $name ) ); | |
| 146 | - } | |
| 147 | - } | |
| 155 | + die( wp_json_encode( array( 'result' => true ) ) ); | |
| 156 | + } | |
| 148 | 157 | |
| 149 | - /** | |
| 150 | - * Action: mainwp_before_plugin_ignore | |
| 151 | - * | |
| 152 | - * Fires before plugin ignore. | |
| 153 | - * | |
| 154 | - * @since 4.1 | |
| 155 | - */ | |
| 156 | - do_action( 'mainwp_before_plugin_ignore', $decodedIgnoredPlugins, $website ); | |
| 158 | + /** | |
| 159 | + * Plugin Action handler. | |
| 160 | + * | |
| 161 | + * @param mixed $pAction activate|deactivate|delete. | |
| 162 | + * | |
| 163 | + * @return mixed error|true | |
| 164 | + * | |
| 165 | + * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed() | |
| 166 | + * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_error_message() | |
| 167 | + * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id() | |
| 168 | + * @uses \MainWP\Dashboard\MainWP_Exception | |
| 169 | + * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website() | |
| 170 | + */ | |
| 171 | + public static function action( $pAction ) { | |
| 172 | + $websiteId = isset( $_POST['websiteId'] ) ? intval( $_POST['websiteId'] ) : false; | |
| 157 | 173 | |
| 158 | - MainWP_DB::instance()->update_website_values( $website->id, array( 'ignored_plugins' => wp_json_encode( $decodedIgnoredPlugins ) ) ); | |
| 174 | + if ( empty( $websiteId ) ) { | |
| 175 | + die( wp_json_encode( array( 'error' => esc_html__( 'Site ID not found. Please reload the page and try again.', 'mainwp' ) ) ) ); | |
| 176 | + } | |
| 159 | 177 | |
| 160 | - /** | |
| 161 | - * Action: mainwp_after_plugin_ignore | |
| 162 | - * | |
| 163 | - * Fires after plugin ignore. | |
| 164 | - * | |
| 165 | - * @since 4.1 | |
| 166 | - */ | |
| 167 | - do_action( 'mainwp_after_plugin_ignore', $decodedIgnoredPlugins, $website ); | |
| 178 | + $website = MainWP_DB::instance()->get_website_by_id( $websiteId ); | |
| 168 | 179 | |
| 169 | - } | |
| 180 | + if ( ! MainWP_System_Utility::can_edit_website( $website ) ) { | |
| 181 | + die( wp_json_encode( array( 'error' => esc_html__( 'You are not allowed to edit this website.', 'mainwp' ) ) ) ); | |
| 182 | + } | |
| 170 | 183 | |
| 171 | - die( wp_json_encode( array( 'result' => true ) ) ); | |
| 172 | - } | |
| 184 | + if ( MainWP_System_Utility::is_suspended_site( $website ) ) { | |
| 185 | + die( | |
| 186 | + wp_json_encode( | |
| 187 | + array( | |
| 188 | + 'error' => esc_html__( 'Suspended site.', 'mainwp' ), | |
| 189 | + 'errorCode' => 'SUSPENDED_SITE', | |
| 190 | + ) | |
| 191 | + ) | |
| 192 | + ); | |
| 193 | + } | |
| 173 | 194 | |
| 174 | - /** | |
| 175 | - * Plugin Action handler. | |
| 176 | - * | |
| 177 | - * @param mixed $pAction activate|deactivate|delete. | |
| 178 | - * | |
| 179 | - * @return mixed error|true | |
| 180 | - * | |
| 181 | - * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed() | |
| 182 | - * @uses \MainWP\Dashboard\MainWP_Error_Helper::get_error_message() | |
| 183 | - * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id() | |
| 184 | - * @uses \MainWP\Dashboard\MainWP_Exception | |
| 185 | - * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website() | |
| 186 | - */ | |
| 187 | - public static function action( $pAction ) { | |
| 188 | - // phpcs:disable WordPress.Security.NonceVerification | |
| 189 | - $websiteId = isset( $_POST['websiteId'] ) ? intval( $_POST['websiteId'] ) : false; | |
| 195 | + try { | |
| 196 | + $plugins = isset( $_POST['plugins'] ) ? wp_unslash( $_POST['plugins'] ) : array(); | |
| 197 | + $plugin = implode( '||', $plugins ); | |
| 198 | + $plugin = urldecode( $plugin ); | |
| 190 | 199 | |
| 191 | - if ( empty( $websiteId ) ) { | |
| 192 | - die( wp_json_encode( array( 'error' => esc_html__( 'Site ID not found. Please reload the page and try again.', 'mainwp' ) ) ) ); | |
| 193 | - } | |
| 200 | + /** | |
| 201 | + * Action: mainwp_before_plugin_action | |
| 202 | + * | |
| 203 | + * Fires before plugin activate/deactivate/delete actions. | |
| 204 | + * | |
| 205 | + * @since 4.1 | |
| 206 | + */ | |
| 207 | + do_action( 'mainwp_before_plugin_action', $pAction, $plugin, $website ); | |
| 194 | 208 | |
| 195 | - $website = MainWP_DB::instance()->get_website_by_id( $websiteId ); | |
| 209 | + $information = MainWP_Connect::fetch_url_authed( | |
| 210 | + $website, | |
| 211 | + 'plugin_action', | |
| 212 | + array( | |
| 213 | + 'action' => $pAction, | |
| 214 | + 'plugin' => $plugin, | |
| 215 | + ) | |
| 216 | + ); | |
| 196 | 217 | |
| 197 | - if ( ! MainWP_System_Utility::can_edit_website( $website ) ) { | |
| 198 | - die( wp_json_encode( array( 'error' => esc_html__( 'You are not allowed to edit this website.', 'mainwp' ) ) ) ); | |
| 199 | - } | |
| 218 | + /** | |
| 219 | + * Action: mainwp_after_plugin_action | |
| 220 | + * | |
| 221 | + * Fires after plugin activate/deactivate/delete actions. | |
| 222 | + * | |
| 223 | + * @since 4.1 | |
| 224 | + */ | |
| 225 | + do_action( 'mainwp_after_plugin_action', $information, $pAction, $plugin, $website ); | |
| 200 | 226 | |
| 201 | - if ( MainWP_System_Utility::is_suspended_site( $website ) ) { | |
| 202 | - die( | |
| 203 | - wp_json_encode( | |
| 204 | - array( | |
| 205 | - 'error' => esc_html__( 'Suspended site.', 'mainwp' ), | |
| 206 | - 'errorCode' => 'SUSPENDED_SITE', | |
| 207 | - ) | |
| 208 | - ) | |
| 209 | - ); | |
| 210 | - } | |
| 227 | + } catch ( MainWP_Exception $e ) { | |
| 228 | + die( wp_json_encode( array( 'error' => MainWP_Error_Helper::get_error_message( $e ) ) ) ); | |
| 229 | + } | |
| 211 | 230 | |
| 212 | - try { | |
| 213 | - $plugins = isset( $_POST['plugins'] ) ? wp_unslash( $_POST['plugins'] ) : array(); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 214 | - $plugin = implode( '||', $plugins ); | |
| 215 | - $plugin = urldecode( $plugin ); | |
| 231 | + if ( ! isset( $information['status'] ) || ( 'SUCCESS' !== $information['status'] ) ) { | |
| 232 | + die( wp_json_encode( array( 'error' => esc_html__( 'Unexpected error. Please try again.', 'mainwp' ) ) ) ); | |
| 233 | + } | |
| 216 | 234 | |
| 217 | - /** | |
| 218 | - * Action: mainwp_before_plugin_action | |
| 219 | - * | |
| 220 | - * Fires before plugin activate/deactivate/delete actions. | |
| 221 | - * | |
| 222 | - * @since 4.1 | |
| 223 | - */ | |
| 224 | - do_action( 'mainwp_before_plugin_action', $pAction, $plugin, $website ); | |
| 235 | + die( wp_json_encode( array( 'result' => true ) ) ); | |
| 236 | + } | |
| 225 | 237 | |
| 226 | - $information = MainWP_Connect::fetch_url_authed( | |
| 227 | - $website, | |
| 228 | - 'plugin_action', | |
| 229 | - array( | |
| 230 | - 'action' => $pAction, | |
| 231 | - 'plugin' => $plugin, | |
| 232 | - ) | |
| 233 | - ); | |
| 238 | + /** | |
| 239 | + * Trust Plugin $_POST. | |
| 240 | + * | |
| 241 | + * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension() | |
| 242 | + * @uses \MainWP\Dashboard\MainWP_DB_Common::update_user_extension() | |
| 243 | + */ | |
| 244 | + public static function trust_post() { | |
| 245 | + $userExtension = MainWP_DB_Common::instance()->get_user_extension(); | |
| 246 | + $trustedPlugins = json_decode( $userExtension->trusted_plugins, true ); | |
| 247 | + if ( ! is_array( $trustedPlugins ) ) { | |
| 248 | + $trustedPlugins = array(); | |
| 249 | + } | |
| 250 | + $action = isset( $_POST['do'] ) ? sanitize_text_field( wp_unslash( $_POST['do'] ) ) : ''; | |
| 251 | + $slugs = isset( $_POST['slugs'] ) && is_array( $_POST['slugs'] ) ? wp_unslash( $_POST['slugs'] ) : ''; | |
| 252 | + if ( ! is_array( $slugs ) ) { | |
| 253 | + return; | |
| 254 | + } | |
| 255 | + if ( 'trust' !== $action && 'untrust' !== $action ) { | |
| 256 | + return; | |
| 257 | + } | |
| 258 | + if ( 'trust' === $action ) { | |
| 259 | + foreach ( $slugs as $slug ) { | |
| 260 | + $idx = array_search( urldecode( $slug ), $trustedPlugins ); | |
| 261 | + if ( false === $idx ) { | |
| 262 | + $trustedPlugins[] = urldecode( $slug ); | |
| 263 | + } | |
| 264 | + } | |
| 265 | + } elseif ( 'untrust' === $action ) { | |
| 266 | + foreach ( $slugs as $slug ) { | |
| 267 | + if ( in_array( urldecode( $slug ), $trustedPlugins ) ) { | |
| 268 | + $trustedPlugins = array_diff( $trustedPlugins, array( urldecode( $slug ) ) ); | |
| 269 | + } | |
| 270 | + } | |
| 271 | + } | |
| 272 | + $userExtension->trusted_plugins = wp_json_encode( $trustedPlugins ); | |
| 273 | + MainWP_DB_Common::instance()->update_user_extension( $userExtension ); | |
| 274 | + } | |
| 234 | 275 | |
| 235 | - /** | |
| 236 | - * Action: mainwp_after_plugin_action | |
| 237 | - * | |
| 238 | - * Fires after plugin activate/deactivate/delete actions. | |
| 239 | - * | |
| 240 | - * @since 4.1 | |
| 241 | - */ | |
| 242 | - do_action( 'mainwp_after_plugin_action', $information, $pAction, $plugin, $website ); | |
| 276 | + /** | |
| 277 | + * Update Trusted Plugin list. | |
| 278 | + * | |
| 279 | + * @param mixed $slug Plugin Slug. | |
| 280 | + * | |
| 281 | + * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension() | |
| 282 | + * @uses \MainWP\Dashboard\MainWP_DB_Common::update_user_extension() | |
| 283 | + */ | |
| 284 | + public static function trust_plugin( $slug ) { | |
| 285 | + $userExtension = MainWP_DB_Common::instance()->get_user_extension(); | |
| 286 | + $trustedPlugins = json_decode( $userExtension->trusted_plugins, true ); | |
| 287 | + if ( ! is_array( $trustedPlugins ) ) { | |
| 288 | + $trustedPlugins = array(); | |
| 289 | + } | |
| 290 | + $idx = array_search( urldecode( $slug ), $trustedPlugins ); | |
| 291 | + if ( false === $idx ) { | |
| 292 | + $trustedPlugins[] = urldecode( $slug ); | |
| 293 | + } | |
| 294 | + $userExtension->trusted_plugins = wp_json_encode( $trustedPlugins ); | |
| 295 | + MainWP_DB_Common::instance()->update_user_extension( $userExtension ); | |
| 296 | + } | |
| 243 | 297 | |
| 244 | - } catch ( MainWP_Exception $e ) { | |
| 245 | - die( wp_json_encode( array( 'error' => MainWP_Error_Helper::get_error_message( $e ) ) ) ); | |
| 246 | - } | |
| 298 | + /** | |
| 299 | + * Check if automatic daily updates is on and | |
| 300 | + * the plugin is on the trusted list. | |
| 301 | + * | |
| 302 | + * @param mixed $slug Plugin Slug. | |
| 303 | + * | |
| 304 | + * @return boolean true|false. | |
| 305 | + * | |
| 306 | + * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension() | |
| 307 | + * @uses \MainWP\Dashboard\MainWP_DB_Common::update_user_extension() | |
| 308 | + */ | |
| 309 | + public static function check_auto_update_plugin( $slug ) { | |
| 310 | + if ( 1 != get_option( 'mainwp_automaticDailyUpdate' ) ) { | |
| 311 | + return false; | |
| 312 | + } | |
| 313 | + $userExtension = MainWP_DB_Common::instance()->get_user_extension(); | |
| 314 | + $trustedPlugins = json_decode( $userExtension->trusted_plugins, true ); | |
| 315 | + if ( is_array( $trustedPlugins ) && in_array( $slug, $trustedPlugins ) ) { | |
| 316 | + return true; | |
| 317 | + } | |
| 318 | + return false; | |
| 319 | + } | |
| 247 | 320 | |
| 248 | - // phpcs:enable | |
| 321 | + /** | |
| 322 | + * Save the trusted plugin note. | |
| 323 | + * | |
| 324 | + * @uses \MainWP\Dashboard\MainWP_Utility::esc_content() | |
| 325 | + */ | |
| 326 | + public static function save_trusted_plugin_note() { | |
| 327 | + $slug = isset( $_POST['slug'] ) ? urldecode( wp_unslash( $_POST['slug'] ) ) : ''; | |
| 328 | + $note = isset( $_POST['note'] ) ? wp_unslash( $_POST['note'] ) : ''; | |
| 329 | + $esc_note = MainWP_Utility::esc_content( $note ); | |
| 330 | + $userExtension = MainWP_DB_Common::instance()->get_user_extension(); | |
| 331 | + $trustedPluginsNotes = json_decode( $userExtension->trusted_plugins_notes, true ); | |
| 332 | + if ( ! is_array( $trustedPluginsNotes ) ) { | |
| 333 | + $trustedPluginsNotes = array(); | |
| 334 | + } | |
| 335 | + $trustedPluginsNotes[ $slug ] = $esc_note; | |
| 336 | + $userExtension->trusted_plugins_notes = wp_json_encode( $trustedPluginsNotes ); | |
| 337 | + MainWP_DB_Common::instance()->update_user_extension( $userExtension ); | |
| 338 | + } | |
| 249 | 339 | |
| 250 | - if ( ! isset( $information['status'] ) || ( 'SUCCESS' !== $information['status'] ) ) { | |
| 251 | - die( wp_json_encode( array( 'error' => esc_html__( 'Unexpected error. Please try again.', 'mainwp' ) ) ) ); | |
| 252 | - } | |
| 253 | - | |
| 254 | - die( wp_json_encode( array( 'result' => true ) ) ); | |
| 255 | - } | |
| 256 | - | |
| 257 | - /** | |
| 258 | - * Trust Plugin $_POST. | |
| 259 | - * | |
| 260 | - * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension() | |
| 261 | - * @uses \MainWP\Dashboard\MainWP_DB_Common::update_user_extension() | |
| 262 | - */ | |
| 263 | - public static function trust_post() { // phpcs:ignore -- NOSONAR - complex. | |
| 264 | - $userExtension = MainWP_DB_Common::instance()->get_user_extension(); | |
| 265 | - $trustedPlugins = json_decode( $userExtension->trusted_plugins, true ); | |
| 266 | - if ( ! is_array( $trustedPlugins ) ) { | |
| 267 | - $trustedPlugins = array(); | |
| 268 | - } | |
| 269 | - // phpcs:disable WordPress.Security.NonceVerification | |
| 270 | - $action = isset( $_POST['do'] ) ? sanitize_text_field( wp_unslash( $_POST['do'] ) ) : ''; | |
| 271 | - $slugs = isset( $_POST['slugs'] ) && is_array( $_POST['slugs'] ) ? wp_unslash( $_POST['slugs'] ) : ''; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 272 | - // phpcs:enable | |
| 273 | - if ( ! is_array( $slugs ) ) { | |
| 274 | - return; | |
| 275 | - } | |
| 276 | - if ( 'trust' !== $action && 'untrust' !== $action ) { | |
| 277 | - return; | |
| 278 | - } | |
| 279 | - if ( 'trust' === $action ) { | |
| 280 | - foreach ( $slugs as $slug ) { | |
| 281 | - $idx = array_search( urldecode( $slug ), $trustedPlugins ); | |
| 282 | - if ( false === $idx ) { | |
| 283 | - $trustedPlugins[] = urldecode( $slug ); | |
| 284 | - } | |
| 285 | - } | |
| 286 | - } elseif ( 'untrust' === $action ) { | |
| 287 | - foreach ( $slugs as $slug ) { | |
| 288 | - if ( in_array( urldecode( $slug ), $trustedPlugins ) ) { | |
| 289 | - $trustedPlugins = array_diff( $trustedPlugins, array( urldecode( $slug ) ) ); | |
| 290 | - } | |
| 291 | - } | |
| 292 | - } | |
| 293 | - $userExtension->trusted_plugins = wp_json_encode( $trustedPlugins ); | |
| 294 | - MainWP_DB_Common::instance()->update_user_extension( $userExtension ); | |
| 295 | - } | |
| 296 | - | |
| 297 | - /** | |
| 298 | - * Update Trusted Plugin list. | |
| 299 | - * | |
| 300 | - * @param mixed $slug Plugin Slug. | |
| 301 | - * | |
| 302 | - * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension() | |
| 303 | - * @uses \MainWP\Dashboard\MainWP_DB_Common::update_user_extension() | |
| 304 | - */ | |
| 305 | - public static function trust_plugin( $slug ) { | |
| 306 | - $userExtension = MainWP_DB_Common::instance()->get_user_extension(); | |
| 307 | - $trustedPlugins = json_decode( $userExtension->trusted_plugins, true ); | |
| 308 | - if ( ! is_array( $trustedPlugins ) ) { | |
| 309 | - $trustedPlugins = array(); | |
| 310 | - } | |
| 311 | - $idx = array_search( urldecode( $slug ), $trustedPlugins ); | |
| 312 | - if ( false === $idx ) { | |
| 313 | - $trustedPlugins[] = urldecode( $slug ); | |
| 314 | - } | |
| 315 | - $userExtension->trusted_plugins = wp_json_encode( $trustedPlugins ); | |
| 316 | - MainWP_DB_Common::instance()->update_user_extension( $userExtension ); | |
| 317 | - } | |
| 318 | - | |
| 319 | - /** | |
| 320 | - * Check if automatic daily updates is on and | |
| 321 | - * the plugin is on the trusted list. | |
| 322 | - * | |
| 323 | - * @param mixed $slug Plugin Slug. | |
| 324 | - * | |
| 325 | - * @return boolean true|false. | |
| 326 | - * | |
| 327 | - * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension() | |
| 328 | - * @uses \MainWP\Dashboard\MainWP_DB_Common::update_user_extension() | |
| 329 | - */ | |
| 330 | - public static function check_auto_update_plugin( $slug ) { | |
| 331 | - if ( 1 !== (int) get_option( 'mainwp_pluginAutomaticDailyUpdate' ) ) { | |
| 332 | - return false; | |
| 333 | - } | |
| 334 | - $userExtension = MainWP_DB_Common::instance()->get_user_extension(); | |
| 335 | - $trustedPlugins = json_decode( $userExtension->trusted_plugins, true ); | |
| 336 | - if ( is_array( $trustedPlugins ) && in_array( $slug, $trustedPlugins ) ) { | |
| 337 | - return true; | |
| 338 | - } | |
| 339 | - return false; | |
| 340 | - } | |
| 341 | - | |
| 342 | - /** | |
| 343 | - * Save the trusted plugin note. | |
| 344 | - * | |
| 345 | - * @uses \MainWP\Dashboard\MainWP_Utility::esc_content() | |
| 346 | - */ | |
| 347 | - public static function save_trusted_plugin_note() { | |
| 348 | - // phpcs:disable WordPress.Security.NonceVerification | |
| 349 | - $slug = isset( $_POST['slug'] ) ? urldecode( wp_unslash( $_POST['slug'] ) ) : ''; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 350 | - $note = isset( $_POST['note'] ) ? wp_unslash( $_POST['note'] ) : ''; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 351 | - // phpcs:enable | |
| 352 | - $esc_note = MainWP_Utility::esc_content( $note ); | |
| 353 | - $userExtension = MainWP_DB_Common::instance()->get_user_extension(); | |
| 354 | - $trustedPluginsNotes = json_decode( $userExtension->trusted_plugins_notes, true ); | |
| 355 | - if ( ! is_array( $trustedPluginsNotes ) ) { | |
| 356 | - $trustedPluginsNotes = array(); | |
| 357 | - } | |
| 358 | - $trustedPluginsNotes[ $slug ] = $esc_note; | |
| 359 | - $userExtension->trusted_plugins_notes = wp_json_encode( $trustedPluginsNotes ); | |
| 360 | - MainWP_DB_Common::instance()->update_user_extension( $userExtension ); | |
| 361 | - } | |
| 362 | 340 | } |