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 ); } ?>
$attr_val ) echo $attr.'="' . esc_attr( $attr_val ) . '" ' ?>>render_before_field( $value, $instance ); $this->render_field( $value, array() ); $this->render_after_field( $value, $instance); ?>
render_field_label( $value, $instance ); } /** * Default label rendering implementation. Subclasses should override if necessary to render labels differently. */ protected function render_field_label( $value, $instance ) { ?> class=""sanitize_field_input( $value, $instance ); if( isset( $this->sanitize ) ) { // This field also needs some custom sanitization switch( $this->sanitize ) { case 'url': $value = sow_esc_url_raw( $value ); break; case 'email': $value = sanitize_email( $value ); break; default: // This isn't a built in sanitization. Maybe it's handled elsewhere. if( is_callable( $this->sanitize ) ) { $value = call_user_func( $this->sanitize, $value, $old_value ); } else if( is_string( $this->sanitize ) ) { $value = apply_filters( 'siteorigin_widgets_sanitize_field_' . $this->sanitize, $value ); } break; } } return $value; } /** * This function is called after the main render function. * * @param $value mixed The current value of this field. * @param $instance array The current widget instance. */ protected function render_after_field( $value, $instance ) { $this->render_field_description(); } /** * Default description rendering implementation. Subclasses should override if necessary to render descriptions * differently. */ protected function render_field_description() { if( ! empty( $this->description ) ) { ?>
render_CSS_classes( $this->get_description_classes() ) ?>>description ) ?>
javascript_variables; } /** * Some more complex fields may require some JavaScript in the front end. Enqueue them here. */ public function enqueue_scripts() { } public function __get( $name ) { if ( isset( $this->$name ) ) { return $this->$name; } } }