/home/apktxduj/public_html/wp-content/themes/appyn-15/includes/widget-ultimos-posts.php
<?php

if( ! defined( 'ABSPATH' ) ) die ( '✋' );

class ultimos_posts_Widget extends WP_Widget {
	public function __construct() {
		parent::__construct(
	 		'ultimos_posts_widget', 
			'[Appyn] '.__( 'Últimas Aplicaciones', 'appyn' ),
			array('description' => __( 'Muestra las últimas aplicaciones de tu sitio', 'appyn' ),)
		);
	}
	public function widget($args, $instance) {
		extract($args);
		$title = '';
		$instance = array_filter($instance);
		
		if( isset($instance['title']) ){
			$title = apply_filters('widget_title', $instance['title']);
		} else {
			$title = __( 'Últimas Aplicaciones', 'appyn' );	
		}
		echo $before_widget; 
		echo $before_title.$title.$after_title; 
		echo '<div class="widget-content">';
		global $wpdb, $post;
		$n = 0;
		$args = array( 'orderby' => 'post_date', 'ignore_sticky_posts' => 1 );

		if( ! empty($instance['cantidad']) ) {
			$args['posts_per_page'] = ( $instance['cantidad'] ) ? $instance['cantidad'] : 5;
		}

		if( ! empty($instance['categoria']) ) {
			$args['cat'] = array($instance['categoria']);
		}
		
		if( appyn_options( 'versiones_mostrar_widgets') == 1 ) {
			$args['post_parent'] = 0;
		}
		
		if( ! empty($instance['mod_apps_widget']) ) {
			$args['meta_query'] = array(
				array(
				'key' => 'app_type',
				'value' => array(1),
				'compare' => 'IN',
				)
			);
		}

		$query = new WP_Query($args);
		echo '<ul>';
		if( $query->have_posts() ) {
			while($query->have_posts()): $query->the_post();
  				get_template_part('template-parts/loop/app-widget');
			endwhile;
		} else { 
			echo '<span class="noentry">'.__( 'No hay entradas', 'appyn' ).'</span>';
		} 
		echo '</ul></div>';
		wp_reset_query(); 
		echo $after_widget;
	}
	public function update($new_instance, $old_instance) {
		$instance = array();
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['cantidad'] = strip_tags($new_instance['cantidad']);
		$instance['categoria'] = $new_instance['categoria'];
		if(!is_numeric(strip_tags($new_instance['cantidad']))){
			$instance['cantidad'] = '0';
		}		
		return $instance;
	}
	public function form($instance) {	
        $defaults = array( 'title' => __( 'Últimas Aplicaciones' , 'appyn' ), 'cantidad' => 5 );
		$instance = wp_parse_args( (array) $instance, $defaults );	
		?>
		 <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php echo __( 'Título', 'appyn' ); ?>:</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 esc_attr($instance["title"]); ?>" />
         </p> 
         <p>
            <label for="<?php echo $this->get_field_id('cantidad'); ?>"><?php echo __( 'De una categoría', 'appyn' ); ?>:</label>
            <select name="<?php echo $this->get_field_name('categoria'); ?>">
	            	<option value="0"><?php echo __( 'Ninguna', 'appyn' ); ?></option>
            	<?php $categories = get_categories();
				foreach($categories as $category) { 
					if( isset($instance['categoria']) ) {
						if( $instance['categoria'] == $category->term_id ) { ?>
	            		<option value="<?php echo $category->term_id; ?>" selected><?php echo $category->name; ?></option>
                    <?php }
					} else { ?>
	            		<option value="<?php echo $category->term_id; ?>"><?php echo $category->name; ?></option>
                    <?php } ?>
                <?php } ?>
            </select>
         </p> 
         <p>
            <label for="<?php echo $this->get_field_id('cantidad'); ?>"><?php echo __( 'Número de entradas a mostrar', 'appyn' ); ?></label>
            <input id="<?php echo $this->get_field_id('cantidad'); ?>" name="<?php echo $this->get_field_name('cantidad'); ?>" type="text" value="<?php if(esc_attr($instance["cantidad"])) echo esc_attr($instance["cantidad"]); else echo '5'; ?>" size="3" maxlength="2" />
         </p> 
         <?php 
	}
}

add_action('widgets_init', function() { register_widget("ultimos_posts_Widget"); });