php [WordPress] - 列出图像并通过ajax按术语对它们进行排序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php [WordPress] - 列出图像并通过ajax按术语对它们进行排序相关的知识,希望对你有一定的参考价值。

<?php
/*
Plugin Name: WPMUDEV - List Attachmetns
Plugin URI: https://premium.wpmudev.org/
Description: List attachments and sort them via ajax
Author: Panos Lyrakis @ WPMUDEV
Author URI: https://premium.wpmudev.org/
License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'WPMUDEV_List_Images' ) ) {

	class WPMUDEV_List_Images {

		private static $_instance = null;
		private static $_post_type = null;
		private static $_per_page = null;

		public static function get_instance() {

			if( is_null( self::$_instance ) ){
				self::$_instance = new WPMUDEV_List_Images();
			}
			return self::$_instance;
		}

		public function __construct() {

			self::$_post_type = 'post';
			self::$_per_page = 25;

			add_shortcode( 'wpmudev_list_images', array( $this, 'list_images_sh' ) );

			add_action( 'wp_ajax_wpmudev_list_attachments_per_term', array( $this, 'ajax_list_images' ) );
			add_action( 'wp_ajax_nopriv_wpmudev_list_attachments_per_term', array( $this, 'ajax_list_images' ) );
			add_action( 'wp_head', array( $this, 'load_styles' ) );
		}

		public function list_images_sh( $atts ){
			return $this->list_images( $atts );
		}

		public function ajax_list_images(){

			check_ajax_referer( 'wpmudev_list_attachments_per_term', 'security' );

			$args = array(
				'taxonomy' 	=> sanitize_text_field( $_POST['taxonomy'] ),
				'term'		=> (int)$_POST[ 'term' ]
			);

			$return = array(
				'message'		=> 'ERROR'
			);

			if( $images_list = $this->list_images( $args, false ) ){
				$return = array(
					'message'	=> 'SUCCESS',
					'html'		=> $images_list
				);
			}	

			echo json_encode( $return );
			exit();
		}

		public function list_images( $args = array(), $full_response = true ){

			$image_query = $post_parents = false;

			$taxonomy = isset( $args[ 'taxonomy' ] ) ? $args[ 'taxonomy' ] : false;
			$term = isset( $args[ 'term' ] ) ? $args[ 'term' ] : false;

			if( ! $taxonomy || ! $term ){

				$parent_args = [
					'posts_per_page' => self::$_per_page,
					'fields' => 'ids',
					'post_type' => self::$_post_type,
				];

				$post_parents = new WP_Query( $parent_args );

			}

			else{

				$parent_args = [
					'posts_per_page' => self::$_per_page,
					'fields' => 'ids',
					'post_type' => self::$_post_type,
					'tax_query' => [
						[
							'taxonomy' => $taxonomy,
							'field' => 'term_id',
							'terms' => $term,
							'include_children' => false
						],
					],
				];

				$post_parents = new WP_Query( $parent_args );
				
			}

			if( $post_parents && isset( $post_parents->posts ) ) {

				$attachement_ids = array();

				foreach( $post_parents->get_posts() as $key => $post_id ){
				
					$attachments_id = get_post_meta( $post_id, '_thumbnail_id', true );
					if( $attachments_id ){
						$attachement_ids[] = $attachments_id;
					}
					
				}

				$args = [
					'post_type' => 'attachment',
					'post_mime_type' => 'image',
					'post_status' => 'inherit',
					'fields' => 'ids',
					'post__in' => $attachement_ids
				];

			}
			else{

				$args = [
					'post_type' => 'attachment',
					'post_mime_type' => 'image',
					'post_status' => 'inherit',
					'fields' => 'ids'
				];
			}


/*
			if( ! $taxonomy || ! $term ){

				$parent_args = [
					'posts_per_page' => -1,
					'fields' => 'ids',
					'post_type' => self::$_post_type,
				];

				$post_parents = new WP_Query( $parent_args );

			}
			else{

				$parent_args = [
					'posts_per_page' => -1,
					'fields' => 'ids',
					'post_type' => self::$_post_type,
					'tax_query' => [
						[
							'taxonomy' => $taxonomy,
							'field' => 'term_id',
							'terms' => $term,
							'include_children' => false
						],
					],
				];

				$post_parents = new WP_Query( $parent_args );
				
			}

			if( $post_parents && isset( $post_parents->posts ) ) {

				$args = [
					'post_type' => 'attachment',
					'post_mime_type' => 'image',
					'post_status' => 'inherit',
					'fields' => 'ids',
					'post_parent__in' => $post_parents->posts,
				];
				

			}
			else{

				$args = [
					'post_type' => 'attachment',
					'post_mime_type' => 'image',
					'post_status' => 'inherit',
					'fields' => 'ids'
				];
			}
*/

			$image_query = new WP_Query( $args );
			$out = '';

			if( $image_query->have_posts() ){

				while( $image_query->have_posts() ) {

					$image_query->the_post();
					$imgurl = wp_get_attachment_url( get_the_ID() );					
					$parentimageurl = get_permalink( wp_get_post_parent_id( get_the_ID() ) );

					$out .= '<div class="talentgallery-item">';
					$out .= '<a href="';
					$out .= $parentimageurl.'">'.'<img class="talent" src="'.$imgurl.'">'.'</a>';
					$out .= '</div>';
				}
			}


			if( ! $full_response ){
				return $out;
			}

			$out = '<div class="spinner"></div><div class="wpmudev-posts-attachments-list">' . $out . '</div>';

			return $this->category_filter() . $out . $this->load_js();


		}

		public function category_filter(){
			
			$out = '';
			$taxonomies = get_object_taxonomies( self::$_post_type, 'object' );

			foreach( $taxonomies as $tax_name => $tax ){

				$terms = $this->get_tax_terms( $tax_name );

				if( $terms ){
					$out .= '<div class="taxonomy-filter taxonomy-' . $tax_name . '">';
						$out .= '<label class="taxonomy-filter-title">' . $tax->label . '</label>';
						$out .= $terms;
					$out .= '</div>';
				}
				

			}

			return $out;
		}

		public function get_tax_terms( $tax_name ){

			$out = false;

			$terms = get_terms( array(
			    'taxonomy' => $tax_name,
			    'hide_empty' => false,
			) );

			if( ! empty( $terms ) ){

				$out = '<select class="tax-filter-term" data-tax="' . $tax_name . '">';

				$out .= '<option value="">Any</option>';

				foreach( $terms as $term ){
					$out  .= '<option value="' . $term->term_id . '">' . $term->name . '</option>';
				}

				$out .= '</select>';

			}			

			return $out;

		}

		public function load_js(){

			$ajax_url = admin_url( 'admin-ajax.php' );
			$ajax_nonce = wp_create_nonce( "wpmudev_list_attachments_per_term" );

			?>
			<script type="text/javascript">
				(function($){
					$( document ).ready(function(){
						$( '.tax-filter-term' ).on( 'change', function(){

							var taxonomy = $( this ).data( 'tax' );
							var term = $( this ).val();
							var data = {
								action: 'wpmudev_list_attachments_per_term',
								security: '<?php echo $ajax_nonce; ?>',
								taxonomy: taxonomy,
								term: term
							};

							$( '.wpmudev-posts-attachments-list' ).fadeOut( 250 );
							$( '.spinner' ).fadeIn( 100 );


							$.ajax({
						        type: "post",
						        dataType: "json",
						        url: '<?php echo $ajax_url ?>',
						        data: data,
						        success: function( response ){						        	
						            if( response.message == "SUCCESS" ){
										$( '.wpmudev-posts-attachments-list' ).html( response.html ). fadeIn( 250 );
									}
									$( '.spinner' ).fadeOut( 100 );
						        }
						    });
							
						});
					});
				})(jQuery)
			</script>
			<?php

		}

		public function load_styles(){
			?>
			<style type="text/css">
				.spinner {
					background: url( '<?php echo admin_url(); ?>/images/spinner-2x.gif') no-repeat;
					background-size: 32px 32px;
					display: none;
					width: 32px;
					height: 32px;
					margin: 0 auto;
				}
			</style>
			<?php
		}

		

	}

	add_action( 'plugins_loaded', function(){
		$GLOBALS['WPMUDEV_List_Images'] = WPMUDEV_List_Images::get_instance();
	}, 10 );

}

以上是关于php [WordPress] - 列出图像并通过ajax按术语对它们进行排序的主要内容,如果未能解决你的问题,请参考以下文章

php 列出所有WordPress用户角色

php 在wordpress中按类别列出帖子

PHP 列出Wordpress中的钩子函数

PHP 按类别列出WordPress帖子

PHP 列出父页面中的WordPress子页面

PHP Wordpress - 列出作者对作者页面的评论