/** * * @param string $file * @param string $filename * @return int */ protected function add_attachment($file, $filename) { $dest_file = trailingslashit(WC_Email_Att_Func::get_full_upload_path($this->options_new['upload_folder'])).$filename; if(file_exists($dest_file)) { $unique = uniqid('_'); $path_parts = pathinfo($filename); $filename = $path_parts['filename'].$unique.'.'.$path_parts['extension']; $dest_file = trailingslashit(WC_Email_Att_Func::get_full_upload_path($this->options_new['upload_folder'])).$filename; } if(!copy($file, $dest_file)) { return 0; } $parent_post_id = WC_Email_Att::instance()->email_post->ID; // Check the type of tile. We'll use this as the 'post_mime_type'. $filetype = wp_check_filetype( basename( $dest_file ), null ); // Get the path to the upload directory. $wp_upload_dir = wp_upload_dir(); // Prepare an array of post data for the attachment. $attachment = array( 'guid' => $wp_upload_dir['baseurl'].trailingslashit($this->options_new['upload_folder']).$filename, 'post_mime_type' => $filetype['type'], 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $dest_file ) ), 'post_content' => '', 'post_status' => 'inherit' ); // Insert the attachment. $attach_id = wp_insert_attachment( $attachment, $dest_file, $parent_post_id ); // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. require_once( ABSPATH . 'wp-admin/includes/image.php' ); // Generate the metadata for the attachment, and update the database record. $attach_data = wp_generate_attachment_metadata( $attach_id, $dest_file ); wp_update_attachment_metadata( $attach_id, $attach_data ); return $attach_id; }
/** * Deletes all the files attached to the only EMail attachment post */ protected function delete_all_attachment_files() { $args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' =>'any', 'post_parent' => WC_Email_Att::instance()->email_post->ID ); $attachments = get_posts( $args ); $to_delete = count( $attachments ); $deleted = 0; foreach ( $attachments as $key => $attachment ) { if( false !== wp_delete_attachment( $attachment->ID, true ) ) { $deleted++; } } if( $to_delete == $deleted ) { $notice_option = array( 'message' => sprintf( __( 'All %d attachment file(s) could be permanently deleted.', WC_Email_Att::TEXT_DOMAIN ), $to_delete ), 'error' => false ); } else { $diff = $to_delete - $deleted; $notice_option = array( 'message' => sprintf( __( '%d attachment file(s) could not be deleted, %d file(s) could be deleted.', WC_Email_Att::TEXT_DOMAIN ), $diff, $deleted ), 'error' => true ); } update_option( WC_Email_Att::OPTIONNAME_NOTICE, $notice_option ); }
/** * Saves the options in own option entry */ public function save_settings_page_options() { $this->options['del_on_deactivate'] = (WC_Email_Att::$show_activation) ? isset( $_REQUEST[ self::PREFIX_JS_NAMES . 'del_on_deactivate' ] ) : false; $this->options['del_on_uninstall'] = (WC_Email_Att::$show_uninstall) ? isset( $_REQUEST[self::PREFIX_JS_NAMES . 'del_on_uninstall'] ) : true; $this->options['notification_headline'] = (isset( $_REQUEST[ self::PREFIX_JS_NAMES . 'notification_headline']) && ! empty( $_REQUEST[self::PREFIX_JS_NAMES . 'notification_headline' ] ) ) ? $_REQUEST[ self::PREFIX_JS_NAMES . 'notification_headline' ] : ''; $this->options['notification_headline'] = trim( stripslashes( $this->options['notification_headline'] ) ); $this->options['notification_text'] = (isset( $_REQUEST[ self::PREFIX_JS_NAMES . 'notification_text']) && ! empty( $_REQUEST[self::PREFIX_JS_NAMES . 'notification_text'])) ? $_REQUEST[self::PREFIX_JS_NAMES . 'notification_text' ] : ''; $this->options['notification_text'] = trim( stripslashes( $this->options['notification_text'] ) ); $this->options['show_notes_always'] = isset( $_REQUEST[ self::PREFIX_JS_NAMES . 'show_notes_always' ] ); $oldpath = $newpath = strtolower( $this->options['upload_folder'] ); if( isset( $_REQUEST[ self::PREFIX_JS_NAMES . 'upload_folder' ] ) ) { $newpath = strtolower( trim( strtolower( stripslashes( $_REQUEST[ self::PREFIX_JS_NAMES . 'upload_folder'] ) ) ) ); $newpath = str_replace( '\\', '/', $newpath ); $newpath = (strlen( $newpath ) == 0 ) ? $oldpath : untrailingslashit( $newpath ); $newpath = '/' . ltrim( $newpath, '/' ); if( $oldpath != $newpath ) { if( WC_Email_Att_Func::create_folder( $newpath ) ) { $this->options['upload_folder'] = $newpath; } WC_Email_Att_Func::remove_empty_folder( $oldpath, WC_Email_Att::$skip_files ); } } foreach ( $this->emailsubjects as $email_key => $emailclass ) { $opt_key = $this->inputfields_param[ $email_key ]['id']; $this->options[ $opt_key ] = $this->get_opt_emailaddress( self::PREFIX_JS_NAMES . 'head_', $email_key ); $opt_key = $this->inputfields_param[ $email_key ]['att_id']; $this->options[ $opt_key ] = $this->get_opt_attachment_files( self::PREFIX_JS_NAMES . 'att_', $email_key ); } $this->options = apply_filters( 'wc_eatt_save_settings_options', $this->options, $this->emailsubjects, $this->inputfields_param ); update_option( WC_Email_Att::OPTIONNAME, $this->options ); WC_Email_Att::instance()->options = $this->options; WC_Email_Att::$admin_message = 'Options saved'; }
<?php if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly global $wc_email_att_skip_files, $wc_email_att_htaccess, $wc_email_att_plugin_base_name, $wc_email_att_are_activation_hooks; $wc_email_att_plugin_path = str_replace( basename( __FILE__ ), '', __FILE__ ); /** * load woocommerce_email_attachments main class, which loads needed classes with autoload when needed **/ require_once $wc_email_att_plugin_path . 'classes/class-wc-email-att.php'; // create object WC_Email_Att::instance(); WC_Email_Att::$show_activation = true; // true to show deactivation and uninstall checkbox WC_Email_Att::$show_uninstall = true; WC_Email_Att::$plugin_path = $wc_email_att_plugin_path; WC_Email_Att::$plugin_url = trailingslashit( plugins_url( '', plugin_basename( __FILE__ ) ) ); // also set in init hook to allow other plugins to change it in a filter hook WC_Email_Att::$plugin_base_name = $wc_email_att_plugin_base_name; WC_Email_Att::$skip_files = $wc_email_att_skip_files; WC_Email_Att_Func::$htaccess = $wc_email_att_htaccess; WC_Email_Att::instance()->init(); // allow plugins to load, which depend on this plugin being already loaded do_action( 'wc_email_att_plugin_loaded' );
/** * Checks for OPT_DEL_ON_UNINSTALL -> removes option and all files and the last folder, if empty */ public function on_uninstall() { $this->delete_message_options(WC_Email_Att::OPTIONNAME_NOTICE); $this->delete_message_options(WC_Email_Att::OPTIONNAME_UPDATE); $this->options = get_option( WC_Email_Att::OPTIONNAME, array() ); // already deleted on deactivation if( empty( $this->options ) ) { WC_Email_Att::instance()->delete_the_only_post(); // fallback - delete option from old version also, but leave the files delete_option( 'woocommerce_email_attachments' ); return; } // fallback - default behaviour if not exist - Delete in any case to clean up database if( ! isset($this->options['del_on_uninstall'] ) || $this->options['del_on_uninstall'] ) { delete_option( WC_Email_Att::OPTIONNAME ); WC_Email_Att::instance()->delete_the_only_post(); // fallback - delete option from old version also, but leave the files delete_option( 'woocommerce_email_attachments' ); } }