<?php
/**
 * Imports the pages of this theme into WordPress as ONYX Builder pages, so they can be edited.
 * Needs the ONYX Builder plugin (1.5.4 or newer). The pages come from onyx-kit.zip in this theme.
 */
if (!defined('ABSPATH')) exit;

function onyx_export_kit_file() { return get_template_directory() . '/onyx-kit.zip'; }

function onyx_export_run_import() {
  if (!function_exists('ob_kit_import') || !is_file(onyx_export_kit_file())) return new WP_Error('onyx', 'ONYX Builder is not active.');
  if (function_exists('set_time_limit')) @set_time_limit(0);
  $r = ob_kit_import(onyx_export_kit_file(), ['mode' => 'replace', 'settings' => true, 'front' => true]);
  if (!is_wp_error($r)) update_option('onyx_export_imported', time(), false);
  return $r;
}

// the moment the theme is activated, import (when the plugin is already active)
add_action('after_switch_theme', function () {
  if (get_option('onyx_export_imported') || !function_exists('ob_kit_import')) return;
  onyx_export_run_import();
});

add_action('admin_post_onyx_export_import', function () {
  check_admin_referer('onyx_export_import');
  if (!current_user_can('manage_options')) wp_die('Not allowed.', 403);
  $r = onyx_export_run_import();
  set_transient('onyx_export_msg', is_wp_error($r) ? $r->get_error_message() : sprintf('Done: %d pages created, %d updated, %d images added. You can edit them with ONYX Builder now.', $r['created'], $r['updated'], $r['media']), 120);
  wp_safe_redirect(function_exists('ob_kit_import') && !is_wp_error($r) ? admin_url('admin.php?page=onyx-builder-home') : admin_url());
  exit;
});

add_action('admin_notices', function () {
  if (!current_user_can('manage_options')) return;
  if ($m = get_transient('onyx_export_msg')) { delete_transient('onyx_export_msg'); echo '<div class="notice notice-info"><p>' . esc_html($m) . '</p></div>'; }
  if (!is_file(onyx_export_kit_file())) return;
  $done = get_option('onyx_export_imported');
  if ($done && function_exists('ob_kit_import')) return; // already imported: no reminder
  if (function_exists('ob_kit_import')) {
    echo '<div class="notice notice-warning"><p><strong>' . ($done ? 'Pages of this theme are in ONYX Builder.' : 'Make the pages of this theme editable.') . '</strong> '
      . ($done ? 'Import again only if you want to reset them to the exported version (your changes to those pages would be replaced).' : 'Import them into WordPress as ONYX Builder pages (images go to the Media Library).') . '</p>'
      . '<p><a class="button button-primary" onclick="return confirm(\'' . ($done ? 'Replace the pages with the exported version?' : 'Import the pages now?') . '\')" href="' . esc_url(wp_nonce_url(admin_url('admin-post.php?action=onyx_export_import'), 'onyx_export_import')) . '">' . ($done ? 'Import again' : 'Import pages into ONYX Builder') . '</a></p></div>';
  } elseif (!$done) {
    echo '<div class="notice notice-warning"><p><strong>To edit the pages of this theme, install and activate the ONYX Builder plugin.</strong> Then come back here: the pages are imported automatically when you press the button that appears, or when you activate this theme again. Without the plugin the pages stay as fixed (static) pages.</p></div>';
  }
});
