LLMS_Frontend_Assets

LLMS_Frontend_Assets


Source Source

File: includes/class.llms.frontend.assets.php

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
class LLMS_Frontend_Assets {
 
    /**
     * Inline script ids that have been enqueued.
     *
     * @var  array
     */
    private static $enqueued_inline_scripts = array();
 
    /**
     * Array of inline scripts to be output in the header / footer respectively.
     *
     * @var  array
     */
    private static $inline_scripts = array(
        'header' => array(),
        'footer' => array(),
    );
 
    /**
     * Initializer
     *
     * Replaces non-static __construct() from 3.4.0 & lower.
     *
     * @since 3.4.1
     * @since 3.17.5 Unknown.
     * @since 5.6.0 Add content protection inline script enqueue.
     *
     * @return void
     */
    public static function init() {
 
        add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_styles' ) );
        add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) );
        add_action( 'wp_head', array( __CLASS__, 'output_header_scripts' ) );
        add_action( 'wp_print_footer_scripts', array( __CLASS__, 'output_footer_scripts' ), 1 );
        add_action( 'wp', array( __CLASS__, 'enqueue_content_protection' ) );
 
    }
 
    /**
     * Enqueue inline copy prevention scripts.
     *
     * @since 5.6.0
     *
     * @return void
     */
    public static function enqueue_content_protection() {
 
        $allow_copying = ! llms_parse_bool( get_option( 'lifterlms_content_protection', 'no' ) ) || llms_can_user_bypass_restrictions( get_current_user_id() );
 
        /**
         * Filters whether or not content prevention is enabled.
         *
         * This hook runs on the `wp` action, at this point the current user is available and
         * the global `$post` has already been set up.
         *
         * @since 5.6.0
         *
         * @param boolean $allow_copying Whether or not copying is allowed. If `true`, copying
         *                               is allowed, otherwise copy prevention scripts are
         *                               loaded.
         */
        $allow_copying = apply_filters( 'llms_skip_content_prevention', $allow_copying );
 
        if ( $allow_copying ) {
            return;
        }
 
        ob_start();
        ?>
        ( function(){
            function dispatchEvent( type ) {
                document.dispatchEvent( new Event( type ) );
            }
            document.addEventListener( 'copy', function( event ) {
                event.preventDefault();
                event.clipboardData.setData( 'text/plain', '<?php echo __( 'Copying is not allowed.', 'lifterlms' ); ?>' );
                dispatchEvent( 'llms-copy-prevented' );
            }, false );
            document.addEventListener( 'contextmenu', function( event ) {
                if ( event.target && 'IMG' === event.target.nodeName ) {
                    event.preventDefault();
                    dispatchEvent( 'llms-context-prevented' );
                }
            }, false );
        } )();
        <?php
        $script = ob_get_clean();
        llms()->assets->enqueue_inline( 'llms-integrity', $script, 'header' );
 
    }
 
    /**
     * Enqueue Styles
     *
     * @since 1.0.0
     * @since 3.18.0 Unknown.
     * @since 3.35.0 Explicitly define asset versions.
     * @since 4.4.0 Enqueue & register scripts using `LLMS_Assets` methods.
     * @since 5.0.0 Enqueue select2 on account and checkout pages for searchable dropdowns for country & state.
     *
     * @return void
     */
    public static function enqueue_styles() {
 
        global $post_type;
 
        llms()->assets->register_style( 'llms-iziModal' );
 
        llms()->assets->enqueue_style( 'webui-popover' );
        llms()->assets->enqueue_style( 'lifterlms-styles' );
 
        if ( in_array( $post_type, array( 'llms_my_certificate', 'llms_certificate' ), true ) ) {
            llms()->assets->enqueue_style( 'certificates' );
        } elseif ( is_llms_account_page() ) {
            llms()->assets->enqueue_style( 'llms-iziModal' );
        } elseif ( is_singular( 'llms_quiz' ) ) {
            wp_enqueue_style( 'wp-mediaelement' );
        }
 
        if ( is_llms_account_page() || is_llms_checkout() ) {
            llms()->assets->enqueue_style( 'llms-select2-styles' );
        }
 
    }
 
    /**
     * Enqueue Scripts
     *
     * @since 1.0.0
     * @since 3.22.0 Unknown.
     * @since 3.35.0 Explicitly define asset versions.
     * @since 3.36.0 Localize tracking with client-side settings.
     * @since 4.0.0 Remove dependencies "collapse" and "transition".
     * @since 4.4.0 Enqueue & register scripts using `LLMS_Assets` methods.
     *              Add Add `window.llms.ajax_nonce` data to replace `wp_ajax_data.nonce`.
     *              Moved inline scripts to `enqueue_inline_scripts()`.
     * @since 5.0.0 Enqueue locale data and dependencies on account and checkout pages for searchable dropdowns for country & state.
     *               Remove password strength inline enqueue.
     * @since 7.5.0 Enqueue `llms-favorites` script on lesson and course page.
     *
     * @return void
     */
    public static function enqueue_scripts() {
 
        // I don't think we need these next 3 scripts.
        wp_enqueue_script( 'jquery-ui-tooltip' );
        wp_enqueue_script( 'jquery-ui-datepicker' );
        wp_enqueue_script( 'jquery-ui-slider' );
 
        llms()->assets->enqueue_script( 'webui-popover' );
 
        llms()->assets->register_script( 'llms-jquery-matchheight' );
        if ( is_llms_account_page() || is_course() || is_membership() || is_lesson() || is_memberships() || is_courses() || is_tax( array( 'course_cat', 'course_tag', 'course_difficulty', 'course_track', 'membership_tag', 'membership_cat' ) ) ) {
            llms()->assets->enqueue_script( 'llms-jquery-matchheight' );
        }
 
        llms()->assets->enqueue_script( 'llms' );
 
        llms()->assets->register_script( 'llms-notifications' );
        if ( get_current_user_id() ) {
            llms()->assets->enqueue_script( 'llms-notifications' );
        }
 
        // Doesn't seem like there's any reason to enqueue this script on the frontend.
        wp_enqueue_script( 'llms-ajax', LLMS_PLUGIN_URL . 'assets/js/llms-ajax' . LLMS_ASSETS_SUFFIX . '.js', array( 'jquery' ), llms()->version, true );
 
        // I think we only need this on account and checkout pages.
        llms()->assets->enqueue_script( 'llms-form-checkout' );
 
        if ( is_singular( 'llms_quiz' ) ) {
            llms()->assets->enqueue_script( 'llms-quiz' );
        }
 
        llms()->assets->register_script( 'llms-favorites' );
        if ( ( is_lesson() || is_course() ) && true === llms_is_favorites_enabled() ) {
            llms()->assets->enqueue_script( 'llms-favorites' );
        }
 
        llms()->assets->register_script( 'llms-iziModal' );
        if ( is_llms_account_page() ) {
            llms()->assets->enqueue_script( 'llms-iziModal' );
        }
 
        self::enqueue_inline_scripts();
        self::enqueue_locale_scripts();
 
    }
 
    /**
     * Enqueue inline scripts.
     *
     * @since 4.4.0
     * @since 7.0.0 Include checkout page script data for AJAX-powered gateways.
     *
     * @return void
     */
    protected static function enqueue_inline_scripts() {
 
        // Ensure the main llms object exists.
        llms()->assets->enqueue_inline( 'llms-obj', 'window.llms = window.llms || {};', 'footer', 5 );
 
        // Define inline scripts.
        $scripts = array(
            'llms-ajaxurl'           => 'window.llms.ajaxurl = "' . admin_url( 'admin-ajax.php', is_ssl() ? 'https' : 'http' ) . '";',
            'llms-ajax-nonce'        => 'window.llms.ajax_nonce = "' . wp_create_nonce( LLMS_AJAX::NONCE ) . '";',
            'llms-tracking-settings' => "window.llms.tracking = '" . wp_json_encode( llms()->events()->get_client_settings() ) . "';",
            'llms-LLMS-obj'          => 'window.LLMS = window.LLMS || {};',
            'llms-l10n'              => 'window.LLMS.l10n = window.LLMS.l10n || {}; window.LLMS.l10n.strings = ' . LLMS_L10n::get_js_strings( true ) . ';',
        );
 
        $checkout_urls = self::get_checkout_urls();
        if ( ! empty( $checkout_urls ) ) {
            $scripts['llms-checkout-urls'] = "window.llms.checkoutUrls = JSON.parse( '" . wp_json_encode( $checkout_urls ) . "' );";
        }
 
        // Enqueue them.
        foreach ( $scripts as $handle => $script ) {
            llms()->assets->enqueue_inline( $handle, $script, 'footer' );
        }
 
    }
 
    /**
     * Enqueue dependencies and inline script data for form localization
     *
     * @since 5.0.0
     *
     * @return void
     */
    protected static function enqueue_locale_scripts() {
 
        if ( is_llms_account_page() || is_llms_checkout() ) {
            llms()->assets->enqueue_script( 'llms-select2' );
            llms()->assets->enqueue_inline(
                'llms-countries-locale',
                "window.llms.address_info = '" . wp_json_encode( llms_get_countries_address_info() ) . "';",
                'footer',
                20
            );
        }
 
    }
 
    /**
     * Retrieves AJAX checkout URLs used for checkout and switching payment source on the student dashboard.
     *
     * @since 7.0.0
     *
     * @return array
     */
    private static function get_checkout_urls() {
        $urls       = array();
        $controller = LLMS_Controller_Checkout::instance();
 
        if ( is_llms_checkout() ) {
            $urls = array(
                'createPendingOrder'  => $controller->get_url( $controller::ACTION_CREATE_PENDING_ORDER ),
                'confirmPendingOrder' => $controller->get_url( $controller::ACTION_CONFIRM_PENDING_ORDER ),
            );
        } elseif ( is_llms_account_page() && 'orders' === LLMS_Student_Dashboard::get_current_tab( 'slug' ) && is_numeric( get_query_var( 'orders', false ) ) ) {
            $urls = array(
                'switchPaymentSource' => $controller->get_url( $controller::ACTION_SWITCH_PAYMENT_SOURCE ),
            );
        }
 
        return $urls;
 
    }
 
    /**
     * Output inline scripts in the footer
     *
     * @since 3.4.1
     * @since 4.4.0 Use `LLMS_Assets::output_inline()` to output scripts.
     *
     * @return void
     */
    public static function output_footer_scripts() {
        llms()->assets->output_inline( 'footer' );
    }
 
    /**
     * Output inline scripts in the header
     *
     * @since 3.4.1
     * @since 4.4.0 Use `LLMS_Assets::output_inline()` to output scripts.
     *
     * @return void

Top ↑

Methods Methods


Top ↑

Changelog Changelog

Changelog
Version Description
6.0.0 Removed deprecated items.
  • LLMS_Frontend_Assets::enqueue_inline_pw_script() method
  • LLMS_Frontend_Assets::enqueue_inline_script() method
  • LLMS_Frontend_Assets::is_inline_script_enqueued() method
4.4.0 Method enqueue_inline_script() is deprecated in favor of LLMS_Assets::enqueue_inline(). Method is_inline_script_enqueued() is deprecated in favor of LLMS_Frontend_Assets::is_inline_enqueued(). Private properties $enqueued_inline_scripts and $inline_scripts have been removed. Removed private methods get_inline_scripts() and output_inline_scripts().
4.0.0 Remove JS dependencies "collapse" and "transition".
3.36.0 Localize tracking with client-side settings.
3.35.0 Explicitly define asset versions.
1.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.