HEX
Server: nginx/1.16.1
System: Linux VM-0-14-centos 4.18.0-348.7.1.el8_5.x86_64 #1 SMP Wed Dec 22 13:25:12 UTC 2021 x86_64
User: www (1000)
PHP: 8.3.31
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/wwwroot/1.scey.cn/wp-content/themes/Leading/template-parts/widget/widgets-post.php
<?php

//date_default_timezone_set("Asia/Shanghai");

class xintheme_post_tools extends WP_Widget {

	/**
	 * Sets up the widgets name etc
	 */
	public function __construct() {
		$widget_ops = array( 
			'classname' => 'xintheme_post_tools',
			'description' => '「XinTheme」主题自带文章小工具,纯标题样式。',
		);
		parent::__construct( 'xintheme_post_tools', '「XinTheme」文章(纯标题)', $widget_ops );
	}

	/**
	 * Outputs the content of the widget
	 *
	 * @param array $args
	 * @param array $instance
	 */
	public function widget( $args, $instance ) {

		extract($args);
		$number   	= $instance['num'];
		$who   		= $instance['who'];
		$days  		= $instance['days'];
		$cat 		= $instance['cat'];
		$post_id 	= $instance['post_id'];
		$strtotime 	= strtotime('-'.$days.' days');
		$title 		= apply_filters('widget_name', $instance['title']);

		switch ($who) {
			case 'new_posts':
				$args = array(
					'type'                => 'post',
					'showposts'           => $number,
					'cat'              	  => $cat,			          
					'ignore_sticky_posts' => 1
				);
				break;

			case 'random_posts':
				$args = array(
					'type'                => 'post',
					'showposts'           => $number,
					'orderby'             => 'rand',
					'cat'              	  => $cat,					          
					'ignore_sticky_posts' => 1
				);
				break;

			case 'views_posts':
				$args = array(
					'type'                => 'post',
					'showposts'           => $number,
					'orderby'             => 'meta_value_num',
					'cat'              	  => $cat,	
					'meta_key'            => 'views',				          
					'ignore_sticky_posts' => 1,
					'date_query'          => array(
						array(
							'before' => date( 'Y-m-d H:00:00', time() ),
						),
						array(
							'after' => date( 'Y-m-d H:00:00', $strtotime ),
						),
					),
				);
				break;

			default:
				$args = null;
				break;
		}

		if( $args ){
			$widget_query = wpjam_query($args);
		}
?>

<div class="right_news">
	<div class="right_news_title">
		<p>
			<?php echo $title; ?>
		</p>
	</div>
	<div class="right_news_list">
		<?php
		if( $args ){ 
			if ($widget_query->have_posts()) :
			while ($widget_query->have_posts()) : $widget_query->the_post();
		?>
		<div class="right_news_list_one">
			<div class="right_news_con">
				<a href="<?php the_permalink(); ?>">
					<p><?php the_title(); ?></p>
				</a>
			</div>
		</div>
		<?php
			endwhile;
			wp_reset_query();
			else :
				echo '<p>抱歉,没有找到文章!</p>';
			endif;
			}else{
				echo '<p>抱歉,没有找到文章!</p>';
			}
		?> 
	</div>
</div>

<?php
	}

	/**
	 * Outputs the options form on admin
	 *
	 * @param array $instance The widget options
	 */
	public function form( $instance ) {
		$instance = wp_parse_args( (array) $instance, array( 
				'title' => '文章小工具',
				'num'   => 5,
				'days'  => 30,
				'who'   => 'new_posts',
				'style' => '1',
			) 
		);
?>
		<p>
			<label> 标题:
				<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance['title']; ?>" />
			</label>
		</p>
		<p>
			<label> 显示数量:
				<input class="widefat" id="<?php echo $this->get_field_id('num'); ?>" name="<?php echo $this->get_field_name('num'); ?>" type="number" step="1" min="4" value="<?php echo $instance['num']; ?>" />
			</label>
		</p>
		<p>
		<p>
			<label> 显示什么:
				<select class="widefat xintheme_select_handle" id="<?php echo $this->get_field_id('who'); ?>" name="<?php echo $this->get_field_name('who'); ?>">
					<option <?php mi_selected( $instance['who'], 'new_posts' ); ?> value="new_posts">最新文章</option>
					<option <?php mi_selected( $instance['who'], 'random_posts' ); ?> value="random_posts">随机文章</option>
					<option <?php mi_selected( $instance['who'], 'views_posts' ); ?> value="views_posts">浏览最多</option>
					<option <?php mi_selected( $instance['who'], 'post_id' ); ?> value="post_id">指定文章</option>
				</select>
			</label>
		</p>
		<?php if( $instance['who'] == 'new_posts' || $instance['who'] == 'random_posts' || $instance['who'] == 'post_id' ){ ?>
		<p id="<?php echo $this->get_field_id('who'); ?>-box" style="display: none">
		<?php }else{ ?>
		<p id="<?php echo $this->get_field_id('who'); ?>-box">
		<?php } ?>
			<label>
				显示
				<input class="tiny-text" id="<?php echo $this->get_field_id('days'); ?>" name="<?php echo $this->get_field_name('days'); ?>" type="number" step="1" min="1" value="<?php echo $instance['days']; ?>" style="width: 70px;" />
				<span>
				<?php 
					switch ($instance['who']) {
						case 'like_posts':
							echo '天内点赞最多的文章';
							break;
						case 'views_posts':
							echo '天内浏览最多的文章';
							break;
						default:
							break;
					}
				?>
				</span>		
			</label>
		</p>

		<?php if( $instance['who'] == 'post_id' ){ ?>
		<p id="<?php echo $this->get_field_id('who'); ?>-box-2">
		<?php }else{ ?>
		<p id="<?php echo $this->get_field_id('who'); ?>-box-2" style="display: none">
		<?php }?>
			<label>
				指定文章ID:
				<input style="width:100%;" id="<?php echo $this->get_field_id('post_id'); ?>" name="<?php echo $this->get_field_name('post_id'); ?>" type="text" value="<?php echo $instance['post_id']; ?>" size="24" />
				格式:1,2 &nbsp;表示只显示文章ID为1,2分类的文章,注意:多个ID之间用英文逗号隔开!
			</label>
		</p>
		
		<?php if( $instance['who'] == 'post_id' ){ ?>
		<p id="<?php echo $this->get_field_id('who'); ?>-box-3" style="display: none">
		<?php }else{ ?>
		<p id="<?php echo $this->get_field_id('who'); ?>-box-3">
		<?php }?>
			<label>
				分类限制:
				<input style="width:100%;" id="<?php echo $this->get_field_id('cat'); ?>" name="<?php echo $this->get_field_name('cat'); ?>" type="text" value="<?php echo $instance['cat']; ?>" size="24" />
				格式:1,2 &nbsp;表示只显示分类ID为1,2分类的文章,格式:-1,-2 &nbsp;表示排除分类ID为1,2的文章,也可以直接写1或者-1;注意:多个ID之间用英文逗号隔开!
			</label>
		</p>

<?php
	}

}
add_action('widgets_init', function(){register_widget('xintheme_post_tools' );});

function mi_selected( $t, $i ){
	if( $t == $i ){
		echo 'selected';
	}
}

/**
 * 加载一段JS
 */
function xintheme_select_handle() {  
?>
	<script>

		jQuery( document ).ready( function() {
			if( jQuery('.xintheme_select_handle').length > 0 ){
				jQuery('.xintheme_select_handle').each(function(index, el) {
					xintheme_select_handle( jQuery(this).attr('id') );
				});
			}

		});

		jQuery(document).on('change', '.xintheme_select_handle', function(event) {
			event.preventDefault();
			xintheme_select_handle( jQuery(this).attr('id') );
		});

		function xintheme_select_handle( id ){
			var selected = jQuery('#'+id+' option:selected');
			if( selected.val() == 'comment_posts' || selected.val() == 'like_posts' || selected.val() == 'views_posts' ){
				jQuery('#'+id+'-box label span').text( ' 天内' + selected.text() + '的文章' );
		 		jQuery('#'+id+'-box').show();
		 		jQuery('#'+id+'-box-2').hide();
		 		jQuery('#'+id+'-box-3').show();

		 	}else if(selected.val() == 'post_id'){
		 		jQuery('#'+id+'-box').hide();
		 		jQuery('#'+id+'-box-2').show();
		 		jQuery('#'+id+'-box-3').hide();
		 	}else{
		 		jQuery('#'+id+'-box').hide();
		 		jQuery('#'+id+'-box-2').hide();
		 		jQuery('#'+id+'-box-3').show();
		 	}
		}
	</script>
<?php
}  
add_action( 'admin_footer', 'xintheme_select_handle' );