post_type === 'domo_property') ? 'domopage' : $file;
});
// "Edit" on a landing page opens our review screen instead of the bare post editor.
add_filter('get_edit_post_link', function ($link, $id) {
return get_post_type($id) === 'domo_property' ? admin_url('admin.php?page=domopage&post=' . (int) $id) : $link;
}, 10, 2);
add_action('admin_enqueue_scripts', function () {
$page = sanitize_key($_GET['page'] ?? '');
if (!in_array($page, apply_filters('domopage_admin_pages', ['domopage', 'domo-settings']), true)) {
return;
}
wp_enqueue_style('domo-admin', DOMO_URL . 'assets/admin.css', [], DOMO_VERSION . '.' . filemtime(DOMO_DIR . 'assets/admin.css'));
$deps = ['jquery'];
if ($page === 'domopage' && !empty($_GET['post'])) {
wp_enqueue_media();
}
if ($page === 'domo-settings') {
wp_enqueue_media();
wp_enqueue_style('wp-color-picker');
$deps[] = 'wp-color-picker';
}
wp_enqueue_script('domo-admin', DOMO_URL . 'assets/admin.js', $deps, DOMO_VERSION . '.' . filemtime(DOMO_DIR . 'assets/admin.js'), true);
wp_localize_script('domo-admin', 'domoAdmin', ['maxLanguages' => domo_max_languages(), 'chooseLogo' => __('Choose logo', 'domopage'), 'chooseVideo' => __('Choose or upload a video', 'domopage')]);
});
/* ------------------------------------------------------------------ Settings */
add_action('admin_init', function () {
register_setting('domo_settings_group', 'domo_settings', ['sanitize_callback' => 'domo_sanitize_settings']);
});
function domo_sanitize_settings($in): array
{
$out = domo_settings();
$in = is_array($in) ? $in : [];
$tab = sanitize_key($in['_tab'] ?? '');
foreach (['company', 'phone', 'ai_model'] as $k) {
if (isset($in[$k])) {
$out[$k] = sanitize_text_field($in[$k]);
}
}
foreach (['logo', 'logo_light', 'privacy_url', 'terms_url'] as $k) {
if (isset($in[$k])) {
$out[$k] = esc_url_raw(trim($in[$k]));
}
}
if (isset($in['email'])) {
$email = sanitize_email($in['email']);
if (is_email($email)) {
$out['email'] = $email;
} else {
add_settings_error('domo_settings', 'email', __('The enquiry email address is not valid, so it was not changed.', 'domopage'));
}
}
foreach (['c_primary', 'c_accent'] as $k) {
if (isset($in[$k])) {
$out[$k] = sanitize_hex_color($in[$k]) ?: $out[$k];
}
}
if (isset($in['font']) && isset(domo_fonts()[$in['font']])) {
$out['font'] = $in['font'];
}
if (isset($in['ai_provider']) && in_array($in['ai_provider'], ['none', 'wordpress', 'anthropic', 'openai'], true)) {
$out['ai_provider'] = $in['ai_provider'];
}
if (!empty($in['ai_key_clear'])) {
$out['ai_key'] = '';
} elseif (isset($in['ai_key']) && trim($in['ai_key']) !== '') {
$out['ai_key'] = sanitize_text_field(trim($in['ai_key']));
}
if (isset($in['whatsapp'])) {
$out['whatsapp'] = preg_replace('/\D/', '', $in['whatsapp']);
}
if (isset($in['country']) && isset(domo_countries()[$in['country']])) {
$out['country'] = $in['country'];
}
foreach (['reg_label', 'reg_number'] as $k) {
if (isset($in[$k])) {
$out[$k] = sanitize_text_field($in[$k]);
}
}
if (isset($in['legal_notice'])) {
$out['legal_notice'] = sanitize_textarea_field($in['legal_notice']);
}
if ($tab === 'brand') {
$out['credit'] = empty($in['credit']) ? 0 : 1;
}
if ($tab === 'languages') {
$out['marketing_optin'] = empty($in['marketing_optin']) ? 0 : 1;
$out['consent_note'] = sanitize_textarea_field($in['consent_note'] ?? '');
$valid = array_keys(domo_language_names());
$langs = array_values(array_intersect(array_map('sanitize_key', (array) ($in['languages'] ?? [])), $valid));
$primary = sanitize_key($in['primary'] ?? '');
if ($primary && in_array($primary, $valid, true)) {
$langs = array_values(array_unique(array_merge([$primary], $langs)));
}
$max = domo_max_languages();
if (count($langs) > $max) {
/* translators: placeholders are filled in at runtime (numbers, names or messages). */
add_settings_error('domo_settings', 'langs', sprintf(__('Domopage supports up to %1$d languages per page. Only the first %2$d were kept.', 'domopage'), $max, $max));
$langs = array_slice($langs, 0, $max);
}
$out['languages'] = $langs ?: ['en'];
}
return apply_filters('domopage_sanitize_settings', $out, $in, $tab);
}
function domo_switch(string $name, bool $on, string $label, string $help = ''): void
{
printf(
'%s',
esc_attr($name),
checked($on, true, false),
esc_html($label),
$help ? '
' . esc_html($help) . '
' : ''
);
}
function domo_page_settings(): void
{
if (!current_user_can('manage_options')) {
return;
}
$s = domo_settings();
$tabs = apply_filters('domopage_settings_tabs', ['brand' => __('Brand', 'domopage'), 'languages' => __('Languages & form', 'domopage'), 'compliance' => __('Compliance', 'domopage'), 'ai' => __('AI', 'domopage')]);
$tabs['licence'] = __('Licence', 'domopage');
$tab = sanitize_key($_GET['tab'] ?? 'brand');
if (!isset($tabs[$tab])) {
$tab = 'brand';
}
?>
Settings.', 'domopage'), ['a' => ['href' => []]]), esc_url(admin_url('admin.php?page=domo-settings'))); ?>
get_error_message());
wp_safe_redirect($back);
exit;
}
$id = wp_insert_post(['post_type' => 'domo_property', 'post_status' => 'draft', 'post_title' => $d['name']], true);
if (is_wp_error($id)) {
domo_notice('error', $id->get_error_message());
wp_safe_redirect($back);
exit;
}
$d['source_title'] = $d['name'];
$primary = domo_primary_lang();
$d['copy'] = [$primary => domo_basic_copy($d)];
$messages = [];
if (!empty($_POST['ai_copy']) && domo_ai_ready()) {
$copy = domo_ai_copy($d, $primary);
if (is_wp_error($copy)) {
$messages[] = $copy->get_error_message() . ' ' . __('The listing text was used instead.', 'domopage');
} else {
$d['copy'][$primary] = $copy;
}
}
if (!empty($_POST['ai_translate']) && domo_ai_ready()) {
$err = domo_translate_all($d);
if ($err) {
$messages[] = $err;
}
}
domo_save_data($id, $d);
/* translators: placeholders are filled in at runtime (numbers, names or messages). */
domo_notice($messages ? 'warning' : 'success', $messages ? implode(' ', $messages) : sprintf(__('Imported %1$s with %2$d photos. Review it below, then publish.', 'domopage'), $d['name'], count($d['images'])));
wp_safe_redirect(admin_url('admin.php?page=domopage&post=' . $id));
exit;
});
/** Translate primary copy into every other enabled language. Returns an error string or ''. */
function domo_translate_all(array &$d, ?array $only = null): string
{
$primary = domo_primary_lang();
$base = $d['copy'][$primary] ?? domo_basic_copy($d);
$targets = array_slice(domo_settings()['languages'], 1);
if ($only !== null) {
$targets = array_values(array_intersect($targets, $only));
}
foreach ($targets as $lang) {
$t = domo_ai_translate($base, $d['facts'], $d['name'], $lang);
if (is_wp_error($t)) {
return $t->get_error_message();
}
$d['copy'][$lang] = $t;
}
return '';
}
/* ------------------------------------------------------------- Review page */
function domo_page_review(int $id): void
{
$post = get_post($id);
if (!$post || $post->post_type !== 'domo_property' || !current_user_can('edit_post', $id)) {
echo '' . esc_html__('Landing page not found.', 'domopage') . '
';
return;
}
$d = domo_get_data($id);
$s = domo_settings();
$langs = $s['languages'];
$names = domo_language_names();
$labels = [
'intro' => __('Hero introduction', 'domopage'),
'story_heading' => __('First section heading', 'domopage'),
'story_text' => __('First section text', 'domopage'),
'location_heading' => __('Second section heading', 'domopage'),
'location_text' => __('Second section text (leave empty to hide the section)', 'domopage'),
];
$published = $post->post_status === 'publish';
?>
post_title); ?>
isset($images[$i])));
$hero = isset($_POST['hero']) ? (int) $_POST['hero'] : ($keep[0] ?? 0);
if ($images && in_array($hero, $keep, true)) {
$keep = array_values(array_unique(array_merge([$hero], $keep)));
}
$d['images'] = array_map(fn($i) => $images[$i], $keep);
$d['images_all'] = $images;
// Copy per language.
$posted = isset($_POST['copy']) && is_array($_POST['copy']) ? map_deep(wp_unslash($_POST['copy']), 'sanitize_textarea_field') : [];
foreach ($langs as $n => $lang) {
if (!isset($posted[$lang]) || !is_array($posted[$lang])) {
continue;
}
$c = [];
foreach (DOMO_COPY_FIELDS as $f) {
$c[$f] = str_ends_with($f, '_heading') ? sanitize_text_field($posted[$lang][$f] ?? '') : sanitize_textarea_field($posted[$lang][$f] ?? '');
}
if ($n > 0) {
$c['facts'] = domo_lines($posted[$lang]['facts'] ?? '');
if (implode('', array_intersect_key($c, array_flip(DOMO_COPY_FIELDS))) === '') {
unset($d['copy'][$lang]);
continue;
}
}
$d['copy'][$lang] = $c;
}
$d = apply_filters('domopage_save_page', $d, $id);
$do = sanitize_key($_POST['do'] ?? 'save');
$type = 'success';
$msg = __('Saved.', 'domopage');
if ($do === 'ai_copy') {
$copy = domo_ai_copy($d, $langs[0]);
if (is_wp_error($copy)) {
[$type, $msg] = ['error', $copy->get_error_message()];
} else {
$d['copy'][$langs[0]] = $copy;
$msg = __('New copy written. Check it below, then translate again if you use other languages.', 'domopage');
}
} elseif ($do === 'translate_pick') {
// Languages chosen on the page: switch them on for the site (within the plan's limit), then translate.
$valid = array_keys(domo_language_names());
$pick = array_values(array_intersect(array_map('sanitize_key', (array) wp_unslash($_POST['translate_to'] ?? [])), $valid));
$pick = array_values(array_diff($pick, [$langs[0]]));
if (!$pick) {
[$type, $msg] = ['error', __('Tick at least one language to translate into.', 'domopage')];
} else {
$site = domo_settings();
$max = domo_max_languages();
$added = [];
foreach ($pick as $code) {
if (!in_array($code, $site['languages'], true) && count($site['languages']) < $max) {
$site['languages'][] = $code;
$added[] = domo_language_names()[$code];
}
}
if ($added) {
$saved = get_option('domo_settings', []);
$saved = is_array($saved) ? $saved : [];
$saved['languages'] = $site['languages'];
// Already validated above; skip the Settings-screen sanitizer, which only accepts a full form.
remove_all_filters('sanitize_option_domo_settings');
update_option('domo_settings', $saved);
$langs = $site['languages'];
}
$pick = array_values(array_intersect($pick, $langs));
$err = domo_translate_all($d, $pick);
if ($err) {
[$type, $msg] = ['error', $err];
} else {
$msg = __('Translations ready. Please check them before publishing.', 'domopage');
if ($added) {
/* translators: %s: list of languages. */
$msg .= ' ' . sprintf(__('Also switched on for your site: %s.', 'domopage'), implode(', ', $added));
}
if (count($pick) < count(array_unique(array_merge($pick, array_map('sanitize_key', (array) wp_unslash($_POST['translate_to'] ?? [])))))) {
$type = 'warning';
/* translators: %d: maximum number of languages. */
$msg .= ' ' . sprintf(__('Some languages were skipped because your plan allows %d.', 'domopage'), $max);
}
}
}
} elseif ($do === 'translate' || $do === 'translate_missing' || str_starts_with($do, 'translate_')) {
$only = null;
if ($do === 'translate_missing') {
$only = array_values(array_filter(array_slice($langs, 1), fn($l) => empty($d['copy'][$l])));
} elseif ($do !== 'translate') {
$only = [substr($do, 10)];
}
$err = domo_translate_all($d, $only);
[$type, $msg] = $err ? ['error', $err] : ['success', __('Translations updated. Please check them before publishing.', 'domopage')];
}
domo_save_data($id, $d);
$postarr = ['ID' => $id, 'post_title' => $d['name']];
if ($do === 'publish') {
$s = domo_settings();
if (!$s['privacy_url']) {
[$type, $msg] = ['error', __('Add your privacy policy URL in Settings → Languages & form before publishing. The form needs it for GDPR consent.', 'domopage')];
} else {
$postarr['post_status'] = 'publish';
$msg = __('Your landing page is live.', 'domopage');
if (domo_energy_missing($d)) {
$type = 'warning';
$msg .= ' ' . __('Note: the energy rating is required in property ads in your country and is not filled in.', 'domopage');
}
}
} elseif ($do === 'unpublish') {
$postarr['post_status'] = 'draft';
$msg = __('Page unpublished and saved as a draft.', 'domopage');
}
wp_update_post($postarr);
if (!apply_filters('domopage_keep_notice', false)) {
domo_notice($type, $msg);
}
wp_safe_redirect($back);
exit;
});
function domo_lines($text): array
{
return array_values(array_filter(array_map(fn($l) => sanitize_text_field($l), preg_split('/\r\n|\r|\n/', (string) $text)), fn($l) => $l !== ''));
}
// Simple columns on the Landing pages list.
add_filter('manage_domo_property_posts_columns', function ($cols) {
return ['cb' => $cols['cb'], 'domo_photo' => '', 'title' => __('Property', 'domopage'), 'domo_langs' => __('Languages', 'domopage'), 'date' => $cols['date']];
});
add_action('manage_domo_property_posts_custom_column', function ($col, $id) {
$d = domo_get_data((int) $id);
if ($col === 'domo_photo' && !empty($d['images'][0])) {
echo '
';
}
if ($col === 'domo_langs') {
echo esc_html(strtoupper(implode(' · ', array_keys($d['copy'] ?? []))));
}
}, 10, 2);
/** Licence status, with the key box one click away. */
function domo_licence_panel(): void
{
$fs = domo_fs();
$active = domo_is_pro();
?>
' . esc_html((string) $fs->get_plan_title()) . ''); ?>
is_premium()) : ?>