widget_slug = 'aiopod-widget-tags'; $this->fields = $this->get_fields(); $this->defaults = $this->get_defaults(); parent::__construct( $this->widget_slug, __( 'AIOPOD - Tags', 'aio-podcast-studio' ), array( 'classname' => $this->widget_slug, 'description' => __( 'Display a list of your episode tags.', 'aio-podcast-studio' ) ) ); } /** * Outputs the content of the widget. * * @since 1.0.0 * @param array $args The array of form elements. * @param array $instance The current instance of the widget. */ public function widget( $args, $instance ) { // Merge incoming $instance array with $defaults if ( count( $instance ) ) { $attributes = array_merge( $this->defaults, $instance ); } else { $attributes = $this->defaults; } $attributes = $this->resolve_attributes( $attributes ); $widget_args = $args; // Enqueue dependencies wp_enqueue_style( AIOPOD_PLUGIN_SLUG . '-public' ); wp_enqueue_script( AIOPOD_PLUGIN_SLUG . '-public' ); // Process output echo wp_kses_post( $widget_args['before_widget'] ); if ( ! empty( $instance['title'] ) ) { echo wp_kses_post( $widget_args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $widget_args['after_title'] ); } include apply_filters( 'aiopod_load_template', AIOPOD_PLUGIN_DIR . 'public/templates/tags/tags-' . sanitize_file_name( $attributes['layout'] ) . '.php', $attributes ); echo wp_kses_post( $widget_args['after_widget'] ); } /** * Processes the widget's options to be saved. * * @since 1.0.0 * @param array $new_instance The new instance of values to be generated via the update. * @param array $old_instance The previous instance of values before the update. */ public function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['title'] = isset( $new_instance['title'] ) ? strip_tags( $new_instance['title'] ) : ''; foreach ( $this->fields['tags']['sections'] as $section ) { foreach ( $section['fields'] as $field ) { $field_name = $field['name']; if ( 'number' === $field['type'] ) { if ( ! empty( $new_instance[ $field_name ] ) ) { $instance[ $field_name ] = ( false === strpos( $new_instance[ $field_name ], '.' ) ) ? (int) $new_instance[ $field_name ] : (float) $new_instance[ $field_name ]; } else { $instance[ $field_name ] = 0; } } elseif ( 'url' === $field['type'] ) { $instance[ $field_name ] = ! empty( $new_instance[ $field_name ] ) ? aiopod_sanitize_url( $new_instance[ $field_name ] ) : ''; } elseif ( 'checkbox' === $field['type'] ) { $instance[ $field_name ] = isset( $new_instance[ $field_name ] ) ? (int) $new_instance[ $field_name ] : 0; } else { $instance[ $field_name ] = ! empty( $new_instance[ $field_name ] ) ? sanitize_text_field( $new_instance[ $field_name ] ) : ''; } } } return $instance; } /** * Generates the administration form for the widget. * * @since 1.0.0 * @param array $instance The array of keys and values for the widget. */ public function form( $instance ) { // Parse incoming $instance into an array and merge it with $defaults $instance = wp_parse_args( (array) $instance, $this->defaults ); // Display the admin form include AIOPOD_PLUGIN_DIR . 'widgets/forms/tags.php'; } /** * Resolves attributes into a valid, final configuration. * * This method guarantees that the returned attributes array is safe, * consistent, and ready for query building and rendering. * * @since 1.0.0 * @param array $attributes Raw attributes merged from defaults and user input. * @return array Fully resolved attributes array. */ private function resolve_attributes( $attributes ) { // Title $attributes['title'] = ''; // Layout if ( ! in_array( $attributes['layout'], array( 'cloud', 'list', 'dropdown' ) ) ) { $attributes['layout'] = 'cloud'; } // Return resolved array return $attributes; } /** * Get the form fields. * * @since 1.0.0 * @access private * @return array An associative array of fields. */ private function get_fields() { if ( empty( $this->fields ) ) { $this->fields = aiopod_get_shortcode_fields(); } return $this->fields; } /** * Get the default attribute values. * * @since 1.0.0 * @access private * @return array An associative array of attributes. */ private function get_defaults() { if ( empty( $this->defaults ) ) { foreach ( $this->fields['tags']['sections'] as $section ) { foreach ( $section['fields'] as $field ) { $this->defaults[ $field['name'] ] = $field['value']; } } $this->defaults['source'] = 'tags'; $this->defaults['title'] = __( 'Tags', 'aio-podcast-studio' ); $this->defaults['include'] = ''; $this->defaults['exclude'] = ''; $this->defaults['hide_if_empty'] = 0; } return $this->defaults; } }