'a-z-listing-widget', 'description' => __( 'Alphabetised links to the A-Z site map', 'a-z-listing' ), ) ); add_action( 'admin_enqueue_scripts', 'a_z_listing_enqueue_widget_admin_script' ); if ( is_active_widget( false, false, $this->id_base, true ) ) { a_z_listing_do_enqueue(); } } /** * Deprecated constructor * * @since 0.1 */ public 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 */ public function form( $instance ) { $args = array( 'public' => true, ); $public_post_types = get_post_types( $args, 'objects' ); $public_taxonomies = get_taxonomies( $args, 'objects' ); $widget_title = isset( $instance['title'] ) ? $instance['title'] : ''; $widget_title_id = $this->get_field_id( 'title' ); $widget_title_name = $this->get_field_name( 'title' ); $display_type = ( isset( $instance['type'] ) && 'terms' === $instance['type'] ) ? 'terms' : 'posts'; $display_type_id = $this->get_field_id( 'type' ); $display_type_name = $this->get_field_name( 'type' ); $target_post = isset( $instance['post'] ) ? intval( $instance['post'] ) : ( isset( $instance['page'] ) ? intval( $instance['page'] ) : 0 ); $target_post_id = $this->get_field_id( 'post' ); $target_post_name = $this->get_field_name( 'post' ); $target_post_title = isset( $instance['target_post'] ) ? $instance['target_post'] : ( 0 < $target_post ) ? get_the_title( $target_post ) : ''; $target_post_title_id = $this->get_field_id( 'target_post_title' ); $target_post_title_name = $this->get_field_name( 'target_post_title' ); $listing_post_type = isset( $instance['post_type'] ) ? $instance['post_type'] : 'page'; $listing_post_type_id = $this->get_field_id( 'post_type' ); $listing_post_type_name = $this->get_field_name( 'post_type' ); $listing_parent_post = isset( $instance['parent_post'] ) ? $instance['parent_post'] : ''; $listing_parent_post_id = $this->get_field_id( 'parent_post' ); $listing_parent_post_name = $this->get_field_name( 'parent_post' ); $listing_parent_post_title = isset( $instance['parent_post_title'] ) ? $instance['parent_post_title'] : ( 0 < $listing_parent_post ) ? get_the_title( $listing_parent_post ) : ''; $listing_parent_post_title_id = $this->get_field_id( 'parent_post_title' ); $listing_parent_post_title_name = $this->get_field_name( 'parent_post_title' ); $listing_all_children = isset( $instance['all_children'] ) ? $instance['all_children'] : 'true'; $listing_all_children_id = $this->get_field_id( 'all_children' ); $listing_all_children_name = $this->get_field_name( 'all_children' ); $listing_taxonomy = isset( $instance['taxonomy'] ) ? $instance['taxonomy'] : 'page'; $listing_taxonomy_id = $this->get_field_id( 'taxonomy' ); $listing_taxonomy_name = $this->get_field_name( 'taxonomy' ); $listing_parent_term = isset( $instance['parent_term'] ) ? $instance['parent_term'] : ''; $listing_parent_term_id = $this->get_field_id( 'parent_term' ); $listing_parent_term_name = $this->get_field_name( 'parent_term' ); $listing_terms_include = isset( $instance['terms'] ) ? $instance['terms'] : ''; $listing_terms_include_id = $this->get_field_id( 'terms' ); $listing_terms_include_name = $this->get_field_name( 'terms' ); $listing_terms_exclude = isset( $instance['terms_exclude'] ) ? $instance['terms_exclude'] : ''; $listing_terms_exclude_id = $this->get_field_id( 'terms_exclude' ); $listing_terms_exclude_name = $this->get_field_name( 'terms_exclude' ); $listing_hide_empty_terms = isset( $instance['hide_empty_terms'] ) ? $instance['hide_empty_terms'] : ''; $listing_hide_empty_terms_id = $this->get_field_id( 'hide_empty_terms' ); $listing_hide_empty_terms_name = $this->get_field_name( 'hide_empty_terms' ); ?>

>

>

value="" />

/>

>

>

>

>

/>

'true', 'exclude_terms' => '', 'hide_empty_terms' => false, 'parent_post' => '', 'parent_term' => '', 'post' => -1, // target. 'page' => -1, // obsolete target. 'post_type' => 'page', 'taxonomy' => '', 'terms' => '', 'title' => '', 'type' => 'posts', ) ); $title = esc_html( $instance['title'] ); $target_url = ''; if ( 0 < $instance['post'] || 0 < $instance['page'] ) { // target. $target_id = (int) $instance['post']; // target. if ( ! ( 0 < $instance['post'] ) ) { $target_id = (int) $instance['page']; // obsolete target. } $target_url = get_the_permalink( $target_id ); if ( empty( $title ) ) { $title = get_the_title( $target_id ); } } elseif ( empty( $title ) ) { $title = esc_html__( 'A-Z Listing', 'a-z-listing' ); } $hide_empty_terms = true === $instance['hide_empty_terms'] ? 'true' : 'false'; $ret = ''; $ret .= $args['before_widget']; $ret .= $args['before_title']; $ret .= $title; $ret .= $args['after_title']; $ret .= do_shortcode( "[a-z-listing alphabet='' display='{$instance['type']}' exclude-posts='' exclude-terms='{$instance['exclude_terms']}' get-all-children='{$instance['all_children']}' group-numbers='' grouping='' hide-empty-terms='{$hide_empty_terms}' numbers='hide' parent-post='{$instance['parent_post']}' parent-term='{$instance['parent_term']}' post-type='{$instance['post_type']}' return='letters' target='{$target_url}' taxonomy='{$instance['taxonomy']}' terms='{$instance['terms']}' ]" ); $ret .= $args['after_widget']; return $ret; } /** * Retrive posts by title. * * @since 2.1.0 * @param string $post_title the title to search for. * @param string $post_type the post type to search within. * @return array the post IDs that are found. */ function a_z_listing_get_posts_by_title( $post_title, $post_type = '' ) { global $wpdb; $post_title = '%' . $wpdb->esc_like( $post_title ) . '%'; if ( ! empty( $post_type ) ) { return $wpdb->get_results( $wpdb->prepare( "SELECT `ID`, `post_title` FROM `$wpdb->posts` WHERE `post_title` LIKE %s AND `post_type` = %s AND `post_status` = 'publish'", $post_title, $post_type ) ); } else { return $wpdb->get_results( $wpdb->prepare( "SELECT `ID`, `post_title` FROM `$wpdb->posts` WHERE `post_title` LIKE %s AND `post_status` = 'publish'", $post_title ) ); } } /** * Ajax responder for A_Z_Listing_Widget configuration * * @since 2.0.0 */ function a_z_listing_autocomplete_post_titles() { $post_title = stripslashes( $_POST['post_title']['term'] ); $post_type = stripslashes( $_POST['post_type'] ); $results = a_z_listing_get_posts_by_title( $post_title, $post_type ); $titles = array(); foreach ( $results as $result ) { $titles[] = array( 'value' => intval( $result->ID ), 'label' => addslashes( $result->post_title ), ); } echo json_encode( $titles ); exit(); } add_action( 'wp_ajax_nopriv_get_a_z_listing_autocomplete_post_titles', 'a_z_listing_autocomplete_post_titles' ); add_action( 'wp_ajax_get_a_z_listing_autocomplete_post_titles', 'a_z_listing_autocomplete_post_titles' ); /** * Register the A_Z_Widget widget * * @since 2.0.0 */ function a_z_listing_widget() { register_widget( 'A_Z_Listing_Widget' ); } add_action( 'widgets_init', 'a_z_listing_widget' ); /** * Enqueue the jquery-ui autocomplete script * * @since 2.0.0 */ function a_z_listing_autocomplete_script() { wp_enqueue_script( 'jquery-ui-autocomplete' ); } add_action( 'admin_enqueue_scripts', 'a_z_listing_autocomplete_script' );