Commit | Line | Data |
---|---|---|
4bcd4c62 TO |
1 | <?php |
2 | /** | |
3 | * @file | |
4 | * | |
5 | * Determine default settings for WordPress. | |
6 | */ | |
7 | ||
8 | if (!defined('CIVI_SETUP')) { | |
9 | exit("Installation plugins must only be loaded by the installer.\n"); | |
10 | } | |
11 | ||
12 | \Civi\Setup::dispatcher() | |
13 | ->addListener('civi.setup.checkAuthorized', function (\Civi\Setup\Event\CheckAuthorizedEvent $e) { | |
14 | $model = $e->getModel(); | |
15 | if ($model->cms !== 'WordPress') { | |
16 | return; | |
17 | } | |
18 | ||
19 | \Civi\Setup::log()->info(sprintf('[%s] Handle %s', basename(__FILE__), 'checkAuthorized')); | |
20 | $e->setAuthorized(current_user_can('activate_plugins')); | |
21 | }); | |
22 | ||
23 | ||
24 | \Civi\Setup::dispatcher() | |
25 | ->addListener('civi.setup.init', function (\Civi\Setup\Event\InitEvent $e) { | |
26 | $model = $e->getModel(); | |
27 | if ($model->cms !== 'WordPress' || !function_exists('current_user_can')) { | |
28 | return; | |
29 | } | |
30 | \Civi\Setup::log()->info(sprintf('[%s] Handle %s', basename(__FILE__), 'init')); | |
31 | ||
32 | // Note: We know WP is bootstrapped, but we don't know if the `civicrm` plugin is active, | |
33 | // so we have to make an educated guess. | |
34 | $civicrmPluginFile = implode(DIRECTORY_SEPARATOR, [WP_PLUGIN_DIR, 'civicrm', 'civicrm.php']); | |
35 | ||
36 | // Compute settingsPath. | |
37 | $uploadDir = wp_upload_dir(); | |
38 | $preferredSettingsPath = $uploadDir['basedir'] . DIRECTORY_SEPARATOR . 'civicrm' . DIRECTORY_SEPARATOR . 'civicrm.settings.php'; | |
39 | $oldSettingsPath = plugin_dir_path($civicrmPluginFile) . 'civicrm.settings.php'; | |
40 | if (file_exists($preferredSettingsPath)) { | |
41 | $model->settingsPath = $preferredSettingsPath; | |
42 | } | |
43 | elseif (file_exists($oldSettingsPath)) { | |
44 | $model->settingsPath = $oldSettingsPath; | |
45 | } | |
46 | else { | |
47 | $model->settingsPath = $preferredSettingsPath; | |
48 | } | |
49 | ||
50 | $model->templateCompilePath = implode(DIRECTORY_SEPARATOR, [$uploadDir['basedir'], 'civicrm', 'templates_c']); | |
51 | ||
52 | // Compute DSN. | |
53 | $model->db = $model->cmsDb = array( | |
54 | 'server' => DB_HOST, | |
55 | 'username' => DB_USER, | |
56 | 'password' => DB_PASSWORD, | |
57 | 'database' => DB_NAME, | |
58 | ); | |
59 | ||
60 | // Compute URLs | |
61 | $model->cmsBaseUrl = site_url(); | |
62 | $model->paths['wp.frontend.base']['url'] = home_url() . '/'; | |
63 | $model->paths['wp.backend.base']['url'] = admin_url(); | |
64 | $model->mandatorySettings['userFrameworkResourceURL'] = plugin_dir_url($civicrmPluginFile) . 'civicrm'; | |
65 | ||
66 | // Compute default locale. | |
67 | $langs = $model->getField('lang', 'options'); | |
68 | $wpLang = get_locale(); | |
69 | $model->lang = isset($langs[$wpLang]) ? $wpLang : 'en_US'; | |
70 | }); |