PHP code example of brianhenryie / bh-wp-private-uploads

1. Go to this page and download the library: Download brianhenryie/bh-wp-private-uploads library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

brianhenryie / bh-wp-private-uploads example snippets


$settings = new class() implements \BrianHenryIE\WP_Private_Uploads\Private_Uploads_Settings_Interface {
	use \BrianHenryIE\WP_Private_Uploads\Private_Uploads_Settings_Trait;

	public function get_plugin_slug(): string {
		return 'my-plugin';
	}
};
$private_uploads = \BrianHenryIE\WP_Private_Uploads\Private_Uploads::instance( $settings );

$private_uploads = \BrianHenryIE\WP_Private_Uploads\Private_Uploads::instance();

// Download `https://example.org/doc.pdf` to `wp-content/uploads/my-plugin/2022/02/target-filename.pdf`.
$result = $private_uploads->download_remote_file_to_private_uploads( 'https://example.org/doc.pdf', 'target-filename.pdf' );
if ( $result->is_success() ) {
    $file_path = $result->get_file();
    $file_url = $result->get_url();
}

// Move `'/local/path/to/doc.pdf` to `wp-content/uploads/my-plugin/2022/02/target-filename.pdf`.
$result = $private_uploads->move_file_to_private_uploads( '/local/path/to/doc.pdf', 'target-filename.pdf' );
if ( $result->is_success() ) {
    $file_path = $result->get_file();
}

add_filter( 'bh_wp_private_uploads_can_upload', 'reject_large_uploads', 10, 5 );

/**
 * @param bool $can_upload
 * @param string $plugin_slug
 * @param string $post_type_name
 * @param string $tmp_file Source filepath.
 * @param string $filename Destination filename.
 */
function reject_large_uploads( bool $can_upload, string $plugin_slug, string $post_type_name, string $tmp_file, string $filename ): bool {
	if ( 'my-plugin' !== $plugin_slug ) {
		return $can_upload;
	}
	return $can_upload && filesize( $tmp_file ) < 10_000_000;
}

// Move the file and record it with a post owned by the user, attached to e.g. a WooCommerce order.
$result = $private_uploads->move_file_to_private_uploads_and_create_post(
    tmp_file: '/local/path/to/doc.pdf',
    filename: 'target-filename.pdf',
    post_author_id: $user_id, // Omit for no owner (`post_author` = `0`).
    post_parent_id: $order_id,
);
$post_id = $result->post_id;

// Download a remote file and record it with a post.
$result = $private_uploads->download_remote_file_to_private_uploads_and_create_post(
    file_url: 'https://example.org/doc.pdf',
    filename: 'target-filename.pdf',
    post_author_id: $user_id,
);

/**
 * Allow filtering for other users.
 *
 * @param bool $should_serve_file
 * @param string $plugin_slug
 * @param string $post_type_name
 * @param string $file
 */
$should_serve_file = apply_filters( 'bh_wp_private_uploads_allow', $should_serve_file, $plugin_slug, $post_type_name, $file );

add_filter( 'bh_wp_private_uploads_allow', 'add_shop_manager_to_allow', 10, 2 );
function add_shop_manager_to_allow( bool $should_serve_file, string $plugin_slug ): bool {
	if ( 'my-plugin' !== $plugin_slug ) {
		return $should_serve_file;
	}
	return $should_serve_file || current_user_can( 'manage_woocommerce' );
}

$settings = new class() implements \BrianHenryIE\WP_Private_Uploads\API\Private_Uploads_Settings_Interface {
	use \BrianHenryIE\WP_Private_Uploads\API\Private_Uploads_Settings_Trait;

	public function get_plugin_slug(): string {
		return 'my-plugin';
	}
	
  /**
	 * Defaults to the plugin slug when using Private_Uploads_Settings_Trait.
	 */
	public function get_uploads_subdirectory_name(): string {
		return 'email-attachments';
	}

};
$private_uploads = \BrianHenryIE\WP_Private_Uploads\Private_Uploads::instance( $settings );

$settings = new class() implements \BrianHenryIE\WP_Private_Uploads\API\Private_Uploads_Settings_Interface {
	use \BrianHenryIE\WP_Private_Uploads\API\Private_Uploads_Settings_Trait;

	public function get_plugin_slug(): string {
		return 'my-plugin';
	}
	
	/**
	 * Defaults to no CLI commands when using Private_Uploads_Settings_Trait.
	 */
	public function get_cli_base(): ?string {
		return 'my-plugin';
	}

};
$private_uploads = \BrianHenryIE\WP_Private_Uploads\Private_Uploads::instance( $settings );

$private_uploads = new Private_Uploads( $private_uploads_settings, $logger );
// Add the hooks:
new BH_WP_Private_Uploads_Hooks( $private_uploads, $private_uploads_settings, $logger );