| @@ -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'; |