| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Modules\FullSiteImport\Runners; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Templately\Modules\FullSiteImport\Utils\Form; |
| 7 |
use Templately\Modules\FullSiteImport\Runners\BaseRunner; |
| 8 |
use Templately\Modules\FullSiteImport\Utils\Utils; |
| 9 |
use Templately\Utils\Helper; |
| 10 |
|
| 11 |
class Customizer extends BaseRunner { |
| 12 |
|
| 13 |
public function get_name(): string { |
| 14 |
return 'customizer'; |
| 15 |
} |
| 16 |
|
| 17 |
public function get_label(): string { |
| 18 |
return __( 'Customizer', 'templately' ); |
| 19 |
} |
| 20 |
|
| 21 |
public function should_log(): bool { |
| 22 |
return true; |
| 23 |
} |
| 24 |
|
| 25 |
public function get_action(): string { |
| 26 |
return 'eventLog'; |
| 27 |
} |
| 28 |
|
| 29 |
public function log_message(): string { |
| 30 |
return __( 'Updating customizer settings.', 'templately' ); |
| 31 |
} |
| 32 |
|
| 33 |
public function should_run( $data, $imported_data = [] ): bool { |
| 34 |
return ! empty( $data['title'] ) || !empty( $data['slogan'] ); |
| 35 |
} |
| 36 |
|
| 37 |
public function import( $data, $imported_data ): array { |
| 38 |
$params = $data; |
| 39 |
$customizer = []; |
| 40 |
|
| 41 |
$this->log( 0 ); |
| 42 |
|
| 43 |
if( ! empty( $params['title'] ) ) { |
| 44 |
// set_theme_mod( 'blogname', $params['title'] ); |
| 45 |
if ( $this->update_site_option( 'blogname', $params['title'], __( 'site title', 'templately' ) ) ) { |
| 46 |
$customizer[] = 'title'; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// update Tagline |
| 51 |
if( ! empty( $params['slogan'] ) ) { |
| 52 |
// set_theme_mod( 'blogdescription', $params['slogan'] ); |
| 53 |
if ( $this->update_site_option( 'blogdescription', $params['slogan'], __( 'tagline', 'templately' ) ) ) { |
| 54 |
$customizer[] = 'slogan'; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return [ 'customizer' => $customizer ]; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Write one customizer option, contained. |
| 63 |
* |
| 64 |
* `update_option()` runs third-party hook code, and the two options this runner writes |
| 65 |
* are the ones builders listen on: Elementor hooks `update_option_blogname` / |
| 66 |
* `update_option_blogdescription` and syncs the value into the ACTIVE KIT. On a site |
| 67 |
* whose kit post is gone — a bad restore, a deleted kit, a half-finished migration — |
| 68 |
* that sync throws `Exception( 'Invalid post.' )` from Elementor's page settings |
| 69 |
* manager. Uncontained, that exception left the runner, aborted the whole import as a |
| 70 |
* NON-retryable "Oops! / Invalid post." four frames from its real cause, and gave the |
| 71 |
* user no way forward — over a site title. |
| 72 |
* |
| 73 |
* Contained per field: the failure is logged, an `eventLog` line names the field, and |
| 74 |
* the import continues (the other field, and every later runner, still run). \Throwable |
| 75 |
* rather than Exception because the same hook chain can raise a PHP \Error from an |
| 76 |
* outdated add-on — the same reason the Templates runner catches one. |
| 77 |
* |
| 78 |
* Returning false means "do not report this field as applied" — deliberately |
| 79 |
* conservative, since where the throw came from decides whether the value landed: |
| 80 |
* `update_option()` fires its hooks AFTER the DB write, so an Elementor kit-sync |
| 81 |
* failure leaves the option itself written, while a `pre_update_option_*` filter |
| 82 |
* throwing means nothing was. The user-facing line is worded for both. The revert |
| 83 |
* backup is taken before either, so revert still restores the old value. |
| 84 |
* |
| 85 |
* @return bool True when the option was written without a hook blowing up. |
| 86 |
*/ |
| 87 |
private function update_site_option( $key, $value, $label ): bool { |
| 88 |
try { |
| 89 |
Utils::update_option( $key, $value ); |
| 90 |
return true; |
| 91 |
} catch ( \Throwable $e ) { |
| 92 |
Helper::log( |
| 93 |
sprintf( 'Customizer option "%s" failed: %s', $key, $e->getMessage() ), |
| 94 |
'fsi', |
| 95 |
'error' |
| 96 |
); |
| 97 |
|
| 98 |
$this->log( |
| 99 |
0, |
| 100 |
sprintf( |
| 101 |
/* translators: %s: the customizer field name, e.g. "site title" */ |
| 102 |
__( 'Could not fully apply the %s — a plugin hooked to that change failed. The rest of the pack was imported.', 'templately' ), |
| 103 |
$label |
| 104 |
), |
| 105 |
'eventLog' |
| 106 |
); |
| 107 |
|
| 108 |
return false; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
} |