Inpsyde\MultilingualPress\Asset\AssetManager::enqueue_script PHP Method

enqueue_script() public method

Enqueues the script with the given handle.
Since: 3.0.0
public enqueue_script ( Inpsyde\MultilingualPress\Asset\Script | string $script, boolean $in_footer = true ) : boolean
$script Inpsyde\MultilingualPress\Asset\Script | string Script object or handle.
$in_footer boolean Optional. Enqueue in the footer? Defaults to true.
return boolean Whether or not the script was enqueued successfully.
    public function enqueue_script($script, $in_footer = true)
    {
        $handle = (string) $script;
        if (empty($this->scripts[$handle])) {
            return false;
        }
        $script = $this->scripts[$handle];
        if (wp_script_is($handle)) {
            $this->handle_script_data($script);
            return true;
        }
        $this->enqueue(function () use($handle, $script, $in_footer) {
            wp_enqueue_script($handle, $script->url(), $script->dependencies(), $script->version(), (bool) $in_footer);
            $this->handle_script_data($script);
        });
        return true;
    }

Usage Example

    /**
     * Returns the remote post links in form of up to three link elements, or a select element for more than three
     * links.
     *
     * @param  string $selections 'option' or 'a' elements.
     * @param  string $type       'links' or 'form'.
     * @param  array  $translated Original array of translated posts, passed to the filter.
     * @param  string $position   Quicklink position.
     *
     * @return string
     */
    protected function get_html_container($selections, $type, $translated, $position)
    {
        $class_inner = 'mlp_inner';
        $label_text = esc_html_x('Read in:', 'Quicklink label', 'multilingual-press');
        if ('links' === $type) {
            $html = <<<HTML
<div class="mlp-quicklinks mlp-quicklink-links {$position} mlp_quicklinks mlp_quicklinks_links">
\t<div class="{$class_inner}">
\t\t{$label_text}<br>
\t\t{$selections}
\t</div>
</div>
HTML;
        } else {
            $home_url = home_url();
            $home_url = esc_attr($home_url);
            $select_id = 'mlp-quicklink-select';
            $select_name = 'mlp_quicklink_select';
            $submit_text = esc_attr_x('Go', 'quicklink submit button', 'multilingual-press');
            $html = <<<HTML
<form action="{$home_url}" method="post" class="mlp-quicklinks mlp-quicklink-form {$position} mlp_quicklinks mlp_quicklinks_form">
\t<div class="{$class_inner}">
\t\t<label for="{$select_id}">
\t\t\t{$label_text}
\t\t\t<select name="{$select_name}" id="{$select_id}" autocomplete="off">
\t\t\t\t{$selections}
\t\t\t</select>
\t\t</label>
\t\t<input type="submit" value="{$submit_text}">
\t</div>
</form>
HTML;
            $this->asset_manager->enqueue_script('multilingualpress');
        }
        /**
         * Filters the quicklinks HTML.
         *
         * @param string $html       HTML output.
         * @param string $type       Quicklink type, 'links' or 'form'.
         * @param array  $translated Array of translated posts.
         * @param string $selections Selections, 'option' or 'a' elements.
         * @param string $position   Quicklink position.
         */
        return (string) apply_filters('mlp_quicklinks_html', $html, $type, $translated, $selections, $position);
    }
All Usage Examples Of Inpsyde\MultilingualPress\Asset\AssetManager::enqueue_script