Issues (257)

src/models/MetaTitleContainer.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS
4
 *
5
 * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
6
 * and flexible
7
 *
8
 * @link      https://nystudio107.com
9
 * @copyright Copyright (c) 2017 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\models;
13
14
use Craft;
15
use nystudio107\seomatic\base\MetaContainer;
16
use nystudio107\seomatic\helpers\ImageTransform as ImageTransformHelper;
17
use nystudio107\seomatic\Seomatic;
18
use yii\caching\TagDependency;
19
20
/**
21
 * @author    nystudio107
22
 * @package   Seomatic
23
 * @since     3.0.0
24
 */
25
class MetaTitleContainer extends MetaContainer
26
{
27
    // Constants
28
    // =========================================================================
29
30
    public const CONTAINER_TYPE = 'MetaTitleContainer';
31
32
    // Public Properties
33
    // =========================================================================
34
35
    /**
36
     * The data in this container
37
     *
38
     * @var MetaTitle[] $data
39
     */
40
    public $data = [];
41
42
    // Public Methods
43
    // =========================================================================
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function includeMetaData($dependency)
49
    {
50
        Craft::beginProfile('MetaTitleContainer::includeMetaData', __METHOD__);
51
        $uniqueKey = $this->handle . $dependency->tags[3];
52
        $cache = Craft::$app->getCache();
53
        if ($this->clearCache) {
54
            TagDependency::invalidate($cache, $dependency->tags[3]);
55
        }
56
        $tagData = $cache->getOrSet(
57
            self::CONTAINER_TYPE . $uniqueKey,
58
            function() use ($uniqueKey) {
59
                Craft::info(
60
                    self::CONTAINER_TYPE . ' cache miss: ' . $uniqueKey,
61
                    __METHOD__
62
                );
63
                $tagData = '';
64
                if ($this->prepForInclusion()) {
65
                    foreach ($this->data as $metaTitleModel) {
66
                        if ($metaTitleModel->include) {
67
                            $title = $metaTitleModel->title;
68
                            if ($metaTitleModel->prepForRender($title)) {
69
                                $tagData = $title;
70
                                // If `devMode` is enabled, validate the Meta Tag and output any model errors
71
                                if (Seomatic::$devMode) {
72
                                    $scenario = [];
73
                                    $scenario['default'] = 'error';
74
                                    $scenario['warning'] = 'warning';
75
                                    $metaTitleModel->debugMetaItem(
76
                                        'Tag attribute: ',
77
                                        $scenario
78
                                    );
79
                                }
80
                            }
81
                        }
82
                    }
83
                }
84
85
                return $tagData;
86
            },
87
            Seomatic::$cacheDuration,
88
            $dependency
89
        );
90
        // Invalidate the cache we just created if there were pending image transforms in it
91
        // or we were asked to clear the cache for this container (because it's a preview request, etc.)
92
        if ($this->clearCache || ImageTransformHelper::$pendingImageTransforms) {
93
            TagDependency::invalidate($cache, $dependency->tags[3]);
94
        }
95
        // Register the tags
96
        Seomatic::$view->title = $tagData;
97
        Craft::endProfile('MetaTitleContainer::includeMetaData', __METHOD__);
98
    }
99
100
    /**
101
     * @inheritdoc
102
     */
103
    public function normalizeContainerData()
104
    {
105
        parent::normalizeContainerData();
106
107
        /** @var array $config */
108
        foreach ($this->data as $key => $config) {
109
            $config['key'] = $key;
110
            $this->data[$key] = MetaTitle::create($config);
0 ignored issues
show
$config of type nystudio107\seomatic\models\MetaTitle is incompatible with the type array expected by parameter $config of nystudio107\seomatic\models\MetaTitle::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
            $this->data[$key] = MetaTitle::create(/** @scrutinizer ignore-type */ $config);
Loading history...
111
        }
112
    }
113
}
114