How to log the results in a file after uploading file/post in the W3TC cdn?

30 Views Asked by At

I am trying to programatically print some logs in a file after uploading files to W3TC CDN.

Files are uploaded to de CDN after a post is saved.

I am trying to make the following wrapper function to trigger W3TC upload() function and excecute in it another function to generate the logs.

function wrapUploadFunction($files, $queue_failed, &$results, $timeout_time = NULL) {
    // new instance of W3TC class
    if (class_exists('\\W3TC\\Cdn_Core')) {
        $instance = new \W3TC\Cdn_Core(); 
    
        // Call upload function
        $return = $instance->upload($files, $queue_failed, $results, $timeout_time);
    
        // Call the function to log uploaded files into the CDN.
        logUploadResult($files, $results);
    
        return $return;
    }

    return false;
}

function logUploadResult($files, $results) {
    // File Path
    $logFile = ABSPATH . 'wp-content/themes/pepe/sf_logs/' . date('d-M-Y') . '.log';

    // Open file
    $logHandle = fopen($logFile, "a");

    if ($logHandle === false) {
        // Stop excecution when opening file fails
        die("No se pudo abrir el archivo de registro");
    }

    // Recorre los archivos y resultados
    for ($i = 0; $i < count($files); $i++) {
        // Generate message
        $logEntry = date(DATE_RFC822) . " - Archivo: " . $files[$i] . " - Resultado: " . $results[$i]['result'];

        if ($results[$i]['result'] != W3TC_CDN_RESULT_OK) {
            // Si hubo un error, agrega el mensaje de error al registro
            $logEntry .= " - Error: " . $results[$i]['error'];
        }

        // Write file entry
        fwrite($logHandle, $logEntry . "\n");
    }

    // Close file
    fclose($logHandle);
}

This is the upload() function from Cdn_Core

public function upload( $files, $queue_failed, &$results, $timeout_time = null ) {
    if ( $this->debug ) {
        Util_Debug::log( 'cdn', 'upload: ' . json_encode( $files, JSON_PRETTY_PRINT ) );
    }

   $cdn           = $this->get_cdn();
   $force_rewrite = $this->_config->get_boolean( 'cdn.force.rewrite' );

   @set_time_limit( $this->_config->get_integer( 'timelimit.cdn_upload' ) );

   $engine = $this->_config->get_string( 'cdn.engine' );
   $return = $cdn->upload( $files, $results, $force_rewrite, $timeout_time );

   if ( ! $return && $queue_failed ) {
       foreach ( $results as $result ) {
           if ( W3TC_CDN_RESULT_OK !== $result['result'] ) {
               $this->queue_add( $result['local_path'], $result['remote_path'], W3TC_CDN_COMMAND_UPLOAD, $result['error'] );
        }
       }
   }

   return $return;
  }

I am trying to debug my functions with x-debug, but seems my functions are never been triggered. Am i missing any steps or am i pointing to an incorrect function from the W3TC core?

0

There are 0 best solutions below