$links The plugin's action links. * @return array */ function pirkoo_plugin_action_links( $links ) { if ( ! current_user_can( 'manage_woocommerce' ) ) { return $links; } if ( pirkoo_is_connected() ) { array_unshift( $links, '' . esc_html__( 'Open Pirkoo', 'pirkoo' ) . '', '' . esc_html__( 'Disconnect from Pirkoo', 'pirkoo' ) . '' ); } else { array_unshift( $links, '' . esc_html__( 'Connect to Pirkoo', 'pirkoo' ) . '' ); } return $links; } /** * Lets wp_safe_redirect() send the browser to app.pirkoo.com. * * @param string[] $hosts Allowed hosts. * @return string[] */ function pirkoo_allowed_redirect_hosts( $hosts ) { $app = wp_parse_url( PIRKOO_APP_URL, PHP_URL_HOST ); if ( is_string( $app ) ) { $hosts[] = $app; } return $hosts; } /** * Connect to Pirkoo: a new one-time code, then app.pirkoo.com to confirm. * * @return void */ function pirkoo_handle_connect() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to connect this store.', 'pirkoo' ), 403 ); } check_admin_referer( 'pirkoo_connect' ); $code = pirkoo_random_token(); pirkoo_update_settings( array( 'code_hash' => hash( 'sha256', $code ), 'code_expires' => time() + PIRKOO_CODE_LIFETIME, ) ); $url = PIRKOO_APP_URL . '/connect?' . http_build_query( array( 'store' => pirkoo_store_origin(), // This plugin's REST address: not /wp-json/ with plain permalinks or WordPress in a // subfolder (0.3.1). The app accepts it only on the store's own host. 'api' => untrailingslashit( rest_url( 'pirkoo/v1' ) ), 'code' => $code, // Where the app sends the administrator back when a firewall blocks its requests here // (relay mode, includes/relay.php). 'return' => admin_url( 'admin-post.php?action=pirkoo_relay' ), ), '', '&', PHP_QUERY_RFC3986 ); wp_safe_redirect( $url ); exit; } /** * Disconnect from Pirkoo: tells the app (best effort) and forgets the secret. The store's history * stays in the app. * * @return void */ function pirkoo_handle_disconnect() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to disconnect this store.', 'pirkoo' ), 403 ); } check_admin_referer( 'pirkoo_disconnect' ); $secret = pirkoo_settings()['secret']; if ( '' !== $secret ) { $body = (string) wp_json_encode( array( 'store' => pirkoo_store_origin() ) ); $path = '/api/stores/disconnect'; wp_remote_post( PIRKOO_APP_URL . $path, array( 'timeout' => 10, 'headers' => array_merge( array( 'Content-Type' => 'application/json' ), pirkoo_signature_headers( $secret, 'POST', $path, $body ) ), 'body' => $body, ) ); } pirkoo_forget_connection(); wp_safe_redirect( add_query_arg( 'pirkoo', 'disconnected', admin_url( 'plugins.php' ) ) ); exit; } /** * Forgets the connection: the secret and any code in progress. * * @return void */ function pirkoo_forget_connection() { pirkoo_update_settings( array( 'secret' => '', 'connected_at' => 0, 'code_hash' => '', 'code_expires' => 0, 'relay' => false, ) ); wp_clear_scheduled_hook( PIRKOO_RELAY_HOOK ); } /** * The notice on the Plugins screen after disconnecting. * * @return void */ function pirkoo_admin_notices() { $screen = get_current_screen(); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only chooses a fixed notice to show. if ( ! $screen || 'plugins' !== $screen->id || ! isset( $_GET['pirkoo'] ) ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Only chooses a fixed notice to show. $which = sanitize_key( wp_unslash( $_GET['pirkoo'] ) ); if ( 'disconnected' === $which ) { echo '

' . esc_html__( 'This store is disconnected from Pirkoo. Its history stays in the app; connect it again from here at any time.', 'pirkoo' ) . '

'; } elseif ( 'relay_expired' === $which ) { echo '

' . esc_html__( 'That connection link has expired. Click Connect to Pirkoo again.', 'pirkoo' ) . '

'; } elseif ( 'relay_failed' === $which ) { echo '

' . esc_html__( 'This store could not reach app.pirkoo.com to finish the connection. Check that its server can make outgoing HTTPS requests, then click Connect to Pirkoo again.', 'pirkoo' ) . '

'; } } /** * The signature headers of a request signed with the connection secret: the hex HMAC-SHA256 of * the timestamp, the method, the URL path and the sha256 of the body, one per line. * * @param string $secret The connection secret. * @param string $method HTTP method. * @param string $path URL path, e.g. /wp-json/pirkoo/v1/collect. * @param string $body The request body. * @param int|null $time Unix seconds (now when null). * @return array{X-Pirkoo-Timestamp: string, X-Pirkoo-Signature: string} */ function pirkoo_signature_headers( $secret, $method, $path, $body, $time = null ) { $time = null === $time ? time() : (int) $time; return array( 'X-Pirkoo-Timestamp' => (string) $time, 'X-Pirkoo-Signature' => pirkoo_signature( $secret, $time, $method, $path, $body ), ); } /** * The signature itself (see pirkoo_signature_headers()). * * @param string $secret The connection secret. * @param int $time Unix seconds. * @param string $method HTTP method. * @param string $path URL path. * @param string $body The request body. * @return string */ function pirkoo_signature( $secret, $time, $method, $path, $body ) { return hash_hmac( 'sha256', $time . "\n" . $method . "\n" . $path . "\n" . hash( 'sha256', $body ), $secret ); } /** * The plugin's REST routes. * * @return void */ function pirkoo_register_routes() { register_rest_route( 'pirkoo/v1', '/connect', array( 'methods' => 'POST', 'permission_callback' => '__return_true', // The one-time code is checked in the callback. 'callback' => 'pirkoo_rest_connect', ) ); foreach ( array( '/collect' => 'pirkoo_rest_collect', '/disconnect' => 'pirkoo_rest_disconnect', '/sync' => 'pirkoo_rest_sync', '/inventory' => 'pirkoo_rest_inventory', '/update' => 'pirkoo_rest_update', ) as $route => $callback ) { register_rest_route( 'pirkoo/v1', $route, array( 'methods' => 'POST', 'permission_callback' => 'pirkoo_rest_signed', 'callback' => $callback, ) ); } } /** * Lets requests to this plugin's own routes through when the site blocks the REST API for visitors * who aren't logged in (a common security setting, which would otherwise stop app.pirkoo.com). Only * /pirkoo/v1/ is affected, and each of its routes still checks the one-time code or the signature * itself. * * @param WP_Error|null|true $result The authentication result so far. * @return WP_Error|null|true */ function pirkoo_rest_allow_own_routes( $result ) { global $wp; if ( ! is_wp_error( $result ) || ! isset( $wp->query_vars['rest_route'] ) ) { return $result; } $route = '/' . ltrim( (string) $wp->query_vars['rest_route'], '/' ); return 0 === strpos( $route, '/pirkoo/v1/' ) ? null : $result; } /** * POST /pirkoo/v1/connect {"code"}: exchanges a valid one-time code for a new connection secret. * * @param WP_REST_Request $request The request. * @return array{secret: string}|WP_Error */ function pirkoo_rest_connect( WP_REST_Request $request ) { $code = $request->get_param( 'code' ); $settings = pirkoo_settings(); if ( ! is_string( $code ) || '' === $settings['code_hash'] || $settings['code_expires'] < time() || ! hash_equals( $settings['code_hash'], hash( 'sha256', $code ) ) ) { return new WP_Error( 'pirkoo_invalid_code', 'Unknown, used or expired connection code.', array( 'status' => 403 ) ); } $secret = pirkoo_random_token(); pirkoo_update_settings( array( 'secret' => $secret, 'connected_at' => time(), 'code_hash' => '', 'code_expires' => 0, 'relay' => false, ) ); wp_clear_scheduled_hook( PIRKOO_RELAY_HOOK ); return array( 'secret' => $secret ); } /** * Permission for the signed routes: a request signed with the connection secret, within 5 minutes * of this server's clock, not seen before. * * @param WP_REST_Request $request The request. * @return true|WP_Error */ function pirkoo_rest_signed( WP_REST_Request $request ) { $secret = pirkoo_settings()['secret']; if ( '' === $secret ) { return new WP_Error( 'pirkoo_not_connected', 'This store is not connected to Pirkoo.', array( 'status' => 401 ) ); } $time = $request->get_header( 'x_pirkoo_timestamp' ); $signature = $request->get_header( 'x_pirkoo_signature' ); if ( ! is_string( $time ) || ! ctype_digit( $time ) || ! is_string( $signature ) || '' === $signature ) { return new WP_Error( 'pirkoo_unsigned', 'The request is not signed.', array( 'status' => 401 ) ); } if ( abs( time() - (int) $time ) > PIRKOO_CLOCK_SKEW ) { return new WP_Error( 'pirkoo_clock_skew', 'The signature\'s time is more than 5 minutes off: check this server\'s clock.', array( 'status' => 401 ) ); } // The signed route is always /wp-json/pirkoo/v1/, whatever this site's REST address // (plain permalinks, a subfolder, another prefix): both sides sign the same string. $path = '/wp-json' . $request->get_route(); $expected = pirkoo_signature( $secret, (int) $time, $request->get_method(), $path, $request->get_body() ); if ( ! hash_equals( $expected, strtolower( $signature ) ) ) { return new WP_Error( 'pirkoo_bad_signature', 'Wrong signature.', array( 'status' => 401 ) ); } $seen = 'pirkoo_sig_' . substr( $expected, 0, 40 ); if ( false !== get_transient( $seen ) ) { return new WP_Error( 'pirkoo_replayed', 'Signature already used.', array( 'status' => 401 ) ); } set_transient( $seen, 1, 2 * PIRKOO_CLOCK_SKEW ); return true; } /** * POST /pirkoo/v1/collect: the store's data for a scan, collected now (the checkout profile makes * it take several seconds). One collection at a time: 409 while another runs. * * @return array|WP_Error */ function pirkoo_rest_collect() { if ( ! pirkoo_lock() ) { return new WP_Error( 'pirkoo_busy', 'A collection is already running.', array( 'status' => 409 ) ); } try { return pirkoo_collect(); } finally { pirkoo_unlock(); } } /** * What a scan needs, as 0.2.x sent it to the gateway, plus what only WordPress knows about the * store for the app's pages. * * @return array */ function pirkoo_collect() { return array( 'website' => home_url( '/' ), 'email' => (string) get_option( 'admin_email' ), 'pages' => array( 'cart' => wc_get_cart_url(), 'checkout' => wc_get_checkout_url(), ), 'metrics' => pirkoo_collect_metrics(), 'detailed' => pirkoo_collect_detailed(), 'store' => array( 'name' => wp_specialchars_decode( (string) get_bloginfo( 'name' ), ENT_QUOTES ), 'timezone' => wp_timezone_string(), 'date_format' => (string) get_option( 'date_format' ), 'time_format' => (string) get_option( 'time_format' ), 'locale' => get_locale(), 'payment_methods' => pirkoo_payment_method_titles(), 'shipping_methods' => pirkoo_shipping_method_titles(), 'plugins' => pirkoo_installed_plugins(), ), ); } /** * The installed plugins' folder names (their WordPress.org slugs) and whether each is active, so the * app can tell which of the plugins it recommends the store already has (0.4). Names only, at most * 300; a single-file plugin counts by its file name. * * @return array */ function pirkoo_installed_plugins() { if ( ! function_exists( 'get_plugins' ) ) { require_once ABSPATH . 'wp-admin/includes/plugin.php'; } $out = array(); foreach ( array_keys( get_plugins() ) as $file ) { $slug = dirname( $file ); if ( '.' === $slug ) { $slug = basename( $file, '.php' ); } $out[] = array( 'slug' => sanitize_key( $slug ), 'active' => is_plugin_active( $file ), ); if ( count( $out ) >= 300 ) { break; } } return $out; } /** * A method title as the store shows it to shoppers: tags stripped, at most 80 characters. * * @param mixed $title The title. * @return string */ function pirkoo_method_title( $title ) { $title = trim( wp_strip_all_tags( html_entity_decode( (string) $title, ENT_QUOTES, 'UTF-8' ) ) ); return function_exists( 'mb_substr' ) ? mb_substr( $title, 0, 80 ) : substr( $title, 0, 80 ); } /** * The enabled payment gateways with the store's own titles (0.3.1), for the app's store profile: * id and title only, never settings. At most 30. * * @return array */ function pirkoo_payment_method_titles() { $out = array(); foreach ( WC()->payment_gateways()->payment_gateways() as $gateway ) { if ( is_object( $gateway ) && isset( $gateway->enabled, $gateway->id ) && 'yes' === $gateway->enabled && method_exists( $gateway, 'get_title' ) ) { $title = pirkoo_method_title( $gateway->get_title() ); if ( '' !== $title ) { $out[] = array( 'id' => (string) $gateway->id, 'title' => $title, ); } } } return array_slice( $out, 0, 30 ); } /** * The shipping methods enabled in any zone with the titles the store gave each (0.3.1), e.g. * flat_rate "PAC" and flat_rate "SEDEX": id and title only. Repeated titles once; at most 30. * * @return array */ function pirkoo_shipping_method_titles() { if ( ! class_exists( 'WC_Shipping_Zones' ) ) { return array(); } $zones = array( new WC_Shipping_Zone( 0 ) ); // "Locations not covered by your other zones". foreach ( WC_Shipping_Zones::get_zones( 'json' ) as $zone ) { $zones[] = new WC_Shipping_Zone( (int) $zone['id'] ); } $out = array(); $seen = array(); foreach ( $zones as $zone ) { foreach ( $zone->get_shipping_methods( true ) as $method ) { $title = pirkoo_method_title( $method->get_title() ); if ( '' === $title || isset( $seen[ $method->id . '|' . $title ] ) ) { continue; } $seen[ $method->id . '|' . $title ] = true; $out[] = array( 'id' => (string) $method->id, 'title' => $title, ); } } return array_slice( $out, 0, 30 ); } /** * POST /pirkoo/v1/disconnect: the app ended the connection; the secret is forgotten. * * @return array{status: string} */ function pirkoo_rest_disconnect() { pirkoo_forget_connection(); return array( 'status' => 'ok' ); } /** * Takes the lock that lets one collection run at a time. The lock is an option row inserted * atomically, as in WordPress core's WP_Upgrader::create_lock(); one older than five minutes was * left by a request that died and is taken over. * * @return bool Whether this request now holds the lock. */ function pirkoo_lock() { global $wpdb; $now = (string) time(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- The atomic insert is the lock; the options API can't refuse a row that exists. if ( $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES ( %s, %s, 'no' )", PIRKOO_LOCK, $now ) ) ) { return true; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Read the row itself: the options cache doesn't know about the insert above. $since = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM `$wpdb->options` WHERE option_name = %s", PIRKOO_LOCK ) ); if ( null === $since || (int) $since > time() - 5 * MINUTE_IN_SECONDS ) { return false; } // Take over a stale lock only if no other request took it over first. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Compare-and-set on the lock row. return 1 === $wpdb->query( $wpdb->prepare( "UPDATE `$wpdb->options` SET option_value = %s WHERE option_name = %s AND option_value = %s", $now, PIRKOO_LOCK, $since ) ); } /** * Releases the lock taken by pirkoo_lock(). * * @return void */ function pirkoo_unlock() { delete_option( PIRKOO_LOCK ); } /** * Suggests text for the site's privacy policy (Settings → Privacy → Policy Guide). * * @return void */ function pirkoo_privacy_policy_content() { if ( ! function_exists( 'wp_add_privacy_policy_content' ) ) { return; } $content = '

' . esc_html__( 'This site uses the Pirkoo plugin (On-board Diagnostics Scanner for WooCommerce), by tvarkom (MB TVARKOM SOFTWARE, Lithuania). After an administrator connects the store to Pirkoo, each time Pirkoo scans the store (once a day, and when the store\'s team asks) its address, its cart and checkout page addresses and the site\'s admin email are sent to tvarkom, with aggregate store data, such as software versions, database sizes, background task counts, order counts, the 30-day gross sales and average order value, new and returning customer counts, daily shop activity totals and order source counts, a profile of one checkout page load, the store\'s name, timezone and date format, and the titles of its payment and shipping methods. From Pirkoo 0.4, the store\'s orders of the last 12 months (with the billing customer\'s name, email, phone, company, city, state, postcode and country), its registered customers, products and coupons are also sent, once a day, so the store\'s team can see them at app.pirkoo.com; tvarkom processes them on this site\'s behalf and keeps orders for 12 months. Street addresses, shipping details, order notes and payment details are not sent. tvarkom keeps each scan\'s results and analysis linked to the store and shows them to the store\'s team at app.pirkoo.com.', 'pirkoo' ) . '

'; $content .= '

' . esc_html__( 'Pirkoo Privacy Policy', 'pirkoo' ) . '

'; wp_add_privacy_policy_content( 'Pirkoo', wp_kses_post( $content ) ); }