PHP/WordPress Scripting Help


#1

I’m building a PHP script which loops through a WordPress custom post type and pulls out any file attachments added to the post(s) via a Advanced Custom Field (‘downloads’). The idea is that the client uploads PDF files to his custom posts, and on a daily basis this script will run (via cron) to zip all the attachments added to each custom post into one zip file, which he can then make available to download for his clients.

I tried it locally on Vargant+VVV and it worked, however on CW(DO) it doesn’t and, as i’ve found out, ZipArchive isn’t exactly verbose in telling you where you went wrong. I have a feeling that maybe it’s $_SERVER[‘DOCUMENT_ROOT’]?

[code]<?php

define(‘WP_USE_THEMES’, false);
require( ‘/home/xxxxx-xxxxx.cloudwaysapps.com/xxxxxxxx/public_html/wp-blog-header.php’ );

$args_brochures = array(
‘post_type’ => ‘furniture’,
‘posts_per_page’=>-1,
);

$query_brochures = new WP_Query( $args_brochures );

if ( $query_brochures->have_posts() ) {

$add_files = array();

while ( $query_brochures->have_posts() ) {

	$query_brochures->the_post();

	if( have_rows( 'downloads' ) ) {
		while ( have_rows( 'downloads' ) ) : the_row();

			$file = get_sub_field( 'file_downloads' );
            $svr_path = rtrim($_SERVER['DOCUMENT_ROOT'], '/');
			$add_files[] = $svr_path . str_replace ( get_site_url(), '', $file['url'] );

		endwhile; 
	}
}

wp_reset_postdata();

var_dump($add_files);

$filename = '/home/xxxxx-xxxxx.cloudwaysapps.com/xxxxxxxx/public_html/image-bank/rianaasiaimports-product-downloads.zip';
$rootPath = realpath('image-bank');

$zip = new ZipArchive();
$zip->open( $filename, ZipArchive::CREATE | ZipArchive::OVERWRITE);

foreach ($add_files as $file_poo) {

    if ( file_exists( $file_poo ) ) {

        $filePath = realpath( $file_poo );
        $relativePath = substr($filePath, strlen($rootPath) + 1);

        echo $relativePath . PHP_EOL;
        $zip->addFile($file_poo, basename($file_poo) );
    
    } else {
        echo 'Nothing to add!';
    }
    
}
$zip->close();

}
?>[/code]

EDIT: Worked it out in the end

[code]<?php

define(‘WP_USE_THEMES’, false);
require( ‘/home/xxxxx-xxxxx.cloudwaysapps.com/xxxxxxxx/public_html/wp-blog-header.php’ );

/*

  • Create a zip file of all our product images
  • The script that creates each products image zip is in page-media.php
    */

$filename_zip = ‘media-images.zip’;
$filename = ‘zips/’ . $filename_zip;

$realpath_file = realpath( $filename );

$rootPath = realpath(‘image-bank’);

$zip = new ZipArchive();
$zip->open( $filename, ZipArchive::CREATE | ZipArchive::OVERWRITE);

$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($files as $name => $file)
{

if (!$file->isDir())
{
    $filePath = $file->getRealPath();
    $relativePath = substr($filePath, strlen($rootPath) + 1);
    $zip->addFile($filePath, $relativePath);
}

}

$zip->close();

/*

  • Create two zip files, for Brochures / Downloads (assembly manuals)
    */

$args_brochures = array(
‘post_type’ => ‘furniture’,
‘posts_per_page’=>-1,
);

$query_brochures = new WP_Query( $args_brochures );

if ( $query_brochures->have_posts() ) {

$add_downloads = array();
$add_brochures = array();

while ( $query_brochures->have_posts() ) {

    $query_brochures->the_post();

    if( have_rows( 'downloads' ) ) {
        
        while ( have_rows( 'downloads' ) ) : the_row();

            $file = get_sub_field( 'file_downloads' );
            $svr_path = rtrim($_SERVER['DOCUMENT_ROOT'], '/');
            $add_downloads[] = $svr_path . str_replace ( get_site_url(), '', $file['url'] );

        endwhile; 
    }
    
    if( have_rows( 'brochures' ) ) {
        
        while ( have_rows( 'brochures' ) ) : the_row();

            $file_brochures = get_sub_field( 'brochure_downloads' );
            $svr_path_brochures = rtrim($_SERVER['DOCUMENT_ROOT'], '/');
            $add_brochures[] = $svr_path_brochures . str_replace ( get_site_url(), '', $file_brochures['url'] );

        endwhile; 
    }
    
    
}

wp_reset_postdata();

//var_dump($add_brochures);

$filename_downloads = $_SERVER['DOCUMENT_ROOT'] . 'zips/product-downloads.zip';
$filename_brochures = $_SERVER['DOCUMENT_ROOT'] . 'zips/product-brochures.zip';

//echo $filename_brochures;

$rootPath = realpath('zips');

// Create downloads zip
$zip = new ZipArchive();
$zip->open( $filename_downloads, ZipArchive::CREATE | ZipArchive::OVERWRITE);

foreach ($add_downloads as $file_poo) {

    if ( file_exists( $file_poo ) ) {

        $filePath = realpath( $file_poo );
        //$relativePath = substr($filePath, strlen($rootPath) + 1);
        $zip->addFile($file_poo, basename($file_poo) );
    
    }
    
}
$zip->close();

// Create brochures zip

$zip_brochures = new ZipArchive();
$zip_brochures->open( $filename_brochures, ZipArchive::CREATE | ZipArchive::OVERWRITE);

foreach ($add_brochures as $file_brochure) {

    if ( file_exists( $file_brochure ) ) {

        $filePathBrochures = realpath( $file_brochure );
        //$relativePathBrochures = substr($filePathBrochures, strlen($rootPath) + 1);
        $zip_brochures->addFile($file_brochure, basename($file_brochure) );
    
    }
    
}
$zip_brochures->close();

}
/*
$content = 'zips created: ’ . $_SERVER[‘DOCUMENT_ROOT’];

echo $content;

*/
?>
[/code]