%s', esc_url( self::page_url() ), esc_html__( 'Settings', 'orderctrl-bridge' ) ) ); return $links; } /** * "Connect to OrderCtrl" notice on the Dashboard and Plugins screens until the store is connected or the user dismisses it. */ public function connection_notice() { if ( OrderCTRL_Bridge_Config::is_configured() || ! current_user_can( self::CAPABILITY ) ) { return; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || ! in_array( $screen->id, array( 'dashboard', 'plugins' ), true ) ) { return; } if ( get_user_meta( get_current_user_id(), self::DISMISS_META, true ) ) { return; } $dismiss = wp_nonce_url( admin_url( 'admin-post.php?action=orderctrl_bridge_dismiss_notice' ), 'orderctrl_bridge_dismiss_notice' ); printf( '

%1$s %2$s

%4$s %6$s

', esc_html__( 'OrderCtrl is installed.', 'orderctrl-bridge' ), esc_html__( 'Connect your store in OrderCtrl to turn on customer sign-in and package protection.', 'orderctrl-bridge' ), esc_url( self::page_url() ), esc_html__( 'Set up OrderCtrl', 'orderctrl-bridge' ), esc_url( $dismiss ), esc_html__( 'Dismiss', 'orderctrl-bridge' ) ); } /** * Hides the connect notice for this user for good. */ public function handle_dismiss_notice() { check_admin_referer( 'orderctrl_bridge_dismiss_notice' ); if ( ! current_user_can( self::CAPABILITY ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'orderctrl-bridge' ), 403 ); } update_user_meta( get_current_user_id(), self::DISMISS_META, time() ); wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url() ); exit; } /** * Removes the connection details from this site. */ public function handle_disconnect() { check_admin_referer( 'orderctrl_bridge_disconnect' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'orderctrl-bridge' ), 403 ); } OrderCTRL_Bridge_Config::reset(); OrderCTRL_Bridge_Config::log( 'Connection removed by an administrator.', 'notice', array( 'user_id' => get_current_user_id() ) ); wp_safe_redirect( add_query_arg( 'orderctrl_notice', 'disconnected', self::page_url( self::SSO_SLUG ) ) ); exit; } /** * Saves connection details entered by hand (support use: when the app can't reach the site). */ public function handle_manual_connect() { check_admin_referer( 'orderctrl_bridge_manual_connect' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'orderctrl-bridge' ), 403 ); } $params = array( 'app_url' => isset( $_POST['app_url'] ) ? esc_url_raw( wp_unslash( $_POST['app_url'] ) ) : '', 'store_slug' => isset( $_POST['store_slug'] ) ? sanitize_text_field( wp_unslash( $_POST['store_slug'] ) ) : '', 'api_key' => isset( $_POST['api_key'] ) ? sanitize_text_field( wp_unslash( $_POST['api_key'] ) ) : '', 'sso_app_origin' => isset( $_POST['sso_app_origin'] ) ? esc_url_raw( wp_unslash( $_POST['sso_app_origin'] ) ) : '', 'sso_check_url' => isset( $_POST['sso_check_url'] ) ? esc_url_raw( wp_unslash( $_POST['sso_check_url'] ) ) : '', ); // A blank key keeps the saved one. if ( '' === trim( $params['api_key'] ) ) { $params['api_key'] = OrderCTRL_Bridge_Config::api_key(); } $result = OrderCTRL_Bridge_Config::save_from_app( $params ); if ( true === $result ) { update_option( OrderCTRL_Bridge_Config::OPTION_AUTO_CONFIGURED, 'no', false ); } wp_safe_redirect( add_query_arg( 'orderctrl_notice', true === $result ? 'saved' : 'invalid', self::page_url( self::SSO_SLUG ) ) ); exit; } /** * Dashboard page. */ public function render_dashboard() { if ( current_user_can( self::CAPABILITY ) ) { $stats = self::protection_stats(); include ORDERCTRL_BRIDGE_DIR . 'admin/partials/dashboard.php'; } } /** * Package Protection page (read-only). */ public function render_protection() { if ( current_user_can( self::CAPABILITY ) ) { include ORDERCTRL_BRIDGE_DIR . 'admin/partials/package-protection.php'; } } /** * SSO Settings page. */ public function render_sso() { if ( current_user_can( self::CAPABILITY ) ) { $notice = isset( $_GET['orderctrl_notice'] ) ? sanitize_key( wp_unslash( $_GET['orderctrl_notice'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display only. include ORDERCTRL_BRIDGE_DIR . 'admin/partials/sso-settings.php'; } } /** * Protected orders and protection fees in the last 30 days, from the order meta this plugin * writes. Cached for an hour. * * @return array{orders: int, revenue: float, days: int} */ public static function protection_stats() { $cached = get_transient( self::STATS_TRANSIENT ); if ( is_array( $cached ) ) { return $cached; } $stats = array( 'orders' => 0, 'revenue' => 0.0, 'days' => 30 ); if ( function_exists( 'wc_get_orders' ) ) { $orders = wc_get_orders( array( 'limit' => -1, 'return' => 'objects', 'status' => array( 'wc-processing', 'wc-completed', 'wc-on-hold' ), 'date_created' => '>' . ( time() - 30 * DAY_IN_SECONDS ), 'meta_key' => OrderCTRL_Bridge_Protection_Settings::META_PROTECTED, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key 'meta_value' => 'yes', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value ) ); foreach ( $orders as $order ) { ++$stats['orders']; $stats['revenue'] += (float) $order->get_meta( OrderCTRL_Bridge_Protection_Settings::META_AMOUNT ); } } set_transient( self::STATS_TRANSIENT, $stats, HOUR_IN_SECONDS ); return $stats; } /** * Adds the "Protection" column after the order status. * * @param array $columns Columns. * @return array */ public function add_orders_column( $columns ) { $result = array(); foreach ( $columns as $key => $label ) { $result[ $key ] = $label; if ( 'order_status' === $key ) { $result['orderctrl_protection'] = __( 'Protection', 'orderctrl-bridge' ); } } if ( ! isset( $result['orderctrl_protection'] ) ) { $result['orderctrl_protection'] = __( 'Protection', 'orderctrl-bridge' ); } return $result; } /** * Column on the legacy (posts) orders list. * * @param string $column Column. * @param int $post_id Order id. */ public function render_orders_column_legacy( $column, $post_id ) { if ( 'orderctrl_protection' === $column ) { $this->render_orders_column( $column, wc_get_order( $post_id ) ); } } /** * Column on the HPOS orders list. * * @param string $column Column. * @param WC_Order $order Order. */ public function render_orders_column( $column, $order ) { if ( 'orderctrl_protection' !== $column || ! $order instanceof WC_Order ) { return; } if ( 'yes' === $order->get_meta( OrderCTRL_Bridge_Protection_Settings::META_PROTECTED ) ) { printf( '%2$s', esc_attr__( 'The customer added OrderCtrl package protection', 'orderctrl-bridge' ), esc_html__( 'Protected', 'orderctrl-bridge' ) ); } else { echo ''; } } /** * A read-only toggle in the settings style (shows the value; changed in OrderCtrl). * * @param bool $on Value. * @return string */ public static function toggle( $on ) { return sprintf( ' %3$s', esc_attr__( 'Change this in OrderCtrl', 'orderctrl-bridge' ), $on ? ' checked' : '', $on ? esc_html__( 'On', 'orderctrl-bridge' ) : esc_html__( 'Off', 'orderctrl-bridge' ) ); } }