Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 107
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
HelpCenter
0.00% covered (danger)
0.00%
0 / 107
0.00% covered (danger)
0.00%
0 / 7
342
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 load_script_translation_file
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 load_textdomains
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 initialize_rest
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 register_settings
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 newfold_help_center
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
20
 assets
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace NewfoldLabs\WP\Module\HelpCenter;
4
5use NewfoldLabs\WP\ModuleLoader\Container;
6use NewfoldLabs\WP\Module\HelpCenter\Data\Brands;
7
8use function NewfoldLabs\WP\ModuleLoader\container;
9
10/**
11 * The class to initialize and load the module.
12 */
13class HelpCenter {
14
15    /**
16     * Dependency injection container.
17     *
18     * @var Container
19     */
20    protected $container;
21
22    /**
23     * Identifier for script handle.
24     *
25     * @var string
26     */
27    public static $handle = 'nfd-help-center';
28
29    /**
30     * Text-domain
31     *
32     * @var string
33     */
34    public static $text_domain = 'wp-module-help-center';
35
36    /**
37     * Constructor.
38     *
39     * @param Container $container The container instance.
40     */
41    public function __construct( Container $container ) {
42        $this->container = $container;
43        add_action( 'init', array( $this, 'load_textdomains' ), 0 );
44        add_action( 'rest_api_init', array( $this, 'initialize_rest' ) );
45        add_action( 'admin_enqueue_scripts', array( $this, 'assets' ) );
46        add_action( 'admin_bar_menu', array( $this, 'newfold_help_center' ), 11 );
47        add_filter(
48            'load_script_translation_file',
49            array( $this, 'load_script_translation_file' ),
50            10,
51            3
52        );
53    }
54
55    /**
56     * Filters the file path for the JS translation JSON.
57     *
58     * If the script handle matches the module's handle, builds a custom path using
59     * the languages directory, current locale, text domain, and a hash of the script.
60     *
61     * @param string $file   Default translation file path.
62     * @param string $handle Script handle.
63     * @param string $domain Text domain.
64     * @return string Modified file path for the translation JSON.
65     */
66    public function load_script_translation_file( $file, $handle, $domain ) {
67
68        if ( $handle === self::$handle ) {
69            $path   = NFD_HELPCENTER_DIR . '/languages/';
70            $locale = determine_locale();
71
72            $file_base = 'default' === $domain
73                ? $locale
74                : $domain . '-' . $locale;
75            $file      = $path . $file_base . '-' . md5( 'build/index.js' )
76                        . '.json';
77
78        }
79
80        return $file;
81    }
82
83    /**
84     * Loads the textdomain for the module. This applies only to PHP strings.
85     */
86    public static function load_textdomains() {
87        $langdir = dirname( container()->plugin()->basename ) . '/vendor/newfold-labs/wp-module-help-center/languages';
88        \load_plugin_textdomain(
89            self::$text_domain,
90            false,
91            $langdir
92        );
93    }
94
95    /**
96     * Initializes REST API routes for the Help Center module.
97     *
98     * This method registers REST API routes by instantiating controller classes
99     * and calling their `register_routes()` methods.
100     */
101    public function initialize_rest() {
102        $controllers = array(
103            'NewfoldLabs\\WP\\Module\\HelpCenter\\UserInteractionController',
104            'NewfoldLabs\\WP\\Module\\HelpCenter\\CapabilityController',
105            'NewfoldLabs\\WP\\Module\\HelpCenter\\MultiSearchController',
106        );
107
108        foreach ( $controllers as $controller ) {
109            $instance = new $controller();
110            $instance->register_routes();
111        }
112        $this->register_settings();
113    }
114    /**
115     * Register the helpcenter settings in the WordPress options API for last three searches.
116     *
117     * @return void
118     */
119    public function register_settings() {
120        $option = array(
121            'type'         => 'string',
122            'description'  => __( 'NFD helpcenter data', 'wp-module-help-center' ),
123            'show_in_rest' => true,
124            'default'      => '',
125        );
126
127        \register_setting( 'general', 'nfd_helpcenter_data', $option );
128    }
129
130    /**
131     * Adds the Help Center icon to the WordPress admin bar.
132     *
133     * @param \WP_Admin_Bar $admin_bar The WordPress Admin Bar instance.
134     */
135    public function newfold_help_center( \WP_Admin_Bar $admin_bar ) {
136        if ( current_user_can( 'manage_options' ) && is_admin() ) {
137            $help_icon        =
138            '<svg width="24" height="24" viewBox="0 0 24 24" fill="#fff" xmlns="http://www.w3.org/2000/svg" style="margin-top: 5px;">
139<path d="M20.25 8.51104C21.1341 8.79549 21.75 9.6392 21.75 10.6082V14.8938C21.75 16.0304 20.9026 16.9943 19.7697 17.0867C19.4308 17.1144 19.0909 17.1386 18.75 17.1592V20.25L15.75 17.25C14.3963 17.25 13.0556 17.1948 11.7302 17.0866C11.4319 17.0623 11.1534 16.9775 10.9049 16.8451M20.25 8.51104C20.0986 8.46232 19.9393 8.43 19.7739 8.41628C18.4472 8.30616 17.1051 8.25 15.75 8.25C14.3948 8.25 13.0528 8.30616 11.7261 8.41627C10.595 8.51015 9.75 9.47323 9.75 10.6082V14.8937C9.75 15.731 10.2099 16.4746 10.9049 16.8451M20.25 8.51104V6.63731C20.25 5.01589 19.0983 3.61065 17.4903 3.40191C15.4478 3.13676 13.365 3 11.2503 3C9.13533 3 7.05233 3.13678 5.00963 3.40199C3.40173 3.61074 2.25 5.01598 2.25 6.63738V12.8626C2.25 14.484 3.40173 15.8893 5.00964 16.098C5.58661 16.1729 6.16679 16.2376 6.75 16.2918V21L10.9049 16.8451" stroke="#0F172A" stroke-width="1.2" stroke-linecap="round" stroke-linejoin="round"/>
140</svg>';
141            $help_center_menu = array(
142                'id'     => 'help-center',
143                'parent' => 'top-secondary',
144                'title'  => $help_icon,
145                'href'   => '',
146                'meta'   => array(
147                    'title'   => esc_attr__( 'Help', 'wp-module-help-center' ),
148                    'onclick' => 'newfoldEmbeddedHelp.toggleNFDLaunchedEmbeddedHelp()',
149                ),
150            );
151            $help_enabled     = $this->container->get( 'capabilities' )->get( 'canAccessHelpCenter' );
152            if ( $help_enabled ) {
153                $admin_bar->add_menu( $help_center_menu );
154                $menu_name = $this->container->plugin()->id . '-help-center';
155                $admin_bar->remove_menu( $menu_name );
156            }
157        }
158    }
159
160    /**
161     * Load WP dependencies into the page.
162     */
163    public function assets() {
164        $dir          = container()->plugin()->url . 'vendor/newfold-labs/wp-module-help-center/';
165        $asset_file   = NFD_HELPCENTER_BUILD_DIR . 'index.asset.php';
166        $help_enabled = $this->container->get( 'capabilities' )->get( 'canAccessHelpCenter' );
167        if ( file_exists( $asset_file ) && $help_enabled && current_user_can( 'manage_options' ) ) {
168            $asset = require_once $asset_file;
169
170            \wp_register_script(
171                self::$handle,
172                $dir . '/build/index.js',
173                array_merge( $asset['dependencies'], array( 'jquery', 'heartbeat' ) ),
174                $asset['version'],
175                true
176            );
177
178            \wp_set_script_translations(
179                self::$handle,
180                self::$text_domain,
181                $dir . '/languages'
182            );
183
184            if ( $help_enabled ) {
185                \wp_enqueue_script( self::$handle );
186
187                \wp_enqueue_style(
188                    self::$handle,
189                    $dir . '/build/index.css',
190                    array(),
191                    $asset['version'],
192                    'screen'
193                );
194
195                $brand_data = Brands::get_data_for_brand( NFD_HELPCENTER_PLUGIN_BRAND );
196                \wp_add_inline_script(
197                    self::$handle,
198                    'var nfdHelpCenter =' . wp_json_encode(
199                        array(
200                            'restUrl'                  => \get_home_url() . '/index.php?rest_route=',
201                            'resourceLink'             => Brands::get_resource_link_for_brand( NFD_HELPCENTER_PLUGIN_BRAND ),
202                            'brand'                    => NFD_HELPCENTER_PLUGIN_BRAND,
203                            'brandConfig'              => $brand_data,
204                            /* translators: 1: account name, 2: phone link, 3: chat link */
205                            'supportMessageTemplate'   => __( 'If you need help with your %1$s account, give us a call at %2$s or %3$s with one of our support agents — we\'re here for you!', 'wp-module-help-center' ),
206                            /* translators: 1: account name, 2: chat link */
207                            'supportMessageTemplateNoPhone' => __( 'If you need help with your %1$s account, %2$s with one of our support agents — we\'re here for you!', 'wp-module-help-center' ),
208                            /* translators: 1: phone link, 2: chat link */
209                            'noResultsSupportTemplate' => __( 'Call at %1$s or %2$s with one of our support agents — we will assist you as soon as possible.', 'wp-module-help-center' ),
210                            /* translators: 1: chat link */
211                            'noResultsSupportTemplateNoPhone' => __( 'Or %1$s with one of our support agents — we will assist you as soon as possible.', 'wp-module-help-center' ),
212                        )
213                    ) . ';',
214                    'before'
215                );
216
217                /*
218                 * Hide the helpcenter on onboarding flow.
219                 */
220                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading page query param for display logic, not form submission.
221                \wp_localize_script( self::$handle, 'newfoldHelpCenter', array( 'closeOnLoad' => ( isset( $_GET['page'] ) && 'nfd-onboarding' === sanitize_text_field( wp_unslash( $_GET['page'] ) ) ) ) );
222
223                /* Remove values on log out */
224                $logout_listener_js = <<<'JS'
225                jQuery(document).ready(function ($) {
226                    $('a[href*="wp-login.php?action=logout"]').on('click', function () {
227                        localStorage.removeItem('helpResultContent');
228                        localStorage.removeItem('searchInput');
229                        localStorage.removeItem('helpVisible');
230                    });
231                });
232                JS;
233
234                \wp_add_inline_script( self::$handle, $logout_listener_js );
235
236                /* Remove values when the user is logged out */
237                $session_expiration_js = <<<'JS'
238                jQuery(document).on('heartbeat-tick', function (event, data) {
239                    if (data.hasOwnProperty('wp-auth-check') && data['wp-auth-check'] === false) {
240                        localStorage.removeItem('helpResultContent');
241                        localStorage.removeItem('searchInput');
242                        localStorage.removeItem('helpVisible');
243                    }
244                });
245                JS;
246
247                \wp_add_inline_script( self::$handle, $session_expiration_js );
248            }
249        }
250    }
251}