$links Existing links. * @return array */ public static function action_links( $links ) { $url = admin_url( 'tools.php?page=bcgn-generator' ); array_unshift( $links, sprintf( '%s', esc_url( $url ), esc_html__( 'Generate a barcode', 'barcodegen' ) ) ); return $links; } /** * Tools → Barcode Generator. */ public static function generator_page() { if ( ! current_user_can( 'edit_posts' ) ) { return; } $formats = BCGN_Barcode::formats(); $options = self::request_options(); // The generator form is a read-only GET form; the nonce only proves the // values came from this screen and were not crafted by someone else. $verified = isset( $_GET['_wpnonce'] ) && false !== wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'bcgn-generator' ); $value = ( $verified && isset( $_GET['value'] ) ) ? sanitize_text_field( wp_unslash( $_GET['value'] ) ) : ''; $svg = ''; $error = ''; if ( '' !== $value ) { $svg = BCGN_Barcode::svg( $options['format'], $value, $options ); if ( is_wp_error( $svg ) ) { $error = $svg->get_error_message(); $svg = ''; } } $shortcode = self::shortcode_snippet( $options, $value ); ?>

*/ protected static function request_options() { $defaults = array( 'format' => BCGN_Settings::get( 'default_format' ), 'scale' => BCGN_Settings::get( 'scale' ), 'height' => BCGN_Settings::get( 'height' ), 'text' => (bool) BCGN_Settings::get( 'text' ), 'bar_color' => BCGN_Settings::get( 'bar_color' ), 'bg_color' => BCGN_Settings::get( 'bg_color' ), 'padding' => 0, ); $verified = isset( $_GET['_wpnonce'] ) && false !== wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'bcgn-generator' ); if ( ! $verified || ! isset( $_GET['format'] ) ) { return $defaults; } $formats = BCGN_Barcode::formats(); if ( isset( $_GET['format'] ) && isset( $formats[ sanitize_key( wp_unslash( $_GET['format'] ) ) ] ) ) { $defaults['format'] = sanitize_key( wp_unslash( $_GET['format'] ) ); } if ( isset( $_GET['scale'] ) ) { $defaults['scale'] = (string) max( 0.5, min( 10, (float) $_GET['scale'] ) ); } if ( isset( $_GET['height'] ) ) { $defaults['height'] = (int) max( 10, min( 400, (int) $_GET['height'] ) ); } if ( isset( $_GET['text'] ) ) { $defaults['text'] = ! empty( $_GET['text'] ); } if ( isset( $_GET['bg_color'] ) ) { $defaults['bg_color'] = BCGN_SVG::color( sanitize_text_field( wp_unslash( $_GET['bg_color'] ) ), 'transparent' ); } return $defaults; } /** * Build the copyable shortcode for the generated barcode. * * @param array $options Current generator options. * @param string $value Barcode value. * @return string */ protected static function shortcode_snippet( $options, $value ) { $parts = array( sprintf( 'format="%s"', $options['format'] ), sprintf( 'value="%s"', $value ) ); if ( (string) $options['scale'] !== (string) BCGN_Settings::get( 'scale' ) ) { $parts[] = sprintf( 'scale="%s"', $options['scale'] ); } if ( (string) $options['height'] !== (string) BCGN_Settings::get( 'height' ) ) { $parts[] = sprintf( 'height="%d"', (int) $options['height'] ); } if ( empty( $options['text'] ) !== empty( BCGN_Settings::get( 'text' ) ) ) { $parts[] = sprintf( 'text="%s"', ! empty( $options['text'] ) ? 'yes' : 'no' ); } if ( 'transparent' !== $options['bg_color'] && $options['bg_color'] !== BCGN_Settings::get( 'bg_color' ) ) { $parts[] = sprintf( 'bg_color="%s"', $options['bg_color'] ); } return '[barcodegen ' . implode( ' ', $parts ) . ']'; } /** * Point power users at the full generator. */ protected static function upsell_box() { ?>