connect = $connect; $this->client = $client; } public function register() { add_action( 'admin_menu', array( $this, 'menu' ) ); add_filter( 'plugin_action_links_' . plugin_basename( THISBIG_FILE ), array( $this, 'action_links' ) ); } public function menu() { // After WooCommerce's own items and Marketing (58), before Appearance (60). $hook = add_menu_page( __( 'ThisBig', 'thisbig' ), __( 'ThisBig', 'thisbig' ), 'manage_options', self::PAGE, array( $this, 'render' ), self::icon(), 59 ); add_action( 'load-' . $hook, array( $this, 'on_load' ) ); add_action( 'admin_print_scripts-' . $hook, array( $this, 'enqueue' ) ); } /** * ThisBig's mark, two end stops and the tape between them, as the menu draws * icons: one colour, which WordPress repaints for the admin colour scheme. */ public static function icon() { $svg = ''; return 'data:image/svg+xml;base64,' . base64_encode( $svg ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- WordPress takes a menu icon as a base64 data URI. } public function enqueue() { wp_enqueue_style( 'thisbig-settings', THISBIG_URL . 'admin/settings.css', array( 'wp-components' ), THISBIG_VERSION ); wp_enqueue_script( 'thisbig-settings', THISBIG_URL . 'admin/settings.js', array( 'wp-element', 'wp-components', 'wp-api-fetch', 'wp-i18n' ), THISBIG_VERSION, array( 'in_footer' => true ) ); wp_set_script_translations( 'thisbig-settings', 'thisbig', ThisBig_Plugin::languages() ); wp_add_inline_script( 'thisbig-settings', 'window.thisbigSettings = ' . wp_json_encode( array( 'adminPost' => admin_url( 'admin-post.php' ), 'portal' => THISBIG_PORTAL, 'nonces' => array( 'connect' => wp_create_nonce( 'thisbig_connect' ), 'disconnect' => wp_create_nonce( 'thisbig_disconnect' ), ), ) ) . ';', 'before' ); } public function action_links( $links ) { array_unshift( $links, sprintf( '%s', esc_url( ThisBig_Connect::settings_url() ), esc_html__( 'Settings', 'thisbig' ) ) ); return $links; } /** Back from the portal: exchange the code, then reload without it, so a refresh can't replay it. */ public function on_load() { $result = $this->connect->handle_return( $_GET ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- the state is the check. if ( null === $result ) { return; } $outcome = is_wp_error( $result ) ? 'failed' : 'connected'; if ( is_wp_error( $result ) ) { set_transient( self::CONNECT_ERROR, $result->get_error_message(), MINUTE_IN_SECONDS * 5 ); } wp_safe_redirect( ThisBig_Connect::settings_url( array( 'thisbig' => $outcome ) ) ); exit; } public function render() { $outcome = isset( $_GET['thisbig'] ) ? sanitize_key( wp_unslash( $_GET['thisbig'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended echo '

' . esc_html__( 'ThisBig', 'thisbig' ) . '

'; if ( 'connected' === $outcome ) { $this->notice( 'success', __( 'Connected. Product pages show the size chip once a shopper sees it.', 'thisbig' ) ); } elseif ( 'failed' === $outcome ) { $this->notice( 'error', (string) get_transient( self::CONNECT_ERROR ) ); } elseif ( 'disconnected' === $outcome ) { $this->notice( 'success', __( 'Disconnected. The size chip is off on this site.', 'thisbig' ) ); } echo '

' . esc_html__( 'Loading…', 'thisbig' ) . '

'; echo ''; echo '
'; } private function notice( $type, $message ) { if ( '' !== $message ) { printf( '

%s

', esc_attr( $type ), esc_html( $message ) ); } } }