| 1 |
<?php |
| 2 |
/** |
| 3 |
* Password Policy Settings. |
| 4 |
* |
| 5 |
* Handles propagation of password policy settings to child sites. |
| 6 |
* |
| 7 |
* @package MainWP/Dashboard |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace MainWP\Dashboard; |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Class MainWP_Password_Policy_Settings |
| 19 |
*/ |
| 20 |
class MainWP_Password_Policy_Settings { |
| 21 |
|
| 22 |
/** |
| 23 |
* Get Class Name |
| 24 |
* |
| 25 |
* @return string __CLASS__ |
| 26 |
*/ |
| 27 |
public static function get_class_name() { |
| 28 |
return __CLASS__; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Initialize hooks. |
| 33 |
*/ |
| 34 |
public static function init() { |
| 35 |
add_action( 'wp_ajax_mainwp_password_policy_update_site', array( static::get_class_name(), 'ajax_update_site' ) ); |
| 36 |
add_action( 'wp_ajax_mainwp_password_policy_save_individual', array( static::get_class_name(), 'ajax_save_individual' ) ); |
| 37 |
add_action( 'admin_enqueue_scripts', array( static::get_class_name(), 'enqueue_scripts' ) ); |
| 38 |
add_filter( 'mainwp_create_security_nonces', array( static::get_class_name(), 'create_security_nonces' ) ); |
| 39 |
add_filter( 'mainwp_getsubpages_sites', array( static::get_class_name(), 'managesites_subpage' ), 10, 1 ); |
| 40 |
add_filter( 'mainwp_manage_sites_navigation_items', array( static::get_class_name(), 'reorder_navigation_items' ), 10, 3 ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Register security nonce for password policy AJAX action. |
| 45 |
* |
| 46 |
* @param array $nonces Existing nonces. |
| 47 |
* @return array Updated nonces. |
| 48 |
*/ |
| 49 |
public static function create_security_nonces( $nonces ) { |
| 50 |
if ( ! is_array( $nonces ) ) { |
| 51 |
$nonces = array(); |
| 52 |
} |
| 53 |
$nonces[] = 'mainwp_password_policy_update_site'; |
| 54 |
$nonces[] = 'mainwp_password_policy_save_individual'; |
| 55 |
return $nonces; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Add Password Policy tab to individual site pages. |
| 60 |
* |
| 61 |
* @param array $subpages Existing subpages. |
| 62 |
* @return array Updated subpages. |
| 63 |
*/ |
| 64 |
public static function managesites_subpage( $subpages ) { |
| 65 |
if ( ! is_array( $subpages ) ) { |
| 66 |
$subpages = array(); |
| 67 |
} |
| 68 |
|
| 69 |
$subpages[] = array( |
| 70 |
'title' => esc_html__( 'Password Policy', 'mainwp' ), |
| 71 |
'slug' => 'PasswordPolicy', |
| 72 |
'sitetab' => true, |
| 73 |
'menu_hidden' => true, |
| 74 |
'callback' => array( static::get_class_name(), 'render_individual_site' ), |
| 75 |
); |
| 76 |
|
| 77 |
return $subpages; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Reorder navigation items to place Password Policy after Site Hardening. |
| 82 |
* |
| 83 |
* @param array $items Navigation items. |
| 84 |
* @param int $site_id Site ID. |
| 85 |
* @return array Reordered navigation items. |
| 86 |
*/ |
| 87 |
public static function reorder_navigation_items( $items, $site_id ) { |
| 88 |
if ( empty( $site_id ) || ! is_array( $items ) ) { |
| 89 |
return $items; |
| 90 |
} |
| 91 |
|
| 92 |
$password_policy_item = null; |
| 93 |
$security_scan_index = -1; |
| 94 |
$new_items = array(); |
| 95 |
|
| 96 |
foreach ( $items as $item ) { |
| 97 |
if ( isset( $item['href'] ) && false !== strpos( $item['href'], 'PasswordPolicy' ) ) { |
| 98 |
$password_policy_item = $item; |
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
if ( isset( $item['href'] ) && false !== strpos( $item['href'], 'scanid=' ) ) { |
| 103 |
$security_scan_index = count( $new_items ); |
| 104 |
} |
| 105 |
|
| 106 |
$new_items[] = $item; |
| 107 |
} |
| 108 |
|
| 109 |
if ( null !== $password_policy_item && $security_scan_index >= 0 ) { |
| 110 |
array_splice( $new_items, $security_scan_index + 1, 0, array( $password_policy_item ) ); |
| 111 |
return $new_items; |
| 112 |
} |
| 113 |
|
| 114 |
if ( null !== $password_policy_item ) { |
| 115 |
$new_items[] = $password_policy_item; |
| 116 |
} |
| 117 |
|
| 118 |
return $new_items; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Enqueue scripts and styles. |
| 123 |
* |
| 124 |
* @param string $hook Current admin page hook. |
| 125 |
*/ |
| 126 |
public static function enqueue_scripts( $hook ) { |
| 127 |
if ( 'mainwp_page_PasswordPolicy' !== $hook && |
| 128 |
'mainwp_page_managesites' !== $hook && |
| 129 |
'mainwp_page_ManageSitesPasswordPolicy' !== $hook ) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
wp_enqueue_script( |
| 134 |
'mainwp-password-policy', |
| 135 |
MAINWP_PLUGIN_URL . 'assets/js/mainwp-password-policy.js', |
| 136 |
array( 'jquery' ), |
| 137 |
MAINWP_VERSION, |
| 138 |
true |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Method init_menu() |
| 144 |
* |
| 145 |
* Add Settings sub menu "Password Policy Settings". |
| 146 |
*/ |
| 147 |
public static function init_menu() { |
| 148 |
add_submenu_page( |
| 149 |
'mainwp_tab', |
| 150 |
__( 'Password Policy', 'mainwp' ), |
| 151 |
'<div class="mainwp-hidden">' . esc_html__( 'Password Policy', 'mainwp' ) . '</div>', |
| 152 |
'read', |
| 153 |
'PasswordPolicy', |
| 154 |
array( |
| 155 |
static::get_class_name(), |
| 156 |
'render', |
| 157 |
) |
| 158 |
); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Renders the individual site Password Policy Settings page. |
| 163 |
*/ |
| 164 |
public static function render_individual_site() { |
| 165 |
do_action( 'mainwp_pageheader_sites', 'PasswordPolicy' ); |
| 166 |
|
| 167 |
$site_id = isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0; |
| 168 |
|
| 169 |
if ( empty( $site_id ) ) { |
| 170 |
?> |
| 171 |
<div class="ui red message"> |
| 172 |
<?php esc_html_e( 'Invalid site ID.', 'mainwp' ); ?> |
| 173 |
</div> |
| 174 |
<?php |
| 175 |
do_action( 'mainwp_pagefooter_sites' ); |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
$website = MainWP_DB::instance()->get_website_by_id( $site_id ); |
| 180 |
|
| 181 |
if ( ! $website ) { |
| 182 |
?> |
| 183 |
<div class="ui red message"> |
| 184 |
<?php esc_html_e( 'Site not found.', 'mainwp' ); ?> |
| 185 |
</div> |
| 186 |
<?php |
| 187 |
do_action( 'mainwp_pagefooter_sites' ); |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
static::render_individual_form( $website ); |
| 192 |
|
| 193 |
do_action( 'mainwp_pagefooter_sites' ); |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Renders the Password Policy Settings page. |
| 198 |
* |
| 199 |
* @uses \MainWP\Dashboard\MainWP_Connect::fetch_urls_authed() |
| 200 |
* @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id() |
| 201 |
* @uses \MainWP\Dashboard\MainWP_DB::query() |
| 202 |
* @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_for_current_user() |
| 203 |
* @uses \MainWP\Dashboard\MainWP_DB::fetch_object() |
| 204 |
* @uses \MainWP\Dashboard\MainWP_DB::free_result() |
| 205 |
* @uses \MainWP\Dashboard\MainWP_User::render_header() |
| 206 |
* @uses \MainWP\Dashboard\MainWP_User::render_footer() |
| 207 |
* @uses \MainWP\Dashboard\MainWP_Utility::map_site() |
| 208 |
*/ |
| 209 |
public static function render() { // phpcs:ignore -- NOSONAR -- complex. |
| 210 |
$dbwebsites = array(); |
| 211 |
$errors = array(); |
| 212 |
$form_submitted = false; |
| 213 |
|
| 214 |
if ( isset( $_POST['bulk_update_password_policy'] ) ) { |
| 215 |
$form_submitted = true; |
| 216 |
check_admin_referer( 'mainwp_password_policy_settings', 'security' ); |
| 217 |
|
| 218 |
if ( ! isset( $_POST['mainwp_password_policy_window'] ) ) { |
| 219 |
$errors[] = esc_html__( 'Please select a password policy window.', 'mainwp' ); |
| 220 |
} else { |
| 221 |
$policy_window = sanitize_text_field( wp_unslash( $_POST['mainwp_password_policy_window'] ) ); |
| 222 |
$valid_windows = array( '0', '30', '60', '90', '120', '180', '360' ); |
| 223 |
if ( ! in_array( $policy_window, $valid_windows, true ) ) { |
| 224 |
$errors[] = esc_html__( 'Invalid password policy window selected.', 'mainwp' ); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
$data_fields = MainWP_System_Utility::get_default_map_site_fields(); |
| 229 |
|
| 230 |
if ( empty( $errors ) ) { |
| 231 |
$policy_window = sanitize_text_field( wp_unslash( $_POST['mainwp_password_policy_window'] ) ); |
| 232 |
$due_soon_message = isset( $_POST['mainwp_password_policy_due_soon_message'] ) ? sanitize_textarea_field( wp_unslash( $_POST['mainwp_password_policy_due_soon_message'] ) ) : ''; |
| 233 |
$overdue_message = isset( $_POST['mainwp_password_policy_overdue_message'] ) ? sanitize_textarea_field( wp_unslash( $_POST['mainwp_password_policy_overdue_message'] ) ) : ''; |
| 234 |
$show_notices_to = isset( $_POST['mainwp_password_policy_show_notices_to'] ) ? sanitize_text_field( wp_unslash( $_POST['mainwp_password_policy_show_notices_to'] ) ) : 'edit_posts'; |
| 235 |
|
| 236 |
if ( ! in_array( $show_notices_to, array( 'edit_posts', 'all_users' ), true ) ) { |
| 237 |
$show_notices_to = 'edit_posts'; |
| 238 |
} |
| 239 |
|
| 240 |
$settings = array( |
| 241 |
'max_age_days' => intval( $policy_window ), |
| 242 |
'due_soon_message' => $due_soon_message, |
| 243 |
'overdue_message' => $overdue_message, |
| 244 |
'show_notices_to' => $show_notices_to, |
| 245 |
); |
| 246 |
update_option( 'mainwp_password_policy_settings', $settings ); |
| 247 |
|
| 248 |
$websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user() ); |
| 249 |
while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) { |
| 250 |
if ( '' !== $website->sync_errors || MainWP_System_Utility::is_suspended_site( $website ) ) { |
| 251 |
continue; |
| 252 |
} |
| 253 |
|
| 254 |
$individual_settings_json = MainWP_DB::instance()->get_website_option( $website, 'password_policy_individual_settings' ); |
| 255 |
$individual_settings = array(); |
| 256 |
|
| 257 |
if ( ! empty( $individual_settings_json ) ) { |
| 258 |
$individual_settings = json_decode( $individual_settings_json, true ); |
| 259 |
if ( ! is_array( $individual_settings ) ) { |
| 260 |
$individual_settings = array(); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
if ( ! empty( $individual_settings['overwrite_enabled'] ) ) { |
| 265 |
continue; |
| 266 |
} |
| 267 |
|
| 268 |
$dbwebsites[ $website->id ] = MainWP_Utility::map_site( |
| 269 |
$website, |
| 270 |
$data_fields |
| 271 |
); |
| 272 |
} |
| 273 |
MainWP_DB::free_result( $websites ); |
| 274 |
|
| 275 |
if ( empty( $dbwebsites ) ) { |
| 276 |
$errors[] = esc_html__( 'No eligible sites found. All sites have sync errors or are suspended.', 'mainwp' ); |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
MainWP_User::render_header( 'PasswordPolicy' ); |
| 282 |
static::render_form( $errors, $form_submitted, $dbwebsites ); |
| 283 |
MainWP_User::render_footer( 'PasswordPolicy' ); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Renders password policy settings update results. |
| 288 |
* |
| 289 |
* @param object $dbwebsites The websites object. |
| 290 |
* @param object $output Result of password policy settings update. |
| 291 |
*/ |
| 292 |
public static function render_modal( $dbwebsites, $output ) { |
| 293 |
?> |
| 294 |
<div class="ui modal" id="mainwp-password-policy-settings-modal"> |
| 295 |
<i class="close icon"></i> |
| 296 |
<div class="header"><?php esc_html_e( 'Password Policy Settings Updated', 'mainwp' ); ?></div> |
| 297 |
<div class="scrolling content"> |
| 298 |
<?php |
| 299 |
/** |
| 300 |
* Action: mainwp_password_policy_settings_modal_top |
| 301 |
* |
| 302 |
* Fires at the top of the Password Policy Settings modal. |
| 303 |
* |
| 304 |
* @since 4.1 |
| 305 |
*/ |
| 306 |
do_action( 'mainwp_password_policy_settings_modal_top' ); |
| 307 |
?> |
| 308 |
<div class="ui relaxed divided list"> |
| 309 |
<?php foreach ( $dbwebsites as $website ) : ?> |
| 310 |
<div class="item"> |
| 311 |
<a href="<?php echo esc_url( admin_url( 'admin.php?page=managesites&dashboard=' . $website->id ) ); ?>"><?php echo esc_html( stripslashes( $website->name ) ); ?></a> |
| 312 |
<span class="right floated content"> |
| 313 |
<?php echo isset( $output->ok[ $website->id ] ) && 1 === (int) $output->ok[ $website->id ] ? '<i class="green check icon"></i>' : '<i class="red times icon"></i> ' . esc_html( $output->errors[ $website->id ] ); ?> |
| 314 |
</span> |
| 315 |
</div> |
| 316 |
<?php endforeach; ?> |
| 317 |
</div> |
| 318 |
<?php |
| 319 |
/** |
| 320 |
* Action: mainwp_password_policy_settings_modal_bottom |
| 321 |
* |
| 322 |
* Fires at the bottom of the Password Policy Settings modal. |
| 323 |
* |
| 324 |
* @since 4.1 |
| 325 |
*/ |
| 326 |
do_action( 'mainwp_password_policy_settings_modal_bottom' ); |
| 327 |
?> |
| 328 |
</div> |
| 329 |
<div class="actions"> |
| 330 |
</div> |
| 331 |
</div> |
| 332 |
<script type="text/javascript"> |
| 333 |
jQuery( '#mainwp-password-policy-settings-modal' ).modal( 'show' ); |
| 334 |
</script> |
| 335 |
<?php |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Renders password policy settings form. |
| 340 |
* |
| 341 |
* @param array $errors Array of error messages. |
| 342 |
* @param bool $form_submitted Whether the form was submitted. |
| 343 |
* @param array $dbwebsites Array of websites to update via AJAX. |
| 344 |
*/ |
| 345 |
public static function render_form( $errors = array(), $form_submitted = false, $dbwebsites = array() ) { |
| 346 |
$defaults = array( |
| 347 |
'max_age_days' => 0, |
| 348 |
'due_soon_message' => __( 'Your password is due to be changed soon. Please update it as soon as possible. This helps keep your account secure.', 'mainwp' ), |
| 349 |
'overdue_message' => __( 'Your password change is overdue. Please update your password now. This is required by your site\'s password policy.', 'mainwp' ), |
| 350 |
'show_notices_to' => 'edit_posts', |
| 351 |
); |
| 352 |
$settings = wp_parse_args( get_option( 'mainwp_password_policy_settings', array() ), $defaults ); |
| 353 |
?> |
| 354 |
<div id="mainwp-password-policy-settings" class="ui padded segment"> |
| 355 |
<?php if ( MainWP_Utility::show_mainwp_message( 'notice', 'mainwp-password-policy-settings-info-message' ) ) : ?> |
| 356 |
<div class="ui info message"> |
| 357 |
<i class="close icon mainwp-notice-dismiss" notice-id="mainwp-password-policy-settings-info-message"></i> |
| 358 |
<div><?php esc_html_e( 'WordPress does not store a reliable "last password change" date by default. MainWP starts tracking password changes from the moment the MainWP Child plugin is updated to this version. Until a user changes their password, their "Last password change" may show as Unknown.', 'mainwp' ); ?></div><br/> |
| 359 |
<div><?php esc_html_e( 'When you enable a password policy (choose a period other than Never), MainWP measures compliance from the date the policy was enabled on each site. For users with an Unknown last-change date, notices are based on "no password change recorded since policy was enabled" (not on a historical password age).', 'mainwp' ); ?></div><br/> |
| 360 |
<div><?php esc_html_e( 'Notices are shown only to logged-in users. Depending on your "Show notices to" setting, they appear in wp-admin (for users with `edit_posts` and above) and/or on the front end. Front-end notices are injected by the plugin and may be affected by theme layout, caching, or custom login/account flows.', 'mainwp' ); ?></div> |
| 361 |
</div> |
| 362 |
<?php endif; ?> |
| 363 |
<h2 class="ui header"> |
| 364 |
<?php esc_html_e( 'Password Policy Settings', 'mainwp' ); ?> |
| 365 |
<div class="sub header"><?php esc_html_e( 'Configure when users should be reminded to change their passwords on your connected sites. Tracking runs in the background; reminders are only shown when a policy is enabled.', 'mainwp' ); ?></div> |
| 366 |
</h2> |
| 367 |
<form action="" method="post" name="mainwp-password-policy-settings-form" id="mainwp-password-policy-settings-form" class="ui form"> |
| 368 |
<?php wp_nonce_field( 'mainwp-admin-nonce' ); ?> |
| 369 |
<input type="hidden" name="security" value="<?php echo esc_attr( wp_create_nonce( 'mainwp_password_policy_settings' ) ); ?>"/> |
| 370 |
<?php if ( $form_submitted ) : ?> |
| 371 |
<?php if ( empty( $errors ) ) : ?> |
| 372 |
<div class="ui green message"> |
| 373 |
<i class="close icon"></i> |
| 374 |
<?php esc_html_e( 'Password policy settings updated successfully.', 'mainwp' ); ?> |
| 375 |
</div> |
| 376 |
<?php else : ?> |
| 377 |
<div class="ui red message"> |
| 378 |
<i class="close icon"></i> |
| 379 |
<?php foreach ( $errors as $error ) : ?> |
| 380 |
<div><?php echo esc_html( $error ); ?></div> |
| 381 |
<?php endforeach; ?> |
| 382 |
</div> |
| 383 |
<?php endif; ?> |
| 384 |
<?php endif; ?> |
| 385 |
|
| 386 |
<div class="ui grid field"> |
| 387 |
<label class="six wide column middle aligned"><?php esc_html_e( 'Require password change every', 'mainwp' ); ?></label> |
| 388 |
<div class="six wide column"> |
| 389 |
<select name="mainwp_password_policy_window" id="mainwp_password_policy_window" class="ui dropdown"> |
| 390 |
<option value="0" <?php selected( $settings['max_age_days'], 0 ); ?>><?php esc_html_e( 'Never (Default)', 'mainwp' ); ?></option> |
| 391 |
<option value="30" <?php selected( $settings['max_age_days'], 30 ); ?>><?php esc_html_e( '30 days', 'mainwp' ); ?></option> |
| 392 |
<option value="60" <?php selected( $settings['max_age_days'], 60 ); ?>><?php esc_html_e( '60 days', 'mainwp' ); ?></option> |
| 393 |
<option value="90" <?php selected( $settings['max_age_days'], 90 ); ?>><?php esc_html_e( '90 days', 'mainwp' ); ?></option> |
| 394 |
<option value="120" <?php selected( $settings['max_age_days'], 120 ); ?>><?php esc_html_e( '120 days', 'mainwp' ); ?></option> |
| 395 |
<option value="180" <?php selected( $settings['max_age_days'], 180 ); ?>><?php esc_html_e( '180 days', 'mainwp' ); ?></option> |
| 396 |
<option value="360" <?php selected( $settings['max_age_days'], 360 ); ?>><?php esc_html_e( '360 days', 'mainwp' ); ?></option> |
| 397 |
</select> |
| 398 |
<br/><span class="ui small grey text"><?php esc_html_e( 'Select how often users should be prompted to change their password. "Never" disables password policy reminders. When enabled, users will see a reminder 7 days before the deadline and an overdue notice after it.', 'mainwp' ); ?></span> |
| 399 |
</div> |
| 400 |
</div> |
| 401 |
|
| 402 |
<div id="mainwp-password-policy-additional-settings" style="<?php echo ( 0 === intval( $settings['max_age_days'] ) ) ? 'display:none;' : ''; ?>"> |
| 403 |
<div class="ui grid field"> |
| 404 |
<label class="six wide column middle aligned"><?php esc_html_e( '"Due soon" reminder message', 'mainwp' ); ?></label> |
| 405 |
<div class="ten wide column"> |
| 406 |
<input |
| 407 |
type="text" |
| 408 |
name="mainwp_password_policy_due_soon_message" |
| 409 |
id="mainwp_password_policy_due_soon_message" |
| 410 |
value="<?php echo esc_attr( $settings['due_soon_message'] ); ?>" |
| 411 |
class="ui fluid input"> |
| 412 |
<span class="ui small grey text"><?php esc_html_e( 'Shown to users 7 days before their password change is due.', 'mainwp' ); ?></span> |
| 413 |
</div> |
| 414 |
</div> |
| 415 |
|
| 416 |
<div class="ui grid field"> |
| 417 |
<label class="six wide column middle aligned"><?php esc_html_e( '"Overdue" reminder message', 'mainwp' ); ?></label> |
| 418 |
<div class="ten wide column"> |
| 419 |
<input |
| 420 |
type="text" |
| 421 |
name="mainwp_password_policy_overdue_message" |
| 422 |
id="mainwp_password_policy_overdue_message" |
| 423 |
value="<?php echo esc_attr( $settings['overdue_message'] ); ?>" |
| 424 |
class="ui fluid input"> |
| 425 |
<span class="ui small grey text"><?php esc_html_e( 'Shown to users when their password change is overdue.', 'mainwp' ); ?></span> |
| 426 |
</div> |
| 427 |
</div> |
| 428 |
|
| 429 |
<div class="ui grid field"> |
| 430 |
<label class="six wide column middle aligned"><?php esc_html_e( 'Show notices to', 'mainwp' ); ?></label> |
| 431 |
<div class="ten wide column"> |
| 432 |
<select name="mainwp_password_policy_show_notices_to" id="mainwp_password_policy_show_notices_to" class="ui dropdown"> |
| 433 |
<option value="edit_posts" <?php selected( $settings['show_notices_to'], 'edit_posts' ); ?>><?php esc_html_e( 'Users with wp-admin access (edit_posts and above)', 'mainwp' ); ?></option> |
| 434 |
<option value="all_users" <?php selected( $settings['show_notices_to'], 'all_users' ); ?>><?php esc_html_e( 'All users (all roles)', 'mainwp' ); ?></option> |
| 435 |
</select> |
| 436 |
<br/><span class="ui small grey text"><?php esc_html_e( 'Choose who receives password policy notices. "wp-admin access" targets roles that can edit posts (e.g., Authors, Editors, Administrators) and avoids showing notices to subscribers/customers on the front end.', 'mainwp' ); ?></span> |
| 437 |
</div> |
| 438 |
</div> |
| 439 |
</div> |
| 440 |
|
| 441 |
<div class="ui divider"></div> |
| 442 |
<input type="submit" name="bulk_update_password_policy" id="bulk_update_password_policy" class="ui green big button" value="<?php esc_attr_e( 'Save Settings', 'mainwp' ); ?>"/> |
| 443 |
</form> |
| 444 |
</div> |
| 445 |
|
| 446 |
<div class="ui modal" id="mainwp-password-policy-progress-modal"> |
| 447 |
<i class="mainwp-modal-close close icon"></i> |
| 448 |
<div class="header"><?php esc_html_e( 'Updating Password Policy Settings', 'mainwp' ); ?></div> |
| 449 |
<div class="ui green progress mainwp-modal-progress"> |
| 450 |
<div class="bar"><div class="progress"></div></div> |
| 451 |
<div class="label"></div> |
| 452 |
</div> |
| 453 |
<div class="scrolling content mainwp-modal-content"> |
| 454 |
<div class="ui middle aligned divided list" id="mainwp-password-policy-sites-list"> |
| 455 |
</div> |
| 456 |
</div> |
| 457 |
</div> |
| 458 |
|
| 459 |
<?php if ( ! empty( $dbwebsites ) ) : ?> |
| 460 |
<script type="text/javascript"> |
| 461 |
jQuery(document).ready(function() { |
| 462 |
var passwordPolicySettings = <?php echo wp_json_encode( $settings ); ?>; |
| 463 |
var sitesToUpdate = <?php echo wp_json_encode( array_keys( $dbwebsites ) ); ?>; |
| 464 |
var sitesData = <?php echo wp_json_encode( $dbwebsites ); ?>; |
| 465 |
|
| 466 |
mainwp_password_policy_start_update(sitesToUpdate, sitesData, passwordPolicySettings); |
| 467 |
}); |
| 468 |
</script> |
| 469 |
<?php endif; ?> |
| 470 |
<?php |
| 471 |
} |
| 472 |
|
| 473 |
/** |
| 474 |
* Renders individual site password policy settings form. |
| 475 |
* |
| 476 |
* @param object $website Website object. |
| 477 |
*/ |
| 478 |
public static function render_individual_form( $website ) { |
| 479 |
$global_defaults = array( |
| 480 |
'max_age_days' => 0, |
| 481 |
'due_soon_message' => __( 'Your password is due to be changed soon. Please update it as soon as possible. This helps keep your account secure.', 'mainwp' ), |
| 482 |
'overdue_message' => __( 'Your password change is overdue. Please update your password now. This is required by your site\'s password policy.', 'mainwp' ), |
| 483 |
'show_notices_to' => 'edit_posts', |
| 484 |
); |
| 485 |
$global_settings = wp_parse_args( get_option( 'mainwp_password_policy_settings', array() ), $global_defaults ); |
| 486 |
|
| 487 |
$individual_settings_json = MainWP_DB::instance()->get_website_option( $website, 'password_policy_individual_settings' ); |
| 488 |
$individual_settings = array(); |
| 489 |
|
| 490 |
if ( ! empty( $individual_settings_json ) ) { |
| 491 |
$individual_settings = json_decode( $individual_settings_json, true ); |
| 492 |
if ( ! is_array( $individual_settings ) ) { |
| 493 |
$individual_settings = array(); |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
$overwrite_enabled = ! empty( $individual_settings['overwrite_enabled'] ); |
| 498 |
|
| 499 |
if ( $overwrite_enabled ) { |
| 500 |
$settings = wp_parse_args( $individual_settings, $global_defaults ); |
| 501 |
} else { |
| 502 |
$settings = $global_settings; |
| 503 |
} |
| 504 |
?> |
| 505 |
<div id="mainwp-individual-password-policy-settings" class="ui padded segment"> |
| 506 |
<div id="individual-password-policy-status" class="ui message" style="display:none;"></div> |
| 507 |
|
| 508 |
<h2 class="ui header"> |
| 509 |
<?php /* translators: %s: site name wrapped in HTML span */ printf( esc_html__( 'Password Policy Settings for %s', 'mainwp' ), '<span class="ui green text">' . esc_html( stripslashes( $website->name ) ) . '</span>' ); ?> |
| 510 |
<div class="sub header"> |
| 511 |
<?php esc_html_e( 'Configure password policy settings specific to this site. When disabled, this site will use the global password policy settings.', 'mainwp' ); ?> <?php /* translators: %s: number of days wrapped in HTML strong tag */ printf( esc_html__( 'Current global setting: Require password change every %s', 'mainwp' ), '<strong>' . ( 0 === intval( $global_settings['max_age_days'] ) ? esc_html__( 'Never', 'mainwp' ) : intval( $global_settings['max_age_days'] ) . ' ' . esc_html__( 'days', 'mainwp' ) ) . '</strong>' ); ?> |
| 512 |
</div> |
| 513 |
</h2> |
| 514 |
|
| 515 |
<div class="ui hidden divider"></div> |
| 516 |
|
| 517 |
<form id="mainwp-individual-password-policy-form" class="ui form"> |
| 518 |
<?php wp_nonce_field( 'mainwp-admin-nonce' ); ?> |
| 519 |
<input type="hidden" name="site_id" value="<?php echo esc_attr( $website->id ); ?>"/> |
| 520 |
|
| 521 |
<div class="ui toggle checkbox field" id="overwrite-enabled-checkbox"> |
| 522 |
<input |
| 523 |
type="checkbox" |
| 524 |
name="overwrite_enabled" |
| 525 |
id="overwrite_enabled" |
| 526 |
<?php checked( $overwrite_enabled, true ); ?>> |
| 527 |
<label for="overwrite_enabled"> |
| 528 |
<strong><?php esc_html_e( 'Overwrite global settings for this site', 'mainwp' ); ?></strong> |
| 529 |
</label> |
| 530 |
</div> |
| 531 |
|
| 532 |
<div id="individual-settings-fields" style="<?php echo $overwrite_enabled ? '' : 'display:none;'; ?>"> |
| 533 |
<div class="ui grid field"> |
| 534 |
<label class="six wide column middle aligned"><?php esc_html_e( 'Require password change every', 'mainwp' ); ?></label> |
| 535 |
<div class="six wide column"> |
| 536 |
<select name="max_age_days" id="individual_max_age_days" class="ui dropdown"> |
| 537 |
<option value="0" <?php selected( $settings['max_age_days'], 0 ); ?>><?php esc_html_e( 'Never (Default)', 'mainwp' ); ?></option> |
| 538 |
<option value="30" <?php selected( $settings['max_age_days'], 30 ); ?>><?php esc_html_e( '30 days', 'mainwp' ); ?></option> |
| 539 |
<option value="60" <?php selected( $settings['max_age_days'], 60 ); ?>><?php esc_html_e( '60 days', 'mainwp' ); ?></option> |
| 540 |
<option value="90" <?php selected( $settings['max_age_days'], 90 ); ?>><?php esc_html_e( '90 days', 'mainwp' ); ?></option> |
| 541 |
<option value="120" <?php selected( $settings['max_age_days'], 120 ); ?>><?php esc_html_e( '120 days', 'mainwp' ); ?></option> |
| 542 |
<option value="180" <?php selected( $settings['max_age_days'], 180 ); ?>><?php esc_html_e( '180 days', 'mainwp' ); ?></option> |
| 543 |
<option value="360" <?php selected( $settings['max_age_days'], 360 ); ?>><?php esc_html_e( '360 days', 'mainwp' ); ?></option> |
| 544 |
</select> |
| 545 |
<br/><span class="ui small grey text"><?php esc_html_e( 'Select how often users should be prompted to change their password.', 'mainwp' ); ?></span> |
| 546 |
</div> |
| 547 |
</div> |
| 548 |
|
| 549 |
<div id="individual-additional-settings" style="<?php echo ( 0 === intval( $settings['max_age_days'] ) ) ? 'display:none;' : ''; ?>"> |
| 550 |
<div class="ui grid field"> |
| 551 |
<label class="six wide column middle aligned"><?php esc_html_e( '"Due soon" reminder message', 'mainwp' ); ?></label> |
| 552 |
<div class="ten wide column"> |
| 553 |
<input |
| 554 |
type="text" |
| 555 |
name="due_soon_message" |
| 556 |
id="individual_due_soon_message" |
| 557 |
value="<?php echo esc_attr( $settings['due_soon_message'] ); ?>" |
| 558 |
class="ui fluid input"> |
| 559 |
<span class="ui small grey text"><?php esc_html_e( 'Shown to users 7 days before their password change is due.', 'mainwp' ); ?></span> |
| 560 |
</div> |
| 561 |
</div> |
| 562 |
|
| 563 |
<div class="ui grid field"> |
| 564 |
<label class="six wide column middle aligned"><?php esc_html_e( '"Overdue" reminder message', 'mainwp' ); ?></label> |
| 565 |
<div class="ten wide column"> |
| 566 |
<input |
| 567 |
type="text" |
| 568 |
name="overdue_message" |
| 569 |
id="individual_overdue_message" |
| 570 |
value="<?php echo esc_attr( $settings['overdue_message'] ); ?>" |
| 571 |
class="ui fluid input"> |
| 572 |
<span class="ui small grey text"><?php esc_html_e( 'Shown to users when their password change is overdue.', 'mainwp' ); ?></span> |
| 573 |
</div> |
| 574 |
</div> |
| 575 |
|
| 576 |
<div class="ui grid field"> |
| 577 |
<label class="six wide column middle aligned"><?php esc_html_e( 'Show notices to', 'mainwp' ); ?></label> |
| 578 |
<div class="ten wide column"> |
| 579 |
<select name="show_notices_to" id="individual_show_notices_to" class="ui dropdown"> |
| 580 |
<option value="edit_posts" <?php selected( $settings['show_notices_to'], 'edit_posts' ); ?>><?php esc_html_e( 'Users with wp-admin access (edit_posts and above)', 'mainwp' ); ?></option> |
| 581 |
<option value="all_users" <?php selected( $settings['show_notices_to'], 'all_users' ); ?>><?php esc_html_e( 'All users (all roles)', 'mainwp' ); ?></option> |
| 582 |
</select> |
| 583 |
<br/><span class="ui small grey text"><?php esc_html_e( 'Choose who receives password policy notices.', 'mainwp' ); ?></span> |
| 584 |
</div> |
| 585 |
</div> |
| 586 |
</div> |
| 587 |
<div class="ui divider"></div> |
| 588 |
</div> |
| 589 |
|
| 590 |
<button type="button" id="save-individual-password-policy" class="ui green big button" style="<?php echo $overwrite_enabled ? '' : 'display:none;'; ?>"> |
| 591 |
<?php esc_html_e( 'Save Settings', 'mainwp' ); ?> |
| 592 |
</button> |
| 593 |
</form> |
| 594 |
</div> |
| 595 |
<?php |
| 596 |
} |
| 597 |
|
| 598 |
/** |
| 599 |
* Handle password policy settings response from child sites. |
| 600 |
* |
| 601 |
* @param mixed $data Response data from child site. |
| 602 |
* @param object $website Website object. |
| 603 |
* @param object $output Output object to store results. |
| 604 |
* @param array $params Additional parameters. |
| 605 |
*/ |
| 606 |
public static function posting_handler( $data, $website, &$output, $params = array() ) { // phpcs:ignore -- NOSONAR -- complex. |
| 607 |
if ( preg_match( '/<mainwp>(.*)<\/mainwp>/', $data, $results ) > 0 ) { |
| 608 |
$decoded = base64_decode( $results[1] ); |
| 609 |
|
| 610 |
if ( false === $decoded ) { |
| 611 |
$output->errors[ $website->id ] = esc_html__( 'Failed to decode response from child site.', 'mainwp' ); |
| 612 |
return; |
| 613 |
} |
| 614 |
|
| 615 |
$information = @unserialize( $decoded ); |
| 616 |
|
| 617 |
if ( false === $information && 'b:0;' !== $decoded ) { |
| 618 |
$output->errors[ $website->id ] = esc_html__( 'Child site does not support password policy settings. Please update MainWP Child plugin.', 'mainwp' ); |
| 619 |
return; |
| 620 |
} |
| 621 |
|
| 622 |
if ( ( isset( $information['success'] ) && $information['success'] ) || |
| 623 |
( isset( $information['result'] ) && 'SUCCESS' === $information['result'] ) ) { |
| 624 |
$output->ok[ $website->id ] = 1; |
| 625 |
} else { |
| 626 |
$output->errors[ $website->id ] = isset( $information['error'] ) ? $information['error'] : esc_html__( 'Undefined error occurred. Please check child site.', 'mainwp' ); |
| 627 |
} |
| 628 |
} else { |
| 629 |
$output->errors[ $website->id ] = esc_html__( 'Invalid response from child site.', 'mainwp' ); |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
/** |
| 634 |
* AJAX handler for saving individual site password policy settings. |
| 635 |
*/ |
| 636 |
public static function ajax_save_individual() { // phpcs:ignore -- NOSONAR -- complex. |
| 637 |
MainWP_Post_Handler::instance()->check_security( 'mainwp_password_policy_save_individual' ); |
| 638 |
|
| 639 |
$site_id = isset( $_POST['site_id'] ) ? intval( $_POST['site_id'] ) : 0; |
| 640 |
|
| 641 |
if ( empty( $site_id ) ) { |
| 642 |
wp_send_json_error( array( 'message' => esc_html__( 'Invalid site ID', 'mainwp' ) ) ); |
| 643 |
} |
| 644 |
|
| 645 |
$website = MainWP_DB::instance()->get_website_by_id( $site_id ); |
| 646 |
|
| 647 |
if ( ! $website ) { |
| 648 |
wp_send_json_error( array( 'message' => esc_html__( 'Site not found', 'mainwp' ) ) ); |
| 649 |
} |
| 650 |
|
| 651 |
$overwrite_enabled = isset( $_POST['overwrite_enabled'] ) && '1' === $_POST['overwrite_enabled']; |
| 652 |
|
| 653 |
$settings = array( |
| 654 |
'overwrite_enabled' => $overwrite_enabled, |
| 655 |
); |
| 656 |
|
| 657 |
if ( $overwrite_enabled ) { |
| 658 |
$max_age_days = isset( $_POST['max_age_days'] ) ? intval( $_POST['max_age_days'] ) : 0; |
| 659 |
$valid_windows = array( 0, 30, 60, 90, 120, 180, 360 ); |
| 660 |
|
| 661 |
if ( ! in_array( $max_age_days, $valid_windows, true ) ) { |
| 662 |
wp_send_json_error( array( 'message' => esc_html__( 'Invalid password policy window selected.', 'mainwp' ) ) ); |
| 663 |
} |
| 664 |
|
| 665 |
$settings['max_age_days'] = $max_age_days; |
| 666 |
$settings['due_soon_message'] = isset( $_POST['due_soon_message'] ) ? sanitize_textarea_field( wp_unslash( $_POST['due_soon_message'] ) ) : ''; |
| 667 |
$settings['overdue_message'] = isset( $_POST['overdue_message'] ) ? sanitize_textarea_field( wp_unslash( $_POST['overdue_message'] ) ) : ''; |
| 668 |
$settings['show_notices_to'] = isset( $_POST['show_notices_to'] ) ? sanitize_text_field( wp_unslash( $_POST['show_notices_to'] ) ) : 'edit_posts'; |
| 669 |
|
| 670 |
if ( ! in_array( $settings['show_notices_to'], array( 'edit_posts', 'all_users' ), true ) ) { |
| 671 |
$settings['show_notices_to'] = 'edit_posts'; |
| 672 |
} |
| 673 |
|
| 674 |
MainWP_DB::instance()->update_website_option( $website, 'password_policy_individual_settings', wp_json_encode( $settings ) ); |
| 675 |
|
| 676 |
if ( '' !== $website->sync_errors || MainWP_System_Utility::is_suspended_site( $website ) ) { |
| 677 |
wp_send_json_success( |
| 678 |
array( |
| 679 |
'message' => esc_html__( 'Settings saved to Dashboard. Site has sync errors or is suspended, so settings were not pushed to child site.', 'mainwp' ), |
| 680 |
) |
| 681 |
); |
| 682 |
} |
| 683 |
|
| 684 |
$post_data = array( |
| 685 |
'action' => 'update_password_policy', |
| 686 |
'max_age_days' => $settings['max_age_days'], |
| 687 |
'due_soon_days' => 7, |
| 688 |
'due_soon_message' => $settings['due_soon_message'], |
| 689 |
'overdue_message' => $settings['overdue_message'], |
| 690 |
'show_notices_to' => $settings['show_notices_to'], |
| 691 |
); |
| 692 |
|
| 693 |
try { |
| 694 |
$result = MainWP_Connect::fetch_url_authed( |
| 695 |
$website, |
| 696 |
'password_policy_settings', |
| 697 |
$post_data |
| 698 |
); |
| 699 |
|
| 700 |
if ( empty( $result ) ) { |
| 701 |
wp_send_json_error( array( 'message' => esc_html__( 'Settings saved to Dashboard but failed to push to child site. Empty response received.', 'mainwp' ) ) ); |
| 702 |
} |
| 703 |
|
| 704 |
if ( is_array( $result ) ) { |
| 705 |
if ( isset( $result['error'] ) ) { |
| 706 |
wp_send_json_error( array( 'message' => esc_html__( 'Settings saved to Dashboard but failed to push to child site: ', 'mainwp' ) . $result['error'] ) ); |
| 707 |
} |
| 708 |
|
| 709 |
if ( isset( $result['result'] ) && 'SUCCESS' === $result['result'] ) { |
| 710 |
wp_send_json_success( array( 'message' => esc_html__( 'Individual password policy settings saved and pushed to child site successfully.', 'mainwp' ) ) ); |
| 711 |
} |
| 712 |
} |
| 713 |
|
| 714 |
if ( is_string( $result ) && preg_match( '/<mainwp>(.*)<\/mainwp>/', $result, $results ) > 0 ) { |
| 715 |
$decoded = base64_decode( $results[1] ); |
| 716 |
|
| 717 |
if ( false !== $decoded ) { |
| 718 |
$information = json_decode( $decoded, true ); |
| 719 |
|
| 720 |
if ( null !== $information ) { |
| 721 |
if ( ( isset( $information['success'] ) && $information['success'] ) || |
| 722 |
( isset( $information['result'] ) && 'SUCCESS' === $information['result'] ) ) { |
| 723 |
wp_send_json_success( array( 'message' => esc_html__( 'Individual password policy settings saved and pushed to child site successfully.', 'mainwp' ) ) ); |
| 724 |
} else { |
| 725 |
$error_msg = isset( $information['error'] ) ? $information['error'] : esc_html__( 'Undefined error occurred.', 'mainwp' ); |
| 726 |
wp_send_json_error( array( 'message' => esc_html__( 'Settings saved to Dashboard but failed to push to child site: ', 'mainwp' ) . $error_msg ) ); |
| 727 |
} |
| 728 |
} |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
wp_send_json_error( array( 'message' => esc_html__( 'Settings saved to Dashboard but received invalid response from child site.', 'mainwp' ) ) ); |
| 733 |
|
| 734 |
} catch ( \Exception $e ) { |
| 735 |
wp_send_json_error( array( 'message' => esc_html__( 'Settings saved to Dashboard but exception occurred: ', 'mainwp' ) . $e->getMessage() ) ); |
| 736 |
} |
| 737 |
} else { |
| 738 |
MainWP_DB::instance()->update_website_option( $website, 'password_policy_individual_settings', wp_json_encode( $settings ) ); |
| 739 |
wp_send_json_success( array( 'message' => esc_html__( 'Individual settings disabled. Site will now use global password policy settings.', 'mainwp' ) ) ); |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
/** |
| 744 |
* AJAX handler for updating a single site's password policy settings. |
| 745 |
*/ |
| 746 |
public static function ajax_update_site() { // phpcs:ignore -- NOSONAR -- complexity. |
| 747 |
MainWP_Post_Handler::instance()->check_security( 'mainwp_password_policy_update_site' ); |
| 748 |
|
| 749 |
$site_id = isset( $_POST['site_id'] ) ? intval( $_POST['site_id'] ) : 0; |
| 750 |
|
| 751 |
if ( empty( $site_id ) ) { |
| 752 |
wp_send_json( array( 'error' => esc_html__( 'Invalid site ID', 'mainwp' ) ) ); |
| 753 |
} |
| 754 |
|
| 755 |
$website = MainWP_DB::instance()->get_website_by_id( $site_id ); |
| 756 |
|
| 757 |
if ( ! $website ) { |
| 758 |
wp_send_json( array( 'error' => esc_html__( 'Site not found', 'mainwp' ) ) ); |
| 759 |
} |
| 760 |
|
| 761 |
$individual_settings_json = MainWP_DB::instance()->get_website_option( $website, 'password_policy_individual_settings' ); |
| 762 |
$individual_settings = array(); |
| 763 |
|
| 764 |
if ( ! empty( $individual_settings_json ) ) { |
| 765 |
$individual_settings = json_decode( $individual_settings_json, true ); |
| 766 |
if ( ! is_array( $individual_settings ) ) { |
| 767 |
$individual_settings = array(); |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
if ( ! empty( $individual_settings['overwrite_enabled'] ) ) { |
| 772 |
wp_send_json( |
| 773 |
array( |
| 774 |
'success' => true, |
| 775 |
'skipped' => true, |
| 776 |
'message' => esc_html__( 'Site is using individual password policy settings', 'mainwp' ), |
| 777 |
) |
| 778 |
); |
| 779 |
} |
| 780 |
|
| 781 |
$post_data = array( |
| 782 |
'action' => 'update_password_policy', |
| 783 |
'max_age_days' => isset( $_POST['max_age_days'] ) ? intval( $_POST['max_age_days'] ) : 0, |
| 784 |
'due_soon_days' => isset( $_POST['due_soon_days'] ) ? intval( $_POST['due_soon_days'] ) : 7, |
| 785 |
'due_soon_message' => isset( $_POST['due_soon_message'] ) ? sanitize_textarea_field( wp_unslash( $_POST['due_soon_message'] ) ) : '', |
| 786 |
'overdue_message' => isset( $_POST['overdue_message'] ) ? sanitize_textarea_field( wp_unslash( $_POST['overdue_message'] ) ) : '', |
| 787 |
'show_notices_to' => isset( $_POST['show_notices_to'] ) ? sanitize_text_field( wp_unslash( $_POST['show_notices_to'] ) ) : 'edit_posts', |
| 788 |
); |
| 789 |
|
| 790 |
try { |
| 791 |
$website = MainWP_DB::instance()->get_website_by_id( $site_id ); |
| 792 |
|
| 793 |
if ( ! $website ) { |
| 794 |
wp_send_json( array( 'error' => esc_html__( 'Site not found', 'mainwp' ) ) ); |
| 795 |
} |
| 796 |
|
| 797 |
if ( '' !== $website->sync_errors || MainWP_System_Utility::is_suspended_site( $website ) ) { |
| 798 |
wp_send_json( array( 'error' => esc_html__( 'Site has sync errors or is suspended', 'mainwp' ) ) ); |
| 799 |
} |
| 800 |
|
| 801 |
$result = MainWP_Connect::fetch_url_authed( |
| 802 |
$website, |
| 803 |
'password_policy_settings', |
| 804 |
$post_data |
| 805 |
); |
| 806 |
|
| 807 |
if ( empty( $result ) ) { |
| 808 |
wp_send_json( array( 'error' => esc_html__( 'Empty response from child site. Please check the child site connection.', 'mainwp' ) ) ); |
| 809 |
} |
| 810 |
|
| 811 |
if ( is_array( $result ) ) { |
| 812 |
if ( isset( $result['error'] ) ) { |
| 813 |
wp_send_json( array( 'error' => $result['error'] ) ); |
| 814 |
} |
| 815 |
|
| 816 |
if ( isset( $result['result'] ) && 'SUCCESS' === $result['result'] ) { |
| 817 |
wp_send_json( array( 'success' => true ) ); |
| 818 |
} |
| 819 |
|
| 820 |
wp_send_json( |
| 821 |
array( |
| 822 |
'error' => esc_html__( 'Invalid response from child site.', 'mainwp' ), |
| 823 |
'debug' => 'Response is array with keys: ' . implode( ', ', array_keys( $result ) ), |
| 824 |
) |
| 825 |
); |
| 826 |
} |
| 827 |
|
| 828 |
if ( ! is_string( $result ) ) { |
| 829 |
wp_send_json( |
| 830 |
array( |
| 831 |
'error' => esc_html__( 'Invalid response type from child site.', 'mainwp' ), |
| 832 |
'debug' => 'Response type: ' . gettype( $result ), |
| 833 |
) |
| 834 |
); |
| 835 |
} |
| 836 |
|
| 837 |
if ( preg_match( '/<mainwp>(.*)<\/mainwp>/', $result, $results ) > 0 ) { |
| 838 |
$decoded = base64_decode( $results[1] ); |
| 839 |
|
| 840 |
if ( false === $decoded ) { |
| 841 |
wp_send_json( array( 'error' => esc_html__( 'Failed to decode response from child site.', 'mainwp' ) ) ); |
| 842 |
} |
| 843 |
|
| 844 |
$information = json_decode( $decoded, true ); |
| 845 |
|
| 846 |
if ( null === $information ) { |
| 847 |
wp_send_json( array( 'error' => esc_html__( 'Failed to parse response from child site.', 'mainwp' ) ) ); |
| 848 |
} |
| 849 |
|
| 850 |
if ( ( isset( $information['success'] ) && $information['success'] ) || |
| 851 |
( isset( $information['result'] ) && 'SUCCESS' === $information['result'] ) ) { |
| 852 |
wp_send_json( array( 'success' => true ) ); |
| 853 |
} else { |
| 854 |
$error_msg = isset( $information['error'] ) ? $information['error'] : esc_html__( 'Undefined error occurred.', 'mainwp' ); |
| 855 |
wp_send_json( array( 'error' => $error_msg ) ); |
| 856 |
} |
| 857 |
} else { |
| 858 |
$response_preview = strlen( $result ) > 200 ? substr( $result, 0, 200 ) . '...' : $result; |
| 859 |
wp_send_json( |
| 860 |
array( |
| 861 |
'error' => esc_html__( 'Invalid response from child site.', 'mainwp' ), |
| 862 |
'debug' => 'Response preview: ' . esc_html( $response_preview ), |
| 863 |
) |
| 864 |
); |
| 865 |
} |
| 866 |
} catch ( \Exception $e ) { |
| 867 |
wp_send_json( array( 'error' => 'Exception: ' . $e->getMessage() ) ); |
| 868 |
} catch ( \Error $e ) { |
| 869 |
wp_send_json( array( 'error' => 'Error: ' . $e->getMessage() ) ); |
| 870 |
} |
| 871 |
} |
| 872 |
} |
| 873 |
|