/** * Create a term field instance. * * @param string $field_slug Slug of existing field definition. * @param int $term_id ID of the term where the field belongs. * * @return null|WPCF_Field_Instance Field instance or null if an error occurs. * @since 1.9 */ public static function create_term_field_instance($field_slug, $term_id) { try { return new WPCF_Field_Instance(WPCF_Field_Term_Definition_Factory::load($field_slug), $term_id); } catch (Exception $e) { return null; } }
/** * Shortcut to load_field_definition(). * * @param string $field_key * @return null|WPCF_Field_Definition */ static function load($field_key) { // we cannot use self::get_instance here, because of low PHP requirements and missing get_called_class function // we have a fallback class for get_called_class but that scans files by debug_backtrace and return 'self' // instead of WPCF_Field_Term_Definition_Factory like the original get_called_class() function does // ends in an error because of parents (abstract) $var = new self(); return WPCF_Field_Term_Definition_Factory::get_instance()->load_field_definition($field_key); }
private function process_bulk_action() { $action = $this->current_action(); if (false == $action) { return; } if (!wp_verify_nonce(wpcf_getpost('_wpnonce'), WPCF_Page_Control_Termmeta::BULK_ACTION_NONCE)) { wp_die(__('Invalid nonce.', 'wpcf')); } $selected_field_definitions = wpcf_getpost(self::INPUT_SLUGS, array()); if (is_string($selected_field_definitions)) { $selected_field_definitions = array($selected_field_definitions); } if (!is_array($selected_field_definitions) || empty($selected_field_definitions)) { // Nothing to do here return; } $factory = WPCF_Field_Term_Definition_Factory::get_instance(); switch ($action) { case self::BULK_ACTION_ADD_TO_GROUP: $group_ids = $this->read_group_ids(); foreach ($group_ids as $group_id) { wpcf_admin_fields_save_group_fields($group_id, $selected_field_definitions, true, WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION); } break; case self::BULK_ACTION_REMOVE_FROM_TO_GROUP: $group_ids = $this->read_group_ids(); foreach ($group_ids as $group_id) { wpcf_admin_fields_remove_field_from_group_bulk($group_id, $selected_field_definitions); } break; case self::BULK_ACTION_CHANGE_TYPE: $field_type_slug = wpcf_getpost('wpcf-id'); if (!empty($field_type_slug)) { wpcf_admin_custom_fields_change_type($selected_field_definitions, $field_type_slug, WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION); } break; case self::BULK_ACTION_ACTIVATE: $fields = wpcf_admin_fields_get_fields(false, true, false, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION); $fields_bulk = wpcf_types_cf_under_control('add', array('fields' => $selected_field_definitions), WPCF_Field_Group_Term::POST_TYPE, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION); foreach ($fields_bulk as $field_id) { if (isset($fields[$field_id])) { $fields[$field_id]['data']['disabled'] = 0; } wpcf_admin_message(sprintf(__('Added to Types control: %s', 'wpcf'), esc_html($field_id)), 'updated', 'echo'); } wpcf_admin_fields_save_fields($fields, false, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION); break; case self::BULK_ACTION_DEACTIVATE: $fields = wpcf_admin_fields_get_fields(false, true, false, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION); foreach ($selected_field_definitions as $field_id) { $field_id = sanitize_text_field($field_id); if (isset($fields[$field_id])) { $fields[$field_id]['data']['disabled'] = 1; wpcf_admin_message(sprintf(__('Removed from Types control: %s', 'wpcf'), $fields[$field_id]['name']), 'updated', 'echo'); } } wpcf_admin_fields_save_fields($fields, false, WPCF_Field_Term_Definition_Factory::FIELD_DEFINITIONS_OPTION); break; case self::BULK_ACTION_DELETE: $failed = array(); $success = array(); foreach ($selected_field_definitions as $field_id) { $field_id = sanitize_text_field($field_id); // Permanently single field definition and field data. $field_definition = $factory->load_field_definition($field_id); if (null == $field_definition) { $response = false; } else { $response = $factory->delete_definition($field_definition); } if (!$response) { $failed[] = str_replace('_' . md5('wpcf_not_controlled'), '', $field_id); } else { $success[] = $field_id; } } if (!empty($success)) { wpcf_admin_message(sprintf(__('Fields %s have been deleted.', 'wpcf'), esc_html(implode(', ', $success))), 'updated', 'echo'); } if (!empty($failed)) { wpcf_admin_message(sprintf(__('Fields %s are not Types fields. Types wont delete these fields.', 'wpcf'), esc_html(implode(', ', $failed))), 'error', 'echo'); } break; } // We made changes to field definitions and now the listing table is going to be rendered. $factory->clear_definition_storage(); }
/** * @return WPCF_Field_Definition_Factory Field definition factory of the correct type. */ protected function get_field_definition_factory() { return WPCF_Field_Term_Definition_Factory::get_instance(); }
/** * Render single cell in a term listing table. * * Catch field columns by their name prefix and render field values with preview renderer. * * @param mixed $value "" * @param string $column_name * @param int $term_id * @link https://make.wordpress.org/docs/plugin-developer-handbook/10-plugin-components/custom-list-table-columns/ * @return string Rendered HTML with the table cell content. * @since 1.9.1 */ public function manage_term_listing_cell($value, $column_name, $term_id) { // Deal only with our custom columns. $is_term_field_cell = substr($column_name, 0, strlen(self::LISTING_COLUMN_PREFIX)) == self::LISTING_COLUMN_PREFIX; if ($is_term_field_cell) { try { $field_slug = substr($column_name, strlen(self::LISTING_COLUMN_PREFIX)); $field_definition = WPCF_Field_Term_Definition_Factory::load($field_slug); $field = new WPCF_Field_Instance_Term($field_definition, $term_id); $renderer_args = array('maximum_item_count' => 5, 'maximum_item_length' => 30, 'maximum_total_length' => 100); $renderer = WPCF_Field_Renderer_Factory::get_instance()->create_preview_renderer($field, $renderer_args); $value = $renderer->render(); } catch (Exception $e) { // Do nothing when we're unable to load the field. } } return $value; }