PluginProbe
Tiered Pricing Table for WooCommerce / 7.1.5
Tiered Pricing Table for WooCommerce v7.1.5
8.0.2 7.1.7 7.1.5 7.1.4 7.1.1 6.5.0 6.4.0 6.1.0 trunk 1.0 1.1 2.0 2.0.1 2.0.2 2.1 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.3.0 2.3.1 2.3.2 2.3.3 All 91 releases
tier-pricing-table / src / Addons / GlobalTieredPricing / CPT / Actions / DuplicateAction.php

DuplicateAction.php in Tiered Pricing Table for WooCommerce 7.1.5, at src/Addons/GlobalTieredPricing/CPT/Actions/DuplicateAction.php

105 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php namespace TierPricingTable\Addons\GlobalTieredPricing\CPT\Actions;
2
3 use TierPricingTable\Addons\GlobalTieredPricing\CPT\GlobalTieredPricingCPT;
4 use TierPricingTable\Core\AdminNotifier;
5 use TierPricingTable\Core\ServiceContainerTrait;
6 use WP_Post;
7
8 class DuplicateAction {
9
10 const ACTION = 'tpt_duplicate_global_rule';
11
12 use ServiceContainerTrait;
13
14 public function __construct() {
15
16 add_action( 'admin_post_' . self::ACTION, array( $this, 'handle' ) );
17
18 add_filter( 'post_row_actions', function ( $actions, WP_Post $post ) {
19
20 if ( GlobalTieredPricingCPT::SLUG !== $post->post_type ) {
21 return $actions;
22 }
23
24 $actions['duplicate'] = sprintf( '<a href="%s" aria-label="%s">%s</a>',
25 esc_url( $this->getRunLink( $post->ID ) ),
26 esc_attr__( 'Duplicate this rule', 'tier-pricing-table' ),
27 $this->getName()
28 );
29
30 return $actions;
31 }, 10, 2 );
32 }
33
34 public function getName(): string {
35 return __( 'Duplicate', 'tier-pricing-table' );
36 }
37
38 public function handle(): bool {
39 $nonce = isset( $_GET['_wpnonce'] ) ? sanitize_text_field( $_GET['_wpnonce'] ) : false;
40
41 if ( wp_verify_nonce( $nonce, self::ACTION ) ) {
42 $ruleId = isset( $_GET['rule_id'] ) ? intval( $_GET['rule_id'] ) : false;
43
44 if ( $ruleId ) {
45
46 $originalPost = get_post( $ruleId );
47
48 if ( $originalPost && $originalPost->post_type === GlobalTieredPricingCPT::SLUG ) {
49
50 $newPostArgs = array(
51 'post_title' => $originalPost->post_title . ' ' . __( '(Copy)', 'tier-pricing-table' ),
52 'post_status' => 'draft',
53 'post_type' => GlobalTieredPricingCPT::SLUG,
54 'post_author' => get_current_user_id(),
55 );
56
57 $newPostId = wp_insert_post( $newPostArgs );
58
59 if ( ! is_wp_error( $newPostId ) && $newPostId ) {
60 // Copy all post meta directly using wpdb to avoid serialization and slashing issues
61 global $wpdb;
62 $post_meta_infos = $wpdb->get_results(
63 $wpdb->prepare( "SELECT meta_key, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d", $ruleId )
64 );
65
66 if ( count( $post_meta_infos ) !== 0 ) {
67 foreach ( $post_meta_infos as $meta_info ) {
68 $wpdb->insert(
69 $wpdb->postmeta,
70 array(
71 'post_id' => $newPostId,
72 'meta_key' => $meta_info->meta_key,
73 'meta_value' => $meta_info->meta_value,
74 )
75 );
76 }
77 }
78
79 clean_post_cache( $newPostId );
80
81 $this->getContainer()->getAdminNotifier()->flash( __( 'The rule was duplicated successfully.',
82 'tier-pricing-table' ), AdminNotifier::SUCCESS, true );
83 } else {
84 $this->getContainer()->getAdminNotifier()->flash( __( 'Failed to duplicate rule.',
85 'tier-pricing-table' ), AdminNotifier::ERROR, true );
86 }
87 }
88
89 }
90
91 } else {
92 wp_die( 'You\'re not allowed to run this action' );
93 }
94
95 return wp_safe_redirect( wp_get_referer() );
96 }
97
98 public function getRunLink( $id ): string {
99 return add_query_arg( array(
100 'rule_id' => $id,
101 'action' => self::ACTION,
102 ), wp_nonce_url( admin_url( 'admin-post.php' ), self::ACTION ) );
103 }
104 }
105