widget_id = $widget_id; $this->widget_file = $widget_file; if ( ! $textdomain ) $textdomain = $widget_id; $this->textdomain = $textdomain; $this->load_textdomain(); $this->load_config(); // input can be 'checkbox', 'multiselect', 'select', 'short_text', 'text', 'textarea', 'hidden', or 'none' // datatype can be 'array' or 'hash' // can also specify input_attributes $this->config = apply_filters( $this->get_hook( 'config' ), $this->config ); if ( empty( $this->hook_prefix ) ) $this->hook_prefix = $this->widget_id; foreach ( $this->config as $key => $value ) $this->defaults[$key] = isset( $value['default'] ) ? $value['default'] : ''; $widget_ops = array( 'classname' => 'widget_' . $this->widget_id, 'description' => $this->description ); $widget_ops = apply_filters( $this->get_hook( 'widget_ops' ), $widget_ops ); $control_ops = apply_filters( $this->get_hook( 'control_ops' ), $control_ops ); $this->WP_Widget( $this->widget_id, $this->title, $widget_ops, $control_ops ); } /** * Outputs the widget * * Simply override this function if you want full control over widget. Otherwise, you can hook into just the body. * * @param array $args Widget args * @param array $instance Widget instance * @return void (Text is echoed.) */ public function widget( $args, $instance ) { extract( $args ); /* Settings */ $settings = array(); foreach ( array_keys( $this->config ) as $key ) $settings[$key] = apply_filters( $this->get_hook( 'config_item_'.$key ), $instance[$key], $this ); $title = $settings['title']; echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; $this->widget_body( $args, $instance, $settings ); echo $after_widget; } /** * Save and validate updates to widget values * * @param array $new_instance New instance * @param array $old_instance Old instance * @return array Updated instance */ public function update( $new_instance, $old_instance ) { $instance = $old_instance; foreach ( array_keys( $this->config ) as $key ) $instance[$key] = isset( $new_instance[$key] ) ? $new_instance[$key] : ''; return $this->validate( $instance ); } /** * Draws the widget input form * @param array $instance Widget instance * @param array|null $exclude_options (optional) The options that should not be drawn in the form. * @return void */ public function form( $instance, $exclude_options = null ) { $exclude_options = apply_filters( $this->get_hook( 'excluded_form_options' ), $exclude_options ); $instance = wp_parse_args( (array) $instance, $this->defaults ); $i = $j = 0; foreach ( $instance as $opt => $value ) { if ( $opt == 'submit' || in_array( $opt, (array) $exclude_options ) ) continue; foreach ( array( 'datatype', 'default', 'help', 'input', 'input_attributes', 'label', 'no_wrap', 'options' ) as $attrib ) { if ( ! isset( $this->config[$opt][$attrib] ) ) $this->config[$opt][$attrib] = ''; } $input = $this->config[$opt]['input']; $label = $this->config[$opt]['label']; if ( $input == 'none' ) { if ( $opt == 'more' ) { $i++; $j++; // echo "
$label
"; echo "

"; echo "
"; } elseif ( $opt == 'endmore' ) { $j--; echo '
'; } continue; } if ( $input == 'multiselect' ) { // Do nothing since it needs the values as an array $value = (array) $value; } elseif ( $this->config[$opt]['datatype'] == 'array' ) { if ( ! is_array( $value ) ) $value = ''; else $value = implode( ( 'textarea' == $input ? "\n" : ', ' ), $value ); } elseif ( $this->config[$opt]['datatype'] == 'hash' ) { if ( ! is_array( $value ) ) $value = ''; else { $new_value = ''; foreach ( $value AS $shortcut => $replacement ) $new_value .= "$shortcut => $replacement\n"; $value = $new_value; } } echo "

"; $input_id = $this->get_field_id( $opt ); $input_name = $this->get_field_name( $opt ); if ( $input == 'multiselect' ) $input_name .= '[]'; $attribs = "name='$input_name' id='$input_id' " . $this->config[$opt]['input_attributes']; if ( $label && ( $input != 'multiselect' ) ) echo " "; if ( $input == '' ) { } elseif ( $input == 'textarea' ) { echo "'; } elseif ( $input == 'select' ) { echo ""; } elseif ( $input == 'multiselect' ) { echo '

'; if ( $label ) echo "$label: "; foreach ( (array) $this->config[$opt]['options'] as $sopt ) echo "$sopt
"; echo '
'; } elseif ( $input == 'checkbox' ) { echo ""; } else { if ( $input == 'short_text' ) { $tclass = ''; $tstyle = 'width:25px;'; } else { $tclass = 'widefat'; $tstyle = ''; } echo ""; } if ( $this->config[$opt]['help'] ) echo "
{$this->config[$opt]['help']}"; echo "

\n"; } // Close any open divs for ( ; $j > 0; $j-- ) { echo ''; } } /** * Loads the localization textdomain for the widget. * * @return void */ public function load_textdomain() { $subdir = '/lang'; load_plugin_textdomain( $this->textdomain, false, basename( dirname( $this->widget_file ) ) . $subdir ); } /** * Returns the full plugin-specific name for a hook. * * @param string $hook The name of a hook, to be made plugin-specific. * @return string The plugin-specific version of the hook name. */ public function get_hook( $hook ) { return $this->hook_prefix . '_' . $hook; } /** * Initializes the plugin's configuration and localizable text variables. * * MUST Be OVERRIDDEN IN SUB-CLASS * * Two class variables containing localized strings should be set in this function, in addition to the config array. * * e.g. * $this->title = __('My Plugin Widget', $this->textdomain); * $this->description => __('Description of this widget.', $this->textdomain); * $this->config = array( ... ); * * @return void */ public function load_config() { die( 'Function load_config() must be overridden in sub-class.' ); } /** * Outputs the body of the widget * * MUST BE OVERRIDDEN IN SUB-CLASS * * @param array $args Widget args * @param array $instance Widget instance * @param array $settings Widget settings * @return void (Text is echoed.) */ public function widget_body( $args, $instance, $settings ) { die( 'Function widget_body() must be overridden in sub-class.' ); } /** * Validates widget instance values * * Intended to be overridden by sub-class, if needed. * * @param array $instance Array of widget instance values * @return array The filtered array of widget instance values */ public function validate( $instance ) { return $instance; } } // end class C2C_Widget endif; // end if !class_exists() ?>