' . esc_html__( 'Settings', 'cartmeter' ) . '' ); if ( ! Plugin::pro_installed() ) { $own[] = '' . esc_html__( 'Get Pro', 'cartmeter' ) . ''; } return array_merge( $own, $links ); } /** * Settings page URL. * * @param string $tab Tab ID. * @return string */ public static function url( $tab = '' ) { $url = admin_url( 'admin.php?page=' . self::SLUG ); return $tab ? $url . '#' . $tab : $url; } /** * Whether the current screen is ours. * * @return bool */ public static function is_our_screen() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; return $screen && ( false !== strpos( $screen->id, self::SLUG ) ); } /** * Loads admin files on our screen only. */ public static function assets() { if ( ! self::is_our_screen() ) { return; } wp_enqueue_style( 'wp-color-picker' ); add_thickbox(); // "More details" on the More plugins tab opens WordPress's own plugin window. wp_enqueue_style( 'fspbar-admin', FSPBAR_URL . 'assets/css/admin.css', array(), FSPBAR_VERSION ); wp_enqueue_script( 'fspbar-admin', FSPBAR_URL . 'assets/js/admin.js', array( 'jquery', 'wp-color-picker' ), FSPBAR_VERSION, array( 'in_footer' => true ) ); wp_add_inline_script( 'fspbar-admin', 'window.fspbarAdmin = ' . wp_json_encode( array( 'unsaved' => __( 'You have unsaved changes.', 'cartmeter' ), 'preview' => self::preview_data(), /** * Filters the tabs that show the live preview beside them. * * @param string[] $tabs Tab IDs. */ 'previewTabs' => array_values( (array) apply_filters( 'fspbar_preview_tabs', array( 'goal', 'where', 'style' ) ) ), ) ) . ';', 'before' ); } /** * Tabs on the settings page. * * @return array ID => label. */ public static function tabs() { $tabs = array( 'goal' => __( 'Goal', 'cartmeter' ), 'where' => __( 'Where', 'cartmeter' ), 'style' => __( 'Style', 'cartmeter' ), 'help' => __( 'Help', 'cartmeter' ), ); if ( ! Plugin::pro_installed() ) { $tabs['pro'] = __( 'Pro', 'cartmeter' ); } /** * Filters the settings tabs. Add-ons add their own. * * @param array $tabs ID => label. */ $tabs = apply_filters( 'fspbar_admin_tabs', $tabs ); // Always last: our other free plugins. $tabs['more'] = __( 'More plugins', 'cartmeter' ); return $tabs; } /** * Saves the settings form. */ public static function save() { if ( ! current_user_can( self::capability() ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to change these settings.', 'cartmeter' ), 403 ); } check_admin_referer( 'fspbar_save' ); // Cleaned here, then checked field by field (choices, color, amount) in Settings::sanitize(). $input = isset( $_POST['fspbar'] ) && is_array( $_POST['fspbar'] ) ? map_deep( wp_unslash( $_POST['fspbar'] ), 'sanitize_text_field' ) : array(); Settings::save( $input ); /** * Runs after settings are saved. Add-ons save their own fields here. */ do_action( 'fspbar_settings_saved' ); $tab = isset( $_POST['fspbar_tab'] ) ? sanitize_key( wp_unslash( $_POST['fspbar_tab'] ) ) : ''; wp_safe_redirect( add_query_arg( 'updated', '1', admin_url( 'admin.php?page=' . self::SLUG ) ) . ( $tab ? '#' . $tab : '' ) ); exit; } /** * The free shipping amount the preview shows: the shop's own, or a round example. * * @return float */ private static function preview_goal() { foreach ( Goal::zones_summary() as $zone ) { if ( null !== $zone['amount'] ) { return (float) $zone['amount']; } } return 50.0; } /** * Data for the live preview. * * @return array */ private static function preview_data() { $defaults = Settings::default_texts(); return array( 'css' => FSPBAR_URL . 'assets/css/bar.css?ver=' . FSPBAR_VERSION, 'goal' => self::preview_goal(), 'color' => Settings::DEFAULT_COLOR, 'money' => array( 'symbol' => html_entity_decode( get_woocommerce_currency_symbol(), ENT_QUOTES, 'UTF-8' ), 'format' => get_woocommerce_price_format(), 'decimals' => wc_get_price_decimals(), 'decimal' => wc_get_price_decimal_separator(), 'thousand' => wc_get_price_thousand_separator(), ), 'texts' => array( 'empty' => $defaults['text_empty'], 'progress' => $defaults['text_progress'], 'done' => $defaults['text_done'], ), 'icons' => array( 'truck' => Frontend::icon( array( 'state' => 'progress' ) ), 'check' => Frontend::icon( array( 'state' => 'done' ) ), ), 'i18n' => array( 'shop' => get_bloginfo( 'name' ), 'shopTitle' => __( 'Shop', 'cartmeter' ), 'cart' => __( 'Cart', 'cartmeter' ), 'item' => __( 'Your product', 'cartmeter' ), 'progress' => __( 'Progress to free shipping', 'cartmeter' ), 'bothOff' => __( 'The top bar and the cart page bar are off.', 'cartmeter' ), 'emptyOff' => __( 'No bar while the cart is empty.', 'cartmeter' ), ), ); } /** * Renders the settings page. */ public static function render() { if ( ! current_user_can( self::capability() ) ) { return; } $s = Settings::get(); $tabs = self::tabs(); ?>

' . esc_html__( 'Pro', 'cartmeter' ) . ''; } ?>

$label ) : ?>
array( __( 'From WooCommerce', 'cartmeter' ), __( 'Recommended. Uses the minimum order amount of your Free shipping method in each shipping zone.', 'cartmeter' ) ), 'manual' => array( __( 'My own amount', 'cartmeter' ), __( 'For free shipping that comes from another plugin.', 'cartmeter' ) ), ) ); self::amount( 'goal_amount', __( 'Free shipping from', 'cartmeter' ), $s['goal_amount'], __( 'Your shipping settings must give free shipping at this amount too. The bar only shows it.', 'cartmeter' ), array( 'data-fspbar-show-when' => 'goal_mode=manual' ) ); self::card_end(); self::card_start( __( 'Free shipping in your shipping zones', 'cartmeter' ), '', array( 'data-fspbar-show-when' => 'goal_mode=auto' ) ); self::zones_table(); self::card_end(); self::card_start( __( 'Messages', 'cartmeter' ), __( '{goal} becomes the free shipping amount and {remaining} the amount still missing. Leave a box empty to use the text shown in it.', 'cartmeter' ) ); $texts = Settings::default_texts(); self::text( 'text_empty', __( 'Cart is empty', 'cartmeter' ), $s['text_empty'], $texts['text_empty'] ); self::text( 'text_progress', __( 'On the way', 'cartmeter' ), $s['text_progress'], $texts['text_progress'] ); self::text( 'text_done', __( 'Free shipping reached', 'cartmeter' ), $s['text_done'], $texts['text_done'] ); self::card_end(); } /** * The zones and the free shipping amount found in each. */ private static function zones_table() { $zones = Goal::zones_summary(); $found = array_filter( $zones, static function ( $zone ) { return null !== $zone['amount']; } ); $link = admin_url( 'admin.php?page=wc-settings&tab=shipping' ); if ( ! $found ) { echo '
'; echo '

' . esc_html__( 'No free shipping with a minimum amount yet.', 'cartmeter' ) . '

'; echo '

' . esc_html__( 'Open a shipping zone, add the "Free shipping" method and choose "A minimum order amount". The bar shows up as soon as it is saved.', 'cartmeter' ) . '

'; echo '

' . esc_html__( 'Open shipping settings', 'cartmeter' ) . '

'; echo '
'; return; } echo ''; foreach ( $zones as $zone ) { echo ''; } echo '
' . esc_html( $zone['name'] ) . ''; if ( null === $zone['amount'] ) { echo '' . esc_html__( 'No free shipping: the bar is hidden for shoppers here.', 'cartmeter' ) . ''; } else { /* translators: %s: amount. */ echo wp_kses_post( sprintf( __( 'Free over %s', 'cartmeter' ), '' . Frontend::price( (float) $zone['amount'] ) . '' ) ); $notes = array(); if ( 'either' === $zone['requires'] ) { $notes[] = __( 'or with a free shipping coupon', 'cartmeter' ); } elseif ( 'both' === $zone['requires'] ) { $notes[] = __( 'needs a free shipping coupon too: the bar shows once the coupon is used', 'cartmeter' ); } $notes[] = $zone['ignore_discounts'] ? __( 'counted before coupon discounts', 'cartmeter' ) : __( 'counted after coupon discounts', 'cartmeter' ); echo ' · ' . esc_html( implode( ' · ', $notes ) ) . ''; } echo '
'; echo '

' . esc_html__( 'Change in WooCommerce shipping settings', 'cartmeter' ) . '

'; } /** * Where tab. * * @param array $s Settings. */ private static function tab_where( array $s ) { self::card_start( __( 'Top of every page', 'cartmeter' ), __( 'A calm strip above your header.', 'cartmeter' ) ); self::toggle( 'place_top', __( 'Show the top bar', 'cartmeter' ), $s['place_top'] ); self::toggle( 'top_empty', __( 'Also when the cart is empty', 'cartmeter' ), $s['top_empty'], __( 'New visitors see "Free shipping on orders over …".', 'cartmeter' ), array( 'data-fspbar-show-when' => 'place_top=1' ) ); self::card_end(); self::card_start( __( 'Shop pages', 'cartmeter' ), __( 'One bar per page: where a page has its own bar, the top bar is not shown there.', 'cartmeter' ) ); self::toggle( 'place_cart', __( 'Cart page', 'cartmeter' ), $s['place_cart'], __( 'Above the products in the cart.', 'cartmeter' ) ); self::toggle( 'place_checkout', __( 'Checkout', 'cartmeter' ), $s['place_checkout'], __( 'At the top of the checkout. Changes when the shopper picks another country.', 'cartmeter' ) ); self::toggle( 'place_mini', __( 'Mini cart', 'cartmeter' ), $s['place_mini'], __( 'Inside the cart that opens from your header.', 'cartmeter' ) ); self::toggle( 'place_product', __( 'Product pages', 'cartmeter' ), $s['place_product'], __( 'Under the Add to cart button.', 'cartmeter' ) ); self::card_end(); self::card_start( __( 'Anywhere else', 'cartmeter' ) ); echo '

' . esc_html__( 'Put this shortcode in any page, post or widget:', 'cartmeter' ) . '

'; echo '

[fspbar]

'; self::card_end(); } /** * Style tab. * * @param array $s Settings. */ private static function tab_style( array $s ) { self::card_start( __( 'Look', 'cartmeter' ) ); self::choices( 'style', __( 'Bar', 'cartmeter' ), $s['style'], array( 'light' => array( __( 'Light', 'cartmeter' ), __( 'Soft background, dark text.', 'cartmeter' ) ), 'dark' => array( __( 'Dark', 'cartmeter' ), __( 'Dark background, light text.', 'cartmeter' ) ), 'color' => array( __( 'Colored', 'cartmeter' ), __( 'Your bar color as the background.', 'cartmeter' ) ), ) ); self::row_start( 'color', __( 'Bar color', 'cartmeter' ) ); echo ''; self::row_end( __( 'The color of the filling line. Leave empty for green. Text colors are picked for you so they are easy to read.', 'cartmeter' ) ); self::card_end(); } /** * Help tab. * * @param array $s Settings. */ private static function tab_help( array $s ) { self::card_start( __( 'How it works', 'cartmeter' ) ); ?>
' . esc_html__( 'Shoppers in a zone without free shipping, and carts with only digital products, see no bar.', 'cartmeter' ) . '

'; echo '

' . esc_html__( 'Works with classic themes and block themes, the Cart and Checkout blocks, and page caching.', 'cartmeter' ) . '

'; self::card_end(); self::card_start( __( 'Need help?', 'cartmeter' ) ); echo '

'; printf( /* translators: %s: support email link. */ esc_html__( 'Email us at %s and we will reply as soon as we can.', 'cartmeter' ), 'pluginnest1@gmail.com' ); echo '

' . esc_html__( 'Like the plugin? Please leave a review.', 'cartmeter' ) . '

'; self::card_end(); More_Plugins::hire_box(); self::card_start( __( 'When deleting the plugin', 'cartmeter' ) ); self::toggle( 'delete_data', __( 'Remove settings', 'cartmeter' ), $s['delete_data'], __( 'Delete this plugin\'s settings when the plugin is deleted.', 'cartmeter' ) ); self::card_end(); } /** * More plugins tab. * * @param array $s Settings. */ private static function tab_more( array $s ) { unset( $s ); More_Plugins::render(); } /** * Pro tab (shown while Pro is not installed). * * @param array $s Settings. */ private static function tab_pro( array $s ) { unset( $s ); $features = array( array( __( 'Several goals on one bar', 'cartmeter' ), __( 'For example free shipping, then a free gift, then 10% off. Each goal is a marker on the bar.', 'cartmeter' ) ), array( __( 'Free gift', 'cartmeter' ), __( 'A product of your choice goes into the cart for free when the goal is reached.', 'cartmeter' ) ), array( __( 'Discount reward', 'cartmeter' ), __( 'A percent or fixed discount when the cart reaches an amount.', 'cartmeter' ) ), array( __( 'Free shipping without setup', 'cartmeter' ), __( 'Pro can make shipping free at the goal, even without a WooCommerce Free shipping method.', 'cartmeter' ) ), array( __( 'Countdown', 'cartmeter' ), __( 'Start and end dates for an offer, with "Ends in 1d 04:12" on the bar.', 'cartmeter' ) ), array( __( 'Full design', 'cartmeter' ), __( 'Every color, size, corners, icon and soft stripes. Keep the top bar on screen while scrolling.', 'cartmeter' ) ), array( __( 'Report', 'cartmeter' ), __( 'Orders that reached each goal, gifts and discounts given, and your average order value.', 'cartmeter' ) ), ); ?>

value. * @return string */ public static function attrs( array $attrs ) { $html = ''; foreach ( $attrs as $name => $value ) { $html .= ' ' . esc_attr( $name ) . '="' . esc_attr( (string) $value ) . '"'; } return $html; } /** * Prints HTML attributes, each one escaped. * * @param array $attrs Name => value. */ public static function print_attrs( array $attrs ) { foreach ( $attrs as $name => $value ) { echo ' ' . esc_attr( $name ) . '="' . esc_attr( (string) $value ) . '"'; } } /** * Name attribute for a setting. * * @param string $key Setting key. * @param string $group Form group (add-ons use their own). * @return string */ public static function name( $key, $group = 'fspbar' ) { return $group . '[' . $key . ']'; } /** * Opens a card. * * @param string $title Title. * @param string $text Optional intro. * @param array $attrs Extra attributes. */ public static function card_start( $title, $text = '', array $attrs = array() ) { echo '
'; echo '

' . esc_html( $title ) . '

'; if ( $text ) { echo '

' . esc_html( $text ) . '

'; } } /** * Closes a card. */ public static function card_end() { echo '
'; } /** * Row wrapper start. * * @param string $key Setting key. * @param string $label Label. * @param array $attrs Row attributes. * @param bool $label_for Whether the label points at an input with the key's ID. */ public static function row_start( $key, $label, array $attrs = array(), $label_for = true ) { echo '
'; if ( $label_for ) { echo ''; } else { echo '
' . esc_html( $label ) . '
'; } echo '
'; } /** * Row wrapper end with an optional description. * * @param string $desc Description. */ public static function row_end( $desc = '' ) { if ( $desc ) { echo '

' . esc_html( $desc ) . '

'; } echo '
'; } /** * On/off switch. * * @param string $key Setting key. * @param string $label Label. * @param bool $value Value. * @param string $desc Description. * @param array $attrs Row attributes. * @param string $group Form group. */ public static function toggle( $key, $label, $value, $desc = '', array $attrs = array(), $group = 'fspbar' ) { self::row_start( $key, $label, $attrs ); echo ''; echo ''; self::row_end( $desc ); } /** * Text input. * * @param string $key Setting key. * @param string $label Label. * @param string $value Value. * @param string $placeholder Placeholder (usually the default text). * @param string $desc Description. * @param array $attrs Row attributes. * @param string $group Form group. */ public static function text( $key, $label, $value, $placeholder = '', $desc = '', array $attrs = array(), $group = 'fspbar' ) { self::row_start( $key, $label, $attrs ); echo ''; self::row_end( $desc ); } /** * Money amount input with the shop's currency symbol. * * @param string $key Setting key. * @param string $label Label. * @param string $value Value (plain decimal). * @param string $desc Description. * @param array $attrs Row attributes. * @param string $group Form group. */ public static function amount( $key, $label, $value, $desc = '', array $attrs = array(), $group = 'fspbar' ) { self::row_start( $key, $label, $attrs ); echo '' . wp_kses_post( get_woocommerce_currency_symbol() ) . ''; echo ''; self::row_end( $desc ); } /** * Number input. * * @param string $key Setting key. * @param string $label Label. * @param int $value Value. * @param int $min Minimum. * @param int $max Maximum. * @param string $desc Description. * @param array $attrs Row attributes. * @param string $group Form group. */ public static function number( $key, $label, $value, $min, $max, $desc = '', array $attrs = array(), $group = 'fspbar' ) { self::row_start( $key, $label, $attrs ); echo ''; self::row_end( $desc ); } /** * Dropdown. * * @param string $key Setting key. * @param string $label Label. * @param string $value Value. * @param array $options Value => label. * @param string $desc Description. * @param array $attrs Row attributes. * @param string $group Form group. */ public static function select( $key, $label, $value, array $options, $desc = '', array $attrs = array(), $group = 'fspbar' ) { self::row_start( $key, $label, $attrs ); echo ''; self::row_end( $desc ); } /** * Choice cards (radio buttons). * * @param string $key Setting key. * @param string $label Label. * @param string $value Value. * @param array $options Value => array( title, description ). * @param array $attrs Row attributes. * @param string $group Form group. */ public static function choices( $key, $label, $value, array $options, array $attrs = array(), $group = 'fspbar' ) { self::row_start( $key, $label, $attrs, false ); echo '
'; foreach ( $options as $option => $texts ) { $id = 'fspbar-' . $key . '-' . $option; echo ''; } echo '
'; self::row_end(); } }