'', 'enabled' => true, 'hide_on' => '', ) ); } /* ------------------------------------------------------------------ */ /* Front end */ /* ------------------------------------------------------------------ */ /** * Whether the widget should load on the current request. */ function chatx_should_load() { $s = chatx_get_settings(); if ( ! $s['enabled'] || '' === $s['site_id'] ) { return false; } if ( '' !== $s['hide_on'] && is_singular() ) { $excluded = array_filter( array_map( 'absint', explode( ',', $s['hide_on'] ) ) ); if ( in_array( get_queried_object_id(), $excluded, true ) ) { return false; } } /** * Filters whether the ChatX widget loads on the current page. * * @param bool $load True to load the widget. */ return (bool) apply_filters( 'chatx_should_load', true ); } function chatx_enqueue_widget() { if ( ! chatx_should_load() ) { return; } wp_enqueue_script( 'chatx-widget', CHATX_WIDGET_URL, array(), CHATX_VERSION, array( 'in_footer' => true, 'strategy' => 'defer', ) ); } add_action( 'wp_enqueue_scripts', 'chatx_enqueue_widget' ); /** * The widget reads its site ID from the data-site attribute of its own script tag. */ function chatx_script_tag( $tag, $handle ) { if ( 'chatx-widget' !== $handle ) { return $tag; } $s = chatx_get_settings(); return str_replace( ' src=', ' data-site="' . esc_attr( $s['site_id'] ) . '" src=', $tag ); } add_filter( 'script_loader_tag', 'chatx_script_tag', 10, 2 ); /* ------------------------------------------------------------------ */ /* Admin */ /* ------------------------------------------------------------------ */ function chatx_admin_menu() { add_options_page( __( 'ChatX', 'chatx' ), __( 'ChatX', 'chatx' ), 'manage_options', 'chatx', 'chatx_render_settings_page' ); } add_action( 'admin_menu', 'chatx_admin_menu' ); function chatx_register_settings() { register_setting( 'chatx', CHATX_OPTION, array( 'type' => 'array', 'sanitize_callback' => 'chatx_sanitize_settings', 'default' => array(), ) ); } add_action( 'admin_init', 'chatx_register_settings' ); function chatx_sanitize_settings( $input ) { $input = is_array( $input ) ? $input : array(); // Site IDs are short slugs such as "chatX" or "my-shop_2". $site_id = isset( $input['site_id'] ) ? sanitize_text_field( wp_unslash( $input['site_id'] ) ) : ''; $site_id = preg_replace( '/[^A-Za-z0-9_-]/', '', $site_id ); $exclude = isset( $input['hide_on'] ) ? sanitize_text_field( wp_unslash( $input['hide_on'] ) ) : ''; $exclude = implode( ',', array_unique( array_filter( array_map( 'absint', explode( ',', $exclude ) ) ) ) ); return array( 'site_id' => substr( $site_id, 0, 64 ), 'enabled' => ! empty( $input['enabled'] ), 'hide_on' => $exclude, ); } function chatx_render_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $s = chatx_get_settings(); ?>

chatx.ro' ); ?>

' . esc_html__( 'Settings', 'chatx' ) . '' ); return $links; } add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'chatx_action_links' ); /** * Reminder until a site ID is saved, shown only where it is actionable. */ function chatx_admin_notice() { $screen = get_current_screen(); if ( ! $screen || ! in_array( $screen->id, array( 'plugins', 'dashboard' ), true ) ) { return; } if ( ! current_user_can( 'manage_options' ) || '' !== chatx_get_settings()['site_id'] ) { return; } printf( '

%s %s

', esc_html__( 'ChatX is installed, but the chat will not appear until you enter your site ID.', 'chatx' ), esc_url( admin_url( 'options-general.php?page=chatx' ) ), esc_html__( 'Open settings', 'chatx' ) ); } add_action( 'admin_notices', 'chatx_admin_notice' );