type = $field_options['type']; } else { throw new InvalidArgumentException( 'SiteOrigin_Widget_Field_Base::__construct: $field_options must contain a \'type\' field.' ); } $this->base_name = $base_name; $this->element_id = $element_id; $this->element_name = $element_name; $this->field_options = $field_options; $this->javascript_variables = array(); $this->for_widget = $for_widget; $this->parent_container = $parent_container; $this->init(); } private function init() { $this->init_options(); $this->initialize(); } /** * Initialization function which may be overridden if required. */ protected function initialize() { } /** * This method ensures that configuration options are set on the corresponding field class instance properties. If * a field has defined default options, those are set first and then can be overwritten by options which were * passed in. */ private function init_options() { // First set properties from default options if any have been set. $default_field_options = $this->get_default_options(); if( ! empty( $default_field_options ) ) { foreach ( $default_field_options as $key => $value ) { if ( property_exists( $this, $key ) ) { if ( isset( $default_field_options[$key] ) ) { $this->$key = $value; } } } } $field_options = $this->field_options; foreach ( $field_options as $key => $value ) { if( property_exists( $this, $key ) ) { if ( isset( $field_options[$key] ) ) { $this->$key = $value; } } } } protected function get_default_options() { //Stub: This function may be overridden by subclasses to have default field options. return null; } /** * The CSS classes to be applied to the default label. * This function should be overridden by subclasses when they want to add custom CSS classes to the HTML input label. * * @return array The array of label CSS classes. */ protected function get_label_classes( $value, $instance ) { return array( 'siteorigin-widget-field-label' ); } /** * The CSS classes to be applied to the default description. * This function should be overridden by subclasses when they want to add custom CSS classes to the description text. * * @return array The modified array of description text CSS classes. */ protected function get_description_classes() { return array( 'siteorigin-widget-description' ); } /** * This function is called by the containing SiteOrigin_Widget when rendering it's form. * * @param $value mixed The current instance value of the field. * @param $instance array Optionally pass in the widget instance, if rendering of additional values is required. */ public function render( $value, $instance = array() ) { if ( is_null( $value ) && isset( $this->default ) ) { $value = $this->default; } $wrapper_attributes = array( 'class' => array( 'siteorigin-widget-field', 'siteorigin-widget-field-type-' . $this->type, 'siteorigin-widget-field-' . $this->base_name ) ); if( !empty( $this->optional ) ) $wrapper_attributes['class'][] = 'siteorigin-widget-field-is-optional'; if( !empty( $this->required ) ) $wrapper_attributes['class'][] = 'siteorigin-widget-field-is-required'; $wrapper_attributes['class'] = implode(' ', array_map('sanitize_html_class', $wrapper_attributes['class']) ); if( !empty( $this->state_emitter ) ) { // State emitters create new states for the form $wrapper_attributes['data-state-emitter'] = json_encode( $this->state_emitter ); } if( !empty( $this->state_handler ) ) { // State handlers decide what to do with form states $wrapper_attributes['data-state-handler'] = json_encode( $this->state_handler ); } if( !empty( $this->state_handler_initial ) ) { // Initial state handlers are only run when the form is first loaded $wrapper_attributes['data-state-handler-initial'] = json_encode( $this->state_handler_initial ); } ?>