| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Handles legacy import/export admin actions that are still invoked from |
| 9 |
* View.php outside the POST action-handler registry. |
| 10 |
*/ |
| 11 |
class ABJ_404_Solution_LegacyImportActionHandler { |
| 12 |
|
| 13 |
/** @var ABJ_404_Solution_PluginLogicAdminActions */ |
| 14 |
private $parent; |
| 15 |
|
| 16 |
public function __construct(ABJ_404_Solution_PluginLogicAdminActions $parent) { |
| 17 |
$this->parent = $parent; |
| 18 |
} |
| 19 |
|
| 20 |
/** @return void */ |
| 21 |
public function handleActionChangeItemsPerRow(): void { |
| 22 |
$userIsPluginAdmin = abj_service('admin_access_policy')->isPluginAdmin(); |
| 23 |
if (ABJ_404_Solution_RequestInputNormalizer::getPostOrGetSanitize('action') == 'changeItemsPerRow' && $userIsPluginAdmin) { |
| 24 |
check_admin_referer('abj404_changeItemsPerRow'); |
| 25 |
$this->parent->updatePerPageOption(absint(ABJ_404_Solution_RequestInputNormalizer::getPostOrGetSanitize('perpage'))); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
/** @return void */ |
| 30 |
public function handleActionExport(): void { |
| 31 |
if ((ABJ_404_Solution_RequestInputNormalizer::getPostOrGetSanitize('action') == 'exportRedirects') && abj_service('admin_access_policy')->isPluginAdmin()) { |
| 32 |
check_admin_referer('abj404_exportRedirects'); |
| 33 |
$this->parent->getPluginLogic()->importExport()->doExport(); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** @return string|null */ |
| 38 |
public function handleActionImportFile() { |
| 39 |
if ((ABJ_404_Solution_RequestInputNormalizer::getPostOrGetSanitize('action') == 'importRedirectsFile') && abj_service('admin_access_policy')->isPluginAdmin()) { |
| 40 |
check_admin_referer('abj404_importRedirectsFile'); |
| 41 |
$result = $this->parent->getPluginLogic()->importExport()->doImportFile(); |
| 42 |
return $result; |
| 43 |
} |
| 44 |
|
| 45 |
return null; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @return string |
| 50 |
*/ |
| 51 |
public function handleActionImportRedirects(): string { |
| 52 |
$message = ""; |
| 53 |
|
| 54 |
if (ABJ_404_Solution_RequestInputNormalizer::getPostOrGetSanitize('action') == 'importRedirects') { |
| 55 |
if (ABJ_404_Solution_RequestInputNormalizer::getPostOrGetSanitize('sanity_404redirected') != '1') { |
| 56 |
return __("Error: You didn't check the I understand checkbox. No importing for you!", '404-solution'); |
| 57 |
} |
| 58 |
|
| 59 |
check_admin_referer('abj404_importRedirects'); |
| 60 |
|
| 61 |
try { |
| 62 |
$result = $this->parent->getDependencies()->getPluginUpdateRepo()->importDataFromPluginRedirectioner(); |
| 63 |
if ($result['last_error'] != '') { |
| 64 |
$lastErrorJson = json_encode($result['last_error']); |
| 65 |
$message = sprintf(__("Error: No records were imported. SQL result: %s", '404-solution'), |
| 66 |
wp_kses_post(is_string($lastErrorJson) ? $lastErrorJson : '')); |
| 67 |
} else { |
| 68 |
$rowsAffected = is_scalar($result['rows_affected']) ? (string)$result['rows_affected'] : '0'; |
| 69 |
$message = sprintf(__("Records imported: %s", '404-solution'), esc_html($rowsAffected)); |
| 70 |
} |
| 71 |
|
| 72 |
} catch (Exception $e) { |
| 73 |
$message = "Error: Importing failed. Message: " . $e->getMessage(); |
| 74 |
$this->parent->getLogger()->errorMessage('Error importing redirects.', $e); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return $message; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @return string Human-readable result message. |
| 83 |
*/ |
| 84 |
public function handleActionUndoRegexAutoPromote(): string { |
| 85 |
$notice = ABJ_404_Solution_RegexAutoPromote::readNotice(); |
| 86 |
if ($notice === null || $notice['redirect_id'] <= 0) { |
| 87 |
return __('Error: No regex auto-promotion to undo.', '404-solution'); |
| 88 |
} |
| 89 |
$dbQuery = $this->parent->getDbQuery(); |
| 90 |
$redirectsTable = $dbQuery->doTableNameReplacements('{wp_abj404_redirects}'); |
| 91 |
$sql = "UPDATE `" . $redirectsTable . "` SET `url` = %s, `status` = %d WHERE `id` = %d"; |
| 92 |
$dbQuery->queryAndGetResults($sql, array('query_params' => array( |
| 93 |
$notice['original_url'], |
| 94 |
(int)ABJ404_STATUS_MANUAL, |
| 95 |
(int)$notice['redirect_id'], |
| 96 |
))); |
| 97 |
ABJ_404_Solution_RegexAutoPromote::clearNotice(); |
| 98 |
return sprintf( |
| 99 |
/* translators: %s = the original from_url string that was restored */ |
| 100 |
__('Regex auto-promotion undone. Restored "%s" with status Manual.', '404-solution'), |
| 101 |
$notice['original_url'] |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @return string Human-readable result message. |
| 107 |
*/ |
| 108 |
public function handleActionImportFromPlugin(): string { |
| 109 |
$source = ABJ_404_Solution_RequestInputNormalizer::readText( |
| 110 |
$_POST, array('name' => 'import_source')); |
| 111 |
|
| 112 |
if ($source === '') { |
| 113 |
return __('Error: No source plugin specified.', '404-solution'); |
| 114 |
} |
| 115 |
|
| 116 |
$allowedSources = array('rankmath', 'yoast', 'aioseo', 'safe-redirect-manager', 'redirection'); |
| 117 |
if (!in_array($source, $allowedSources, true)) { |
| 118 |
return sprintf( |
| 119 |
/* translators: %s = unknown source identifier */ |
| 120 |
__('Error: Unknown source plugin "%s".', '404-solution'), |
| 121 |
esc_html($source) |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
$importer = new ABJ_404_Solution_CrossPluginImporter($this->parent->getRedirectsRepo(), $this->parent->getLogger()); |
| 126 |
$count = $importer->importFrom($source); |
| 127 |
|
| 128 |
return sprintf( |
| 129 |
/* translators: %d = number of redirects imported */ |
| 130 |
_n( |
| 131 |
'%d redirect imported successfully.', |
| 132 |
'%d redirects imported successfully.', |
| 133 |
$count, |
| 134 |
'404-solution' |
| 135 |
), |
| 136 |
$count |
| 137 |
); |
| 138 |
} |
| 139 |
} |
| 140 |
|