attachToHead(function() use ($name, $content, $attribute) { foreach ($content as $cont) { echo "\n"; } }, true); } $this->data[$name] = $content; return $this; } /** * Gets a meta tag. * Since Wordpress doesn't own a system to handle meta data, * we can only return a cached version of the data set using this class. * * @param string $name Name of the meta HTML tag. * * @return string */ public function getMetaData($name) { if (isset($this->data[$name])) { return $this->data[$name]; } return ''; } /** * Sets the title of the document in the front-end only. * * @param string $title The title to be set. * * @return self This object to support chaining. * * @link https://developer.wordpress.org/reference/hooks/wp_title/ (Wordpress < 4.4) * @link https://developer.wordpress.org/reference/functions/wp_get_document_title/ (Wordpress 4.4+) */ public function setTitle($title) { /** * We should use the hook 'pre_get_document_title' (available from WP >= v4.4), * instead of 'wp_title', or the title won't actually be set. * * @since 10.1.23 */ add_filter('pre_get_document_title', function() use ($title) { return $title; }); return $this; } /** * Return the title of the document. * * @return string * * @link https://developer.wordpress.org/reference/hooks/wp_title/ (Wordpress < 4.4) * @link https://developer.wordpress.org/reference/functions/wp_get_document_title/ (Wordpress 4.4+) */ public function getTitle() { global $wp_version; if (version_compare($wp_version, '4.4', '>=')) { return wp_get_document_title(); } return wp_title('»', false); } /** * Sets the description of the document. * * @param string $desc The description to be set. * * @return self This object to support chaining. * * @uses setMetaData() */ public function setDescription($desc) { return $this->setMetaData('description', (string) $desc); } /** * Return the description of the document. * * @return string * * @uses getMetaData() */ public function getDescription() { return $this->getMetaData('description'); } /** * Adds a linked script to the page. * * @param string $url URL to the linked script. * @param array $options Array of options. Example: array('version' => 'auto', 'conditional' => 'lt IE 9') * @param array $attribs Array of attributes. Example: array('id' => 'scriptid', 'async' => 'async', 'data-test' => 1) * * @return self This object to support chaining. * * @link https://developer.wordpress.org/reference/functions/wp_register_script/ */ public function addScript($url, $options = [], $attribs = []) { // check if the script should be loaded using WP native libs $version = array_key_exists('version', $options) ? $options['version'] : null; $footer = array_key_exists('footer', $options) ? (bool) $options['footer'] : false; $id = empty($attribs['id']) ? md5($url) : $attribs['id']; /** * Use filter to approve/deny the loading of the given script. * * @param boolean $load The recursive filtered value (default 'true'). * @param string $url The resource URL. * @param string $id The script ID attribute. * @param string $version The script version, if specified. * @param boolean $footer True whether the script is going to be loaded in the footer. * * @return boolean True to load the resource, false to ignore it. * * @since 10.1.25 */ $load = apply_filters('vik_before_include_script', true, $url, $id, $version, $footer); if ($load) { // make sure this is not an AJAX call if (!wp_doing_ajax()) { // if the headers have been sent, the script must be registered in the footer. if (headers_sent()) { $footer = true; } // the default array of dependencies $deps = [ 'jquery-core', 'jquery-ui-core', ]; /** * Added support to custom dependencies. * * @since 10.1.38 */ if (!empty($options['dependencies'])) { // join default dependencies with the given ones $deps = array_merge($deps, (array) $options['dependencies']); // get rid of duplicates $deps = array_values(array_unique($deps)); } // loads scripts always after jQuery Core (included by Wordpress) wp_register_script($id, $url, $deps, $version, $footer); wp_enqueue_script($id); } // since the footer is already printed, we need to each our script directly else if ($url) { if ($version) { $url .= '?ver=' . $version; } echo ''; } } return $this; } /** * Adds a script to the page. * * @param string $content Script snippet. * @param string $type Scripting mime (defaults to 'text/javascript'). * * @return self This object to support chaining. * * @uses attachToHead() */ public function addScriptDeclaration($content, $type = 'text/javascript') { /** * Always use the script declaration without checking * if it has been already loaded. * * @since 10.1.17 */ $this->attachToHead(function() use ($content, $type) { /** * Use "joomla-options new" class in case we are attaching a JSON string. * The "new" word means that the options has to be loaded. * * @since 10.1.14 */ $class = $type == 'application/json' ? ' class="joomla-options new"' : ''; echo "\n"; }); return $this; } /** * Adds a linked stylesheet to the page. * * @param string $url URL to the linked style sheet. * @param array $options Array of options. Example: array('version' => 'auto', 'conditional' => 'lt IE 9') * @param array $attribs Array of attributes. Example: array('id' => 'stylesheet', 'data-test' => 1) * * @return self This object to support chaining. * * @link https://codex.wordpress.org/Function_Reference/wp_register_style */ public function addStyleSheet($url, $options = [], $attribs = []) { $version = array_key_exists('version', $options) ? $options['version'] : null; $media = array_key_exists('media', $options) ? (string) $options['media'] : 'all'; $id = empty($attribs['id']) ? md5($url) : $attribs['id']; /** * Use filter to approve/deny the loading of the given stylesheet. * * @param boolean $load The recursive filtered value (default 'true'). * @param string $url The resource URL. * @param string $id The stylesheet ID attribute. * @param string $version The stylesheet version, if specified. * * @return boolean True to load the resource, false to ignore it. * * @since 10.1.25 */ $load = apply_filters('vik_before_include_style', true, $url, $id, $version); if ($load) { // attach the style to the only if the headers haven't been sent // and if we are not doing an AJAX call if (!headers_sent() && !wp_doing_ajax()) { $deps = []; /** * Added support to custom dependencies. * * @since 10.1.38 */ if (!empty($options['dependencies'])) { // join default dependencies with the given ones $deps = array_merge($deps, (array) $options['dependencies']); // get rid of duplicates $deps = array_values(array_unique($deps)); } wp_register_style($id, $url, $deps, $version, $media); wp_enqueue_style($id); } // otherwise print the style in the document else { if ($version) { $url .= '?ver=' . $version; } echo ''; } } return $this; } /** * Adds a stylesheet declaration to the page. * * @param string $content Style declaration. * @param string $type Type of stylesheet (defaults to 'text/css'). * * @return self This object to support chaining. * * @uses attachToHead() */ public function addStyleDeclaration($content, $type = 'text/css') { if (!in_array($content, $this->styleDeclarations)) { $this->attachToHead(function() use ($content, $type) { echo "\n"; }); $this->styleDeclarations[] = $content; } return $this; } /** * Internal method execute a callback within the tags. * The callback will be attached to the wp_head or admin_head hooks * depending on the section we are currently using. * * @param mixed $callback The callback or the function name to attach. * @param boolean $frontOnly True to attach the method only in the front-end. * * @return void * * @link https://codex.wordpress.org/Plugin_API/Action_Reference/wp_head * @link https://codex.wordpress.org/Plugin_API/Action_Reference/admin_head * @link https://developer.wordpress.org/reference/hooks/wp_print_footer_scripts/ * @link https://developer.wordpress.org/reference/hooks/admin_print_footer_scripts/ */ protected function attachToHead($callback, $frontOnly = false) { $app = JFactory::getApplication(); // make sure we are in the front-end or the back-end is allowed if ($app->isSite() || !$frontOnly) { /** * If provided, use the custom callback instead. * * @since 10.1.51 */ if (is_callable($this->attachToHeadCustomCallback)) { // invoke callback and teminate return call_user_func_array($this->attachToHeadCustomCallback, [$callback]); } // make sure we are not doing an AJAX call if (!wp_doing_ajax()) { $head_hook = $app->isAdmin() ? 'admin_head' : 'wp_head'; // make sure that the head hook that we are using haven't been called yet, // otherwise our script will never be executed if (!headers_sent() && !did_action($head_hook)) { // admin_head hook for the back-end // wp_head hook for the front-end add_action($head_hook, $callback); } else { // admin_print_footer_scripts hook for the back-end