*/
public static function tables() {
global $wpdb;
return array(
'orders' => $wpdb->prefix . 'paygrab_orders',
'items' => $wpdb->prefix . 'paygrab_order_items',
'downloads' => $wpdb->prefix . 'paygrab_downloads',
'events' => $wpdb->prefix . 'paygrab_order_events',
);
}
/**
* Activation hook.
*
* @return void
*/
public static function activate() {
self::create_tables();
self::create_pages();
Paygrab_Files::protect();
Paygrab_Cron::schedule();
Paygrab_Product::register();
flush_rewrite_rules();
}
/**
* Deactivation hook.
*
* @return void
*/
public static function deactivate() {
Paygrab_Cron::unschedule();
flush_rewrite_rules();
}
/**
* Upgrade the tables when the plugin was updated without re-activation.
*
* @return void
*/
public static function maybe_upgrade() {
if ( get_option( 'paygrab_db_version' ) !== PAYGRAB_DB_VERSION ) {
self::create_tables();
Paygrab_Cron::schedule();
// Pages added by an update (e.g. the login page); after init so their titles are translated.
add_action( 'init', array( __CLASS__, 'create_pages' ), 20 );
}
add_action( 'admin_init', array( __CLASS__, 'resolve_mypage_clash' ) );
add_action( 'admin_notices', array( __CLASS__, 'mypage_moved_notice' ) );
}
/**
* When another plugin or theme uses the same URL as PAYGRAB's My page, move PAYGRAB's page.
*
* YOMERU (and the paid articles of TCD themes such as PANDORA) serve their My page with a rewrite rule, so PAYGRAB's page
* at the same URL could never be opened (links in emails and the buy box would land on their page).
*
* @return void
*/
public static function resolve_mypage_clash() {
$id = (int) Paygrab_Settings::get( 'page_mypage' );
$page = $id ? get_post( $id ) : null;
if ( ! $page || ! current_user_can( 'manage_options' ) ) {
return;
}
$yomeru = paygrab_yomeru_mypage_slug();
$by = '';
if ( '' !== $yomeru && $page->post_name === $yomeru ) {
$by = 'yomeru';
} elseif ( ! str_starts_with( $page->post_name, 'purchases' ) && paygrab_slug_claimed_by_rewrite( $page->post_name ) ) {
// A page already moved ("purchases", "purchases-2" …) is left where it is: it moves once, never again.
$by = 'other';
}
if ( '' === $by ) {
return;
}
$new = wp_unique_post_slug( 'purchases', $id, $page->post_status, 'page', (int) $page->post_parent );
$args = array(
'ID' => $id,
'post_name' => $new,
);
// The title is renamed only while it is still the one PAYGRAB gave; a title the seller wrote is kept.
if ( _x( 'My page', 'page title', 'paygrab' ) === $page->post_title ) {
$args['post_title'] = __( 'Order history', 'paygrab' );
}
wp_update_post( $args );
update_option( 'paygrab_mypage_moved', home_url( '/' . $new . '/' ), false );
update_option( 'paygrab_mypage_moved_by', $by, false );
}
/**
* Tell the seller once that the page was moved.
*
* @return void
*/
public static function mypage_moved_notice() {
$url = (string) get_option( 'paygrab_mypage_moved' );
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
// Shown once, and only on PAYGRAB's own screens (the notice stays until the seller opens one).
if ( '' === $url || ! current_user_can( 'manage_options' ) || ! $screen || false === strpos( $screen->id, 'paygrab' ) ) {
return;
}
$by = (string) get_option( 'paygrab_mypage_moved_by' );
delete_option( 'paygrab_mypage_moved' );
delete_option( 'paygrab_mypage_moved_by' );
if ( 'other' === $by ) {
/* translators: %s: new URL */
$text = __( 'PAYGRAB: another plugin or your theme uses the same address for its My page, so PAYGRAB’s page was renamed “Order history” and moved to %s. Links in emails and on product pages now point there.', 'paygrab' );
} else {
/* translators: %s: new URL */
$text = __( 'PAYGRAB: YOMERU uses the same address for its My page, so PAYGRAB’s page was renamed “Order history” and moved to %s. Links in emails and on product pages now point there.', 'paygrab' );
}
printf(
'
',
esc_html( sprintf( $text, $url ) )
);
}
/**
* Create or update the tables.
*
* @return void
*/
public static function create_tables() {
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$t = self::tables();
$charset = $wpdb->get_charset_collate();
dbDelta(
"CREATE TABLE {$t['orders']} (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
order_number varchar(32) NOT NULL DEFAULT '',
transfer_code varchar(8) NOT NULL DEFAULT '',
user_id bigint(20) unsigned NOT NULL DEFAULT 0,
status varchar(20) NOT NULL DEFAULT 'pending',
gateway varchar(40) NOT NULL DEFAULT '',
payment_method varchar(40) NOT NULL DEFAULT '',
subtotal int(11) NOT NULL DEFAULT 0,
discount int(11) NOT NULL DEFAULT 0,
total int(11) NOT NULL DEFAULT 0,
tax_total int(11) NOT NULL DEFAULT 0,
billing_name varchar(191) NOT NULL DEFAULT '',
billing_email varchar(191) NOT NULL DEFAULT '',
gateway_ref varchar(191) NOT NULL DEFAULT '',
source_id bigint(20) unsigned NOT NULL DEFAULT 0,
pay_deadline datetime DEFAULT NULL,
reminded_at datetime DEFAULT NULL,
created_at datetime NOT NULL,
updated_at datetime NOT NULL,
paid_at datetime DEFAULT NULL,
cancelled_at datetime DEFAULT NULL,
refunded_at datetime DEFAULT NULL,
PRIMARY KEY (id),
KEY order_number (order_number),
KEY user_id (user_id),
KEY status_deadline (status,pay_deadline),
KEY transfer_code (transfer_code),
KEY gateway_ref (gateway_ref)
) $charset;"
);
dbDelta(
"CREATE TABLE {$t['items']} (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
order_id bigint(20) unsigned NOT NULL,
product_id bigint(20) unsigned NOT NULL DEFAULT 0,
name varchar(255) NOT NULL DEFAULT '',
unit_price int(11) NOT NULL DEFAULT 0,
quantity int(11) NOT NULL DEFAULT 1,
tax_rate tinyint(3) unsigned NOT NULL DEFAULT 10,
PRIMARY KEY (id),
KEY order_id (order_id),
KEY product_id (product_id)
) $charset;"
);
dbDelta(
"CREATE TABLE {$t['downloads']} (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
order_id bigint(20) unsigned NOT NULL,
order_item_id bigint(20) unsigned NOT NULL,
user_id bigint(20) unsigned NOT NULL,
product_id bigint(20) unsigned NOT NULL,
file_key varchar(64) NOT NULL DEFAULT 'main',
download_limit int(11) DEFAULT NULL,
download_count int(11) NOT NULL DEFAULT 0,
expires_at datetime DEFAULT NULL,
active tinyint(1) NOT NULL DEFAULT 1,
last_downloaded_at datetime DEFAULT NULL,
created_at datetime NOT NULL,
PRIMARY KEY (id),
KEY order_id (order_id),
KEY user_product (user_id,product_id)
) $charset;"
);
dbDelta(
"CREATE TABLE {$t['events']} (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
order_id bigint(20) unsigned NOT NULL,
created_at datetime NOT NULL,
actor_id bigint(20) unsigned NOT NULL DEFAULT 0,
type varchar(40) NOT NULL DEFAULT '',
message text NOT NULL,
PRIMARY KEY (id),
KEY order_id (order_id)
) $charset;"
);
update_option( 'paygrab_db_version', PAYGRAB_DB_VERSION, true );
}
/**
* Create the pages PAYGRAB needs (only those not set yet).
*
* @return void
*/
public static function create_pages() {
$pages = array(
'checkout' => array( _x( 'Checkout', 'page title', 'paygrab' ), 'checkout', '[paygrab_checkout]' ),
'mypage' => array( _x( 'My page', 'page title', 'paygrab' ), 'mypage', '[paygrab_mypage]' ),
'register' => array( _x( 'Sign up', 'page title', 'paygrab' ), 'register', '[paygrab_register]' ),
'login' => array( _x( 'Log in', 'page title', 'paygrab' ), 'login', '[paygrab_login]' ),
'tokushoho' => array( _x( 'Legal notice (Specified Commercial Transactions Act)', 'page title', 'paygrab' ), 'tokushoho', '[paygrab_tokushoho]' ),
);
// YOMERU (or another plugin or theme) owns the My page URL; give PAYGRAB's page another name and URL.
if ( 'mypage' === paygrab_yomeru_mypage_slug() || paygrab_slug_claimed_by_rewrite( 'mypage' ) ) {
$pages['mypage'] = array( __( 'Order history', 'paygrab' ), 'purchases', '[paygrab_mypage]' );
}
$changes = array();
foreach ( $pages as $key => $page ) {
$current = (int) Paygrab_Settings::get( 'page_' . $key );
if ( $current && get_post( $current ) && 'trash' !== get_post_status( $current ) ) {
continue;
}
$id = wp_insert_post(
array(
'post_type' => 'page',
'post_status' => 'publish',
'post_title' => $page[0],
'post_name' => $page[1],
'post_content' => '' . $page[2] . '',
'comment_status' => 'closed',
)
);
if ( $id && ! is_wp_error( $id ) ) {
$changes[ 'page_' . $key ] = (int) $id;
}
}
if ( ! Paygrab_Settings::get( 'page_terms' ) ) {
$privacy = (int) get_option( 'wp_page_for_privacy_policy' );
if ( $privacy ) {
$changes['page_terms'] = $privacy;
}
}
if ( $changes ) {
Paygrab_Settings::update( $changes );
}
}
}