A_Z_Widget

Definition for the AZ_Widget which displays alphabetically-ordered list of latin letters linking to the A-Z Listing page.


Description


Source

File: widgets/class-a-z-widget.php

class A_Z_Widget extends WP_Widget {
	/**
	 * Register the widget's meta information.
	 *
	 * @since 0.1
	 */
	function __construct() {
		parent::__construct('bh_az_widget', __( 'A-Z Site Map', 'a-z-listing' ), array(
			'classname' => 'a-z-listing-widget',
			'description' => __( 'Alphabetised links to the A-Z site map', 'a-z-listing' ),
		));

		if ( is_active_widget( false, false, $this->id_base, true ) ) {
			a_z_listing_add_styling();
		}
	}

	/**
	 * Deprecated constructor.
	 *
	 * @since 0.1
	 */
	function A_Z_Widget() {
		$this->__construct();
	}

	/**
	 * Print-out the configuration form for the widget.
	 *
	 * @since 0.1
	 * @param  Array $instance Widget instance as provided by WordPress core.
	 * @return void
	 */
	function form( $instance ) {
		$title = $instance['title'];
		$title_id = $this->get_field_id( 'title' );
		$title_name = $this->get_field_name( 'title' );

		$post = isset( $instance['post'] ) ? $instance['post'] : ( isset( $instance['page'] ) ? $instance['page'] : 0 );
		$post_id = $this->get_field_id( 'post' );
		$post_name = $this->get_field_name( 'post' );

		$post_type = isset( $instance['post_type'] ) ? $instance['post_type'] : 'page';
		$post_type_id = $this->get_field_id( 'post_type' );
		$post_type_name = $this->get_field_name( 'post_type' );
		?>
		<div><label for="<?php echo esc_attr( $title_id ); ?>">
				<?php esc_html_e( 'Widget Title', 'a-z-listing' ); ?>
			</label></div>
		<input class="widefat" type="text"
			   id="<?php echo esc_attr( $title_id ); ?>"
			   name="<?php echo esc_attr( $title_name ); ?>"
			   placeholder="<?php esc_attr_e( 'Widget Title', 'a-z-listing' ); ?>"
			   value="<?php echo esc_attr( $title ); ?>" />

		<p style="color: #333;">
			<?php esc_html_e( 'Leave the title field blank, above, to use the title from the page set in the next field', 'a-z-listing' ); ?>
		</p>

		<div><label for="<?php echo esc_attr( $post_id ); ?>">
				<?php esc_html_e( 'Site map A-Z page', 'a-z-listing' ); ?>
			</label></div>
		<?php
		wp_dropdown_pages( array(
			'id' => intval( $post_id ),
			'name' => esc_html( $post_name ),
			'selected' => intval( $post ),
		) );
		?>

		<div><label for="<?php echo esc_attr( $post_type_id ); ?>">
				<?php esc_html_e( 'Post-type to display', 'a-z-listing' ); ?>
			</label></div>
		<?php
		$post_types = get_post_types();
		sort( $post_types );
		?>
		<select id="<?php echo esc_attr( $post_type_id ); ?>" name="<?php echo esc_attr( $post_type_name ); ?>">
			<?php foreach ( $post_types as $t ) : ?>
				<option value="<?php echo esc_attr( $t ); ?>" <?php if ( $post_type === $t ) { echo 'selected'; } ?>>
					<?php echo esc_html( $t ); ?>
				</option>
			<?php endforeach; ?>
		</select>
		<?php
	}

	/**
	 * Called by WordPress core. Sanitises changes to the Widget's configuration.
	 *
	 * @since 0.1
	 * @param  Array $new_instance the new configuration values.
	 * @param  Array $old_instance the previous configuration values.
	 * @return Array               sanitised version of the new configuration values to be saved
	 */
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['post'] = (int) $new_instance['post'];
		$instance['post_type'] = strip_tags( $new_instance['post_type'] );
		$instance['parent'] = (int) $new_instance['parent'];
		return $instance;
	}

	/**
	 * Print the user-visible widget to the page.
	 *
	 * @since 0.1
	 * @param  Array $args     General widget configuration. Often shared between all widgets on the site.
	 * @param  Array $instance Configuration of this Widget. Unique to this invocation.
	 */
	function widget( $args, $instance ) {
		the_section_az_widget( $args, $instance );
	}
}

Changelog

Changelog
Version Description
0.1 Introduced.

Methods

  • __construct — Register the widget's meta information.
  • A_Z_Widget — Deprecated constructor.
  • form — Print-out the configuration form for the widget.
  • update — Called by WordPress core. Sanitises changes to the Widget's configuration.
  • widget — Print the user-visible widget to the page.

User Contributed Notes

You must log in before being able to contribute a note or feedback.