Sindbad~EG File Manager

Current Path : /home/nicedoo/www/wp-content/plugins/tier-pricing-table/src/Services/
Upload File :
Current File : /home/nicedoo/www/wp-content/plugins/tier-pricing-table/src/Services/DebugService.php

<?php namespace TierPricingTable\Services;

/*
 * Class DebugService
 *
 * @package TierPricingTable/Services
 */

use TierPricingTable\CalculationLogic;
use TierPricingTable\Core\ServiceContainer;
use TierPricingTable\PricingRule;

class DebugService {
	
	public function __construct() {
		
		if ( ServiceContainer::getInstance()->getSettings()->get( 'debug_enabled', 'no' ) !== 'yes' ) {
			return;
		}
		
		add_action( 'woocommerce_after_cart_item_name', function ( $cartItem ) {
			if ( isset( $cartItem['tpt_pricing_rule'] ) && $cartItem['tpt_pricing_rule'] instanceof PricingRule ) {
				$this->formatPricingRule( $cartItem['tpt_pricing_rule'] );
			}
		} );
		
		add_action( 'tiered_pricing_table/before_rendering_tiered_pricing/inner',
			function ( PricingRule $pricingRule ) {
				$this->formatPricingRule( $pricingRule );
			} );
	}
	
	protected function getPricingRuleData( PricingRule $pricingRule ): array {
		
		switch ( $pricingRule->provider ) {
			case 'global-rules':
				$globalRuleId     = $pricingRule->providerData['rule_id'] ?? 'undefined';
				$data['provider'] = sprintf( '<a href="%s">%s</a>', get_edit_post_link( $globalRuleId ),
					"Global rule ($globalRuleId)" );
				break;
			case 'category-rules':
				$categoryId       = $pricingRule->providerData['category_id'] ?? 'undefined';
				$data['provider'] = sprintf( '<a href="%s">%s</a>', get_edit_term_link( $categoryId ),
					"DEPRECATED Category rule ($categoryId)" );
				break;
			case 'role-based':
				$role             = $pricingRule->providerData['role'] ?? 'undefined';
				$data['provider'] = "Role-based rule ($role)";
				break;
			case 'product':
				$data['provider'] = 'Product level rule';
				break;
			default:
				$provider         = $pricingRule->provider ?? 'undefined';
				$data['provider'] = "Undefined provider ($provider)";
		}
		
		$data['minimum']  = $pricingRule->getMinimum();
		$data['maximum']  = ! empty( $pricingRule->data['maximum_quantity'] ) ? $pricingRule->data['maximum_quantity'] : '-';
		$data['group_of'] = ! empty( $pricingRule->data['group_of'] ) ? $pricingRule->data['group_of'] : '-';
		
		$data['pricing_type'] = $pricingRule->getType();
		$data['rules']        = '-';
		
		if ( ! empty( $pricingRule->getRules() ) ) {
			$data['rules'] = '';
			
			foreach ( $pricingRule->getRules() as $quantity => $price ) {
				$data['rules'] .= $quantity . ':' . $price . ',';
			}
		}
		
		$data['total_in_cart'] = ! empty( $pricingRule->cartItemData['total_item_quantity'] ) ? $pricingRule->cartItemData['total_item_quantity'] : 'undefined';
		$data['current_price'] = ! empty( $pricingRule->cartItemData['new_price'] ) ? $pricingRule->cartItemData['new_price'] : 'undefined';
		
		$data['custom_regular_price'] = ! empty( $pricingRule->pricingData['regular_price'] ) ? $pricingRule->pricingData['regular_price'] : '-';
		$data['custom_sale_price']    = ! empty( $pricingRule->pricingData['sale_price'] ) ? $pricingRule->pricingData['sale_price'] : '-';
		$data['discount']             = ! empty( $pricingRule->pricingData['discount'] ) ? $pricingRule->pricingData['discount'] : '-';
		$data['custom_pricing_type']  = ! empty( $pricingRule->pricingData['pricing_type'] ) ? $pricingRule->pricingData['pricing_type'] : '-';
		
		//$data['calculate_discounts_by_regular_price'] = CalculationLogic::calculateDiscountBasedOnRegularPrice() ? 'yes' : 'no';
		//$data['variations_are_the_same_product']      = CalculationLogic::considerProductVariationAsOneProduct() ? 'yes' : 'no';
		//$data['round_price']      = CalculationLogic::roundPrice() ? 'yes' : 'no';
		
		return $data;
	}
	
	protected function formatPricingRule( PricingRule $pricingRule ) {
		
		$data = $this->getPricingRuleData( $pricingRule );
		?>
		<div>
			<table style="font-size: .7em">
				<thead>
				<tr>
					<th colspan="2">Debug info</th>
				</tr>
				</thead>
				<tbody>
				<?php foreach ( $data as $key => $value ) : ?>
					<tr>
						<td style="padding: 0">
							<?php echo esc_html( $key . ':' ); ?>
						</td>
						<td style="padding: 0">
							<?php echo wp_kses_post( $value ); ?>
						</td>
					</tr>
				<?php endforeach; ?>
				</tbody>
			</table>
		</div>
		<?php
	}
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists