Fixed a few files…
-
First off let me clearly state I am NOT a coder/programmer and only dabble when needed. I cleaned up the following files and providing them AS-IS YMMV
a-z-listing.php
<?php
/**
* Plugin Name: A-Z Listing
* Plugin URI:
* Description: Display an A to Z listing of posts
* Author:
* Author URI:
* Text Domain: a-z-listing
* Domain Path: /languages
* Version: 4.3.1
* Requires WordPressat least: 6.8
* Requires PHP: 7.4
*
* @package A_Z_Listing
*/
declare(strict_types=1);
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! function_exists( '\get_plugin_data' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
if ( ! defined( 'A_Z_LISTING_VERSION' ) ) {
define( 'A_Z_LISTING_VERSION', \get_plugin_data( __FILE__, false, false )['Version'] );
}
if ( ! defined( 'A_Z_LISTING_LOG' ) ) {
define( 'A_Z_LISTING_LOG', false );
}
if ( ! defined( 'A_Z_LISTING_PLUGIN_FILE' ) ) {
define( 'A_Z_LISTING_PLUGIN_FILE', __FILE__ );
}
if ( ! defined( 'A_Z_LISTING_DEFAULT_TEMPLATE' ) ) {
define( 'A_Z_LISTING_DEFAULT_TEMPLATE', __DIR__ . '/templates/a-z-listing.php' );
}
if ( file_exists( __DIR__ . '/build/vendor/scoper-autoload.php' ) ) {
require_once __DIR__ . '/build/vendor/scoper-autoload.php';
} else {
require_once __DIR__ . '/vendor/autoload.php';
}
require_once __DIR__ . '/functions/i18n.php';
require_once __DIR__ . '/functions/health-check.php';
require_once __DIR__ . '/functions/helpers.php';
require_once __DIR__ . '/functions/styles.php';
require_once __DIR__ . '/functions/scripts.php';
require_once __DIR__ . '/functions/enqueues.php';
require_once __DIR__ . '/widgets/class-a-z-listing-widget.php';
/**
* Initialize the plugin.
*
* @return void
*/
function a_z_listing_init() {
\A_Z_Listing\Indices::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\GutenBlock::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode\PostsQuery::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode\TermsQuery::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode\QueryParts\Alphabet::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode\QueryParts\Columns::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode\QueryParts\ColumnGap::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode\QueryParts\ColumnWidth::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode\QueryParts\ExcludePosts::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode\QueryParts\ExcludeTerms::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode\QueryParts\HideEmpty_Deprecated::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode\QueryParts\HideEmptyTerms::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode\QueryParts\InstanceId::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode\QueryParts\ParentPost::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode\QueryParts\ParentTermId::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode\QueryParts\ParentTermSlugOrId::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode\QueryParts\PostsTerms::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode\QueryParts\PostType::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode\QueryParts\SymbolsFirst::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode\QueryParts\Taxonomy::instance()->activate( __FILE__ )->initialize();
\A_Z_Listing\Shortcode\QueryParts\TermsTerms::instance()->activate( __FILE__ )->initialize();
}
add_action( 'init', 'a_z_listing_init', 5 );class-a-z-listing-widget.php
<?php declare(strict_types=1);
/**
* Definition for the a-z-listing's main widget
*
* @package a-z-listing
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Definition for the AZ_Widget which displays alphabetically-ordered list of latin letters linking to the A-Z Listing page
*
* @since 0.1
*/
class A_Z_Listing_Widget extends WP_Widget {
/**
* Register the widget's meta information
*
* @since 0.1
* @param string $id_base Optional Base ID for the widget, lowercase and unique.
* @param string $name Name for the widget displayed on the configuration page.
* @param array<string,mixed> $widget_options Optional widget options.
* @param array<string,mixed> $control_options Optional widget control options.
*/
public function __construct( $id_base = '', $name = '', $widget_options = array(), $control_options = array() ) {
$widget_options['classname'] = $widget_options['classname'] ?? 'a-z-listing-widget';
$widget_options['description'] = $widget_options['description'] ?? __(
'Alphabetised links to the A-Z site map',
'a-z-listing'
);
parent::__construct(
! empty( $id_base ) ? $id_base : 'bh_az_widget',
! empty( $name ) ? $name : __( 'A-Z Site Map', 'a-z-listing' ),
$widget_options,
$control_options
);
add_action( 'admin_enqueue_scripts', 'a_z_listing_enqueue_widget_admin_script' );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_if_active' ) );
}
/**
* Enqueue scripts and stylesheets if the widget is active on the page
*
* @since 4.0.0
* @return void
*/
public function enqueue_if_active() {
if ( false !== is_active_widget( false, false, $this->id_base, true ) ) {
a_z_listing_do_enqueue();
}
}
/**
* Print-out the configuration form for the widget
*
* @since 0.1
* @param array<string,mixed> $instance Widget instance as provided by WordPress core.
* @return string
*/
public function form( $instance ) {
$args = array(
'public' => true,
);
$public_post_types = get_post_types( $args, 'objects' );
$public_taxonomies = get_taxonomies( $args, 'objects' );
$widget_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'] ) ? (int) $instance['post'] : ( isset( $instance['page'] ) ? (int) $instance['page'] : 0 );
$target_post_id = $this->get_field_id( 'post' );
$target_post_name = $this->get_field_name( 'post' );
$target_post_title = $instance['target_post_title'] ?? ( ( 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 = $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 = $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 = $instance['parent_post_title'] ?? ( ( 0 < (int) $listing_parent_post ) ? get_the_title( (int) $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 = $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 = $instance['taxonomy'] ?? 'page';
$listing_taxonomy_id = $this->get_field_id( 'taxonomy' );
$listing_taxonomy_name = $this->get_field_name( 'taxonomy' );
$listing_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 = $instance['terms'] ?? '';
$listing_terms_include_id = $this->get_field_id( 'terms' );
$listing_terms_include_name = $this->get_field_name( 'terms' );
$listing_terms_exclude = $instance['exclude_terms'] ?? '';
$listing_terms_exclude_id = $this->get_field_id( 'exclude_terms' );
$listing_terms_exclude_name = $this->get_field_name( 'exclude_terms' );
$listing_hide_empty_terms = $instance['hide_empty_terms'] ?? 'false';
$listing_hide_empty_terms_id = $this->get_field_id( 'hide_empty_terms' );
$listing_hide_empty_terms_name = $this->get_field_name( 'hide_empty_terms' );
wp_nonce_field( 'posts-by-title', '_posts_by_title_wpnonce', false, true );
?>
<div class="a-z-listing-widget">
<div class="a-z-listing-widget-title-wrapper">
<p>
<label for="<?php echo esc_attr( $widget_title_id ); ?>">
<?php esc_html_e( 'Widget Title', 'a-z-listing' ); ?>
</label>
<input type="text" class="widefat a-z-listing-title"
id="<?php echo esc_attr( $widget_title_id ); ?>"
name="<?php echo esc_attr( $widget_title_name ); ?>"
placeholder="<?php esc_attr_e( 'Widget Title', 'a-z-listing' ); ?>"
value="<?php echo esc_attr( $widget_title ); ?>" />
</p>
<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>
<div class="a-z-listing-target-post-wrapper">
<p>
<label for="<?php echo esc_attr( $target_post_title_id ); ?>">
<?php esc_html_e( 'Sitemap A-Z page', 'a-z-listing' ); ?>
</label>
<input type="text" class="widefat a-z-listing-target-post-title"
id="<?php echo esc_attr( $target_post_title_id ); ?>"
name="<?php echo esc_attr( $target_post_title_name ); ?>"
value="<?php echo esc_attr( $target_post_title ); ?>" />
<input type="hidden"
id="<?php echo esc_attr( $target_post_id ); ?>"
name="<?php echo esc_attr( $target_post_name ); ?>"
value="<?php echo esc_attr( $target_post ); ?>" />
</p>
<p>
<?php esc_html_e( 'Type some or all of the title of the page you want links to point at. Ensure this input field is not selected when you save the settings.', 'a-z-listing' ); ?>
<?php esc_html_e( 'Matching posts will be shown as you type. Click on the correct post from the matches to update the setting.', 'a-z-listing' ); ?>
</p>
</div>
<div class="a-z-listing-display-type-wrapper">
<p>
<label for="<?php echo esc_attr( $display_type_id ); ?>">
<?php esc_html_e( 'Display posts or terms?', 'a-z-listing' ); ?>
</label>
<select class="widefat a-z-listing-display-type"
id="<?php echo esc_attr( $display_type_id ); ?>"
name="<?php echo esc_attr( $display_type_name ); ?>">
<option value="posts" <?php selected( 'posts', $display_type ); ?>>
<?php esc_html_e( 'Posts', 'a-z-listing' ); ?>
</option>
<option value="terms" <?php selected( 'terms', $display_type ); ?>>
<?php esc_html_e( 'Taxonomy terms', 'a-z-listing' ); ?>
</option>
</select>
</p>
</div>
<div class="a-z-listing-post-type-wrapper" <?php echo ( 'terms' !== $display_type ) ? '' : 'style="display: none;"'; ?>>
<p>
<label for="<?php echo esc_attr( $listing_post_type_id ); ?>">
<?php esc_html_e( 'Post-type to display', 'a-z-listing' ); ?>
</label>
<select class="widefat a-z-listing-post-type"
id="<?php echo esc_attr( $listing_post_type_id ); ?>"
name="<?php echo esc_attr( $listing_post_type_name ); ?>"
<?php echo ( 'terms' !== $display_type ) ? '' : 'disabled'; ?>>
<?php foreach ( $public_post_types as $k => $t ) : ?>
<option value="<?php echo esc_attr( $k ); ?>" <?php selected( $k, $listing_post_type ); ?>>
<?php echo esc_html( $t->labels->name ); ?>
</option>
<?php endforeach; ?>
</select>
</p>
</div>
<div class="a-z-listing-parent-post-wrapper" <?php echo ( 'terms' !== $display_type ) ? '' : 'style="display: none"'; ?>>
<p>
<label for="<?php echo esc_attr( $listing_parent_post_title_id ); ?>">
<?php esc_html_e( 'Show only children of this post (ID)', 'a-z-listing' ); ?>
</label>
<input type="text" class="widefat a-z-listing-parent-post-title"
id="<?php echo esc_attr( $listing_parent_post_title_id ); ?>"
name="<?php echo esc_attr( $listing_parent_post_title_name ); ?>"
<?php echo ( 'terms' !== $display_type ) ? '' : 'disabled'; ?>
value="<?php echo esc_attr( $listing_parent_post_title ); ?>" />
<input type="hidden"
id="<?php echo esc_attr( $listing_parent_post_id ); ?>"
name="<?php echo esc_attr( $listing_parent_post_name ); ?>"
value="<?php echo esc_attr( $listing_parent_post ); ?>" />
</p>
<p>
<?php esc_html_e( 'Type some or all of the title of the post to limit the listing to only the children of that post. Ensure this input field is not selected when you save the settings.', 'a-z-listing' ); ?>
<?php esc_html_e( 'Matching posts will be shown as you type. Click on the correct post from the matches to update the setting.', 'a-z-listing' ); ?>
</p>
<p>
<label for="<?php echo esc_attr( $listing_all_children_id ); ?>">
<?php esc_html_e( 'Include grand-children?', 'a-z-listing' ); ?>
</label>
<input type="checkbox" class="a-z-listing-all-children"
id="<?php echo esc_attr( $listing_all_children_id ); ?>"
name="<?php echo esc_attr( $listing_all_children_name ); ?>"
<?php checked( 'true', $listing_all_children ); ?> />
</p>
</div>
<div class="a-z-listing-taxonomy-wrapper" <?php echo ( 'terms' === $display_type ) ? '' : 'style="display: none;"'; ?>>
<p>
<label for="<?php echo esc_attr( $listing_taxonomy_id ); ?>">
<?php esc_html_e( 'Taxonomy to display', 'a-z-listing' ); ?>
</label>
<select class="widefat a-z-listing-taxonomy"
id="<?php echo esc_attr( $listing_taxonomy_id ); ?>"
name="<?php echo esc_attr( $listing_taxonomy_name ); ?>"
<?php echo ( 'terms' === $display_type ) ? '' : 'disabled'; ?>>
<?php foreach ( $public_taxonomies as $k => $t ) : ?>
<option value="<?php echo esc_attr( $k ); ?>" <?php selected( $k, $listing_taxonomy ); ?>>
<?php echo esc_html( $t->labels->name ); ?>
</option>
<?php endforeach; ?>
</select>
</p>
</div>
<div class="a-z-listing-parent-term-wrapper" <?php echo ( 'terms' === $display_type ) ? '' : 'style="display: none;"'; ?>>
<p>
<label for="<?php echo esc_attr( $listing_parent_term_id ); ?>">
<?php esc_html_e( 'Parent term to display children of', 'a-z-listing' ); ?>
</label>
<input type="text" class="widefat a-z-listing-parent-term"
id="<?php echo esc_attr( $listing_parent_term_id ); ?>"
name="<?php echo esc_attr( $listing_parent_term_name ); ?>"
value="<?php echo esc_attr( $listing_parent_term ); ?>" />
</p>
</div>
<div class="a-z-listing-include-terms-wrapper">
<p>
<label for="<?php echo esc_attr( $listing_terms_include_id ); ?>">
<?php esc_html_e( 'Terms to include (IDs)', 'a-z-listing' ); ?>
</label>
<input type="text" class="widefat a-z-listing-include-terms"
id="<?php echo esc_attr( $listing_terms_include_id ); ?>"
name="<?php echo esc_attr( $listing_terms_include_name ); ?>"
value="<?php echo esc_attr( $listing_terms_include ); ?>" />
</p>
</div>
<div class="a-z-listing-exclude-terms-wrapper" <?php echo ( 'terms' === $display_type ) ? '' : 'style="display: none;"'; ?>>
<p>
<label for="<?php echo esc_attr( $listing_terms_exclude_id ); ?>">
<?php esc_html_e( 'Terms to exclude (IDs)', 'a-z-listing' ); ?>
</label>
<input type="text" class="widefat a-z-listing-exclude-terms"
id="<?php echo esc_attr( $listing_terms_exclude_id ); ?>"
name="<?php echo esc_attr( $listing_terms_exclude_name ); ?>"
value="<?php echo esc_attr( $listing_terms_exclude ); ?>" />
</p>
</div>
<div class="a-z-listing-hide-empty-terms-wrapper" <?php echo ( 'terms' === $display_type ) ? '' : 'style="display: none;"'; ?>>
<p>
<label for="<?php echo esc_attr( $listing_hide_empty_terms_id ); ?>">
<?php esc_html_e( 'Hide empty terms', 'a-z-listing' ); ?>
</label>
<input type="checkbox" class="a-z-listing-hide-empty-terms"
id="<?php echo esc_attr( $listing_hide_empty_terms_id ); ?>"
name="<?php echo esc_attr( $listing_hide_empty_terms_name ); ?>"
<?php checked( 'true', $listing_hide_empty_terms ); ?> />
</p>
</div>
</div>
<?php
return '';
}
/**
* Called by WordPress core. Sanitises changes to the Widget's configuration
*
* @since 0.1
* @param array<string,mixed> $new_instance the new configuration values.
* @param array<string,mixed> $old_instance the previous configuration values.
* @return array<string,mixed> sanitised version of the new configuration values to be saved
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = isset( $new_instance['title'] ) ? wp_strip_all_tags( $new_instance['title'] ) : '';
$instance['type'] = isset( $new_instance['type'] ) ? wp_strip_all_tags( $new_instance['type'] ) : 'posts';
$instance['post'] = isset( $new_instance['post'] ) ? (int) $new_instance['post'] : 0;
$instance['target_post_title'] = isset( $new_instance['target_post_title'] ) ? wp_strip_all_tags( $new_instance['target_post_title'] ) : '';
$instance['post_type'] = isset( $new_instance['post_type'] ) ? wp_strip_all_tags( $new_instance['post_type'] ) : 'page';
$instance['taxonomy'] = isset( $new_instance['taxonomy'] ) ? wp_strip_all_tags( $new_instance['taxonomy'] ) : '';
$instance['parent_post'] = isset( $new_instance['parent_post'] ) ? (int) $new_instance['parent_post'] : 0;
$instance['parent_post_title'] = isset( $new_instance['parent_post_title'] ) ? wp_strip_all_tags( $new_instance['parent_post_title'] ) : '';
$instance['all_children'] = ( isset( $new_instance['all_children'] ) && 'on' === $new_instance['all_children'] ) ? 'true' : 'false';
$instance['parent_term'] = isset( $new_instance['parent_term'] ) ? wp_strip_all_tags( $new_instance['parent_term'] ) : '';
$instance['terms'] = isset( $new_instance['terms'] ) ? wp_strip_all_tags( $new_instance['terms'] ) : '';
$instance['exclude_terms'] = isset( $new_instance['exclude_terms'] ) ? wp_strip_all_tags( $new_instance['exclude_terms'] ) : '';
$instance['hide_empty_terms'] = ( isset( $new_instance['hide_empty_terms'] ) && 'on' === $new_instance['hide_empty_terms'] ) ? 'true' : 'false';
if ( empty( $instance['target_post_title'] ) ) {
$instance['post'] = 0;
}
if ( empty( $instance['parent_post_title'] ) ) {
$instance['parent_post'] = 0;
}
return $instance;
}
/**
* Print the user-visible widget to the page
*
* @since 0.1
* @param array<string,mixed> $args General widget configuration.
* @param array<string,mixed> $instance Configuration of this Widget.
* @return void
*/
public function widget( $args, $instance ) {
the_section_a_z_widget( $args, $instance );
}
}
/**
* Print the user-visible widget to the page implentation
*
* @since 0.1
* @since 0.8.0 deprecated.
* @see A_Z_Widget::the_section_a_z_widget()
* @deprecated use the_section_a_z_widget()
* @param array<string,mixed> $args General widget configuration.
* @param array<string,mixed> $instance Configuration of this Widget.
* @return void
*/
function the_section_az_widget( array $args, array $instance ) {
_deprecated_function( __FUNCTION__, '0.8.0', 'the_section_a_z_widget' );
the_section_a_z_widget( $args, $instance );
}
/**
* Print the user-visible widget to the page implentation
*
* @since 0.8.0
* @param array<string,mixed> $args General widget configuration.
* @param array<string,mixed> $instance Configuration of this Widget.
* @return void
*/
function the_section_a_z_widget( array $args, array $instance ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
echo get_the_section_a_z_widget( $args, $instance ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Deprecated print the user-visible widget to the page implementation.
*
* @since 0.1
* @since 0.8.0 deprecated.
* @see A_Z_Widget::get_the_section_a_z_widget()
* @deprecated use get_the_section_a_z_widget()
* @param array<string,mixed> $args General widget configuration.
* @param array<string,mixed> $instance Configuration of this Widget.
* @return string The complete A-Z Widget HTML ready for echoing to the page.
*/
function get_the_section_az_widget( array $args, array $instance ): string {
_deprecated_function( __FUNCTION__, '0.8.0', 'get_the_section_a_z_widget' );
return get_the_section_a_z_widget( $args, $instance );
}
/**
* Get the user-visible widget html
*
* @since 0.8.0
* @param array<string,mixed> $args General widget configuration.
* @param array<string,mixed> $instance Configuration of this Widget.
* @return string The complete A-Z Widget HTML ready for echoing to the page.
*/
function get_the_section_a_z_widget( array $args, array $instance ): string { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
do_action( 'a_z_listing_log', 'A-Z Listing: Running widget' );
$instance = wp_parse_args(
$instance,
array(
'all_children' => 'true',
'exclude_terms' => '',
'hide_empty_terms' => 'false',
'parent_post' => '',
'parent_term' => '',
'post' => -1,
'page' => -1,
'post_type' => 'page',
'taxonomy' => '',
'terms' => '',
'title' => '',
'type' => 'posts',
)
);
$title = esc_html( (string) $instance['title'] );
$target_url = '';
if ( 0 < (int) $instance['post'] || 0 < (int) $instance['page'] ) {
$target_id = (int) $instance['post'];
if ( ! ( 0 < (int) $instance['post'] ) ) {
$target_id = (int) $instance['page'];
}
$target_url = (string) get_the_permalink( $target_id );
if ( empty( $title ) ) {
$title = (string) get_the_title( $target_id );
}
} elseif ( empty( $title ) ) {
$title = esc_html__( 'A-Z Listing', 'a-z-listing' );
}
$hide_empty_terms = a_z_listing_is_truthy( $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;
}
/**
* Replace the WP_Query search parameters to search just the title.
*
* @since 4.0.0
* @param string $search The search database query snippet.
* @param WP_Query $wp_query The WP_Query.
* @return string The updated search database query snippet.
*/
function a_z_listing_search_titles_only( $search, $wp_query ) {
if ( empty( $search ) || empty( $wp_query->query_vars['search_terms'] ) ) {
return $search;
}
global $wpdb;
$search = array();
$params = $wp_query->query_vars;
$n = empty( $params['exact'] ) ? '%' : '';
foreach ( $params['search_terms'] as $term ) {
$search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $n . $wpdb->esc_like( $term ) . $n );
}
if ( ! is_user_logged_in() ) {
$search[] = "$wpdb->posts.post_password = ''";
}
return ' AND ' . implode( ' AND ', $search );
}
/**
* Retrieve posts by title.
*
* @since 2.1.0
* @since 4.0.0 Use WP_Query
* @param string $post_title the title to search for.
* @param string $post_type the post type to search within.
* @return array<int,object> the post IDs that are found.
*/
function a_z_listing_get_posts_by_title( string $post_title, string $post_type = '' ): array {
$params = array(
's' => $post_title,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
);
if ( ! empty( $post_type ) ) {
$params['post_type'] = $post_type;
}
add_filter( 'posts_search', 'a_z_listing_search_titles_only', 10, 2 );
$query = new WP_Query( $params );
$results = $query->posts;
remove_filter( 'posts_search', 'a_z_listing_search_titles_only', 10 );
return $results;
}
/**
* Ajax responder for A_Z_Listing_Widget configuration
*
* @since 2.0.0
* @return void
*/
function a_z_listing_autocomplete_post_titles() {
if ( ! current_user_can( 'edit_theme_options' ) ) {
wp_send_json(
array(
'message' => __( 'Permission denied', 'a-z-listing' ),
),
403
);
}
check_ajax_referer( 'posts-by-title', '_posts_by_title_wpnonce' );
$post_title = '';
$post_type = '';
if ( isset( $_POST['post_title']['term'] ) ) {
$post_title = sanitize_text_field( wp_unslash( $_POST['post_title']['term'] ) );
}
if ( isset( $_POST['post_type'] ) ) {
$post_type = sanitize_text_field( wp_unslash( $_POST['post_type'] ) );
}
$results = a_z_listing_get_posts_by_title( $post_title, $post_type );
$titles = array();
foreach ( $results as $result ) {
$titles[] = array(
'value' => (int) $result->ID,
'label' => $result->post_title,
);
}
wp_send_json( $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
* @return void
*/
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
* @return void
*/
function a_z_listing_autocomplete_script() {
wp_enqueue_script( 'jquery-ui-autocomplete' );
}
add_action( 'admin_enqueue_scripts', 'a_z_listing_autocomplete_script' );scripts.php
<?php declare(strict_types=1);
/**
* Javascripts enqueueing functions.
*
* @package a-z-listing
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Enqueue Tabs script on pages where the shortcode is active
*
* @since 2.0.0
* @param bool $force Set this to true if you want the script to always be enqueued.
* @return void
*/
function a_z_listing_enqueue_tabs( bool $force = false ) {
global $post;
$post_content = '';
if ( isset( $post ) && is_object( $post ) && isset( $post->post_content ) && is_string( $post->post_content ) ) {
$post_content = $post->post_content;
}
if ( $force || ( is_singular() && '' !== $post_content && has_shortcode( $post_content, 'a-z-listing' ) ) ) {
wp_enqueue_script( 'a-z-listing-tabs' );
}
}
/**
* Forcibly enqueue Tabs script. This is a helper function which can be hooked in-place of the default hook added ina_z_listing_add_styling
*
* @since 2.0.0
* @return void
*/
function a_z_listing_force_enqueue_tabs() {
a_z_listing_enqueue_tabs( true );
}
/**
* Replace the default Tabs script enqueue function witha_z_listing_force_enqueue_tabsto always add the Tabs script to pages
*
* @since 2.0.0
* @return void
*/
function a_z_listing_force_enable_tabs() {
if ( false !== has_action( 'wp_enqueue_scripts', 'a_z_listing_enqueue_tabs' ) ) {
remove_action( 'wp_enqueue_scripts', 'a_z_listing_enqueue_tabs' );
}
add_action( 'wp_enqueue_scripts', 'a_z_listing_force_enqueue_tabs' );
}
/**
* Enqueue the widget configuration support script
*
* @since 2.1.0
* @return void
*/
function a_z_listing_enqueue_widget_admin_script() {
wp_enqueue_script( 'a-z-listing-widget-admin' );
}
/**
* Enqueue Scrollfix script
*
* @since 4.0.0
* @return void
*/
function a_z_listing_enqueue_scroll_fix() {
wp_enqueue_script( 'a-z-listing-scroll-fix' );
}helpers.php
<?php
/**
* Support functions for the A-Z Index page
*
* @package a-z-listing
*/
declare(strict_types=1);
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'a_z_listing_log', 'a_z_listing_log' );
/**
* A-Z Listing Logging wrapper function.
*
* @since 4.0.0
* @return void
*/
function a_z_listing_log() {
do_action_ref_array( 'log', func_get_args() ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
}
/**
* Retrieve whether the query has any letters left
*
* @since 0.7
* @param array|string|WP_Query|\A_Z_Listing\Query $query a valid WordPress query or an A_Z_Listing\Query instance.
* @return bool whether there are letters still to be iterated-over
*/
function have_a_z_letters( $query = null ): bool {
return a_z_listing_cache( $query )->have_letters();
}
/**
* Whether the query has any posts left for the current letter
*
* @since 0.7
* @since 0.8.0 deprecated
* @see have_a_z_items()
* @deprecated use have_a_z_items()
* @return bool
*/
function have_a_z_posts(): bool {
_deprecated_function( __FUNCTION__, '0.8.0', 'have_a_z_items' );
return have_a_z_items();
}
/**
* Whether the query has any posts left for the current letter
*
* @since 0.8.0
* @param array|string|WP_Query|\A_Z_Listing\Query $query a valid WordPress query or an A_Z_Listing\Query instance.
* @return bool whether there are still posts available
*/
function have_a_z_items( $query = null ): bool {
return a_z_listing_cache( $query )->have_items();
}
/**
* Proceed to the next letter
*
* @since 0.7
* @param array|string|WP_Query|\A_Z_Listing\Query $query a valid WordPress query or an A_Z_Listing\Query instance.
* @return void
*/
function the_a_z_letter( $query = null ) {
a_z_listing_cache( $query )->the_letter();
}
/**
* Proceed to the next letter
*
* @since 0.7
* @since 0.8.0 deprecated
* @see the_a_z_item()
* @deprecated use the_a_z_item()
* @return void
*/
function the_a_z_post() {
_deprecated_function( __FUNCTION__, '0.8.0', 'the_a_z_item' );
the_a_z_item();
}
/**
* Proceed to the next post
*
* @since 0.8.0
* @param array|string|WP_Query|\A_Z_Listing\Query $query a valid WordPress query or an A_Z_Listing\Query instance.
* @return void
*/
function the_a_z_item( $query = null ) {
a_z_listing_cache( $query )->the_item();
}
/**
* Retrieve the number of posts for the letter
*
* @since 0.7
* @since 1.0.0 deprecated
* @see get_the_a_z_letter_count()
* @deprecated use get_the_a_z_letter_count()
* @return int
*/
function num_a_z_letters(): int {
_deprecated_function( __FUNCTION__, '1.0.0', 'get_the_a_z_letter_count' );
return get_the_a_z_letter_count();
}
/**
* Retrieve the number of posts for the letter
*
* @since 0.7
* @since 1.0.0 deprecated
* @see get_the_a_z_letter_count()
* @deprecated use get_the_a_z_letter_count()
* @return int
*/
function num_a_z_posts(): int {
_deprecated_function( __FUNCTION__, '1.0.0', 'get_the_a_z_letter_count' );
return get_the_a_z_letter_count();
}
/**
* Retrieve the number of posts for the letter
*
* @since 0.7
* @since 1.0.0 deprecated
* @see get_the_a_z_letter_count()
* @deprecated use get_the_a_z_letter_count()
* @return int
*/
function num_a_z_items(): int {
_deprecated_function( __FUNCTION__, '1.0.0', 'get_the_a_z_letter_count' );
return get_the_a_z_letter_count();
}
/**
* Print the number of letters for the query
*
* @since 1.0.0
* @param array|string|WP_Query|\A_Z_Listing\Query $query a valid WordPress query or an A_Z_Listing\Query instance.
* @return void
*/
function the_a_z_letter_count( $query = null ) {
echo esc_html( a_z_listing_cache( $query )->num_letters() );
}
/**
* Retrieve the number of items for the current letter
*
* @since 1.0.0
* @param array|string|WP_Query|\A_Z_Listing\Query $query a valid WordPress query or an A_Z_Listing\Query instance.
* @return int the number of letters
*/
function get_the_a_z_letter_count( $query = null ): int {
return a_z_listing_cache( $query )->num_letters();
}
/**
* Print the current letter ID
*
* @since 0.7
* @param array|string|WP_Query|\A_Z_Listing\Query $query either a valid WordPress query or an A_Z_Listing\Query instance.
* @return void
*/
function the_a_z_letter_id( $query = null ) {
a_z_listing_cache( $query )->the_letter_id();
}
/**
* Retrieve the current letter ID
*
* @since 0.7
* @param array|string|WP_Query|\A_Z_Listing\Query $query either a valid WordPress query or an A_Z_Listing\Query instance.
* @return string the current letter ID
*/
function get_the_a_z_letter_id( $query = null ): string {
return a_z_listing_cache( $query )->get_the_letter_id();
}
/**
* Print the current letter title
*
* @since 0.7
* @param array|string|WP_Query|\A_Z_Listing\Query $query either a valid WordPress query or an A_Z_Listing\Query instance.
* @return void
*/
function the_a_z_letter_title( $query = null ) {
a_z_listing_cache( $query )->the_letter_title();
}
/**
* Retrieve the current letter title
*
* @since 0.7
* @param array|string|WP_Query|\A_Z_Listing\Query $query either a valid WordPress query or an A_Z_Listing\Query instance.
* @return string the letter title
*/
function get_the_a_z_letter_title( $query = null ): string {
return a_z_listing_cache( $query )->get_the_letter_title();
}
/**
* Print the current item title
*
* @since 0.8.0
* @param array|string|WP_Query|\A_Z_Listing\Query $query either a valid WordPress query or an A_Z_Listing\Query instance.
* @return void
*/
function the_a_z_item_title( $query = null ) {
a_z_listing_cache( $query )->the_title();
}
/**
* Retrieve the current item title
*
* @since 0.8.0
* @param array|string|WP_Query|\A_Z_Listing\Query $query either a valid WordPress query or an A_Z_Listing\Query instance.
* @return string the post or taxonomy-term title
*/
function get_the_a_z_item_title( $query = null ): string {
return a_z_listing_cache( $query )->get_the_title();
}
/**
* Print the current item permalink
*
* @since 0.8.0
* @param array|string|WP_Query|\A_Z_Listing\Query $query either a valid WordPress query or an A_Z_Listing\Query instance.
* @return void
*/
function the_a_z_item_permalink( $query = null ) {
a_z_listing_cache( $query )->the_permalink();
}
/**
* Retrieve the current item permalink
*
* @since 0.8.0
* @param array|string|WP_Query|\A_Z_Listing\Query $query either a valid WordPress query or an A_Z_Listing\Query instance.
* @return string the permalink
*/
function get_the_a_z_item_permalink( $query = null ): string {
return a_z_listing_cache( $query )->get_the_permalink();
}
/**
* Print the A-Z Index page content
*
* @since 0.1
* @since 0.8.0 deprecated
* @see the_a_z_listing()
* @deprecated use the_a_z_listing()
* @param array|string|WP_Query|\A_Z_Listing\Query $query either a valid WordPress query or an A_Z_Listing\Query instance.
* @return void
*/
function the_az_listing( $query = null ) {
_deprecated_function( __FUNCTION__, '0.8.0', 'the_a_z_listing' );
the_a_z_listing( $query );
}
/**
* Print the A-Z Index page content
*
* @since 0.8.0
* @param array|string|WP_Query|\A_Z_Listing\Query $query a valid WordPress query or an A_Z_Listing\Query instance.
* @param bool $use_cache use the plugin's in-built query cache.
* @return void
*/
function the_a_z_listing( $query = null, bool $use_cache = true ) {
a_z_listing_cache( $query, '', $use_cache )->the_listing();
}
/**
* Retrieve the A-Z Index page content
*
* @since 0.1
* @since 0.8.0 deprecated
* @see get_the_a_z_listing()
* @deprecated use get_the_a_z_listing()
* @param array|string|WP_Query|\A_Z_Listing\Query $query either a valid WordPress query or an A_Z_Listing\Query instance.
* @return string The listing html content ready for echoing to the page.
*/
function get_the_az_listing( $query = null ): string {
_deprecated_function( __FUNCTION__, '0.8.0', 'get_the_a_z_listing' );
return get_the_a_z_listing( $query );
}
/**
* Retrieve the index of posts ordered and segmented alphabetically
*
* @since 0.8.0
* @param array|string|WP_Query|\A_Z_Listing\Query $query a valid WordPress query or an A_Z_Listing\Query instance.
* @param bool $use_cache use the plugin's in-built query cache.
* @return string The listing html content ready for echoing to the page.
*/
function get_the_a_z_listing( $query = null, bool $use_cache = true ): string {
return a_z_listing_cache( $query, '', $use_cache )->get_the_listing();
}
/**
* Print the A-Z Letter list
*
* @since 0.7
* @since 0.8.0 deprecated
* @see the_a_z_letters()
* @deprecated use the_a_z_letters()
* @param array|string|WP_Query|\A_Z_Listing\Query $query either a valid WordPress query or an A_Z_Listing\Query instance.
* @param bool|string $target URL of the page to send the browser when a letter is clicked.
* @param string $styling unused.
* @return void
*/
function the_az_letters( $query = null, $target = false, string $styling = '' ) {
_deprecated_function( __FUNCTION__, '0.8.0', 'the_a_z_letters' );
if ( false === $target ) {
$target = '';
}
the_a_z_letters( $query, (string) $target, $styling );
}
/**
* Print the A-Z Letter list
*
* @since 0.8.0
* @param array|string|WP_Query|\A_Z_Listing\Query $query a valid WordPress query or an A_Z_Listing\Query instance.
* @param string $target URL of the page to send the browser when a letter is clicked.
* @param string $styling unused.
* @param bool $use_cache use the plugin's in-built query cache.
* @return void
*/
function the_a_z_letters( $query = null, string $target = '', string $styling = '', bool $use_cache = true ) {
echo get_the_a_z_letters( $query, $target, $styling, $use_cache ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Retrieve the A-Z Letter list
*
* @since 0.7
* @since 0.8.0 deprecated
* @see get_the_a_z_letters()
* @deprecated use get_the_a_z_letters()
* @param array|string|WP_Query|\A_Z_Listing\Query $query a valid WordPress query or an A_Z_Listing\Query instance.
* @param bool|string $target URL of the page to send the browser when a letter is clicked.
* @param string $styling unused.
* @return string HTML ready for echoing containing the list of A-Z letters with anchor links to the A-Z Index page.
*/
function get_the_az_letters( $query = null, $target = false, string $styling = '' ): string {
_deprecated_function( __FUNCTION__, '0.8.0', 'get_the_a_z_letters' );
if ( false === $target ) {
$target = '';
}
return get_the_a_z_letters( $query, (string) $target, $styling );
}
/**
* Retrieve the A-Z Letter list
*
* @since 0.8.0
* @param array|string|WP_Query|\A_Z_Listing\Query $query a valid WordPress query or an A_Z_Listing\Query instance.
* @param string $target URL of the page to send the browser when a letter is clicked.
* @param string $styling unused.
* @param bool $use_cache use the plugin's in-built query cache.
* @return string HTML ready for echoing containing the list of A-Z letters with anchor links to the A-Z Index page.
*/
function get_the_a_z_letters( $query = null, string $target = '', string $styling = '', bool $use_cache = true ): string {
return a_z_listing_cache( $query, '', $use_cache )->get_the_letters( $target, $styling );
}
/**
* Get a saved copy of the A_Z_Listing instance if we have one, or make a new one and save it for later
*
* @param array|string|WP_Query|\A_Z_Listing\Query $query A valid WordPress query or an A_Z_Listing instance.
* @param string $type The type of items displayed in the listing: 'terms' or 'posts'.
* @param bool $use_cache Try to use a caching plugin. See https://a-z-listing.com/ for the caching plugin we created to work with this feature.
* @return \A_Z_Listing\Query A new or previously-saved instance of A_Z_Listing using the provided construct_query
*/
function a_z_listing_cache( $query = null, string $type = '', bool $use_cache = true ) {
return new \A_Z_Listing\Query( $query, $type, $use_cache );
}
/**
* Check value for truthiness
*
* @since 2.1.0
* @param string|int|bool $value The value to check for truthiness.
* @return bool The truthiness of the value.
*/
function a_z_listing_is_truthy( $value ): bool {
if (
'1' === $value ||
'on' === $value ||
'yes' === $value ||
'true' === $value ||
1 === $value ||
true === $value
) {
return true;
}
return false;
}enqueues.php
<?php declare(strict_types=1);
/**
* A-Z Listing Styles and Scripts enqueue functions
*
* @package a-z-listing
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Register default A-Z stylesheet, jQuery-UI Tabs script and add our enqueue
* functions to thewp_enqueue_scriptsaction
*
* @since 2.0.0 Renamed from a_z_listing_add_styling. Added jQuery-UI Tabs support.
* @return void
*/
function a_z_listing_do_enqueue() {
wp_register_style(
'a-z-listing',
plugins_url( 'css/a-z-listing-default.css', A_Z_LISTING_PLUGIN_FILE ),
array( 'dashicons' ),
A_Z_LISTING_VERSION
);
wp_register_style(
'a-z-listing-admin',
plugins_url( 'css/a-z-listing-customize.css', A_Z_LISTING_PLUGIN_FILE ),
array(),
A_Z_LISTING_VERSION
);
wp_register_script(
'a-z-listing-tabs',
plugins_url( 'scripts/a-z-listing-tabs.js', A_Z_LISTING_PLUGIN_FILE ),
array( 'jquery', 'jquery-ui-tabs' ),
A_Z_LISTING_VERSION,
true
);
wp_register_script(
'a-z-listing-widget-admin',
plugins_url( 'scripts/a-z-listing-widget-admin.js', A_Z_LISTING_PLUGIN_FILE ),
array( 'jquery', 'jquery-ui-autocomplete' ),
A_Z_LISTING_VERSION,
true
);
wp_localize_script(
'a-z-listing-widget-admin',
'a_z_listing_widget_admin',
array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
)
);
wp_register_script(
'a-z-listing-scroll-fix',
plugins_url( 'scripts/a-z-listing-scroll-fix.js', A_Z_LISTING_PLUGIN_FILE ),
array(),
A_Z_LISTING_VERSION,
true
);
wp_localize_script(
'a-z-listing-scroll-fix',
'a_z_listing_scroll_fix',
array(
'offset' => -120,
)
);
$add_styles = get_option( 'a-z-listing-add-styling', true );
/**
* Determine whether to add default listing styling
*
* @param bool|string|int $add_styles True to add default styling, false-like to disable.
* @since 1.7.1
*/
$add_styles = apply_filters( 'a_z_listing_add_styling', $add_styles );
/**
* Determine whether to add default listing styling
*
* @param bool|string|int $add_styles True to add default styling, false-like to disable.
* @since 1.7.1
*/
$add_styles = apply_filters( 'a-z-listing-add-styling', $add_styles ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
if ( defined( 'A_Z_LISTING_LOG' ) && A_Z_LISTING_LOG ) {
do_action( 'a_z_listing_log', 'A-Z Listing: Add Styles', $add_styles );
}
if ( a_z_listing_is_truthy( $add_styles ) && ! has_action( 'wp_enqueue_scripts', 'a_z_listing_enqueue_styles' ) ) {
add_action( 'wp_enqueue_scripts', 'a_z_listing_enqueue_styles' );
}
add_action( 'customize_controls_enqueue_scripts', 'a_z_listing_customize_enqueue_styles' );
$tabify = get_option( 'a-z-listing-add-tabs', false );
/**
* Determine whether to add jQuery-UI Tabs
*
* @param bool|string|int $tabify True to add jQuery-UI Tabs, false-like to disable.
* @since 2.0.0
*/
$tabify = apply_filters( 'a_z_listing_tabify', $tabify );
/**
* Determine whether to add jQuery-UI Tabs
*
* @param bool|string|int $tabify True to add jQuery-UI Tabs, false-like to disable.
* @since 2.0.0
*/
$tabify = apply_filters( 'a-z-listing-tabify', $tabify ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
if ( defined( 'A_Z_LISTING_LOG' ) && A_Z_LISTING_LOG ) {
do_action( 'a_z_listing_log', 'A-Z Listing: Tabify', $tabify );
}
if ( a_z_listing_is_truthy( $tabify ) && ! has_action( 'wp_enqueue_scripts', 'a_z_listing_enqueue_tabs' ) ) {
add_action( 'wp_enqueue_scripts', 'a_z_listing_enqueue_tabs' );
}
}
add_action( 'init', 'a_z_listing_do_enqueue' );
You must be logged in to reply to this topic.