Created
November 3, 2022 12:55
-
-
Save damiencarbery/5fbb147c8a14e1224d5dade324631023 to your computer and use it in GitHub Desktop.
Attach WooCommerce download docs to Contact Form 7 email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Attach WooCommerce download docs to Contact Form 7 email | |
Plugin URI: https://www.damiencarbery.com/ | |
Description: Use a CF7 filter to attach a WooCommerce product's downloadable files to the email. | |
Author: Damien Carbery | |
Version: 0.2 | |
*/ | |
class AttachDownloadDocsToCF7Email { | |
private $cf7_form_id; | |
// Returns an instance of this class. | |
public static function get_instance() { | |
if ( null == self::$instance ) { | |
self::$instance = new self; | |
} | |
return self::$instance; | |
} | |
// Initialize the plugin variables. | |
public function __construct() { | |
$this->cf7_form_id = 79; | |
$this->init(); | |
} | |
// Set up WordPress specfic actions. | |
public function init() { | |
// Add downloadable files to email as an attachment | |
add_filter( 'wpcf7_mail_components', array( $this, 'attach_downloadable_files' ), 10, 3 ); | |
} | |
public function attach_downloadable_files( $components, $current_cf7_form, $form_object ) { | |
// Verify that the we are processing the correct form. | |
if ( $this->cf7_form_id != $current_cf7_form->id() ) { | |
//error_log( 'Form ID mismatch: Is ' . $current_cf7_form->id() . '; expecting: ' . $this->cf7_form_id ); | |
return $components; | |
} | |
// Ensure that a match was found. | |
if ( 1 !== preg_match( '/Product ID: (\d+)/m', $components[ 'body' ], $matches ) ) { | |
//error_log( 'Product ID regex error: ' . var_export( $components[ 'body' ], true ) ); | |
return $components; | |
} | |
$product_id = $matches[1]; | |
// Check that the matched product ID is a number. | |
if ( !is_numeric( $product_id ) || $product_id < 0 ) { | |
//error_log( 'Product ID error: ' . var_export( $product_id, true ) ); | |
return $components; | |
} | |
// Create a product object... | |
$product = wc_get_product( $product_id ); | |
// checking that the ID is a valid Downloadable product. | |
if ( is_object( $product ) && $product->is_downloadable() ) { | |
$uploads = wp_upload_dir( null, false ); | |
foreach( $product->get_downloads() as $key_download_id => $download ) { | |
$file = $download->get_file(); | |
//error_log( 'Name: ' . $download->get_name() ); | |
//error_log( 'URL: ' . $file ); | |
// $file is a url but we need a file so use $uploads elements to convert | |
// the url into a path. | |
$file = str_replace( $uploads[ 'baseurl' ], $uploads[ 'basedir' ], $file ); | |
// Verify that the file exists before appending it to the attachments array. | |
if ( file_exists( $file ) ) { | |
$components[ 'attachments' ][] = $file; | |
} | |
else { | |
//error_log( 'File: ' . $file . ' (DOES NOT EXIST)' ); | |
} | |
} | |
} | |
//error_log( 'Attachments: ' . var_export( $components[ 'attachments' ], true ) ); | |
return $components; | |
} | |
} | |
$AttachDownloadDocsToCF7Email = new AttachDownloadDocsToCF7Email(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment