{"action":"opened","issue":{"url":"https://api.github.com/repos/Codestar/codestar-framework/issues/169","labels_url":"https://api.github.com/repos/Codestar/codestar-framework/issues/169/labels{/name}","comments_url":"https://api.github.com/repos/Codestar/codestar-framework/issues/169/comments","events_url":"https://api.github.com/repos/Codestar/codestar-framework/issues/169/events","html_url":"https://github.com/Codestar/codestar-framework/issues/169","id":117268647,"number":169,"title":"Base class for using framework fields in widget","user":{"login":"DeathMark","id":8908968,"avatar_url":"https://avatars.githubusercontent.com/u/8908968?v=3","gravatar_id":"","url":"https://api.github.com/users/DeathMark","html_url":"https://github.com/DeathMark","followers_url":"https://api.github.com/users/DeathMark/followers","following_url":"https://api.github.com/users/DeathMark/following{/other_user}","gists_url":"https://api.github.com/users/DeathMark/gists{/gist_id}","starred_url":"https://api.github.com/users/DeathMark/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DeathMark/subscriptions","organizations_url":"https://api.github.com/users/DeathMark/orgs","repos_url":"https://api.github.com/users/DeathMark/repos","events_url":"https://api.github.com/users/DeathMark/events{/privacy}","received_events_url":"https://api.github.com/users/DeathMark/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-11-17T02:31:42Z","updated_at":"2015-11-17T02:31:42Z","closed_at":null,"body":"Hi,\r\n\r\nFor anyone who want to use framework fields in widget, I have created a base class that help you render form and update form values based on your defined options. It works with group field as well. All you need is override method **get_options()**\r\n\r\n```php\r\n<?php\r\n/**\r\n * Base class for widget using codestar framework fields.\r\n * Created by: DeathMark (https://github.com/DeathMark)\r\n */\r\n\r\ndefined('ABSPATH') or die;\r\n\r\nclass CS_Widget extends WP_Widget {\r\n\tfunction get_options() {\r\n\t\treturn array();\r\n\t}\r\n\t\r\n\tfunction update($new_instance, $old_instance) {\r\n\t\t$instance\t= $old_instance;\r\n\t\t$options\t= $this->get_options();\r\n\r\n\t\tforeach ($options as $option) {\r\n\t\t\tif (isset($new_instance[$option['id']])) {\r\n\t\t\t\t$instance[$option['id']]\t= $new_instance[$option['id']];\r\n\t\t\t} else if (in_array($option['type'], array('group', 'checkbox'))) {\r\n\t\t\t\t$instance[$option['id']]\t= '';\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\treturn $instance;\r\n\t}\r\n\r\n\tfunction form($instance) {\r\n\t\t$options\t\t\t= $this->get_options();\r\n\r\n\t\t$default_options\t= array();\r\n\r\n\t\tforeach ($options as $option) {\r\n\t\t\t$default_options[$option['id']]\t\t= isset($option['default']) ? $option['default'] : '';\r\n\t\t}\r\n\r\n\t\t$instance\t= wp_parse_args((array) $instance, $default_options);\r\n\r\n\t\t// render fields.\r\n\t\tforeach ($options as $option) {\r\n\t\t\t$id\t\t\t\t= $option['id'];\r\n\t\t\t$unique\t\t\t= '';\r\n\r\n\t\t\tif (!in_array($option['type'], array('group'))) {\r\n\t\t\t\t$option['id']\t= $this->get_field_id($id);\r\n\t\t\t\t$option['name']\t= $this->get_field_name($id);\r\n\t\t\t} else {\r\n\t\t\t\t$unique\t\t\t= 'widget-' . $this->id_base . '[' . $this->number . ']';\r\n\t\t\t}\r\n\r\n\t\t\techo cs_add_element($option, is_array($instance[$id]) ? $instance[$id] : esc_attr($instance[$id]), $unique);\r\n\t\t}\r\n\t}\r\n}\r\n```"}}