Suporte » Plugins » Código causando lentidão – WP

  • Resolvido f5atualizese

    (@f5atualizese)


    Olá Pessoal, dentro do meu tema. Tenho um arquivo que esta causando uma enorme lentidão quando o site passa de 150 acessos simultâneos. o error_log mostra que o erro esta dentro deste arquivo, mas não consigo identificar qual é o erro e o que possa estar causando essa lentidão.

    obs o error_log não mostra a linha, mas o erro é ('passei do if',0

    Segue o código do arquivo class-rascunho.php

    <?php
    /* Class auto draft */
    class Brasa_guerreiro_Rascunho{
    	public function __construct(){
    		//add_action( 'save_post', array(&$this, 'save') );
    		add_action( 'wp', array($this,'add_cron') );
    		add_action( 'brasa_anuncio_cron', array($this,'do_cron') );
    		add_action( 'pre_get_posts', array($this, 'show_draft') );
    		add_action( 'wp_ajax_save_post_guerreiro', array($this, 'save_guerreiro') );
    		add_action( 'wp_ajax_save_post_anuncio', array($this, 'save_anuncio') );
    	}
    	public function add_cron(){
    		if( wp_next_scheduled( 'brasa_anuncio_cron' ) )
    			return;
    
    		wp_schedule_event( time(), 'hourly', 'brasa_anuncio_cron' );
    	}
    	private function invalid_date($date){
    		$today = new DateTime();
    		$today->modify('-1 day');
    		$post_date = new DateTime();
    		$post_date->setTimestamp($date);
    
    		if($post_date > $today){
    			return true;
    		}
    		else{
    			return false;
    		}	
    	}
    	public function do_cron(){
    		// WP_Query arguments
    		$today = new DateTime();
    		$today->modify('-1 day');
    
    		$args = array (
    			'suppress_filters'       => true,
    			'post_type'              => 'anuncio',
    			'post_status'            => 'publish',
    			'posts_per_page'         => -1,
    			'tax_query' => array(
    				array(
    					'taxonomy' => 'tipo-anuncio',
    					'field'    => 'slug',
    					'terms'    => 'vitrine-guerreiros',
    				),
    			),
    			'meta_query' => array(
    				array(
    				    'key' => 'wpcf-data-fim',
    				    'compare' => '<',
    					'value' => $today->getTimestamp()
    				),
    			),
    		);
    
    		// The Query
    		$posts = get_posts( $args );
    
    		if(!is_array($posts) || empty($posts))
    			return;
    		
    		foreach($posts as $anuncio){
    		    // The Loop
    		    if(!get_post_meta( $anuncio->ID, '_wpcf_belongs_guerreiro_id', true ))
    		    	continue;
    		    if($this->invalid_date(get_post_meta( $anuncio->ID, 'wpcf-data-fim', true)))
    		    	continue;
    
    	        error_log('passei do if',0);
    	        global $wpdb;
    
    			//wp_remove_object_terms( $anuncio->ID, 'vitrine-guerreiros', 'tipo-anuncio' );
    				
    		    $_edited = $wpdb->update(
    		    	$wpdb->posts,
    		    	array( 'post_status' => 'draft' ),
    		    	array( 'ID' => get_post_meta( $anuncio->ID, '_wpcf_belongs_guerreiro_id', true ) )
    		    );
    			error_log(print_r($_edited,true),0);
    			error_log(print_r('id::'.get_post_meta( $anuncio->ID, '_wpcf_belongs_guerreiro_id', true ),true),0);	
    
    		}
    	}
    	public function save($post_id){
    		$id = $_POST['post_ID'];
    		if (defined('DOING_AJAX') && DOING_AJAX)
    			return;
    	    if (isset($_GET['action']) && $_GET['action'] == 'trash')
    			return;
    	    if($_POST['post_type'] == 'anuncio'){
    	    	$this->save_anuncio($id);
    	    }	
    	    if($_POST['post_type'] == 'guerreiro'){
    	    	$this->save_guerreiro($id);
    	    }	
    	}
    	public function save_anuncio(){
    		$post_id = $_POST['post_ID'];
    		$have_term = false; 
    
    		// WP_Query arguments
    		$today = new DateTime();
    		$today->modify('-1 day');
    		$args = array (
    			'post_type'              => 'anuncio',
    			'posts_per_page'         => -1,
    			'meta_query'             => array(
    				array(
    					'key'       => '_wpcf_belongs_guerreiro_id',
    					'value'     => get_post_meta( $post_id, '_wpcf_belongs_guerreiro_id', true ),
    				),
    				array(
    				    'key' => 'wpcf-data-fim',
    				    'compare' => '>=',
    					'value' => $today->getTimestamp()
    				),
    			),
    		);
    
    		// The Query
    		$query = new WP_Query( $args );
    
    		// The Loop
    		if ( $query->have_posts() ) {
    			while ( $query->have_posts() ) {
    				$query->the_post();
    				if(has_term( 'vitrine-guerreiros', 'tipo-anuncio' )){
    					$have_term = true;
    					break;
    				}
    			}
    			wp_reset_postdata();
    		}
    		global $wpdb;
    		if($have_term == true){
    			$_edited = $wpdb->update(
    		    	$wpdb->posts,
    		    	array( 'post_status' => 'publish' ),
    		    	array( 'ID' => get_post_meta( $post_id, '_wpcf_belongs_guerreiro_id', true ) )
    		    );
    		}
    		else{
    		    $_edited = $wpdb->update(
    		    	$wpdb->posts,
    		    	array( 'post_status' => 'draft' ),
    		    	array( 'ID' => get_post_meta( $post_id, '_wpcf_belongs_guerreiro_id', true ) )
    		    );
    		}
    	}
    	public function save_guerreiro(){
    		$post_id = $_POST['post_id'];
    		$have_term = false;
    		// WP_Query arguments
    		$today = new DateTime();	
    		$today->modify('-1 day');
    
    		$args = array (
    			'post_type'              => 'anuncio',
    			'meta_query'             => array(
    				array(
    					'key'       => '_wpcf_belongs_guerreiro_id',
    					'value'     => $post_id,
    				),
    				array(
    				    'key' => 'wpcf-data-fim',
    				    'compare' => '>=',
    					'value' => $today->getTimestamp()
    				),
    			),
    		);
     
    		// The Query
    		$query = new WP_Query( $args );
    		if ( $query->have_posts() ){
    			while ( $query->have_posts() ){
    				$query->the_post();
    				if(has_term( 'vitrine-guerreiros', 'tipo-anuncio' )){
    					$have_term = true;
    					break;
    				}
    			}
    			wp_reset_postdata();
    		}
    		global $wpdb;
    		if($have_term == true){
    			$_edited = $wpdb->update(
    		    	$wpdb->posts,
    		    	array( 'post_status' => 'publish' ),
    		    	array( 'ID' => $post_id )
    		    );
    		}
    		else{
    			$_edited = $wpdb->update(
    		    	$wpdb->posts,
    		    	array( 'post_status' => 'draft' ),
    		    	array( 'ID' =>  $post_id )
    		    );
    		}
    	}
    	public function show_draft($query){
    		if(!is_admin())
    			return;
    
    		if ( isset($_GET['action']) && $_GET['action'] == 'edit' || isset($_GET['post_type']) && $_GET['post_type'] == 'anuncio' && !isset($_GET['action']) ){
    			$query->set('post_status','any');
    		}
    	}
    }
    new Brasa_guerreiro_Rascunho();
    ?>
Visualizando 1 resposta (de um total de 1)
Visualizando 1 resposta (de um total de 1)
  • O tópico ‘Código causando lentidão – WP’ está fechado para novas respostas.