Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Internal
File: DownloadPermissionsAdjuster.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* DownloadPermissionsAdjuster class file.
[2] Fix | Delete
*/
[3] Fix | Delete
[4] Fix | Delete
namespace Automattic\WooCommerce\Internal;
[5] Fix | Delete
[6] Fix | Delete
use Automattic\WooCommerce\Proxies\LegacyProxy;
[7] Fix | Delete
use WC_Product;
[8] Fix | Delete
[9] Fix | Delete
defined( 'ABSPATH' ) || exit;
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* Class to adjust download permissions on product save.
[13] Fix | Delete
*/
[14] Fix | Delete
class DownloadPermissionsAdjuster {
[15] Fix | Delete
[16] Fix | Delete
/**
[17] Fix | Delete
* The downloads data store to use.
[18] Fix | Delete
*
[19] Fix | Delete
* @var WC_Data_Store
[20] Fix | Delete
*/
[21] Fix | Delete
private $downloads_data_store;
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Class initialization, to be executed when the class is resolved by the container.
[25] Fix | Delete
*
[26] Fix | Delete
* @internal
[27] Fix | Delete
*/
[28] Fix | Delete
final public function init() {
[29] Fix | Delete
$this->downloads_data_store = wc_get_container()->get( LegacyProxy::class )->get_instance_of( \WC_Data_Store::class, 'customer-download' );
[30] Fix | Delete
add_action( 'adjust_download_permissions', array( $this, 'adjust_download_permissions' ), 10, 1 );
[31] Fix | Delete
}
[32] Fix | Delete
[33] Fix | Delete
/**
[34] Fix | Delete
* Schedule a download permissions adjustment for a product if necessary.
[35] Fix | Delete
* This should be executed whenever a product is saved.
[36] Fix | Delete
*
[37] Fix | Delete
* @param \WC_Product $product The product to schedule a download permission adjustments for.
[38] Fix | Delete
*/
[39] Fix | Delete
public function maybe_schedule_adjust_download_permissions( \WC_Product $product ) {
[40] Fix | Delete
$children_ids = $product->get_children();
[41] Fix | Delete
if ( ! $children_ids ) {
[42] Fix | Delete
return;
[43] Fix | Delete
}
[44] Fix | Delete
[45] Fix | Delete
$are_any_children_downloadable = false;
[46] Fix | Delete
foreach ( $children_ids as $child_id ) {
[47] Fix | Delete
$child = wc_get_product( $child_id );
[48] Fix | Delete
if ( $child && $child->is_downloadable() ) {
[49] Fix | Delete
$are_any_children_downloadable = true;
[50] Fix | Delete
break;
[51] Fix | Delete
}
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
if ( ! $product->is_downloadable() && ! $are_any_children_downloadable ) {
[55] Fix | Delete
return;
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
$scheduled_action_args = array( $product->get_id() );
[59] Fix | Delete
[60] Fix | Delete
$already_scheduled_actions =
[61] Fix | Delete
WC()->call_function(
[62] Fix | Delete
'as_get_scheduled_actions',
[63] Fix | Delete
array(
[64] Fix | Delete
'hook' => 'adjust_download_permissions',
[65] Fix | Delete
'args' => $scheduled_action_args,
[66] Fix | Delete
'status' => \ActionScheduler_Store::STATUS_PENDING,
[67] Fix | Delete
),
[68] Fix | Delete
'ids'
[69] Fix | Delete
);
[70] Fix | Delete
[71] Fix | Delete
if ( empty( $already_scheduled_actions ) ) {
[72] Fix | Delete
WC()->call_function(
[73] Fix | Delete
'as_schedule_single_action',
[74] Fix | Delete
WC()->call_function( 'time' ) + 1,
[75] Fix | Delete
'adjust_download_permissions',
[76] Fix | Delete
$scheduled_action_args
[77] Fix | Delete
);
[78] Fix | Delete
}
[79] Fix | Delete
}
[80] Fix | Delete
[81] Fix | Delete
/**
[82] Fix | Delete
* Create additional download permissions for variations if necessary.
[83] Fix | Delete
*
[84] Fix | Delete
* When a simple downloadable product is converted to a variable product,
[85] Fix | Delete
* existing download permissions are still present in the database but they don't apply anymore.
[86] Fix | Delete
* This method creates additional download permissions for the variations based on
[87] Fix | Delete
* the old existing ones for the main product.
[88] Fix | Delete
*
[89] Fix | Delete
* The procedure is as follows. For each existing download permission for the parent product,
[90] Fix | Delete
* check if there's any variation offering the same file for download (the file URL, not name, is checked).
[91] Fix | Delete
* If that is found, check if an equivalent permission exists (equivalent means for the same file and with
[92] Fix | Delete
* the same order id and customer id). If no equivalent permission exists, create it.
[93] Fix | Delete
*
[94] Fix | Delete
* @param int $product_id The id of the product to check permissions for.
[95] Fix | Delete
*/
[96] Fix | Delete
public function adjust_download_permissions( int $product_id ) {
[97] Fix | Delete
$product = wc_get_product( $product_id );
[98] Fix | Delete
if ( ! $product ) {
[99] Fix | Delete
return;
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
$children_ids = $product->get_children();
[103] Fix | Delete
if ( ! $children_ids ) {
[104] Fix | Delete
return;
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
$parent_downloads = $this->get_download_files_and_permissions( $product );
[108] Fix | Delete
if ( ! $parent_downloads ) {
[109] Fix | Delete
return;
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
$children_with_downloads = array();
[113] Fix | Delete
foreach ( $children_ids as $child_id ) {
[114] Fix | Delete
$child = wc_get_product( $child_id );
[115] Fix | Delete
[116] Fix | Delete
// Ensure we have a valid child product.
[117] Fix | Delete
if ( ! $child instanceof WC_Product ) {
[118] Fix | Delete
wc_get_logger()->warning(
[119] Fix | Delete
sprintf(
[120] Fix | Delete
/* translators: 1: child product ID 2: parent product ID. */
[121] Fix | Delete
__( 'Unable to load child product %1$d while adjusting download permissions for product %2$d.', 'woocommerce' ),
[122] Fix | Delete
$child_id,
[123] Fix | Delete
$product_id
[124] Fix | Delete
)
[125] Fix | Delete
);
[126] Fix | Delete
continue;
[127] Fix | Delete
}
[128] Fix | Delete
[129] Fix | Delete
$children_with_downloads[ $child_id ] = $this->get_download_files_and_permissions( $child );
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
foreach ( $parent_downloads['permission_data_by_file_order_user'] as $parent_file_order_and_user => $parent_download_data ) {
[133] Fix | Delete
foreach ( $children_with_downloads as $child_id => $child_download_data ) {
[134] Fix | Delete
$file_url = $parent_download_data['file'];
[135] Fix | Delete
[136] Fix | Delete
$must_create_permission =
[137] Fix | Delete
// The variation offers the same file as the parent for download...
[138] Fix | Delete
in_array( $file_url, array_keys( $child_download_data['download_ids_by_file_url'] ), true ) &&
[139] Fix | Delete
// ...but no equivalent download permission (same file URL, order id and user id) exists.
[140] Fix | Delete
! array_key_exists( $parent_file_order_and_user, $child_download_data['permission_data_by_file_order_user'] );
[141] Fix | Delete
[142] Fix | Delete
if ( $must_create_permission ) {
[143] Fix | Delete
// The new child download permission is a copy of the parent's,
[144] Fix | Delete
// but with the product and download ids changed to match those of the variation.
[145] Fix | Delete
$new_download_data = $parent_download_data['data'];
[146] Fix | Delete
$new_download_data['product_id'] = $child_id;
[147] Fix | Delete
$new_download_data['download_id'] = $child_download_data['download_ids_by_file_url'][ $file_url ];
[148] Fix | Delete
$this->downloads_data_store->create_from_data( $new_download_data );
[149] Fix | Delete
}
[150] Fix | Delete
}
[151] Fix | Delete
}
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
/**
[155] Fix | Delete
* Get the existing downloadable files and download permissions for a given product.
[156] Fix | Delete
* The returned value is an array with two keys:
[157] Fix | Delete
*
[158] Fix | Delete
* - download_ids_by_file_url: an associative array of file url => download_id.
[159] Fix | Delete
* - permission_data_by_file_order_user: an associative array where key is "file_url:customer_id:order_id" and value is the full permission data set.
[160] Fix | Delete
*
[161] Fix | Delete
* @param \WC_Product $product The product to get the downloadable files and permissions for.
[162] Fix | Delete
* @return array[] Information about the downloadable files and permissions for the product.
[163] Fix | Delete
*/
[164] Fix | Delete
private function get_download_files_and_permissions( \WC_Product $product ) {
[165] Fix | Delete
$result = array(
[166] Fix | Delete
'permission_data_by_file_order_user' => array(),
[167] Fix | Delete
'download_ids_by_file_url' => array(),
[168] Fix | Delete
);
[169] Fix | Delete
$downloads = $product->get_downloads();
[170] Fix | Delete
foreach ( $downloads as $download ) {
[171] Fix | Delete
$result['download_ids_by_file_url'][ $download->get_file() ] = $download->get_id();
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
$permissions = $this->downloads_data_store->get_downloads( array( 'product_id' => $product->get_id() ) );
[175] Fix | Delete
foreach ( $permissions as $permission ) {
[176] Fix | Delete
$permission_data = (array) $permission->data;
[177] Fix | Delete
if ( array_key_exists( $permission_data['download_id'], $downloads ) ) {
[178] Fix | Delete
$file = $downloads[ $permission_data['download_id'] ]->get_file();
[179] Fix | Delete
$data = array(
[180] Fix | Delete
'file' => $file,
[181] Fix | Delete
'data' => (array) $permission->data,
[182] Fix | Delete
);
[183] Fix | Delete
$result['permission_data_by_file_order_user'][ "{$file}:{$permission_data['user_id']}:{$permission_data['order_id']}" ] = $data;
[184] Fix | Delete
}
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
return $result;
[188] Fix | Delete
}
[189] Fix | Delete
}
[190] Fix | Delete
[191] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function