Skip to content

Commit

Permalink
feat: 將非訂單、非訂閱的 paynow postmeta 移除
Browse files Browse the repository at this point in the history
  • Loading branch information
j7-dev committed Dec 18, 2024
1 parent e3ea1b1 commit c97b0ba
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 12 deletions.
127 changes: 127 additions & 0 deletions Compatibility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/**
* Compatibility 不同版本間的相容性設定
*/

declare (strict_types = 1);

namespace J7\Woomp;

/**
* Class Compatibility
*/
final class Compatibility {



const AS_COMPATIBILITY_ACTION = 'woomp_compatibility_action_scheduler';

/**
* Singleton instance
*
* @var self
*/
private static $instance;

/**
* Constructor
*/
public function __construct() {
// 排程只執行一次的兼容設定
\add_action( 'init', [ __CLASS__, 'compatibility_action_scheduler' ] );
\add_action( self::AS_COMPATIBILITY_ACTION, [ __CLASS__, 'compatibility' ]);
}

/**
* Get the singleton instance
*
* @param mixed ...$args Arguments
*
* @return self
*/
public static function instance(...$args) { // phpcs:ignore
if ( null === self::$instance ) {
self::$instance = new self(...$args);
}

return self::$instance;
}


/**
* 排程只執行一次的兼容設定
*
* @return void
*/
public static function compatibility_action_scheduler(): void {
$scheduled_version = \get_option('woomp_compatibility_action_scheduled');
if ($scheduled_version === WOOMP_VERSION) {
return;
}
\as_enqueue_async_action( self::AS_COMPATIBILITY_ACTION, [] );
}

/**
* 執行排程
*
* @return void
*/
public static function compatibility(): void {

/**
* ============== START 相容性代碼 ==============
*/

self::delete_post_meta();

/**
* ============== END 相容性代碼 ==============
*/

// ❗不要刪除此行,註記已經執行過相容設定
\update_option('woomp_compatibility_action_scheduled', WOOMP_VERSION);
}

/**
* 刪除 post meta
*
* @return void
*/
public static function delete_post_meta(): void {

$meta_keys = [
'_paynow_ei_issue_type',
'_paynow_ei_carrier_type',
'_paynow_ei_buyer_name',
'_paynow_ei_ubn',
'_paynow_ei_carrier_num',
'_paynow_ei_donate_org',
];
$meta_keys_string = implode( "','", $meta_keys );

$post_types = [
'shop_order',
'shop_subscription',
'shop_order_refund',
];
$post_types_string = \implode( "','", $post_types );

global $wpdb;
$results = $wpdb->query(
\wp_unslash(
$wpdb->prepare(
"DELETE pm FROM $wpdb->postmeta pm
LEFT JOIN $wpdb->posts p ON pm.post_id = p.ID
WHERE pm.meta_value = '' AND pm.meta_key IN (%s) AND p.post_type NOT IN (%s)",
$meta_keys_string,
$post_types_string
)
)
);

// TEST 印出 ErrorLog 記得移除
\J7\WpUtils\Classes\ErrorLog::info($results, 'delete_post_meta result');
}
}

Compatibility::instance();
23 changes: 12 additions & 11 deletions init.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
<?php

/**
* Currently plugin version.
* Start at version 1.0.0 and use SemVer - https://semver.org
* Rename this for your plugin and update it as you release new versions.
*/
define( 'WOOMP_VERSION', '3.4.49' );
define( 'WOOMP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'WOOMP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'WOOMP_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
define( 'WOOMP_ACTIVE_PLUGINS', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) );


// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
Expand Down Expand Up @@ -31,17 +43,6 @@ function require_woocommerce_notice() {
return;
}

/**
* Currently plugin version.
* Start at version 1.0.0 and use SemVer - https://semver.org
* Rename this for your plugin and update it as you release new versions.
*/
define( 'WOOMP_VERSION', '3.3.16' );
define( 'WOOMP_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'WOOMP_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'WOOMP_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
define( 'WOOMP_ACTIVE_PLUGINS', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) );

require WOOMP_PLUGIN_DIR . 'vendor/autoload.php';


Expand Down
3 changes: 2 additions & 1 deletion woomp.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin Name: 好用版擴充 MorePower Addon for WooCommerce
* Plugin URI: https://morepower.club/morepower-addon/
* Description: WooCommerce 好用版擴充,改善結帳流程與可變商品等區塊,並整合多項金流,讓 WooCommerce 更符合亞洲人使用習慣。
* Version: 3.4.48
* Version: 3.4.49
* Author: MorePower
* Author URI: https://morepower.club
* License: GPL-2.0+
Expand All @@ -16,3 +16,4 @@
*/
require_once 'init.php';
require_once 'debug.php';
require_once 'Compatibility.php';

0 comments on commit c97b0ba

Please sign in to comment.