/** * @inheritdoc * * todo document * * @param array $arguments */ public function process_call($arguments) { $am = $this->get_am(); $am->ajax_begin(array('nonce' => $am->get_action_js_name(Types_Ajax::CALLBACK_FIELD_CONTROL_ACTION))); // Read and validate input $field_action = sanitize_text_field(wpcf_getpost('field_action')); $fields = wpcf_getpost('fields'); // array of values, will be sanitized when processed $current_domain = wpcf_getpost('domain', null, Types_Field_Utils::get_domains()); if (null == $current_domain) { $am->ajax_finish(array('message' => __('Wrong field domain.', 'wpcf')), false); } if (!is_array($fields) || empty($fields)) { $am->ajax_finish(array('message' => __('No fields have been selected.', 'wpcf')), false); } // will be sanitized when/if used by the action-specific method $action_specific_data = wpcf_getpost('action_specific', array()); // Process fields one by one $errors = array(); $results = array(); foreach ($fields as $field) { $result = $this->single_field_control_action($field_action, $field, $current_domain, $action_specific_data); if (is_array($result)) { // Array of errors $errors = array_merge($errors, $result); } else { if ($result instanceof WP_Error) { // Single error $errors[] = $result; } else { if (false == $result) { // This should not happen...! $errors[] = new WP_Error(0, __('An unexpected error happened while processing the request.', 'wpcf')); } else { // Success // Save the field definition model as a result if we got a whole definition if ($result instanceof WPCF_Field_Definition) { $result = $result->to_json(); } $results[wpcf_getarr($field, 'slug')] = $result; } } } } $data = array('results' => $results); $is_success = empty($errors); if (!$is_success) { $error_messages = array(); /** @var WP_Error $error */ foreach ($errors as $error) { $error_messages[] = $error->get_error_message(); } $data['messages'] = $error_messages; } $am->ajax_finish($data, $is_success); }
/** * Query field groups from a single domain. * * @param string $domain One of the valid field domains. Legacy "meta type" values will be also accepted. * @param array $query Query arguments for Types_Field_Group_Factory::query_groups(). * * @return null|Types_Field_Group[] Array of field groups or null on error. * @since m2m */ private function query_specific_domain($domain, $query) { // Make sure we have a valid domain string. $valid_domains = Types_Field_Utils::get_domains(); if (!in_array($domain, $valid_domains)) { $domain = Types_Field_Utils::legacy_meta_type_to_domain($domain); } // Pass the group query to the proper factory class. try { $group_factory = Types_Field_Group_Factory::get_factory_by_domain($domain); $groups = $group_factory->query_groups($query); } catch (Exception $e) { // We don't care, it's a failure. return null; } return $groups; }
/** * Hook for types_filter_query_field_definitions. * * @param mixed $ignored * @param array $query Field definition query. See Types_Field_Definition_Factory::query() for supported arguments. * Additionally, you can specify: * - 'domain': A single field domain (see Types_Field_Utils) or 'all'. Legacy domain names are also accepted. * For 'all', the method returns a multidimensional arrays with results for individual domains: * - 'refresh': A boolean to refresh the definitions, useful when getting data after saving fields * array( 'posts' => array( ... ), 'users' => array( ... ), ... ). * * @note The 'refresh' parameter is temporal and might dissapear without prior notice when the groups and fields saving gets integrated in the fields definition factory * * @return null|array Field definition arrays, sanitized as per field type, or null if an error has occurred. * @since 2.1 */ public function query_field_definitions($ignored, $query) { $domain = wpcf_getarr($query, 'domain', 'all'); if ('all' == $domain) { // Call itself for each available domain. $results_by_domain = array(); $domains = Types_Field_Utils::get_domains(); foreach ($domains as $field_domain) { $per_domain_query = $query; $per_domain_query['domain'] = $field_domain; $results_by_domain[$field_domain] = $this->query_field_definitions(null, $per_domain_query); } return $results_by_domain; } else { // Sanitize input if (!is_string($domain) || !is_array($query)) { return null; } // Get the factory by domain, and if it fails, try to convert from legacy meta_type value. try { $definition_factory = Types_Field_Utils::get_definition_factory_by_domain($domain); } catch (InvalidArgumentException $e) { $definition_factory = null; } if (null == $definition_factory) { try { $definition_factory = Types_Field_Utils::get_definition_factory_by_domain(Types_Field_Utils::legacy_meta_type_to_domain($domain)); } catch (InvalidArgumentException $e) { return null; } } // Allways query only Types fields. $query['filter'] = 'types'; if (isset($query['refresh']) && $query['refresh']) { $definition_factory->clear_definition_storage(); } /** @var WPCF_Field_Definition[] $definitions */ $definitions = $definition_factory->query_definitions($query); $definition_arrays = array(); foreach ($definitions as $definition) { $definition_arrays[] = $definition->get_definition_array(); } return $definition_arrays; } }
/** * Current field domain. * * @return string|null * @since 2.0 */ private function get_current_domain() { if (null == $this->current_domain) { $this->current_domain = wpcf_getget(self::PARAM_DOMAIN, null, Types_Field_Utils::get_domains()); } return $this->current_domain; }