| 1 |
<?php namespace TierPricingTable\Addons\GlobalTieredPricing\CPT; |
| 2 |
|
| 3 |
use TierPricingTable\Addons\GlobalTieredPricing\GlobalPricingRule; |
| 4 |
use TierPricingTable\Addons\Tools\PricingTest\PricingTester; |
| 5 |
use WP_Post; |
| 6 |
|
| 7 |
/** |
| 8 |
* Links from the pricing rules list to the Pricing test utility (Tools add-on): a button next to |
| 9 |
* "Add Pricing Rule" and a "Test" row action. The rule editor links from its Rule status box. |
| 10 |
*/ |
| 11 |
class PricingTestLinks { |
| 12 |
|
| 13 |
public function __construct() { |
| 14 |
add_filter( 'post_row_actions', array( $this, 'addRowAction' ), 20, 2 ); |
| 15 |
add_action( 'admin_footer-edit.php', array( $this, 'addListButton' ) ); |
| 16 |
} |
| 17 |
|
| 18 |
public function addRowAction( $actions, WP_Post $post ) { |
| 19 |
if ( GlobalTieredPricingCPT::SLUG !== $post->post_type || ! PricingTester::isAvailable() ) { |
| 20 |
return $actions; |
| 21 |
} |
| 22 |
|
| 23 |
// a skipped rule changes nothing, so there is nothing to test |
| 24 |
if ( 'publish' === $post->post_status && 'skipped' === GlobalPricingRule::build( $post->ID )->getEffectiveStatus() ) { |
| 25 |
return $actions; |
| 26 |
} |
| 27 |
|
| 28 |
// a new tab: the test is a side trip from the list or the editor, and the editor may hold unsaved changes |
| 29 |
$actions['tpt_pricing_test'] = sprintf( '<a href="%s" target="_blank" rel="noopener">%s</a>', esc_url( PricingTester::getUrl( $post->ID ) ), esc_html__( 'Test', 'tier-pricing-table' ) ); |
| 30 |
|
| 31 |
return $actions; |
| 32 |
} |
| 33 |
|
| 34 |
public function addListButton() { |
| 35 |
$screen = get_current_screen(); |
| 36 |
|
| 37 |
if ( ! $screen || GlobalTieredPricingCPT::SLUG !== $screen->post_type || ! PricingTester::isAvailable() ) { |
| 38 |
return; |
| 39 |
} |
| 40 |
?> |
| 41 |
<script> |
| 42 |
jQuery( function ( $ ) { |
| 43 |
$( '.wrap .page-title-action' ).first().after( ' <a href="<?php echo esc_url( PricingTester::getUrl() ); ?>" class="page-title-action"><?php echo esc_js( __( 'Pricing test', 'tier-pricing-table' ) ); ?></a>' ); |
| 44 |
} ); |
| 45 |
</script> |
| 46 |
<?php |
| 47 |
} |
| 48 |
} |
| 49 |
|