| @@ -2,9 +2,9 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * @package VikWP - Libraries |
| 4 | 4 | * @subpackage adapter.application |
| 5 | 5 | * @author E4J s.r.l. |
| 6 | - * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. | |
| 6 | + * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. | |
| 7 | 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | 8 | * @link https://vikwp.com |
| 9 | 9 | */ |
| 10 | 10 | |
| @@ -50,8 +50,17 @@ | ||
| 50 | 50 | */ |
| 51 | 51 | protected $ajaxScripts = []; |
| 52 | 52 | |
| 53 | 53 | /** |
| 54 | + * Adds the possibility to override the callback used to register | |
| 55 | + * something within the head. | |
| 56 | + * | |
| 57 | + * @param callable | |
| 58 | + * @since 10.1.51 | |
| 59 | + */ | |
| 60 | + public $attachToHeadCustomCallback = null; | |
| 61 | + | |
| 62 | + /** | |
| 54 | 63 | * Sets a meta tag in the front-end only. |
| 55 | 64 | * |
| 56 | 65 | * @param string $name Name of the meta HTML tag. |
| 57 | 66 | * @param mixed $content Value of the meta HTML tag as array or string. |
| @@ -57,28 +66,69 @@ | ||
| 57 | 66 | * @param mixed $content Value of the meta HTML tag as array or string. |
| 58 | 67 | * @param string $attribute Attribute to use in the meta HTML tag. |
| 59 | 68 | * |
| 60 | 69 | * @return self This object to support chaining. |
| 70 | + * | |
| 71 | + * @since 10.1.66 Robots are now treated differently. | |
| 61 | 72 | * |
| 62 | 73 | * @uses attachToHead() |
| 63 | 74 | */ |
| 64 | 75 | public function setMetaData($name, $content, $attribute = 'name') |
| 65 | 76 | { |
| 66 | - $attribute = empty($attribute) || !is_string($attribute) ? 'name' : $attribute; | |
| 77 | + if ($name === 'robots') | |
| 78 | + { | |
| 79 | + add_filter('wp_robots', function($robots) use ($content) { | |
| 80 | + if (!$content) | |
| 81 | + { | |
| 82 | + return $robots; | |
| 83 | + } | |
| 67 | 84 | |
| 68 | - if (is_scalar($content)) | |
| 85 | + $robots = []; | |
| 86 | + | |
| 87 | + // obtain all the directives from the content (separated by a comma) | |
| 88 | + $directives = preg_split("/\s*,\s*/", $content); | |
| 89 | + | |
| 90 | + foreach ($directives as $directive) | |
| 91 | + { | |
| 92 | + // obtain key-val pairs (separated by a colon) | |
| 93 | + $directive = preg_split("/\s*:\s*/", $directive); | |
| 94 | + | |
| 95 | + $key = strtolower(array_shift($directive)); | |
| 96 | + | |
| 97 | + if (count($directive) == 0) | |
| 98 | + { | |
| 99 | + // attribute only, use true as value | |
| 100 | + $val = true; | |
| 101 | + } | |
| 102 | + else | |
| 103 | + { | |
| 104 | + $val = implode(':', $directive); | |
| 105 | + } | |
| 106 | + | |
| 107 | + $robots[$key] = $val; | |
| 108 | + } | |
| 109 | + | |
| 110 | + return $robots; | |
| 111 | + }); | |
| 112 | + } | |
| 113 | + else | |
| 69 | 114 | { |
| 70 | - $content = array($content); | |
| 71 | - } | |
| 115 | + $attribute = empty($attribute) || !is_string($attribute) ? 'name' : $attribute; | |
| 72 | 116 | |
| 73 | - $this->attachToHead(function() use ($name, $content, $attribute) | |
| 74 | - { | |
| 75 | - foreach ($content as $cont) | |
| 117 | + if (is_scalar($content)) | |
| 76 | 118 | { |
| 77 | - echo "<meta {$attribute}=\"{$name}\" content=\"" . esc_attr($cont) . "\" />\n"; | |
| 119 | + $content = array($content); | |
| 78 | 120 | } |
| 79 | - }, true); | |
| 80 | 121 | |
| 122 | + $this->attachToHead(function() use ($name, $content, $attribute) | |
| 123 | + { | |
| 124 | + foreach ($content as $cont) | |
| 125 | + { | |
| 126 | + echo "<meta {$attribute}=\"{$name}\" content=\"" . esc_attr($cont) . "\" />\n"; | |
| 127 | + } | |
| 128 | + }, true); | |
| 129 | + } | |
| 130 | + | |
| 81 | 131 | $this->data[$name] = $content; |
| 82 | 132 | |
| 83 | 133 | return $this; |
| 84 | 134 | } |
| @@ -406,8 +456,19 @@ | ||
| 406 | 456 | |
| 407 | 457 | // make sure we are in the front-end or the back-end is allowed |
| 408 | 458 | if ($app->isSite() || !$frontOnly) |
| 409 | 459 | { |
| 460 | + /** | |
| 461 | + * If provided, use the custom callback instead. | |
| 462 | + * | |
| 463 | + * @since 10.1.51 | |
| 464 | + */ | |
| 465 | + if (is_callable($this->attachToHeadCustomCallback)) | |
| 466 | + { | |
| 467 | + // invoke callback and teminate | |
| 468 | + return call_user_func_array($this->attachToHeadCustomCallback, [$callback]); | |
| 469 | + } | |
| 470 | + | |
| 410 | 471 | // make sure we are not doing an AJAX call |
| 411 | 472 | if (!wp_doing_ajax()) |
| 412 | 473 | { |
| 413 | 474 | $head_hook = $app->isAdmin() ? 'admin_head' : 'wp_head'; |
| @@ -533,14 +594,14 @@ | ||
| 533 | 594 | public function getCharset() |
| 534 | 595 | { |
| 535 | 596 | $output = get_option('blog_charset'); |
| 536 | 597 | |
| 537 | - if (!$output) | |
| 538 | - { | |
| 539 | - $output = 'UTF-8'; | |
| 540 | - } | |
| 598 | + if (!$output) | |
| 599 | + { | |
| 600 | + $output = 'UTF-8'; | |
| 601 | + } | |
| 541 | 602 | |
| 542 | - return $output; | |
| 603 | + return $output; | |
| 543 | 604 | } |
| 544 | 605 | |
| 545 | 606 | /** |
| 546 | 607 | * Returns the document language. |
| @@ -564,14 +625,41 @@ | ||
| 564 | 625 | public function getDirection() |
| 565 | 626 | { |
| 566 | 627 | if (function_exists('is_rtl')) |
| 567 | 628 | { |
| 568 | - $output = is_rtl() ? 'rtl' : 'ltr'; | |
| 569 | - } | |
| 570 | - else | |
| 571 | - { | |
| 572 | - $output = 'ltr'; | |
| 573 | - } | |
| 629 | + $output = is_rtl() ? 'rtl' : 'ltr'; | |
| 630 | + } | |
| 631 | + else | |
| 632 | + { | |
| 633 | + $output = 'ltr'; | |
| 634 | + } | |
| 574 | 635 | |
| 575 | - return $output; | |
| 636 | + return $output; | |
| 637 | + } | |
| 638 | + | |
| 639 | + /** | |
| 640 | + * Adds `<link>` tags to the head of the document. | |
| 641 | + * | |
| 642 | + * $relType defaults to 'rel' as it is the most common relation type used | |
| 643 | + * ('rev' refers to reverse relation, 'rel' indicates normal, forward relation). | |
| 644 | + * Typical tag: `<link href="index.php" rel="Start">`. | |
| 645 | + * | |
| 646 | + * @param string $href The link that is being related. | |
| 647 | + * @param string $relation Relation of link. | |
| 648 | + * @param string $relType Relation type attribute. Either rel or rev (default: 'rel'). | |
| 649 | + * @param array $attribs Associative array of remaining attributes. | |
| 650 | + * | |
| 651 | + * @return self This object to support chaining. | |
| 652 | + * | |
| 653 | + * @since 10.1.48 | |
| 654 | + */ | |
| 655 | + public function addHeadLink(string $href, string $relation, string $relType = 'rel', array $attribs = []) | |
| 656 | + { | |
| 657 | + $this->attachToHead(function() use ($href, $relation, $relType, $attribs) | |
| 658 | + { | |
| 659 | + JLoader::import('adapter.utilities.array'); | |
| 660 | + echo "<link href=\"{$href}\" {$relType}=\"$relation\" " . ArrayHelper::toString($attribs) . " />\n"; | |
| 661 | + }); | |
| 662 | + | |
| 663 | + return $this; | |
| 576 | 664 | } |
| 577 | 665 | } |