public function initialize_new_domain($domain_name, $region = false)
 {
     if ($this->domain_exists($domain_name, $region)) {
         return new WP_Error('domain_exists', 'There was an error creating the domain.  The domain already exists.');
     }
     if (is_wp_error($error = $this->config_api->CreateDomain($domain_name, $region))) {
         return $error;
     }
     Lift_Search::set_search_domain_name($domain_name);
     Lift_Search::set_domain_region($region);
     TAE_Async_Event::WatchWhen(array($this, 'domain_is_created'), array($domain_name, $region), 60, 'lift_domain_created_' . $domain_name)->then(array($this, 'apply_schema'), array($domain_name, null, null, $region), true)->then(array($this, 'apply_access_policy'), array($domain_name, false, $region), true)->commit();
     return true;
 }
Exemplo n.º 2
0
 public function action__wp_ajax_lift_setting()
 {
     if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['model'])) {
         $settings_data = json_decode(stripslashes($_POST['model']));
         $response = array('status' => 'SUCCESS', 'data' => array(), 'errors' => array());
         $setting_value = $settings_data->value;
         $response['model']['id'] = $setting_key = $settings_data->id;
         $error = new WP_Error();
         if (isset($_GET['nonce']) && wp_verify_nonce($_GET['nonce'], 'lift_setting')) {
             switch ($setting_key) {
                 case 'credentials':
                     if ('' === $setting_value->accessKey && '' === $setting_value->secretKey) {
                         //empty values are used to reset
                         Lift_Search::set_access_key_id('');
                         Lift_Search::set_secret_access_key('');
                     } else {
                         $result = $this->test_credentials($setting_value->accessKey, $setting_value->secretKey);
                         if ($result['error']) {
                             $error->add('invalid_credentials', $result['message']);
                         } else {
                             Lift_Search::set_access_key_id($setting_value->accessKey);
                             Lift_Search::set_secret_access_key($setting_value->secretKey);
                         }
                     }
                     $response['model']['value'] = array('accessKey' => Lift_Search::get_access_key_id(), 'secretKey' => Lift_Search::get_secret_access_key());
                     break;
                 case 'batch_interval':
                     $value = max(array(1, intval($setting_value->value)));
                     $unit = $setting_value->unit;
                     Lift_Search::set_batch_interval_display($value, $unit);
                     $response['model']['value'] = Lift_Search::get_batch_interval_display();
                     break;
                 case 'domainname':
                     $domain_manager = Lift_Search::get_domain_manager();
                     $replacing_domain = Lift_Search::get_search_domain_name() != $setting_value;
                     $region = !empty($settings_data->region) ? $settings_data->region : false;
                     if ($setting_value === '') {
                         //assume that empty domain name means that we're clearing the set domain
                         Lift_Search::set_search_domain_name('');
                         Lift_Batch_Handler::_deactivation_cleanup();
                         $response['model']['value'] = '';
                     } elseif ($domain_manager->domain_exists($setting_value, $region)) {
                         $changed_fields = array();
                         if (!is_wp_error($result = $domain_manager->apply_schema($setting_value, null, $changed_fields, $region))) {
                             if ($replacing_domain) {
                                 Lift_Batch_Handler::queue_all();
                                 Lift_Batch_Handler::enable_cron();
                             }
                             Lift_Search::set_search_domain_name($setting_value);
                             Lift_Search::set_domain_region($region);
                         } else {
                             $error->add('schema_error', 'There was an error while applying the schema to the domain.');
                         }
                     } else {
                         $error->add('invalid_domain', 'The given domain does not exist.');
                     }
                     $response['model']['value'] = Lift_Search::get_search_domain_name();
                     break;
                 case 'region':
                     $response['model']['value'] = Lift_Search::get_domain_region();
                     break;
                 case 'next_sync':
                     //for now just assume that anh post for next_sync is to fire sync immediately
                     Lift_Batch_Handler::disable_cron();
                     Lift_Batch_Handler::enable_cron(time());
                     break;
                 case 'override_search':
                     Lift_Search::set_override_search((bool) $setting_value);
                     $response['model']['value'] = Lift_Search::get_override_search();
                     break;
                 default:
                     $error->add('invalid_setting', 'The name of the setting you are trying to set is invalid.');
                     break;
             }
         } else {
             $error->add('invalid_nonce', 'The request was missing required authentication data.');
         }
         if (count($error->get_error_codes())) {
             foreach ($error->get_error_codes() as $code) {
                 $response['errors'][] = array('code' => $code, 'message' => $error->get_error_message($code));
             }
             status_header(400);
             header('Content-Type: application/json');
             $response['status'] = 'FAILURE';
         }
         die(json_encode($response));
     }
 }