'es', 'pt' => 'pt_BR' );
if ( isset( $file[ $lang ] ) ) {
load_textdomain( 'kloom-digitala', __DIR__ . '/languages/kloom-digitala-' . $file[ $lang ] . '.mo' );
}
} );
// Service pages in the admin's language: Spanish has no prefix.
function kloom_digitala_url( $path ) {
$lang = substr( determine_locale(), 0, 2 );
$pre = 'es' === $lang ? '' : ( 'pt' === $lang ? '/pt' : '/en' );
return KLOOM_DIGITALA_SERVICE . $pre . $path;
}
// The webhook this plugin created, if WooCommerce still has it.
function kloom_digitala_webhook() {
$id = (int) get_option( KLOOM_DIGITALA_OPTION );
if ( ! $id || ! function_exists( 'wc_get_webhook' ) ) {
return null;
}
$webhook = wc_get_webhook( $id );
return $webhook && $webhook->get_id() ? $webhook : null;
}
// A deactivated plugin sends nothing: the webhook is paused until it is activated again.
function kloom_digitala_set_status( $status ) {
$webhook = kloom_digitala_webhook();
if ( $webhook ) {
$webhook->set_status( $status );
$webhook->save();
}
}
register_deactivation_hook( __FILE__, function () {
kloom_digitala_set_status( 'paused' );
} );
register_activation_hook( __FILE__, function () {
kloom_digitala_set_status( 'active' );
} );
add_action( 'admin_init', function () {
wp_add_privacy_policy_content( 'Digitala', wp_kses_post( wpautop( __( 'This store delivers its digital products with Digitala (https://digitala.kloomstudio.com.ar). When an order is paid, this store sends Digitala the order number, status, items and the name and email of the buyer, so Digitala can email the buyer the download links. Privacy policy: https://digitala.kloomstudio.com.ar/en/privacidad', 'kloom-digitala' ) ) ) );
} );
add_action( 'admin_menu', function () {
add_submenu_page( 'woocommerce', 'Digitala', 'Digitala', 'manage_woocommerce', 'kloom-digitala', 'kloom_digitala_page' );
}, 99 );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), function ( $links ) {
array_unshift( $links, sprintf( '%s', esc_url( admin_url( 'admin.php?page=kloom-digitala' ) ), esc_html__( 'Settings', 'kloom-digitala' ) ) );
return $links;
} );
add_action( 'admin_notices', function () {
$screen = get_current_screen();
if ( ! $screen || 'plugins' !== $screen->id || ! current_user_can( 'manage_woocommerce' ) || kloom_digitala_webhook() ) {
return;
}
printf(
'
',
esc_html__( 'Digitala is installed. Connect your store to start delivering files.', 'kloom-digitala' ),
esc_url( admin_url( 'admin.php?page=kloom-digitala' ) ),
esc_html__( 'Connect your store', 'kloom-digitala' )
);
} );
function kloom_digitala_back( $msg ) {
wp_safe_redirect( add_query_arg( 'kd', $msg, admin_url( 'admin.php?page=kloom-digitala' ) ) );
exit;
}
add_action( 'admin_post_kloom_digitala_connect', function () {
check_admin_referer( 'kloom_digitala_connect' );
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_die( esc_html__( 'You are not allowed to do this.', 'kloom-digitala' ) );
}
$url = isset( $_POST['kd_url'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['kd_url'] ) ) ) : '';
$secret = isset( $_POST['kd_secret'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['kd_secret'] ) ) ) : '';
// Only Digitala's own delivery address, so the form can never point order data anywhere else.
if ( ! preg_match( '#^' . preg_quote( KLOOM_DIGITALA_SERVICE, '#' ) . '/webhooks/in/[0-9a-f]{24}$#', $url ) ) {
kloom_digitala_back( 'bad_url' );
}
if ( ! preg_match( '/^[0-9a-f]{64}$/', $secret ) ) {
kloom_digitala_back( 'bad_secret' );
}
$webhook = kloom_digitala_webhook();
if ( ! $webhook ) {
$webhook = new WC_Webhook();
$webhook->set_user_id( get_current_user_id() );
}
$webhook->set_name( 'Digitala' );
$webhook->set_topic( 'order.updated' );
$webhook->set_api_version( 'wp_api_v3' );
$webhook->set_delivery_url( $url );
$webhook->set_secret( $secret );
$webhook->set_status( 'active' );
$id = $webhook->save();
if ( ! $id ) {
kloom_digitala_back( 'failed' );
}
update_option( KLOOM_DIGITALA_OPTION, $id, false );
kloom_digitala_back( 'connected' );
} );
add_action( 'admin_post_kloom_digitala_disconnect', function () {
check_admin_referer( 'kloom_digitala_disconnect' );
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_die( esc_html__( 'You are not allowed to do this.', 'kloom-digitala' ) );
}
$webhook = kloom_digitala_webhook();
if ( $webhook ) {
$webhook->delete( true );
}
delete_option( KLOOM_DIGITALA_OPTION );
kloom_digitala_back( 'disconnected' );
} );
// Review request on WordPress.org: one step, only to a merchant whose store has been connected for a
// week, never during setup. "Not now" hides it 15 days for this user; "already left a review" for good.
add_action( 'admin_notices', function () {
$screen = get_current_screen();
if ( ! $screen || ! in_array( $screen->id, array( 'plugins', 'woocommerce_page_kloom-digitala' ), true ) || ! current_user_can( 'manage_woocommerce' ) || ! kloom_digitala_webhook() ) {
return;
}
$since = (int) get_option( 'kloom_digitala_connected_since' );
if ( ! $since ) {
update_option( 'kloom_digitala_connected_since', time(), false );
return;
}
if ( time() - $since < 7 * DAY_IN_SECONDS || (int) get_user_meta( get_current_user_id(), 'kloom_digitala_review_hide', true ) > time() ) {
return;
}
$hide = function ( $what ) {
return wp_nonce_url( admin_url( 'admin-post.php?action=kloom_digitala_review&what=' . $what ), 'kloom_digitala_review' );
};
printf(
'',
/* translators: %s: plugin name */
esc_html( sprintf( __( '%s has been running on your store for a while. A review on WordPress.org helps other stores find it, and we read every one.', 'kloom-digitala' ), 'Digitala' ) ),
'https://wordpress.org/support/plugin/kloom-digitala/reviews/#new-post',
esc_html__( 'Review on WordPress.org', 'kloom-digitala' ),
esc_url( $hide( 'later' ) ),
esc_html__( 'Not now', 'kloom-digitala' ),
esc_url( $hide( 'done' ) ),
esc_html__( 'I already left a review', 'kloom-digitala' )
);
} );
add_action( 'admin_post_kloom_digitala_review', function () {
check_admin_referer( 'kloom_digitala_review' );
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- checked just above.
$done = isset( $_GET['what'] ) && 'done' === $_GET['what'];
update_user_meta( get_current_user_id(), 'kloom_digitala_review_hide', time() + ( $done ? 20 * YEAR_IN_SECONDS : 15 * DAY_IN_SECONDS ) );
wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url( 'plugins.php' ) );
exit;
} );
function kloom_digitala_page() {
$webhook = kloom_digitala_webhook();
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display-only status flag set by our own redirect.
$msg = isset( $_GET['kd'] ) ? sanitize_key( wp_unslash( $_GET['kd'] ) ) : '';
$messages = array(
'connected' => array( 'success', __( 'Connected. Paid orders now go to Digitala.', 'kloom-digitala' ) ),
'disconnected' => array( 'success', __( 'Disconnected. Orders no longer go to Digitala.', 'kloom-digitala' ) ),
'bad_url' => array( 'error', __( 'That delivery address is not a Digitala address. Copy it again from Channels in your Digitala dashboard.', 'kloom-digitala' ) ),
'bad_secret' => array( 'error', __( 'That secret is not valid. It is 64 characters long; if you lost it, create a new one in your Digitala dashboard.', 'kloom-digitala' ) ),
'failed' => array( 'error', __( 'WooCommerce could not save the webhook. Try again.', 'kloom-digitala' ) ),
);
?>